Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Samba in PHP

  1. #1
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts

    Samba in PHP

    So I wrote this tonight:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    class 
    playlistConfig {
        static public 
    $mediaLocation = array();
        static public 
    $mediaRegex = array();
        static public 
    $database '';
    }

    class 
    mediaID3 {
        private 
    $genres = array(
            
    // Source http://www.multimediasoft.com/amp3dj/help/amp3dj_00003e.htm
            
    'Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge',
            
    'Hip-Hop','Jazz','Metal','New Age','Oldies','Other','Pop','R&B',
            
    'Rap','Reggae','Rock','Techno','Industrial','Alternative','Ska',
            
    'Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient',
            
    'Trip-Hop','Vocal','Jazz+Funk','Fusion','Trance','Classical',
            
    'Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise',
            
    'Alternative Rock','Bass','Soul','Punk','Space','Meditative',
            
    'Instrumental Pop','Instrumental Rock','Ethnic','Gothic','Darkwave',
            
    'Techno-Industrial','Electronic','Pop-Folk','Eurodance','Dream',
            
    'Southern Rock','Comedy','Cult','Gangsta','Top 40','Christian Rap',
            
    'Pop/Funk','Jungle','Native US','Cabaret','New Wave','Psychadelic',
            
    'Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk',
            
    'Acid Jazz','Polka','Retro','Musical','Rock & Roll','Hard Rock',
            
    'Folk','Folk-Rock','National Folk','Swing','Fast Fusion','Bebob',
            
    'Latin','Revival','Celtic','Bluegrass','Avantgarde','Gothic Rock',
            
    'Progressive Rock','Psychedelic Rock','Symphonic Rock','Slow Rock',
            
    'Big Band','Chorus','Easy Listening','Acoustic','Humour','Speech',
            
    'Chanson','Opera','Chamber Music','Sonata','Symphony','Booty Bass',
            
    'Primus','Porn Groove','Satire','Slow Jam','Club','Tango','Samba',
            
    'Folklore','Ballad','Power Ballad','Rhythmic Soul','Freestyle',
            
    'Duet','Punk Rock','Drum Solo','Acapella','Euro-House','Dance Hall',
            
    'Goa','Drum & Bass','Club - House','Hardcore','Terror','Indie',
            
    'BritPop','Negerpunk','Polsk Punk','Beat','Christian Gangsta Rap',
            
    'Heavy Metal','Black Metal','Crossover','Contemporary Christian',
            
    'Christian Rock','Merengue','Salsa','Thrash Metal','Anime','JPop',
            
    'Synthpop' 
        
    );
        protected 
    $handle;
        protected 
    $readonly;
        public 
    $tag = array();
        
        function 
    __construct($filename$readonly False) {
            
    $this->readonly =  (!is_writable($filename) || $readonly);
            if (!(
    $handle = @fopen($filename, ($this->readonly 'rb' 'rb+'))))
                throw new 
    Exception('Unable to open '.$filename.' in '.($readonly 'read-only' 'read/write').' mode');
            
    $this->blankTag();
        }
        
        function 
    __destruct() {
            @
    fclose($handle);
        }
        
        protected function 
    blankTag() {
            
    $this->tag = array(
                
    'song' => '',
                
    'artist' => '',
                
    'album' => '',
                
    'year' => '',
                
    'comment' => '',
                
    'genre' => '',
                
    'track' => -1
            
    );
        }

        public function 
    get() {
            
    fseek($this->handle, -128SEEK_END);
            
    $tag = @fread($this->handle128);
            
    $verification substr($tag03);
            if (
    $verification == 'TAG') {
                
    $this->tag['song'] = trim(substr($tag330), " \0");
                
    $this->tag['artist'] = trim(substr($tag3330), " \0");
                
    $this->tag['album'] = trim(substr($tag6330), " \0");
                
    $this->tag['year'] = trim(substr($tag934), " \0");
                
    $this->tag['comment'] = trim(substr($tag9728), " \0");
                
    $track substr($tag1252);
                
    $genreid ord(substr($tag1271));
                
    $this->tag['genre'] = (isset($this->genres[$genreid]) ? $this->genres[$genreid] : '');
                if ((
    $track[0] == "\0") && ($track[1] != "\0")) {
                    
    $this->tag['track'] = ord($track[1]);
                } else 
                    
    $this->tag['comment'] .= trim($track" \0");
            }
            return 
    $this->tag;
        }
        
        public function 
    set($tag null) {
            if (
    $this->readonly)
                throw new 
    Exception('mediaID3::set is in read-only mode');
            
    $this->delete();
            if (!
    is_null($tag))
                
    $this->tag $tag;
            
    $data 'TAG'.
                
    str_pad(substr($this->tag['song'], 030), 30"\0").
                
    str_pad(substr($this->tag['artist'], 030), 30"\0").
                
    str_pad(substr($this->tag['album'], 030), 30"\0").
                
    str_pad(substr($this->tag['year'], 04), 4"\0");
            if ((
    $this->tag['track'] > 0) && ($this->tag['track'] <= 255)) {
                
    $data .= str_pad(substr($this->tag['comment'], 028), 28"\0")."\0".chr($this->tag['track']);
            } else {
                
    $data .= str_pad(substr($this->tag['comment'], 030), 30"\0");
            }
            
    $genreid array_search($this->tag['genre'], $this->genres);
            
    $data .= ($genreid chr($genreid) : chr(255));
            
    fseek($this->handle0SEEK_END);
            
    fwrite($this->handle$data128);
        }
        
        public function 
    delete() {
            if (
    $this->readonly)
                throw new 
    Exception('mediaID3::delete is in read-only mode');
            
    $this->blankTag();
            
    fseek($this->handle, -128SEEK_END);
            
    $tag = @fread($this->handle128);
            
    $verification substr($tag03);
            if (
    $verification == 'TAG') {
                
    fseek($this->handle, -128SEEK_END);
                
    ftruncate($this->handleftell($handle));
            }
        }

    }




    class 
    playlistDatabase {
        protected 
    $db;
        
        function 
    __construct() {
            
    $this->db maiDatabase::open(playlistConfig::$database);
        }
        
        function 
    __destruct() {
            unset(
    $this->db);
        }
        
        private function 
    addMedia($folder$file) {
            
        }
        
        private function 
    mediaScan($folder) {
            
    $handle opendir($folder);
            echo(
    $folder."\n");
            while (
    $file readdir($handle))
                if ((
    $file != '.') && ($file != '..')) {
                    if (
    is_file($file))
                        
    $this->addMedia($folder$file);
                    if (
    is_dir($file))
                        
    $this->mediaScan($folder.DIRECTORY_SEPARATOR.$file);
                }
        }

        public function 
    mediaRescan() {
            foreach(
    playlistConfig::$mediaLocation as $location)
                
    $this->mediaScan(rtrim($location'\/'));
        }
        
        public function 
    removeOrphans() {
            foreach(
    $this->getAllMedia() as $media)
                if (!
    file_exists($media['folder'].DIRECTORY_SEPARATOR.$media['file']))
                    
    $db('DELETE FROM `media` WHERE `mediaid` = %s'$media['mediaid']);
            
    $db('DELETE FROM `locations` WHERE (SELECT COUNT(*) FROM `media` WHERE `media`.`locationid` = `locations`.`locationid`) = 0');
            
    $db('DELETE FROM `albums` WHERE (SELECT COUNT(*) FROM `media` WHERE `media`.`albumid` = `albums`.`albumid`) = 0');
            
    $db('DELETE FROM `artists` WHERE (SELECT COUNT(*) FROM `albums` WHERE `albums`.`artistid` = `artists`.`artistid`) = 0');
        }
        
        public function 
    getArtistByAlbum($id) {
            
    // Returns the artist names for a album name or id
            
    if (is_numeric($id)) {
                return 
    $this->db('SELECT `artist`.`artistname` FROM `albums` WHERE `albumid` = %s'$id);
            } else {
                return 
    $this->db('SELECT `artist`.`artistname` FROM `albums` WHERE `artistname` LIKE %s'$id);
            }
        }
        
        public function 
    getLocationId($uri) {
            
    // Returns a location id, either from an existing location, or from a newly inserted one
            
    $uri rtrim($uri'/');
            if (
    $row $this->db($this->db('SELECT `locationid` FROM `locations` WHERE `folder` LIKE %s'$uri))) {
                return 
    $row['locationid'];
            } else {
                return 
    $this->db('INSERT INTO `locations` (`folder`) VALUES (%s)'$uri);
            }
        }
        
        public function 
    getFullURIByMediaId($id) {
            
    $row $this->db($this->db('SELECT `media`.`file`, `locations`.`folder` FROM `media` LEFT JOIN `locations` ON `locations`.`locationid` = `media`.`locationid` WHERE `media`.`mediaid` = %s'$id));
            return 
    $row['folder'].DIRECTORY_SEPARATOR.$row['file'];
        }

        public function 
    searchMediaBySong($search) {
            return 
    $this->db('SELECT * FROM `media` WHERE `song` LIKE %s''%'.$search.'%');
        }

        public function 
    searchAlbums($search) {
            return 
    $this->db('SELECT * FROM `albums` WHERE `albumname` LIKE %s''%'.$search.'%');
        }

        public function 
    searchArtists($search) {
            return 
    $this->db('SELECT * FROM `artists` WHERE `artistname` LIKE %s''%'.$search.'%');
        }
        
        public function 
    getAllMedia() {
            return 
    $this->db('SELECT * FROM `media` LEFT JOIN `locations` ON `locations`.`locationid` = `media`.`locationid` LEFT JOIN `albums` ON `albums`.`albumid` = `media`.`albumid` LEFT JOIN `artists` ON `artists`.artistid` = `albums`.`artistid`');
        }
    }

    $mai_modules = array('mai_database');
    require_once(
    '/home/computerx/Aptana Studio Workspace/E-Commerce/mai_api/mai_bootstrap.php');
    playlistConfig::$mediaLocation[] = 'smb://kotchanski-pc/bulk/';
    playlistConfig::$mediaRegex[] = '/(?<artist>[^-]*)-(?<album>[^-]*)-(?<track>\s*[0-9]*\s*)?-(?<song>.*?)\.mp3/i';
    playlistConfig::$database 'mysql://computerx:password@localhost/playlist';

    if (isset(
    $_GET['rescan']) || True) {
        echo(
    'Rescan in progress. You may continue to use the server.');
        
    $playlist = new playlistDatabase();
        
    $playlist->mediaRescan();
        die();
    }
    ?>
    <html>
        <head>
            <title>Maiestas Playlist Server</title>
            <style type="text/css">
                body { margin: 0; padding: 0; font-family: Calibri, Tahoma, Loma, Arial, Helvetica; height: 100%; width: 100%; color: #000; font-size: 1em; }
                #mai_searchbar { position: relative; width: 100%; background-color: #7975b2; color: #fff; border-bottom: 1px Solid #777; }
                #mai_optionsbar { position: absolute; right: 4px; top: 4px; font-size: 0.75em; }
                #mai_optionsbar a { color: white; font-weight: bold; }
                h1 { font-size: 1.2em; font-style: normal; font-weight: bold; }
                h2 { font-size: 1em; font-style: normal; font-weight: bold; }
                p { font-size: 0.75em; }
            </style>
        </head>
        <body>
            <div id="mai_searchbar">
                Search: <input type="text" name="search" /><input type="submit" value="Go..." />
                <div id="mai_optionsbar">
                    <a href="?rescan" target="_blank">Find New Media</a> | <a href="?clean">Clean Database</a>
                </div>
            </div>
            <?php
            
    if ($_GET['search']) {
                
    $playlist = new playlistDatabase();
                echo(
    '<h2>Songs:</h2>');
                foreach(
    $playlist->searchMediaBySong($_GET['SEARCH']) as $song) {
                    echo(
    '<p>'.$song['song'].'</p>');
                }
            }
            
    ?>
            <p>&copy; <a href="http://www.maiestas.net/">Maiestas</a> Internet Services 2009</p>
        </body>
    </html>
    I know, shoddy code. Awful. The point is: PHP thinks the "smb://" network access is some kind of URL wrapper.

    How do I access a samba share this way?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Well I got a fix in place with:

    Code:
    sudo mount -t smbfs -o username=Kotchanski //192.168.10.4/bulk /mnt/bulk
    But I'd still like to access it directly without disabling url wrappers
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •