| [ Index ] |
PHP Cross Reference of Nucleus CMS v3.51 code documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 4 * Copyright (C) 2002-2009 The Nucleus Group 5 * 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) 11 */ 12 /** 13 * Media classes for nucleus 14 * 15 * @license http://nucleuscms.org/license.txt GNU General Public License 16 * @copyright Copyright (C) 2002-2009 The Nucleus Group 17 * @version $Id: MEDIA.php 1388 2009-07-18 06:31:28Z shizuki $ 18 */ 19 20 define('PRIVATE_COLLECTION', 'Private Collection'); 21 define('READ_ONLY_MEDIA_FOLDER', '(Read Only)'); 22 23 /** 24 * Represents the media objects for a certain member 25 */ 26 class MEDIA { 27 28 /** 29 * Gets the list of collections available to the currently logged 30 * in member 31 * 32 * @returns array of dirname => display name 33 */ 34 function getCollectionList($exceptReadOnly = false) { 35 global $member, $DIR_MEDIA; 36 37 $collections = array(); 38 39 // add private directory for member 40 $collections[$member->getID()] = PRIVATE_COLLECTION; 41 42 // add global collections 43 if (!is_dir($DIR_MEDIA)) return $collections; 44 45 $dirhandle = opendir($DIR_MEDIA); 46 while ($dirname = readdir($dirhandle)) { 47 // only add non-numeric (numeric=private) dirs 48 if (@is_dir($DIR_MEDIA . $dirname) && 49 ($dirname != '.') && 50 ($dirname != '..') && 51 ($dirname != 'CVS') && 52 (!is_numeric($dirname))) { 53 if (@is_writable($DIR_MEDIA . $dirname)) 54 $collections[$dirname] = $dirname; 55 else if ($exceptReadOnly == false) 56 $collections[$dirname] = $dirname . ' ' . READ_ONLY_MEDIA_FOLDER; 57 } 58 } 59 closedir($dirhandle); 60 61 return $collections; 62 63 } 64 65 /** 66 * Returns an array of MEDIAOBJECT objects for a certain collection 67 * 68 * @param $collection 69 * name of the collection 70 * @param $filter 71 * filter on filename (defaults to none) 72 */ 73 function getMediaListByCollection($collection, $filter = '') { 74 global $DIR_MEDIA; 75 76 $filelist = array(); 77 78 // 1. go through all objects and add them to the filelist 79 80 $mediadir = $DIR_MEDIA . $collection . '/'; 81 82 // return if dir does not exist 83 if (!is_dir($mediadir)) return $filelist; 84 85 $dirhandle = opendir($mediadir); 86 while ($filename = readdir($dirhandle)) { 87 // only add files that match the filter 88 if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter)) 89 array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename))); 90 } 91 closedir($dirhandle); 92 93 // sort array so newer files are shown first 94 usort($filelist, 'sort_media'); 95 96 return $filelist; 97 } 98 99 function checkFilter($strText, $strFilter) { 100 if ($strFilter == '') 101 return 1; 102 else 103 return is_integer(strpos(strtolower($strText), strtolower($strFilter))); 104 } 105 106 /** 107 * checks if a collection exists with the given name, and if it's 108 * allowed for the currently logged in member to upload files to it 109 */ 110 function isValidCollection($collectionName, $exceptReadOnly = false) { 111 global $member, $DIR_MEDIA; 112 113 // allow creating new private directory 114 if ($collectionName === (string)$member->getID()) 115 return true; 116 117 $collections = MEDIA::getCollectionList($exceptReadOnly); 118 $dirname = $collections[$collectionName]; 119 if ($dirname == NULL || $dirname === PRIVATE_COLLECTION) 120 return false; 121 122 // other collections should exist and be writable 123 $collectionDir = $DIR_MEDIA . $collectionName; 124 if ($exceptReadOnly) 125 return (@is_dir($collectionDir) && @is_writable($collectionDir)); 126 127 // other collections should exist 128 return @is_dir($collectionDir); 129 } 130 131 /** 132 * Adds an uploaded file to the media archive 133 * 134 * @param collection 135 * collection 136 * @param uploadfile 137 * the postFileInfo(..) array 138 * @param filename 139 * the filename that should be used to save the file as 140 * (date prefix should be already added here) 141 */ 142 function addMediaObject($collection, $uploadfile, $filename) { 143 global $DIR_MEDIA, $manager; 144 145 $manager->notify('PreMediaUpload',array('collection' => &$collection, 'uploadfile' => $uploadfile, 'filename' => &$filename)); 146 147 // don't allow uploads to unknown or forbidden collections 148 $exceptReadOnly = true; 149 if (!MEDIA::isValidCollection($collection,$exceptReadOnly)) 150 return _ERROR_DISALLOWED; 151 152 // check dir permissions (try to create dir if it does not exist) 153 $mediadir = $DIR_MEDIA . $collection; 154 155 // try to create new private media directories if needed 156 if (!@is_dir($mediadir) && is_numeric($collection)) { 157 $oldumask = umask(0000); 158 if (!@mkdir($mediadir, 0777)) 159 return _ERROR_BADPERMISSIONS; 160 umask($oldumask); 161 } 162 163 // if dir still not exists, the action is disallowed 164 if (!@is_dir($mediadir)) 165 return _ERROR_DISALLOWED; 166 167 if (!is_writeable($mediadir)) 168 return _ERROR_BADPERMISSIONS; 169 170 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems) 171 $mediadir .= '/'; 172 173 if (file_exists($mediadir . $filename)) 174 return _ERROR_UPLOADDUPLICATE; 175 176 // move file to directory 177 if (is_uploaded_file($uploadfile)) { 178 if (!@move_uploaded_file($uploadfile, $mediadir . $filename)) 179 return _ERROR_UPLOADMOVEP; 180 } else { 181 if (!copy($uploadfile, $mediadir . $filename)) 182 return _ERROR_UPLOADCOPY ; 183 } 184 185 // chmod uploaded file 186 $oldumask = umask(0000); 187 @chmod($mediadir . $filename, 0644); 188 umask($oldumask); 189 190 $manager->notify('PostMediaUpload',array('collection' => $collection, 'mediadir' => $mediadir, 'filename' => $filename)); 191 192 return ''; 193 194 } 195 196 /** 197 * Adds an uploaded file to the media dir. 198 * 199 * @param $collection 200 * collection to use 201 * @param $filename 202 * the filename that should be used to save the file as 203 * (date prefix should be already added here) 204 * @param &$data 205 * File data (binary) 206 * 207 * NOTE: does not check if $collection is valid. 208 */ 209 function addMediaObjectRaw($collection, $filename, &$data) { 210 global $DIR_MEDIA; 211 212 // check dir permissions (try to create dir if it does not exist) 213 $mediadir = $DIR_MEDIA . $collection; 214 215 // try to create new private media directories if needed 216 if (!@is_dir($mediadir) && is_numeric($collection)) { 217 $oldumask = umask(0000); 218 if (!@mkdir($mediadir, 0777)) 219 return _ERROR_BADPERMISSIONS; 220 umask($oldumask); 221 } 222 223 // if dir still not exists, the action is disallowed 224 if (!@is_dir($mediadir)) 225 return _ERROR_DISALLOWED; 226 227 if (!is_writeable($mediadir)) 228 return _ERROR_BADPERMISSIONS; 229 230 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems) 231 $mediadir .= '/'; 232 233 if (file_exists($mediadir . $filename)) 234 return _ERROR_UPLOADDUPLICATE; 235 236 // create file 237 $fh = @fopen($mediadir . $filename, 'wb'); 238 if (!$fh) 239 return _ERROR_UPLOADFAILED; 240 $ok = @fwrite($fh, $data); 241 @fclose($fh); 242 if (!$ok) 243 return _ERROR_UPLOADFAILED; 244 245 // chmod uploaded file 246 $oldumask = umask(0000); 247 @chmod($mediadir . $filename, 0644); 248 umask($oldumask); 249 250 return ''; 251 252 } 253 254 } 255 256 /** 257 * Represents the characteristics of one single media-object 258 * 259 * Description of properties: 260 * - filename: filename, without paths 261 * - timestamp: last modification (unix timestamp) 262 * - collection: collection to which the file belongs (can also be a owner ID, for private collections) 263 * - private: true if the media belongs to a private member collection 264 */ 265 class MEDIAOBJECT { 266 267 var $private; 268 var $collection; 269 var $filename; 270 var $timestamp; 271 272 function MEDIAOBJECT($collection, $filename, $timestamp) { 273 $this->private = is_numeric($collection); 274 $this->collection = $collection; 275 $this->filename = $filename; 276 $this->timestamp = $timestamp; 277 } 278 279 } 280 281 /** 282 * User-defined sort method to sort an array of MEDIAOBJECTS 283 */ 284 function sort_media($a, $b) { 285 if ($a->timestamp == $b->timestamp) return 0; 286 return ($a->timestamp > $b->timestamp) ? -1 : 1; 287 } 288 289 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Aug 1 03:56:06 2010 |