• R/O
  • HTTP
  • SSH
  • HTTPS

nucleus-plugins: Commit

Nucleus CMS日本語版用プラグインのうち、日本語版開発者がサポートしているもの


Commit MetaInfo

Revision9fb5c297afb4b8c88574809a6fa9bb61149c3f17 (tree)
Time2012-10-20 15:09:53
Authorsakamocchi <o-takashi@saka...>
Commitersakamocchi

Log Message

FIX: NP_MediaUtilsをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正

Change Summary

Incremental Difference

--- a/NP_MediaUtils/NP_MediaUtils.php
+++ b/NP_MediaUtils/NP_MediaUtils.php
@@ -1,8 +1,8 @@
11 <?php
22 /**
33 * 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
66 *
77 * This program is free software; you can redistribute it and/or
88 * modify it under the terms of the GNU General Public License
@@ -10,35 +10,41 @@
1010 * of the License, or (at your option) any later version.
1111 */
1212
13-class NP_MediaUtils extends NucleusPlugin {
13+class NP_MediaUtils extends NucleusPlugin
14+{
1415 public function getName() { return 'MediaUtils'; }
1516 public function getAuthor() { return 'Mocchi'; }
1617 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'; }
1819 public function getDescription() { return 'Load MediaUtils, static function set for media management. Another function of this plugin is keeping Cookies for identifying weblog id.'; }
1920
2021 public function getMinNucleusVersion() { return 340; }
2122 public function supportsFeature($feature) { return in_array ($feature, array ('SqlTablePrefix', 'SqlApi')); }
2223 public function getEventList() { return array('PostAuthentication', 'InitSkinParse', 'PreSendContentType'); }
2324
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+ {
2837 global $CONF;
2938 static $blogid = 0;
3039 static $blogs = array();
3140
32- if (!class_exists('MediaUtils', FALSE)) {
33- include ($this->getDirectory() . 'MediaUtils.php');
34- }
35-
3641 MediaUtils::$lib_path = preg_replace('#/*$#', '', $this->getDirectory());
3742 MediaUtils::$prefix = (boolean) $CONF['MediaPrefix'];
3843 MediaUtils::$maxsize = (integer) $CONF['MaxUploadSize'];
3944
4045 $suffixes = explode(',', $CONF['AllowedTypes']);
41- foreach ($suffixes as $suffix) {
46+ foreach ( $suffixes as $suffix )
47+ {
4248 $suffix = trim($suffix);
4349 if(!in_array($suffix, MediaUtils::$suffixes)) {
4450 MediaUtils::$suffixes[] = strtolower($suffix);
@@ -46,77 +52,74 @@ class NP_MediaUtils extends NucleusPlugin {
4652 }
4753
4854 $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+ {
5057 $blogs[$row['bnumber']] = $row['bshortname'];
5158 }
52- MediaUtils::$blogs = $blogs;
59+ MediaUtils::$blogs =& $blogs;
5360
54- if (array_key_exists('blogid', $_GET)) {
61+ if ( array_key_exists('blogid', $_GET) )
62+ {
5563 $blogid = (integer) $_GET['blogid'];
56- } else if (array_key_exists('blogid', $_POST)) {
64+ }
65+ else if ( array_key_exists('blogid', $_POST) )
66+ {
5767 $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+ {
5971 $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'];
6472 }
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']);
6976 }
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'];
8280 }
83-
84- if (!$blogid && !$blog) {
85- self::setCookie(-1);
81+ else
82+ {
8683 return;
8784 }
8885
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];
10088
101- self::setCookie(1);
10289 return;
10390 }
10491
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+ {
106100 global $blogid;
107- if (MediaUtils::$blogid != $blogid) {
101+ if ( MediaUtils::$blogid == 0 )
102+ {
108103 MediaUtils::$blogid = $blogid;
109104 MediaUtils::$bshortname = MediaUtils::$blogs[MediaUtils::$blogid];
110- self::setCookie(1);
111105 }
112106 return;
113107 }
114108
115- private function setCookie($factor) {
109+ public function event_PreSendContentType($data)
110+ {
116111 global $CONF;
117- $factor = (integer) $factor;
118112
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+ }
120123 return;
121124 }
122125 }
--- a/NP_MediaUtils/mediautils/MediaUtils.php
+++ b/NP_MediaUtils/mediautils/MediaUtils.php
@@ -1,8 +1,8 @@
11 <?php
22 /**
33 * 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
66 *
77 * This program is free software; you can redistribute it and/or
88 * modify it under the terms of the GNU General Public License
@@ -10,11 +10,12 @@
1010 * of the License, or (at your option) any later version.
1111 */
1212
13-class MediaUtils {
13+class MediaUtils
14+{
1415 static public $lib_path = '';
1516
1617 static public $cookiename = 'blogid';
17- static public $blogid = FALSE;
18+ static public $blogid = 0;
1819 static public $blogs = array();
1920 static public $bshortname = '';
2021
@@ -23,167 +24,199 @@ class MediaUtils {
2324 static public $suffixes = array();
2425
2526 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');
3131
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;
4041 header("HTTP/1.0 404 Not Found");
4142 exit($message);
4243 }
4344
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+ {
5255 include(self::$lib_path . '/Medium.php');
5356 }
5457
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');
5761 return;
5862 }
5963
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');
6368 return;
6469 }
6570
66- header ('Content-type: ' . $medium->mime);
71+ header('Content-type: ' . $medium->mime);
6772 echo $resampledimage;
68- unset ($resampledimage);
73+ unset($resampledimage);
6974 exit;
7075 }
7176
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+ {
8289 include(self::$lib_path . '/Medium.php');
8390 }
8491
85- if ('Medium' !== get_class($medium)) {
92+ if ( 'Medium' !== get_class($medium) )
93+ {
8694 return FALSE;
8795 }
8896
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);
91100 return FALSE;
92101 }
93102
94- if (FALSE === ($handle = @fopen ("{$root}/{$target}", 'w'))) {
95- unset ($resampledimage);
103+ if ( FALSE === ($handle = @fopen("{$root}/{$target}", 'w')) )
104+ {
105+ unset($resampledimage);
96106 return FALSE;
97107 }
98108
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}");
102113 return FALSE;
103114 }
104115
105- unset ($resampledimage);
106- fclose ($handle);
116+ unset($resampledimage);
117+ fclose($handle);
107118
108- if (@chmod ("{$root}/{$target}", 0744) === FALSE) {
109- @unlink ("{$root}/{$target}");
119+ if ( @chmod("{$root}/{$target}", 0744) === FALSE )
120+ {
121+ @unlink("{$root}/{$target}");
110122 return FALSE;
111123 }
112124
113125 return TRUE;
114126 }
115127
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+ {
123136 $fullpath = (string) $fullpath;
124137
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+ {
127142 return TRUE;
128- } else {
143+ }
144+ else
145+ {
129146 return FALSE;
130147 }
131- } else {
132- if (!@mkdir ($fullpath, 0777)) {
148+ }
149+ else
150+ {
151+ if ( !@mkdir($fullpath, 0777) )
152+ {
133153 return FALSE;
134- } else {
154+ }
155+ else
156+ {
135157 return TRUE;
136158 }
137159 }
138160 }
139161
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+ {
152175 $root = (string) $root;
153- $hidden = (boolean) $hidden;
176+ $hidden = (boolean) $hidden;
154177 $recursive = (boolean) $recursive;
155- $suffix = (string) $suffix;
178+ $suffix = (string) $suffix;
156179 $path = (string) $path;
157- $media = (array) $media;
180+ $media = (array) $media;
158181
159- if (!class_exists('Medium', FALSE)) {
182+ if ( !class_exists('Medium', FALSE) )
183+ {
160184 include(self::$lib_path . '/Medium.php');
161185 }
162186
163187 $root = rtrim($root, '/');
164- if(!$root || !file_exists($root)) {
188+ if ( !$root || !file_exists($root) )
189+ {
165190 return FALSE;
166191 }
167192
168193 $fullpath = $root;
169- if ($path) {
194+ if ( $path )
195+ {
170196 $path = trim($path, '/');
171197 $fullpath = "{$root}/{$path}";
172198 }
173199
174- if (FALSE === ($handle = @opendir($fullpath))) {
200+ if ( FALSE === ($handle = @opendir($fullpath)) )
201+ {
175202 return FALSE;
176203 }
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+ {
181211 continue;
182212 }
183213
184- if($recursive && filetype("{$fullpath}/{$filename}") === "dir") {
214+ if ( $recursive && filetype("{$fullpath}/{$filename}") === "dir" )
215+ {
185216 $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+ {
187220 $media[] = new Medium($root, trim("{$path}/{$filename}", '/'), self::$prefix);
188221 continue;
189222 }
@@ -193,102 +226,130 @@ class MediaUtils {
193226 return $media;
194227 }
195228
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+ {
205238 $root = (string) $root;
206239 $path = (string) $path;
207240 $logs = (array) $logs;
208241
209242 $root = rtrim($root, '/');
210- if(!$root || !file_exists($root)) {
243+ if ( !$root || !file_exists($root) )
244+ {
211245 return FALSE;
212246 }
213247
214248 $fullpath = $root;
215- if ($path) {
249+ if ( $path )
250+ {
216251 $path = trim($path, '/');
217252 $fullpath = "{$root}/{$path}";
218253 }
219254
220- if (FALSE === ($handle = @opendir($fullpath))) {
255+ if ( FALSE === ($handle = @opendir($fullpath)) )
256+ {
221257 return FALSE;
222258 }
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+ {
226265 $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+ {
229271 $logs[] = "Exists: {$path}/{$filename}";
230- } else {
272+ }
273+ else
274+ {
231275 $logs[] = "Removed: {$path}/{$filename}";
232276 }
233277 continue;
234278 }
235279 }
236280 }
237- if(!rmdir("{$root}/{$path}")) {
281+ if ( !rmdir("{$root}/{$path}") )
282+ {
238283 $logs[] = "Exists: {$path}";
239- } else {
284+ }
285+ else
286+ {
240287 $logs[] = "Removed: {$path}";
241288 }
242289 return $logs;
243290 }
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+ {
257304 $root = (string) $root;
258305 $path = (string) $path;
259- $hidden = (boolean) $hidden;
260- $recursive = (boolean) $recursive;
306+ $hidden = (boolean) $hidden;
307+ $recursive = (boolean) $recursive;
261308
262309 $paths=array();
263310
264311 $root = rtrim($root, '/');
265- if(!$root || !file_exists($root)) {
312+ if ( !$root || !file_exists($root) )
313+ {
266314 return FALSE;
267315 }
268316
269317 $fullpath = $root;
270- if ($path) {
318+ if ( $path )
319+ {
271320 $path = trim($path, '/');
272321 $fullpath = "{$root}/{$path}";
273322 }
274323
275- if (FALSE === ($handle = @opendir($fullpath))) {
324+ if ( FALSE === ($handle = @opendir($fullpath)) )
325+ {
276326 return FALSE;
277327 }
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+ {
280332 continue;
281- } else if (is_file("{$fullpath}/{$filename}")) {
333+ }
334+ else if ( is_file("{$fullpath}/{$filename}") )
335+ {
282336 continue;
283- } else if ($hidden && preg_match('#^\.#', $filename)) {
337+ }
338+ else if ($hidden && preg_match('#^\.#', $filename) )
339+ {
284340 continue;
285- } else if ($private && is_numeric($filename) && $path==''&& $private != $filename) {
341+ }
342+ else if ( $private && is_numeric($filename) && $path==''&& $private != $filename )
343+ {
286344 continue;
287345 }
288346
289- if (!$path) {
347+ if ( !$path )
348+ {
290349 $relpath = $filename;
291- } else {
350+ }
351+ else
352+ {
292353 $relpath = "{$path}/{$filename}";
293354 }
294355
@@ -296,8 +357,10 @@ class MediaUtils {
296357 }
297358 closedir($handle);
298359
299- if ($path=='' && $private) {
300- if (!array_key_exists($private, $paths)) {
360+ if ( $path=='' && $private )
361+ {
362+ if ( !array_key_exists($private, $paths) )
363+ {
301364 $paths[$private] = array('root'=>$root , 'path'=>$private, 'files'=>0, 'dirs'=>0);
302365 }
303366 $paths[$private]['label'] = 'PRIVATE';
@@ -307,61 +370,76 @@ class MediaUtils {
307370 return $paths;
308371 }
309372
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+ {
321385 $root = (string) $root;
322386 $path = (string) $path;
323387 $private = (boolean) $private;
324- $hidden = (boolean) $hidden;
325- $recursive = (boolean) $recursive;
388+ $hidden = (boolean) $hidden;
389+ $recursive = (boolean) $recursive;
326390
327391 $cnt_files = 0;
328392 $cnt_dirs = 0;
329393
330394 $root = rtrim($root, '/');
331- if(!$root || !file_exists($root)) {
395+ if ( !$root || !file_exists($root) )
396+ {
332397 return FALSE;
333398 }
334399
335400 $fullpath = $root;
336- if ($path) {
401+ if ( $path )
402+ {
337403 $path = trim($path, '/');
338404 $fullpath = "{$root}/{$path}";
339405 }
340406
341- if (FALSE === ($handle = @opendir($fullpath))) {
407+ if ( FALSE === ($handle = @opendir($fullpath)) )
408+ {
342409 return FALSE;
343410 }
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+ {
346415 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+ {
349421 $cnt_files++;
350422 }
351423 continue;
352- } else if ($hidden && preg_match('#^\.#', $filename)) {
424+ }
425+ else if ( $hidden && preg_match('#^\.#', $filename) )
426+ {
353427 continue;
354428 }
355429
356430 $cnt_dirs++;
357431
358- if (!$path) {
432+ if ( !$path )
433+ {
359434 $relpath = $filename;
360- } else {
435+ }
436+ else
437+ {
361438 $relpath = "{$path}/{$filename}";
362439 }
363440
364- if ($recursive) {
441+ if ( $recursive )
442+ {
365443 $paths = self::getPathData($root, $relpath, $private, $recursive, $hidden, $paths);
366444 }
367445 }
@@ -372,44 +450,52 @@ class MediaUtils {
372450 $paths[$path]['name'] = basename($fullpath);
373451 $paths[$path]['files'] = $cnt_files;
374452 $paths[$path]['dirs'] = $cnt_dirs;
375- if ($private) {
453+ if ( $private )
454+ {
376455 $paths[$path]['label'] = preg_replace("#^$private#", 'PRIVATE', $path);
377- } else {
456+ }
457+ else
458+ {
378459 $paths[$path]['label'] = $path;
379460 }
380461
381462 return $paths;
382463 }
383464
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;
401484
402485 /**
403486 * $temp should be derived from $_FILE
404487 */
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+ {
407492 return 'NP_MediaUtils: Miss uploaded file.';
408493 }
409494 }
410495
411496 $root = rtrim($root, '/');
412- if(!$root || !file_exists($root) || !$path) {
497+ if ( !$root || !file_exists($root) || !$path )
498+ {
413499 return 'NP_MediaUtils: Destination is invalid.';
414500 }
415501 $path = trim($path, '/');
@@ -417,7 +503,8 @@ class MediaUtils {
417503 /**
418504 * see http://www.php.net/manual/en/features.file-upload.errors.php
419505 */
420- switch ($temp['error']) {
506+ switch ( $temp['error'] )
507+ {
421508 case UPLOAD_ERR_OK:
422509 break;
423510 case UPLOAD_ERR_INI_SIZE:
@@ -432,48 +519,68 @@ class MediaUtils {
432519 return 'NP_MediaUtils: Request rejected';
433520 }
434521
435- if (preg_match ("#(\\\\|/|\\n)#", $temp['name'])) {
522+ if ( preg_match ("#(\\\\|/|\\n)#", $temp['name']) )
523+ {
436524 return 'NP_MediaUtils: invalid filename';
437525 }
438526
439- if ($temp['size'] > self::$maxsize) {
527+ if ( $temp['size'] > self::$maxsize )
528+ {
440529 return 'NP_MediaUtils: Binary is too big';
441530 }
442531
443- if (!empty(self::$suffixes) && is_array(self::$suffixes)) {
532+ if ( !empty(self::$suffixes) && is_array(self::$suffixes) )
533+ {
444534 preg_match("#\.(.+)$#", $temp['name'], $match);
445535 $suffix = strtolower($match[1]);
446- if (!in_array($suffix, self::$suffixes)) {
536+ if ( !in_array($suffix, self::$suffixes) )
537+ {
447538 return 'NP_MediaUtils: Forbidden file suffix';
448539 }
449540 }
450541
451- if (!self::checkDir("{$root}/{$path}")) {
542+ if ( !self::checkDir("{$root}/{$path}") )
543+ {
452544 return 'NP_MediaUtils: Invalid target directory';
453545 }
454546
455- if ($overwrite) {
456- if (!preg_match("#\.($suffix)$#", strtolower($overwrite), $match)) {
547+ if ( $overwrite )
548+ {
549+ if ( !preg_match("#\.($suffix)$#", strtolower($overwrite), $match) )
550+ {
457551 return 'NP_MediaUtils: suffix is not the same.';
458552 }
459553 $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'];
462558 }
463559
464- if (!$overwrite && file_exists("{$root}/{$path}/{$temp['name']}")) {
560+ if ( !$overwrite && file_exists("{$root}/{$path}/{$temp['name']}") )
561+ {
465562 return 'NP_MediaUtils: The same filename already exists in this target directory.';
466563 }
467564
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);
470573 }
471574
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+ {
474579 return 'NP_MediaUtils: Fail to move uploaded binary to file sytem.';
475580 }
476- } else if (!copy($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}")) {
581+ }
582+ else if ( !copy($temp['tmp_name'], "{$root}/{$path}/{$temp['name']}") )
583+ {
477584 return 'NP_MediaUtils: Fail to copy uploaded binary to file sytem.';
478585 }
479586
@@ -481,7 +588,8 @@ class MediaUtils {
481588 @chmod("{$root}/{$path}/{$temp['name']}", 0644);
482589 umask($oldumask);
483590
484- if ($manager) {
591+ if ( $manager )
592+ {
485593 $manager->notify('PostMediaUpload',array('collection' => $path, 'mediadir' => $root, 'filename' => $temp['name']));
486594 }
487595
--- a/NP_MediaUtils/mediautils/Medium.php
+++ b/NP_MediaUtils/mediautils/Medium.php
@@ -1,8 +1,8 @@
11 <?php
22 /**
33 * 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
66 *
77 * This program is free software; you can redistribute it and/or
88 * modify it under the terms of the GNU General Public License
@@ -10,7 +10,8 @@
1010 * of the License, or (at your option) any later version.
1111 */
1212
13-class Medium {
13+class Medium
14+{
1415 public $root = '';
1516 public $path = '';
1617 public $name = '';
@@ -25,28 +26,31 @@ class Medium {
2526 public $resampledwidth = 0;
2627 public $resampledheight = 0;
2728
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+ {
3739 static $fullpath;
3840 static $info;
3941
40- if ($root == '' || $relativepath == '') {
42+ if ( $root == '' || $relativepath == '' )
43+ {
4144 return FALSE;
4245 }
4346
4447 $root = preg_replace('#/*$#', '', $root);
45- if ($root == '' || $relativepath == ''
48+ if ( $root == '' || $relativepath == ''
4649 || !file_exists($root)
4750 || FALSE === ($fullpath = realpath(rtrim($root . '/' . ltrim($relativepath, '/'), '/')))
4851 || strpos($fullpath, $root) !== 0
49- || !file_exists($fullpath)) {
52+ || !file_exists($fullpath) )
53+ {
5054 return FALSE;
5155 }
5256
@@ -54,34 +58,41 @@ class Medium {
5458 $this->name = basename($fullpath);
5559 $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath);
5660
57- if ($this->path === $this->name) {
61+ if ( $this->path === $this->name )
62+ {
5863 $this->path = '';
5964 }
6065
61- if (FALSE === ($info = @getimagesize ($fullpath))) {
66+ if ( FALSE === ($info = @getimagesize($fullpath)) )
67+ {
6268 $this->mime = 'application/octet-stream';
6369 $this->width = 0;
6470 $this->height = 0;
65- } else {
71+ }
72+ else
73+ {
6674 $this->mime = $info['mime'];
6775 $this->width = $info[0];
6876 $this->height = $info[1];
6977 }
7078
7179 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+ {
7584 $this->mime = finfo_file($info, $fullpath);
7685 }
7786
7887 $this->update = date("Y/m/d", @filemtime($fullpath));
7988 $this->size = ceil(filesize($fullpath) / 1000);
8089
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+ {
8292 $this->filename = $info[1];
8393 $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+ {
8596 $this->prefix = preg_replace('#^([0-9]{4})([0-9]{2})([0-9]{2})$#', '$1/$2/$3', $info[1]);
8697 $this->filename = $info[2];
8798 }
@@ -90,30 +101,41 @@ class Medium {
90101 return $this;
91102 }
92103
93- public function __destruct () {
104+ public function __destruct()
105+ {
94106 return;
95107 }
96108
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+ {
106120 return FALSE;
107- } else if ($this->width == 0 || $this->height == 0) {
121+ }
122+ else if ( $this->width == 0 || $this->height == 0 )
123+ {
108124 return FALSE;
109- } else if ($this->width < $maxwidth && $this->height < $maxheight) {
125+ }
126+ else if ( $this->width < $maxwidth && $this->height < $maxheight )
127+ {
110128 $this->resampledwidth = $this->width;
111129 $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);
114134 $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);
117139 $this->resampledheight = $maxheight;
118140 }
119141 return TRUE;
@@ -126,104 +148,130 @@ class Medium {
126148 * @param Integer $maxheight
127149 * @return Boolean
128150 */
129- public function getResampledBinary ($image_mime) {
151+ public function getResampledBinary($image_mime)
152+ {
130153 static $gdinfo;
131154 static $original;
132155 static $resampledimage;
133156
134157 $gdinfo = gd_info();
135158
136- if ($this->path !== '') {
159+ if ( $this->path !== '' )
160+ {
137161 $fullpath = "{$this->root}/{$this->path}/{$this->name}";
138- } else {
162+ }
163+ else
164+ {
139165 $fullpath = "{$this->root}/{$this->name}";
140166 }
141- if (!file_exists($fullpath)) {
167+ if ( !file_exists($fullpath) )
168+ {
142169 return FALSE;
143170 }
144171
145- if (!array_key_exists($this->mime, $image_mime)
172+ if ( !array_key_exists($this->mime, $image_mime)
146173 || $this->width == 0
147174 || $this->height == 0
148175 || $this->resampledwidth == 0
149- || $this->resampledheight == 0) {
150- return FALSE;
176+ || $this->resampledheight == 0 )
177+ {
178+ return FALSE;
151179 }
152180
153181 // check current available memory
154182 $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;
162191 }
163192
164193 // this code is based on analyze if gd.c in php source code
165194 // 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+ {
167197 return FALSE;
168198 }
169199
170- switch ($this->mime) {
200+ switch ( $this->mime )
201+ {
171202 case 'image/gif':
172- if (!$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support']) {
203+ if ( !$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support'] )
204+ {
173205 return FALSE;
174206 }
175- $original = imagecreatefromgif ($fullpath);
207+ $function = 'imagecreatefromgif';
176208 break;
177209 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+ {
179213 return FALSE;
180214 }
181- $original = imagecreatefromjpeg ($fullpath);
215+ $function = 'imagecreatefromjpeg';
182216 break;
183217 case 'image/png':
184- if (!$gdinfo['PNG Support']) {
218+ if ( !$gdinfo['PNG Support'] )
219+ {
185220 return FALSE;
186221 }
187- $original = imagecreatefrompng ($fullpath);
222+ $function = 'imagecreatefrompng';
188223 break;
189224 default:
190225 return FALSE;
191226 }
192227
193- $resampledimage = imagecreatetruecolor ($this->resampledwidth, $this->resampledheight);
228+ if ( !is_callable($function) )
229+ {
230+ return FALSE;
231+ }
194232
195- if (!$resampledimage) {
233+ if ( FALSE === ($original = call_user_func_array($function,array(&$fullpath))) )
234+ {
196235 return FALSE;
197236 }
198237
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+ {
201240 return FALSE;
202241 }
203242
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);
205250
206251 ob_start();
207252
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;
219266 }
220267
221- imagedestroy ($resampledimage);
268+ imagedestroy($resampledimage);
222269
223270 return ob_get_clean();
224271 }
225272
226- public function getHashedName($algorism) {
273+ public function getHashedName($algorism)
274+ {
227275 return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE);
228276 }
229277 }
Show on old repository browser