OSDN Git Service

FIX:コメント取得のクエリに必要なスペースがないために発生するクエリエラーを修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / MEDIA.php
index e8f7e98..4fffced 100644 (file)
-<?php
-/*
- * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2009 The Nucleus Group
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- * (see nucleus/documentation/index.html#license for more info)
- */
-/**
- * Media classes for nucleus
- *
- * @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2009 The Nucleus Group
- * @version $Id: MEDIA.php 1525 2011-06-21 10:20:19Z sakamocchi $
- */
-
-define('PRIVATE_COLLECTION', 'Private Collection');
-define('READ_ONLY_MEDIA_FOLDER', '(Read Only)');
-
-/**
-  * Represents the media objects for a certain member
-  */
-class MEDIA {
-
-       /**
-         * Gets the list of collections available to the currently logged
-         * in member
-         *
-         * @returns array of dirname => display name
-         */
-       function getCollectionList($exceptReadOnly = false) {
-               global $member, $DIR_MEDIA;
-
-               $collections = array();
-
-               // add private directory for member
-               $collections[$member->getID()] = PRIVATE_COLLECTION;
-
-               // add global collections
-               if (!is_dir($DIR_MEDIA)) return $collections;
-
-               $dirhandle = opendir($DIR_MEDIA);
-               while ($dirname = readdir($dirhandle)) {
-                       // only add non-numeric (numeric=private) dirs
-                       if (@is_dir($DIR_MEDIA . $dirname) &&
-                               ($dirname != '.') &&
-                               ($dirname != '..') &&
-                               ($dirname != 'CVS') &&
-                               (!is_numeric($dirname)))  {
-                               if (@is_writable($DIR_MEDIA . $dirname))
-                                       $collections[$dirname] = $dirname;
-                               else if ($exceptReadOnly == false)
-                                       $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER;
-                       }
-               }
-               closedir($dirhandle);
-
-               return $collections;
-
-       }
-
-       /**
-         * Returns an array of MEDIAOBJECT objects for a certain collection
-         *
-         * @param $collection
-         *             name of the collection
-         * @param $filter
-         *             filter on filename (defaults to none)
-         */
-       function getMediaListByCollection($collection, $filter = '') {
-               global $DIR_MEDIA;
-
-               $filelist = array();
-
-               // 1. go through all objects and add them to the filelist
-
-               $mediadir = $DIR_MEDIA . $collection . '/';
-
-               // return if dir does not exist
-               if (!is_dir($mediadir)) return $filelist;
-
-               $dirhandle = opendir($mediadir);
-               while ($filename = readdir($dirhandle)) {
-                       // only add files that match the filter
-                       if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter))
-                               array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename)));
-               }
-               closedir($dirhandle);
-
-               // sort array so newer files are shown first
-               usort($filelist, 'sort_media');
-
-               return $filelist;
-       }
-
-       function checkFilter($strText, $strFilter) {
-               if ($strFilter == '')
-                       return 1;
-               else
-                       return is_integer(i18n::strpos(strtolower($strText), strtolower($strFilter)));
-       }
-
-       /**
-         * checks if a collection exists with the given name, and if it's
-         * allowed for the currently logged in member to upload files to it
-         */
-       function isValidCollection($collectionName, $exceptReadOnly = false) {
-               global $member, $DIR_MEDIA;
-
-               // allow creating new private directory
-               if ($collectionName === (string)$member->getID())
-                       return true;
-                       
-               $collections = MEDIA::getCollectionList($exceptReadOnly);
-               $dirname = $collections[$collectionName];
-               if ($dirname == NULL || $dirname === PRIVATE_COLLECTION)
-                       return false;  
-
-               // other collections should exist and be writable
-               $collectionDir = $DIR_MEDIA . $collectionName;
-               if ($exceptReadOnly)
-                       return (@is_dir($collectionDir) && @is_writable($collectionDir));
-
-               // other collections should exist
-               return @is_dir($collectionDir);
-       }
-
-       /**
-         * Adds an uploaded file to the media archive
-         *
-         * @param collection
-         *             collection
-         * @param uploadfile
-         *             the postFileInfo(..) array
-         * @param filename
-         *             the filename that should be used to save the file as
-         *             (date prefix should be already added here)
-         */
-       function addMediaObject($collection, $uploadfile, $filename) {
-               global $DIR_MEDIA, $manager;
-
-               // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php
-               $filename = cleanFileName($filename);
-               // should already have tested for allowable types before calling this method. This will only catch files with no extension at all
-               if ($filename === false) 
-                       return _ERROR_BADFILETYPE;
-               
-               // trigger PreMediaUpload event
-               $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));
-
-               // don't allow uploads to unknown or forbidden collections
-               $exceptReadOnly = true;
-               if (!MEDIA::isValidCollection($collection,$exceptReadOnly))
-                       return _ERROR_DISALLOWED;
-
-               // check dir permissions (try to create dir if it does not exist)
-               $mediadir = $DIR_MEDIA . $collection;
-
-               // try to create new private media directories if needed
-               if (!@is_dir($mediadir) && is_numeric($collection)) {
-                       $oldumask = umask(0000);
-                       if (!@mkdir($mediadir, 0777))
-                               return _ERROR_BADPERMISSIONS;
-                       umask($oldumask);
-               }
-
-               // if dir still not exists, the action is disallowed
-               if (!@is_dir($mediadir))
-                       return _ERROR_DISALLOWED;
-
-               if (!is_writeable($mediadir))
-                       return _ERROR_BADPERMISSIONS;
-
-               // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
-               $mediadir .= '/';
-
-               if (file_exists($mediadir . $filename))
-                       return _ERROR_UPLOADDUPLICATE;
-
-               // move file to directory
-               if (is_uploaded_file($uploadfile)) {
-                       if (!@move_uploaded_file($uploadfile, $mediadir . $filename))
-                               return _ERROR_UPLOADMOVEP;
-               } else {
-                       if (!copy($uploadfile, $mediadir . $filename))
-                               return _ERROR_UPLOADCOPY ;
-               }
-
-               // chmod uploaded file
-               $oldumask = umask(0000);
-               @chmod($mediadir . $filename, 0644);
-               umask($oldumask);
-
-               $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));
-
-               return '';
-
-       }
-
-       /**
-        * Adds an uploaded file to the media dir.
-        *
-        * @param $collection
-        *              collection to use
-        * @param $filename
-        *              the filename that should be used to save the file as
-        *              (date prefix should be already added here)
-        * @param &$data
-        *              File data (binary)
-        *
-        * NOTE: does not check if $collection is valid.
-        */
-       function addMediaObjectRaw($collection, $filename, &$data) {
-               global $DIR_MEDIA;
-
-               // check dir permissions (try to create dir if it does not exist)
-               $mediadir = $DIR_MEDIA . $collection;
-
-               // try to create new private media directories if needed
-               if (!@is_dir($mediadir) && is_numeric($collection)) {
-                       $oldumask = umask(0000);
-                       if (!@mkdir($mediadir, 0777))
-                               return _ERROR_BADPERMISSIONS;
-                       umask($oldumask);
-               }
-
-               // if dir still not exists, the action is disallowed
-               if (!@is_dir($mediadir))
-                       return _ERROR_DISALLOWED;
-
-               if (!is_writeable($mediadir))
-                       return _ERROR_BADPERMISSIONS;
-
-               // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
-               $mediadir .= '/';
-
-               if (file_exists($mediadir . $filename))
-                       return _ERROR_UPLOADDUPLICATE;
-
-               // create file
-               $fh = @fopen($mediadir . $filename, 'wb');
-               if (!$fh)
-                       return _ERROR_UPLOADFAILED;
-               $ok = @fwrite($fh, $data);
-               @fclose($fh);
-               if (!$ok)
-                       return _ERROR_UPLOADFAILED;
-
-               // chmod uploaded file
-               $oldumask = umask(0000);
-               @chmod($mediadir . $filename, 0644);
-               umask($oldumask);
-
-               return '';
-
-       }
-
-}
-
-/**
-  * Represents the characteristics of one single media-object
-  *
-  * Description of properties:
-  *  - filename: filename, without paths
-  *  - timestamp: last modification (unix timestamp)
-  *  - collection: collection to which the file belongs (can also be a owner ID, for private collections)
-  *  - private: true if the media belongs to a private member collection
-  */
-class MEDIAOBJECT {
-
-       var $private;
-       var $collection;
-       var $filename;
-       var $timestamp;
-
-       function MEDIAOBJECT($collection, $filename, $timestamp) {
-               $this->private = is_numeric($collection);
-               $this->collection = $collection;
-               $this->filename = $filename;
-               $this->timestamp = $timestamp;
-       }
-
-}
-
-/**
-  * User-defined sort method to sort an array of MEDIAOBJECTS
-  */
-function sort_media($a, $b) {
-       if ($a->timestamp == $b->timestamp) return 0;
-       return ($a->timestamp > $b->timestamp) ? -1 : 1;
-}
-
-?>
+<?php\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * Media classes for nucleus\r
+ *\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
+ */\r
+\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
+        * 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
+       {\r
+               global $member, $DIR_MEDIA;\r
+               \r
+               $collections = array();\r
+               \r
+               // add private directory for member\r
+               $collections[$member->getID()] = PRIVATE_COLLECTION;\r
+\r
+               // add global collections\r
+               if (!is_dir($DIR_MEDIA)) return $collections;\r
+\r
+               $dirhandle = opendir($DIR_MEDIA);\r
+               while ($dirname = readdir($dirhandle)) {\r
+                       // only add non-numeric (numeric=private) dirs\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
+                                       $collections[$dirname] = $dirname;\r
+                               else if ($exceptReadOnly == false)\r
+                                       $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER;\r
+                       }\r
+               }\r
+               closedir($dirhandle);\r
+\r
+               return $collections;\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
+               $filelist = array();\r
+\r
+               // 1. go through all objects and add them to the filelist\r
+\r
+               $mediadir = $DIR_MEDIA . $collection . '/';\r
+\r
+               // return if dir does not exist\r
+               if (!is_dir($mediadir)) return $filelist;\r
+\r
+               $dirhandle = opendir($mediadir);\r
+               while ($filename = readdir($dirhandle)) {\r
+                       // only add files that match the filter\r
+                       if (!@is_dir($filename) && Media::checkFilter($filename, $filter))\r
+                               array_push($filelist, new MediaObject($collection, $filename, filemtime($mediadir . $filename)));\r
+               }\r
+               closedir($dirhandle);\r
+\r
+               // sort array so newer files are shown first\r
+               usort($filelist, 'sort_media');\r
+\r
+               return $filelist;\r
+       }\r
+\r
+       function checkFilter($strText, $strFilter) {\r
+               if ($strFilter == '')\r
+                       return 1;\r
+               else\r
+                       return is_integer(i18n::strpos(strtolower($strText), strtolower($strFilter)));\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
+               global $member, $DIR_MEDIA;\r
+\r
+               // allow creating new private directory\r
+               if ($collectionName === (string)$member->getID())\r
+                       return true;\r
+                       \r
+               $collections = Media::getCollectionList($exceptReadOnly);\r
+               $dirname = $collections[$collectionName];\r
+               if ($dirname == NULL || $dirname === PRIVATE_COLLECTION)\r
+                       return false;  \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
+               // other collections should exist\r
+               return @is_dir($collectionDir);\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
+               global $DIR_MEDIA, $manager;\r
+\r
+               // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php\r
+               $filename = cleanFileName($filename);\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
+                       return _ERROR_BADFILETYPE;\r
+               \r
+               // trigger PreMediaUpload event\r
+               $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));\r
+\r
+               // don't allow uploads to unknown or forbidden collections\r
+               $exceptReadOnly = true;\r
+               if (!Media::isValidCollection($collection,$exceptReadOnly))\r
+                       return _ERROR_DISALLOWED;\r
+\r
+               // check dir permissions (try to create dir if it does not exist)\r
+               $mediadir = $DIR_MEDIA . $collection;\r
+\r
+               // try to create new private media directories if needed\r
+               if (!@is_dir($mediadir) && is_numeric($collection)) {\r
+                       $oldumask = umask(0000);\r
+                       if (!@mkdir($mediadir, 0777))\r
+                               return _ERROR_BADPERMISSIONS;\r
+                       umask($oldumask);\r
+               }\r
+\r
+               // if dir still not exists, the action is disallowed\r
+               if (!@is_dir($mediadir))\r
+                       return _ERROR_DISALLOWED;\r
+\r
+               if (!is_writeable($mediadir))\r
+                       return _ERROR_BADPERMISSIONS;\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
+                       return _ERROR_UPLOADDUPLICATE;\r
+\r
+               // move file to directory\r
+               if (is_uploaded_file($uploadfile)) {\r
+                       if (!@move_uploaded_file($uploadfile, $mediadir . $filename))\r
+                               return _ERROR_UPLOADMOVEP;\r
+               } else {\r
+                       if (!copy($uploadfile, $mediadir . $filename))\r
+                               return _ERROR_UPLOADCOPY ;\r
+               }\r
+\r
+               // chmod uploaded file\r
+               $oldumask = umask(0000);\r
+               @chmod($mediadir . $filename, 0644);\r
+               umask($oldumask);\r
+\r
+               $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));\r
+\r
+               return '';\r
+\r
+       }\r
+\r
+       /**\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
+        * NOTE: does not check if $collection is valid.\r
+        */\r
+       function addMediaObjectRaw($collection, $filename, &$data) {\r
+               global $DIR_MEDIA;\r
+\r
+               // check dir permissions (try to create dir if it does not exist)\r
+               $mediadir = $DIR_MEDIA . $collection;\r
+\r
+               // try to create new private media directories if needed\r
+               if (!@is_dir($mediadir) && is_numeric($collection)) {\r
+                       $oldumask = umask(0000);\r
+                       if (!@mkdir($mediadir, 0777))\r
+                               return _ERROR_BADPERMISSIONS;\r
+                       umask($oldumask);\r
+               }\r
+\r
+               // if dir still not exists, the action is disallowed\r
+               if (!@is_dir($mediadir))\r
+                       return _ERROR_DISALLOWED;\r
+\r
+               if (!is_writeable($mediadir))\r
+                       return _ERROR_BADPERMISSIONS;\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
+                       return _ERROR_UPLOADDUPLICATE;\r
+\r
+               // create file\r
+               $fh = @fopen($mediadir . $filename, 'wb');\r
+               if (!$fh)\r
+                       return _ERROR_UPLOADFAILED;\r
+               $ok = @fwrite($fh, $data);\r
+               @fclose($fh);\r
+               if (!$ok)\r
+                       return _ERROR_UPLOADFAILED;\r
+\r
+               // chmod uploaded file\r
+               $oldumask = umask(0000);\r
+               @chmod($mediadir . $filename, 0644);\r
+               umask($oldumask);\r
+\r
+               return '';\r
+\r
+       }\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
+class MediaObject\r
+{\r
+       public $private;\r
+       public $collection;\r
+       public $filename;\r
+       public $timestamp;\r
+       \r
+       public function __construct($collection, $filename, $timestamp)\r
+       {\r
+               $this->private = is_numeric($collection);\r
+               $this->collection = $collection;\r
+               $this->filename = $filename;\r
+               $this->timestamp = $timestamp;\r
+               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