Location: PHPKode > projects > mediaIndex > classes/class.file.php
<?php
/**
 * $Id: class.file.php,v 1.40 2004/12/02 05:12:18 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.
 */

/**
 * File class
 *
 * @version     $Revision: 1.40 $
 * @package     mediaIndexer
 * @author      jason hines, <hide@address.com>
 */

class file {

    public $title       = "";
    public $path        = "";
    public $desc        = "";
    public $filemtime   = "";
    public $filectime   = "";
    public $fileatime   = "";
    public $_filemtime   = 0;
    public $_filectime   = 0;
    public $_fileatime   = 0;
    public $fileowner   = "";
    public $fileperms   = "";
    public $hash        = "";
    public $filesize    = 0;
    public $hits        = 0;
    public $handlerRef  = null;
    public $groupRef    = null;
    protected $handlerName = null;
    protected $rawInfo  = null;

    /**
     * Constructor sets primary key property and loads object
     */
    function file($path) {
        $this->path = $path;
        $this->filename = basename($path);
        $this->filesize = _filesize(filesize(_MEDIAPATH.$path));
        $this->hash = $this->getHash(_MEDIAPATH.$path);
        $this->_filemtime = filemtime(_MEDIAPATH.$path);
        $this->_filectime = filectime(_MEDIAPATH.$path);
        $this->_fileatime = fileatime(_MEDIAPATH.$path);
        $this->filemtime = date(_DATEFORMAT,$this->_filemtime);
        $this->filectime = date(_DATEFORMAT,$this->_filectime);
        $this->fileatime = date(_DATEFORMAT,$this->_fileatime);
        $this->fileowner = fileowner(_MEDIAPATH.$path);
        $this->fileperms = $this->getFilePerms();
        $this->handlerName = $this->getHandlerName();
        $this->handlerRef = $this->getHandlerRef();
        $this->groupRef = $this->getGroupRef();
        $this->loadMetadata($this->hash);
    }

    /**
     * Loads metadata and assigns it to object
     */
    protected function loadMetadata($hash) {
        static $metadata = array();
        if (empty($_filemetadata)) {
            // get file metadata, index it
            $sql = "SELECT * FROM files WHERE desc != '' OR title != ''";
            $result = sqlite_array_query($GLOBALS['gDb'],$sql,SQLITE_ASSOC);
            foreach ($result as $A) {
                $metadata[$A['hash']] = array(
                    'title' => $A['title'],
                    'desc' => $A['desc'],
                    'hits' => $A['hits'],
                );
            }
        }

        if (isset($metadata[$hash])) {
            $this->title = !empty($metadata[$hash]['title']) ? $metadata[$hash]['title'] : basename($this->path);
            $this->desc = $metadata[$hash]['desc'];
            $this->hits = $metadata[$hash]['hits'];
        } else {
            $this->title = basename($this->path);
            $this->desc = "";
            $this->hits = 0;
        }
    }

    /**
     * Retrieve file handler reference
     */
    protected function getHandlerRef() {
        if (is_null($this->handlerRef)) {
            if (!@include_once(_BASEPATH . "/classes/handler.".$this->handlerName.".php")) {
                throw new Exception("Handler type {$this->handlerName} has no class file.");
            }
            $classname = $this->handlerName . "Handler";
            $this->handlerRef = new $classname;
        }
        return $this->handlerRef;
    }

    /**
     * Retreive file handler name from $_PATTERNS
     */
    protected function getHandlerName() {
        if (is_null($this->handlerName)) {
            $this->handlerName = _HANDLER_DEFAULT;
            foreach ($GLOBALS['_PATTERNS'] as $_pattern=>$_handler) {
                if (fnmatch(strtolower($_pattern),strtolower($this->filename))) {
                    $this->handlerName = $_handler;
                }
            }
        }
        return $this->handlerName;
    }

    /**
     * Retreives group class reference for a given file (from $_MAPPING)
     */
    function getGroupRef() {
        static $groups = array();
        if (!isset($groups[$this->handlerName])) {
            if (!$_groupName = $GLOBALS['_MAPPING'][$this->handlerName]) {
                throw new Exception("File handler '{$this->handlerName}' is not associated to a media group.");
                return false;
            }
            include_once(_BASEPATH . "/classes/group.{$_groupName}.php");
            $classname = $_groupName . "Group";
            $groups[$this->handlerName] = new $classname;
        }
        return $groups[$this->handlerName];
    }

    /**
     * Save object's metadata to database
     */
    public function saveMetadata() {
        // update metadata
        $sql = "REPLACE INTO files (hash,title,desc) ";
        $sql .= "VALUES ('{$this->hash}',";
        $sql .= "'"._escape($this->title)."',";
        $sql .= "'"._escape($this->desc)."')";
        sqlite_query($GLOBALS['gDb'],$sql);
        _addMessage("Changes to file saved.");
    }

    /**
     * Returns a md5 checksum on the first 1024 bytes of a file
     */
    protected function getHash($fn) {
        $handle = fopen($fn, "r");
        $hash = md5(fread($handle, 1024));
        fclose($handle);
        return $hash;
    }

    /**
     * Returns array of raw media info from GetID3 library
     */
    public function getRawInfo() {
        if (is_null($this->rawInfo)) {
            include_once(_BASEPATH . '/lib/getid3/getid3.php');
            try {
                $getID3 = new getID3;
                $this->rawInfo = $getID3->analyze(_MEDIAPATH . $this->path);
            } catch (Exception $e) {
                $this->rawInfo = array();
            }
        }
        return $this->rawInfo;
    }

    /**
     * Returns a UNIX-like permission string from a file path
     */
    function getFilePerms() {
        $perms = fileperms(_MEDIAPATH.$this->path);
        if (($perms & 0xC000) == 0xC000) { // Socket
            $info = 's';
        } elseif (($perms & 0xA000) == 0xA000) { // Symbolic Link
            $info = 'l';
        } elseif (($perms & 0x8000) == 0x8000) { // Regular
            $info = '-';
        } elseif (($perms & 0x6000) == 0x6000) { // Block special
            $info = 'b';
        } elseif (($perms & 0x4000) == 0x4000) { // Directory
            $info = 'd';
        } elseif (($perms & 0x2000) == 0x2000) { // Character special
            $info = 'c';
        } elseif (($perms & 0x1000) == 0x1000) { // FIFO pipe
            $info = 'p';
        } else { // Unknown
            $info = 'u';
        }
        // Owner
        $info .= (($perms & 0x0100) ? 'r' : '-');
        $info .= (($perms & 0x0080) ? 'w' : '-');
        $info .= (($perms & 0x0040) ?
           (($perms & 0x0800) ? 's' : 'x' ) :
           (($perms & 0x0800) ? 'S' : '-'));
        // Group
        $info .= (($perms & 0x0020) ? 'r' : '-');
        $info .= (($perms & 0x0010) ? 'w' : '-');
        $info .= (($perms & 0x0008) ?
           (($perms & 0x0400) ? 's' : 'x' ) :
           (($perms & 0x0400) ? 'S' : '-'));
        // World
        $info .= (($perms & 0x0004) ? 'r' : '-');
        $info .= (($perms & 0x0002) ? 'w' : '-');
        $info .= (($perms & 0x0001) ?
           (($perms & 0x0200) ? 't' : 'x' ) :
           (($perms & 0x0200) ? 'T' : '-'));
        return $info;
    }

    /**
     * Returns an array of mirroring URLs for this file.
     */
    public function getMirrors() {
        $sql = "SELECT * FROM mirrors WHERE hash = '{$this->hash}'";
        return sqlite_array_query($GLOBALS['gDb'],$sql,SQLITE_ASSOC);
    }

    /**
     * Increments the hit tracker for this file by 1.
     */
    public function bumpHit() {
        $sql = "UPDATE files SET hits = hits+1 WHERE hash = '{$GLOBALS['file']->hash}'";
        sqlite_query($GLOBALS['gDb'],$sql);
    }
}

?>
Return current item: mediaIndex