Location: PHPKode > scripts > Audio_Playlist > audio_playlist/Audio/Playlist.php
<?php
/**
 * $Id: Playlist.php 1667 2007-05-23 04:21:49Z matthieu $
 */
if (!class_exists('Audio_Playlist')) {
    if (!defined('__CLASS_PATH__')) {
        define('__CLASS_PATH__', realpath(dirname(__FILE__) . '/../'));
    }
    require_once __CLASS_PATH__ . '/Autoload.php';
    require_once __CLASS_PATH__ . '/templates/template.inc';
    require_once __CLASS_PATH__ . '/Audio/class_audioFile_data.php';
    /**
     * this class is an easyWay to build Audio Playlist
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     * create the : july 9th 2003.
     * @author Matthieu MARY <hide@address.com>
     * @package playlist
     */
    class Audio_Playlist
    {
        /**
         * @$templateFolder
         */
        private static $__TEMPLATE_FOLDER__ = '/home/usage/';
        /**
         * @var array $_aLog log events
         * @access private
         * @deprecated
         */
        private $_aLog = array ();
        /**
         * @var array $_aErrors log errors
         * @access private
         * @deprecated
         */
        private $_aErrors = array ();
        /**
         * @var string $sBandname bandname of your playlist
         * @access private
         */
        private $sBandname = '';
        /**
        *  @var array playlist const
        *  @access private
        */
        private $__PLAYLIST_CONST__ = array (
            'b4s' => 'playlistb4s.tpl',
            'm3u' => 'playlistm3u.tpl',
            'pls' => 'playlistpls.tpl'
        );
        /**
         * @var string $_folder : the folder to analyse
         * @access private
         */
        private $_folder = '';
        /**
         * @var array $_expectedAudioExtensions : the expected extension list
         * @acces private
         */
        private $_expectedAudioExtensions = array ();
        /**
         * @var array $_filelist : the filelist to process
         * @access private
         */
        private $_filelist = array ();
        /**
         * Builder
         * @param string sPath. the path which have the file
         * @param string SBandname. the name of the band for playlist
         * @param string sSavepath. folder+filename for the playlist will be create
         * @param array aAudiotypes. the type of audio file you wants
         * @access public
         * @return void
         * @throws Audio_Playlist_Exception
         */
        public function __construct($sPath, $sBandname, $sSavepath = 'playlist.b4s', $aAudiotypes = array (
            'mp3'
        ))
        {
            //-------------------------------------------
            // check if path is ok
            //-------------------------------------------
            $this->_folder = str_replace("\\", '/', $sPath);
            $this->_expectedAudioExtensions = $aAudiotypes;
            //--------------------------------
            // get the file list for playlist
            //--------------------------------
            $dir = new Files_DirectoryListing($this->_folder);
            if ($dir->DATA_errors_size()) {
                foreach ($dir->DATA_errors() as $current_error) {
                    throw new Audio_Playlist_Exception($current_error, 1);
                }
                return;
            }
            $this->_filelist = $dir->LIST_get(false, $this->_expectedAudioExtensions);
            if ($dir->DATA_errors_size()) {
                foreach ($dir->DATA_errors() as $current_error) {
                    throw new Audio_Playlist_Exception($current_error, 1);
                }
                return;
            }
            sort($this->_filelist);
            $validator = new Validator_Playlist($this);
            if (!$validator->isValid()) {
                while ($error = $validator->getError()) {
                    throw new Audio_Playlist_Exception($error, 1);
                }
            }
            //----------------------------------------
            // check the playlist infos
            //----------------------------------------
            // get the album name
            $aSubdirs = explode('/', $this->_folder);
            $sAlbum = $aSubdirs[(count($aSubdirs) - 1)];
            // is the dir valid?
            $dPsave = pathinfo($sSavepath);
            $dPlaylist = pathinfo($sSavepath);
            $dPlaylist['path'] = $sSavepath;
            if (!in_array(strtolower($dPlaylist['extension']), array_keys($this->__PLAYLIST_CONST__))) {
                throw new Audio_Playlist_Exception('your extension of your playlist [' . $dPlaylist['extension'] . '] isn\'t recognize in the list ' . implode('-', array_keys($this->__PLAYLIST_CONST__)));
            }
            //-------------------------------------------
            // check if the template model really exists
            //-------------------------------------------
            $sTemplate = $this->__PLAYLIST_CONST__[strtolower($dPlaylist['extension'])];
            if (!file_exists(self :: $__TEMPLATE_FOLDER__ . $sTemplate)) {
                throw new Audio_Playlist_Exception("template [$sTemplate" . "] doesn't exist in current folder; exit", 1);
            }
            //----------------------------------------
            // check if playlist already exists
            //----------------------------------------
            if (file_exists($dPlaylist['path'])) {
                throw new Audio_Playlist_Exception('playlist [' . $dPlaylist['path'] . '] already exists; abort', 1);
            }
            //----------------------------------------
            // get the contents of the playlist
            //----------------------------------------
            // block output
            ob_start();
            $bOK = true;
            // create new objects template
            $t = new Template(self :: $__TEMPLATE_FOLDER__);
            // set template file
            $t->set_file('playlist', $sTemplate);
            // set var
            $t->set_block('playlist', 'FILE', 'one_file');
            $t->set_var('one_file', '');
            $t->set_var('numentries', count($this->_filelist));
            $t->set_var('label', htmlspecialchars($sAlbum));
            reset($this->_filelist);
            while (list ($id, $pathfile) = each($this->_filelist)) {
                $dpathinfos = pathinfo($pathfile);
                clearstatcache();
                $size = @ filesize($pathfile);
                $name = substr($dpathinfos['basename'], 0, ((strlen($dpathinfos['basename'])) - (strlen($dpathinfos['extension']) + 1)));
                if (!$size) {
                    $this->_log('filesize 0 for [' . $pathfile . ']; abord building for [' . $name . ']');
                    $bOK = false;
                    break;
                }
                $AF = new audiofile_data($pathfile);
                $t->set_var(array (
                    'path' => $pathfile,
                'length' => floor($AF->getLength()), 'num' => ($id +1), 'name' => $name, 'size' => $size));
                $t->parse('one_file', 'FILE', true);
                $AF->clear();
                unset ($AF);
            } //while
            //write output
            $t->pparse('contents', 'playlist');
            // get output
            $content = ob_get_contents();
            ob_end_clean();
            unset ($t);
            //----------------------------------------
            // make playlist file
            //----------------------------------------
            if (!$bOK) {
                return;
            }
            $fp = fopen($sSavepath, "w");
            if (!$fp) {
                return $this->_log('unable to create [' . $dPlaylist['path'] . ']', true);
            }
            fputs($fp, $content);
            fclose($fp);
            return;
        }
        /**
         * @return string
         * @access public
         */
        public function getFoldername()
        {
            return $this->_folder;
        }
        /**
         * @return array
         * @access public
         */
        public function getExpectedAudioFilesExtensions()
        {
            return $this->_expectedAudioExtensions;
        }
        /**
         * @return array
         * @acces public
         */
        public function getFilesForPlaylist()
        {
            return $this->_filelist;
        }
        /**
         * add a string in the log array
         * @param string sString required. the string to add
         * @param bool bToError optional. does the string must be add to error array?
         * @access private
         * @return boolean (false)
         * @deprecated
         */
        private function _log($sString, $bToError = false)
        {
            $this->_aLog[] = $sString;
            if ($bToError) {
                $this->_aErrors[] = $sString;
            }
            return false;
        }
        /**
         * Function that return the size of the error array
         * @access public
         * @return int
         * @deprecated
         */
        public function DATA_error_size()
        {
            return count($this->_aErrors);
        }
        /**
         * Function that the array errors
         * @access public
         * @return array
         * @deprecated
         */
        public function DATA_error()
        {
            return $this->_aErrors;
        }
        /**
         * Function returning the log
         * @access public
         * @return array
         * @deprecated
         */
        public function DATA_log()
        {
            return $this->_aLog;
        }
    }
}
?>
Return current item: Audio_Playlist