[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> MEDIA.php (source)

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


Generated: Tue Feb 12 15:34:36 2008 Cross-referenced by PHPXref 0.7