OSDN Git Service

MERGE: リビジョン1864。Mediaクラス、MediaObjectクラスのコード整理。
authorsakamocchi <o-takashi@sakamocchi.jp>
Sat, 19 May 2012 11:46:56 +0000 (20:46 +0900)
committersakamocchi <o-takashi@sakamocchi.jp>
Sat, 19 May 2012 11:47:11 +0000 (20:47 +0900)
Revision 1864:
code cleanup for Media class and MediaObject class
http://nucleuscms.svn.sourceforge.net/viewvc/nucleuscms?view=revision&revision=1864

nucleus/libs/MEDIA.php

index 4fffced..0195d2d 100644 (file)
  *\r
  * @license http://nucleuscms.org/license.txt GNU General Public License\r
  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
- * @version $Id: MEDIA.php 1525 2011-06-21 10:20:19Z sakamocchi $\r
+ * @version $Id: MEDIA.php 1864 2012-05-19 11:39:44Z sakamocchi $
  */\r
 \r
-define('PRIVATE_COLLECTION', 'Private Collection');\r
-define('READ_ONLY_MEDIA_FOLDER', '(Read Only)');\r
+define('PRIVATE_COLLECTION',           'Private Collection');\r
+define('READ_ONLY_MEDIA_FOLDER',       '(Read Only)');\r
 \r
-/**\r
-  * Represents the media objects for a certain member\r
-  */\r
 class Media\r
 {\r
        /**\r
+        * Media::getCollectionList()\r
         * Gets the list of collections available to the currently logged\r
         * in member\r
         *\r
         * @returns array of dirname => display name\r
         */\r
-       public function getCollectionList($exceptReadOnly = false)\r
+       static public function getCollectionList($exceptReadOnly = FALSE)\r
        {\r
                global $member, $DIR_MEDIA;\r
                \r
@@ -39,237 +37,321 @@ class Media
                \r
                // add private directory for member\r
                $collections[$member->getID()] = PRIVATE_COLLECTION;\r
-\r
+               \r
                // add global collections\r
-               if (!is_dir($DIR_MEDIA)) return $collections;\r
-\r
+               if ( !is_dir($DIR_MEDIA) )\r
+               {\r
+                       return $collections;\r
+               }\r
+               \r
                $dirhandle = opendir($DIR_MEDIA);\r
-               while ($dirname = readdir($dirhandle)) {\r
+               while ( $dirname = readdir($dirhandle) )\r
+               {\r
                        // only add non-numeric (numeric=private) dirs\r
-                       if (@is_dir($DIR_MEDIA . $dirname) &&\r
+                       if ( @is_dir($DIR_MEDIA . $dirname) &&\r
                                ($dirname != '.') &&\r
                                ($dirname != '..') &&\r
                                ($dirname != 'CVS') &&\r
-                               (!is_numeric($dirname)))  {\r
-                               if (@is_writable($DIR_MEDIA . $dirname))\r
+                               (!is_numeric($dirname)) )\r
+                               {\r
+                               if ( @is_writable($DIR_MEDIA . $dirname) )\r
+                               {\r
                                        $collections[$dirname] = $dirname;\r
-                               else if ($exceptReadOnly == false)\r
+                               }\r
+                               else if ( $exceptReadOnly == FALSE )\r
+                               {\r
                                        $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER;\r
+                               }\r
                        }\r
                }\r
                closedir($dirhandle);\r
-\r
+               \r
                return $collections;\r
-\r
        }\r
-\r
+       \r
        /**\r
-         * Returns an array of MediaObject objects for a certain collection\r
-         *\r
-         * @param $collection\r
-         *             name of the collection\r
-         * @param $filter\r
-         *             filter on filename (defaults to none)\r
-         */\r
-       function getMediaListByCollection($collection, $filter = '') {\r
-               global $DIR_MEDIA;\r
-\r
+        * Media::getMediaListByCollection()\r
+        * Returns an array of MediaObject objects for a certain collection\r
+        *\r
+        * @param       string  $collection     name of the collection\r
+        * @param       string  $filter         filter on filename (defaults to none)\r
+        * @return      void\r
+        */\r
+       static public function getMediaListByCollection($collection, $filter = '')\r
+       {\r
+               global $CONF, $DIR_MEDIA;\r
+               \r
                $filelist = array();\r
-\r
+               \r
                // 1. go through all objects and add them to the filelist\r
-\r
                $mediadir = $DIR_MEDIA . $collection . '/';\r
-\r
+               \r
                // return if dir does not exist\r
-               if (!is_dir($mediadir)) return $filelist;\r
-\r
+               if ( !is_dir($mediadir) )\r
+               {\r
+                       return $filelist;\r
+               }\r
+               \r
                $dirhandle = opendir($mediadir);\r
-               while ($filename = readdir($dirhandle)) {\r
+               while ( $filename = readdir($dirhandle) )\r
+               {\r
                        // only add files that match the filter\r
-                       if (!@is_dir($filename) && Media::checkFilter($filename, $filter))\r
+                       if ( !@is_dir($filename) && self::checkFilter($filename, $filter) )\r
+                       {\r
                                array_push($filelist, new MediaObject($collection, $filename, filemtime($mediadir . $filename)));\r
+                       }\r
                }\r
                closedir($dirhandle);\r
-\r
+               \r
                // sort array so newer files are shown first\r
-               usort($filelist, 'sort_media');\r
-\r
+               usort($filelist,  array(__CLASS__, 'sort_media'));\r
+               \r
                return $filelist;\r
        }\r
-\r
-       function checkFilter($strText, $strFilter) {\r
-               if ($strFilter == '')\r
+       \r
+       /**\r
+        * Media::checkFilter()\r
+        * \r
+        * @param       string  $strText\r
+        * @param       string  $strFilter\r
+        * @return      boolean\r
+        */\r
+       static public function checkFilter($strText, $strFilter)\r
+       {\r
+               if ( $strFilter == '' )\r
+               {\r
                        return 1;\r
+               }\r
                else\r
+               {\r
                        return is_integer(i18n::strpos(strtolower($strText), strtolower($strFilter)));\r
+               }\r
        }\r
-\r
+       \r
        /**\r
-         * checks if a collection exists with the given name, and if it's\r
-         * allowed for the currently logged in member to upload files to it\r
-         */\r
-       function isValidCollection($collectionName, $exceptReadOnly = false) {\r
+        * Media::isValidCollection()\r
+        * checks if a collection exists with the given name, and if it's\r
+        * allowed for the currently logged in member to upload files to it\r
+        * \r
+        * @param       string  $collectionName\r
+        * @param       string  $exceptReadOnly\r
+        * @return      boolean\r
+        */\r
+       static public function isValidCollection($collectionName, $exceptReadOnly = FALSE)\r
+       {\r
                global $member, $DIR_MEDIA;\r
-\r
+               \r
                // allow creating new private directory\r
-               if ($collectionName === (string)$member->getID())\r
-                       return true;\r
-                       \r
-               $collections = Media::getCollectionList($exceptReadOnly);\r
+               if ( $collectionName === (string)$member->getID() )\r
+               {\r
+                       return TRUE;\r
+               }\r
+               \r
+               $collections = self::getCollectionList($exceptReadOnly);\r
                $dirname = $collections[$collectionName];\r
-               if ($dirname == NULL || $dirname === PRIVATE_COLLECTION)\r
-                       return false;  \r
-\r
+               \r
+               if ( $dirname == NULL || $dirname === PRIVATE_COLLECTION )\r
+               {\r
+                       return FALSE;\r
+               }\r
+               \r
                // other collections should exist and be writable\r
                $collectionDir = $DIR_MEDIA . $collectionName;\r
-               if ($exceptReadOnly)\r
-                       return (@is_dir($collectionDir) && @is_writable($collectionDir));\r
-\r
+               if ( $exceptReadOnly )\r
+               {\r
+                       return ( @is_dir($collectionDir) && @is_writable($collectionDir) );\r
+               }\r
+               \r
                // other collections should exist\r
                return @is_dir($collectionDir);\r
-       }\r
-\r
+       }\r
+       \r
        /**\r
-         * Adds an uploaded file to the media archive\r
-         *\r
-         * @param collection\r
-         *             collection\r
-         * @param uploadfile\r
-         *             the postFileInfo(..) array\r
-         * @param filename\r
-         *             the filename that should be used to save the file as\r
-         *             (date prefix should be already added here)\r
-         */\r
-       function addMediaObject($collection, $uploadfile, $filename) {\r
+        * Media::addMediaObject()\r
+        * Adds an uploaded file to the media archive\r
+        *\r
+        * @param       string  $collection     collection\r
+        * @param       array   $uploadfile     the postFileInfo(..) array\r
+        * @param       string  $filename       the filename that should be used to save the file as\r
+        *                                                              (date prefix should be already added here)\r
+        * @return      string  blank if success, message if failed\r
+        */\r
+       static public function addMediaObject($collection, $uploadfile, $filename)\r
+       {\r
                global $DIR_MEDIA, $manager;\r
-\r
+               \r
                // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php\r
                $filename = cleanFileName($filename);\r
+               \r
                // should already have tested for allowable types before calling this method. This will only catch files with no extension at all\r
-               if ($filename === false) \r
+               if ( $filename === FALSE )\r
+               {\r
                        return _ERROR_BADFILETYPE;\r
+               }\r
                \r
                // trigger PreMediaUpload event\r
                $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));\r
-\r
+               \r
                // don't allow uploads to unknown or forbidden collections\r
-               $exceptReadOnly = true;\r
-               if (!Media::isValidCollection($collection,$exceptReadOnly))\r
+               $exceptReadOnly = TRUE;\r
+               if ( !self::isValidCollection($collection,$exceptReadOnly) )\r
+               {\r
                        return _ERROR_DISALLOWED;\r
-\r
+               }\r
+               \r
                // check dir permissions (try to create dir if it does not exist)\r
                $mediadir = $DIR_MEDIA . $collection;\r
-\r
+               \r
                // try to create new private media directories if needed\r
-               if (!@is_dir($mediadir) && is_numeric($collection)) {\r
+               if ( !@is_dir($mediadir) && is_numeric($collection) )\r
+               {\r
                        $oldumask = umask(0000);\r
-                       if (!@mkdir($mediadir, 0777))\r
+                       if ( !@mkdir($mediadir, 0777) )\r
+                       {\r
                                return _ERROR_BADPERMISSIONS;\r
+                       }\r
                        umask($oldumask);\r
                }\r
-\r
+               \r
                // if dir still not exists, the action is disallowed\r
-               if (!@is_dir($mediadir))\r
+               if ( !@is_dir($mediadir) )\r
+               {\r
                        return _ERROR_DISALLOWED;\r
-\r
-               if (!is_writeable($mediadir))\r
+               }\r
+               \r
+               if ( !is_writeable($mediadir) )\r
+               {\r
                        return _ERROR_BADPERMISSIONS;\r
-\r
+               }\r
+               \r
                // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)\r
                $mediadir .= '/';\r
-\r
-               if (file_exists($mediadir . $filename))\r
+               \r
+               if ( file_exists($mediadir . $filename) )\r
+               {\r
                        return _ERROR_UPLOADDUPLICATE;\r
-\r
+               }\r
+               \r
                // move file to directory\r
-               if (is_uploaded_file($uploadfile)) {\r
-                       if (!@move_uploaded_file($uploadfile, $mediadir . $filename))\r
+               if ( is_uploaded_file($uploadfile) )\r
+               {\r
+                       if ( !@move_uploaded_file($uploadfile, $mediadir . $filename) )\r
+                       {\r
                                return _ERROR_UPLOADMOVEP;\r
-               } else {\r
-                       if (!copy($uploadfile, $mediadir . $filename))\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       if ( !copy($uploadfile, $mediadir . $filename) )\r
+                       {\r
                                return _ERROR_UPLOADCOPY ;\r
+                       }\r
                }\r
-\r
+               \r
                // chmod uploaded file\r
                $oldumask = umask(0000);\r
                @chmod($mediadir . $filename, 0644);\r
                umask($oldumask);\r
-\r
+               \r
                $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));\r
-\r
+               \r
                return '';\r
-\r
        }\r
-\r
+       \r
        /**\r
+        * Media::addMediaObjectRaw()\r
         * Adds an uploaded file to the media dir.\r
-        *\r
-        * @param $collection\r
-        *              collection to use\r
-        * @param $filename\r
-        *              the filename that should be used to save the file as\r
-        *              (date prefix should be already added here)\r
-        * @param &$data\r
-        *              File data (binary)\r
-        *\r
+        * \r
         * NOTE: does not check if $collection is valid.\r
+        * \r
+        * @param       string  $collection     collection to use\r
+        * @param       string  $filename       the filename that should be used to save the file\r
+        *                                                              as (date prefix should be already added here)\r
+        * @param       &$data  File data (binary)\r
+        * @return      string  blank if success, message if failed\r
         */\r
-       function addMediaObjectRaw($collection, $filename, &$data) {\r
+       static public function addMediaObjectRaw($collection, $filename, &$data)\r
+       {\r
                global $DIR_MEDIA;\r
-\r
+               \r
                // check dir permissions (try to create dir if it does not exist)\r
                $mediadir = $DIR_MEDIA . $collection;\r
-\r
+               \r
                // try to create new private media directories if needed\r
-               if (!@is_dir($mediadir) && is_numeric($collection)) {\r
+               if ( !@is_dir($mediadir) && is_numeric($collection) )\r
+               {\r
                        $oldumask = umask(0000);\r
-                       if (!@mkdir($mediadir, 0777))\r
+                       if ( !@mkdir($mediadir, 0777) )\r
+                       {\r
                                return _ERROR_BADPERMISSIONS;\r
+                       }\r
                        umask($oldumask);\r
                }\r
-\r
+               \r
                // if dir still not exists, the action is disallowed\r
-               if (!@is_dir($mediadir))\r
+               if ( !@is_dir($mediadir) )\r
+               {\r
                        return _ERROR_DISALLOWED;\r
-\r
-               if (!is_writeable($mediadir))\r
+               }\r
+               \r
+               if ( !is_writeable($mediadir) )\r
+               {\r
                        return _ERROR_BADPERMISSIONS;\r
-\r
+               }\r
+               \r
                // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)\r
                $mediadir .= '/';\r
-\r
-               if (file_exists($mediadir . $filename))\r
+               \r
+               if ( file_exists($mediadir . $filename) )\r
+               {\r
                        return _ERROR_UPLOADDUPLICATE;\r
-\r
+               }\r
+               \r
                // create file\r
                $fh = @fopen($mediadir . $filename, 'wb');\r
-               if (!$fh)\r
+               if ( !$fh )\r
+               {\r
                        return _ERROR_UPLOADFAILED;\r
+               }\r
                $ok = @fwrite($fh, $data);\r
                @fclose($fh);\r
-               if (!$ok)\r
+               if ( !$ok )\r
+               {\r
                        return _ERROR_UPLOADFAILED;\r
-\r
+               }\r
+               \r
                // chmod uploaded file\r
                $oldumask = umask(0000);\r
                @chmod($mediadir . $filename, 0644);\r
                umask($oldumask);\r
-\r
+               \r
                return '';\r
-\r
        }\r
-\r
+       \r
+       /**\r
+        * Media::sort_media()\r
+        * User-defined sort method to sort an array of MediaObjects\r
+        * \r
+        * @param       object  $a\r
+        * @param       object  $b\r
+        * @return      boolean\r
+        */\r
+       static private function sort_media($a, $b)\r
+       {\r
+               if ($a->timestamp == $b->timestamp) return 0;\r
+               return ($a->timestamp > $b->timestamp) ? -1 : 1;\r
+       }\r
 }\r
 \r
 /**\r
 * Represents the characteristics of one single media-object\r
 *\r
 * Description of properties:\r
 *  - filename: filename, without paths\r
 *  - timestamp: last modification (unix timestamp)\r
 *  - collection: collection to which the file belongs (can also be a owner ID, for private collections)\r
 *  - private: true if the media belongs to a private member collection\r
 */\r
+ * Represents the characteristics of one single media-object\r
+ *\r
+ * Description of properties:\r
+ *  - filename: filename, without paths\r
+ *  - timestamp: last modification (unix timestamp)\r
+ *  - collection: collection to which the file belongs (can also be a owner ID, for private collections)\r
+ *  - private: true if the media belongs to a private member collection\r
+ */\r
 class MediaObject\r
 {\r
        public $private;\r
@@ -286,11 +368,3 @@ class MediaObject
                return;\r
        }\r
 }\r
-\r
-/**\r
-  * User-defined sort method to sort an array of MediaObjects\r
-  */\r
-function sort_media($a, $b) {\r
-       if ($a->timestamp == $b->timestamp) return 0;\r
-       return ($a->timestamp > $b->timestamp) ? -1 : 1;\r
-}\r