OSDN Git Service

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