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