Renich Posted May 10, 2008 Share Posted May 10, 2008 Hello, my PHP Freaks amigos! I've made a jukebox (rockola) function to add a non-flash jukebox to my Creative Commons band... Anyway, the function works fine, so far, to me and I want to share it and see if anybody can improve it, since I'm not a good programmer. Should I post it too sourceforge or something? Nah, probably isn't worth it... anyway! Here it goes. <?php function rockola( $path, $rpath = null, $song = null, $desc = null ) { function audio_object_build ( $song ) { // define type if ( substr( $song, -3 ) == 'ogg' ) { $type = 'application/ogg'; } else { $type = 'audio/mpeg'; } // define object $object = <<< EOF <object id="rockola_player" data="$song" type="$type" width="400" height="120"> <param name="autostart" value="true"> Lo siento, no puedes reproducir los formatos mp3 u ogg. Consíguete el quicktime o algo. </object> EOF; // return data return $object; } function list_build( $file_list ) { $list = null; foreach( $file_list as $file => $path ) { if ( is_array( $path ) ) { $list .= "<optgroup label=\"$file\">\n"; $list .= list_build( $path ) . "\n"; $list .= "</optgroup>\n"; } else { if ( isset($_POST['song']) && $_POST['song'] == $path ) { $list .= <<< EOF <option id="song_$file" label="$file" value="$path" selected="selected"> $file </option> EOF; } else { $list .= <<< EOF <option id="song_$file" label="$file" value="$path"> $file </option> EOF; } } } return $list; } function list_get( $path, $lastdir = null ) { // scan $path if ( $files = scandir( $path ) ) { foreach( $files as $id => $filename ) { switch( $filename ) { // adios . case '.': unset( $files[$id] ); break; // adios .. case '..': unset( $files[$id] ); break; // if is dir, do it again case is_dir( "$path/$filename" ): if ( $lastdir !== null ) { // set name to directory at key and put the filelist inside it $files["$lastdir/$filename"] = list_get( "$path/$filename", $filename ); unset( $files[$id] ); } else { $files["$filename"] = list_get( "$path/$filename", $filename ); unset( $files[$id] ); } break; default: $files[$filename] = "$path/$filename"; unset( $files[$id] ); break; } } // return data return $files; } else { // error message die( "El directorio que proveiste no existe. {$_SERVER['SCRIPT_FILENAME']}" ); } } function reorg( $a, $b='' ) { $out = array(); foreach( $a as $key => $value ) { if ( !$b ) { foreach( $value as $k1 => $v1 ) { if ( !is_array( $v1 ) ) { $out[$key][$k1] = $v1; } else { $tmp = reorg( $v1, $k1 ); foreach ($tmp as $k2 => $v2) { $out[$k2] = $v2; } } } } else { if ( !is_array( $value ) ) { $out[$b][$key] = $value; } else { $tmp = reorg($value, $key); foreach ( $tmp as $k2 => $v2 ) { $out[$k2] = $v2; } } } } return $out; } function reorg_rpath( $list, $path, $rpath ) { if( is_array( $list ) ) { foreach ($list as $key => $value) { if ( is_array( $value ) ) { $list[$key] = reorg_rpath( $value, $path, $rpath ); } else { $list[$key] = str_replace( $path, $rpath, $value ); } } } return $list; } // get file list $list = list_get( $path ); // reorganize $list = reorg( $list ); // check if remote path declared if ( $rpath !== null ) { $list = reorg_rpath( $list, $path, $rpath ); } // build the audio object $output = audio_object_build( $song ); // define form $output .= '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">' . "\n"; // start select $output .= "\n\n" . '<select id="rockola_selector" name="song">' . "\n"; // Set description and some whitespace if ( $desc !== null ) { $output .= "\t" . '<option id="id_' . rand() . '" value="none" selected="selected">' . "\n"; $output .= "\t\t$desc\n"; $output .= "\t</option>\n"; $output .= "\t" . '<option id="id_' . rand() . '" value="none" disabled="disabled">' . "\n"; $output .= "\t</option>\n"; } // build a list $output .= list_build( $list ); // close select $output .= '</select>' . "\n\n"; // add buttons $output .= '<input type="submit" name="do" value="Play" />' . "\n"; $output .= '<input type="submit" name="do" value="Stop" />' . "\n"; // close form $output .= '</form>' . "\n"; return $output; } echo rockola( '../../downloads.woralelandia.com/audio/mp3', 'http://downloads.woralelandia.com/audio/mp3' ); ?> Link to comment https://forums.phpfreaks.com/topic/104981-jukebox/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.