Location: PHPKode > projects > mediaIndex > classes/class.dir.php
<?php
/**
 * $Id: class.dir.php,v 1.49 2004/12/02 04:17:31 openface Exp $      
 *
 *               _ _        _         _                 
 *  _ __  ___ __| (_)__ _  (_)_ _  __| |_____ _____ _ _ 
 * | '  \/ -_) _` | / _` | | | ' \/ _` / -_) \ / -_) '_|
 * |_|_|_\___\__,_|_\__,_| |_|_||_\__,_\___/_\_\___|_|  
 *                                                     
 *     Standalone Indexer Script for Media Files
 *        jason hines, <hide@address.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
 * General Public License for more details.
 */

/**
 * Zip dir (tm)
 */
define("_ZIP",        "/usr/bin/zip");
define("_ZIPDIRPATH", _CACHEPATH."/zipdir");

/**
 * Directory images
 */
define("_DIRIMAGEPATH",_CACHEPATH."/dirimages");
define("_DIRIMAGEURL",_CACHEURL."/dirimages");

define("_DIRTHUMBPATH",_DIRIMAGEPATH."/thumbs");
define("_DIRTHUMBURL",_DIRIMAGEURL."/thumbs");

/**
 * Directory class
 *
 * @version   $Revision: 1.49 $
 * @package   mediaIndexer
 * @author    jason hines, <hide@address.com>
 */
class dir {

    public $title = "";
    public $path  = "";
    public $desc  = "";
    public $filemtime = "";
    public $filectime = "";
    public $_filemtime = 0;
    public $_filectime = 0;
    public $image = "";
    public $thumbimage = "";
    protected static $_resultstack = array();

    /**
     * Constructor sets primary key property and loads object
     */
    function dir($path) {
        if (substr($path,strlen($path)-1,1) != "/") $path .= "/";
        $this->path = $path;
        $this->_filemtime = filemtime(_MEDIAPATH.$path);
        $this->_filectime = filectime(_MEDIAPATH.$path);
        $this->filemtime = date(_DATEFORMAT,$this->_filemtime);
        $this->filectime = date(_DATEFORMAT,$this->_filectime);
        $this->loadMetadata($path);
    }

    /**
     * Loads metadata and assigns it to object
     */
    protected function loadMetadata($path,$force=false) {
        static $metadata = array();
        if (empty($metadata) || $force) {
          // get ALL directory metadata, store it statically
          $sql = "SELECT * FROM dirs WHERE desc != '' OR title != ''";
          $result = sqlite_array_query($GLOBALS['gDb'],$sql,SQLITE_ASSOC);
          foreach ($result as $A) {
            $metadata[$A['path']] = array(
                'title' => $A['title'],
                'desc' => $A['desc'],
                'image' => $A['image'],
            );
          }
        }

        if (isset($metadata[$path])) {
            $this->title = stripslashes($metadata[$path]['title']);
            $this->desc = stripslashes($metadata[$path]['desc']);
            $this->image = $metadata[$path]['image'];
            $this->thumbimage = $this->getThumbImage();
        } else {
            $this->title = ucwords(basename($path));
            $this->desc = "";
            $this->image = "";
            $this->thumbimage = "";
        }
    }

    /**
     * Generate a thumbnail for directory image, or false on error
     */
    protected function getThumbImage() {
        if (empty($this->image)) return "";
        // generate thumbnail for directory image
        $thumbimage = md5($this->path)."."._getExtension($this->image);
        if (!is_file(_DIRTHUMBPATH ."/". $thumbimage)) {
            if (!is_writable(_DIRTHUMBPATH)) mkdir(_DIRTHUMBPATH);

            // thumbnail image doesn't exist, generate it
            _addMessage("Generating thumbnail image for {$this->image} ...");
            $cmd = _CONVERT ." -geometry "._THUMBSIZE." "._DIRIMAGEPATH."/".escapeshellarg($this->image)." "._DIRTHUMBPATH."/{$thumbimage}";
            exec($cmd,$out,$ret);
            if ($ret != 0) {
              _addMessage("Unable to generate thumbnail image for {$this->image}.");
              return "";
            }
        }
        return $thumbimage;
    }

    /**
     * Return array of all sub-directories of this directory
     */
    public function getSubDirs() {
        $dir = scandir(_MEDIAPATH . $this->path);
        $dirs = array();
        foreach ($dir as $file) {
            if (is_dir(_MEDIAPATH . $this->path . '/'. $file)
                    && $file!="." && $file!=".."
                    && !_isExcluded($file)) {
                $dirs[] = new dir($this->path.$file);
            }
        }
        usort($dirs, "_key_compare");
        return $dirs;
    }

    /**
     * Return array of all files in this directory
     */
    public function getFiles() {
        $dir = scandir(_MEDIAPATH . $this->path);
        $files = array();
        foreach ($dir as $file) {
            if (is_file(_MEDIAPATH . $this->path . '/' . $file)
                    && $file!="." && $file!=".."
                    && !_isExcluded($file)) {
                $files[] = new file($this->path.$file);
            }
        }
        return $files;
    }

    /**
     * Search this directory for a given description match
     */
    public function searchDescriptions($match) {
        $results = array();
        $this->_resultstack = array();
        $this->_scanTree($this->path);
        foreach ($this->_resultstack as $path=>$file) {
            if (stripos($file->desc,$match)!==FALSE) {
                $results[] = $file;
            }
        }
        return $results;
    }

    /**
     * Search this directory for a given filename match
     */
    public function searchFilenames($match) {
        $results = array();
        $this->_resultstack = array();
        $this->_scanTree($this->path);
        foreach ($this->_resultstack as $path=>$file) {
            if (fnmatch(strtolower("*".$match."*"),strtolower($file->title))) {
                $results[] = $file;
            }
        }
        return $results;
    }

    /**
     * Print newest files
     */
    public function getNewestFiles() {
        $max = 10;
        $results = array();
        $this->_resultstack = array();
        $this->_scanTree($this->path);
        $compare = create_function('$a,$b','if ($a->_filemtime == $b->_filemtime) {return 0;}else {return ($a->_filemtime > $b->_filemtime) ? -1 : 1;}');
        usort($this->_resultstack, $compare);
        return array_splice($this->_resultstack,0,$max);
    }

    /**
     * Recusively scan this directory, construct static array
     */
    protected function _scanTree($path) {
        $dir = scandir(_MEDIAPATH.$path);
        foreach ($dir as $file) {
            if ($file=='.' || $file=='..' || _isExcluded($file))
                continue;

            $fullpath = $path!="/" ? $path.'/'.$file : $path.$file;
            if (is_dir(_MEDIAPATH . $fullpath)) {
                //$this->_resultstack[$fullpath.'/'] = new dir($fullpath);
                $this->_scanTree($fullpath);
            } else {
                $this->_resultstack[$fullpath] = new file($fullpath);
            }
        }
    }

    /**
     * Saves object metadata to database
     */
    public function saveMetadata() {
        $sql = "REPLACE INTO dirs (path,title,desc,image) ";
        $sql .= "VALUES ('"._escape($this->path)."','"._escape($this->title)."','"._escape($this->desc)."','"._escape($this->image)."')";
        sqlite_query($GLOBALS['gDb'],$sql);
        _addMessage("Changes to directory saved.");

        // reload
        $this->loadMetadata($this->path,true);
    }

}
?>
Return current item: mediaIndex