#!/usr/bin/php * * version: 0.1.1 * * date: January 15, 2006 * * requirements: PHP v4, CDir.php, XMMS, XMMS Pipe Control Plugin with the * * inpipe file in ~/.xmms/inpipe * ******************************************************************************** * xmms_add_mp3_recursive.php is a simple PHP script that adds all mp3 files in * * the current directory and all subdirectories to the playlist of XMMS by use * * of the XMMS Pipe Control Plugin. Empties the playlist before filling by * * default, unless when using the option -a or --append. For use in a shell. * *******************************************************************************/ // The path to the inpipe file. If your inpipe file is somewhere else, change it // here. define(XMMS_INPIPE, "~/.xmms/inpipe"); define(SCRIPT_NAME, "xmms_add_mp3_recursive.php"); define(SCRIPT_VERSION, "0.1.1"); define(SCRIPT_AUTHOR, "Richard Vrijhof "); define( SCRIPT_SYNTAX, "Syntax: " . basename($_SERVER[argv][0]) . " [-a|--append] path\n" ); // Exit codes. All fatal errors (but ERR_NO_MP3 is at the end anyway, because // the script has done its job already; there were just no MP3 files to add. So, // technically that's not an error, but at least you can check for this exit // code. define(ERR_SYNTAX, 1); define(ERR_PATH_NOT_EXISTS, 2); define(ERR_PATH_NOT_DIR, 3); define(ERR_XMMS_INPIPE_NOT_FIFO, 4); define(ERR_NO_MP3, 5); require_once("cdir.php"); $dir = new CDir(); echo SCRIPT_NAME . " v" . SCRIPT_VERSION . " by " . SCRIPT_AUTHOR . "\n"; if ($_SERVER[argc] != 2 && $_SERVER[argc] != 3) { echo SCRIPT_SYNTAX; exit(ERR_SYNTAX); } if ($_SERVER[argc] == 2) { $append = false; $path = $_SERVER[argv][1]; } elseif ($_SERVER[argv][1] == "-a" || $_SERVER[argv][1] == "--append") { $append = true; $path = $_SERVER[argv][2]; } else { echo SCRIPT_SYNTAX; exit(ERR_SYNTAX); } if (! file_exists($path)) { echo "Fatal error: \"$path\" does not exist!\n"; exit(ERR_PATH_NOT_EXISTS); } if (! is_dir($path)) { echo "Fatal error: \"$path\" is not a directory!\n"; exit(ERR_PATH_NOT_DIR); } // On my system the filetype() function returns "", so I use this workaround. $output = shell_exec("stat -L " . XMMS_INPIPE . " | grep prwx"); $filetype = ereg("Access: \([0-9]{4}/p[rwx-]{9}\)", $output) ? "fifo" : "?"; if ($filetype != "fifo") { echo "Fatal error: inpipe file is not a FIFO file (is the Pipe Control "; echo "Plugin turned\non in XMMS?)!\n"; exit(ERR_XMMS_INPIPE_NOT_FIFO); } // CDir Read method needs to have a path ending with forward slash. Make sure. if (substr($path, -1, 1) != "/") { $path .= "/"; } // Get all MP3 files in the current dir and subdirectories, disable warnings. @$dir->Read($path, "\.[Mm][Pp]3$", true, true, false, "", ""); if ($dir->Count() > 0) { $dir->Sort("Fullname", true); if (! $append) { shell_exec("echo playlist clear > " . XMMS_INPIPE); } foreach ($dir->aFiles as $var) { shell_exec( "echo playlist add " . escapeshellarg(realpath($path) . "/" . $dir->Fullname($var)) . " > " . XMMS_INPIPE ); } } else { echo "No MP3 files found at all! Nothing to do.\n"; exit(ERR_NO_MP3); } ?>