Nucleus CMS日本語版用プラグインのうち、日本語版開発者がサポートしているもの
Revision | 9fb5c297afb4b8c88574809a6fa9bb61149c3f17 (tree) |
---|---|
Time | 2012-10-20 15:09:53 |
Author | sakamocchi <o-takashi@saka...> |
Commiter | sakamocchi |
FIX: NP_MediaUtilsをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
@@ -1,8 +1,8 @@ | ||
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * MediaUtils plugin for Nucleus CMS |
4 | - * Version 0.9.6 (1.0 RC2) for PHP5 | |
5 | - * Written By Mocchi, Apr. 04, 2011 | |
4 | + * Version 1.0.0 for PHP5 | |
5 | + * Written By Mocchi, Oct. 20, 2011 | |
6 | 6 | * |
7 | 7 | * This program is free software; you can redistribute it and/or |
8 | 8 | * modify it under the terms of the GNU General Public License |
@@ -10,35 +10,41 @@ | ||
10 | 10 | * of the License, or (at your option) any later version. |
11 | 11 | */ |
12 | 12 | |
13 | -class NP_MediaUtils extends NucleusPlugin { | |
13 | +class NP_MediaUtils extends NucleusPlugin | |
14 | +{ | |
14 | 15 | public function getName() { return 'MediaUtils'; } |
15 | 16 | public function getAuthor() { return 'Mocchi'; } |
16 | 17 | public function getURL() { return 'http://japan.nucleuscms.org/wiki/plugins:mediautils'; } |
17 | - public function getVersion() { return '0.9.6 (1.0 RC2)'; } | |
18 | + public function getVersion() { return '1.0.0'; } | |
18 | 19 | public function getDescription() { return 'Load MediaUtils, static function set for media management. Another function of this plugin is keeping Cookies for identifying weblog id.'; } |
19 | 20 | |
20 | 21 | public function getMinNucleusVersion() { return 340; } |
21 | 22 | public function supportsFeature($feature) { return in_array ($feature, array ('SqlTablePrefix', 'SqlApi')); } |
22 | 23 | public function getEventList() { return array('PostAuthentication', 'InitSkinParse', 'PreSendContentType'); } |
23 | 24 | |
24 | - /* | |
25 | - * NOTICE: Event drivened method can get a correct blogid in initSkinParse. | |
26 | - */ | |
27 | - public function event_PostAuthentication($data) { | |
25 | + public function init() | |
26 | + { | |
27 | + if ( !class_exists('MediaUtils', FALSE) ) | |
28 | + { | |
29 | + include ($this->getDirectory() . 'MediaUtils.php'); | |
30 | + } | |
31 | + | |
32 | + return; | |
33 | + } | |
34 | + | |
35 | + public function event_PostAuthentication(&$data) | |
36 | + { | |
28 | 37 | global $CONF; |
29 | 38 | static $blogid = 0; |
30 | 39 | static $blogs = array(); |
31 | 40 | |
32 | - if (!class_exists('MediaUtils', FALSE)) { | |
33 | - include ($this->getDirectory() . 'MediaUtils.php'); | |
34 | - } | |
35 | - | |
36 | 41 | MediaUtils::$lib_path = preg_replace('#/*$#', '', $this->getDirectory()); |
37 | 42 | MediaUtils::$prefix = (boolean) $CONF['MediaPrefix']; |
38 | 43 | MediaUtils::$maxsize = (integer) $CONF['MaxUploadSize']; |
39 | 44 | |
40 | 45 | $suffixes = explode(',', $CONF['AllowedTypes']); |
41 | - foreach ($suffixes as $suffix) { | |
46 | + foreach ( $suffixes as $suffix ) | |
47 | + { | |
42 | 48 | $suffix = trim($suffix); |
43 | 49 | if(!in_array($suffix, MediaUtils::$suffixes)) { |
44 | 50 | MediaUtils::$suffixes[] = strtolower($suffix); |
@@ -46,77 +52,74 @@ class NP_MediaUtils extends NucleusPlugin { | ||
46 | 52 | } |
47 | 53 | |
48 | 54 | $result = sql_query('SELECT bnumber, bshortname FROM ' . sql_table('blog') . ';'); |
49 | - while(FALSE !== ($row = sql_fetch_assoc($result))) { | |
55 | + while ( FALSE !== ($row = sql_fetch_assoc($result)) ) | |
56 | + { | |
50 | 57 | $blogs[$row['bnumber']] = $row['bshortname']; |
51 | 58 | } |
52 | - MediaUtils::$blogs = $blogs; | |
59 | + MediaUtils::$blogs =& $blogs; | |
53 | 60 | |
54 | - if (array_key_exists('blogid', $_GET)) { | |
61 | + if ( array_key_exists('blogid', $_GET) ) | |
62 | + { | |
55 | 63 | $blogid = (integer) $_GET['blogid']; |
56 | - } else if (array_key_exists('blogid', $_POST)) { | |
64 | + } | |
65 | + else if ( array_key_exists('blogid', $_POST) ) | |
66 | + { | |
57 | 67 | $blogid = (integer) $_POST['blogid']; |
58 | - } else if (array_key_exists('itemid', $_GET)) { | |
68 | + } | |
69 | + else if ( array_key_exists('itemid', $_GET) && function_exists('getBlogIDFromItemID') ) | |
70 | + { | |
59 | 71 | $blogid = (integer) getBlogIDFromItemID((integer) $_GET['itemid']); |
60 | - } else if (array_key_exists('itemid', $_POST)) { | |
61 | - $blogid = (integer) getBlogIDFromItemID((integer) $_POST['itemid']); | |
62 | - } else if (array_key_exists(MediaUtils::$cookiename, $_COOKIE)) { | |
63 | - $blogid = (integer) $_COOKIE['blogid']; | |
64 | 72 | } |
65 | - | |
66 | - if (!$blogid || !array_key_exists($blogid, $blogs)) { | |
67 | - self::setCookie(-1); | |
68 | - return; | |
73 | + else if ( array_key_exists('itemid', $_POST) && function_exists('getBlogIDFromItemID') ) | |
74 | + { | |
75 | + $blogid = (integer) getBlogIDFromItemID((integer) $_POST['itemid']); | |
69 | 76 | } |
70 | - | |
71 | - MediaUtils::$blogid = (integer) $blogid; | |
72 | - MediaUtils::$bshortname = (string) $blogs[$blogid]; | |
73 | - self::setCookie(1); | |
74 | - | |
75 | - return; | |
76 | - } | |
77 | - | |
78 | - public function event_PreSendContentType($data) { | |
79 | - global $blog, $blogid; | |
80 | - if (MediaUtils::$blogid) { | |
81 | - return; | |
77 | + else if ( array_key_exists(MediaUtils::$cookiename, $_COOKIE) ) | |
78 | + { | |
79 | + $blogid = (integer) $_COOKIE['blogid']; | |
82 | 80 | } |
83 | - | |
84 | - if (!$blogid && !$blog) { | |
85 | - self::setCookie(-1); | |
81 | + else | |
82 | + { | |
86 | 83 | return; |
87 | 84 | } |
88 | 85 | |
89 | - if (!$blogid) { | |
90 | - MediaUtils::$blogid = $blog->getID(); | |
91 | - } else { | |
92 | - MediaUtils::$blogid = $blogid; | |
93 | - } | |
94 | - | |
95 | - if (!$blog) { | |
96 | - MediaUtils::$bshortname = $manager->getBlog(MediaUtils::$blogid)->getShortName(); | |
97 | - } else { | |
98 | - MediaUtils::$bshortname = $blog->getShortName(); | |
99 | - } | |
86 | + MediaUtils::$blogid =& $blogid; | |
87 | + MediaUtils::$bshortname =& MediaUtils::$blogs[MediaUtils::$blogid]; | |
100 | 88 | |
101 | - self::setCookie(1); | |
102 | 89 | return; |
103 | 90 | } |
104 | 91 | |
105 | - public function event_InitSkinParse($data) { | |
92 | + /* | |
93 | + * When index.php is directly called, there is no $blogid at first. | |
94 | + * $blogid is set when selector() is called after globalfunctions is included (selectBlog() is optional). | |
95 | + * In this case, plugin can finally get correct $blogid in InitSkinParse event. | |
96 | + * (Nucleus CMS 3.64, Apr. 06, 2011) | |
97 | + */ | |
98 | + public function event_InitSkinParse(&$data) | |
99 | + { | |
106 | 100 | global $blogid; |
107 | - if (MediaUtils::$blogid != $blogid) { | |
101 | + if ( MediaUtils::$blogid == 0 ) | |
102 | + { | |
108 | 103 | MediaUtils::$blogid = $blogid; |
109 | 104 | MediaUtils::$bshortname = MediaUtils::$blogs[MediaUtils::$blogid]; |
110 | - self::setCookie(1); | |
111 | 105 | } |
112 | 106 | return; |
113 | 107 | } |
114 | 108 | |
115 | - private function setCookie($factor) { | |
109 | + public function event_PreSendContentType($data) | |
110 | + { | |
116 | 111 | global $CONF; |
117 | - $factor = (integer) $factor; | |
118 | 112 | |
119 | - setcookie($CONF['CookiePrefix'] . MediaUtils::$cookiename, MediaUtils::$blogid, time()+180*$factor, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']); | |
113 | + /* delete my cookie */ | |
114 | + if ( MediaUtils::$blogid == 0 ) | |
115 | + { | |
116 | + setcookie($CONF['CookiePrefix'] . MediaUtils::$cookiename, MediaUtils::$blogid, -1, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']); | |
117 | + } | |
118 | + /* set my cookie */ | |
119 | + else | |
120 | + { | |
121 | + setcookie($CONF['CookiePrefix'] . MediaUtils::$cookiename, MediaUtils::$blogid, time()+180, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']); | |
122 | + } | |
120 | 123 | return; |
121 | 124 | } |
122 | 125 | } |
@@ -1,8 +1,8 @@ | ||
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * MediaUtils plugin for Nucleus CMS |
4 | - * Version 0.9.6 (1.0 RC2) for PHP5 | |
5 | - * Written By Mocchi, Apr. 04, 2011 | |
4 | + * Version 1.0.0 for PHP5 | |
5 | + * Written By Mocchi, Oct. 20, 2011 | |
6 | 6 | * |
7 | 7 | * This program is free software; you can redistribute it and/or |
8 | 8 | * modify it under the terms of the GNU General Public License |
@@ -10,11 +10,12 @@ | ||
10 | 10 | * of the License, or (at your option) any later version. |
11 | 11 | */ |
12 | 12 | |
13 | -class MediaUtils { | |
13 | +class MediaUtils | |
14 | +{ | |
14 | 15 | static public $lib_path = ''; |
15 | 16 | |
16 | 17 | static public $cookiename = 'blogid'; |
17 | - static public $blogid = FALSE; | |
18 | + static public $blogid = 0; | |
18 | 19 | static public $blogs = array(); |
19 | 20 | static public $bshortname = ''; |
20 | 21 |
@@ -23,167 +24,199 @@ class MediaUtils { | ||
23 | 24 | static public $suffixes = array(); |
24 | 25 | |
25 | 26 | static public $algorism = 'md5'; |
26 | - static public $image_mime | |
27 | - = array('image/jpeg' => '.jpeg', | |
28 | - 'image/png' => '.png', | |
29 | - /* 'image/bmp' => '.bmp', not implemented*/ | |
30 | - 'image/gif' => '.gif'); | |
27 | + static public $image_mime = array( | |
28 | + 'image/jpeg' => '.jpeg', | |
29 | + 'image/png' => '.png', | |
30 | + 'image/gif' => '.gif'); | |
31 | 31 | |
32 | -/** | |
33 | - * error and exit | |
34 | - * @access Public MediaUtils::error | |
35 | - * @param String $message Error message | |
36 | - * @exit | |
37 | - */ | |
38 | - static public function error ($message) { | |
39 | - (string) $message; | |
32 | + /** | |
33 | + * error and exit | |
34 | + * @access Public MediaUtils::error | |
35 | + * @param String $message Error message | |
36 | + * @exit | |
37 | + */ | |
38 | + static public function error($message) | |
39 | + { | |
40 | + $message = (string) $message; | |
40 | 41 | header("HTTP/1.0 404 Not Found"); |
41 | 42 | exit($message); |
42 | 43 | } |
43 | 44 | |
44 | -/** | |
45 | - * send resampled image via HTTP | |
46 | - * @access Public MediaUtils::responseResampledImage | |
47 | - * @param Object $medium Medium Object | |
48 | - * @exit | |
49 | - */ | |
50 | - static public function responseResampledImage ($medium) { | |
51 | - if (!class_exists('Medium', FALSE)) { | |
45 | + /** | |
46 | + * send resampled image via HTTP | |
47 | + * @access Public MediaUtils::responseResampledImage | |
48 | + * @param Object $medium Medium Object | |
49 | + * @exit | |
50 | + */ | |
51 | + static public function responseResampledImage($medium) | |
52 | + { | |
53 | + if ( !class_exists('Medium', FALSE) ) | |
54 | + { | |
52 | 55 | include(self::$lib_path . '/Medium.php'); |
53 | 56 | } |
54 | 57 | |
55 | - if ('Medium' !== get_class($medium)) { | |
56 | - self::error ('NP_MediaUtils: Fail to generate resampled image'); | |
58 | + if ( 'Medium' !== get_class($medium) ) | |
59 | + { | |
60 | + self::error('NP_MediaUtils: Fail to generate resampled image'); | |
57 | 61 | return; |
58 | 62 | } |
59 | 63 | |
60 | - if (FALSE === ($resampledimage = $medium->getResampledBinary(self::$image_mime))) { | |
61 | - unset ($resampledimage); | |
62 | - self::error ('NP_MediaUtils: Fail to generate resampled image'); | |
64 | + if ( FALSE === ($resampledimage = $medium->getResampledBinary(self::$image_mime)) ) | |
65 | + { | |
66 | + unset($resampledimage); | |
67 | + self::error('NP_MediaUtils: Fail to generate resampled image'); | |
63 | 68 | return; |
64 | 69 | } |
65 | 70 | |
66 | - header ('Content-type: ' . $medium->mime); | |
71 | + header('Content-type: ' . $medium->mime); | |
67 | 72 | echo $resampledimage; |
68 | - unset ($resampledimage); | |
73 | + unset($resampledimage); | |
69 | 74 | exit; |
70 | 75 | } |
71 | 76 | |
72 | -/** | |
73 | - * Store resampled image binary to filesystem as file | |
74 | - * @access Public MediaUtils::storeResampledImage | |
75 | - * @param String $root root directory for media | |
76 | - * @param String $path Relative path from root to destination | |
77 | - * @param Object $medium Medium Object | |
78 | - * @return Boolean TRUE/FALSE | |
79 | - */ | |
80 | - static public function storeResampledImage ($root, $target, $medium) { | |
81 | - if (!class_exists('Medium', FALSE)) { | |
77 | + /** | |
78 | + * Store resampled image binary to filesystem as file | |
79 | + * @access Public MediaUtils::storeResampledImage | |
80 | + * @param String $root root directory for media | |
81 | + * @param String $path Relative path from root to destination | |
82 | + * @param Object $medium Medium Object | |
83 | + * @return Boolean TRUE/FALSE | |
84 | + */ | |
85 | + static public function storeResampledImage($root, $target, $medium) | |
86 | + { | |
87 | + if ( !class_exists('Medium', FALSE) ) | |
88 | + { | |
82 | 89 | include(self::$lib_path . '/Medium.php'); |
83 | 90 | } |
84 | 91 | |
85 | - if ('Medium' !== get_class($medium)) { | |
92 | + if ( 'Medium' !== get_class($medium) ) | |
93 | + { | |
86 | 94 | return FALSE; |
87 | 95 | } |
88 | 96 | |
89 | - if (FALSE === ($resampledimage = $medium->getResampledBinary(self::$image_mime))) { | |
90 | - unset ($resampledimage); | |
97 | + if ( FALSE === ($resampledimage = $medium->getResampledBinary(self::$image_mime)) ) | |
98 | + { | |
99 | + unset($resampledimage); | |
91 | 100 | return FALSE; |
92 | 101 | } |
93 | 102 | |
94 | - if (FALSE === ($handle = @fopen ("{$root}/{$target}", 'w'))) { | |
95 | - unset ($resampledimage); | |
103 | + if ( FALSE === ($handle = @fopen("{$root}/{$target}", 'w')) ) | |
104 | + { | |
105 | + unset($resampledimage); | |
96 | 106 | return FALSE; |
97 | 107 | } |
98 | 108 | |
99 | - if (@fwrite ($handle, $resampledimage) === FALSE) { | |
100 | - unset ($resampledimage); | |
101 | - @unlink ("{$root}/{$target}"); | |
109 | + if ( @fwrite($handle, $resampledimage) === FALSE ) | |
110 | + { | |
111 | + unset($resampledimage); | |
112 | + @unlink("{$root}/{$target}"); | |
102 | 113 | return FALSE; |
103 | 114 | } |
104 | 115 | |
105 | - unset ($resampledimage); | |
106 | - fclose ($handle); | |
116 | + unset($resampledimage); | |
117 | + fclose($handle); | |
107 | 118 | |
108 | - if (@chmod ("{$root}/{$target}", 0744) === FALSE) { | |
109 | - @unlink ("{$root}/{$target}"); | |
119 | + if ( @chmod("{$root}/{$target}", 0744) === FALSE ) | |
120 | + { | |
121 | + @unlink("{$root}/{$target}"); | |
110 | 122 | return FALSE; |
111 | 123 | } |
112 | 124 | |
113 | 125 | return TRUE; |
114 | 126 | } |
115 | 127 | |
116 | -/** | |
117 | - * Check the path as directory and writable | |
118 | - * @access Public MediaUtils::checkDir | |
119 | - * @param String Fullpath | |
120 | - * @return Boolean TRUE/FALSE | |
121 | - */ | |
122 | - static public function checkDir ($fullpath) { | |
128 | + /** | |
129 | + * Check the path as directory and writable | |
130 | + * @access Public MediaUtils::checkDir | |
131 | + * @param String Fullpath | |
132 | + * @return Boolean TRUE/FALSE | |
133 | + */ | |
134 | + static public function checkDir($fullpath) | |
135 | + { | |
123 | 136 | $fullpath = (string) $fullpath; |
124 | 137 | |
125 | - if (file_exists ($fullpath)) { | |
126 | - if (is_dir ($fullpath) && is_writable ($fullpath)) { | |
138 | + if ( file_exists($fullpath) ) | |
139 | + { | |
140 | + if ( is_dir($fullpath) && is_writable($fullpath) ) | |
141 | + { | |
127 | 142 | return TRUE; |
128 | - } else { | |
143 | + } | |
144 | + else | |
145 | + { | |
129 | 146 | return FALSE; |
130 | 147 | } |
131 | - } else { | |
132 | - if (!@mkdir ($fullpath, 0777)) { | |
148 | + } | |
149 | + else | |
150 | + { | |
151 | + if ( !@mkdir($fullpath, 0777) ) | |
152 | + { | |
133 | 153 | return FALSE; |
134 | - } else { | |
154 | + } | |
155 | + else | |
156 | + { | |
135 | 157 | return TRUE; |
136 | 158 | } |
137 | 159 | } |
138 | 160 | } |
139 | 161 | |
140 | -/** | |
141 | - * Return file data list | |
142 | - * @access Public MediaUtils::getMediaList | |
143 | - * @param String $root path to media root directory | |
144 | - * @param Boolean $hidden show hidden files (.*) or not | |
145 | - * @param Boolean $recursive follow recursively or not | |
146 | - * @param Boolean $prefix analyze its prefix or not | |
147 | - * @param String $suffix limit by suffix | |
148 | - * @param String $path relative path from root to destination | |
149 | - * @return Array Array(Medium) | |
150 | - */ | |
151 | - static public function getMediaList ($root, $hidden=TRUE, $recursive=TRUE, $suffix='', $path='', $media=array()) { | |
162 | + /** | |
163 | + * Return file data list | |
164 | + * @access Public MediaUtils::getMediaList | |
165 | + * @param String $root path to media root directory | |
166 | + * @param Boolean $hidden show hidden files (.*) or not | |
167 | + * @param Boolean $recursive follow recursively or not | |
168 | + * @param Boolean $prefix analyze its prefix or not | |
169 | + * @param String $suffix limit by suffix | |
170 | + * @param String $path relative path from root to destination | |
171 | + * @return Array Array(Medium) | |
172 | + */ | |
173 | + static public function getMediaList($root, $hidden=TRUE, $recursive=TRUE, $suffix='', $path='', $media=array()) | |
174 | + { | |
152 | 175 | $root = (string) $root; |
153 | - $hidden = (boolean) $hidden; | |
176 | + $hidden = (boolean) $hidden; | |
154 | 177 | $recursive = (boolean) $recursive; |
155 | - $suffix = (string) $suffix; | |
178 | + $suffix = (string) $suffix; | |
156 | 179 | $path = (string) $path; |
157 | - $media = (array) $media; | |
180 | + $media = (array) $media; | |
158 | 181 | |
159 | - if (!class_exists('Medium', FALSE)) { | |
182 | + if ( !class_exists('Medium', FALSE) ) | |
183 | + { | |
160 | 184 | include(self::$lib_path . '/Medium.php'); |
161 | 185 | } |
162 | 186 | |
163 | 187 | $root = rtrim($root, '/'); |
164 | - if(!$root || !file_exists($root)) { | |
188 | + if ( !$root || !file_exists($root) ) | |
189 | + { | |
165 | 190 | return FALSE; |
166 | 191 | } |
167 | 192 | |
168 | 193 | $fullpath = $root; |
169 | - if ($path) { | |
194 | + if ( $path ) | |
195 | + { | |
170 | 196 | $path = trim($path, '/'); |
171 | 197 | $fullpath = "{$root}/{$path}"; |
172 | 198 | } |
173 | 199 | |
174 | - if (FALSE === ($handle = @opendir($fullpath))) { | |
200 | + if ( FALSE === ($handle = @opendir($fullpath)) ) | |
201 | + { | |
175 | 202 | return FALSE; |
176 | 203 | } |
177 | - while(FALSE !== ($filename = readdir($handle))) { | |
178 | - if ($filename !== '.' && $filename !== '..') { | |
179 | - if (($hidden && preg_match("#^\.#", $filename)) | |
180 | - || ($suffix && !preg_match("#\.$suffix$#", $filename))) { | |
204 | + while ( FALSE !== ($filename = readdir($handle)) ) | |
205 | + { | |
206 | + if ( $filename !== '.' && $filename !== '..' ) | |
207 | + { | |
208 | + if ( ($hidden && preg_match("#^\.#", $filename)) | |
209 | + || ($suffix && !preg_match("#\.$suffix$#", $filename)) ) | |
210 | + { | |
181 | 211 | continue; |
182 | 212 | } |
183 | 213 | |
184 | - if($recursive && filetype("{$fullpath}/{$filename}") === "dir") { | |
214 | + if ( $recursive && filetype("{$fullpath}/{$filename}") === "dir" ) | |
215 | + { | |
185 | 216 | $media = self::getMediaList($root, $hidden, $recursive, $suffix, trim("{$path}/{$filename}", '/'), $media); |
186 | - } else if ($path !== '' && filetype("{$fullpath}/{$filename}") === "file") { | |
217 | + } | |
218 | + else if ( $path !== '' && filetype("{$fullpath}/{$filename}") === "file" ) | |
219 | + { | |
187 | 220 | $media[] = new Medium($root, trim("{$path}/{$filename}", '/'), self::$prefix); |
188 | 221 | continue; |
189 | 222 | } |
@@ -193,102 +226,130 @@ class MediaUtils { | ||
193 | 226 | return $media; |
194 | 227 | } |
195 | 228 | |
196 | -/* | |
197 | - * Purge directory | |
198 | - * @access Public MediaUtils::purgeDir | |
199 | - * @param String $root path to media root directory | |
200 | - * @param String $path relative path from root to destination | |
201 | - * @return Array/FALSE $logs | |
202 | - * | |
203 | - */ | |
204 | - static public function purgeDir($root, $path='', $logs=array()) { | |
229 | + /** | |
230 | + * Purge directory | |
231 | + * @access Public MediaUtils::purgeDir | |
232 | + * @param String $root path to media root directory | |
233 | + * @param String $path relative path from root to destination | |
234 | + * @return Mixed $logs | |
235 | + */ | |
236 | + static public function purgeDir($root, $path='', $logs=array()) | |
237 | + { | |
205 | 238 | $root = (string) $root; |
206 | 239 | $path = (string) $path; |
207 | 240 | $logs = (array) $logs; |
208 | 241 | |
209 | 242 | $root = rtrim($root, '/'); |
210 | - if(!$root || !file_exists($root)) { | |
243 | + if ( !$root || !file_exists($root) ) | |
244 | + { | |
211 | 245 | return FALSE; |
212 | 246 | } |
213 | 247 | |
214 | 248 | $fullpath = $root; |
215 | - if ($path) { | |
249 | + if ( $path ) | |
250 | + { | |
216 | 251 | $path = trim($path, '/'); |
217 | 252 | $fullpath = "{$root}/{$path}"; |
218 | 253 | } |
219 | 254 | |
220 | - if (FALSE === ($handle = @opendir($fullpath))) { | |
255 | + if ( FALSE === ($handle = @opendir($fullpath)) ) | |
256 | + { | |
221 | 257 | return FALSE; |
222 | 258 | } |
223 | - while(FALSE !== ($filename = readdir($handle))) { | |
224 | - if ($filename !== '.' && $filename !== '..') { | |
225 | - if(filetype("{$fullpath}/{$filename}") === "dir") { | |
259 | + while ( FALSE !== ($filename = readdir($handle)) ) | |
260 | + { | |
261 | + if ( $filename !== '.' && $filename !== '..' ) | |
262 | + { | |
263 | + if ( filetype("{$fullpath}/{$filename}") === "dir" ) | |
264 | + { | |
226 | 265 | $logs = self::purgeDir($root, "{$path}/{$filename}", $logs); |
227 | - } else { | |
228 | - if(!unlink("{$root}/{$path}/{$filename}")) { | |
266 | + } | |
267 | + else | |
268 | + { | |
269 | + if ( !unlink("{$root}/{$path}/{$filename}") ) | |
270 | + { | |
229 | 271 | $logs[] = "Exists: {$path}/{$filename}"; |
230 | - } else { | |
272 | + } | |
273 | + else | |
274 | + { | |
231 | 275 | $logs[] = "Removed: {$path}/{$filename}"; |
232 | 276 | } |
233 | 277 | continue; |
234 | 278 | } |
235 | 279 | } |
236 | 280 | } |
237 | - if(!rmdir("{$root}/{$path}")) { | |
281 | + if ( !rmdir("{$root}/{$path}") ) | |
282 | + { | |
238 | 283 | $logs[] = "Exists: {$path}"; |
239 | - } else { | |
284 | + } | |
285 | + else | |
286 | + { | |
240 | 287 | $logs[] = "Removed: {$path}"; |
241 | 288 | } |
242 | 289 | return $logs; |
243 | 290 | } |
244 | - | |
245 | -/* | |
246 | - * Return path list under root | |
247 | - * @access Public MediaUtils::getPathList | |
248 | - * @param String $root full path to root directory | |
249 | - * @param String $path certain path to search | |
250 | - * @param Boolean $private use private directory or not | |
251 | - * @param Boolean $recursize search recursively or not | |
252 | - * @param Boolean $hidden do not list up the directory started with piriod or not | |
253 | - * @return String $name | |
254 | - * | |
255 | - */ | |
256 | - static public function getPathList($root, $path='', $private=FALSE, $hidden=TRUE, $recursive=TRUE) { | |
291 | + | |
292 | + /** | |
293 | + * Return path list under root | |
294 | + * @access Public MediaUtils::getPathList | |
295 | + * @param String $root full path to root directory | |
296 | + * @param String $path certain path to search | |
297 | + * @param Boolean $private use private directory or not | |
298 | + * @param Boolean $recursize search recursively or not | |
299 | + * @param Boolean $hidden do not list up the directory started with piriod or not | |
300 | + * @return String $name | |
301 | + */ | |
302 | + static public function getPathList($root, $path='', $private=FALSE, $hidden=TRUE, $recursive=TRUE) | |
303 | + { | |
257 | 304 | $root = (string) $root; |
258 | 305 | $path = (string) $path; |
259 | - $hidden = (boolean) $hidden; | |
260 | - $recursive = (boolean) $recursive; | |
306 | + $hidden = (boolean) $hidden; | |
307 | + $recursive = (boolean) $recursive; | |
261 | 308 | |
262 | 309 | $paths=array(); |
263 | 310 | |
264 | 311 | $root = rtrim($root, '/'); |
265 | - if(!$root || !file_exists($root)) { | |
312 | + if ( !$root || !file_exists($root) ) | |
313 | + { | |
266 | 314 | return FALSE; |
267 | 315 | } |
268 | 316 | |
269 | 317 | $fullpath = $root; |
270 | - if ($path) { | |
318 | + if ( $path ) | |
319 | + { | |
271 | 320 | $path = trim($path, '/'); |
272 | 321 | $fullpath = "{$root}/{$path}"; |
273 | 322 | } |
274 | 323 | |
275 | - if (FALSE === ($handle = @opendir($fullpath))) { | |
324 | + if ( FALSE === ($handle = @opendir($fullpath)) ) | |
325 | + { | |
276 | 326 | return FALSE; |
277 | 327 | } |
278 | - while (FALSE !== ($filename = readdir($handle))) { | |
279 | - if (in_array($filename, array('.', '..', 'CVS'))) { | |
328 | + while ( FALSE !== ($filename = readdir($handle)) ) | |
329 | + { | |
330 | + if ( in_array($filename, array('.', '..', 'CVS')) ) | |
331 | + { | |
280 | 332 | continue; |
281 | - } else if (is_file("{$fullpath}/{$filename}")) { | |
333 | + } | |
334 | + else if ( is_file("{$fullpath}/{$filename}") ) | |
335 | + { | |
282 | 336 | continue; |
283 | - } else if ($hidden && preg_match('#^\.#', $filename)) { | |
337 | + } | |
338 | + else if ($hidden && preg_match('#^\.#', $filename) ) | |
339 | + { | |
284 | 340 | continue; |
285 | - } else if ($private && is_numeric($filename) && $path==''&& $private != $filename) { | |
341 | + } | |
342 | + else if ( $private && is_numeric($filename) && $path==''&& $private != $filename ) | |
343 | + { | |
286 | 344 | continue; |
287 | 345 | } |
288 | 346 | |
289 | - if (!$path) { | |
347 | + if ( !$path ) | |
348 | + { | |
290 | 349 | $relpath = $filename; |
291 | - } else { | |
350 | + } | |
351 | + else | |
352 | + { | |
292 | 353 | $relpath = "{$path}/{$filename}"; |
293 | 354 | } |
294 | 355 |
@@ -296,8 +357,10 @@ class MediaUtils { | ||
296 | 357 | } |
297 | 358 | closedir($handle); |
298 | 359 | |
299 | - if ($path=='' && $private) { | |
300 | - if (!array_key_exists($private, $paths)) { | |
360 | + if ( $path=='' && $private ) | |
361 | + { | |
362 | + if ( !array_key_exists($private, $paths) ) | |
363 | + { | |
301 | 364 | $paths[$private] = array('root'=>$root , 'path'=>$private, 'files'=>0, 'dirs'=>0); |
302 | 365 | } |
303 | 366 | $paths[$private]['label'] = 'PRIVATE'; |
@@ -307,61 +370,76 @@ class MediaUtils { | ||
307 | 370 | return $paths; |
308 | 371 | } |
309 | 372 | |
310 | -/* | |
311 | - * Return path data | |
312 | - * @access Public MediaUtils::getPathData | |
313 | - * @param String $root full path to root directory | |
314 | - * @param String $path relative path from root to target directory | |
315 | - * @param Boolean $private use private directory or not | |
316 | - * @param Boolean $hidden do not list up the directory started with piriod or not | |
317 | - * @param Boolean $recursive search recursively or not | |
318 | - * @return Array Array('root', 'parent', 'name', 'files', 'dirs', 'label') | |
319 | - */ | |
320 | - static public function getPathData($root, $path, $private=FALSE, $hidden=TRUE, $recursive=FALSE, $paths=array()) { | |
373 | + /** | |
374 | + * Return path data | |
375 | + * @access Public MediaUtils::getPathData | |
376 | + * @param String $root full path to root directory | |
377 | + * @param String $path relative path from root to target directory | |
378 | + * @param Boolean $private use private directory or not | |
379 | + * @param Boolean $hidden do not list up the directory started with piriod or not | |
380 | + * @param Boolean $recursive search recursively or not | |
381 | + * @return Array Array('root', 'parent', 'name', 'files', 'dirs', 'label') | |
382 | + */ | |
383 | + static public function getPathData($root, $path, $private=FALSE, $hidden=TRUE, $recursive=FALSE, $paths=array()) | |
384 | + { | |
321 | 385 | $root = (string) $root; |
322 | 386 | $path = (string) $path; |
323 | 387 | $private = (boolean) $private; |
324 | - $hidden = (boolean) $hidden; | |
325 | - $recursive = (boolean) $recursive; | |
388 | + $hidden = (boolean) $hidden; | |
389 | + $recursive = (boolean) $recursive; | |
326 | 390 | |
327 | 391 | $cnt_files = 0; |
328 | 392 | $cnt_dirs = 0; |
329 | 393 | |
330 | 394 | $root = rtrim($root, '/'); |
331 | - if(!$root || !file_exists($root)) { | |
395 | + if ( !$root || !file_exists($root) ) | |
396 | + { | |
332 | 397 | return FALSE; |
333 | 398 | } |
334 | 399 | |
335 | 400 | $fullpath = $root; |
336 | - if ($path) { | |
401 | + if ( $path ) | |
402 | + { | |
337 | 403 | $path = trim($path, '/'); |
338 | 404 | $fullpath = "{$root}/{$path}"; |
339 | 405 | } |
340 | 406 | |
341 | - if (FALSE === ($handle = @opendir($fullpath))) { | |
407 | + if ( FALSE === ($handle = @opendir($fullpath)) ) | |
408 | + { | |
342 | 409 | return FALSE; |
343 | 410 | } |
344 | - while (FALSE !== ($filename = readdir($handle))) { | |
345 | - if (in_array($filename, array('.', '..', 'CVS'))) { | |
411 | + while ( FALSE !== ($filename = readdir($handle)) ) | |
412 | + { | |
413 | + if ( in_array($filename, array('.', '..', 'CVS')) ) | |
414 | + { | |
346 | 415 | continue; |
347 | - } else if (!is_dir("{$fullpath}/{$filename}")) { | |
348 | - if (!$hidden || !preg_match('#^\.#', $filename)){ | |
416 | + } | |
417 | + else if ( !is_dir("{$fullpath}/{$filename}") ) | |
418 | + { | |
419 | + if ( !$hidden || !preg_match('#^\.#', $filename) ) | |
420 | + { | |
349 | 421 | $cnt_files++; |
350 | 422 | } |
351 | 423 | continue; |
352 | - } else if ($hidden && preg_match('#^\.#', $filename)) { | |
424 | + } | |
425 | + else if ( $hidden && preg_match('#^\.#', $filename) ) | |
426 | + { | |
353 | 427 | continue; |
354 | 428 | } |
355 | 429 | |
356 | 430 | $cnt_dirs++; |
357 | 431 | |
358 | - if (!$path) { | |
432 | + if ( !$path ) | |
433 | + { | |
359 | 434 | $relpath = $filename; |
360 | - } else { | |
435 | + } | |
436 | + else | |
437 | + { | |
361 | 438 | $relpath = "{$path}/{$filename}"; |
362 | 439 | } |
363 | 440 | |
364 | - if ($recursive) { | |
441 | + if ( $recursive ) | |
442 | + { | |
365 | 443 | $paths = self::getPathData($root, $relpath, $private, $recursive, $hidden, $paths); |
366 | 444 | } |
367 | 445 | } |
@@ -372,44 +450,52 @@ class MediaUtils { | ||
372 | 450 | $paths[$path]['name'] = basename($fullpath); |
373 | 451 | $paths[$path]['files'] = $cnt_files; |
374 | 452 | $paths[$path]['dirs'] = $cnt_dirs; |
375 | - if ($private) { | |
453 | + if ( $private ) | |
454 | + { | |
376 | 455 | $paths[$path]['label'] = preg_replace("#^$private#", 'PRIVATE', $path); |
377 | - } else { | |
456 | + } | |
457 | + else | |
458 | + { | |
378 | 459 | $paths[$path]['label'] = $path; |
379 | 460 | } |
380 | 461 | |
381 | 462 | return $paths; |
382 | 463 | } |
383 | 464 | |
384 | -/* | |
385 | - * Store uploaded binary to filesystem | |
386 | - * @access Public MediaUtils::uploadMedium | |
387 | - * @param String $root path to edia root directory | |
388 | - * @param String $path relative path from root to target directory | |
389 | - * @param Array $medium uploaded binary data. | |
390 | - * @param String/FALSE $overwrite overwrite or not if the file already exists | |
391 | - * @param Object $manager Nucleus Manager Object | |
392 | - * @return String $log Return '' if success, others is error messages | |
393 | - * | |
394 | - */ | |
395 | - static public function uploadMedium ($root, $path, &$temp, $overwrite='', &$manager='') { | |
396 | - $root = (string) $root; | |
397 | - $path = (string) $path; | |
398 | - $temp = (array) $temp; | |
399 | - $overwrite = (string) $overwrite; | |
400 | - $manager = (object) $manager; | |
465 | + /** | |
466 | + * Store uploaded binary to filesystem | |
467 | + * @access Public MediaUtils::uploadMedium | |
468 | + * @param String $root path to edia root directory | |
469 | + * @param String $path relative path from root to target directory | |
470 | + * @param Array $medium uploaded binary data. | |
471 | + * @param Mixed $overwrite overwrite or not if the file already exists | |
472 | + * @param Object $manager Nucleus Manager Object | |
473 | + * @return String $log Return '' if success, others is error messages | |
474 | + */ | |
475 | + static public function uploadMedium($root, $path, &$temp, $overwrite='', &$manager='') | |
476 | + { | |
477 | + global $manager; | |
478 | + | |
479 | + $root = (string) $root; | |
480 | + $path = (string) $path; | |
481 | + $temp = (array) $temp; | |
482 | + $overwrite = (string) $overwrite; | |
483 | + $manager = (object) $manager; | |
401 | 484 | |
402 | 485 | /** |
403 | 486 | * $temp should be derived from $_FILE |
404 | 487 | */ |
405 | - foreach(array('name', 'tmp_name','size', 'error') as $key) { | |
406 | - if (!array_key_exists($key, $temp)) { | |
488 | + foreach ( array('name', 'tmp_name','size', 'error') as $key ) | |
489 | + { | |
490 | + if ( !array_key_exists($key, $temp) ) | |
491 | + { | |
407 | 492 | return 'NP_MediaUtils: Miss uploaded file.'; |
408 | 493 | } |
409 | 494 | } |
410 | 495 | |
411 | 496 | $root = rtrim($root, '/'); |
412 | - if(!$root || !file_exists($root) || !$path) { | |
497 | + if ( !$root || !file_exists($root) || !$path ) | |
498 | + { | |
413 | 499 | return 'NP_MediaUtils: Destination is invalid.'; |
414 | 500 | } |
415 | 501 | $path = trim($path, '/'); |
@@ -417,7 +503,8 @@ class MediaUtils { | ||
417 | 503 | /** |
418 | 504 | * see http://www.php.net/manual/en/features.file-upload.errors.php |
419 | 505 | */ |
420 | - switch ($temp['error']) { | |
506 | + switch ( $temp['error'] ) | |
507 | + { | |
421 | 508 | case UPLOAD_ERR_OK: |
422 | 509 | break; |
423 | 510 | case UPLOAD_ERR_INI_SIZE: |
@@ -432,48 +519,68 @@ class MediaUtils { | ||
432 | 519 | return 'NP_MediaUtils: Request rejected'; |
433 | 520 | } |
434 | 521 | |
435 | - if (preg_match ("#(\\\\|/|\\n)#", $temp['name'])) { | |
522 | + if ( preg_match ("#(\\\\|/|\\n)#", $temp['name']) ) | |
523 | + { | |
436 | 524 | return 'NP_MediaUtils: invalid filename'; |
437 | 525 | } |
438 | 526 | |
439 | - if ($temp['size'] > self::$maxsize) { | |
527 | + if ( $temp['size'] > self::$maxsize ) | |
528 | + { | |
440 | 529 | return 'NP_MediaUtils: Binary is too big'; |
441 | 530 | } |
442 | 531 | |
443 | - if (!empty(self::$suffixes) && is_array(self::$suffixes)) { | |
532 | + if ( !empty(self::$suffixes) && is_array(self::$suffixes) ) | |
533 | + { | |
444 | 534 | preg_match("#\.(.+)$#", $temp['name'], $match); |
445 | 535 | $suffix = strtolower($match[1]); |
446 | - if (!in_array($suffix, self::$suffixes)) { | |
536 | + if ( !in_array($suffix, self::$suffixes) ) | |
537 | + { | |
447 | 538 | return 'NP_MediaUtils: Forbidden file suffix'; |
448 | 539 | } |
449 | 540 | } |
450 | 541 | |
451 | - if (!self::checkDir("{$root}/{$path}")) { | |
542 | + if ( !self::checkDir("{$root}/{$path}") ) | |
543 | + { | |
452 | 544 | return 'NP_MediaUtils: Invalid target directory'; |
453 | 545 | } |
454 | 546 | |
455 | - if ($overwrite) { | |
456 | - if (!preg_match("#\.($suffix)$#", strtolower($overwrite), $match)) { | |
547 | + if ( $overwrite ) | |
548 | + { | |
549 | + if ( !preg_match("#\.($suffix)$#", strtolower($overwrite), $match) ) | |
550 | + { | |
457 | 551 | return 'NP_MediaUtils: suffix is not the same.'; |
458 | 552 | } |
459 | 553 | $temp['name'] = $overwrite; |
460 | - } else if (self::$prefix) { | |
461 | - $temp['name'] = strftime ("%Y%m%d-", time ()) . $temp['name']; | |
554 | + } | |
555 | + else if ( self::$prefix ) | |
556 | + { | |
557 | + $temp['name'] = strftime("%Y%m%d-", time()) . $temp['name']; | |
462 | 558 | } |
463 | 559 | |
464 | - if (!$overwrite && file_exists("{$root}/{$path}/{$temp['name']}")) { | |
560 | + if ( !$overwrite && file_exists("{$root}/{$path}/{$temp['name']}") ) | |
561 | + { | |
465 | 562 | return 'NP_MediaUtils: The same filename already exists in this target directory.'; |
466 | 563 | } |
467 | 564 | |
468 | - if ($manager) { | |
469 | - $manager->notify('PreMediaUpload',array('collection' => &$path, 'uploadfile' => $temp['tmp_name'], 'filename' => $temp['name'])); | |
565 | + if ( $manager ) | |
566 | + { | |
567 | + $params = array( | |
568 | + 'collection' => &$path, | |
569 | + 'uploadfile' => $temp['tmp_name'], | |
570 | + 'filename' => $temp['name'] | |
571 | + ); | |
572 | + $manager->notify('PreMediaUpload', $params); | |
470 | 573 | } |
471 | 574 | |
472 | - if (is_uploaded_file($temp['tmp_name'])) { | |
473 | - if (!@move_uploaded_file($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}")) { | |
575 | + if ( is_uploaded_file($temp['tmp_name']) ) | |
576 | + { | |
577 | + if ( !@move_uploaded_file($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}") ) | |
578 | + { | |
474 | 579 | return 'NP_MediaUtils: Fail to move uploaded binary to file sytem.'; |
475 | 580 | } |
476 | - } else if (!copy($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}")) { | |
581 | + } | |
582 | + else if ( !copy($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}") ) | |
583 | + { | |
477 | 584 | return 'NP_MediaUtils: Fail to copy uploaded binary to file sytem.'; |
478 | 585 | } |
479 | 586 |
@@ -481,7 +588,8 @@ class MediaUtils { | ||
481 | 588 | @chmod("{$root}/{$path}/{$temp['name']}", 0644); |
482 | 589 | umask($oldumask); |
483 | 590 | |
484 | - if ($manager) { | |
591 | + if ( $manager ) | |
592 | + { | |
485 | 593 | $manager->notify('PostMediaUpload',array('collection' => $path, 'mediadir' => $root, 'filename' => $temp['name'])); |
486 | 594 | } |
487 | 595 |
@@ -1,8 +1,8 @@ | ||
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * MediaUtils plugin for Nucleus CMS |
4 | - * Version 0.9.6 (1.0 RC2) for PHP5 | |
5 | - * Written By Mocchi, Apr. 04, 2011 | |
4 | + * Version 1.0.0 for PHP5 | |
5 | + * Written By Mocchi, Oct. 20, 2011 | |
6 | 6 | * |
7 | 7 | * This program is free software; you can redistribute it and/or |
8 | 8 | * modify it under the terms of the GNU General Public License |
@@ -10,7 +10,8 @@ | ||
10 | 10 | * of the License, or (at your option) any later version. |
11 | 11 | */ |
12 | 12 | |
13 | -class Medium { | |
13 | +class Medium | |
14 | +{ | |
14 | 15 | public $root = ''; |
15 | 16 | public $path = ''; |
16 | 17 | public $name = ''; |
@@ -25,28 +26,31 @@ class Medium { | ||
25 | 26 | public $resampledwidth = 0; |
26 | 27 | public $resampledheight = 0; |
27 | 28 | |
28 | -/** | |
29 | - * Return MEDIUM Object | |
30 | - * @access Public $this->__construnct | |
31 | - * @param String $root | |
32 | - * @param String $relativepath | |
33 | - * @param Boolean $prefix | |
34 | - * @return Object/FALSE | |
35 | - */ | |
36 | - public function __construct ($root, $relativepath, $prefix) { | |
29 | + /** | |
30 | + * construct instance of Medium | |
31 | + * @access Public $this->__construnct | |
32 | + * @param String $root | |
33 | + * @param String $relativepath | |
34 | + * @param Boolean $prefix | |
35 | + * @return Object/FALSE | |
36 | + */ | |
37 | + public function __construct($root, $relativepath, $prefix) | |
38 | + { | |
37 | 39 | static $fullpath; |
38 | 40 | static $info; |
39 | 41 | |
40 | - if ($root == '' || $relativepath == '') { | |
42 | + if ( $root == '' || $relativepath == '' ) | |
43 | + { | |
41 | 44 | return FALSE; |
42 | 45 | } |
43 | 46 | |
44 | 47 | $root = preg_replace('#/*$#', '', $root); |
45 | - if ($root == '' || $relativepath == '' | |
48 | + if ( $root == '' || $relativepath == '' | |
46 | 49 | || !file_exists($root) |
47 | 50 | || FALSE === ($fullpath = realpath(rtrim($root . '/' . ltrim($relativepath, '/'), '/'))) |
48 | 51 | || strpos($fullpath, $root) !== 0 |
49 | - || !file_exists($fullpath)) { | |
52 | + || !file_exists($fullpath) ) | |
53 | + { | |
50 | 54 | return FALSE; |
51 | 55 | } |
52 | 56 |
@@ -54,34 +58,41 @@ class Medium { | ||
54 | 58 | $this->name = basename($fullpath); |
55 | 59 | $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath); |
56 | 60 | |
57 | - if ($this->path === $this->name) { | |
61 | + if ( $this->path === $this->name ) | |
62 | + { | |
58 | 63 | $this->path = ''; |
59 | 64 | } |
60 | 65 | |
61 | - if (FALSE === ($info = @getimagesize ($fullpath))) { | |
66 | + if ( FALSE === ($info = @getimagesize($fullpath)) ) | |
67 | + { | |
62 | 68 | $this->mime = 'application/octet-stream'; |
63 | 69 | $this->width = 0; |
64 | 70 | $this->height = 0; |
65 | - } else { | |
71 | + } | |
72 | + else | |
73 | + { | |
66 | 74 | $this->mime = $info['mime']; |
67 | 75 | $this->width = $info[0]; |
68 | 76 | $this->height = $info[1]; |
69 | 77 | } |
70 | 78 | |
71 | 79 | set_time_limit(ini_get('max_execution_time')); |
72 | - if (defined('FILEINFO_MIME_TYPE') | |
73 | - && function_exists ('finfo_open') | |
74 | - && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE)))) { | |
80 | + if ( defined('FILEINFO_MIME_TYPE') | |
81 | + && function_exists('finfo_open') | |
82 | + && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE))) ) | |
83 | + { | |
75 | 84 | $this->mime = finfo_file($info, $fullpath); |
76 | 85 | } |
77 | 86 | |
78 | 87 | $this->update = date("Y/m/d", @filemtime($fullpath)); |
79 | 88 | $this->size = ceil(filesize($fullpath) / 1000); |
80 | 89 | |
81 | - if (preg_match('#^(.*)\.([a-zA-Z0-9]{2,})$#', $this->name, $info) === 1) { | |
90 | + if ( preg_match('#^(.*)\.([a-zA-Z0-9]{2,})$#', $this->name, $info) === 1 ) | |
91 | + { | |
82 | 92 | $this->filename = $info[1]; |
83 | 93 | $this->suffix = $info[2]; |
84 | - if ($prefix && preg_match('#^([0-9]{8})\-(.*)$#', $this->filename, $info) == 1 ) { | |
94 | + if ( $prefix && preg_match('#^([0-9]{8})\-(.*)$#', $this->filename, $info) == 1 ) | |
95 | + { | |
85 | 96 | $this->prefix = preg_replace('#^([0-9]{4})([0-9]{2})([0-9]{2})$#', '$1/$2/$3', $info[1]); |
86 | 97 | $this->filename = $info[2]; |
87 | 98 | } |
@@ -90,30 +101,41 @@ class Medium { | ||
90 | 101 | return $this; |
91 | 102 | } |
92 | 103 | |
93 | - public function __destruct () { | |
104 | + public function __destruct() | |
105 | + { | |
94 | 106 | return; |
95 | 107 | } |
96 | 108 | |
97 | -/** | |
98 | - * Set resampled size | |
99 | - * @access Public $this->setResampledSize | |
100 | - * @param Integer $maxwidth | |
101 | - * @param Integer $maxheight | |
102 | - * @return Boolean | |
103 | - */ | |
104 | - public function setResampledSize($maxwidth=0, $maxheight=0) { | |
105 | - if (($maxwidth == 0) && ($maxheight == 0)) { | |
109 | + /** | |
110 | + * Set resampled size | |
111 | + * @access Public $this->setResampledSize | |
112 | + * @param Integer $maxwidth | |
113 | + * @param Integer $maxheight | |
114 | + * @return Boolean | |
115 | + */ | |
116 | + public function setResampledSize($maxwidth=0, $maxheight=0) | |
117 | + { | |
118 | + if ( ($maxwidth == 0) && ($maxheight == 0) ) | |
119 | + { | |
106 | 120 | return FALSE; |
107 | - } else if ($this->width == 0 || $this->height == 0) { | |
121 | + } | |
122 | + else if ( $this->width == 0 || $this->height == 0 ) | |
123 | + { | |
108 | 124 | return FALSE; |
109 | - } else if ($this->width < $maxwidth && $this->height < $maxheight) { | |
125 | + } | |
126 | + else if ( $this->width < $maxwidth && $this->height < $maxheight ) | |
127 | + { | |
110 | 128 | $this->resampledwidth = $this->width; |
111 | 129 | $this->resampledheight = $this->height; |
112 | - } else if ($maxheight == 0 || $this->width > $this->height) { | |
113 | - $this->resampledheight = intval ($this->height * $maxwidth / $this->width); | |
130 | + } | |
131 | + else if ( $maxheight == 0 || $this->width > $this->height ) | |
132 | + { | |
133 | + $this->resampledheight = intval($this->height * $maxwidth / $this->width); | |
114 | 134 | $this->resampledwidth = $maxwidth; |
115 | - } else if ($maxwidth == 0 || $this->width <= $this->height) { | |
116 | - $this->resampledwidth = intval ($this->width * $maxheight / $this->height); | |
135 | + } | |
136 | + else if ( $maxwidth == 0 || $this->width <= $this->height ) | |
137 | + { | |
138 | + $this->resampledwidth = intval($this->width * $maxheight / $this->height); | |
117 | 139 | $this->resampledheight = $maxheight; |
118 | 140 | } |
119 | 141 | return TRUE; |
@@ -126,104 +148,130 @@ class Medium { | ||
126 | 148 | * @param Integer $maxheight |
127 | 149 | * @return Boolean |
128 | 150 | */ |
129 | - public function getResampledBinary ($image_mime) { | |
151 | + public function getResampledBinary($image_mime) | |
152 | + { | |
130 | 153 | static $gdinfo; |
131 | 154 | static $original; |
132 | 155 | static $resampledimage; |
133 | 156 | |
134 | 157 | $gdinfo = gd_info(); |
135 | 158 | |
136 | - if ($this->path !== '') { | |
159 | + if ( $this->path !== '' ) | |
160 | + { | |
137 | 161 | $fullpath = "{$this->root}/{$this->path}/{$this->name}"; |
138 | - } else { | |
162 | + } | |
163 | + else | |
164 | + { | |
139 | 165 | $fullpath = "{$this->root}/{$this->name}"; |
140 | 166 | } |
141 | - if (!file_exists($fullpath)) { | |
167 | + if ( !file_exists($fullpath) ) | |
168 | + { | |
142 | 169 | return FALSE; |
143 | 170 | } |
144 | 171 | |
145 | - if (!array_key_exists($this->mime, $image_mime) | |
172 | + if ( !array_key_exists($this->mime, $image_mime) | |
146 | 173 | || $this->width == 0 |
147 | 174 | || $this->height == 0 |
148 | 175 | || $this->resampledwidth == 0 |
149 | - || $this->resampledheight == 0) { | |
150 | - return FALSE; | |
176 | + || $this->resampledheight == 0 ) | |
177 | + { | |
178 | + return FALSE; | |
151 | 179 | } |
152 | 180 | |
153 | 181 | // check current available memory |
154 | 182 | $memorymax = trim(ini_get("memory_limit")); |
155 | - switch (strtolower ($memorymax[strlen($memorymax)-1])) { | |
156 | - case 'g': | |
157 | - $memorymax *= 1024; | |
158 | - case 'm': | |
159 | - $memorymax *= 1024; | |
160 | - case 'k': | |
161 | - $memorymax *= 1024; | |
183 | + switch ( strtolower($memorymax[strlen($memorymax)-1]) ) | |
184 | + { | |
185 | + case 'g': | |
186 | + $memorymax *= 1024; | |
187 | + case 'm': | |
188 | + $memorymax *= 1024; | |
189 | + case 'k': | |
190 | + $memorymax *= 1024; | |
162 | 191 | } |
163 | 192 | |
164 | 193 | // this code is based on analyze if gd.c in php source code |
165 | 194 | // if you can read C/C++, please check these elements and notify us if you have some ideas |
166 | - if ((memory_get_usage() + ($this->resampledwidth * $this->resampledheight * 5 + $this->resampledheight * 24 + 10000) + ($this->width * $this->height * 5 + $this->height * 24 + 10000)) > $memorymax) { | |
195 | + if ( (memory_get_usage() + ($this->resampledwidth * $this->resampledheight * 5 + $this->resampledheight * 24 + 10000) + ($this->width * $this->height * 5 + $this->height * 24 + 10000)) > $memorymax ) | |
196 | + { | |
167 | 197 | return FALSE; |
168 | 198 | } |
169 | 199 | |
170 | - switch ($this->mime) { | |
200 | + switch ( $this->mime ) | |
201 | + { | |
171 | 202 | case 'image/gif': |
172 | - if (!$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support']) { | |
203 | + if ( !$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support'] ) | |
204 | + { | |
173 | 205 | return FALSE; |
174 | 206 | } |
175 | - $original = imagecreatefromgif ($fullpath); | |
207 | + $function = 'imagecreatefromgif'; | |
176 | 208 | break; |
177 | 209 | case 'image/jpeg': |
178 | - if ((array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support']) && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support'])) { | |
210 | + if ( (array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support']) | |
211 | + && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support']) ) | |
212 | + { | |
179 | 213 | return FALSE; |
180 | 214 | } |
181 | - $original = imagecreatefromjpeg ($fullpath); | |
215 | + $function = 'imagecreatefromjpeg'; | |
182 | 216 | break; |
183 | 217 | case 'image/png': |
184 | - if (!$gdinfo['PNG Support']) { | |
218 | + if ( !$gdinfo['PNG Support'] ) | |
219 | + { | |
185 | 220 | return FALSE; |
186 | 221 | } |
187 | - $original = imagecreatefrompng ($fullpath); | |
222 | + $function = 'imagecreatefrompng'; | |
188 | 223 | break; |
189 | 224 | default: |
190 | 225 | return FALSE; |
191 | 226 | } |
192 | 227 | |
193 | - $resampledimage = imagecreatetruecolor ($this->resampledwidth, $this->resampledheight); | |
228 | + if ( !is_callable($function) ) | |
229 | + { | |
230 | + return FALSE; | |
231 | + } | |
194 | 232 | |
195 | - if (!$resampledimage) { | |
233 | + if ( FALSE === ($original = call_user_func_array($function,array(&$fullpath))) ) | |
234 | + { | |
196 | 235 | return FALSE; |
197 | 236 | } |
198 | 237 | |
199 | - set_time_limit(ini_get('max_execution_time')); | |
200 | - if (!ImageCopyResampled ($resampledimage, $original, 0, 0, 0, 0, $this->resampledwidth, $this->resampledheight, $this->width, $this->height)) { | |
238 | + if ( FALSE === ($resampledimage = imagecreatetruecolor($this->resampledwidth, $this->resampledheight)) ) | |
239 | + { | |
201 | 240 | return FALSE; |
202 | 241 | } |
203 | 242 | |
204 | - imagedestroy ($original); | |
243 | + @set_time_limit(ini_get('max_execution_time')); | |
244 | + if ( !ImageCopyResampled($resampledimage, $original, 0, 0, 0, 0, $this->resampledwidth, $this->resampledheight, $this->width, $this->height) ) | |
245 | + { | |
246 | + return FALSE; | |
247 | + } | |
248 | + | |
249 | + imagedestroy($original); | |
205 | 250 | |
206 | 251 | ob_start(); |
207 | 252 | |
208 | - switch ($this->mime) { | |
209 | - case 'image/gif': | |
210 | - imagegif ($resampledimage); | |
211 | - break; | |
212 | - case 'image/jpeg': | |
213 | - imagejpeg ($resampledimage); | |
214 | - break; | |
215 | - case 'image/png': | |
216 | - imagepng ($resampledimage); | |
217 | - break; | |
218 | - default: | |
253 | + switch ( $this->mime ) | |
254 | + { | |
255 | + case 'image/gif': | |
256 | + imagegif($resampledimage); | |
257 | + break; | |
258 | + case 'image/jpeg': | |
259 | + imagejpeg($resampledimage); | |
260 | + break; | |
261 | + case 'image/png': | |
262 | + imagepng($resampledimage); | |
263 | + break; | |
264 | + default: | |
265 | + return FALSE; | |
219 | 266 | } |
220 | 267 | |
221 | - imagedestroy ($resampledimage); | |
268 | + imagedestroy($resampledimage); | |
222 | 269 | |
223 | 270 | return ob_get_clean(); |
224 | 271 | } |
225 | 272 | |
226 | - public function getHashedName($algorism) { | |
273 | + public function getHashedName($algorism) | |
274 | + { | |
227 | 275 | return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE); |
228 | 276 | } |
229 | 277 | } |