3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
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)
13 * Media classes for nucleus
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
18 * $NucleusJP: MEDIA.php,v 1.5 2006/07/17 20:03:44 kimitake Exp $
21 define('PRIVATE_COLLECTION', 'Private Collection');
22 define('READ_ONLY_MEDIA_FOLDER', '(Read Only)');
25 * Represents the media objects for a certain member
30 * Gets the list of collections available to the currently logged
33 * @returns array of dirname => display name
35 function getCollectionList($exceptReadOnly = false) {
36 global $member, $DIR_MEDIA;
38 $collections = array();
40 // add private directory for member
41 $collections[$member->getID()] = PRIVATE_COLLECTION;
43 // add global collections
44 if (!is_dir($DIR_MEDIA)) return $collections;
46 $dirhandle = opendir($DIR_MEDIA);
47 while ($dirname = readdir($dirhandle)) {
48 // only add non-numeric (numeric=private) dirs
49 if (@is_dir($DIR_MEDIA . $dirname) &&
52 ($dirname != 'CVS') &&
53 (!is_numeric($dirname))) {
54 if (@is_writable($DIR_MEDIA . $dirname))
55 $collections[$dirname] = $dirname;
56 else if ($exceptReadOnly == false)
57 $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER;
67 * Returns an array of MEDIAOBJECT objects for a certain collection
70 * name of the collection
72 * filter on filename (defaults to none)
74 function getMediaListByCollection($collection, $filter = '') {
79 // 1. go through all objects and add them to the filelist
81 $mediadir = $DIR_MEDIA . $collection . '/';
83 // return if dir does not exist
84 if (!is_dir($mediadir)) return $filelist;
86 $dirhandle = opendir($mediadir);
87 while ($filename = readdir($dirhandle)) {
88 // only add files that match the filter
89 if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter))
90 array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename)));
94 // sort array so newer files are shown first
95 usort($filelist, 'sort_media');
100 function checkFilter($strText, $strFilter) {
101 if ($strFilter == '')
104 return is_integer(strpos(strtolower($strText), strtolower($strFilter)));
108 * checks if a collection exists with the given name, and if it's
109 * allowed for the currently logged in member to upload files to it
111 function isValidCollection($collectionName, $exceptReadOnly = false) {
112 global $member, $DIR_MEDIA;
114 // allow creating new private directory
115 if ($collectionName === (string)$member->getID())
118 $collections = MEDIA::getCollectionList($exceptReadOnly);
119 $dirname = $collections[$collectionName];
120 if ($dirname == NULL || $dirname === PRIVATE_COLLECTION)
123 // other collections should exist and be writable
124 $collectionDir = $DIR_MEDIA . $collectionName;
126 return (@is_dir($collectionDir) && @is_writable($collectionDir));
128 // other collections should exist
129 return @is_dir($collectionDir);
133 * Adds an uploaded file to the media archive
138 * the postFileInfo(..) array
140 * the filename that should be used to save the file as
141 * (date prefix should be already added here)
143 function addMediaObject($collection, $uploadfile, $filename) {
144 global $DIR_MEDIA, $manager;
146 $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));
148 // don't allow uploads to unknown or forbidden collections
149 $exceptReadOnly = true;
150 if (!MEDIA::isValidCollection($collection,$exceptReadOnly))
151 return _ERROR_DISALLOWED;
153 // check dir permissions (try to create dir if it does not exist)
154 $mediadir = $DIR_MEDIA . $collection;
156 // try to create new private media directories if needed
157 if (!@is_dir($mediadir) && is_numeric($collection)) {
158 $oldumask = umask(0000);
159 if (!@mkdir($mediadir, 0777))
160 return _ERROR_BADPERMISSIONS;
164 // if dir still not exists, the action is disallowed
165 if (!@is_dir($mediadir))
166 return _ERROR_DISALLOWED;
168 if (!is_writeable($mediadir))
169 return _ERROR_BADPERMISSIONS;
171 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
174 if (file_exists($mediadir . $filename))
175 return _ERROR_UPLOADDUPLICATE;
177 // move file to directory
178 if (is_uploaded_file($uploadfile)) {
179 if (!@move_uploaded_file($uploadfile, $mediadir . $filename))
180 return _ERROR_UPLOADMOVEP;
182 if (!copy($uploadfile, $mediadir . $filename))
183 return _ERROR_UPLOADCOPY ;
186 // chmod uploaded file
187 $oldumask = umask(0000);
188 @chmod($mediadir . $filename, 0644);
191 $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));
198 * Adds an uploaded file to the media dir.
203 * the filename that should be used to save the file as
204 * (date prefix should be already added here)
208 * NOTE: does not check if $collection is valid.
210 function addMediaObjectRaw($collection, $filename, &$data) {
213 // check dir permissions (try to create dir if it does not exist)
214 $mediadir = $DIR_MEDIA . $collection;
216 // try to create new private media directories if needed
217 if (!@is_dir($mediadir) && is_numeric($collection)) {
218 $oldumask = umask(0000);
219 if (!@mkdir($mediadir, 0777))
220 return _ERROR_BADPERMISSIONS;
224 // if dir still not exists, the action is disallowed
225 if (!@is_dir($mediadir))
226 return _ERROR_DISALLOWED;
228 if (!is_writeable($mediadir))
229 return _ERROR_BADPERMISSIONS;
231 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
234 if (file_exists($mediadir . $filename))
235 return _ERROR_UPLOADDUPLICATE;
238 $fh = @fopen($mediadir . $filename, 'wb');
240 return _ERROR_UPLOADFAILED;
241 $ok = @fwrite($fh, $data);
244 return _ERROR_UPLOADFAILED;
246 // chmod uploaded file
247 $oldumask = umask(0000);
248 @chmod($mediadir . $filename, 0644);
258 * Represents the characteristics of one single media-object
260 * Description of properties:
261 * - filename: filename, without paths
262 * - timestamp: last modification (unix timestamp)
263 * - collection: collection to which the file belongs (can also be a owner ID, for private collections)
264 * - private: true if the media belongs to a private member collection
273 function MEDIAOBJECT($collection, $filename, $timestamp) {
274 $this->private = is_numeric($collection);
275 $this->collection = $collection;
276 $this->filename = $filename;
277 $this->timestamp = $timestamp;
283 * User-defined sort method to sort an array of MEDIAOBJECTS
285 function sort_media($a, $b) {
286 if ($a->timestamp == $b->timestamp) return 0;
287 return ($a->timestamp > $b->timestamp) ? -1 : 1;