xref: /plugin/bez/renderer/xhtmlmail.php (revision 522c019c0bd8bcd6e522fddaa2cf94cce8e0780d)
1<?php
2
3class renderer_plugin_bez_xhtmlmail extends Doku_Renderer_xhtml {
4
5    /** @var array Settings, control the behavior of the renderer */
6    public $info = array(
7        'cache' => true, // may the rendered result cached?
8        'toc'   => true, // render the TOC?
9        'img'   => array(), //images to attach in mail
10    );
11
12    /**
13     * Our own format
14     *
15     * @return string
16     */
17    function getFormat() {
18        return 'bez_xhtmlmail';
19    }
20
21    /**
22     * Unique media file cid used by Mailer to identify images
23     *
24     * @param string $mediaId
25     * @param string $rev
26     * @param bool   $delimiters wrap emded with '%%'
27     * @return string
28     */
29    private function embed($mediaId, $rev='', $delimiters=false) {
30        $embed = $mediaId . $rev;
31        if ($delimiters) $embed = '%%' . $embed . '%%';
32        return $embed;
33    }
34
35    /**
36     * Render an internal media file
37     *
38     * @param string $src       media ID
39     * @param string $title     descriptive text
40     * @param string $align     left|center|right
41     * @param int    $width     width of media in pixel
42     * @param int    $height    height of media in pixel
43     * @param string $cache     cache|recache|nocache
44     * @param string $linking   linkonly|detail|nolink
45     * @param bool   $return    return HTML instead of adding to $doc
46     * @return void|string writes to doc attribute or returns html depends on $return
47     */
48    function internalmedia($src, $title = null, $align = null, $width = null,
49                           $height = null, $cache = null, $linking = null, $return = false) {
50        global $ID;
51        list($src, $hash) = explode('#', $src, 2);
52        resolve_mediaid(getNS($ID), $src, $exists, $this->date_at, true);
53
54        $noLink = false;
55        $render = ($linking == 'linkonly') ? false : true;
56        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
57        if ($exists) {
58            $rev = $this->_getLastMediaRevisionAt($src);
59            $path = mediaFN($src, $rev);
60            list($ext, $mime) = mimetype($src);
61
62            $this->info['img'][] = array(
63                'path'  => $path,
64                'mime'  => $mime,
65                'name'  => $title,
66                'embed' => $this->embed($src, $rev)
67            );
68        }
69
70        list($ext, $mime) = mimetype($src, false);
71        if(substr($mime, 0, 5) == 'image' && $render) {
72            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src)),
73                ($linking == 'direct'), '&amp;', true);
74        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
75            // don't link movies
76            $noLink = true;
77        } else {
78            // add file icons
79            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
80            $link['class'] .= ' mediafile mf_'.$class;
81            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache , 'rev'=>$this->_getLastMediaRevisionAt($src)),
82                              true, '&amp;', true);
83            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
84        }
85
86        if($hash) $link['url'] .= '#'.$hash;
87
88        //markup non existing files
89        if(!$exists) {
90            $link['class'] .= ' wikilink2';
91        }
92
93        //output formatted
94        if($return) {
95            if($linking == 'nolink' || $noLink) return $link['name'];
96            else return $this->_formatLink($link);
97        } else {
98            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
99            else $this->doc .= $this->_formatLink($link);
100        }
101    }
102
103    /**
104     * Renders internal and external media
105     *
106     * @author Andreas Gohr <andi@splitbrain.org>
107     * @param string $src       media ID
108     * @param string $title     descriptive text
109     * @param string $align     left|center|right
110     * @param int    $width     width of media in pixel
111     * @param int    $height    height of media in pixel
112     * @param string $cache     cache|recache|nocache
113     * @param bool   $render    should the media be embedded inline or just linked
114     * @return string
115     */
116    function _media($src, $title = null, $align = null, $width = null,
117                    $height = null, $cache = null, $render = true) {
118
119        $ret = '';
120
121        list($ext, $mime) = mimetype($src);
122        if(substr($mime, 0, 5) == 'image') {
123            // first get the $title
124            if(!is_null($title)) {
125                $title = $this->_xmlEntities($title);
126            } elseif($ext == 'jpg' || $ext == 'jpeg') {
127                //try to use the caption from IPTC/EXIF
128                require_once(DOKU_INC.'inc/JpegMeta.php');
129                $jpeg = new JpegMeta(mediaFN($src));
130                if($jpeg !== false) $cap = $jpeg->getTitle();
131                if(!empty($cap)) {
132                    $title = $this->_xmlEntities($cap);
133                }
134            }
135            if(!$render) {
136                // if the picture is not supposed to be rendered
137                // return the title of the picture
138                if(!$title) {
139                    // just show the sourcename
140                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
141                }
142                return $title;
143            }
144            //add image tag
145            $rev = $this->_getLastMediaRevisionAt($src);
146            $ret .= '<img src="' . $this->embed($src, $rev, true) . '"';
147            $ret .= ' class="media'.$align.'"';
148
149            if($title) {
150                $ret .= ' title="'.$title.'"';
151                $ret .= ' alt="'.$title.'"';
152            } else {
153                $ret .= ' alt=""';
154            }
155
156            if(!is_null($width))
157                $ret .= ' width="'.$this->_xmlEntities($width).'"';
158
159            if(!is_null($height))
160                $ret .= ' height="'.$this->_xmlEntities($height).'"';
161
162            $ret .= ' />';
163
164        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
165            // first get the $title
166            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
167            if(!$render) {
168                // if the file is not supposed to be rendered
169                // return the title of the file (just the sourcename if there is no title)
170                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
171            }
172
173            $att          = array();
174            $att['class'] = "media$align";
175            if($title) {
176                $att['title'] = $title;
177            }
178
179            if(media_supportedav($mime, 'video')) {
180                //add video
181                $ret .= $this->_video($src, $width, $height, $att);
182            }
183            if(media_supportedav($mime, 'audio')) {
184                //add audio
185                $ret .= $this->_audio($src, $att);
186            }
187
188        } elseif($mime == 'application/x-shockwave-flash') {
189            if(!$render) {
190                // if the flash is not supposed to be rendered
191                // return the title of the flash
192                if(!$title) {
193                    // just show the sourcename
194                    $title = utf8_basename(noNS($src));
195                }
196                return $this->_xmlEntities($title);
197            }
198
199            $att          = array();
200            $att['class'] = "media$align";
201            if($align == 'right') $att['align'] = 'right';
202            if($align == 'left') $att['align'] = 'left';
203            $ret .= html_flashobject(
204                ml($src, array('cache' => $cache), true, '&'), $width, $height,
205                array('quality' => 'high'),
206                null,
207                $att,
208                $this->_xmlEntities($title)
209            );
210        } elseif($title) {
211            // well at least we have a title to display
212            $ret .= $this->_xmlEntities($title);
213        } else {
214            // just show the sourcename
215            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
216        }
217
218        return $ret;
219    }
220}