2 // PukiWiki - Yet another WikiWikiWeb clone.
4 // Copyright 2002-2018 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
7 // Inline-image plugin (Output inline-image tag from a URI)
9 define('PLUGIN_IMG_USAGE', '#img(): Usage: (URI-to-image[,right[,clear]])<br />' . "\n");
10 define('PLUGIN_IMG_CLEAR', '<div style="clear:both"></div>' . "\n"); // Stop word-wrapping
11 if (defined('PKWK_DISABLE_INLINE_IMAGE_FROM_URI') && PKWK_DISABLE_INLINE_IMAGE_FROM_URI) {
12 define('PLUGIN_IMG_SHOW_IMAGE', 0); // 1: Show image, 0: Don't show image
14 define('PLUGIN_IMG_SHOW_IMAGE', 1); // 1: Show image, 0: Don't show image
17 function plugin_img_get_style($args)
20 for ($i = 1; $i <= 3; $i++) {
21 if (isset($args[$i])) {
24 if (preg_match('#^(\d+)x(\d+)$#', $arg, $m)) {
25 $style = 'max-width:' . $m[1] . 'px;max-height:' . $m[2] . 'px;';
27 } else if (preg_match('#^(\d+)px$#', $arg, $m)) {
28 $style = 'max-width:' . $m[1] . 'px;max-height:' . $m[1] . 'px;';
30 } else if (preg_match('#^(\d+)%$#', $arg, $m)) {
31 // Note: zoom is not standard. Recommend using MAXpx or WIDTHxHEIGHT
32 $style = 'zoom:' . $m[1] . '%;';
41 * Determine link or not.
43 function plugin_img_get_islink($args)
45 for ($i = 1; $i <= 4; $i++) {
46 if (isset($args[$i])) {
47 if ($args[$i] === 'nolink') {
56 * @param[in] $args func_get_args() of xxx_inline() and xxx_convert()
57 * @return array(url, is_url, file_path, page, style, a_begin, a_end)
59 function plugin_img_get_props($args)
64 $file_path = isset($args[0]) ? $args[0] : '';
65 $page = isset($vars['page']) ? $vars['page'] : '';
66 if (is_url($file_path)) {
69 } else if (isset($file_path)) {
70 // $file_path s not an URL. It should be attached-file path
72 if (preg_match('#^(.+)/([^/]+)$#', $file_path, $matches)) {
73 // (Page_name/maybe-separated-with/slashes/ATTACHED_FILENAME)
74 if ($matches[1] == '.' || $matches[1] == '..') {
75 $matches[1] .= '/'; // Restore relative paths
77 $attach_name = $matches[2];
78 $attach_page = get_fullname($matches[1], $page);
80 // Simple single argument
81 $attach_name = $file_path;
84 $file = UPLOAD_DIR . encode($attach_page) . '_' . encode($attach_name);
85 $is_file = is_file($file);
87 $url = get_base_uri() . '?plugin=attach' .
88 '&refer=' . rawurlencode($attach_page) .
89 '&openfile=' . rawurlencode($attach_name);
93 $h_url = htmlsc($url);
94 $style = plugin_img_get_style($args);
97 if (plugin_img_get_islink($args)) {
98 $a_begin = "<a href=\"$h_url\" class=\"image-link\">";
101 return (object)array('url' => $url, 'is_url' => $is_url,
102 'file_path' => $file_path, 'is_file' => $is_file,
104 'a_begin' => $a_begin, 'a_end' => $a_end,);
107 function plugin_img_inline()
109 $args = func_get_args();
110 $p = plugin_img_get_props($args);
111 if (!PLUGIN_IMG_SHOW_IMAGE) {
113 $h_url = htmlsc($p->url);
114 $title = '&img(): PLUGIN_IMG_SHOW_IMAGE prohibits this';
115 return "<a href=\"$h_url\" title=\"$title\">$h_url</a>";
117 return '&img(): File not found: ' . htmlsc($p->file_path) . "\n";
120 $h_url = htmlsc($p->url);
122 $a_begin = $p->a_begin;
125 $a_begin<img class="plugin-img-inline" src="$h_url" style="$style" alt="" />$a_end
128 return '&img(): File not found: ' . htmlsc($p->file_path) . "\n";
131 function plugin_img_convert()
133 $args = func_get_args();
134 $p = plugin_img_get_props($args);
135 // Check the 2nd argument first, for compatibility
136 $arg = isset($args[1]) ? strtoupper($args[1]) : '';
137 if ($a->file_path === '' && $arg == 'CLEAR') {
138 // Stop word-wrapping only (Ugly but compatible)
139 // Short usage: #img(,clear)
140 return PLUGIN_IMG_CLEAR;
142 if ($arg === '' || $arg === 'L' || $arg === 'LEFT') {
144 } else if ($arg === 'R' || $arg === 'RIGHT') {
147 $arg2 = isset($args[2]) ? strtoupper($args[2]) : '';
148 $clear = ($arg2 === 'C' || $arg2 === 'CLEAR') ? PLUGIN_IMG_CLEAR : '';
149 if (!PLUGIN_IMG_SHOW_IMAGE) {
151 $h_url = htmlsc($p->url);
152 $title = '#img(): PLUGIN_IMG_SHOW_IMAGE prohibits this';
153 return "<div><a href=\"$h_url\" title=\"$title\">$h_url</a></div>";
155 return '#img(): File not found: ' . htmlsc($p->file_path) . "\n";
158 $h_url = htmlsc($p->url);
160 $a_begin = $p->a_begin;
163 <div style="float:$align;padding:.5em 1.5em .5em 1.5em;">
164 $a_begin<img class="plugin-img-block" src="$h_url" style="$style" alt="" />$a_end
168 return '#img(): File not found: ' . htmlsc($p->file_path) . "\n";