1<?php
2class Text_Wiki_Render_Xhtml_Image extends Text_Wiki_Render {
3
4    var $conf = array(
5        'base' => '/',
6        'css'  => null,
7        'css_link' => null
8    );
9
10
11    /**
12    *
13    * Renders a token into text matching the requested format.
14    *
15    * @access public
16    *
17    * @param array $options The "options" portion of the token (second
18    * element).
19    *
20    * @return string The text rendered from the token options.
21    *
22    */
23
24    function token($options)
25    {
26        // note the image source
27        $src = $options['src'];
28
29        // is the source a local file or URL?
30        if (strpos($src, '://') === false) {
31            // the source refers to a local file.
32            // add the URL base to it.
33            $src = $this->getConf('base', '/') . $src;
34        }
35
36        // stephane@metacites.net
37        // is the image clickable?
38        if (isset($options['attr']['link'])) {
39            // yes, the image is clickable.
40            // are we linked to a URL or a wiki page?
41            if (strpos($options['attr']['link'], '://')) {
42                // it's a URL
43                $href = $options['attr']['link'];
44            } else {
45                // it's a WikiPage; assume it exists.
46                /** @todo This needs to honor sprintf wikilinks (pmjones) */
47                /** @todo This needs to honor interwiki (pmjones) */
48                /** @todo This needs to honor freelinks (pmjones) */
49                $href = $this->wiki->getRenderConf('xhtml', 'wikilink', 'view_url') .
50                    $options['attr']['link'];
51            }
52        } else {
53            // image is not clickable.
54            $href = null;
55        }
56        // unset so it won't show up as an attribute
57        unset($options['attr']['link']);
58
59        // stephane@metacites.net -- 25/07/2004
60        // we make up an align="center" value for the <img> tag.
61        if (isset($options['attr']['align']) &&
62            $options['attr']['align'] == 'center') {
63
64            // unset so it won't show up as an attribute
65            unset($options['attr']['align']);
66
67            // make sure we have a style attribute
68            if (! isset($options['attr']['style'])) {
69                // no style, set up a blank one
70                $options['attr']['style'] = '';
71            } else {
72                // style exists, add a space
73                $options['attr']['style'] .= ' ';
74            }
75
76            // add a "center" style to the existing style.
77            $options['attr']['style'] .=
78                'display: block; margin-left: auto; margin-right: auto;';
79        }
80
81        // stephane@metacites.net -- 25/07/2004
82        // try to guess width and height
83        if (! isset($options['attr']['width']) &&
84            ! isset($options['attr']['height'])) {
85
86            // does the source refer to a local file or a URL?
87            if (strpos($src,'://')) {
88                // is a URL link
89                $imageFile = $src;
90            } else {
91                // is a local file
92                $imageFile = $_SERVER['DOCUMENT_ROOT'] . $src;
93            }
94
95            // attempt to get the image size
96            $imageSize = @getimagesize($imageFile);
97
98            if (is_array($imageSize)) {
99                $options['attr']['width'] = $imageSize[0];
100                $options['attr']['height'] = $imageSize[1];
101            }
102
103        }
104
105        // start the HTML output
106        $output = '<img src="' . htmlspecialchars($src) . '"';
107
108        // get the CSS class but don't add it yet
109        $css = $this->formatConf(' class="%s"', 'css');
110
111        // add the attributes to the output, and be sure to
112        // track whether or not we find an "alt" attribute
113        $alt = false;
114        foreach ($options['attr'] as $key => $val) {
115
116            // track the 'alt' attribute
117            if (strtolower($key) == 'alt') {
118                $alt = true;
119            }
120
121            // the 'class' attribute overrides the CSS class conf
122            if (strtolower($key) == 'class') {
123                $css = null;
124            }
125
126            $key = htmlspecialchars($key);
127            $val = htmlspecialchars($val);
128            $output .= " $key=\"$val\"";
129        }
130
131        // always add an "alt" attribute per Stephane Solliec
132        if (! $alt) {
133            $alt = htmlspecialchars(basename($options['src']));
134            $output .= " alt=\"$alt\"";
135        }
136
137        // end the image tag with the automatic CSS class (if any)
138        $output .= "$css />";
139
140        // was the image clickable?
141        if ($href) {
142            // yes, add the href and return
143            $href = htmlspecialchars($href);
144            $css = $this->formatConf(' class="%s"', 'css_link');
145            $output = "<a$css href=\"$href\">$output</a>";
146        }
147
148        return $output;
149    }
150}
151?>