1: <?php
2: /**
3: * This class renders an inline image.
4: *
5: * @package Wicked
6: */
7: class Text_Wiki_Render_Xhtml_Image2 extends Text_Wiki_Render
8: {
9: public $conf = array(
10: 'base' => '',
11: 'url_base' => null,
12: 'css' => null,
13: 'css_link' => null
14: );
15:
16: /**
17: * Renders a token into text matching the requested format.
18: *
19: * @param array $options The "options" portion of the token (second
20: * element).
21: *
22: * @return string The text rendered from the token options.
23: */
24: public function token($options)
25: {
26: if (!isset($options['attr']['alt'])) {
27: $options['attr']['alt'] = $options['src'];
28: }
29:
30: if (strpos($options['src'], '://') === false) {
31: if ($options['src'][0] != '/') {
32: if (strpos($options['src'], ':')) {
33: list($page, $options['src']) = explode(':', $options['src'], 2);
34: } else {
35: $page = Horde_Util::getFormData('page');
36: if ($page == 'EditPage') {
37: $page = Horde_Util::getFormData('referrer');
38: }
39: if (empty($page)) {
40: $page = 'Wiki/Home';
41: }
42: }
43: $params = array('page' => $page,
44: 'mime' => '1',
45: 'file' => $options['src']);
46: $options['src'] = Horde_Util::addParameter(Horde::url('view.php', true),
47: $params, null, false);
48: }
49: } else {
50: $options['src'] = Horde_Util::addParameter(Horde::externalUrl($options['src']), 'untrusted', 1, false);
51: }
52:
53: // Send external links through Horde::externalUrl().
54: if (isset($options['attr']['link']) && strpos($options['attr']['link'], '://')) {
55: $href = htmlspecialchars($options['attr']['link']);
56: unset($options['attr']['link']);
57: return Horde::link(Horde::externalUrl($href), $href) . $this->_token($options) . '</a>';
58: } else {
59: return $this->_token($options);
60: }
61: }
62:
63: /**
64: * Render code from Text_Wiki's Image with Horde tweaks (remove
65: * getimagesize call, etc).
66: *
67: * @access private
68: *
69: * @param array $options The "options" portion of the token (second
70: * element).
71: *
72: * @return string The text rendered from the token options.
73: */
74: protected function _token($options)
75: {
76: // note the image source
77: $src = $options['src'];
78:
79: // is the source a local file or URL?
80: if (strpos($src, '://') === false) {
81: // the source refers to a local file.
82: // add the URL base to it.
83: $src = $this->getConf('base', '/') . $src;
84: }
85:
86: // stephane@metacites.net
87: // is the image clickable?
88: if (isset($options['attr']['link'])) {
89: // yes, the image is clickable.
90: // are we linked to a URL or a wiki page?
91: if (strpos($options['attr']['link'], '://')) {
92: // it's a URL, prefix the URL base
93: $href = $this->getConf('url_base') . $options['attr']['link'];
94: } else {
95: // it's a WikiPage; assume it exists.
96: /** @todo This needs to honor sprintf wikilinks (pmjones) */
97: /** @todo This needs to honor interwiki (pmjones) */
98: /** @todo This needs to honor freelinks (pmjones) */
99: $href = $this->wiki->getRenderConf('xhtml', 'wikilink', 'view_url') .
100: $options['attr']['link'];
101: }
102: } else {
103: // image is not clickable.
104: $href = null;
105: }
106: // unset so it won't show up as an attribute
107: unset($options['attr']['link']);
108:
109: // start the HTML output
110: $output = '<img src="' . htmlspecialchars($src) . '"';
111:
112: // get the CSS class but don't add it yet
113: $css = $this->formatConf(' class="%s"', 'css');
114:
115: // add the attributes to the output, and be sure to
116: // track whether or not we find an "alt" attribute
117: $alt = false;
118: foreach ($options['attr'] as $key => $val) {
119:
120: // track the 'alt' attribute
121: if (strtolower($key) == 'alt') {
122: $alt = true;
123: }
124:
125: // the 'class' attribute overrides the CSS class conf
126: if (strtolower($key) == 'class') {
127: $css = null;
128: }
129:
130: $key = htmlspecialchars($key);
131: $val = htmlspecialchars($val);
132: $output .= " $key=\"$val\"";
133: }
134:
135: // always add an "alt" attribute per Stephane Solliec
136: if (!$alt) {
137: $alt = htmlspecialchars(basename($options['src']));
138: $output .= " alt=\"$alt\"";
139: }
140:
141: // end the image tag with the automatic CSS class (if any)
142: $output .= "$css />";
143:
144: // was the image clickable?
145: if ($href) {
146: // yes, add the href and return
147: $href = htmlspecialchars($href);
148: $css = $this->formatConf(' class="%s"', 'css_link');
149: $output = "<a$css href=\"$href\">$output</a>";
150: }
151:
152: return $output;
153: }
154: }
155: