OSDN Git Service

9dc77932a470b81a33d44aa1fd861e38fa4a05ad
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / MEDIA.php
1 <?php\r
2 /**\r
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
4   * Copyright (C) 2002-2005 The Nucleus Group\r
5   *\r
6   * This program is free software; you can redistribute it and/or\r
7   * modify it under the terms of the GNU General Public License\r
8   * as published by the Free Software Foundation; either version 2\r
9   * of the License, or (at your option) any later version.\r
10   * (see nucleus/documentation/index.html#license for more info)\r
11   *\r
12   * Media classes for nucleus\r
13   *\r
14   * $Id: MEDIA.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $\r
15   * $NucleusJP$\r
16   */\r
17 \r
18 \r
19 /**\r
20   * Represents the media objects for a certain member\r
21   */\r
22 class MEDIA {\r
23         \r
24         /**\r
25           * Gets the list of collections available to the currently logged\r
26           * in member \r
27           *\r
28           * @returns array of dirname => display name\r
29           */\r
30         function getCollectionList() {\r
31                 global $member, $DIR_MEDIA;\r
32                 \r
33                 $collections = array();\r
34                 \r
35                 // add private directory for member\r
36                 $collections[$member->getID()] = 'Private Collection';\r
37                 \r
38                 // add global collections\r
39                 if (!is_dir($DIR_MEDIA)) return $collections;\r
40                 \r
41                 $dirhandle = opendir($DIR_MEDIA);\r
42                 while ($dirname = readdir($dirhandle)) {\r
43                         // only add non-numeric (numeric=private) dirs\r
44                         if (@is_dir($DIR_MEDIA . $dirname) && ($dirname != '.') && ($dirname != '..') && ($dirname != 'CVS') && (!is_numeric($dirname)))  {\r
45                                 $collections[$dirname] = $dirname;\r
46                         }\r
47                 }\r
48                 closedir($dirhandle);\r
49                 \r
50                 return $collections;\r
51                 \r
52         }\r
53 \r
54         /**\r
55           * Returns an array of MEDIAOBJECT objects for a certain collection\r
56           *\r
57           * @param $collection\r
58           *             name of the collection\r
59           * @param $filter\r
60           *             filter on filename (defaults to none)     \r
61           */\r
62         function getMediaListByCollection($collection, $filter = '') {\r
63                 global $DIR_MEDIA;\r
64 \r
65                 $filelist = array();    \r
66                 \r
67                 // 1. go through all objects and add them to the filelist\r
68 \r
69                 $mediadir = $DIR_MEDIA . $collection . '/';\r
70                 \r
71                 // return if dir does not exist\r
72                 if (!is_dir($mediadir)) return $filelist;\r
73                 \r
74                 $dirhandle = opendir($mediadir);\r
75                 while ($filename = readdir($dirhandle)) {\r
76                         // only add files that match the filter\r
77                         if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter))\r
78                                 array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename)));\r
79                 }\r
80                 closedir($dirhandle);\r
81 \r
82                 // sort array so newer files are shown first\r
83                 usort($filelist, 'sort_media');\r
84                 \r
85                 return $filelist;\r
86         }\r
87         \r
88         function checkFilter($strText, $strFilter) {\r
89                 if ($strFilter == '')\r
90                         return 1;\r
91                 else \r
92                         return is_integer(strpos(strtolower($strText), strtolower($strFilter)));\r
93         }\r
94 \r
95         /**\r
96           * checks if a collection exists with the given name, and if it's \r
97           * allowed for the currently logged in member to upload files to it\r
98           */ \r
99         function isValidCollection($collectionName) {\r
100                 global $member, $DIR_MEDIA;\r
101                 \r
102                 // private collections only accept uploads from their owners\r
103                 if (is_numeric($collectionName))\r
104                         return ($member->getID() == $collectionName);\r
105                         \r
106                 // other collections should exists and be writable\r
107                 $collectionDir = $DIR_MEDIA . $collectionName;\r
108                 return (@is_dir($collectionDir) || @is_writable($collectionDir));\r
109         }\r
110 \r
111         /**\r
112           * Adds an uploaded file to the media archive\r
113           *\r
114           * @param collection\r
115           *             collection\r
116           * @param uploadfile\r
117           *             the postFileInfo(..) array \r
118           * @param filename\r
119           *             the filename that should be used to save the file as\r
120           *             (date prefix should be already added here)\r
121           */\r
122         function addMediaObject($collection, $uploadfile, $filename) {\r
123                 global $DIR_MEDIA;\r
124                 \r
125                 // don't allow uploads to unknown or forbidden collections\r
126                 if (!MEDIA::isValidCollection($collection))\r
127                         return _ERROR_DISALLOWED;\r
128 \r
129                 // check dir permissions (try to create dir if it does not exist)\r
130                 $mediadir = $DIR_MEDIA . $collection;\r
131 \r
132                 // try to create new private media directories if needed\r
133                 if (!@is_dir($mediadir) && is_numeric($collection)) {\r
134                         $oldumask = umask(0000);\r
135                         if (!@mkdir($mediadir, 0777))\r
136                                 return _ERROR_BADPERMISSIONS;\r
137                         umask($oldumask);                               \r
138                 } \r
139                 \r
140                 // if dir still not exists, the action is disallowed\r
141                 if (!@is_dir($mediadir))\r
142                         return _ERROR_DISALLOWED;\r
143                 \r
144                 if (!is_writeable($mediadir))\r
145                         return _ERROR_BADPERMISSIONS;\r
146                         \r
147                 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)\r
148                 $mediadir .= '/';\r
149                         \r
150                 if (file_exists($mediadir . $filename))\r
151                         return _ERROR_UPLOADDUPLICATE;\r
152 \r
153                 // move file to directory\r
154                 if (is_uploaded_file($uploadfile)) {\r
155                         if (!@move_uploaded_file($uploadfile, $mediadir . $filename))\r
156                                 return _ERROR_UPLOADMOVEP;\r
157                 } else {\r
158                         if (!copy($uploadfile, $mediadir . $filename))\r
159                                 return _ERROR_UPLOADCOPY ;\r
160                 }\r
161                 \r
162                 // chmod uploaded file\r
163                 $oldumask = umask(0000);\r
164                 @chmod($mediadir . $filename, 0644); \r
165                 umask($oldumask);               \r
166                 \r
167                 return '';\r
168         \r
169         }\r
170         \r
171         /**\r
172          * Adds an uploaded file to the media dir. \r
173          *\r
174          * @param $collection\r
175          *              collection to use\r
176          * @param $filename\r
177          *              the filename that should be used to save the file as\r
178          *              (date prefix should be already added here)\r
179          * @param &$data\r
180          *              File data (binary)\r
181          *\r
182          * NOTE: does not check if $collection is valid.\r
183          */\r
184         function addMediaObjectRaw($collection, $filename, &$data) {\r
185                 global $DIR_MEDIA;\r
186                 \r
187                 // check dir permissions (try to create dir if it does not exist)\r
188                 $mediadir = $DIR_MEDIA . $collection;\r
189 \r
190                 // try to create new private media directories if needed\r
191                 if (!@is_dir($mediadir) && is_numeric($collection)) {\r
192                         $oldumask = umask(0000);\r
193                         if (!@mkdir($mediadir, 0777))\r
194                                 return _ERROR_BADPERMISSIONS;\r
195                         umask($oldumask);                               \r
196                 } \r
197                 \r
198                 // if dir still not exists, the action is disallowed\r
199                 if (!@is_dir($mediadir))\r
200                         return _ERROR_DISALLOWED;\r
201                 \r
202                 if (!is_writeable($mediadir))\r
203                         return _ERROR_BADPERMISSIONS;\r
204                         \r
205                 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)\r
206                 $mediadir .= '/';\r
207                         \r
208                 if (file_exists($mediadir . $filename))\r
209                         return _ERROR_UPLOADDUPLICATE;\r
210 \r
211                 // create file\r
212                 $fh = @fopen($mediadir . $filename, 'wb');\r
213                 if (!$fh) \r
214                         return _ERROR_UPLOADFAILED;\r
215                 $ok = @fwrite($fh, $data);\r
216                 @fclose($fh);\r
217                 if (!$ok)\r
218                         return _ERROR_UPLOADFAILED;\r
219                 \r
220                 // chmod uploaded file\r
221                 $oldumask = umask(0000);\r
222                 @chmod($mediadir . $filename, 0644); \r
223                 umask($oldumask);               \r
224                 \r
225                 return '';\r
226         \r
227         }\r
228 \r
229 }\r
230 \r
231 /**\r
232   * Represents the characteristics of one single media-object\r
233   *\r
234   * Description of properties:\r
235   *  - filename: filename, without paths\r
236   *  - timestamp: last modification (unix timestamp)\r
237   *  - collection: collection to which the file belongs (can also be a owner ID, for private collections)\r
238   *  - private: true if the media belongs to a private member collection\r
239   */\r
240 class MEDIAOBJECT {\r
241 \r
242         var $private;\r
243         var $collection;\r
244         var $filename;\r
245         var $timestamp;\r
246 \r
247         function MEDIAOBJECT($collection, $filename, $timestamp) {\r
248                 $this->private = is_numeric($collection);\r
249                 $this->collection = $collection;\r
250                 $this->filename = $filename;\r
251                 $this->timestamp = $timestamp;\r
252         }\r
253         \r
254 }\r
255 \r
256 /**\r
257   * User-defined sort method to sort an array of MEDIAOBJECTS\r
258   */\r
259 function sort_media($a, $b) {\r
260     if ($a->timestamp == $b->timestamp) return 0;\r
261     return ($a->timestamp > $b->timestamp) ? -1 : 1;\r
262 }\r
263 \r
264 ?>\r