00001 <?php 00002 /* 00003 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00004 * Copyright (C) 2002-2007 The Nucleus Group 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * (see nucleus/documentation/index.html#license for more info) 00011 */ 00025 class MEDIA { 00026 00033 function getCollectionList() { 00034 global $member, $DIR_MEDIA; 00035 00036 $collections = array(); 00037 00038 // add private directory for member 00039 $collections[$member->getID()] = 'Private Collection'; 00040 00041 // add global collections 00042 if (!is_dir($DIR_MEDIA)) return $collections; 00043 00044 $dirhandle = opendir($DIR_MEDIA); 00045 while ($dirname = readdir($dirhandle)) { 00046 // only add non-numeric (numeric=private) dirs 00047 if (@is_dir($DIR_MEDIA . $dirname) && ($dirname != '.') && ($dirname != '..') && ($dirname != 'CVS') && (!is_numeric($dirname))) { 00048 $collections[$dirname] = $dirname; 00049 } 00050 } 00051 closedir($dirhandle); 00052 00053 return $collections; 00054 00055 } 00056 00065 function getMediaListByCollection($collection, $filter = '') { 00066 global $DIR_MEDIA; 00067 00068 $filelist = array(); 00069 00070 // 1. go through all objects and add them to the filelist 00071 00072 $mediadir = $DIR_MEDIA . $collection . '/'; 00073 00074 // return if dir does not exist 00075 if (!is_dir($mediadir)) return $filelist; 00076 00077 $dirhandle = opendir($mediadir); 00078 while ($filename = readdir($dirhandle)) { 00079 // only add files that match the filter 00080 if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter)) 00081 array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename))); 00082 } 00083 closedir($dirhandle); 00084 00085 // sort array so newer files are shown first 00086 usort($filelist, 'sort_media'); 00087 00088 return $filelist; 00089 } 00090 00091 function checkFilter($strText, $strFilter) { 00092 if ($strFilter == '') 00093 return 1; 00094 else 00095 return is_integer(strpos(strtolower($strText), strtolower($strFilter))); 00096 } 00097 00102 function isValidCollection($collectionName) { 00103 global $member, $DIR_MEDIA; 00104 00105 // private collections only accept uploads from their owners 00106 if (is_numeric($collectionName)) 00107 return ($member->getID() == $collectionName); 00108 00109 // other collections should exists and be writable 00110 $collectionDir = $DIR_MEDIA . $collectionName; 00111 return (@is_dir($collectionDir) || @is_writable($collectionDir)); 00112 } 00113 00125 function addMediaObject($collection, $uploadfile, $filename) { 00126 global $DIR_MEDIA, $manager; 00127 00128 $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename)); 00129 00130 // don't allow uploads to unknown or forbidden collections 00131 if (!MEDIA::isValidCollection($collection)) 00132 return _ERROR_DISALLOWED; 00133 00134 // check dir permissions (try to create dir if it does not exist) 00135 $mediadir = $DIR_MEDIA . $collection; 00136 00137 // try to create new private media directories if needed 00138 if (!@is_dir($mediadir) && is_numeric($collection)) { 00139 $oldumask = umask(0000); 00140 if (!@mkdir($mediadir, 0777)) 00141 return _ERROR_BADPERMISSIONS; 00142 umask($oldumask); 00143 } 00144 00145 // if dir still not exists, the action is disallowed 00146 if (!@is_dir($mediadir)) 00147 return _ERROR_DISALLOWED; 00148 00149 if (!is_writeable($mediadir)) 00150 return _ERROR_BADPERMISSIONS; 00151 00152 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems) 00153 $mediadir .= '/'; 00154 00155 if (file_exists($mediadir . $filename)) 00156 return _ERROR_UPLOADDUPLICATE; 00157 00158 // move file to directory 00159 if (is_uploaded_file($uploadfile)) { 00160 if (!@move_uploaded_file($uploadfile, $mediadir . $filename)) 00161 return _ERROR_UPLOADMOVEP; 00162 } else { 00163 if (!copy($uploadfile, $mediadir . $filename)) 00164 return _ERROR_UPLOADCOPY ; 00165 } 00166 00167 // chmod uploaded file 00168 $oldumask = umask(0000); 00169 @chmod($mediadir . $filename, 0644); 00170 umask($oldumask); 00171 00172 $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename)); 00173 00174 return ''; 00175 00176 } 00177 00191 function addMediaObjectRaw($collection, $filename, &$data) { 00192 global $DIR_MEDIA; 00193 00194 // check dir permissions (try to create dir if it does not exist) 00195 $mediadir = $DIR_MEDIA . $collection; 00196 00197 // try to create new private media directories if needed 00198 if (!@is_dir($mediadir) && is_numeric($collection)) { 00199 $oldumask = umask(0000); 00200 if (!@mkdir($mediadir, 0777)) 00201 return _ERROR_BADPERMISSIONS; 00202 umask($oldumask); 00203 } 00204 00205 // if dir still not exists, the action is disallowed 00206 if (!@is_dir($mediadir)) 00207 return _ERROR_DISALLOWED; 00208 00209 if (!is_writeable($mediadir)) 00210 return _ERROR_BADPERMISSIONS; 00211 00212 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems) 00213 $mediadir .= '/'; 00214 00215 if (file_exists($mediadir . $filename)) 00216 return _ERROR_UPLOADDUPLICATE; 00217 00218 // create file 00219 $fh = @fopen($mediadir . $filename, 'wb'); 00220 if (!$fh) 00221 return _ERROR_UPLOADFAILED; 00222 $ok = @fwrite($fh, $data); 00223 @fclose($fh); 00224 if (!$ok) 00225 return _ERROR_UPLOADFAILED; 00226 00227 // chmod uploaded file 00228 $oldumask = umask(0000); 00229 @chmod($mediadir . $filename, 0644); 00230 umask($oldumask); 00231 00232 return ''; 00233 00234 } 00235 00236 } 00237 00247 class MEDIAOBJECT { 00248 00249 var $private; 00250 var $collection; 00251 var $filename; 00252 var $timestamp; 00253 00254 function MEDIAOBJECT($collection, $filename, $timestamp) { 00255 $this->private = is_numeric($collection); 00256 $this->collection = $collection; 00257 $this->filename = $filename; 00258 $this->timestamp = $timestamp; 00259 } 00260 00261 } 00262 00266 function sort_media($a, $b) { 00267 if ($a->timestamp == $b->timestamp) return 0; 00268 return ($a->timestamp > $b->timestamp) ? -1 : 1; 00269 } 00270 00271 ?>