@@ -3968,7 +3968,7 @@ |
||
3968 | 3968 | * @param int|string $options Either a PATHINFO_* constant, |
3969 | 3969 | * or a string name to return only the specified piece |
3970 | 3970 | * |
3971 | - * @return string|array |
|
3971 | + * @return string |
|
3972 | 3972 | */ |
3973 | 3973 | public static function mb_pathinfo($path, $options = null) |
3974 | 3974 | { |
@@ -2059,7 +2059,8 @@ discard block |
||
2059 | 2059 | */ |
2060 | 2060 | public function addrFormat($addr) |
2061 | 2061 | { |
2062 | - if (empty($addr[1])) { // No name provided |
|
2062 | + if (empty($addr[1])) { |
|
2063 | +// No name provided |
|
2063 | 2064 | return $this->secureHeader($addr[0]); |
2064 | 2065 | } |
2065 | 2066 | |
@@ -3425,7 +3426,7 @@ discard block |
||
3425 | 3426 | { |
3426 | 3427 | $this->RecipientsQueue = array_filter( |
3427 | 3428 | $this->RecipientsQueue, |
3428 | - function ($params) use ($kind) { |
|
3429 | + function ($params) use ($kind){ |
|
3429 | 3430 | return $params[0] != $kind; |
3430 | 3431 | } |
3431 | 3432 | ); |
@@ -209,7 +209,7 @@ |
||
209 | 209 | * Connect to a POP3 server. |
210 | 210 | * |
211 | 211 | * @param string $host |
212 | - * @param int|bool $port |
|
212 | + * @param integer $port |
|
213 | 213 | * @param int $tval |
214 | 214 | * |
215 | 215 | * @return bool |
@@ -344,7 +344,8 @@ |
||
344 | 344 | protected function sendString($string) |
345 | 345 | { |
346 | 346 | if ($this->pop_conn) { |
347 | - if ($this->do_debug >= 2) { //Show client messages when debug >= 2 |
|
347 | + if ($this->do_debug >= 2) { |
|
348 | +//Show client messages when debug >= 2 |
|
348 | 349 | echo 'Client -> Server: ', $string; |
349 | 350 | } |
350 | 351 |
@@ -1314,7 +1314,7 @@ |
||
1314 | 1314 | * If no reply has been received yet, it will return null. |
1315 | 1315 | * If no pattern was matched, it will return false. |
1316 | 1316 | * |
1317 | - * @return bool|null|string |
|
1317 | + * @return string |
|
1318 | 1318 | * |
1319 | 1319 | * @see recordLastTransactionID() |
1320 | 1320 | */ |
@@ -666,6 +666,9 @@ discard block |
||
666 | 666 | return json_encode(array('version' => false)); |
667 | 667 | } |
668 | 668 | |
669 | + /** |
|
670 | + * @param string $dir |
|
671 | + */ |
|
669 | 672 | protected function moveUploadFile($file, $dir) { |
670 | 673 | $message = $this->checkUploadedFile($file); |
671 | 674 | |
@@ -697,6 +700,9 @@ discard block |
||
697 | 700 | return "/" . basename($target); |
698 | 701 | } |
699 | 702 | |
703 | + /** |
|
704 | + * @param string $file |
|
705 | + */ |
|
700 | 706 | protected function sendDefaultThumb($file=null) { |
701 | 707 | if ($file !== null) { |
702 | 708 | $ext = file::getExtension($file); |
@@ -873,6 +879,9 @@ discard block |
||
873 | 879 | return ""; |
874 | 880 | } |
875 | 881 | |
882 | + /** |
|
883 | + * @param string $message |
|
884 | + */ |
|
876 | 885 | protected function errorMsg($message, array $data=null) { |
877 | 886 | if (in_array($this->action, array("thumb", "upload", "download", "downloadDir"))) |
878 | 887 | die($this->label($message, $data)); |
@@ -12,23 +12,29 @@ discard block |
||
12 | 12 | * @link http://kcfinder.sunhater.com |
13 | 13 | */ |
14 | 14 | |
15 | -class browser extends uploader { |
|
15 | +class browser extends uploader |
|
16 | +{ |
|
16 | 17 | protected $action; |
17 | 18 | protected $thumbsDir; |
18 | 19 | protected $thumbsTypeDir; |
19 | 20 | |
20 | - public function __construct($modx) { |
|
21 | + public function __construct($modx) |
|
22 | + { |
|
21 | 23 | parent::__construct($modx); |
22 | 24 | |
23 | 25 | if (isset($this->post['dir'])) { |
24 | 26 | $dir = $this->checkInputDir($this->post['dir'], true, false); |
25 | - if ($dir === false) unset($this->post['dir']); |
|
27 | + if ($dir === false) { |
|
28 | + unset($this->post['dir']); |
|
29 | + } |
|
26 | 30 | $this->post['dir'] = $dir; |
27 | 31 | } |
28 | 32 | |
29 | 33 | if (isset($this->get['dir'])) { |
30 | 34 | $dir = $this->checkInputDir($this->get['dir'], true, false); |
31 | - if ($dir === false) unset($this->get['dir']); |
|
35 | + if ($dir === false) { |
|
36 | + unset($this->get['dir']); |
|
37 | + } |
|
32 | 38 | $this->get['dir'] = $dir; |
33 | 39 | } |
34 | 40 | |
@@ -44,8 +50,9 @@ discard block |
||
44 | 50 | !is_dir("$thumbsDir/{$this->type}") && |
45 | 51 | !@mkdir("$thumbsDir/{$this->type}", $this->config['dirPerms']) |
46 | 52 | ) |
47 | - ) |
|
48 | - $this->errorMsg("Cannot access or create thumbnails folder."); |
|
53 | + ) { |
|
54 | + $this->errorMsg("Cannot access or create thumbnails folder."); |
|
55 | + } |
|
49 | 56 | |
50 | 57 | $this->thumbsDir = $thumbsDir; |
51 | 58 | $this->thumbsTypeDir = "$thumbsDir/{$this->type}"; |
@@ -58,23 +65,29 @@ discard block |
||
58 | 65 | |
59 | 66 | if (is_array($files) && count($files)) { |
60 | 67 | $time = time(); |
61 | - foreach ($files as $file) |
|
62 | - if (is_file($file) && ($time - filemtime($file) > 3600)) |
|
68 | + foreach ($files as $file) { |
|
69 | + if (is_file($file) && ($time - filemtime($file) > 3600)) |
|
63 | 70 | unlink($file); |
71 | + } |
|
64 | 72 | } |
65 | 73 | |
66 | 74 | if (isset($this->get['theme']) && |
67 | 75 | ($this->get['theme'] == basename($this->get['theme'])) && |
68 | 76 | is_dir("themes/{$this->get['theme']}") |
69 | - ) |
|
70 | - $this->config['theme'] = $this->get['theme']; |
|
77 | + ) { |
|
78 | + $this->config['theme'] = $this->get['theme']; |
|
79 | + } |
|
71 | 80 | } |
72 | 81 | |
73 | - public function action() { |
|
82 | + public function action() |
|
83 | + { |
|
74 | 84 | $act = isset($this->get['act']) ? $this->get['act'] : "browser"; |
75 | - if(!preg_match('@^[0-9a-zA-Z_]+$@', $act)) $this->errorMsg("Unknown error."); |
|
76 | - if (!method_exists($this, "act_$act")) |
|
77 | - $act = "browser"; |
|
85 | + if(!preg_match('@^[0-9a-zA-Z_]+$@', $act)) { |
|
86 | + $this->errorMsg("Unknown error."); |
|
87 | + } |
|
88 | + if (!method_exists($this, "act_$act")) { |
|
89 | + $act = "browser"; |
|
90 | + } |
|
78 | 91 | $this->action = $act; |
79 | 92 | $method = "act_$act"; |
80 | 93 | |
@@ -82,21 +95,22 @@ discard block |
||
82 | 95 | $message = $this->label("You don't have permissions to browse server."); |
83 | 96 | if (in_array($act, array("browser", "upload")) || |
84 | 97 | (substr($act, 0, 8) == "download") |
85 | - ) |
|
86 | - $this->backMsg($message); |
|
87 | - else { |
|
98 | + ) { |
|
99 | + $this->backMsg($message); |
|
100 | + } else { |
|
88 | 101 | header("Content-Type: text/plain; charset={$this->charset}"); |
89 | 102 | die(json_encode(array('error' => $message))); |
90 | 103 | } |
91 | 104 | } |
92 | 105 | |
93 | - if (!isset($this->session['dir'])) |
|
94 | - $this->session['dir'] = $this->type; |
|
95 | - else { |
|
106 | + if (!isset($this->session['dir'])) { |
|
107 | + $this->session['dir'] = $this->type; |
|
108 | + } else { |
|
96 | 109 | $type = $this->getTypeFromPath($this->session['dir']); |
97 | 110 | $dir = $this->config['uploadDir'] . "/" . $this->session['dir']; |
98 | - if (($type != $this->type) || !is_dir($dir) || !is_readable($dir)) |
|
99 | - $this->session['dir'] = $this->type; |
|
111 | + if (($type != $this->type) || !is_dir($dir) || !is_readable($dir)) { |
|
112 | + $this->session['dir'] = $this->type; |
|
113 | + } |
|
100 | 114 | } |
101 | 115 | $this->session['dir'] = path::normalize($this->session['dir']); |
102 | 116 | |
@@ -106,8 +120,9 @@ discard block |
||
106 | 120 | } elseif ( |
107 | 121 | (substr($act, 0, 8) != "download") && |
108 | 122 | !in_array($act, array("thumb", "upload")) |
109 | - ) |
|
110 | - header("Content-Type: text/plain; charset={$this->charset}"); |
|
123 | + ) { |
|
124 | + header("Content-Type: text/plain; charset={$this->charset}"); |
|
125 | + } |
|
111 | 126 | |
112 | 127 | $return = $this->$method(); |
113 | 128 | echo ($return === true) |
@@ -115,21 +130,25 @@ discard block |
||
115 | 130 | : $return; |
116 | 131 | } |
117 | 132 | |
118 | - protected function act_browser() { |
|
133 | + protected function act_browser() |
|
134 | + { |
|
119 | 135 | if (isset($this->get['dir']) && |
120 | 136 | is_dir("{$this->typeDir}/{$this->get['dir']}") && |
121 | 137 | is_readable("{$this->typeDir}/{$this->get['dir']}") |
122 | - ) |
|
123 | - $this->session['dir'] = path::normalize("{$this->type}/{$this->get['dir']}"); |
|
138 | + ) { |
|
139 | + $this->session['dir'] = path::normalize("{$this->type}/{$this->get['dir']}"); |
|
140 | + } |
|
124 | 141 | |
125 | 142 | return $this->output(); |
126 | 143 | } |
127 | 144 | |
128 | - protected function act_init() { |
|
145 | + protected function act_init() |
|
146 | + { |
|
129 | 147 | $tree = $this->getDirInfo($this->typeDir); |
130 | 148 | $tree['dirs'] = $this->getTree($this->session['dir']); |
131 | - if (!is_array($tree['dirs']) || !count($tree['dirs'])) |
|
132 | - unset($tree['dirs']); |
|
149 | + if (!is_array($tree['dirs']) || !count($tree['dirs'])) { |
|
150 | + unset($tree['dirs']); |
|
151 | + } |
|
133 | 152 | $files = $this->getFiles($this->session['dir']); |
134 | 153 | $dirWritable = dir::isWritable("{$this->config['uploadDir']}/{$this->session['dir']}"); |
135 | 154 | $data = array( |
@@ -140,21 +159,26 @@ discard block |
||
140 | 159 | return json_encode($data); |
141 | 160 | } |
142 | 161 | |
143 | - protected function act_thumb() { |
|
162 | + protected function act_thumb() |
|
163 | + { |
|
144 | 164 | $this->getDir($this->get['dir'], true); |
145 | - if (!isset($this->get['file']) || !isset($this->get['dir'])) |
|
146 | - $this->sendDefaultThumb(); |
|
165 | + if (!isset($this->get['file']) || !isset($this->get['dir'])) { |
|
166 | + $this->sendDefaultThumb(); |
|
167 | + } |
|
147 | 168 | $file = $this->get['file']; |
148 | - if (basename($file) != $file) |
|
149 | - $this->sendDefaultThumb(); |
|
169 | + if (basename($file) != $file) { |
|
170 | + $this->sendDefaultThumb(); |
|
171 | + } |
|
150 | 172 | $file = "{$this->thumbsDir}/{$this->type}/{$this->get['dir']}/$file"; |
151 | 173 | if (!is_file($file) || !is_readable($file)) { |
152 | 174 | $file = "{$this->config['uploadDir']}/{$this->type}/{$this->get['dir']}/" . basename($file); |
153 | - if (!is_file($file) || !is_readable($file)) |
|
154 | - $this->sendDefaultThumb($file); |
|
175 | + if (!is_file($file) || !is_readable($file)) { |
|
176 | + $this->sendDefaultThumb($file); |
|
177 | + } |
|
155 | 178 | $image = image::factory($this->imageDriver, $file); |
156 | - if ($image->initError) |
|
157 | - $this->sendDefaultThumb($file); |
|
179 | + if ($image->initError) { |
|
180 | + $this->sendDefaultThumb($file); |
|
181 | + } |
|
158 | 182 | list($tmp, $tmp, $type) = getimagesize($file); |
159 | 183 | if (in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) && |
160 | 184 | ($image->width <= $this->config['thumbWidth']) && |
@@ -165,17 +189,20 @@ discard block |
||
165 | 189 | ($type == IMAGETYPE_PNG) ? "png" : "jpeg"); |
166 | 190 | $mime = "image/$mime"; |
167 | 191 | httpCache::file($file, $mime); |
168 | - } else |
|
169 | - $this->sendDefaultThumb($file); |
|
192 | + } else { |
|
193 | + $this->sendDefaultThumb($file); |
|
194 | + } |
|
170 | 195 | } |
171 | 196 | httpCache::file($file, "image/jpeg"); |
172 | 197 | } |
173 | 198 | |
174 | - protected function act_expand() { |
|
199 | + protected function act_expand() |
|
200 | + { |
|
175 | 201 | return json_encode(array('dirs' => $this->getDirs($this->postDir()))); |
176 | 202 | } |
177 | 203 | |
178 | - protected function act_chDir() { |
|
204 | + protected function act_chDir() |
|
205 | + { |
|
179 | 206 | $this->postDir(); // Just for existing check |
180 | 207 | $this->session['dir'] = $this->type . "/" . $this->post['dir']; |
181 | 208 | $dirWritable = dir::isWritable("{$this->config['uploadDir']}/{$this->session['dir']}"); |
@@ -185,81 +212,104 @@ discard block |
||
185 | 212 | )); |
186 | 213 | } |
187 | 214 | |
188 | - protected function act_newDir() { |
|
215 | + protected function act_newDir() |
|
216 | + { |
|
189 | 217 | if (!$this->config['access']['dirs']['create'] || |
190 | 218 | !isset($this->post['dir']) || |
191 | 219 | !isset($this->post['newDir']) |
192 | - ) |
|
193 | - $this->errorMsg("Unknown error."); |
|
220 | + ) { |
|
221 | + $this->errorMsg("Unknown error."); |
|
222 | + } |
|
194 | 223 | |
195 | 224 | $dir = $this->postDir(); |
196 | 225 | $newDir = $this->normalizeDirname(trim($this->post['newDir'])); |
197 | - if (!strlen($newDir)) |
|
198 | - $this->errorMsg("Please enter new folder name."); |
|
199 | - if (preg_match('/[\/\\\\]/s', $newDir)) |
|
200 | - $this->errorMsg("Unallowable characters in folder name."); |
|
201 | - if (substr($newDir, 0, 1) == ".") |
|
202 | - $this->errorMsg("Folder name shouldn't begins with '.'"); |
|
203 | - if (file_exists("$dir/$newDir")) |
|
204 | - $this->errorMsg("A file or folder with that name already exists."); |
|
205 | - if (!@mkdir("$dir/$newDir", $this->config['dirPerms'])) |
|
206 | - $this->errorMsg("Cannot create {dir} folder.", array('dir' => $newDir)); |
|
226 | + if (!strlen($newDir)) { |
|
227 | + $this->errorMsg("Please enter new folder name."); |
|
228 | + } |
|
229 | + if (preg_match('/[\/\\\\]/s', $newDir)) { |
|
230 | + $this->errorMsg("Unallowable characters in folder name."); |
|
231 | + } |
|
232 | + if (substr($newDir, 0, 1) == ".") { |
|
233 | + $this->errorMsg("Folder name shouldn't begins with '.'"); |
|
234 | + } |
|
235 | + if (file_exists("$dir/$newDir")) { |
|
236 | + $this->errorMsg("A file or folder with that name already exists."); |
|
237 | + } |
|
238 | + if (!@mkdir("$dir/$newDir", $this->config['dirPerms'])) { |
|
239 | + $this->errorMsg("Cannot create {dir} folder.", array('dir' => $newDir)); |
|
240 | + } |
|
207 | 241 | return true; |
208 | 242 | } |
209 | 243 | |
210 | - protected function act_renameDir() { |
|
244 | + protected function act_renameDir() |
|
245 | + { |
|
211 | 246 | if (!$this->config['access']['dirs']['rename'] || |
212 | 247 | !isset($this->post['dir']) || |
213 | 248 | !isset($this->post['newName']) |
214 | - ) |
|
215 | - $this->errorMsg("Unknown error."); |
|
249 | + ) { |
|
250 | + $this->errorMsg("Unknown error."); |
|
251 | + } |
|
216 | 252 | |
217 | 253 | $dir = $this->postDir(); |
218 | 254 | $newName = $this->normalizeDirname(trim($this->post['newName'])); |
219 | - if (!strlen($newName)) |
|
220 | - $this->errorMsg("Please enter new folder name."); |
|
221 | - if (preg_match('/[\/\\\\]/s', $newName)) |
|
222 | - $this->errorMsg("Unallowable characters in folder name."); |
|
223 | - if (substr($newName, 0, 1) == ".") |
|
224 | - $this->errorMsg("Folder name shouldn't begins with '.'"); |
|
225 | - if (!@rename($dir, dirname($dir) . "/$newName")) |
|
226 | - $this->errorMsg("Cannot rename the folder."); |
|
255 | + if (!strlen($newName)) { |
|
256 | + $this->errorMsg("Please enter new folder name."); |
|
257 | + } |
|
258 | + if (preg_match('/[\/\\\\]/s', $newName)) { |
|
259 | + $this->errorMsg("Unallowable characters in folder name."); |
|
260 | + } |
|
261 | + if (substr($newName, 0, 1) == ".") { |
|
262 | + $this->errorMsg("Folder name shouldn't begins with '.'"); |
|
263 | + } |
|
264 | + if (!@rename($dir, dirname($dir) . "/$newName")) { |
|
265 | + $this->errorMsg("Cannot rename the folder."); |
|
266 | + } |
|
227 | 267 | $thumbDir = "$this->thumbsTypeDir/{$this->post['dir']}"; |
228 | - if (is_dir($thumbDir)) |
|
229 | - @rename($thumbDir, dirname($thumbDir) . "/$newName"); |
|
268 | + if (is_dir($thumbDir)) { |
|
269 | + @rename($thumbDir, dirname($thumbDir) . "/$newName"); |
|
270 | + } |
|
230 | 271 | return json_encode(array('name' => $newName)); |
231 | 272 | } |
232 | 273 | |
233 | - protected function act_deleteDir() { |
|
274 | + protected function act_deleteDir() |
|
275 | + { |
|
234 | 276 | if (!$this->config['access']['dirs']['delete'] || |
235 | 277 | !isset($this->post['dir']) || |
236 | 278 | !strlen(trim($this->post['dir'])) |
237 | - ) |
|
238 | - $this->errorMsg("Unknown error."); |
|
279 | + ) { |
|
280 | + $this->errorMsg("Unknown error."); |
|
281 | + } |
|
239 | 282 | |
240 | 283 | $dir = $this->postDir(); |
241 | 284 | |
242 | - if (!dir::isWritable($dir)) |
|
243 | - $this->errorMsg("Cannot delete the folder."); |
|
285 | + if (!dir::isWritable($dir)) { |
|
286 | + $this->errorMsg("Cannot delete the folder."); |
|
287 | + } |
|
244 | 288 | $result = !dir::prune($dir, false); |
245 | - if (is_array($result) && count($result)) |
|
246 | - $this->errorMsg("Failed to delete {count} files/folders.", |
|
289 | + if (is_array($result) && count($result)) { |
|
290 | + $this->errorMsg("Failed to delete {count} files/folders.", |
|
247 | 291 | array('count' => count($result))); |
292 | + } |
|
248 | 293 | $thumbDir = "$this->thumbsTypeDir/{$this->post['dir']}"; |
249 | - if (is_dir($thumbDir)) dir::prune($thumbDir); |
|
294 | + if (is_dir($thumbDir)) { |
|
295 | + dir::prune($thumbDir); |
|
296 | + } |
|
250 | 297 | return true; |
251 | 298 | } |
252 | 299 | |
253 | - protected function act_upload() { |
|
300 | + protected function act_upload() |
|
301 | + { |
|
254 | 302 | if (!$this->config['access']['files']['upload'] || |
255 | 303 | !isset($this->post['dir']) |
256 | - ) |
|
257 | - $this->errorMsg("Unknown error."); |
|
304 | + ) { |
|
305 | + $this->errorMsg("Unknown error."); |
|
306 | + } |
|
258 | 307 | |
259 | 308 | $dir = $this->postDir(); |
260 | 309 | |
261 | - if (!dir::isWritable($dir)) |
|
262 | - $this->errorMsg("Cannot access or write to upload folder."); |
|
310 | + if (!dir::isWritable($dir)) { |
|
311 | + $this->errorMsg("Cannot access or write to upload folder."); |
|
312 | + } |
|
263 | 313 | |
264 | 314 | if (is_array($this->file['name'])) { |
265 | 315 | $return = array(); |
@@ -271,19 +321,22 @@ discard block |
||
271 | 321 | ), $dir); |
272 | 322 | } |
273 | 323 | return implode("\n", $return); |
274 | - } else |
|
275 | - return $this->moveUploadFile($this->file, $dir); |
|
324 | + } else { |
|
325 | + return $this->moveUploadFile($this->file, $dir); |
|
326 | + } |
|
276 | 327 | } |
277 | 328 | |
278 | - protected function act_download() { |
|
329 | + protected function act_download() |
|
330 | + { |
|
279 | 331 | $dir = $this->postDir(); |
280 | 332 | if (!isset($this->post['dir']) || |
281 | 333 | !isset($this->post['file']) || |
282 | 334 | strpos($this->post['file'],'../')!==false || |
283 | 335 | (false === ($file = "$dir/{$this->post['file']}")) || |
284 | 336 | !file_exists($file) || !is_readable($file) |
285 | - ) |
|
286 | - $this->errorMsg("Unknown error."); |
|
337 | + ) { |
|
338 | + $this->errorMsg("Unknown error."); |
|
339 | + } |
|
287 | 340 | |
288 | 341 | header("Pragma: public"); |
289 | 342 | header("Expires: 0"); |
@@ -297,7 +350,8 @@ discard block |
||
297 | 350 | die; |
298 | 351 | } |
299 | 352 | |
300 | - protected function act_rename() { |
|
353 | + protected function act_rename() |
|
354 | + { |
|
301 | 355 | $dir = $this->postDir(); |
302 | 356 | if (!$this->config['access']['files']['rename'] || |
303 | 357 | !isset($this->post['dir']) || |
@@ -306,42 +360,52 @@ discard block |
||
306 | 360 | !isset($this->post['newName']) || |
307 | 361 | (false === ($file = "$dir/{$this->post['file']}")) || |
308 | 362 | !file_exists($file) || !is_readable($file) || !file::isWritable($file) |
309 | - ) |
|
310 | - $this->errorMsg("Unknown error."); |
|
363 | + ) { |
|
364 | + $this->errorMsg("Unknown error."); |
|
365 | + } |
|
311 | 366 | |
312 | 367 | if (isset($this->config['denyExtensionRename']) && |
313 | 368 | $this->config['denyExtensionRename'] && |
314 | 369 | (file::getExtension($this->post['file'], true) !== |
315 | 370 | file::getExtension($this->post['newName'], true) |
316 | 371 | ) |
317 | - ) |
|
318 | - $this->errorMsg("You cannot rename the extension of files!"); |
|
372 | + ) { |
|
373 | + $this->errorMsg("You cannot rename the extension of files!"); |
|
374 | + } |
|
319 | 375 | |
320 | 376 | $newName = $this->normalizeFilename(trim($this->post['newName'])); |
321 | - if (!strlen($newName)) |
|
322 | - $this->errorMsg("Please enter new file name."); |
|
323 | - if (preg_match('/[\/\\\\]/s', $newName)) |
|
324 | - $this->errorMsg("Unallowable characters in file name."); |
|
325 | - if (substr($newName, 0, 1) == ".") |
|
326 | - $this->errorMsg("File name shouldn't begins with '.'"); |
|
377 | + if (!strlen($newName)) { |
|
378 | + $this->errorMsg("Please enter new file name."); |
|
379 | + } |
|
380 | + if (preg_match('/[\/\\\\]/s', $newName)) { |
|
381 | + $this->errorMsg("Unallowable characters in file name."); |
|
382 | + } |
|
383 | + if (substr($newName, 0, 1) == ".") { |
|
384 | + $this->errorMsg("File name shouldn't begins with '.'"); |
|
385 | + } |
|
327 | 386 | $newName = "$dir/$newName"; |
328 | - if (file_exists($newName)) |
|
329 | - $this->errorMsg("A file or folder with that name already exists."); |
|
387 | + if (file_exists($newName)) { |
|
388 | + $this->errorMsg("A file or folder with that name already exists."); |
|
389 | + } |
|
330 | 390 | $ext = file::getExtension($newName); |
331 | - if (!$this->validateExtension($ext, $this->type)) |
|
332 | - $this->errorMsg("Denied file extension."); |
|
333 | - if (!@rename($file, $newName)) |
|
334 | - $this->errorMsg("Unknown error."); |
|
391 | + if (!$this->validateExtension($ext, $this->type)) { |
|
392 | + $this->errorMsg("Denied file extension."); |
|
393 | + } |
|
394 | + if (!@rename($file, $newName)) { |
|
395 | + $this->errorMsg("Unknown error."); |
|
396 | + } |
|
335 | 397 | |
336 | 398 | $thumbDir = "{$this->thumbsTypeDir}/{$this->post['dir']}"; |
337 | 399 | $thumbFile = "$thumbDir/{$this->post['file']}"; |
338 | 400 | |
339 | - if (file_exists($thumbFile)) |
|
340 | - @rename($thumbFile, "$thumbDir/" . basename($newName)); |
|
401 | + if (file_exists($thumbFile)) { |
|
402 | + @rename($thumbFile, "$thumbDir/" . basename($newName)); |
|
403 | + } |
|
341 | 404 | return true; |
342 | 405 | } |
343 | 406 | |
344 | - protected function act_delete() { |
|
407 | + protected function act_delete() |
|
408 | + { |
|
345 | 409 | $dir = $this->postDir(); |
346 | 410 | if (!$this->config['access']['files']['delete'] || |
347 | 411 | !isset($this->post['dir']) || |
@@ -350,152 +414,184 @@ discard block |
||
350 | 414 | (false === ($file = "$dir/{$this->post['file']}")) || |
351 | 415 | !file_exists($file) || !is_readable($file) || !file::isWritable($file) || |
352 | 416 | !@unlink($file) |
353 | - ) |
|
354 | - $this->errorMsg("Unknown error."); |
|
417 | + ) { |
|
418 | + $this->errorMsg("Unknown error."); |
|
419 | + } |
|
355 | 420 | |
356 | 421 | $thumb = "{$this->thumbsTypeDir}/{$this->post['dir']}/{$this->post['file']}"; |
357 | - if (file_exists($thumb)) @unlink($thumb); |
|
422 | + if (file_exists($thumb)) { |
|
423 | + @unlink($thumb); |
|
424 | + } |
|
358 | 425 | return true; |
359 | 426 | } |
360 | 427 | |
361 | - protected function act_cp_cbd() { |
|
428 | + protected function act_cp_cbd() |
|
429 | + { |
|
362 | 430 | $dir = $this->postDir(); |
363 | 431 | if (!$this->config['access']['files']['copy'] || |
364 | 432 | !isset($this->post['dir']) || |
365 | 433 | !is_dir($dir) || !is_readable($dir) || !dir::isWritable($dir) || |
366 | 434 | !isset($this->post['files']) || !is_array($this->post['files']) || |
367 | 435 | !count($this->post['files']) |
368 | - ) |
|
369 | - $this->errorMsg("Unknown error."); |
|
436 | + ) { |
|
437 | + $this->errorMsg("Unknown error."); |
|
438 | + } |
|
370 | 439 | |
371 | 440 | $error = array(); |
372 | 441 | foreach($this->post['files'] as $file) { |
373 | 442 | $file = path::normalize($file); |
374 | - if (substr($file, 0, 1) == ".") continue; |
|
443 | + if (substr($file, 0, 1) == ".") { |
|
444 | + continue; |
|
445 | + } |
|
375 | 446 | $type = explode("/", $file); |
376 | 447 | $type = $type[0]; |
377 | - if ($type != $this->type) continue; |
|
448 | + if ($type != $this->type) { |
|
449 | + continue; |
|
450 | + } |
|
378 | 451 | $path = "{$this->config['uploadDir']}/$file"; |
379 | 452 | $base = basename($file); |
380 | 453 | $replace = array('file' => $base); |
381 | 454 | $ext = file::getExtension($base); |
382 | - if (!file_exists($path)) |
|
383 | - $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
384 | - elseif (substr($base, 0, 1) == ".") |
|
385 | - $error[] = "$base: " . $this->label("File name shouldn't begins with '.'"); |
|
386 | - elseif (!$this->validateExtension($ext, $type)) |
|
387 | - $error[] = "$base: " . $this->label("Denied file extension."); |
|
388 | - elseif (file_exists("$dir/$base")) |
|
389 | - $error[] = "$base: " . $this->label("A file or folder with that name already exists."); |
|
390 | - elseif (!is_readable($path) || !is_file($path)) |
|
391 | - $error[] = $this->label("Cannot read '{file}'.", $replace); |
|
392 | - elseif (!@copy($path, "$dir/$base")) |
|
393 | - $error[] = $this->label("Cannot copy '{file}'.", $replace); |
|
394 | - else { |
|
395 | - if (function_exists("chmod")) |
|
396 | - @chmod("$dir/$base", $this->config['filePerms']); |
|
455 | + if (!file_exists($path)) { |
|
456 | + $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
457 | + } elseif (substr($base, 0, 1) == ".") { |
|
458 | + $error[] = "$base: " . $this->label("File name shouldn't begins with '.'"); |
|
459 | + } elseif (!$this->validateExtension($ext, $type)) { |
|
460 | + $error[] = "$base: " . $this->label("Denied file extension."); |
|
461 | + } elseif (file_exists("$dir/$base")) { |
|
462 | + $error[] = "$base: " . $this->label("A file or folder with that name already exists."); |
|
463 | + } elseif (!is_readable($path) || !is_file($path)) { |
|
464 | + $error[] = $this->label("Cannot read '{file}'.", $replace); |
|
465 | + } elseif (!@copy($path, "$dir/$base")) { |
|
466 | + $error[] = $this->label("Cannot copy '{file}'.", $replace); |
|
467 | + } else { |
|
468 | + if (function_exists("chmod")) { |
|
469 | + @chmod("$dir/$base", $this->config['filePerms']); |
|
470 | + } |
|
397 | 471 | $fromThumb = "{$this->thumbsDir}/$file"; |
398 | 472 | if (is_file($fromThumb) && is_readable($fromThumb)) { |
399 | 473 | $toThumb = "{$this->thumbsTypeDir}/{$this->post['dir']}"; |
400 | - if (!is_dir($toThumb)) |
|
401 | - @mkdir($toThumb, $this->config['dirPerms'], true); |
|
474 | + if (!is_dir($toThumb)) { |
|
475 | + @mkdir($toThumb, $this->config['dirPerms'], true); |
|
476 | + } |
|
402 | 477 | $toThumb .= "/$base"; |
403 | 478 | @copy($fromThumb, $toThumb); |
404 | 479 | } |
405 | 480 | } |
406 | 481 | } |
407 | - if (count($error)) |
|
408 | - return json_encode(array('error' => $error)); |
|
482 | + if (count($error)) { |
|
483 | + return json_encode(array('error' => $error)); |
|
484 | + } |
|
409 | 485 | return true; |
410 | 486 | } |
411 | 487 | |
412 | - protected function act_mv_cbd() { |
|
488 | + protected function act_mv_cbd() |
|
489 | + { |
|
413 | 490 | $dir = $this->postDir(); |
414 | 491 | if (!$this->config['access']['files']['move'] || |
415 | 492 | !isset($this->post['dir']) || |
416 | 493 | !is_dir($dir) || !is_readable($dir) || !dir::isWritable($dir) || |
417 | 494 | !isset($this->post['files']) || !is_array($this->post['files']) || |
418 | 495 | !count($this->post['files']) |
419 | - ) |
|
420 | - $this->errorMsg("Unknown error."); |
|
496 | + ) { |
|
497 | + $this->errorMsg("Unknown error."); |
|
498 | + } |
|
421 | 499 | |
422 | 500 | $error = array(); |
423 | 501 | foreach($this->post['files'] as $file) { |
424 | 502 | $file = path::normalize($file); |
425 | - if (substr($file, 0, 1) == ".") continue; |
|
503 | + if (substr($file, 0, 1) == ".") { |
|
504 | + continue; |
|
505 | + } |
|
426 | 506 | $type = explode("/", $file); |
427 | 507 | $type = $type[0]; |
428 | - if ($type != $this->type) continue; |
|
508 | + if ($type != $this->type) { |
|
509 | + continue; |
|
510 | + } |
|
429 | 511 | $path = "{$this->config['uploadDir']}/$file"; |
430 | 512 | $base = basename($file); |
431 | 513 | $replace = array('file' => $base); |
432 | 514 | $ext = file::getExtension($base); |
433 | - if (!file_exists($path)) |
|
434 | - $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
435 | - elseif (substr($base, 0, 1) == ".") |
|
436 | - $error[] = "$base: " . $this->label("File name shouldn't begins with '.'"); |
|
437 | - elseif (!$this->validateExtension($ext, $type)) |
|
438 | - $error[] = "$base: " . $this->label("Denied file extension."); |
|
439 | - elseif (file_exists("$dir/$base")) |
|
440 | - $error[] = "$base: " . $this->label("A file or folder with that name already exists."); |
|
441 | - elseif (!is_readable($path) || !is_file($path)) |
|
442 | - $error[] = $this->label("Cannot read '{file}'.", $replace); |
|
443 | - elseif (!file::isWritable($path) || !@rename($path, "$dir/$base")) |
|
444 | - $error[] = $this->label("Cannot move '{file}'.", $replace); |
|
445 | - else { |
|
446 | - if (function_exists("chmod")) |
|
447 | - @chmod("$dir/$base", $this->config['filePerms']); |
|
515 | + if (!file_exists($path)) { |
|
516 | + $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
517 | + } elseif (substr($base, 0, 1) == ".") { |
|
518 | + $error[] = "$base: " . $this->label("File name shouldn't begins with '.'"); |
|
519 | + } elseif (!$this->validateExtension($ext, $type)) { |
|
520 | + $error[] = "$base: " . $this->label("Denied file extension."); |
|
521 | + } elseif (file_exists("$dir/$base")) { |
|
522 | + $error[] = "$base: " . $this->label("A file or folder with that name already exists."); |
|
523 | + } elseif (!is_readable($path) || !is_file($path)) { |
|
524 | + $error[] = $this->label("Cannot read '{file}'.", $replace); |
|
525 | + } elseif (!file::isWritable($path) || !@rename($path, "$dir/$base")) { |
|
526 | + $error[] = $this->label("Cannot move '{file}'.", $replace); |
|
527 | + } else { |
|
528 | + if (function_exists("chmod")) { |
|
529 | + @chmod("$dir/$base", $this->config['filePerms']); |
|
530 | + } |
|
448 | 531 | $fromThumb = "{$this->thumbsDir}/$file"; |
449 | 532 | if (is_file($fromThumb) && is_readable($fromThumb)) { |
450 | 533 | $toThumb = "{$this->thumbsTypeDir}/{$this->post['dir']}"; |
451 | - if (!is_dir($toThumb)) |
|
452 | - @mkdir($toThumb, $this->config['dirPerms'], true); |
|
534 | + if (!is_dir($toThumb)) { |
|
535 | + @mkdir($toThumb, $this->config['dirPerms'], true); |
|
536 | + } |
|
453 | 537 | $toThumb .= "/$base"; |
454 | 538 | @rename($fromThumb, $toThumb); |
455 | 539 | } |
456 | 540 | } |
457 | 541 | } |
458 | - if (count($error)) |
|
459 | - return json_encode(array('error' => $error)); |
|
542 | + if (count($error)) { |
|
543 | + return json_encode(array('error' => $error)); |
|
544 | + } |
|
460 | 545 | return true; |
461 | 546 | } |
462 | 547 | |
463 | - protected function act_rm_cbd() { |
|
548 | + protected function act_rm_cbd() |
|
549 | + { |
|
464 | 550 | if (!$this->config['access']['files']['delete'] || |
465 | 551 | !isset($this->post['files']) || |
466 | 552 | !is_array($this->post['files']) || |
467 | 553 | !count($this->post['files']) |
468 | - ) |
|
469 | - $this->errorMsg("Unknown error."); |
|
554 | + ) { |
|
555 | + $this->errorMsg("Unknown error."); |
|
556 | + } |
|
470 | 557 | |
471 | 558 | $error = array(); |
472 | 559 | foreach($this->post['files'] as $file) { |
473 | 560 | $file = path::normalize($file); |
474 | - if (substr($file, 0, 1) == ".") continue; |
|
561 | + if (substr($file, 0, 1) == ".") { |
|
562 | + continue; |
|
563 | + } |
|
475 | 564 | $type = explode("/", $file); |
476 | 565 | $type = $type[0]; |
477 | - if ($type != $this->type) continue; |
|
566 | + if ($type != $this->type) { |
|
567 | + continue; |
|
568 | + } |
|
478 | 569 | $path = "{$this->config['uploadDir']}/$file"; |
479 | 570 | $base = basename($file); |
480 | 571 | $replace = array('file' => $base); |
481 | - if (!is_file($path)) |
|
482 | - $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
483 | - elseif (!@unlink($path)) |
|
484 | - $error[] = $this->label("Cannot delete '{file}'.", $replace); |
|
485 | - else { |
|
572 | + if (!is_file($path)) { |
|
573 | + $error[] = $this->label("The file '{file}' does not exist.", $replace); |
|
574 | + } elseif (!@unlink($path)) { |
|
575 | + $error[] = $this->label("Cannot delete '{file}'.", $replace); |
|
576 | + } else { |
|
486 | 577 | $thumb = "{$this->thumbsDir}/$file"; |
487 | - if (is_file($thumb)) @unlink($thumb); |
|
578 | + if (is_file($thumb)) { |
|
579 | + @unlink($thumb); |
|
580 | + } |
|
488 | 581 | } |
489 | 582 | } |
490 | - if (count($error)) |
|
491 | - return json_encode(array('error' => $error)); |
|
583 | + if (count($error)) { |
|
584 | + return json_encode(array('error' => $error)); |
|
585 | + } |
|
492 | 586 | return true; |
493 | 587 | } |
494 | 588 | |
495 | - protected function act_downloadDir() { |
|
589 | + protected function act_downloadDir() |
|
590 | + { |
|
496 | 591 | $dir = $this->postDir(); |
497 | - if (!isset($this->post['dir']) || $this->config['denyZipDownload']) |
|
498 | - $this->errorMsg("Unknown error."); |
|
592 | + if (!isset($this->post['dir']) || $this->config['denyZipDownload']) { |
|
593 | + $this->errorMsg("Unknown error."); |
|
594 | + } |
|
499 | 595 | $filename = basename($dir) . ".zip"; |
500 | 596 | do { |
501 | 597 | $file = md5(time() . session_id()); |
@@ -510,23 +606,27 @@ discard block |
||
510 | 606 | die; |
511 | 607 | } |
512 | 608 | |
513 | - protected function act_downloadSelected() { |
|
609 | + protected function act_downloadSelected() |
|
610 | + { |
|
514 | 611 | $dir = $this->postDir(); |
515 | 612 | if (!isset($this->post['dir']) || |
516 | 613 | !isset($this->post['files']) || |
517 | 614 | !is_array($this->post['files']) || |
518 | 615 | $this->config['denyZipDownload'] |
519 | - ) |
|
520 | - $this->errorMsg("Unknown error."); |
|
616 | + ) { |
|
617 | + $this->errorMsg("Unknown error."); |
|
618 | + } |
|
521 | 619 | |
522 | 620 | $zipFiles = array(); |
523 | 621 | foreach ($this->post['files'] as $file) { |
524 | 622 | $file = path::normalize($file); |
525 | - if ((substr($file, 0, 1) == ".") || (strpos($file, '/') !== false)) |
|
526 | - continue; |
|
623 | + if ((substr($file, 0, 1) == ".") || (strpos($file, '/') !== false)) { |
|
624 | + continue; |
|
625 | + } |
|
527 | 626 | $file = "$dir/$file"; |
528 | - if (!is_file($file) || !is_readable($file)) |
|
529 | - continue; |
|
627 | + if (!is_file($file) || !is_readable($file)) { |
|
628 | + continue; |
|
629 | + } |
|
530 | 630 | $zipFiles[] = $file; |
531 | 631 | } |
532 | 632 | |
@@ -538,8 +638,9 @@ discard block |
||
538 | 638 | $zip = new ZipArchive(); |
539 | 639 | $res = $zip->open($file, ZipArchive::CREATE); |
540 | 640 | if ($res === TRUE) { |
541 | - foreach ($zipFiles as $cfile) |
|
542 | - $zip->addFile($cfile, basename($cfile)); |
|
641 | + foreach ($zipFiles as $cfile) { |
|
642 | + $zip->addFile($cfile, basename($cfile)); |
|
643 | + } |
|
543 | 644 | $zip->close(); |
544 | 645 | } |
545 | 646 | header("Content-Type: application/x-zip"); |
@@ -550,25 +651,30 @@ discard block |
||
550 | 651 | die; |
551 | 652 | } |
552 | 653 | |
553 | - protected function act_downloadClipboard() { |
|
654 | + protected function act_downloadClipboard() |
|
655 | + { |
|
554 | 656 | if (!isset($this->post['files']) || |
555 | 657 | !is_array($this->post['files']) || |
556 | 658 | $this->config['denyZipDownload'] |
557 | - ) |
|
558 | - $this->errorMsg("Unknown error."); |
|
659 | + ) { |
|
660 | + $this->errorMsg("Unknown error."); |
|
661 | + } |
|
559 | 662 | |
560 | 663 | $zipFiles = array(); |
561 | 664 | foreach ($this->post['files'] as $file) { |
562 | 665 | $file = path::normalize($file); |
563 | - if ((substr($file, 0, 1) == ".")) |
|
564 | - continue; |
|
666 | + if ((substr($file, 0, 1) == ".")) { |
|
667 | + continue; |
|
668 | + } |
|
565 | 669 | $type = explode("/", $file); |
566 | 670 | $type = $type[0]; |
567 | - if ($type != $this->type) |
|
568 | - continue; |
|
671 | + if ($type != $this->type) { |
|
672 | + continue; |
|
673 | + } |
|
569 | 674 | $file = $this->config['uploadDir'] . "/$file"; |
570 | - if (!is_file($file) || !is_readable($file)) |
|
571 | - continue; |
|
675 | + if (!is_file($file) || !is_readable($file)) { |
|
676 | + continue; |
|
677 | + } |
|
572 | 678 | $zipFiles[] = $file; |
573 | 679 | } |
574 | 680 | |
@@ -580,8 +686,9 @@ discard block |
||
580 | 686 | $zip = new ZipArchive(); |
581 | 687 | $res = $zip->open($file, ZipArchive::CREATE); |
582 | 688 | if ($res === TRUE) { |
583 | - foreach ($zipFiles as $cfile) |
|
584 | - $zip->addFile($cfile, basename($cfile)); |
|
689 | + foreach ($zipFiles as $cfile) { |
|
690 | + $zip->addFile($cfile, basename($cfile)); |
|
691 | + } |
|
585 | 692 | $zip->close(); |
586 | 693 | } |
587 | 694 | header("Content-Type: application/x-zip"); |
@@ -592,16 +699,19 @@ discard block |
||
592 | 699 | die; |
593 | 700 | } |
594 | 701 | |
595 | - protected function act_check4Update() { |
|
596 | - if ($this->config['denyUpdateCheck']) |
|
597 | - return json_encode(array('version' => false)); |
|
702 | + protected function act_check4Update() |
|
703 | + { |
|
704 | + if ($this->config['denyUpdateCheck']) { |
|
705 | + return json_encode(array('version' => false)); |
|
706 | + } |
|
598 | 707 | |
599 | 708 | // Caching HTTP request for 6 hours |
600 | 709 | if (isset($this->session['checkVersion']) && |
601 | 710 | isset($this->session['checkVersionTime']) && |
602 | 711 | ((time() - $this->session['checkVersionTime']) < 21600) |
603 | - ) |
|
604 | - return json_encode(array('version' => $this->session['checkVersion'])); |
|
712 | + ) { |
|
713 | + return json_encode(array('version' => $this->session['checkVersion'])); |
|
714 | + } |
|
605 | 715 | |
606 | 716 | $protocol = "http"; |
607 | 717 | $host = "kcfinder.sunhater.com"; |
@@ -651,27 +761,32 @@ discard block |
||
651 | 761 | (false !== @socket_write($socket, $cmd, strlen($cmd)) ) && |
652 | 762 | (false !== ( $ver = @socket_read($socket, 2048) )) && |
653 | 763 | preg_match($responsePattern, $ver, $match) |
654 | - ) |
|
655 | - $ver = $match[2]; |
|
764 | + ) { |
|
765 | + $ver = $match[2]; |
|
766 | + } |
|
656 | 767 | |
657 | - if (isset($socket) && is_resource($socket)) |
|
658 | - @socket_close($socket); |
|
768 | + if (isset($socket) && is_resource($socket)) { |
|
769 | + @socket_close($socket); |
|
770 | + } |
|
659 | 771 | } |
660 | 772 | |
661 | 773 | if (isset($ver) && preg_match($pattern, $ver)) { |
662 | 774 | $this->session['checkVersion'] = $ver; |
663 | 775 | $this->session['checkVersionTime'] = time(); |
664 | 776 | return json_encode(array('version' => $ver)); |
665 | - } else |
|
666 | - return json_encode(array('version' => false)); |
|
777 | + } else { |
|
778 | + return json_encode(array('version' => false)); |
|
779 | + } |
|
667 | 780 | } |
668 | 781 | |
669 | - protected function moveUploadFile($file, $dir) { |
|
782 | + protected function moveUploadFile($file, $dir) |
|
783 | + { |
|
670 | 784 | $message = $this->checkUploadedFile($file); |
671 | 785 | |
672 | 786 | if ($message !== true) { |
673 | - if (isset($file['tmp_name'])) |
|
674 | - @unlink($file['tmp_name']); |
|
787 | + if (isset($file['tmp_name'])) { |
|
788 | + @unlink($file['tmp_name']); |
|
789 | + } |
|
675 | 790 | return "{$file['name']}: $message"; |
676 | 791 | } |
677 | 792 | |
@@ -684,8 +799,9 @@ discard block |
||
684 | 799 | ) { |
685 | 800 | @unlink($file['tmp_name']); |
686 | 801 | return "{$file['name']}: " . $this->label("Cannot move uploaded file to target folder."); |
687 | - } elseif (function_exists('chmod')) |
|
688 | - chmod($target, $this->config['filePerms']); |
|
802 | + } elseif (function_exists('chmod')) { |
|
803 | + chmod($target, $this->config['filePerms']); |
|
804 | + } |
|
689 | 805 | |
690 | 806 | $this->modx->invokeEvent('OnFileBrowserUpload',array( |
691 | 807 | 'filepath'=>realpath($dir), |
@@ -697,25 +813,29 @@ discard block |
||
697 | 813 | return "/" . basename($target); |
698 | 814 | } |
699 | 815 | |
700 | - protected function sendDefaultThumb($file=null) { |
|
816 | + protected function sendDefaultThumb($file=null) |
|
817 | + { |
|
701 | 818 | if ($file !== null) { |
702 | 819 | $ext = file::getExtension($file); |
703 | 820 | $thumb = "themes/{$this->config['theme']}/img/files/big/$ext.png"; |
704 | 821 | } |
705 | - if (!isset($thumb) || !file_exists($thumb)) |
|
706 | - $thumb = "themes/{$this->config['theme']}/img/files/big/..png"; |
|
822 | + if (!isset($thumb) || !file_exists($thumb)) { |
|
823 | + $thumb = "themes/{$this->config['theme']}/img/files/big/..png"; |
|
824 | + } |
|
707 | 825 | header("Content-Type: image/png"); |
708 | 826 | readfile($thumb); |
709 | 827 | die; |
710 | 828 | } |
711 | 829 | |
712 | - protected function getFiles($dir) { |
|
830 | + protected function getFiles($dir) |
|
831 | + { |
|
713 | 832 | $thumbDir = "{$this->config['uploadDir']}/{$this->config['thumbsDir']}/$dir"; |
714 | 833 | $dir = "{$this->config['uploadDir']}/$dir"; |
715 | 834 | $return = array(); |
716 | 835 | $files = dir::content($dir, array('types' => "file")); |
717 | - if ($files === false) |
|
718 | - return $return; |
|
836 | + if ($files === false) { |
|
837 | + return $return; |
|
838 | + } |
|
719 | 839 | |
720 | 840 | foreach ($files as $file) { |
721 | 841 | $ext = file::getExtension($file); |
@@ -724,8 +844,9 @@ discard block |
||
724 | 844 | $size = @getimagesize($file); |
725 | 845 | if (is_array($size) && count($size)) { |
726 | 846 | $thumb_file = "$thumbDir/" . basename($file); |
727 | - if (!is_file($thumb_file) || filemtime($file) > filemtime($thumb_file)) |
|
728 | - $this->makeThumb($file); |
|
847 | + if (!is_file($thumb_file) || filemtime($file) > filemtime($thumb_file)) { |
|
848 | + $this->makeThumb($file); |
|
849 | + } |
|
729 | 850 | $smallThumb = |
730 | 851 | ($size[0] <= $this->config['thumbWidth']) && |
731 | 852 | ($size[1] <= $this->config['thumbHeight']) && |
@@ -733,12 +854,18 @@ discard block |
||
733 | 854 | } |
734 | 855 | } |
735 | 856 | $stat = stat($file); |
736 | - if ($stat === false) continue; |
|
857 | + if ($stat === false) { |
|
858 | + continue; |
|
859 | + } |
|
737 | 860 | $name = basename($file); |
738 | 861 | $types = $this->config['types']; |
739 | 862 | $types = explode(' ',$types['images'].' '.$types['image']); |
740 | - if (substr($name,0,1) == '.' && !$this->config['showHiddenFiles']) continue; |
|
741 | - if ($this->type == 'images' && !in_array(strtolower($ext),$types)) continue; |
|
863 | + if (substr($name,0,1) == '.' && !$this->config['showHiddenFiles']) { |
|
864 | + continue; |
|
865 | + } |
|
866 | + if ($this->type == 'images' && !in_array(strtolower($ext),$types)) { |
|
867 | + continue; |
|
868 | + } |
|
742 | 869 | $bigIcon = file_exists("themes/{$this->config['theme']}/img/files/big/$ext.png"); |
743 | 870 | $smallIcon = file_exists("themes/{$this->config['theme']}/img/files/small/$ext.png"); |
744 | 871 | $thumb = file_exists("$thumbDir/$name"); |
@@ -758,14 +885,17 @@ discard block |
||
758 | 885 | return $return; |
759 | 886 | } |
760 | 887 | |
761 | - protected function getTree($dir, $index=0) { |
|
888 | + protected function getTree($dir, $index=0) |
|
889 | + { |
|
762 | 890 | $path = explode("/", $dir); |
763 | 891 | |
764 | 892 | $pdir = ""; |
765 | - for ($i = 0; ($i <= $index && $i < count($path)); $i++) |
|
766 | - $pdir .= "/{$path[$i]}"; |
|
767 | - if (strlen($pdir)) |
|
768 | - $pdir = substr($pdir, 1); |
|
893 | + for ($i = 0; ($i <= $index && $i < count($path)); $i++) { |
|
894 | + $pdir .= "/{$path[$i]}"; |
|
895 | + } |
|
896 | + if (strlen($pdir)) { |
|
897 | + $pdir = substr($pdir, 1); |
|
898 | + } |
|
769 | 899 | |
770 | 900 | $fdir = "{$this->config['uploadDir']}/$pdir"; |
771 | 901 | |
@@ -787,38 +917,48 @@ discard block |
||
787 | 917 | } |
788 | 918 | } |
789 | 919 | } |
790 | - } else |
|
791 | - return false; |
|
920 | + } else { |
|
921 | + return false; |
|
922 | + } |
|
792 | 923 | |
793 | 924 | return $dirs; |
794 | 925 | } |
795 | 926 | |
796 | - protected function postDir($existent=true) { |
|
927 | + protected function postDir($existent=true) |
|
928 | + { |
|
797 | 929 | $dir = $this->typeDir; |
798 | - if (isset($this->post['dir'])) |
|
799 | - $dir .= "/" . $this->post['dir']; |
|
800 | - if ($existent && (!is_dir($dir) || !is_readable($dir))) |
|
801 | - $this->errorMsg("Inexistant or inaccessible folder."); |
|
930 | + if (isset($this->post['dir'])) { |
|
931 | + $dir .= "/" . $this->post['dir']; |
|
932 | + } |
|
933 | + if ($existent && (!is_dir($dir) || !is_readable($dir))) { |
|
934 | + $this->errorMsg("Inexistant or inaccessible folder."); |
|
935 | + } |
|
802 | 936 | return $dir; |
803 | 937 | } |
804 | 938 | |
805 | - protected function getDir($existent=true) { |
|
939 | + protected function getDir($existent=true) |
|
940 | + { |
|
806 | 941 | $dir = $this->typeDir; |
807 | - if (isset($this->get['dir'])) |
|
808 | - $dir .= "/" . $this->get['dir']; |
|
809 | - if ($existent && (!is_dir($dir) || !is_readable($dir))) |
|
810 | - $this->errorMsg("Inexistant or inaccessible folder."); |
|
942 | + if (isset($this->get['dir'])) { |
|
943 | + $dir .= "/" . $this->get['dir']; |
|
944 | + } |
|
945 | + if ($existent && (!is_dir($dir) || !is_readable($dir))) { |
|
946 | + $this->errorMsg("Inexistant or inaccessible folder."); |
|
947 | + } |
|
811 | 948 | return $dir; |
812 | 949 | } |
813 | 950 | |
814 | - protected function getDirs($dir) { |
|
951 | + protected function getDirs($dir) |
|
952 | + { |
|
815 | 953 | $dirs = dir::content($dir, array('types' => "dir")); |
816 | 954 | $return = array(); |
817 | 955 | if (is_array($dirs)) { |
818 | 956 | $writable = dir::isWritable($dir); |
819 | 957 | foreach ($dirs as $cdir) { |
820 | 958 | $info = $this->getDirInfo($cdir); |
821 | - if ($info === false) continue; |
|
959 | + if ($info === false) { |
|
960 | + continue; |
|
961 | + } |
|
822 | 962 | $info['removable'] = $writable && $info['writable']; |
823 | 963 | $return[] = $info; |
824 | 964 | } |
@@ -826,17 +966,21 @@ discard block |
||
826 | 966 | return $return; |
827 | 967 | } |
828 | 968 | |
829 | - protected function getDirInfo($dir, $removable=false) { |
|
830 | - if ((substr(basename($dir), 0, 1) == ".") || !is_dir($dir) || !is_readable($dir)) |
|
831 | - return false; |
|
969 | + protected function getDirInfo($dir, $removable=false) |
|
970 | + { |
|
971 | + if ((substr(basename($dir), 0, 1) == ".") || !is_dir($dir) || !is_readable($dir)) { |
|
972 | + return false; |
|
973 | + } |
|
832 | 974 | $dirs = dir::content($dir, array('types' => "dir")); |
833 | 975 | if (is_array($dirs)) { |
834 | - foreach ($dirs as $key => $cdir) |
|
835 | - if (substr(basename($cdir), 0, 1) == ".") |
|
976 | + foreach ($dirs as $key => $cdir) { |
|
977 | + if (substr(basename($cdir), 0, 1) == ".") |
|
836 | 978 | unset($dirs[$key]); |
979 | + } |
|
837 | 980 | $hasDirs = count($dirs) ? true : false; |
838 | - } else |
|
839 | - $hasDirs = false; |
|
981 | + } else { |
|
982 | + $hasDirs = false; |
|
983 | + } |
|
840 | 984 | |
841 | 985 | $writable = dir::isWritable($dir); |
842 | 986 | $info = array( |
@@ -847,24 +991,30 @@ discard block |
||
847 | 991 | 'hasDirs' => $hasDirs |
848 | 992 | ); |
849 | 993 | |
850 | - if ($dir == "{$this->config['uploadDir']}/{$this->session['dir']}") |
|
851 | - $info['current'] = true; |
|
994 | + if ($dir == "{$this->config['uploadDir']}/{$this->session['dir']}") { |
|
995 | + $info['current'] = true; |
|
996 | + } |
|
852 | 997 | |
853 | 998 | return $info; |
854 | 999 | } |
855 | 1000 | |
856 | - protected function output($data=null, $template=null) { |
|
857 | - if (!is_array($data)) $data = array(); |
|
858 | - if ($template === null) |
|
859 | - $template = $this->action; |
|
1001 | + protected function output($data=null, $template=null) |
|
1002 | + { |
|
1003 | + if (!is_array($data)) { |
|
1004 | + $data = array(); |
|
1005 | + } |
|
1006 | + if ($template === null) { |
|
1007 | + $template = $this->action; |
|
1008 | + } |
|
860 | 1009 | |
861 | 1010 | if (file_exists("tpl/tpl_$template.php")) { |
862 | 1011 | ob_start(); |
863 | 1012 | $eval = "unset(\$data);unset(\$template);unset(\$eval);"; |
864 | 1013 | $_ = $data; |
865 | - foreach (array_keys($data) as $key) |
|
866 | - if (preg_match('/^[a-z\d_]+$/i', $key)) |
|
1014 | + foreach (array_keys($data) as $key) { |
|
1015 | + if (preg_match('/^[a-z\d_]+$/i', $key)) |
|
867 | 1016 | $eval .= "\$$key=\$_['$key'];"; |
1017 | + } |
|
868 | 1018 | $eval .= "unset(\$_);require \"tpl/tpl_$template.php\";"; |
869 | 1019 | eval($eval); |
870 | 1020 | return ob_get_clean(); |
@@ -873,12 +1023,14 @@ discard block |
||
873 | 1023 | return ""; |
874 | 1024 | } |
875 | 1025 | |
876 | - protected function errorMsg($message, array $data=null) { |
|
877 | - if (in_array($this->action, array("thumb", "upload", "download", "downloadDir"))) |
|
878 | - die($this->label($message, $data)); |
|
879 | - if (($this->action === null) || ($this->action == "browser")) |
|
880 | - $this->backMsg($message, $data); |
|
881 | - else { |
|
1026 | + protected function errorMsg($message, array $data=null) |
|
1027 | + { |
|
1028 | + if (in_array($this->action, array("thumb", "upload", "download", "downloadDir"))) { |
|
1029 | + die($this->label($message, $data)); |
|
1030 | + } |
|
1031 | + if (($this->action === null) || ($this->action == "browser")) { |
|
1032 | + $this->backMsg($message, $data); |
|
1033 | + } else { |
|
882 | 1034 | $message = $this->label($message, $data); |
883 | 1035 | die(json_encode(array('error' => $message))); |
884 | 1036 | } |
@@ -385,6 +385,9 @@ discard block |
||
385 | 385 | } |
386 | 386 | |
387 | 387 | |
388 | + /** |
|
389 | + * @return string |
|
390 | + */ |
|
388 | 391 | protected function normalizeFilename($filename) { |
389 | 392 | if ($this->getTransaliasSettings()) { |
390 | 393 | $format = strrchr($filename, "."); |
@@ -394,6 +397,9 @@ discard block |
||
394 | 397 | return $filename; |
395 | 398 | } |
396 | 399 | |
400 | + /** |
|
401 | + * @param string $dirname |
|
402 | + */ |
|
397 | 403 | protected function normalizeDirname($dirname) { |
398 | 404 | return $this->modx->stripAlias($dirname); |
399 | 405 | } |
@@ -506,6 +512,9 @@ discard block |
||
506 | 512 | return (is_dir($path) && is_readable($path)) ? $return : false; |
507 | 513 | } |
508 | 514 | |
515 | + /** |
|
516 | + * @param string $ext |
|
517 | + */ |
|
509 | 518 | protected function validateExtension($ext, $type) { |
510 | 519 | $ext = trim(strtolower($ext)); |
511 | 520 | if (!isset($this->types[$type])) |
@@ -531,11 +540,17 @@ discard block |
||
531 | 540 | return in_array($ext, $exts); |
532 | 541 | } |
533 | 542 | |
543 | + /** |
|
544 | + * @return string |
|
545 | + */ |
|
534 | 546 | protected function getTypeFromPath($path) { |
535 | 547 | return preg_match('/^([^\/]*)\/.*$/', $path, $patt) |
536 | 548 | ? $patt[1] : $path; |
537 | 549 | } |
538 | 550 | |
551 | + /** |
|
552 | + * @param string $path |
|
553 | + */ |
|
539 | 554 | protected function removeTypeFromPath($path) { |
540 | 555 | return preg_match('/^[^\/]*\/(.*)$/', $path, $patt) |
541 | 556 | ? $patt[1] : ""; |
@@ -728,6 +743,9 @@ discard block |
||
728 | 743 | die; |
729 | 744 | } |
730 | 745 | |
746 | + /** |
|
747 | + * @param string $url |
|
748 | + */ |
|
731 | 749 | protected function callBack($url, $message="") { |
732 | 750 | $message = text::jsValue($message); |
733 | 751 | $CKfuncNum = isset($this->opener['CKEditor']['funcNum']) |
@@ -12,7 +12,8 @@ discard block |
||
12 | 12 | * @link http://kcfinder.sunhater.com |
13 | 13 | */ |
14 | 14 | |
15 | -class uploader { |
|
15 | +class uploader |
|
16 | +{ |
|
16 | 17 | |
17 | 18 | /** Release version */ |
18 | 19 | const VERSION = "2.54"; |
@@ -104,17 +105,21 @@ discard block |
||
104 | 105 | /** Magic method which allows read-only access to protected or private class properties |
105 | 106 | * @param string $property |
106 | 107 | * @return mixed */ |
107 | - public function __get($property) { |
|
108 | + public function __get($property) |
|
109 | + { |
|
108 | 110 | return property_exists($this, $property) ? $this->$property : null; |
109 | 111 | } |
110 | 112 | |
111 | - public function __construct($modx) { |
|
113 | + public function __construct($modx) |
|
114 | + { |
|
112 | 115 | |
113 | 116 | //MODX |
114 | 117 | try { |
115 | 118 | if ($modx instanceof DocumentParser) { |
116 | 119 | $this->modx = $modx; |
117 | - } else throw new Exception('MODX should be instance of DocumentParser'); |
|
120 | + } else { |
|
121 | + throw new Exception('MODX should be instance of DocumentParser'); |
|
122 | + } |
|
118 | 123 | } catch (Exception $e) { |
119 | 124 | die($e->getMessage()); |
120 | 125 | } |
@@ -129,26 +134,34 @@ discard block |
||
129 | 134 | // SET CMS INTEGRATION ATTRIBUTE |
130 | 135 | if (isset($this->get['cms']) && |
131 | 136 | in_array($this->get['cms'], array("drupal")) |
132 | - ) |
|
133 | - $this->cms = $this->get['cms']; |
|
137 | + ) { |
|
138 | + $this->cms = $this->get['cms']; |
|
139 | + } |
|
134 | 140 | |
135 | 141 | // LINKING UPLOADED FILE |
136 | - if (count($_FILES)) |
|
137 | - $this->file = &$_FILES[key($_FILES)]; |
|
142 | + if (count($_FILES)) { |
|
143 | + $this->file = &$_FILES[key($_FILES)]; |
|
144 | + } |
|
138 | 145 | |
139 | 146 | // LOAD DEFAULT CONFIGURATION |
140 | 147 | require "config.php"; |
141 | 148 | |
142 | 149 | // SETTING UP SESSION |
143 | - if (isset($_CONFIG['_sessionLifetime'])) |
|
144 | - ini_set('session.gc_maxlifetime', $_CONFIG['_sessionLifetime'] * 60); |
|
145 | - if (isset($_CONFIG['_sessionDir'])) |
|
146 | - ini_set('session.save_path', $_CONFIG['_sessionDir']); |
|
147 | - if (isset($_CONFIG['_sessionDomain'])) |
|
148 | - ini_set('session.cookie_domain', $_CONFIG['_sessionDomain']); |
|
150 | + if (isset($_CONFIG['_sessionLifetime'])) { |
|
151 | + ini_set('session.gc_maxlifetime', $_CONFIG['_sessionLifetime'] * 60); |
|
152 | + } |
|
153 | + if (isset($_CONFIG['_sessionDir'])) { |
|
154 | + ini_set('session.save_path', $_CONFIG['_sessionDir']); |
|
155 | + } |
|
156 | + if (isset($_CONFIG['_sessionDomain'])) { |
|
157 | + ini_set('session.cookie_domain', $_CONFIG['_sessionDomain']); |
|
158 | + } |
|
149 | 159 | switch ($this->cms) { |
150 | 160 | case "drupal": break; |
151 | - default: if (!session_id()) session_start(); break; |
|
161 | + default: if (!session_id()) { |
|
162 | + session_start(); |
|
163 | + } |
|
164 | + break; |
|
152 | 165 | } |
153 | 166 | |
154 | 167 | // RELOAD DEFAULT CONFIGURATION |
@@ -159,31 +172,37 @@ discard block |
||
159 | 172 | if (isset($_CONFIG['_sessionVar']) && |
160 | 173 | is_array($_CONFIG['_sessionVar']) |
161 | 174 | ) { |
162 | - foreach ($_CONFIG['_sessionVar'] as $key => $val) |
|
163 | - if ((substr($key, 0, 1) != "_") && isset($_CONFIG[$key])) |
|
175 | + foreach ($_CONFIG['_sessionVar'] as $key => $val) { |
|
176 | + if ((substr($key, 0, 1) != "_") && isset($_CONFIG[$key])) |
|
164 | 177 | $this->config[$key] = $val; |
165 | - if (!isset($this->config['_sessionVar']['self'])) |
|
166 | - $this->config['_sessionVar']['self'] = array(); |
|
178 | + } |
|
179 | + if (!isset($this->config['_sessionVar']['self'])) { |
|
180 | + $this->config['_sessionVar']['self'] = array(); |
|
181 | + } |
|
167 | 182 | $this->session = &$this->config['_sessionVar']['self']; |
168 | - } else |
|
169 | - $this->session = &$_SESSION; |
|
183 | + } else { |
|
184 | + $this->session = &$_SESSION; |
|
185 | + } |
|
170 | 186 | |
171 | 187 | // IMAGE DRIVER INIT |
172 | 188 | if (isset($this->config['imageDriversPriority'])) { |
173 | 189 | $this->config['imageDriversPriority'] = |
174 | 190 | text::clearWhitespaces($this->config['imageDriversPriority']); |
175 | 191 | $driver = image::getDriver(explode(' ', $this->config['imageDriversPriority'])); |
176 | - if ($driver !== false) |
|
177 | - $this->imageDriver = $driver; |
|
192 | + if ($driver !== false) { |
|
193 | + $this->imageDriver = $driver; |
|
194 | + } |
|
178 | 195 | } |
179 | 196 | if ((!isset($driver) || ($driver === false)) && |
180 | 197 | (image::getDriver(array($this->imageDriver)) === false) |
181 | - ) |
|
182 | - die("Cannot find any of the supported PHP image extensions!"); |
|
198 | + ) { |
|
199 | + die("Cannot find any of the supported PHP image extensions!"); |
|
200 | + } |
|
183 | 201 | |
184 | 202 | // WATERMARK INIT |
185 | - if (isset($this->config['watermark']) && is_string($this->config['watermark'])) |
|
186 | - $this->config['watermark'] = array('file' => $this->config['watermark']); |
|
203 | + if (isset($this->config['watermark']) && is_string($this->config['watermark'])) { |
|
204 | + $this->config['watermark'] = array('file' => $this->config['watermark']); |
|
205 | + } |
|
187 | 206 | |
188 | 207 | // GET TYPE DIRECTORY |
189 | 208 | $this->types = &$this->config['types']; |
@@ -197,9 +216,10 @@ discard block |
||
197 | 216 | |
198 | 217 | // LOAD TYPE DIRECTORY SPECIFIC CONFIGURATION IF EXISTS |
199 | 218 | if (is_array($this->types[$this->type])) { |
200 | - foreach ($this->types[$this->type] as $key => $val) |
|
201 | - if (in_array($key, $this->typeSettings)) |
|
219 | + foreach ($this->types[$this->type] as $key => $val) { |
|
220 | + if (in_array($key, $this->typeSettings)) |
|
202 | 221 | $this->config[$key] = $val; |
222 | + } |
|
203 | 223 | $this->types[$this->type] = isset($this->types[$this->type]['type']) |
204 | 224 | ? $this->types[$this->type]['type'] : ""; |
205 | 225 | } |
@@ -209,12 +229,14 @@ discard block |
||
209 | 229 | $ip = '/^' . implode('\.', array($ip, $ip, $ip, $ip)) . '$/'; |
210 | 230 | if (preg_match($ip, $_SERVER['HTTP_HOST']) || |
211 | 231 | preg_match('/^[^\.]+$/', $_SERVER['HTTP_HOST']) |
212 | - ) |
|
213 | - $this->config['cookieDomain'] = ""; |
|
214 | - elseif (!strlen($this->config['cookieDomain'])) |
|
215 | - $this->config['cookieDomain'] = $_SERVER['HTTP_HOST']; |
|
216 | - if (!strlen($this->config['cookiePath'])) |
|
217 | - $this->config['cookiePath'] = "/"; |
|
232 | + ) { |
|
233 | + $this->config['cookieDomain'] = ""; |
|
234 | + } elseif (!strlen($this->config['cookieDomain'])) { |
|
235 | + $this->config['cookieDomain'] = $_SERVER['HTTP_HOST']; |
|
236 | + } |
|
237 | + if (!strlen($this->config['cookiePath'])) { |
|
238 | + $this->config['cookiePath'] = "/"; |
|
239 | + } |
|
218 | 240 | |
219 | 241 | // UPLOAD FOLDER INIT |
220 | 242 | |
@@ -250,26 +272,30 @@ discard block |
||
250 | 272 | $this->typeDir = "{$this->config['uploadDir']}/{$this->type}"; |
251 | 273 | $this->typeURL = "{$this->config['uploadURL']}/{$this->type}"; |
252 | 274 | } |
253 | - if (!is_dir($this->config['uploadDir'])) |
|
254 | - @mkdir($this->config['uploadDir'], $this->config['dirPerms']); |
|
275 | + if (!is_dir($this->config['uploadDir'])) { |
|
276 | + @mkdir($this->config['uploadDir'], $this->config['dirPerms']); |
|
277 | + } |
|
255 | 278 | |
256 | 279 | // HOST APPLICATIONS INIT |
257 | - if (isset($this->get['CKEditorFuncNum'])) |
|
258 | - $this->opener['CKEditor']['funcNum'] = $this->get['CKEditorFuncNum']; |
|
280 | + if (isset($this->get['CKEditorFuncNum'])) { |
|
281 | + $this->opener['CKEditor']['funcNum'] = $this->get['CKEditorFuncNum']; |
|
282 | + } |
|
259 | 283 | if (isset($this->get['opener']) && |
260 | 284 | (strtolower($this->get['opener']) == "tinymce") && |
261 | 285 | isset($this->config['_tinyMCEPath']) && |
262 | 286 | strlen($this->config['_tinyMCEPath']) |
263 | - ) |
|
264 | - $this->opener['TinyMCE'] = true; |
|
287 | + ) { |
|
288 | + $this->opener['TinyMCE'] = true; |
|
289 | + } |
|
265 | 290 | |
266 | 291 | // LOCALIZATION |
267 | - foreach ($this->langInputNames as $key) |
|
268 | - if (isset($this->get[$key]) && |
|
292 | + foreach ($this->langInputNames as $key) { |
|
293 | + if (isset($this->get[$key]) && |
|
269 | 294 | preg_match('/^[a-z][a-z\._\-]*$/i', $this->get[$key]) && |
270 | 295 | file_exists("lang/" . strtolower($this->get[$key]) . ".php") |
271 | 296 | ) { |
272 | 297 | $this->lang = $this->get[$key]; |
298 | + } |
|
273 | 299 | break; |
274 | 300 | } |
275 | 301 | $this->localize($this->lang); |
@@ -280,31 +306,39 @@ discard block |
||
280 | 306 | ) { |
281 | 307 | $htaccess = "{$this->config['uploadDir']}/.htaccess"; |
282 | 308 | if (!file_exists($htaccess)) { |
283 | - if (!@file_put_contents($htaccess, $this->get_htaccess())) |
|
284 | - $this->backMsg("Cannot write to upload folder. {$this->config['uploadDir']}"); |
|
309 | + if (!@file_put_contents($htaccess, $this->get_htaccess())) { |
|
310 | + $this->backMsg("Cannot write to upload folder. {$this->config['uploadDir']}"); |
|
311 | + } |
|
285 | 312 | } else { |
286 | - if (false === ($data = @file_get_contents($htaccess))) |
|
287 | - $this->backMsg("Cannot read .htaccess"); |
|
288 | - if (($data != $this->get_htaccess()) && !@file_put_contents($htaccess, $data)) |
|
289 | - $this->backMsg("Incorrect .htaccess file. Cannot rewrite it!"); |
|
313 | + if (false === ($data = @file_get_contents($htaccess))) { |
|
314 | + $this->backMsg("Cannot read .htaccess"); |
|
315 | + } |
|
316 | + if (($data != $this->get_htaccess()) && !@file_put_contents($htaccess, $data)) { |
|
317 | + $this->backMsg("Incorrect .htaccess file. Cannot rewrite it!"); |
|
318 | + } |
|
290 | 319 | } |
291 | 320 | } |
292 | 321 | |
293 | 322 | // CHECK & CREATE UPLOAD FOLDER |
294 | 323 | if (!is_dir($this->typeDir)) { |
295 | - if (!mkdir($this->typeDir, $this->config['dirPerms'])) |
|
296 | - $this->backMsg("Cannot create {dir} folder.", array('dir' => $this->type)); |
|
297 | - } elseif (!is_readable($this->typeDir)) |
|
298 | - $this->backMsg("Cannot read upload folder."); |
|
324 | + if (!mkdir($this->typeDir, $this->config['dirPerms'])) { |
|
325 | + $this->backMsg("Cannot create {dir} folder.", array('dir' => $this->type)); |
|
326 | + } |
|
327 | + } elseif (!is_readable($this->typeDir)) { |
|
328 | + $this->backMsg("Cannot read upload folder."); |
|
329 | + } |
|
299 | 330 | } |
300 | 331 | |
301 | - public function upload() { |
|
332 | + public function upload() |
|
333 | + { |
|
302 | 334 | $config = &$this->config; |
303 | 335 | $file = &$this->file; |
304 | 336 | $url = $message = ""; |
305 | 337 | |
306 | 338 | if ($config['disabled'] || !$config['access']['files']['upload']) { |
307 | - if (isset($file['tmp_name'])) @unlink($file['tmp_name']); |
|
339 | + if (isset($file['tmp_name'])) { |
|
340 | + @unlink($file['tmp_name']); |
|
341 | + } |
|
308 | 342 | $message = $this->label("You don't have permissions to upload files."); |
309 | 343 | |
310 | 344 | } elseif (true === ($message = $this->checkUploadedFile())) { |
@@ -315,9 +349,9 @@ discard block |
||
315 | 349 | (false !== ($gdir = $this->checkInputDir($this->get['dir']))) |
316 | 350 | ) { |
317 | 351 | $udir = path::normalize("$dir$gdir"); |
318 | - if (substr($udir, 0, strlen($dir)) !== $dir) |
|
319 | - $message = $this->label("Unknown error."); |
|
320 | - else { |
|
352 | + if (substr($udir, 0, strlen($dir)) !== $dir) { |
|
353 | + $message = $this->label("Unknown error."); |
|
354 | + } else { |
|
321 | 355 | $l = strlen($dir); |
322 | 356 | $dir = "$udir/"; |
323 | 357 | $udir = substr($udir, $l); |
@@ -325,8 +359,9 @@ discard block |
||
325 | 359 | } |
326 | 360 | |
327 | 361 | if (!strlen($message)) { |
328 | - if (!is_dir(path::normalize($dir))) |
|
329 | - @mkdir(path::normalize($dir), $this->config['dirPerms'], true); |
|
362 | + if (!is_dir(path::normalize($dir))) { |
|
363 | + @mkdir(path::normalize($dir), $this->config['dirPerms'], true); |
|
364 | + } |
|
330 | 365 | |
331 | 366 | $filename = $this->normalizeFilename($file['name']); |
332 | 367 | $target = file::getInexistantFilename($dir . $filename); |
@@ -334,21 +369,25 @@ discard block |
||
334 | 369 | if (!@move_uploaded_file($file['tmp_name'], $target) && |
335 | 370 | !@rename($file['tmp_name'], $target) && |
336 | 371 | !@copy($file['tmp_name'], $target) |
337 | - ) |
|
338 | - $message = $this->label("Cannot move uploaded file to target folder."); |
|
339 | - else { |
|
340 | - if (function_exists('chmod')) |
|
341 | - @chmod($target, $this->config['filePerms']); |
|
372 | + ) { |
|
373 | + $message = $this->label("Cannot move uploaded file to target folder."); |
|
374 | + } else { |
|
375 | + if (function_exists('chmod')) { |
|
376 | + @chmod($target, $this->config['filePerms']); |
|
377 | + } |
|
342 | 378 | $this->makeThumb($target); |
343 | 379 | $url = $this->typeURL; |
344 | - if (isset($udir)) $url .= "/$udir"; |
|
380 | + if (isset($udir)) { |
|
381 | + $url .= "/$udir"; |
|
382 | + } |
|
345 | 383 | $url .= "/" . basename($target); |
346 | 384 | if (preg_match('/^([a-z]+)\:\/\/([^\/^\:]+)(\:(\d+))?\/(.+)$/', $url, $patt)) { |
347 | 385 | list($unused, $protocol, $domain, $unused, $port, $path) = $patt; |
348 | 386 | $base = "$protocol://$domain" . (strlen($port) ? ":$port" : "") . "/"; |
349 | 387 | $url = $base . path::urlPathEncode($path); |
350 | - } else |
|
351 | - $url = path::urlPathEncode($url); |
|
388 | + } else { |
|
389 | + $url = path::urlPathEncode($url); |
|
390 | + } |
|
352 | 391 | } |
353 | 392 | } |
354 | 393 | } |
@@ -356,16 +395,19 @@ discard block |
||
356 | 395 | if (strlen($message) && |
357 | 396 | isset($this->file['tmp_name']) && |
358 | 397 | file_exists($this->file['tmp_name']) |
359 | - ) |
|
360 | - @unlink($this->file['tmp_name']); |
|
398 | + ) { |
|
399 | + @unlink($this->file['tmp_name']); |
|
400 | + } |
|
361 | 401 | |
362 | - if (strlen($message) && method_exists($this, 'errorMsg')) |
|
363 | - $this->errorMsg($message); |
|
402 | + if (strlen($message) && method_exists($this, 'errorMsg')) { |
|
403 | + $this->errorMsg($message); |
|
404 | + } |
|
364 | 405 | $this->callBack($url, $message); |
365 | 406 | } |
366 | 407 | |
367 | 408 | |
368 | - protected function getTransaliasSettings() { |
|
409 | + protected function getTransaliasSettings() |
|
410 | + { |
|
369 | 411 | $modx = evolutionCMS(); |
370 | 412 | |
371 | 413 | // Cleaning uploaded filename? |
@@ -385,7 +427,8 @@ discard block |
||
385 | 427 | } |
386 | 428 | |
387 | 429 | |
388 | - protected function normalizeFilename($filename) { |
|
430 | + protected function normalizeFilename($filename) |
|
431 | + { |
|
389 | 432 | if ($this->getTransaliasSettings()) { |
390 | 433 | $format = strrchr($filename, "."); |
391 | 434 | $filename = str_replace($format, "", $filename); |
@@ -394,16 +437,19 @@ discard block |
||
394 | 437 | return $filename; |
395 | 438 | } |
396 | 439 | |
397 | - protected function normalizeDirname($dirname) { |
|
440 | + protected function normalizeDirname($dirname) |
|
441 | + { |
|
398 | 442 | return $this->modx->stripAlias($dirname); |
399 | 443 | } |
400 | 444 | |
401 | - protected function checkUploadedFile(array $aFile=null) { |
|
445 | + protected function checkUploadedFile(array $aFile=null) |
|
446 | + { |
|
402 | 447 | $config = &$this->config; |
403 | 448 | $file = ($aFile === null) ? $this->file : $aFile; |
404 | 449 | |
405 | - if (!is_array($file) || !isset($file['name'])) |
|
406 | - return $this->label("Unknown error"); |
|
450 | + if (!is_array($file) || !isset($file['name'])) { |
|
451 | + return $this->label("Unknown error"); |
|
452 | + } |
|
407 | 453 | |
408 | 454 | if (is_array($file['name'])) { |
409 | 455 | foreach ($file['name'] as $i => $name) { |
@@ -412,8 +458,9 @@ discard block |
||
412 | 458 | 'tmp_name' => $file['tmp_name'][$i], |
413 | 459 | 'error' => $file['error'][$i] |
414 | 460 | )); |
415 | - if ($return !== true) |
|
416 | - return "$name: $return"; |
|
461 | + if ($return !== true) { |
|
462 | + return "$name: $return"; |
|
463 | + } |
|
417 | 464 | } |
418 | 465 | return true; |
419 | 466 | } |
@@ -422,8 +469,8 @@ discard block |
||
422 | 469 | $typePatt = strtolower(text::clearWhitespaces($this->types[$this->type])); |
423 | 470 | |
424 | 471 | // CHECK FOR UPLOAD ERRORS |
425 | - if ($file['error']) |
|
426 | - return |
|
472 | + if ($file['error']) { |
|
473 | + return |
|
427 | 474 | ($file['error'] == UPLOAD_ERR_INI_SIZE) ? |
428 | 475 | $this->label("The uploaded file exceeds {size} bytes.", |
429 | 476 | array('size' => ini_get('upload_max_filesize'))) : ( |
@@ -440,14 +487,17 @@ discard block |
||
440 | 487 | $this->label("Failed to write file.") : |
441 | 488 | $this->label("Unknown error.") |
442 | 489 | ))))); |
490 | + } |
|
443 | 491 | |
444 | 492 | // HIDDEN FILENAMES CHECK |
445 | - elseif (substr($file['name'], 0, 1) == ".") |
|
446 | - return $this->label("File name shouldn't begins with '.'"); |
|
493 | + elseif (substr($file['name'], 0, 1) == ".") { |
|
494 | + return $this->label("File name shouldn't begins with '.'"); |
|
495 | + } |
|
447 | 496 | |
448 | 497 | // EXTENSION CHECK |
449 | - elseif (!$this->validateExtension($extension, $this->type)) |
|
450 | - return $this->label("Denied file extension."); |
|
498 | + elseif (!$this->validateExtension($extension, $this->type)) { |
|
499 | + return $this->label("Denied file extension."); |
|
500 | + } |
|
451 | 501 | |
452 | 502 | // SPECIAL DIRECTORY TYPES CHECK (e.g. *img) |
453 | 503 | elseif (preg_match('/^\*([^ ]+)(.*)?$/s', $typePatt, $patt)) { |
@@ -457,70 +507,84 @@ discard block |
||
457 | 507 | $type = new $class(); |
458 | 508 | $cfg = $config; |
459 | 509 | $cfg['filename'] = $file['name']; |
460 | - if (strlen($params)) |
|
461 | - $cfg['params'] = trim($params); |
|
510 | + if (strlen($params)) { |
|
511 | + $cfg['params'] = trim($params); |
|
512 | + } |
|
462 | 513 | $response = $type->checkFile($file['tmp_name'], $cfg); |
463 | - if ($response !== true) |
|
464 | - return $this->label($response); |
|
465 | - } else |
|
466 | - return $this->label("Non-existing directory type."); |
|
514 | + if ($response !== true) { |
|
515 | + return $this->label($response); |
|
516 | + } |
|
517 | + } else { |
|
518 | + return $this->label("Non-existing directory type."); |
|
519 | + } |
|
467 | 520 | } |
468 | 521 | |
469 | 522 | // IMAGE RESIZE |
470 | 523 | $img = image::factory($this->imageDriver, $file['tmp_name']); |
471 | - if (!$img->initError && !$this->imageResize($img, $file['tmp_name'])) |
|
472 | - return $this->label("The image is too big and/or cannot be resized."); |
|
524 | + if (!$img->initError && !$this->imageResize($img, $file['tmp_name'])) { |
|
525 | + return $this->label("The image is too big and/or cannot be resized."); |
|
526 | + } |
|
473 | 527 | |
474 | 528 | |
475 | 529 | // CHECK FOR MODX MAX FILE SIZE |
476 | 530 | $actualfilesize=filesize($file['tmp_name']); |
477 | - if (isset($this->config['maxfilesize']) && $actualfilesize > $this->config['maxfilesize']) |
|
478 | - return $this->label("File is too big: ".$actualfilesize." Bytes. (max ".$this->config['maxfilesize']." Bytes)"); |
|
531 | + if (isset($this->config['maxfilesize']) && $actualfilesize > $this->config['maxfilesize']) { |
|
532 | + return $this->label("File is too big: ".$actualfilesize." Bytes. (max ".$this->config['maxfilesize']." Bytes)"); |
|
533 | + } |
|
479 | 534 | |
480 | 535 | return true; |
481 | 536 | } |
482 | 537 | |
483 | - protected function checkInputDir($dir, $inclType=true, $existing=true) { |
|
538 | + protected function checkInputDir($dir, $inclType=true, $existing=true) |
|
539 | + { |
|
484 | 540 | $dir = path::normalize($dir); |
485 | - if (substr($dir, 0, 1) == "/") |
|
486 | - $dir = substr($dir, 1); |
|
541 | + if (substr($dir, 0, 1) == "/") { |
|
542 | + $dir = substr($dir, 1); |
|
543 | + } |
|
487 | 544 | |
488 | - if ((substr($dir, 0, 1) == ".") || (substr(basename($dir), 0, 1) == ".")) |
|
489 | - return false; |
|
545 | + if ((substr($dir, 0, 1) == ".") || (substr(basename($dir), 0, 1) == ".")) { |
|
546 | + return false; |
|
547 | + } |
|
490 | 548 | |
491 | 549 | if ($inclType) { |
492 | 550 | $first = explode("/", $dir); |
493 | 551 | $first = $first[0]; |
494 | - if ($first != $this->type) |
|
495 | - return false; |
|
552 | + if ($first != $this->type) { |
|
553 | + return false; |
|
554 | + } |
|
496 | 555 | $return = $this->removeTypeFromPath($dir); |
497 | 556 | } else { |
498 | 557 | $return = $dir; |
499 | 558 | $dir = "{$this->type}/$dir"; |
500 | 559 | } |
501 | 560 | |
502 | - if (!$existing) |
|
503 | - return $return; |
|
561 | + if (!$existing) { |
|
562 | + return $return; |
|
563 | + } |
|
504 | 564 | |
505 | 565 | $path = "{$this->config['uploadDir']}/$dir"; |
506 | 566 | return (is_dir($path) && is_readable($path)) ? $return : false; |
507 | 567 | } |
508 | 568 | |
509 | - protected function validateExtension($ext, $type) { |
|
569 | + protected function validateExtension($ext, $type) |
|
570 | + { |
|
510 | 571 | $ext = trim(strtolower($ext)); |
511 | - if (!isset($this->types[$type])) |
|
512 | - return false; |
|
572 | + if (!isset($this->types[$type])) { |
|
573 | + return false; |
|
574 | + } |
|
513 | 575 | |
514 | 576 | $exts = strtolower(text::clearWhitespaces($this->config['deniedExts'])); |
515 | 577 | if (strlen($exts)) { |
516 | 578 | $exts = explode(" ", $exts); |
517 | - if (in_array($ext, $exts)) |
|
518 | - return false; |
|
579 | + if (in_array($ext, $exts)) { |
|
580 | + return false; |
|
581 | + } |
|
519 | 582 | } |
520 | 583 | |
521 | 584 | $exts = trim($this->types[$type]); |
522 | - if (!strlen($exts) || substr($exts, 0, 1) == "*") |
|
523 | - return true; |
|
585 | + if (!strlen($exts) || substr($exts, 0, 1) == "*") { |
|
586 | + return true; |
|
587 | + } |
|
524 | 588 | |
525 | 589 | if (substr($exts, 0, 1) == "!") { |
526 | 590 | $exts = explode(" ", trim(strtolower(substr($exts, 1)))); |
@@ -531,26 +595,32 @@ discard block |
||
531 | 595 | return in_array($ext, $exts); |
532 | 596 | } |
533 | 597 | |
534 | - protected function getTypeFromPath($path) { |
|
598 | + protected function getTypeFromPath($path) |
|
599 | + { |
|
535 | 600 | return preg_match('/^([^\/]*)\/.*$/', $path, $patt) |
536 | 601 | ? $patt[1] : $path; |
537 | 602 | } |
538 | 603 | |
539 | - protected function removeTypeFromPath($path) { |
|
604 | + protected function removeTypeFromPath($path) |
|
605 | + { |
|
540 | 606 | return preg_match('/^[^\/]*\/(.*)$/', $path, $patt) |
541 | 607 | ? $patt[1] : ""; |
542 | 608 | } |
543 | 609 | |
544 | - protected function imageResize($image, $file=null) { |
|
610 | + protected function imageResize($image, $file=null) |
|
611 | + { |
|
545 | 612 | |
546 | 613 | if (!($image instanceof image)) { |
547 | 614 | $img = image::factory($this->imageDriver, $image); |
548 | - if ($img->initError) return false; |
|
615 | + if ($img->initError) { |
|
616 | + return false; |
|
617 | + } |
|
549 | 618 | $file = $image; |
550 | - } elseif ($file === null) |
|
551 | - return false; |
|
552 | - else |
|
553 | - $img = $image; |
|
619 | + } elseif ($file === null) { |
|
620 | + return false; |
|
621 | + } else { |
|
622 | + $img = $image; |
|
623 | + } |
|
554 | 624 | |
555 | 625 | $orientation = 1; |
556 | 626 | if (function_exists("exif_read_data")) { |
@@ -572,8 +642,9 @@ discard block |
||
572 | 642 | ) |
573 | 643 | ) && |
574 | 644 | ($orientation == 1) |
575 | - ) |
|
576 | - return true; |
|
645 | + ) { |
|
646 | + return true; |
|
647 | + } |
|
577 | 648 | |
578 | 649 | |
579 | 650 | // PROPORTIONAL RESIZE |
@@ -593,15 +664,17 @@ discard block |
||
593 | 664 | $width = $img->getPropWidth($height); |
594 | 665 | } |
595 | 666 | |
596 | - if (isset($width) && isset($height) && !$img->resize($width, $height)) |
|
597 | - return false; |
|
667 | + if (isset($width) && isset($height) && !$img->resize($width, $height)) { |
|
668 | + return false; |
|
669 | + } |
|
598 | 670 | |
599 | 671 | // RESIZE TO FIT |
600 | 672 | } elseif ( |
601 | 673 | $this->config['maxImageWidth'] && $this->config['maxImageHeight'] && |
602 | 674 | !$img->resizeFit($this->config['maxImageWidth'], $this->config['maxImageHeight']) |
603 | - ) |
|
604 | - return false; |
|
675 | + ) { |
|
676 | + return false; |
|
677 | + } |
|
605 | 678 | |
606 | 679 | // AUTO FLIP AND ROTATE FROM EXIF |
607 | 680 | if ((($orientation == 2) && !$img->flipHorizontal()) || |
@@ -611,11 +684,13 @@ discard block |
||
611 | 684 | (($orientation == 6) && !$img->rotate(90)) || |
612 | 685 | (($orientation == 7) && (!$img->flipHorizontal() || !$img->rotate(90))) || |
613 | 686 | (($orientation == 8) && !$img->rotate(270)) |
614 | - ) |
|
615 | - return false; |
|
616 | - if (($orientation >= 2) && ($orientation <= 8) && ($this->imageDriver == "imagick")) |
|
617 | - try { |
|
687 | + ) { |
|
688 | + return false; |
|
689 | + } |
|
690 | + if (($orientation >= 2) && ($orientation <= 8) && ($this->imageDriver == "imagick")) { |
|
691 | + try { |
|
618 | 692 | $img->image->setImageProperty('exif:Orientation', "1"); |
693 | + } |
|
619 | 694 | } catch (Exception $e) {} |
620 | 695 | |
621 | 696 | // WATERMARK |
@@ -646,22 +721,26 @@ discard block |
||
646 | 721 | |
647 | 722 | } |
648 | 723 | |
649 | - protected function makeThumb($file, $overwrite=true) { |
|
724 | + protected function makeThumb($file, $overwrite=true) |
|
725 | + { |
|
650 | 726 | $img = image::factory($this->imageDriver, $file); |
651 | 727 | |
652 | 728 | // Drop files which are not images |
653 | - if ($img->initError) |
|
654 | - return true; |
|
729 | + if ($img->initError) { |
|
730 | + return true; |
|
731 | + } |
|
655 | 732 | |
656 | 733 | $thumb = substr($file, strlen($this->config['uploadDir'])); |
657 | 734 | $thumb = $this->config['uploadDir'] . "/" . $this->config['thumbsDir'] . "/" . $thumb; |
658 | 735 | $thumb = path::normalize($thumb); |
659 | 736 | $thumbDir = dirname($thumb); |
660 | - if (!is_dir($thumbDir) && !@mkdir($thumbDir, $this->config['dirPerms'], true)) |
|
661 | - return false; |
|
737 | + if (!is_dir($thumbDir) && !@mkdir($thumbDir, $this->config['dirPerms'], true)) { |
|
738 | + return false; |
|
739 | + } |
|
662 | 740 | |
663 | - if (!$overwrite && is_file($thumb)) |
|
664 | - return true; |
|
741 | + if (!$overwrite && is_file($thumb)) { |
|
742 | + return true; |
|
743 | + } |
|
665 | 744 | |
666 | 745 | // Images with smaller resolutions than thumbnails |
667 | 746 | /*if (($img->width <= $this->config['thumbWidth']) && |
@@ -674,8 +753,9 @@ discard block |
||
674 | 753 | |
675 | 754 | // Resize image |
676 | 755 | } else */ |
677 | - if (!$img->resizeFit($this->config['thumbWidth'], $this->config['thumbHeight'])) |
|
678 | - return false; |
|
756 | + if (!$img->resizeFit($this->config['thumbWidth'], $this->config['thumbHeight'])) { |
|
757 | + return false; |
|
758 | + } |
|
679 | 759 | |
680 | 760 | if ( $this->imageDriver == 'gd' ) { |
681 | 761 | $width = imagesx( $img->image ); |
@@ -697,7 +777,8 @@ discard block |
||
697 | 777 | )); |
698 | 778 | } |
699 | 779 | |
700 | - protected function localize($langCode) { |
|
780 | + protected function localize($langCode) |
|
781 | + { |
|
701 | 782 | require "lang/{$langCode}.php"; |
702 | 783 | setlocale(LC_ALL, $lang['_locale']); |
703 | 784 | $this->charset = $lang['_charset']; |
@@ -712,27 +793,34 @@ discard block |
||
712 | 793 | $this->labels = $lang; |
713 | 794 | } |
714 | 795 | |
715 | - protected function label($string, array $data=null) { |
|
796 | + protected function label($string, array $data=null) |
|
797 | + { |
|
716 | 798 | $return = isset($this->labels[$string]) ? $this->labels[$string] : $string; |
717 | - if (is_array($data)) |
|
718 | - foreach ($data as $key => $val) |
|
799 | + if (is_array($data)) { |
|
800 | + foreach ($data as $key => $val) |
|
719 | 801 | $return = str_replace("{{$key}}", $val, $return); |
802 | + } |
|
720 | 803 | return $return; |
721 | 804 | } |
722 | 805 | |
723 | - protected function backMsg($message, array $data=null) { |
|
806 | + protected function backMsg($message, array $data=null) |
|
807 | + { |
|
724 | 808 | $message = $this->label($message, $data); |
725 | - if (isset($this->file['tmp_name']) && file_exists($this->file['tmp_name'])) |
|
726 | - @unlink($this->file['tmp_name']); |
|
809 | + if (isset($this->file['tmp_name']) && file_exists($this->file['tmp_name'])) { |
|
810 | + @unlink($this->file['tmp_name']); |
|
811 | + } |
|
727 | 812 | $this->callBack("", $message); |
728 | 813 | die; |
729 | 814 | } |
730 | 815 | |
731 | - protected function callBack($url, $message="") { |
|
816 | + protected function callBack($url, $message="") |
|
817 | + { |
|
732 | 818 | $message = text::jsValue($message); |
733 | 819 | $CKfuncNum = isset($this->opener['CKEditor']['funcNum']) |
734 | 820 | ? $this->opener['CKEditor']['funcNum'] : 0; |
735 | - if (!$CKfuncNum) $CKfuncNum = 0; |
|
821 | + if (!$CKfuncNum) { |
|
822 | + $CKfuncNum = 0; |
|
823 | + } |
|
736 | 824 | header("Content-Type: text/html; charset={$this->charset}"); |
737 | 825 | |
738 | 826 | ?><html> |
@@ -769,7 +857,8 @@ discard block |
||
769 | 857 | |
770 | 858 | } |
771 | 859 | |
772 | - protected function get_htaccess() { |
|
860 | + protected function get_htaccess() |
|
861 | + { |
|
773 | 862 | return "<IfModule mod_php4.c> |
774 | 863 | php_value engine off |
775 | 864 | </IfModule> |
@@ -183,6 +183,9 @@ discard block |
||
183 | 183 | } |
184 | 184 | |
185 | 185 | |
186 | + /** |
|
187 | + * @param integer $n |
|
188 | + */ |
|
186 | 189 | private function getChars($n) |
187 | 190 | { |
188 | 191 | $response = null; |
@@ -225,6 +228,9 @@ discard block |
||
225 | 228 | } |
226 | 229 | |
227 | 230 | |
231 | + /** |
|
232 | + * @param false|string $str |
|
233 | + */ |
|
228 | 234 | private function readInt($str) |
229 | 235 | { |
230 | 236 | $size = unpack("C*", $str); |
@@ -21,13 +21,17 @@ discard block |
||
21 | 21 | |
22 | 22 | public function __construct($uri = null) |
23 | 23 | { |
24 | - if ($uri) $this->load($uri); |
|
24 | + if ($uri) { |
|
25 | + $this->load($uri); |
|
26 | + } |
|
25 | 27 | } |
26 | 28 | |
27 | 29 | |
28 | 30 | public function load($uri) |
29 | 31 | { |
30 | - if ($this->handle) $this->close(); |
|
32 | + if ($this->handle) { |
|
33 | + $this->close(); |
|
34 | + } |
|
31 | 35 | |
32 | 36 | $this->uri = $uri; |
33 | 37 | $this->handle = fopen($uri, 'r'); |
@@ -36,15 +40,16 @@ discard block |
||
36 | 40 | |
37 | 41 | public function close() |
38 | 42 | { |
39 | - if (is_resource($this->handle)) fclose($this->handle); |
|
43 | + if (is_resource($this->handle)) { |
|
44 | + fclose($this->handle); |
|
45 | + } |
|
40 | 46 | } |
41 | 47 | |
42 | 48 | |
43 | 49 | public function getSize() |
44 | 50 | { |
45 | 51 | $this->strpos = 0; |
46 | - if ($this->getType()) |
|
47 | - { |
|
52 | + if ($this->getType()) { |
|
48 | 53 | return array_values($this->parseSize()); |
49 | 54 | } |
50 | 55 | |
@@ -56,10 +61,8 @@ discard block |
||
56 | 61 | { |
57 | 62 | $this->strpos = 0; |
58 | 63 | |
59 | - if (!$this->type) |
|
60 | - { |
|
61 | - switch ($this->getChars(2)) |
|
62 | - { |
|
64 | + if (!$this->type) { |
|
65 | + switch ($this->getChars(2)) { |
|
63 | 66 | case "BM": |
64 | 67 | return $this->type = 'bmp'; |
65 | 68 | case "GI": |
@@ -81,8 +84,7 @@ discard block |
||
81 | 84 | { |
82 | 85 | $this->strpos = 0; |
83 | 86 | |
84 | - switch ($this->type) |
|
85 | - { |
|
87 | + switch ($this->type) { |
|
86 | 88 | case 'png': |
87 | 89 | return $this->parseSizeForPNG(); |
88 | 90 | case 'gif': |
@@ -127,10 +129,8 @@ discard block |
||
127 | 129 | { |
128 | 130 | $state = null; |
129 | 131 | |
130 | - while (true) |
|
131 | - { |
|
132 | - switch ($state) |
|
133 | - { |
|
132 | + while (true) { |
|
133 | + switch ($state) { |
|
134 | 134 | default: |
135 | 135 | $this->getChars(2); |
136 | 136 | $state = 'started'; |
@@ -138,27 +138,22 @@ discard block |
||
138 | 138 | |
139 | 139 | case 'started': |
140 | 140 | $b = $this->getByte(); |
141 | - if ($b === false) return false; |
|
141 | + if ($b === false) { |
|
142 | + return false; |
|
143 | + } |
|
142 | 144 | |
143 | 145 | $state = $b == 0xFF ? 'sof' : 'started'; |
144 | 146 | break; |
145 | 147 | |
146 | 148 | case 'sof': |
147 | 149 | $b = $this->getByte(); |
148 | - if (in_array($b, range(0xe0, 0xef))) |
|
149 | - { |
|
150 | + if (in_array($b, range(0xe0, 0xef))) { |
|
150 | 151 | $state = 'skipframe'; |
151 | - } |
|
152 | - elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF)))) |
|
153 | - { |
|
152 | + } elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF)))) { |
|
154 | 153 | $state = 'readsize'; |
155 | - } |
|
156 | - elseif ($b == 0xFF) |
|
157 | - { |
|
154 | + } elseif ($b == 0xFF) { |
|
158 | 155 | $state = 'sof'; |
159 | - } |
|
160 | - else |
|
161 | - { |
|
156 | + } else { |
|
162 | 157 | $state = 'skipframe'; |
163 | 158 | } |
164 | 159 | break; |
@@ -187,21 +182,16 @@ discard block |
||
187 | 182 | $response = null; |
188 | 183 | |
189 | 184 | // do we need more data? |
190 | - if ($this->strpos + $n -1 >= strlen($this->str)) |
|
191 | - { |
|
185 | + if ($this->strpos + $n -1 >= strlen($this->str)) { |
|
192 | 186 | $end = ($this->strpos + $n); |
193 | 187 | |
194 | - while (strlen($this->str) < $end && $response !== false) |
|
195 | - { |
|
188 | + while (strlen($this->str) < $end && $response !== false) { |
|
196 | 189 | // read more from the file handle |
197 | 190 | $need = $end - ftell($this->handle); |
198 | 191 | |
199 | - if ($response = fread($this->handle, $need)) |
|
200 | - { |
|
192 | + if ($response = fread($this->handle, $need)) { |
|
201 | 193 | $this->str .= $response; |
202 | - } |
|
203 | - else |
|
204 | - { |
|
194 | + } else { |
|
205 | 195 | return false; |
206 | 196 | } |
207 | 197 | } |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | * The parameter $bigger_size is the bigger dimension (width or height) of calculated sizes. |
169 | 169 | * The other dimension (height or width) will be calculated autamaticaly |
170 | 170 | * @param integer $bigger_size |
171 | - * @return array */ |
|
171 | + * @return integer[] */ |
|
172 | 172 | |
173 | 173 | public function get_prop_size($bigger_size) { |
174 | 174 | |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * no samo po edno (x ili y) izmerenie |
256 | 256 | * @param integer $width |
257 | 257 | * @param integer $height |
258 | - * @return bool */ |
|
258 | + * @return gd */ |
|
259 | 259 | |
260 | 260 | public function resize_overflow($width, $height) { |
261 | 261 | |
@@ -397,6 +397,12 @@ discard block |
||
397 | 397 | return false; |
398 | 398 | } |
399 | 399 | |
400 | + /** |
|
401 | + * @param integer $x1 |
|
402 | + * @param integer $y1 |
|
403 | + * @param integer $x2 |
|
404 | + * @param integer $y2 |
|
405 | + */ |
|
400 | 406 | public function imagefilledrectangle($x1, $y1, $x2, $y2, $color) { |
401 | 407 | $color = $this->gd_color($color); |
402 | 408 | if ($color === false) return false; |
@@ -12,7 +12,8 @@ discard block |
||
12 | 12 | * @link http://kcfinder.sunhater.com |
13 | 13 | */ |
14 | 14 | |
15 | -class gd { |
|
15 | +class gd |
|
16 | +{ |
|
16 | 17 | |
17 | 18 | /** GD resource |
18 | 19 | * @var resource */ |
@@ -40,23 +41,24 @@ discard block |
||
40 | 41 | * @param mixed $image |
41 | 42 | * @return array */ |
42 | 43 | |
43 | - protected function build_image($image) { |
|
44 | + protected function build_image($image) |
|
45 | + { |
|
44 | 46 | |
45 | - if ($image instanceof gd) { |
|
47 | + if ($image instanceof gd) { |
|
46 | 48 | $width = $image->get_width(); |
47 | 49 | $height = $image->get_height(); |
48 | 50 | $image = $image->get_image(); |
49 | 51 | |
50 | - } elseif (is_resource($image) && (get_resource_type($image) == "gd")) { |
|
52 | + } elseif (is_resource($image) && (get_resource_type($image) == "gd")) { |
|
51 | 53 | $width = @imagesx($image); |
52 | 54 | $height = @imagesy($image); |
53 | 55 | |
54 | - } elseif (is_array($image)) { |
|
56 | + } elseif (is_array($image)) { |
|
55 | 57 | list($key, $width) = each($image); |
56 | 58 | list($key, $height) = each($image); |
57 | 59 | $image = imagecreatetruecolor($width, $height); |
58 | 60 | |
59 | - } elseif (false !== (list($width, $height, $type) = @getimagesize($image))) { |
|
61 | + } elseif (false !== (list($width, $height, $type) = @getimagesize($image))) { |
|
60 | 62 | $image = |
61 | 63 | ($type == IMAGETYPE_GIF) ? @imagecreatefromgif($image) : ( |
62 | 64 | ($type == IMAGETYPE_WBMP) ? @imagecreatefromwbmp($image) : ( |
@@ -66,8 +68,9 @@ discard block |
||
66 | 68 | ($type == IMAGETYPE_XBM) ? @imagecreatefromxbm($image) : false |
67 | 69 | ))))); |
68 | 70 | |
69 | - if ($type == IMAGETYPE_PNG) |
|
70 | - imagealphablending($image, false); |
|
71 | + if ($type == IMAGETYPE_PNG) { |
|
72 | + imagealphablending($image, false); |
|
73 | + } |
|
71 | 74 | } |
72 | 75 | |
73 | 76 | $return = ( |
@@ -81,8 +84,9 @@ discard block |
||
81 | 84 | ? array($image, $width, $height) |
82 | 85 | : false; |
83 | 86 | |
84 | - if (($return !== false) && isset($type)) |
|
85 | - $this->type = $type; |
|
87 | + if (($return !== false) && isset($type)) { |
|
88 | + $this->type = $type; |
|
89 | + } |
|
86 | 90 | |
87 | 91 | return $return; |
88 | 92 | } |
@@ -98,20 +102,22 @@ discard block |
||
98 | 102 | * @param integer $bigger_size |
99 | 103 | * @return gd */ |
100 | 104 | |
101 | - public function __construct($image, $bigger_size=null) { |
|
105 | + public function __construct($image, $bigger_size=null) |
|
106 | + { |
|
102 | 107 | $this->image = $this->width = $this->height = null; |
103 | 108 | |
104 | 109 | $image_details = $this->build_image($image); |
105 | 110 | |
106 | - if ($image_details !== false) |
|
107 | - list($this->image, $this->width, $this->height) = $image_details; |
|
108 | - else |
|
109 | - $this->init_error = true; |
|
111 | + if ($image_details !== false) { |
|
112 | + list($this->image, $this->width, $this->height) = $image_details; |
|
113 | + } else { |
|
114 | + $this->init_error = true; |
|
115 | + } |
|
110 | 116 | |
111 | 117 | if (!is_null($this->image) && |
112 | 118 | !is_null($bigger_size) && |
113 | 119 | (preg_match('/^[1-9][0-9]*$/', $bigger_size) !== false) |
114 | - ) { |
|
120 | + ) { |
|
115 | 121 | $image = $this->image; |
116 | 122 | list($width, $height) = $this->get_prop_size($bigger_size); |
117 | 123 | $this->image = imagecreatetruecolor($width, $height); |
@@ -126,21 +132,24 @@ discard block |
||
126 | 132 | /** Returns the GD resource |
127 | 133 | * @return resource */ |
128 | 134 | |
129 | - public function get_image() { |
|
135 | + public function get_image() |
|
136 | + { |
|
130 | 137 | return $this->image; |
131 | 138 | } |
132 | 139 | |
133 | 140 | /** Returns the image width |
134 | 141 | * @return integer */ |
135 | 142 | |
136 | - public function get_width() { |
|
143 | + public function get_width() |
|
144 | + { |
|
137 | 145 | return $this->width; |
138 | 146 | } |
139 | 147 | |
140 | 148 | /** Returns the image height |
141 | 149 | * @return integer */ |
142 | 150 | |
143 | - public function get_height() { |
|
151 | + public function get_height() |
|
152 | + { |
|
144 | 153 | return $this->height; |
145 | 154 | } |
146 | 155 | |
@@ -148,9 +157,12 @@ discard block |
||
148 | 157 | * @param integer $resized_height |
149 | 158 | * @return integer */ |
150 | 159 | |
151 | - public function get_prop_width($resized_height) { |
|
160 | + public function get_prop_width($resized_height) |
|
161 | + { |
|
152 | 162 | $width = intval(($this->width * $resized_height) / $this->height); |
153 | - if (!$width) $width = 1; |
|
163 | + if (!$width) { |
|
164 | + $width = 1; |
|
165 | + } |
|
154 | 166 | return $width; |
155 | 167 | } |
156 | 168 | |
@@ -158,9 +170,12 @@ discard block |
||
158 | 170 | * @param integer $resized_width |
159 | 171 | * @return integer */ |
160 | 172 | |
161 | - public function get_prop_height($resized_width) { |
|
173 | + public function get_prop_height($resized_width) |
|
174 | + { |
|
162 | 175 | $height = intval(($this->height * $resized_width) / $this->width); |
163 | - if (!$height) $height = 1; |
|
176 | + if (!$height) { |
|
177 | + $height = 1; |
|
178 | + } |
|
164 | 179 | return $height; |
165 | 180 | } |
166 | 181 | |
@@ -170,18 +185,20 @@ discard block |
||
170 | 185 | * @param integer $bigger_size |
171 | 186 | * @return array */ |
172 | 187 | |
173 | - public function get_prop_size($bigger_size) { |
|
188 | + public function get_prop_size($bigger_size) |
|
189 | + { |
|
174 | 190 | |
175 | - if ($this->width > $this->height) { |
|
191 | + if ($this->width > $this->height) { |
|
176 | 192 | $width = $bigger_size; |
177 | 193 | $height = $this->get_prop_height($width); |
178 | 194 | |
179 | - } elseif ($this->height > $this->width) { |
|
195 | + } elseif ($this->height > $this->width) { |
|
180 | 196 | $height = $bigger_size; |
181 | 197 | $width = $this->get_prop_width($height); |
182 | 198 | |
183 | - } else |
|
184 | - $width = $height = $bigger_size; |
|
199 | + } else { |
|
200 | + $width = $height = $bigger_size; |
|
201 | + } |
|
185 | 202 | |
186 | 203 | return array($width, $height); |
187 | 204 | } |
@@ -191,9 +208,14 @@ discard block |
||
191 | 208 | * @param integer $height |
192 | 209 | * @return bool */ |
193 | 210 | |
194 | - public function resize($width, $height) { |
|
195 | - if (!$width) $width = 1; |
|
196 | - if (!$height) $height = 1; |
|
211 | + public function resize($width, $height) |
|
212 | + { |
|
213 | + if (!$width) { |
|
214 | + $width = 1; |
|
215 | + } |
|
216 | + if (!$height) { |
|
217 | + $height = 1; |
|
218 | + } |
|
197 | 219 | return ( |
198 | 220 | (false !== ($img = new gd(array($width, $height)))) && |
199 | 221 | $img->imagecopyresampled($this) && |
@@ -208,19 +230,20 @@ discard block |
||
208 | 230 | * @param mixed $src |
209 | 231 | * @return bool */ |
210 | 232 | |
211 | - public function resize_crop($src) { |
|
233 | + public function resize_crop($src) |
|
234 | + { |
|
212 | 235 | $image_details = $this->build_image($src); |
213 | 236 | |
214 | - if ($image_details !== false) { |
|
237 | + if ($image_details !== false) { |
|
215 | 238 | list($src, $src_width, $src_height) = $image_details; |
216 | 239 | |
217 | - if (($src_width / $src_height) > ($this->width / $this->height)) { |
|
240 | + if (($src_width / $src_height) > ($this->width / $this->height)) { |
|
218 | 241 | $src_w = $this->get_prop_width($src_height); |
219 | 242 | $src_h = $src_height; |
220 | 243 | $src_x = intval(($src_width - $src_w) / 2); |
221 | 244 | $src_y = 0; |
222 | 245 | |
223 | - } else { |
|
246 | + } else { |
|
224 | 247 | $src_w = $src_width; |
225 | 248 | $src_h = $this->get_prop_height($src_width); |
226 | 249 | $src_x = 0; |
@@ -229,8 +252,9 @@ discard block |
||
229 | 252 | |
230 | 253 | return imagecopyresampled($this->image, $src, 0, 0, $src_x, $src_y, $this->width, $this->height, $src_w, $src_h); |
231 | 254 | |
232 | - } else |
|
233 | - return false; |
|
255 | + } else { |
|
256 | + return false; |
|
257 | + } |
|
234 | 258 | } |
235 | 259 | |
236 | 260 | /** Resize image to fit in given resolution. Returns TRUE on success or FALSE on failure |
@@ -238,15 +262,22 @@ discard block |
||
238 | 262 | * @param integer $height |
239 | 263 | * @return bool */ |
240 | 264 | |
241 | - public function resize_fit($width, $height) { |
|
242 | - if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height))) |
|
243 | - return true; |
|
244 | - if (!$width || (($height / $width) < ($this->height / $this->width))) |
|
245 | - $width = intval(($this->width * $height) / $this->height); |
|
246 | - elseif (!$height || (($width / $height) < ($this->width / $this->height))) |
|
247 | - $height = intval(($this->height * $width) / $this->width); |
|
248 | - if (!$width) $width = 1; |
|
249 | - if (!$height) $height = 1; |
|
265 | + public function resize_fit($width, $height) |
|
266 | + { |
|
267 | + if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height))) { |
|
268 | + return true; |
|
269 | + } |
|
270 | + if (!$width || (($height / $width) < ($this->height / $this->width))) { |
|
271 | + $width = intval(($this->width * $height) / $this->height); |
|
272 | + } elseif (!$height || (($width / $height) < ($this->width / $this->height))) { |
|
273 | + $height = intval(($this->height * $width) / $this->width); |
|
274 | + } |
|
275 | + if (!$width) { |
|
276 | + $width = 1; |
|
277 | + } |
|
278 | + if (!$height) { |
|
279 | + $height = 1; |
|
280 | + } |
|
250 | 281 | return $this->resize($width, $height); |
251 | 282 | } |
252 | 283 | |
@@ -257,7 +288,8 @@ discard block |
||
257 | 288 | * @param integer $height |
258 | 289 | * @return bool */ |
259 | 290 | |
260 | - public function resize_overflow($width, $height) { |
|
291 | + public function resize_overflow($width, $height) |
|
292 | + { |
|
261 | 293 | |
262 | 294 | $big = (($this->width / $this->height) > ($width / $height)) |
263 | 295 | ? ($this->width * $height) / $this->height |
@@ -266,7 +298,7 @@ discard block |
||
266 | 298 | |
267 | 299 | $return = ($img = new gd($this->image, $big)); |
268 | 300 | |
269 | - if ($return) { |
|
301 | + if ($return) { |
|
270 | 302 | $this->image = $img->get_image(); |
271 | 303 | $this->width = $img->get_width(); |
272 | 304 | $this->height = $img->get_height(); |
@@ -275,7 +307,8 @@ discard block |
||
275 | 307 | return $return; |
276 | 308 | } |
277 | 309 | |
278 | - public function gd_color() { |
|
310 | + public function gd_color() |
|
311 | + { |
|
279 | 312 | $args = func_get_args(); |
280 | 313 | |
281 | 314 | $expr_rgb = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i'; |
@@ -283,22 +316,23 @@ discard block |
||
283 | 316 | $expr_hex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i'; |
284 | 317 | $expr_byte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/'; |
285 | 318 | |
286 | - if (!isset($args[0])) |
|
287 | - return false; |
|
319 | + if (!isset($args[0])) { |
|
320 | + return false; |
|
321 | + } |
|
288 | 322 | |
289 | - if (count($args[0]) == 3) { |
|
323 | + if (count($args[0]) == 3) { |
|
290 | 324 | list($r, $g, $b) = $args[0]; |
291 | 325 | |
292 | - } elseif (preg_match($expr_rgb, $args[0])) { |
|
326 | + } elseif (preg_match($expr_rgb, $args[0])) { |
|
293 | 327 | list($r, $g, $b) = explode(" ", preg_replace($expr_rgb, "$1 $2 $3", $args[0])); |
294 | 328 | |
295 | - } elseif (preg_match($expr_hex1, $args[0])) { |
|
329 | + } elseif (preg_match($expr_hex1, $args[0])) { |
|
296 | 330 | list($r, $g, $b) = explode(" ", preg_replace($expr_hex1, "$1 $2 $3", $args[0])); |
297 | 331 | $r = hexdec($r); |
298 | 332 | $g = hexdec($g); |
299 | 333 | $b = hexdec($b); |
300 | 334 | |
301 | - } elseif (preg_match($expr_hex2, $args[0])) { |
|
335 | + } elseif (preg_match($expr_hex2, $args[0])) { |
|
302 | 336 | list($r, $g, $b) = explode(" ", preg_replace($expr_hex2, "$1$1 $2$2 $3$3", $args[0])); |
303 | 337 | $r = hexdec($r); |
304 | 338 | $g = hexdec($g); |
@@ -308,16 +342,18 @@ discard block |
||
308 | 342 | preg_match($expr_byte, $args[0]) && |
309 | 343 | preg_match($expr_byte, $args[1]) && |
310 | 344 | preg_match($expr_byte, $args[2]) |
311 | - ) { |
|
345 | + ) { |
|
312 | 346 | list($r, $g, $b) = $args; |
313 | 347 | |
314 | - } else |
|
315 | - return false; |
|
348 | + } else { |
|
349 | + return false; |
|
350 | + } |
|
316 | 351 | |
317 | 352 | return imagecolorallocate($this->image, $r, $g, $b); |
318 | 353 | } |
319 | 354 | |
320 | - public function fill_color($color) { |
|
355 | + public function fill_color($color) |
|
356 | + { |
|
321 | 357 | return $this->imagefilledrectangle(0, 0, $this->width - 1, $this->height - 1, $color); |
322 | 358 | } |
323 | 359 | |
@@ -330,20 +366,29 @@ discard block |
||
330 | 366 | $src_x=0, $src_y=0, |
331 | 367 | $dst_w=null, $dst_h=null, |
332 | 368 | $src_w=null, $src_h=null |
333 | - ) { |
|
369 | + ) { |
|
334 | 370 | $image_details = $this->build_image($src); |
335 | 371 | |
336 | - if ($image_details !== false) { |
|
372 | + if ($image_details !== false) { |
|
337 | 373 | list($src, $src_width, $src_height) = $image_details; |
338 | 374 | |
339 | - if (is_null($dst_w)) $dst_w = $this->width - $dst_x; |
|
340 | - if (is_null($dst_h)) $dst_h = $this->height - $dst_y; |
|
341 | - if (is_null($src_w)) $src_w = $src_width - $src_x; |
|
342 | - if (is_null($src_h)) $src_h = $src_height - $src_y; |
|
375 | + if (is_null($dst_w)) { |
|
376 | + $dst_w = $this->width - $dst_x; |
|
377 | + } |
|
378 | + if (is_null($dst_h)) { |
|
379 | + $dst_h = $this->height - $dst_y; |
|
380 | + } |
|
381 | + if (is_null($src_w)) { |
|
382 | + $src_w = $src_width - $src_x; |
|
383 | + } |
|
384 | + if (is_null($src_h)) { |
|
385 | + $src_h = $src_height - $src_y; |
|
386 | + } |
|
343 | 387 | return imagecopy($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); |
344 | 388 | |
345 | - } else |
|
346 | - return false; |
|
389 | + } else { |
|
390 | + return false; |
|
391 | + } |
|
347 | 392 | } |
348 | 393 | |
349 | 394 | public function imagecopyresampled( |
@@ -352,16 +397,24 @@ discard block |
||
352 | 397 | $src_x=0, $src_y=0, |
353 | 398 | $dst_w=null, $dst_h=null, |
354 | 399 | $src_w=null, $src_h=null |
355 | - ) { |
|
400 | + ) { |
|
356 | 401 | $image_details = $this->build_image($src); |
357 | 402 | |
358 | - if ($image_details !== false) { |
|
403 | + if ($image_details !== false) { |
|
359 | 404 | list($src, $src_width, $src_height) = $image_details; |
360 | 405 | |
361 | - if (is_null($dst_w)) $dst_w = $this->width - $dst_x; |
|
362 | - if (is_null($dst_h)) $dst_h = $this->height - $dst_y; |
|
363 | - if (is_null($src_w)) $src_w = $src_width - $src_x; |
|
364 | - if (is_null($src_h)) $src_h = $src_height - $src_y; |
|
406 | + if (is_null($dst_w)) { |
|
407 | + $dst_w = $this->width - $dst_x; |
|
408 | + } |
|
409 | + if (is_null($dst_h)) { |
|
410 | + $dst_h = $this->height - $dst_y; |
|
411 | + } |
|
412 | + if (is_null($src_w)) { |
|
413 | + $src_w = $src_width - $src_x; |
|
414 | + } |
|
415 | + if (is_null($src_h)) { |
|
416 | + $src_h = $src_height - $src_y; |
|
417 | + } |
|
365 | 418 | imagealphablending($this->image, false); |
366 | 419 | imagesavealpha($this->image,true); |
367 | 420 | |
@@ -372,7 +425,7 @@ discard block |
||
372 | 425 | |
373 | 426 | $transindex = imagecolortransparent($src); |
374 | 427 | $palletsize = imagecolorstotal($src); |
375 | - if($transindex >= 0 && $transindex < $palletsize) { |
|
428 | + if($transindex >= 0 && $transindex < $palletsize) { |
|
376 | 429 | $transcol = imagecolorsforindex($src, $transindex); |
377 | 430 | |
378 | 431 | /*** end gif transparent fix ***/ |
@@ -381,44 +434,56 @@ discard block |
||
381 | 434 | imagefilledrectangle($this->image, 0, 0, $dst_w, $dst_h, $transindex); |
382 | 435 | imagecopyresampled($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
383 | 436 | imagecolortransparent($this->image, $transindex); |
384 | - for($y=0; $y<$dst_h; ++$y) |
|
385 | - for($x=0; $x<$dst_w; ++$x) |
|
386 | - if(((imagecolorat($this->image, $x, $y)>>24) & 0x7F) >= 100) imagesetpixel($this->image, $x, $y, $transindex); |
|
437 | + for($y=0; $y<$dst_h; ++$y) { |
|
438 | + for($x=0; |
|
439 | + } |
|
440 | + $x<$dst_w; ++$x) { |
|
441 | + if(((imagecolorat($this->image, $x, $y)>>24) & 0x7F) >= 100) imagesetpixel($this->image, $x, $y, $transindex); |
|
442 | + } |
|
387 | 443 | imagetruecolortopalette($this->image, true, 255); |
388 | - } |
|
389 | - else { |
|
444 | + } else { |
|
390 | 445 | $transparent = imagecolorallocatealpha($this->image, 255, 255, 255, 127); |
391 | 446 | imagefilledrectangle($this->image, 0, 0, $dst_w, $dst_h, $transparent); |
392 | 447 | imagecopyresampled($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
393 | 448 | } |
394 | 449 | return $this->image; |
395 | 450 | |
396 | - } else |
|
397 | - return false; |
|
451 | + } else { |
|
452 | + return false; |
|
453 | + } |
|
398 | 454 | } |
399 | 455 | |
400 | - public function imagefilledrectangle($x1, $y1, $x2, $y2, $color) { |
|
456 | + public function imagefilledrectangle($x1, $y1, $x2, $y2, $color) |
|
457 | + { |
|
401 | 458 | $color = $this->gd_color($color); |
402 | - if ($color === false) return false; |
|
459 | + if ($color === false) { |
|
460 | + return false; |
|
461 | + } |
|
403 | 462 | return imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $color); |
404 | 463 | } |
405 | 464 | |
406 | - public function imagepng($filename=null, $quality=null, $filters=null) { |
|
407 | - if (is_null($filename) && !headers_sent()) |
|
408 | - header("Content-Type: image/png"); |
|
465 | + public function imagepng($filename=null, $quality=null, $filters=null) |
|
466 | + { |
|
467 | + if (is_null($filename) && !headers_sent()) { |
|
468 | + header("Content-Type: image/png"); |
|
469 | + } |
|
409 | 470 | @imagesavealpha($this->image, true); |
410 | 471 | return imagepng($this->image, $filename, $quality, $filters); |
411 | 472 | } |
412 | 473 | |
413 | - public function imagejpeg($filename=null, $quality=75) { |
|
414 | - if (is_null($filename) && !headers_sent()) |
|
415 | - header("Content-Type: image/jpeg"); |
|
474 | + public function imagejpeg($filename=null, $quality=75) |
|
475 | + { |
|
476 | + if (is_null($filename) && !headers_sent()) { |
|
477 | + header("Content-Type: image/jpeg"); |
|
478 | + } |
|
416 | 479 | return imagejpeg($this->image, $filename, $quality); |
417 | 480 | } |
418 | 481 | |
419 | - public function imagegif($filename=null) { |
|
420 | - if (is_null($filename) && !headers_sent()) |
|
421 | - header("Content-Type: image/gif"); |
|
482 | + public function imagegif($filename=null) |
|
483 | + { |
|
484 | + if (is_null($filename) && !headers_sent()) { |
|
485 | + header("Content-Type: image/gif"); |
|
486 | + } |
|
422 | 487 | @imagesavealpha($this->image, true); |
423 | 488 | return imagegif($this->image, $filename); |
424 | 489 | } |
@@ -179,7 +179,6 @@ |
||
179 | 179 | |
180 | 180 | /** Resize and crop the image to fit in given resolution. Returns TRUE on |
181 | 181 | * success or FALSE on failure |
182 | - * @param mixed $src |
|
183 | 182 | * @param integer $offset |
184 | 183 | * @return bool */ |
185 | 184 | abstract public function resizeCrop($width, $height, $offset=false); |
@@ -12,7 +12,8 @@ discard block |
||
12 | 12 | * @link http://kcfinder.sunhater.com |
13 | 13 | */ |
14 | 14 | |
15 | -abstract class image { |
|
15 | +abstract class image |
|
16 | +{ |
|
16 | 17 | const DEFAULT_JPEG_QUALITY = 75; |
17 | 18 | |
18 | 19 | /** Image resource or object |
@@ -41,7 +42,8 @@ discard block |
||
41 | 42 | * @param string $property |
42 | 43 | * @return mixed */ |
43 | 44 | |
44 | - final public function __get($property) { |
|
45 | + final public function __get($property) |
|
46 | + { |
|
45 | 47 | return property_exists($this, $property) ? $this->$property : null; |
46 | 48 | } |
47 | 49 | |
@@ -57,14 +59,16 @@ discard block |
||
57 | 59 | * @param mixed $image |
58 | 60 | * @param array $options */ |
59 | 61 | |
60 | - public function __construct($image, array $options=array()) { |
|
62 | + public function __construct($image, array $options=array()) |
|
63 | + { |
|
61 | 64 | $this->image = $this->width = $this->height = null; |
62 | 65 | $imageDetails = $this->buildImage($image); |
63 | 66 | |
64 | - if ($imageDetails !== false) |
|
65 | - list($this->image, $this->width, $this->height) = $imageDetails; |
|
66 | - else |
|
67 | - $this->initError = true; |
|
67 | + if ($imageDetails !== false) { |
|
68 | + list($this->image, $this->width, $this->height) = $imageDetails; |
|
69 | + } else { |
|
70 | + $this->initError = true; |
|
71 | + } |
|
68 | 72 | $this->options = $options; |
69 | 73 | } |
70 | 74 | |
@@ -75,7 +79,8 @@ discard block |
||
75 | 79 | * @param mixed $image |
76 | 80 | * @return object */ |
77 | 81 | |
78 | - final static function factory($driver, $image, array $options=array()) { |
|
82 | + final static function factory($driver, $image, array $options=array()) |
|
83 | + { |
|
79 | 84 | $class = "image_$driver"; |
80 | 85 | return new $class($image, $options); |
81 | 86 | } |
@@ -86,14 +91,18 @@ discard block |
||
86 | 91 | * @param array $drivers |
87 | 92 | * @return string */ |
88 | 93 | |
89 | - final static function getDriver(array $drivers=array('gd')) { |
|
94 | + final static function getDriver(array $drivers=array('gd')) |
|
95 | + { |
|
90 | 96 | foreach ($drivers as $driver) { |
91 | - if (!preg_match('/^[a-z0-9\_]+$/i', $driver)) |
|
92 | - continue; |
|
97 | + if (!preg_match('/^[a-z0-9\_]+$/i', $driver)) { |
|
98 | + continue; |
|
99 | + } |
|
93 | 100 | $class = "image_$driver"; |
94 | 101 | if (class_exists($class) && method_exists($class, "available")) { |
95 | 102 | eval("\$avail = $class::available();"); |
96 | - if ($avail) return $driver; |
|
103 | + if ($avail) { |
|
104 | + return $driver; |
|
105 | + } |
|
97 | 106 | } |
98 | 107 | } |
99 | 108 | return false; |
@@ -105,7 +114,8 @@ discard block |
||
105 | 114 | * @param mixed $image |
106 | 115 | * @return array */ |
107 | 116 | |
108 | - final protected function buildImage($image) { |
|
117 | + final protected function buildImage($image) |
|
118 | + { |
|
109 | 119 | $class = get_class($this); |
110 | 120 | |
111 | 121 | if ($image instanceof $class) { |
@@ -118,8 +128,9 @@ discard block |
||
118 | 128 | list($key, $height) = each($image); |
119 | 129 | $img = $this->getBlankImage($width, $height); |
120 | 130 | |
121 | - } else |
|
122 | - $img = $this->getImage($image, $width, $height); |
|
131 | + } else { |
|
132 | + $img = $this->getImage($image, $width, $height); |
|
133 | + } |
|
123 | 134 | |
124 | 135 | return ($img !== false) |
125 | 136 | ? array($img, $width, $height) |
@@ -131,9 +142,12 @@ discard block |
||
131 | 142 | * @param integer $resizedHeight |
132 | 143 | * @return integer */ |
133 | 144 | |
134 | - final public function getPropWidth($resizedHeight) { |
|
145 | + final public function getPropWidth($resizedHeight) |
|
146 | + { |
|
135 | 147 | $width = round(($this->width * $resizedHeight) / $this->height); |
136 | - if (!$width) $width = 1; |
|
148 | + if (!$width) { |
|
149 | + $width = 1; |
|
150 | + } |
|
137 | 151 | return $width; |
138 | 152 | } |
139 | 153 | |
@@ -142,9 +156,12 @@ discard block |
||
142 | 156 | * @param integer $resizedWidth |
143 | 157 | * @return integer */ |
144 | 158 | |
145 | - final public function getPropHeight($resizedWidth) { |
|
159 | + final public function getPropHeight($resizedWidth) |
|
160 | + { |
|
146 | 161 | $height = round(($this->height * $resizedWidth) / $this->width); |
147 | - if (!$height) $height = 1; |
|
162 | + if (!$height) { |
|
163 | + $height = 1; |
|
164 | + } |
|
148 | 165 | return $height; |
149 | 166 | } |
150 | 167 | |
@@ -153,13 +170,17 @@ discard block |
||
153 | 170 | * static method should be implemented into driver classes like abstract |
154 | 171 | * methods |
155 | 172 | * @return bool */ |
156 | - static function available() { return false; } |
|
173 | + static function available() |
|
174 | + { |
|
175 | +return false; } |
|
157 | 176 | |
158 | 177 | /** Checks if file is an image. This static method should be implemented into |
159 | 178 | * driver classes like abstract methods |
160 | 179 | * @param string $file |
161 | 180 | * @return bool */ |
162 | - static function checkImage($file) { return false; } |
|
181 | + static function checkImage($file) |
|
182 | + { |
|
183 | +return false; } |
|
163 | 184 | |
164 | 185 | /** Resize image. Should return TRUE on success or FALSE on failure |
165 | 186 | * @param integer $width |
@@ -327,6 +327,11 @@ |
||
327 | 327 | return imagecolorallocate($this->image, $r, $g, $b); |
328 | 328 | } |
329 | 329 | |
330 | + /** |
|
331 | + * @param integer $x1 |
|
332 | + * @param integer $y1 |
|
333 | + * @param boolean $color |
|
334 | + */ |
|
330 | 335 | protected function imageFilledRectangle($x1, $y1, $x2, $y2, $color) { |
331 | 336 | $color = $this->gdColor($color); |
332 | 337 | if ($color === false) return false; |
@@ -12,14 +12,20 @@ discard block |
||
12 | 12 | * @link http://kcfinder.sunhater.com |
13 | 13 | */ |
14 | 14 | |
15 | -class image_gd extends image { |
|
15 | +class image_gd extends image |
|
16 | +{ |
|
16 | 17 | |
17 | 18 | |
18 | 19 | // ABSTRACT PUBLIC METHODS |
19 | 20 | |
20 | - public function resize($width, $height) { |
|
21 | - if (!$width) $width = 1; |
|
22 | - if (!$height) $height = 1; |
|
21 | + public function resize($width, $height) |
|
22 | + { |
|
23 | + if (!$width) { |
|
24 | + $width = 1; |
|
25 | + } |
|
26 | + if (!$height) { |
|
27 | + $height = 1; |
|
28 | + } |
|
23 | 29 | return ( |
24 | 30 | (false !== ($img = new image_gd(array($width, $height)))) && |
25 | 31 | $img->imageCopyResampled($this) && |
@@ -29,9 +35,11 @@ discard block |
||
29 | 35 | ); |
30 | 36 | } |
31 | 37 | |
32 | - public function resizeFit($width, $height, $background=false) { |
|
33 | - if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height))) |
|
34 | - return true; |
|
38 | + public function resizeFit($width, $height, $background=false) |
|
39 | + { |
|
40 | + if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height))) { |
|
41 | + return true; |
|
42 | + } |
|
35 | 43 | if (!$width || (($height / $width) < ($this->height / $this->width))) { |
36 | 44 | $h = $height; |
37 | 45 | $w = round(($this->width * $h) / $this->height); |
@@ -42,13 +50,16 @@ discard block |
||
42 | 50 | $w = $width; |
43 | 51 | $h = $height; |
44 | 52 | } |
45 | - if (!$w) $w = 1; |
|
46 | - if (!$h) $h = 1; |
|
47 | - |
|
48 | - if ($background === false) |
|
49 | - return $this->resize($w, $h); |
|
53 | + if (!$w) { |
|
54 | + $w = 1; |
|
55 | + } |
|
56 | + if (!$h) { |
|
57 | + $h = 1; |
|
58 | + } |
|
50 | 59 | |
51 | - else { |
|
60 | + if ($background === false) { |
|
61 | + return $this->resize($w, $h); |
|
62 | + } else { |
|
52 | 63 | $img = new image_gd(array($width, $height)); |
53 | 64 | $x = round(($width - $w) / 2); |
54 | 65 | $y = round(($height - $h) / 2); |
@@ -56,8 +67,9 @@ discard block |
||
56 | 67 | if ((false === $this->resize($w, $h)) || |
57 | 68 | (false === $img->imageFilledRectangle(0, 0, $width, $height, $background)) || |
58 | 69 | (false === $img->imageCopyResampled($this->image, $x, $y, 0, 0, $w, $h)) |
59 | - ) |
|
60 | - return false; |
|
70 | + ) { |
|
71 | + return false; |
|
72 | + } |
|
61 | 73 | |
62 | 74 | $this->image = $img->image; |
63 | 75 | $this->width = $width; |
@@ -67,41 +79,52 @@ discard block |
||
67 | 79 | } |
68 | 80 | } |
69 | 81 | |
70 | - public function resizeCrop($width, $height, $offset=false) { |
|
82 | + public function resizeCrop($width, $height, $offset=false) |
|
83 | + { |
|
71 | 84 | |
72 | 85 | if (($this->width / $this->height) > ($width / $height)) { |
73 | 86 | $h = $height; |
74 | 87 | $w = ($this->width * $h) / $this->height; |
75 | 88 | $y = 0; |
76 | 89 | if ($offset !== false) { |
77 | - if ($offset > 0) |
|
78 | - $offset = -$offset; |
|
79 | - if (($w + $offset) <= $width) |
|
80 | - $offset = $width - $w; |
|
90 | + if ($offset > 0) { |
|
91 | + $offset = -$offset; |
|
92 | + } |
|
93 | + if (($w + $offset) <= $width) { |
|
94 | + $offset = $width - $w; |
|
95 | + } |
|
81 | 96 | $x = $offset; |
82 | - } else |
|
83 | - $x = ($width - $w) / 2; |
|
97 | + } else { |
|
98 | + $x = ($width - $w) / 2; |
|
99 | + } |
|
84 | 100 | |
85 | 101 | } else { |
86 | 102 | $w = $width; |
87 | 103 | $h = ($this->height * $w) / $this->width; |
88 | 104 | $x = 0; |
89 | 105 | if ($offset !== false) { |
90 | - if ($offset > 0) |
|
91 | - $offset = -$offset; |
|
92 | - if (($h + $offset) <= $height) |
|
93 | - $offset = $height - $h; |
|
106 | + if ($offset > 0) { |
|
107 | + $offset = -$offset; |
|
108 | + } |
|
109 | + if (($h + $offset) <= $height) { |
|
110 | + $offset = $height - $h; |
|
111 | + } |
|
94 | 112 | $y = $offset; |
95 | - } else |
|
96 | - $y = ($height - $h) / 2; |
|
113 | + } else { |
|
114 | + $y = ($height - $h) / 2; |
|
115 | + } |
|
97 | 116 | } |
98 | 117 | |
99 | 118 | $x = round($x); |
100 | 119 | $y = round($y); |
101 | 120 | $w = round($w); |
102 | 121 | $h = round($h); |
103 | - if (!$w) $w = 1; |
|
104 | - if (!$h) $h = 1; |
|
122 | + if (!$w) { |
|
123 | + $w = 1; |
|
124 | + } |
|
125 | + if (!$h) { |
|
126 | + $h = 1; |
|
127 | + } |
|
105 | 128 | |
106 | 129 | $return = ( |
107 | 130 | (false !== ($img = new image_gd(array($width, $height))))) && |
@@ -117,50 +140,59 @@ discard block |
||
117 | 140 | return $return; |
118 | 141 | } |
119 | 142 | |
120 | - public function rotate($angle, $background="#000000") { |
|
143 | + public function rotate($angle, $background="#000000") |
|
144 | + { |
|
121 | 145 | $angle = -$angle; |
122 | 146 | $img = @imagerotate($this->image, $angle, $this->gdColor($background)); |
123 | - if ($img === false) |
|
124 | - return false; |
|
147 | + if ($img === false) { |
|
148 | + return false; |
|
149 | + } |
|
125 | 150 | $this->width = imagesx($img); |
126 | 151 | $this->height = imagesy($img); |
127 | 152 | $this->image = $img; |
128 | 153 | return true; |
129 | 154 | } |
130 | 155 | |
131 | - public function flipHorizontal() { |
|
156 | + public function flipHorizontal() |
|
157 | + { |
|
132 | 158 | $img = imagecreatetruecolor($this->width, $this->height); |
133 | 159 | imagealphablending($img, false); |
134 | 160 | imagesavealpha($img, true); |
135 | - if (imagecopyresampled($img, $this->image, 0, 0, ($this->width - 1), 0, $this->width, $this->height, -$this->width, $this->height)) |
|
136 | - $this->image = $img; |
|
137 | - else |
|
138 | - return false; |
|
161 | + if (imagecopyresampled($img, $this->image, 0, 0, ($this->width - 1), 0, $this->width, $this->height, -$this->width, $this->height)) { |
|
162 | + $this->image = $img; |
|
163 | + } else { |
|
164 | + return false; |
|
165 | + } |
|
139 | 166 | return true; |
140 | 167 | } |
141 | 168 | |
142 | - public function flipVertical() { |
|
169 | + public function flipVertical() |
|
170 | + { |
|
143 | 171 | $img = imagecreatetruecolor($this->width, $this->height); |
144 | 172 | imagealphablending($img, false); |
145 | 173 | imagesavealpha($img, true); |
146 | - if (imagecopyresampled($img, $this->image, 0, 0, 0, ($this->height - 1), $this->width, $this->height, $this->width, -$this->height)) |
|
147 | - $this->image = $img; |
|
148 | - else |
|
149 | - return false; |
|
174 | + if (imagecopyresampled($img, $this->image, 0, 0, 0, ($this->height - 1), $this->width, $this->height, $this->width, -$this->height)) { |
|
175 | + $this->image = $img; |
|
176 | + } else { |
|
177 | + return false; |
|
178 | + } |
|
150 | 179 | return true; |
151 | 180 | } |
152 | 181 | |
153 | - public function watermark($file, $left=false, $top=false) { |
|
182 | + public function watermark($file, $left=false, $top=false) |
|
183 | + { |
|
154 | 184 | $info = getimagesize($file); |
155 | 185 | list($w, $h, $t) = $info; |
156 | - if (!in_array($t, array(IMAGETYPE_PNG, IMAGETYPE_GIF))) |
|
157 | - return false; |
|
186 | + if (!in_array($t, array(IMAGETYPE_PNG, IMAGETYPE_GIF))) { |
|
187 | + return false; |
|
188 | + } |
|
158 | 189 | $imagecreate = ($t == IMAGETYPE_PNG) ? "imagecreatefrompng" : "imagecreatefromgif"; |
159 | 190 | |
160 | 191 | if (!@imagealphablending($this->image, true) || |
161 | 192 | (false === ($wm = @$imagecreate($file))) |
162 | - ) |
|
163 | - return false; |
|
193 | + ) { |
|
194 | + return false; |
|
195 | + } |
|
164 | 196 | |
165 | 197 | $w = imagesx($wm); |
166 | 198 | $h = imagesy($wm); |
@@ -176,28 +208,33 @@ discard block |
||
176 | 208 | if ((($x + $w) > $this->width) || |
177 | 209 | (($y + $h) > $this->height) || |
178 | 210 | ($x < 0) || ($y < 0) |
179 | - ) |
|
180 | - return false; |
|
211 | + ) { |
|
212 | + return false; |
|
213 | + } |
|
181 | 214 | |
182 | - if (($wm === false) || !@imagecopy($this->image, $wm, $x, $y, 0, 0, $w, $h)) |
|
183 | - return false; |
|
215 | + if (($wm === false) || !@imagecopy($this->image, $wm, $x, $y, 0, 0, $w, $h)) { |
|
216 | + return false; |
|
217 | + } |
|
184 | 218 | |
185 | 219 | @imagealphablending($this->image, false); |
186 | 220 | @imagesavealpha($this->image, true); |
187 | 221 | return true; |
188 | 222 | } |
189 | 223 | |
190 | - public function output($type='jpeg', array $options=array()) { |
|
224 | + public function output($type='jpeg', array $options=array()) |
|
225 | + { |
|
191 | 226 | $method = "output_$type"; |
192 | - if (!method_exists($this, $method)) |
|
193 | - return false; |
|
227 | + if (!method_exists($this, $method)) { |
|
228 | + return false; |
|
229 | + } |
|
194 | 230 | return $this->$method($options); |
195 | 231 | } |
196 | 232 | |
197 | 233 | |
198 | 234 | // ABSTRACT PROTECTED METHODS |
199 | 235 | |
200 | - protected function getBlankImage($width, $height) { |
|
236 | + protected function getBlankImage($width, $height) |
|
237 | + { |
|
201 | 238 | $img = @imagecreatetruecolor($width, $height); |
202 | 239 | // imagealphablending($img, false); |
203 | 240 | // imagesavealpha($img, true); |
@@ -207,7 +244,8 @@ discard block |
||
207 | 244 | return $img; |
208 | 245 | } |
209 | 246 | |
210 | - protected function getImage($image, &$width, &$height) { |
|
247 | + protected function getImage($image, &$width, &$height) |
|
248 | + { |
|
211 | 249 | |
212 | 250 | if (is_resource($image) && (get_resource_type($image) == "gd")) { |
213 | 251 | $width = @imagesx($image); |
@@ -227,22 +265,26 @@ discard block |
||
227 | 265 | |
228 | 266 | return $image; |
229 | 267 | |
230 | - } else |
|
231 | - return false; |
|
268 | + } else { |
|
269 | + return false; |
|
270 | + } |
|
232 | 271 | } |
233 | 272 | |
234 | 273 | |
235 | 274 | // PSEUDO-ABSTRACT STATIC METHODS |
236 | 275 | |
237 | - static function available() { |
|
276 | + static function available() |
|
277 | + { |
|
238 | 278 | return function_exists("imagecreatefromjpeg"); |
239 | 279 | } |
240 | 280 | |
241 | - static function checkImage($file) { |
|
281 | + static function checkImage($file) |
|
282 | + { |
|
242 | 283 | if (!is_string($file) || |
243 | 284 | ((false === (list($width, $height, $t) = @getimagesize($file)))) |
244 | - ) |
|
245 | - return false; |
|
285 | + ) { |
|
286 | + return false; |
|
287 | + } |
|
246 | 288 | |
247 | 289 | $img = |
248 | 290 | ($t == IMAGETYPE_GIF) ? @imagecreatefromgif($file) : ( |
@@ -258,34 +300,41 @@ discard block |
||
258 | 300 | |
259 | 301 | // OWN METHODS |
260 | 302 | |
261 | - protected function output_png(array $options=array()) { |
|
303 | + protected function output_png(array $options=array()) |
|
304 | + { |
|
262 | 305 | $file = isset($options['file']) ? $options['file'] : null; |
263 | 306 | $quality = isset($options['quality']) ? $options['quality'] : null; |
264 | 307 | $filters = isset($options['filters']) ? $options['filters'] : null; |
265 | - if (($file === null) && !headers_sent()) |
|
266 | - header("Content-Type: image/png"); |
|
308 | + if (($file === null) && !headers_sent()) { |
|
309 | + header("Content-Type: image/png"); |
|
310 | + } |
|
267 | 311 | @imagesavealpha($this->image, true); |
268 | 312 | return imagepng($this->image, $file, $quality, $filters); |
269 | 313 | } |
270 | 314 | |
271 | - protected function output_jpeg(array $options=array()) { |
|
315 | + protected function output_jpeg(array $options=array()) |
|
316 | + { |
|
272 | 317 | $file = isset($options['file']) ? $options['file'] : null; |
273 | 318 | $quality = isset($options['quality']) |
274 | 319 | ? $options['quality'] |
275 | 320 | : self::DEFAULT_JPEG_QUALITY; |
276 | - if (($file === null) && !headers_sent()) |
|
277 | - header("Content-Type: image/jpeg"); |
|
321 | + if (($file === null) && !headers_sent()) { |
|
322 | + header("Content-Type: image/jpeg"); |
|
323 | + } |
|
278 | 324 | return imagejpeg($this->image, $file, $quality); |
279 | 325 | } |
280 | 326 | |
281 | - protected function output_gif(array $options=array()) { |
|
327 | + protected function output_gif(array $options=array()) |
|
328 | + { |
|
282 | 329 | $file = isset($options['file']) ? $options['file'] : null; |
283 | - if (isset($options['file']) && !headers_sent()) |
|
284 | - header("Content-Type: image/gif"); |
|
330 | + if (isset($options['file']) && !headers_sent()) { |
|
331 | + header("Content-Type: image/gif"); |
|
332 | + } |
|
285 | 333 | return imagegif($this->image, $file); |
286 | 334 | } |
287 | 335 | |
288 | - protected function gdColor() { |
|
336 | + protected function gdColor() |
|
337 | + { |
|
289 | 338 | $args = func_get_args(); |
290 | 339 | |
291 | 340 | $exprRGB = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i'; |
@@ -293,8 +342,9 @@ discard block |
||
293 | 342 | $exprHex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i'; |
294 | 343 | $exprByte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/'; |
295 | 344 | |
296 | - if (!isset($args[0])) |
|
297 | - return false; |
|
345 | + if (!isset($args[0])) { |
|
346 | + return false; |
|
347 | + } |
|
298 | 348 | |
299 | 349 | if (count($args[0]) == 3) { |
300 | 350 | list($r, $g, $b) = $args[0]; |
@@ -321,15 +371,19 @@ discard block |
||
321 | 371 | ) { |
322 | 372 | list($r, $g, $b) = $args; |
323 | 373 | |
324 | - } else |
|
325 | - return false; |
|
374 | + } else { |
|
375 | + return false; |
|
376 | + } |
|
326 | 377 | |
327 | 378 | return imagecolorallocate($this->image, $r, $g, $b); |
328 | 379 | } |
329 | 380 | |
330 | - protected function imageFilledRectangle($x1, $y1, $x2, $y2, $color) { |
|
381 | + protected function imageFilledRectangle($x1, $y1, $x2, $y2, $color) |
|
382 | + { |
|
331 | 383 | $color = $this->gdColor($color); |
332 | - if ($color === false) return false; |
|
384 | + if ($color === false) { |
|
385 | + return false; |
|
386 | + } |
|
333 | 387 | return imageFilledRectangle($this->image, $x1, $y1, $x2, $y2, $color); |
334 | 388 | } |
335 | 389 | |
@@ -338,15 +392,24 @@ discard block |
||
338 | 392 | ) { |
339 | 393 | $imageDetails = $this->buildImage($src); |
340 | 394 | |
341 | - if ($imageDetails === false) |
|
342 | - return false; |
|
395 | + if ($imageDetails === false) { |
|
396 | + return false; |
|
397 | + } |
|
343 | 398 | |
344 | 399 | list($src, $srcWidth, $srcHeight) = $imageDetails; |
345 | 400 | |
346 | - if (is_null($dstW)) $dstW = $this->width - $dstW; |
|
347 | - if (is_null($dstH)) $dstH = $this->height - $dstY; |
|
348 | - if (is_null($srcW)) $srcW = $srcWidth - $srcX; |
|
349 | - if (is_null($srcH)) $srcH = $srcHeight - $srcY; |
|
401 | + if (is_null($dstW)) { |
|
402 | + $dstW = $this->width - $dstW; |
|
403 | + } |
|
404 | + if (is_null($dstH)) { |
|
405 | + $dstH = $this->height - $dstY; |
|
406 | + } |
|
407 | + if (is_null($srcW)) { |
|
408 | + $srcW = $srcWidth - $srcX; |
|
409 | + } |
|
410 | + if (is_null($srcH)) { |
|
411 | + $srcH = $srcHeight - $srcY; |
|
412 | + } |
|
350 | 413 | return imageCopyResampled($this->image, $src, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH); |
351 | 414 | } |
352 | 415 | } |