3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2005 The Nucleus Group
\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
12 * Media classes for nucleus
\r
14 * $Id: MEDIA.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $
\r
20 * Represents the media objects for a certain member
\r
25 * Gets the list of collections available to the currently logged
\r
28 * @returns array of dirname => display name
\r
30 function getCollectionList() {
\r
31 global $member, $DIR_MEDIA;
\r
33 $collections = array();
\r
35 // add private directory for member
\r
36 $collections[$member->getID()] = 'Private Collection';
\r
38 // add global collections
\r
39 if (!is_dir($DIR_MEDIA)) return $collections;
\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
48 closedir($dirhandle);
\r
50 return $collections;
\r
55 * Returns an array of MEDIAOBJECT objects for a certain collection
\r
57 * @param $collection
\r
58 * name of the collection
\r
60 * filter on filename (defaults to none)
\r
62 function getMediaListByCollection($collection, $filter = '') {
\r
65 $filelist = array();
\r
67 // 1. go through all objects and add them to the filelist
\r
69 $mediadir = $DIR_MEDIA . $collection . '/';
\r
71 // return if dir does not exist
\r
72 if (!is_dir($mediadir)) return $filelist;
\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
80 closedir($dirhandle);
\r
82 // sort array so newer files are shown first
\r
83 usort($filelist, 'sort_media');
\r
88 function checkFilter($strText, $strFilter) {
\r
89 if ($strFilter == '')
\r
92 return is_integer(strpos(strtolower($strText), strtolower($strFilter)));
\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
99 function isValidCollection($collectionName) {
\r
100 global $member, $DIR_MEDIA;
\r
102 // private collections only accept uploads from their owners
\r
103 if (is_numeric($collectionName))
\r
104 return ($member->getID() == $collectionName);
\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
112 * Adds an uploaded file to the media archive
\r
114 * @param collection
\r
116 * @param uploadfile
\r
117 * the postFileInfo(..) array
\r
119 * the filename that should be used to save the file as
\r
120 * (date prefix should be already added here)
\r
122 function addMediaObject($collection, $uploadfile, $filename) {
\r
125 // don't allow uploads to unknown or forbidden collections
\r
126 if (!MEDIA::isValidCollection($collection))
\r
127 return _ERROR_DISALLOWED;
\r
129 // check dir permissions (try to create dir if it does not exist)
\r
130 $mediadir = $DIR_MEDIA . $collection;
\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
140 // if dir still not exists, the action is disallowed
\r
141 if (!@is_dir($mediadir))
\r
142 return _ERROR_DISALLOWED;
\r
144 if (!is_writeable($mediadir))
\r
145 return _ERROR_BADPERMISSIONS;
\r
147 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
\r
150 if (file_exists($mediadir . $filename))
\r
151 return _ERROR_UPLOADDUPLICATE;
\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
158 if (!copy($uploadfile, $mediadir . $filename))
\r
159 return _ERROR_UPLOADCOPY ;
\r
162 // chmod uploaded file
\r
163 $oldumask = umask(0000);
\r
164 @chmod($mediadir . $filename, 0644);
\r
172 * Adds an uploaded file to the media dir.
\r
174 * @param $collection
\r
175 * collection to use
\r
177 * the filename that should be used to save the file as
\r
178 * (date prefix should be already added here)
\r
180 * File data (binary)
\r
182 * NOTE: does not check if $collection is valid.
\r
184 function addMediaObjectRaw($collection, $filename, &$data) {
\r
187 // check dir permissions (try to create dir if it does not exist)
\r
188 $mediadir = $DIR_MEDIA . $collection;
\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
198 // if dir still not exists, the action is disallowed
\r
199 if (!@is_dir($mediadir))
\r
200 return _ERROR_DISALLOWED;
\r
202 if (!is_writeable($mediadir))
\r
203 return _ERROR_BADPERMISSIONS;
\r
205 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
\r
208 if (file_exists($mediadir . $filename))
\r
209 return _ERROR_UPLOADDUPLICATE;
\r
212 $fh = @fopen($mediadir . $filename, 'wb');
\r
214 return _ERROR_UPLOADFAILED;
\r
215 $ok = @fwrite($fh, $data);
\r
218 return _ERROR_UPLOADFAILED;
\r
220 // chmod uploaded file
\r
221 $oldumask = umask(0000);
\r
222 @chmod($mediadir . $filename, 0644);
\r
232 * Represents the characteristics of one single media-object
\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
240 class MEDIAOBJECT {
\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
257 * User-defined sort method to sort an array of MEDIAOBJECTS
\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