3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2007 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-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 $
23 * Represents the media objects for a certain member
28 * Gets the list of collections available to the currently logged
31 * @returns array of dirname => display name
33 function getCollectionList() {
34 global $member, $DIR_MEDIA;
36 $collections = array();
38 // add private directory for member
39 $collections[$member->getID()] = 'Private Collection';
41 // add global collections
42 if (!is_dir($DIR_MEDIA)) return $collections;
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;
58 * Returns an array of MEDIAOBJECT objects for a certain collection
61 * name of the collection
63 * filter on filename (defaults to none)
65 function getMediaListByCollection($collection, $filter = '') {
70 // 1. go through all objects and add them to the filelist
72 $mediadir = $DIR_MEDIA . $collection . '/';
74 // return if dir does not exist
75 if (!is_dir($mediadir)) return $filelist;
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)));
85 // sort array so newer files are shown first
86 usort($filelist, 'sort_media');
91 function checkFilter($strText, $strFilter) {
95 return is_integer(strpos(strtolower($strText), strtolower($strFilter)));
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
102 function isValidCollection($collectionName) {
103 global $member, $DIR_MEDIA;
105 // private collections only accept uploads from their owners
106 if (is_numeric($collectionName))
107 return ($member->getID() == $collectionName);
109 // other collections should exists and be writable
110 $collectionDir = $DIR_MEDIA . $collectionName;
111 return (@is_dir($collectionDir) || @is_writable($collectionDir));
115 * Adds an uploaded file to the media archive
120 * the postFileInfo(..) array
122 * the filename that should be used to save the file as
123 * (date prefix should be already added here)
125 function addMediaObject($collection, $uploadfile, $filename) {
126 global $DIR_MEDIA, $manager;
128 $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));
130 // don't allow uploads to unknown or forbidden collections
131 if (!MEDIA::isValidCollection($collection))
132 return _ERROR_DISALLOWED;
134 // check dir permissions (try to create dir if it does not exist)
135 $mediadir = $DIR_MEDIA . $collection;
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;
145 // if dir still not exists, the action is disallowed
146 if (!@is_dir($mediadir))
147 return _ERROR_DISALLOWED;
149 if (!is_writeable($mediadir))
150 return _ERROR_BADPERMISSIONS;
152 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
155 if (file_exists($mediadir . $filename))
156 return _ERROR_UPLOADDUPLICATE;
158 // move file to directory
159 if (is_uploaded_file($uploadfile)) {
160 if (!@move_uploaded_file($uploadfile, $mediadir . $filename))
161 return _ERROR_UPLOADMOVEP;
163 if (!copy($uploadfile, $mediadir . $filename))
164 return _ERROR_UPLOADCOPY ;
167 // chmod uploaded file
168 $oldumask = umask(0000);
169 @chmod($mediadir . $filename, 0644);
172 $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));
179 * Adds an uploaded file to the media dir.
184 * the filename that should be used to save the file as
185 * (date prefix should be already added here)
189 * NOTE: does not check if $collection is valid.
191 function addMediaObjectRaw($collection, $filename, &$data) {
194 // check dir permissions (try to create dir if it does not exist)
195 $mediadir = $DIR_MEDIA . $collection;
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;
205 // if dir still not exists, the action is disallowed
206 if (!@is_dir($mediadir))
207 return _ERROR_DISALLOWED;
209 if (!is_writeable($mediadir))
210 return _ERROR_BADPERMISSIONS;
212 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
215 if (file_exists($mediadir . $filename))
216 return _ERROR_UPLOADDUPLICATE;
219 $fh = @fopen($mediadir . $filename, 'wb');
221 return _ERROR_UPLOADFAILED;
222 $ok = @fwrite($fh, $data);
225 return _ERROR_UPLOADFAILED;
227 // chmod uploaded file
228 $oldumask = umask(0000);
229 @chmod($mediadir . $filename, 0644);
239 * Represents the characteristics of one single media-object
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
254 function MEDIAOBJECT($collection, $filename, $timestamp) {
255 $this->private = is_numeric($collection);
256 $this->collection = $collection;
257 $this->filename = $filename;
258 $this->timestamp = $timestamp;
264 * User-defined sort method to sort an array of MEDIAOBJECTS
266 function sort_media($a, $b) {
267 if ($a->timestamp == $b->timestamp) return 0;
268 return ($a->timestamp > $b->timestamp) ? -1 : 1;