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