3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2012 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
13 * Media classes for nucleus
\r
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
17 * @version $Id: MEDIA.php 1864 2012-05-19 11:39:44Z sakamocchi $
20 define('PRIVATE_COLLECTION', 'Private Collection');
\r
21 define('READ_ONLY_MEDIA_FOLDER', '(Read Only)');
\r
26 * Media::getCollectionList()
\r
27 * Gets the list of collections available to the currently logged
\r
30 * @returns array of dirname => display name
\r
32 static public function getCollectionList($exceptReadOnly = FALSE)
\r
34 global $member, $DIR_MEDIA;
\r
36 $collections = array();
\r
38 // add private directory for member
\r
39 $collections[$member->getID()] = PRIVATE_COLLECTION;
\r
41 // add global collections
\r
42 if ( !is_dir($DIR_MEDIA) )
\r
44 return $collections;
\r
47 $dirhandle = opendir($DIR_MEDIA);
\r
48 while ( $dirname = readdir($dirhandle) )
\r
50 // only add non-numeric (numeric=private) dirs
\r
51 if ( @is_dir($DIR_MEDIA . $dirname) &&
\r
52 ($dirname != '.') &&
\r
53 ($dirname != '..') &&
\r
54 ($dirname != 'CVS') &&
\r
55 (!is_numeric($dirname)) )
\r
57 if ( @is_writable($DIR_MEDIA . $dirname) )
\r
59 $collections[$dirname] = $dirname;
\r
61 else if ( $exceptReadOnly == FALSE )
\r
63 $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER;
\r
67 closedir($dirhandle);
\r
69 return $collections;
\r
73 * Media::getMediaListByCollection()
\r
74 * Returns an array of MediaObject objects for a certain collection
\r
76 * @param string $collection name of the collection
\r
77 * @param string $filter filter on filename (defaults to none)
\r
80 static public function getMediaListByCollection($collection, $filter = '')
\r
82 global $CONF, $DIR_MEDIA;
\r
84 $filelist = array();
\r
86 // 1. go through all objects and add them to the filelist
\r
87 $mediadir = $DIR_MEDIA . $collection . '/';
\r
89 // return if dir does not exist
\r
90 if ( !is_dir($mediadir) )
\r
95 $dirhandle = opendir($mediadir);
\r
96 while ( $filename = readdir($dirhandle) )
\r
98 // only add files that match the filter
\r
99 if ( !@is_dir($filename) && self::checkFilter($filename, $filter) )
\r
101 array_push($filelist, new MediaObject($collection, $filename, filemtime($mediadir . $filename)));
\r
104 closedir($dirhandle);
\r
106 // sort array so newer files are shown first
\r
107 usort($filelist, array(__CLASS__, 'sort_media'));
\r
113 * Media::checkFilter()
\r
115 * @param string $strText
\r
116 * @param string $strFilter
\r
119 static public function checkFilter($strText, $strFilter)
\r
121 if ( $strFilter == '' )
\r
127 return is_integer(i18n::strpos(strtolower($strText), strtolower($strFilter)));
\r
132 * Media::isValidCollection()
\r
133 * checks if a collection exists with the given name, and if it's
\r
134 * allowed for the currently logged in member to upload files to it
\r
136 * @param string $collectionName
\r
137 * @param string $exceptReadOnly
\r
140 static public function isValidCollection($collectionName, $exceptReadOnly = FALSE)
\r
142 global $member, $DIR_MEDIA;
\r
144 // allow creating new private directory
\r
145 if ( $collectionName === (string)$member->getID() )
\r
150 $collections = self::getCollectionList($exceptReadOnly);
\r
151 $dirname = $collections[$collectionName];
\r
153 if ( $dirname == NULL || $dirname === PRIVATE_COLLECTION )
\r
158 // other collections should exist and be writable
\r
159 $collectionDir = $DIR_MEDIA . $collectionName;
\r
160 if ( $exceptReadOnly )
\r
162 return ( @is_dir($collectionDir) && @is_writable($collectionDir) );
\r
165 // other collections should exist
\r
166 return @is_dir($collectionDir);
\r
170 * Media::addMediaObject()
\r
171 * Adds an uploaded file to the media archive
\r
173 * @param string $collection collection
\r
174 * @param array $uploadfile the postFileInfo(..) array
\r
175 * @param string $filename the filename that should be used to save the file as
\r
176 * (date prefix should be already added here)
\r
177 * @return string blank if success, message if failed
\r
179 static public function addMediaObject($collection, $uploadfile, $filename)
\r
181 global $DIR_MEDIA, $manager;
\r
183 // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php
\r
184 $filename = cleanFileName($filename);
\r
186 // should already have tested for allowable types before calling this method. This will only catch files with no extension at all
\r
187 if ( $filename === FALSE )
\r
189 return _ERROR_BADFILETYPE;
\r
192 // trigger PreMediaUpload event
\r
193 $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename));
\r
195 // don't allow uploads to unknown or forbidden collections
\r
196 $exceptReadOnly = TRUE;
\r
197 if ( !self::isValidCollection($collection,$exceptReadOnly) )
\r
199 return _ERROR_DISALLOWED;
\r
202 // check dir permissions (try to create dir if it does not exist)
\r
203 $mediadir = $DIR_MEDIA . $collection;
\r
205 // try to create new private media directories if needed
\r
206 if ( !@is_dir($mediadir) && is_numeric($collection) )
\r
208 $oldumask = umask(0000);
\r
209 if ( !@mkdir($mediadir, 0777) )
\r
211 return _ERROR_BADPERMISSIONS;
\r
216 // if dir still not exists, the action is disallowed
\r
217 if ( !@is_dir($mediadir) )
\r
219 return _ERROR_DISALLOWED;
\r
222 if ( !is_writeable($mediadir) )
\r
224 return _ERROR_BADPERMISSIONS;
\r
227 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
\r
230 if ( file_exists($mediadir . $filename) )
\r
232 return _ERROR_UPLOADDUPLICATE;
\r
235 // move file to directory
\r
236 if ( is_uploaded_file($uploadfile) )
\r
238 if ( !@move_uploaded_file($uploadfile, $mediadir . $filename) )
\r
240 return _ERROR_UPLOADMOVEP;
\r
245 if ( !copy($uploadfile, $mediadir . $filename) )
\r
247 return _ERROR_UPLOADCOPY ;
\r
251 // chmod uploaded file
\r
252 $oldumask = umask(0000);
\r
253 @chmod($mediadir . $filename, 0644);
\r
256 $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename));
\r
262 * Media::addMediaObjectRaw()
\r
263 * Adds an uploaded file to the media dir.
\r
265 * NOTE: does not check if $collection is valid.
\r
267 * @param string $collection collection to use
\r
268 * @param string $filename the filename that should be used to save the file
\r
269 * as (date prefix should be already added here)
\r
270 * @param &$data File data (binary)
\r
271 * @return string blank if success, message if failed
\r
273 static public function addMediaObjectRaw($collection, $filename, &$data)
\r
277 // check dir permissions (try to create dir if it does not exist)
\r
278 $mediadir = $DIR_MEDIA . $collection;
\r
280 // try to create new private media directories if needed
\r
281 if ( !@is_dir($mediadir) && is_numeric($collection) )
\r
283 $oldumask = umask(0000);
\r
284 if ( !@mkdir($mediadir, 0777) )
\r
286 return _ERROR_BADPERMISSIONS;
\r
291 // if dir still not exists, the action is disallowed
\r
292 if ( !@is_dir($mediadir) )
\r
294 return _ERROR_DISALLOWED;
\r
297 if ( !is_writeable($mediadir) )
\r
299 return _ERROR_BADPERMISSIONS;
\r
302 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
\r
305 if ( file_exists($mediadir . $filename) )
\r
307 return _ERROR_UPLOADDUPLICATE;
\r
311 $fh = @fopen($mediadir . $filename, 'wb');
\r
314 return _ERROR_UPLOADFAILED;
\r
316 $ok = @fwrite($fh, $data);
\r
320 return _ERROR_UPLOADFAILED;
\r
323 // chmod uploaded file
\r
324 $oldumask = umask(0000);
\r
325 @chmod($mediadir . $filename, 0644);
\r
332 * Media::sort_media()
\r
333 * User-defined sort method to sort an array of MediaObjects
\r
339 static private function sort_media($a, $b)
\r
341 if ($a->timestamp == $b->timestamp) return 0;
\r
342 return ($a->timestamp > $b->timestamp) ? -1 : 1;
\r
347 * Represents the characteristics of one single media-object
\r
349 * Description of properties:
\r
350 * - filename: filename, without paths
\r
351 * - timestamp: last modification (unix timestamp)
\r
352 * - collection: collection to which the file belongs (can also be a owner ID, for private collections)
\r
353 * - private: true if the media belongs to a private member collection
\r
358 public $collection;
\r
362 public function __construct($collection, $filename, $timestamp)
\r
364 $this->private = is_numeric($collection);
\r
365 $this->collection = $collection;
\r
366 $this->filename = $filename;
\r
367 $this->timestamp = $timestamp;
\r