OSDN Git Service

BugTrack/2420 AutoTicketLink - Improve regex and JSON encode
[pukiwiki/pukiwiki.git] / plugin / img.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // img.inc.php
4 // Copyright 2002-2018 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // Inline-image plugin (Output inline-image tag from a URI)
8
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
13 } else {
14         define('PLUGIN_IMG_SHOW_IMAGE', 1); // 1: Show image, 0: Don't show image
15 }
16
17 function plugin_img_get_style($args)
18 {
19         $style = '';
20         for ($i = 1; $i <= 3; $i++) {
21                 if (isset($args[$i])) {
22                         $arg = $args[$i];
23                         $m = null;
24                         if (preg_match('#^(\d+)x(\d+)$#', $arg, $m)) {
25                                 $style = 'max-width:' . $m[1] . 'px;max-height:' . $m[2] . 'px;';
26                                 break;
27                         } else if (preg_match('#^(\d+)px$#', $arg, $m)) {
28                                 $style = 'max-width:' . $m[1] . 'px;max-height:' . $m[1] . 'px;';
29                                 break;
30                         } else if (preg_match('#^(\d+)%$#', $arg, $m)) {
31                                 // Note: zoom is not standard. Recommend using MAXpx or WIDTHxHEIGHT
32                                 $style = 'zoom:' . $m[1] . '%;';
33                                 break;
34                         }
35                 }
36         }
37         return $style;
38 }
39
40 /**
41  * Determine link or not.
42  */
43 function plugin_img_get_islink($args)
44 {
45         for ($i = 1; $i <= 4; $i++) {
46                 if (isset($args[$i])) {
47                         if ($args[$i] === 'nolink') {
48                                 return false;
49                         }
50                 }
51         }
52         return true;
53 }
54
55 /**
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)
58  */
59 function plugin_img_get_props($args)
60 {
61         global $vars;
62         $is_file = false;
63         $is_url = false;
64         $file_path = isset($args[0]) ? $args[0] : '';
65         $page = isset($vars['page']) ? $vars['page'] : '';
66         if (is_url($file_path)) {
67                 $url = $file_path;
68                 $is_url = true;
69         } else if (isset($file_path)) {
70                 // $file_path s not an URL. It should be attached-file path
71                 $matches = null;
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
76                         }
77                         $attach_name = $matches[2];
78                         $attach_page = get_fullname($matches[1], $page);
79                 } else {
80                         // Simple single argument
81                         $attach_name = $file_path;
82                         $attach_page = $page;
83                 }
84                 $file = UPLOAD_DIR . encode($attach_page) . '_' . encode($attach_name);
85                 $is_file = is_file($file);
86                 if ($is_file) {
87                         $url = get_base_uri() . '?plugin=attach' .
88                                 '&refer=' . rawurlencode($attach_page) .
89                                 '&openfile=' . rawurlencode($attach_name);
90                         $is_url = true;
91                 }
92         }
93         $h_url = htmlsc($url);
94         $style = plugin_img_get_style($args);
95         $a_begin = '';
96         $a_end = '';
97         if (plugin_img_get_islink($args)) {
98                 $a_begin = "<a href=\"$h_url\" class=\"image-link\">";
99                 $a_end = '</a>';
100         }
101         return (object)array('url' => $url, 'is_url' => $is_url,
102                 'file_path' => $file_path, 'is_file' => $is_file,
103                 'style' => $style,
104                 'a_begin' => $a_begin, 'a_end' => $a_end,);
105 }
106
107 function plugin_img_inline()
108 {
109         $args = func_get_args();
110         $p = plugin_img_get_props($args);
111         if (!PLUGIN_IMG_SHOW_IMAGE) {
112                 if ($p->is_url) {
113                         $h_url = htmlsc($p->url);
114                         $title = '&amp;img(): PLUGIN_IMG_SHOW_IMAGE prohibits this';
115                         return "<a href=\"$h_url\" title=\"$title\">$h_url</a>";
116                 }
117                 return '&amp;img(): File not found: ' . htmlsc($p->file_path) . "\n";
118         }
119         if ($p->is_url) {
120                 $h_url = htmlsc($p->url);
121                 $style = $p->style;
122                 $a_begin = $p->a_begin;
123                 $a_end = $p->a_end;
124                 return <<<EOD
125 $a_begin<img class="plugin-img-inline" src="$h_url" style="$style" alt="" />$a_end
126 EOD;
127         }
128         return '&amp;img(): File not found: ' . htmlsc($p->file_path) . "\n";
129 }
130
131 function plugin_img_convert()
132 {
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;
141         }
142         if ($arg === '' || $arg === 'L' || $arg === 'LEFT') {
143                 $align = 'left';
144         } else if ($arg === 'R' || $arg === 'RIGHT') {
145                 $align = 'right';
146         }
147         $arg2 = isset($args[2]) ? strtoupper($args[2]) : '';
148         $clear = ($arg2 === 'C' || $arg2 === 'CLEAR') ? PLUGIN_IMG_CLEAR : '';
149         if (!PLUGIN_IMG_SHOW_IMAGE) {
150                 if ($p->is_url) {
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>";
154                 }
155                 return '#img(): File not found: ' . htmlsc($p->file_path) . "\n";
156         }
157         if ($p->is_url) {
158                 $h_url = htmlsc($p->url);
159                 $style = $p->style;
160                 $a_begin = $p->a_begin;
161                 $a_end = $p->a_end;
162                 return <<<EOD
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
165 </div>$clear
166 EOD;
167         }
168         return '#img(): File not found: ' . htmlsc($p->file_path) . "\n";
169 }