00001 <?php
00028 function pic_dir()
00029 {
00030 return skin_url.'img/music/';
00031 }
00032
00033 function GarbageCollector()
00034 {
00035
00036 if (0 == mt_rand(0, 30))
00037 {
00038 $query = 'DELETE FROM music_playlists '.
00039 "WHERE playlist_name='".mysql_real_escape_string('MythWeb Temporary Playlist')."'".
00040 ' AND (NOW() - last_accessed) > ('.MYTH_PLAYLIST_SAVE_TIME.');';
00041 mysql_query($query);
00042 }
00043 }
00044
00045 function getplaylistnames()
00046 {
00047 $output='';
00048 $query = 'SELECT playlist_name, hostname FROM music_playlists WHERE hostname=\'\';';
00049 $result = mysql_query($query);
00050
00051 if (!$result)
00052 return '';
00053
00054 while ($row = mysql_fetch_array($result))
00055 {
00056 $output .= '<option>'.$row['playlist_name'].'</option>';
00057 }
00058 mysql_free_result($result);
00059 return $output;
00060 }
00061
00062 function genreform()
00063 {
00064 $query = "SELECT genre FROM music_genres ORDER BY genre";
00065 $result = mysql_query($query);
00066
00067 if (!$result)
00068 return '';
00069
00070 $output = '<select id="genre" name="genre" onchange="updateBox(\'genre\',this.options[selectedIndex].value); return false;">
00071 <option value="" selected>'.t('Choose Genre..').'</option>';
00072
00073 while ($genre = mysql_fetch_array($result))
00074 {
00075 $output .= '<option value="'.$genre['genre'].'">'.$genre['genre'].'</option>';
00076 }
00077 $output .= '</select>';
00078 mysql_free_result($result);
00079 return $output;
00080 }
00081
00082 function letters()
00083 {
00084 $output = '<ul class="music" id="letters">';
00085 $letters = array('#','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
00086
00087 foreach($letters as $letter)
00088 {
00089 $output .= '<li><a class=music href="#" onclick="updateBox(\'letter\',\''.$letter.'\'); return false;">'.strtoupper($letter).'</a></li>';
00090 }
00091 $output .= '</ul>';
00092 return $output;
00093 }
00094
00095 function getDropDown($type, $id)
00096 {
00097 return "";
00098 }
00099
00100 function buildBreadcrumb($page, $parent, $parentitem, $child, $childitem)
00101 {
00102 $childoutput='';
00103 $parentoutput ='';
00104 if ($page == 'browse' && $child != '')
00105 {
00106 $output = '<a class="music" href="#" onclick="updateBox(\'browse\',0); return false;">'.t('Browse').'</a> » ';
00107 }
00108 switch ($child)
00109 {
00110 case 'album':
00111 $query = 'SELECT music_albums.album_name, music_artists.artist_name, music_artists.artist_id '.
00112 'FROM music_albums '.
00113 'LEFT JOIN music_artists ON music_albums.artist_id=music_artists.artist_id '.
00114 'WHERE music_albums.album_id='.mysql_real_escape_string($childitem);
00115 $result = mysql_query($query);
00116 if (!$result)
00117 break;
00118
00119 $row = mysql_fetch_array($result);
00120 mysql_free_result($result);
00121
00122 $query = 'SELECT album_name, album_id '.
00123 'FROM music_albums '.
00124 'WHERE artist_id='.$row['artist_id'].' '.
00125 'ORDER BY album_name';
00126 $result = mysql_query($query);
00127 if (!$result)
00128 break;
00129
00130 $albums = '';
00131 while ($row2 = mysql_fetch_array($result))
00132 {
00133 $albums .= '<li><a class="music" href="#"'.
00134 ' onclick="updateBox(\'album\','.$row2['album_id'].'); return false;"'.
00135 ' title="'.sprintf(t('View Details of %s'), $row2['album_name']).'">'.
00136 $row2['album_name'].'</a></li>';
00137 }
00138 mysql_free_result($result);
00139
00140 $childoutput .= '<span><a class="music" href="#"'.
00141 ' onclick="updateBox(\'artist\','.$row['artist_id'].'); return false;">'.
00142 $row['artist_name'].'</a>'.
00143 '<ul class="music">'.$albums.'</ul></span> » '.
00144 html_entities($row['album_name']);
00145 break;
00146
00147 case 'artist':
00148 $query = 'SELECT artist_name '.
00149 'FROM music_artists '.
00150 'WHERE artist_id='.mysql_real_escape_string($childitem);
00151 $result = mysql_query($query);
00152 if (!$result)
00153 break;
00154 $row = mysql_fetch_array($result);
00155 mysql_free_result($result);
00156
00157 $query = 'SELECT music_albums.album_id, album_name '.
00158 'FROM music_songs '.
00159 'LEFT JOIN music_albums ON music_songs.album_id=music_albums.album_id '.
00160 'WHERE music_songs.artist_id='.mysql_real_escape_string($childitem).' '.
00161 'GROUP BY music_albums.album_id;';
00162 $result = mysql_query($query);
00163 if (!$result)
00164 break;
00165 $albums = '';
00166 while ($row2 = mysql_fetch_array($result))
00167 {
00168 $albums .= '<li><a class="music" href="#"'.
00169 ' onclick="updateBox(\'album\','.$row2['album_id'].'); return false;"'.
00170 ' title="'.sprintf(t('View Details of %s'), $row2['album_name']).'">'.
00171 $row2['album_name'].'</a></li>';
00172 }
00173 mysql_free_result($result);
00174
00175 $childoutput .= '<span><a class="music" href="#"'.
00176 ' onclick="updateBox(\'artist\','.$childitem.'); return false;">'.
00177 $row['artist_name'].'</a>'.
00178 '<ul class="music">'.$albums.'</ul></span>';
00179 break;
00180
00181 case 'letter':
00182 $childoutput .= '<span><a class="music" href="#"'.
00183 ' onclick="updateBox(\'letter\',\''.$childitem.'\'); return false;">'.
00184 strtoupper($childitem).'</a>'.letters().'</span>';
00185 break;
00186
00187 case 'genre':
00188 case 'all':
00189 $childoutput .= $childitem;
00190 break;
00191 }
00192 switch ($parent)
00193 {
00194 case 'letter':
00195 $parentoutput .= '<span><a class="music" href="#"'.
00196 ' onclick=\"updateBox(\'letter\',\''.$parentitem.'\'); return false;">'.
00197 strtoupper($parentitem).'</a>'.letters().'</span> » ';
00198 break;
00199
00200 case 'genre':
00201 case 'all':
00202 $parentoutput .= '<a class="music" href="#"'.
00203 ' onclick="updateBox(\''.$parent.'\',\''.$parentitem.'\'); return false;">'.
00204 $parentitem.'</a> » ';
00205 break;
00206 }
00207
00208 if (isset($output))
00209 {
00210 return $output.$parentoutput.$childoutput;
00211 }
00212
00213 return '';
00214 }
00215
00216 function musicLookup($type, $itemid)
00217 {
00218 global $db;
00219 $sql_itemid = "'".mysql_real_escape_string($itemid)."'";
00220 switch($type)
00221 {
00222 case 'browse':
00223 $output = '<div class="head">
00224 <h2 class="music">'.t('Browse the Music Database').'</h2></div>
00225 <p>
00226 <strong>'.t('By Artist Beginning With').'</strong><br/>'.letters().'<br/></p>
00227 <p><strong>'.t('By Genre').'</strong><br/>
00228 '.genreForm().'<br/><br/>
00229 <input type="button" value="'.t('Browse All Albums').'" onclick="updateBox(\'all\',\'All\'); return false;" class="btn2">
00230 </p>';
00231 break;
00232
00233 case 'search':
00234 $output = '<div class="head">
00235 <h2 class="music">'.t('Search the Music Database').'</h2></div>
00236 <form onsubmit="return searchMusic(this)" method="get" action="">
00237 <p>
00238 <strong>'.t('Keywords').'</strong><br/>
00239 <input type="text" onfocus="this.select()" name="searchbox" size="35" id="searchbox" value="['.t('Enter your search terms').']">
00240 <br/><br/>
00241 <strong>'.t('Narrow Your Search').'</strong>
00242 <br/>
00243 <select name="search_options" size="1">
00244 <option value="all">'.t('All Fields').'</option>
00245 <option value="artists">'.t('Artists').'</option>
00246 <option value="albums">'.t('Albums').'</option>
00247 <option value="songs">'.t('Songs').'</option>
00248 </select><br/><br/>
00249 <input type="submit" value="'.t('Submit Search').'" class="btn"></form>
00250 </p>';
00251 break;
00252
00253 case 'letter':
00254 if($itemid == "#")
00255 {
00256 $query = 'SELECT * FROM music_artists '.
00257 "WHERE artist_name REGEXP '^[0-9].*' ".
00258 'ORDER BY artist_name';
00259 }
00260 else
00261 {
00262 $query = 'SELECT * FROM music_artists '.
00263 'WHERE artist_name LIKE \''.mysql_real_escape_string($itemid.'%').'\' '.
00264 'ORDER BY artist_name';
00265 }
00266 $result = mysql_query($query);
00267 if (!$result)
00268 break;
00269
00270 $output = '<div class="head">
00271 <div class="right">
00272 <a class="music" href="#" onclick="updateBox(\'browse\',0); return false;"
00273 title="'.t('Browse').'">'.t('Back').'</a></div>
00274 <h2 class="music">'.sprintf(t('Artists Beginning with %s'), "'".strtoupper($itemid)."'").'</h2></div>
00275 <p>
00276 <strong>'.t('Artist Listing').'</strong></p>
00277 <ul class="music">';
00278 $alt = false;
00279 while ($row = mysql_fetch_array($result))
00280 {
00281 $output .= '<li'.($alt ? ' class="alt"' : '').'>
00282 <a class="music" href="#"
00283 onclick="updateBox(\'artist\','.$row['artist_id'].'); return false;"
00284 title="'.sprintf(t('View Albums by %s'), $row['artist_name']).'">'.
00285 $row['artist_name'].'</a></li>';
00286 $alt = !$alt;
00287 }
00288 mysql_free_result($result);
00289 $output .= '</ul>';
00290 break;
00291
00292 case 'all':
00293 $output = '<div class="head">
00294 <div class="right">
00295 <a class="music" href="#" onclick="updateBox(\'browse\',0); return false;"
00296 title="'.t('Browse').'">'.t('Back').'</a></div>
00297 <h2 class="music">'.t('All Albums').'</h2></div>
00298 <p>
00299 <strong>'.t('Album Listing').'</strong></p>
00300 <ul class="music">';
00301 $start = $itemid;
00302 $query = 'SELECT ma.album_id, ma.album_name, mt.artist_name '.
00303 'FROM music_albums AS ma '.
00304 'LEFT JOIN music_artists AS mt ON ma.artist_id=mt.artist_id '.
00305 'ORDER BY album_name, artist_name';
00306
00307 $result = mysql_query($query);
00308 if (!$result)
00309 break;
00310
00311 $alt = false;
00312 while ($row = mysql_fetch_array($result))
00313 {
00314 $output .= getHtmlAlbum($row['album_id'], $row['album_name'],
00315 $row['artist_name']);
00316 }
00317 mysql_free_result($result);
00318 $output .= '</ul>';
00319 break;
00320
00321 case 'album':
00322 // Get some statistics about the album
00323 $query = 'SELECT COUNT(*), SEC_TO_TIME(SUM(music_songs.length)/1000) '.
00324 'FROM music_songs '.
00325 'WHERE music_songs.album_id='.$sql_itemid.' '.
00326 'GROUP BY music_songs.album_id;';
00327 $result = mysql_query($query);
00328 if (!$result)
00329 break;
00330
00331 $row = mysql_fetch_array($result);
00332 mysql_free_result($result);
00333 $num_tracks = $row[0];
00334 $length = $row[1];
00335
00336 // Attempt to find some album art.
00337 $query='SELECT ms.filename, ms.album_id, md.path, ma.artist_name, ma.artist_id, ms.directory_id, mal.album_name
00338 FROM music_songs AS ms
00339 LEFT JOIN music_directories AS md
00340 ON ms.directory_id=md.directory_id
00341 LEFT JOIN music_artists AS ma
00342 ON ms.artist_id=ma.artist_id
00343 LEFT JOIN music_albums AS mal
00344 ON ms.album_id=mal.album_id
00345 WHERE ms.album_id='.$sql_itemid.'
00346 LIMIT 1';
00347 $result = mysql_query($query);
00348 if (!$result)
00349 break;
00350
00351 $row = mysql_fetch_array($result);
00352 mysql_free_result($result);
00353
00354 // Load album art
00355 $art_id = $db->query_col('SELECT ma.albumart_id
00356 FROM music_albumart AS ma
00357 LEFT JOIN music_directories AS md
00358 ON ma.directory_id=md.directory_id
00359 WHERE ma.directory_id = ?
00360 AND ma.imagetype = 1
00361 LIMIT 1',
00362 $row['directory_id']);
00363
00364 $output = '<div class="head">
00365 <div class="right">
00366 <a class="music" href="#"
00367 onclick="play(\'album\','.$row['album_id'].'); return false;"
00368 title="'.t('Play this Album Now').'">'.t('Play').'</a>
00369 <a class="music" href="#"
00370 onclick="pladd(\'album\','.$row['album_id'].'); return false;"
00371 title="'.t('Add Album to Current Playlist').'">'.t('Add').'</a>
00372 <a class="music" href="#" onclick="updateBox(\'artist\','.$row['artist_id'].'); return false;"
00373 title="'.$row['artist_name'].'">'.t('Back').'</a>
00374 </div>
00375 <h2 class="music">'.$row['album_name'].'</h2>
00376 </div>'.
00377 (!empty($art_id) ? '<center><img width="200" src="'.stream_url().'stream?a='.$art_id.'" /></center><br>' : '').
00378 '<strong>'.t('Play Time').':</strong> '.$length.
00379 '<br><br>
00380 <strong>'.t('Album Tracks').'</strong>
00381 <ul class="music">';
00382
00383 $query = 'SELECT ms.song_id, ms.track, ms.name, ms.length, ms.numplays, ms.rating, '.
00384 'SEC_TO_TIME(ms.length/1000) AS length, artist_name, genre '.
00385 'FROM music_songs AS ms '.
00386 'LEFT JOIN music_artists ON ms.artist_id=music_artists.artist_id '.
00387 'LEFT JOIN music_genres ON ms.genre_id=music_genres.genre_id '.
00388 'WHERE ms.album_id='.$sql_itemid.' '.
00389 'ORDER BY ms.track';
00390 $result = mysql_query($query);
00391 if (!$result)
00392 break;
00393
00394 while ($row = mysql_fetch_array($result))
00395 {
00396 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00397 '', $row['track'], $row['name'],
00398 $row['length'], $row['numplays'], $row['genre'], $row['rating']);
00399 }
00400 mysql_free_result($result);
00401 $output .= '</ul>';
00402 break;
00403
00404 case 'genre':
00405 $output = '<div class="head">
00406 <div class="right">
00407 <a class="music" href="#" onclick="updateBox(\'browse\',0); return false;"
00408 title="'.t('Browse').'">'.t('Back').'</a></div>
00409 <h2 class="music">'.t('Songs for Genre')." '".utf8_encode($itemid)."'</h2></div>
00410 <p><strong>".t('Songs').'</strong></p>
00411 <ul class="music">';
00412
00413 $query = 'SELECT ms.song_id, ms.name, SEC_TO_TIME(ms.length/1000) AS length, ms.numplays, ms.rating, ma.artist_name, mg.genre '.
00414 'FROM music_songs AS ms '.
00415 'LEFT JOIN music_artists AS ma ON ms.artist_id=ma.artist_id '.
00416 'LEFT JOIN music_genres AS mg ON ms.genre_id=mg.genre_id '.
00417 'WHERE genre='.utf8_encode($sql_itemid);
00418
00419 $result = mysql_query($query);
00420 if (!$result)
00421 break;
00422
00423 while ($row = mysql_fetch_array($result))
00424 {
00425 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00426 '', '', $row['name'],
00427 $row['length'], $row['numplays'], '', $row['rating']);
00428 }
00429 mysql_free_result($result);
00430 $output .= '</ul>';
00431 break;
00432
00433 case 'artist':
00434 $query = 'SELECT artist_name '.
00435 'FROM music_artists '.
00436 'WHERE artist_id='.$sql_itemid;
00437 $result = mysql_query($query);
00438 if (!$result)
00439 break;
00440
00441 $row = mysql_fetch_array($result);
00442 mysql_free_result($result);
00443 $artist = $row['artist_name'];
00444
00445 $letter = (!preg_match('/^[0-9]/', $artist) ? strtoupper($artist{0}) : '#');
00446
00447 $output = '<div class="head">
00448 <div class="right">
00449 <a class="music" href="#" onclick="updateBox(\'letter\',\''.$letter.'\'); return false;"
00450 title="'.sprintf(t('Artists Beginning with %s'), "'".$letter."'").'">'.t('Back').'</a></div>
00451 <h2 class="music">'.$artist.'</h2></div>
00452 <p><strong>'.sprintf(t('Albums with songs by %s'),'<i>'.$artist.'</i>').'</strong></p>
00453 <ul class="music">';
00454
00455 $query = 'SELECT ma.album_id, album_name, ma.year, ma.artist_id, artist_name'.
00456 ',SEC_TO_TIME(SUM(ms.length)/1000) AS length, COUNT(ms.song_id) AS num_tracks '.
00457 'FROM music_songs AS ms '.
00458 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
00459 'LEFT JOIN music_artists AS mt ON ma.artist_id=mt.artist_id '.
00460 'WHERE ms.artist_id='.$sql_itemid.' '.
00461 'GROUP BY ma.album_id;';
00462 $result = mysql_query($query);
00463 if (!$result)
00464 break;
00465
00466 while ($row = mysql_fetch_array($result))
00467 {
00468 $artist = '';
00469 if ($itemid != $row['artist_id'])
00470 $artist = $row['artist_name'];
00471
00472 $output .= getHtmlAlbum($row['album_id'], $row['album_name'],
00473 $artist, $row['year'], $row['num_tracks'], $row['length']);
00474 }
00475 mysql_free_result($result);
00476
00477 $output .='</ul><p><strong>'.t('Songs').'</strong></p>
00478 <ul class="music">';
00479
00480 $query = 'SELECT ms.song_id, ms.track, ms.name, ms.length, ms.numplays, ms.rating, '.
00481 'SEC_TO_TIME(ms.length/1000) AS length, music_artists.artist_name, track, '.
00482 'music_albums.album_name, genre '.
00483 'FROM music_songs AS ms '.
00484 'LEFT JOIN music_artists ON ms.artist_id=music_artists.artist_id '.
00485 'LEFT JOIN music_albums ON ms.album_id=music_albums.album_id '.
00486 'LEFT JOIN music_genres ON ms.genre_id=music_genres.genre_id '.
00487 'WHERE ms.artist_id='.$sql_itemid.';';
00488 $result = mysql_query($query);
00489 if (!$result)
00490 break;
00491
00492 while ($row = mysql_fetch_array($result))
00493 {
00494 $output .= getHtmlSong($row['song_id'], '',
00495 $row['album_name'], $row['track'], $row['name'],
00496 $row['length'], $row['numplays'], $row['genre'], $row['rating']);
00497 }
00498 mysql_free_result($result);
00499 $output .= '</ul>';
00500 break;
00501
00502 case 'random':
00503 $output = '<div class="head">
00504 <h2 class="music">'.t('Random Mix Maker').'</h2></div>
00505 <form onsubmit="return randAdd(this)" method="get" action="">
00506 <strong>'.t('Number of Songs').'</strong><br>
00507 <select name="random_count">
00508 <option>5</option>
00509 <option>10</option>
00510 <option>20</option>
00511 <option>30</option>
00512 <option>40</option>
00513 <option>50</option>
00514 <option>100</option>
00515 </select><br>
00516 <strong>'.t('Random Type').'</strong><br>
00517 <select name="random_type" onchange="getRandItems(this.options[selectedIndex].value); return false;">
00518 <option value="">'.t('Choose Type').'...</option>
00519 <option value="artists">'.t('Artists').'</option>
00520 <option value="genre">'.t('Genre').'</option>
00521 <option value="albums">'.t('Albums').'</option>
00522 <option value="all">'.t('Everything').'</option>
00523 </select><br>
00524 <strong>'.t('Random Items').'</strong>
00525 <span id="rand_items"></span>
00526 <br><br>
00527 <input type="submit" value="'.t('Add Mix').'" class="btn">
00528 </form>';
00529 break;
00530
00531 case 'playlists':
00532 $query = 'SELECT playlist_id, playlist_name, songcount, hostname, SEC_TO_TIME(length/1000) AS length '.
00533 'FROM music_playlists '.
00534 'WHERE hostname=\'\'';
00535 $result = mysql_query($query);
00536 if (!$result)
00537 break;
00538
00539 $output = '<div class="head">
00540 <h2 class="music">'.t('Saved Playlists').'</h2></div><br>';
00541
00542 if (mysql_num_rows($result) == 0)
00543 {
00544 $output .= t('No Public Playlists');
00545 }
00546 else
00547 {
00548 $unsaved_id = 0;
00549 $pl = internalGetPlaylist();
00550 if (!empty($pl['playlist_name'])
00551 && MYTH_WEB_PLAYLIST_NAME == $pl['playlist_name'])
00552 {
00553 $unsaved_id = $pl['playlist_id'];
00554 }
00555
00556 $output .= '<ul class="music">';
00557 while ($row = mysql_fetch_array($result))
00558 {
00559 $output .= getHtmlPlaylist($row['playlist_id'], $row['playlist_name'],
00560 $row['songcount'], $row['length'], $unsaved_id);
00561 }
00562 $output .= '</ul>';
00563 }
00564 mysql_free_result($result);
00565 break;
00566
00567 case 'saved_pl':
00568 $query = 'SELECT playlist_id, playlist_name, playlist_songs, songcount, SEC_TO_TIME(length/1000) AS length '.
00569 'FROM music_playlists '.
00570 'WHERE playlist_id='.$sql_itemid;
00571 $result = mysql_query($query);
00572 if (!$result)
00573 break;
00574
00575 $row = mysql_fetch_array($result);
00576 mysql_free_result($result);
00577
00578 $unsaved_id = 0;
00579 $pl = internalGetPlaylist();
00580 if (!empty($pl['playlist_name'])
00581 && MYTH_WEB_PLAYLIST_NAME == $pl['playlist_name'])
00582 {
00583 $unsaved_id = $pl['playlist_id'];
00584 }
00585
00586 $output = '<div class="head">
00587 <div class="right">
00588 <a class="music" href="#"
00589 onclick="checkPlaylistLoad(\''.$row['playlist_id'].'\', '.$unsaved_id.')'.
00590 ' && pladd(\'loadplaylist\','.$row['playlist_id'].'); return false;"
00591 title="'.t('Load Playlist').'">'.t('Load').
00592 '</a>
00593 <a class="music" href="#"
00594 onclick="pladd(\'playlist\','.$row['playlist_id'].'); return false;"
00595 title="'.t('Append to Current Playlist').'">'.t('Append').
00596 '</a>
00597 <a class="music" href="#"
00598 onclick="play(\'pl\','.$row['playlist_id'].'); return false;"
00599 title="'.t('Play this Playlist Now').'">'.t('Play').
00600 '</a>
00601 </div>
00602 <h2 class="music">'.t('View Saved Playlist').'</h2></div>
00603 <p><strong>'.t('Playlist Info').'</strong><br>'.
00604 sprintf('%s Songs', $row['songcount']).'<br>'.$row['length'].'</p>
00605 <p><strong>'.t('Playlist Items').'</strong></p>';
00606
00607
00608 if (empty($row['playlist_songs']))
00609 {
00610 $output = '<b>'.t('There are no items in this Playlist!').'</b>';
00611 }
00612 else
00613 {
00614
00615 $query = 'SELECT ms.song_id, mt.artist_name, ms.name, ma.album_name, ms.track'.
00616 ', SEC_TO_TIME(ms.length/1000) AS length '.
00617 'FROM music_songs AS ms '.
00618 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00619 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
00620 'WHERE ms.song_id IN ('.$row['playlist_songs'].');';
00621 $result = mysql_query($query);
00622 if (!$result)
00623 return;
00624
00625 $song_info = array();
00626 while ($row2 = mysql_fetch_array($result))
00627 {
00628 $song_info[$row2['song_id']] = $row2;
00629 }
00630 mysql_free_result($result);
00631
00632
00633
00634
00635 $query = 'SELECT playlist_id, playlist_name, SEC_TO_TIME(length/1000) AS length, songcount '.
00636 'FROM music_playlists '.
00637 'WHERE (-1.0 * (playlist_id+0.0)) IN ('.$row['playlist_songs'].');';
00638 $result = mysql_query($query);
00639 if (!$result)
00640 return;
00641
00642 $pl_info = array();
00643 while ($row2 = mysql_fetch_array($result))
00644 {
00645 $pl_info[$row2['playlist_id']] = $row2;
00646 }
00647 mysql_free_result($result);
00648
00649 $songs = explode(',', $row['playlist_songs']);
00650 $output .= '<ul class="music">';
00651 foreach ($songs as $song_id)
00652 {
00653 if ($song_id > 0)
00654 {
00655 $row = $song_info[$song_id];
00656 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00657 '', '', $row['name'],
00658 $row['length'], $row['numplays'], '', '');
00659 }
00660 else if ($song_id < 0)
00661 {
00662 $row = $pl_info[-1 * $song_id];
00663 $output .= getHtmlPlaylist($row['playlist_id'], $row['playlist_name'],
00664 $row['songcount'], $row['length'], $unsaved_id, false);
00665 }
00666 }
00667 $output .= '</ul>';
00668 }
00669 break;
00670
00671 case 'stats':
00672 $query = 'SELECT * FROM music_stats';
00673 $result = mysql_query($query);
00674 if (!$result)
00675 break;
00676
00677 $row = mysql_fetch_array($result);
00678 mysql_free_result($result);
00679
00680 $query = 'SELECT COUNT(*) AS songs FROM music_songs WHERE numplays>0';
00681 $result = mysql_query($query);
00682 if (!$result)
00683 break;
00684
00685 $row2 = mysql_fetch_array($result);
00686 mysql_free_result($result);
00687
00688 $output = '<div class="head">
00689 <h2 class="music">'.t('Server Statistics').'</h2></div>
00690 <p><a class="music" href="#" onclick="updateBox(\'recentadd\',0); return false;">'.
00691 t('Recently Added Albums').'</a><br>
00692 <a class="music" href="#" onclick="updateBox(\'recentplay\',0); return false;">'.
00693 t('Recently Played Songs').'</a><br>
00694 <a class="music" href="#" onclick="updateBox(\'topplay\',0); return false;">'.
00695 t('Top Played Songs').'</a><br>
00696 <a class="music" href="#" onclick="updateBox(\'toprated\',0); return false;">'.
00697 t('Top Rated Songs').'</a><br>
00698 </p>
00699 <h3>'.t('Local Server Statistics').'</h3>
00700 <p>';
00701
00702 foreach (array('music_songs' => t('Songs'),
00703 'music_albums' => t('Albums'),
00704 'music_artists' => t('Artists'),
00705 'music_genres' => t('Genres')) as $table => $title)
00706 {
00707 $result = mysql_query('SELECT COUNT(*) FROM '.$table.';');
00708 if (!$result)
00709 continue;
00710 $count = mysql_fetch_array($result);
00711 mysql_free_result($result);
00712 $output .= '<strong>'.$title.':</strong> '.$count[0].'<br>';
00713 }
00714 $output .= '<br><strong>'.t('Songs Played').':</strong> '.$row2['songs'].'<br>';
00715
00716 $result = mysql_query('SELECT COUNT(*) AS songs FROM music_songs WHERE rating > 0;');
00717 if(!$result)
00718 break;
00719
00720 $row3 = mysql_fetch_array($result);
00721 mysql_free_result($result);
00722 $output .= '<strong>'.t('Songs Rated').':</strong> '.$row3['songs'].'<br></p>';
00723
00724 break;
00725
00726 case 'recentadd':
00727 $query = 'SELECT ma.album_name, ma.album_id, mt.artist_name, UNIX_TIMESTAMP(ms.date_entered) AS pubdate '.
00728 'FROM music_songs AS ms '.
00729 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
00730 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00731 'GROUP BY ms.album_id '.
00732 'ORDER BY ms.date_entered DESC '.
00733 'LIMIT 40';
00734 $result = mysql_query($query);
00735 if (!$result)
00736 break;
00737
00738 $output = '<div class="head">
00739 <div class="right">
00740 <a class="music" href="#"
00741 onclick="switchPage(\'stats\'); return false;"
00742 title="'.t('Return to Statistics Page').'">'.t('Back').'</a></div>
00743 <h2 class="music">'.t('Recently Added Albums').'</h2></div>
00744 <ul class="music">';
00745 while ($row = mysql_fetch_array($result))
00746 {
00747 $output .= getHtmlAlbum($row['album_id'], $row['album_name'],
00748 $row['artist_name'], '', '', '', date('m.d.Y', $row['pubdate']));
00749 }
00750 mysql_free_result($result);
00751 $output .= '</ul>';
00752 break;
00753
00754 case 'topplay':
00755 $query = 'SELECT ma.album_name, ms.numplays, ms.name, mt.artist_name, ms.song_id '.
00756 'FROM music_songs AS ms '.
00757 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
00758 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00759 'WHERE ms.numplays > 0 '.
00760 'ORDER BY ms.numplays DESC '.
00761 'LIMIT 40';
00762 $result = mysql_query($query);
00763 if (!$result)
00764 break;
00765
00766 $output = '<div class="head">
00767 <div class="right">
00768 <a class="music" href="#"
00769 onclick="switchPage(\'stats\'); return false;"
00770 title="'.t('Return to Statistics Page').'">'.t('Back').'</a></div>
00771 <h2 class="music">'.t('Top Played Songs').'</h2></div>
00772 <ul class="music">';
00773 while ($row = mysql_fetch_array($result))
00774 {
00775 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00776 '', '', $row['name'], '', '', '', '');
00777 }
00778 mysql_free_result($result);
00779 $output .= '</ul>';
00780 break;
00781
00782 case 'recentplay':
00783 $query = 'SELECT ms.name, ms.song_id, mt.artist_name, UNIX_TIMESTAMP(ms.lastplay) AS playdate '.
00784 'FROM music_songs AS ms '.
00785 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00786 'WHERE ms.numplays > 0 '.
00787 'ORDER BY ms.lastplay DESC '.
00788 'LIMIT 40';
00789 $result = mysql_query($query);
00790 if (!$result)
00791 break;
00792
00793 $output = '<div class="head">
00794 <div class="right">
00795 <a class="music" href="#"
00796 onclick="switchPage(\'stats\'); return false;"
00797 title="'.t('Return to Statistics Page').'">'.t('Back').'</a></div>
00798 <h2 class="music">'.t('Recently Played Songs').'</h2></div>
00799 <ul class="music">';
00800 while ($row = mysql_fetch_array($result))
00801 {
00802 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00803 '', '', $row['name'], '', '', '', '');
00804 }
00805 $output .= '</ul>';
00806 break;
00807
00808 case 'toprated':
00809 $query = 'SELECT ms.name, ms.song_id, ms.rating, mt.artist_name '.
00810 'FROM music_songs AS ms '.
00811 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00812 'ORDER BY ms.rating DESC '.
00813 'LIMIT 40';
00814 $result = mysql_query($query);
00815 if(!result)
00816 break;
00817
00818 $output = '<div class="head">
00819 <div class="right">
00820 <a class="music" href="#"
00821 onclick="switchPage(\'stats\'); return false;"
00822 title="'.t('Return to Statistics Page').'">'.t('Back').'</a></div>
00823 <h2 class="music">'.t('Top Rated Songs').'</h2></div>
00824 <ul class="music">';
00825 while ($row = mysql_fetch_array($result))
00826 {
00827 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00828 '', '', $row['name'], '', '', '', $row['rating']);
00829 }
00830 $output .= '</ul>';
00831 break;
00832 }
00833
00834 return $output;
00835 }
00836
00837
00838 function getRandItems($type)
00839 {
00840 switch ($type)
00841 {
00842 case 'artists':
00843 $query = 'SELECT artist_id, artist_name FROM music_artists ORDER BY artist_name';
00844 break;
00845 case 'genre':
00846 $query = 'SELECT genre_id, genre FROM music_genres ORDER BY genre';
00847 break;
00848 case 'albums':
00849 $query = 'SELECT album_id, album_name FROM music_albums ORDER BY album_name';
00850 break;
00851 default:
00852 return '<br>'.t('All Songs');
00853 }
00854
00855 $result = mysql_query($query);
00856 if (!$result)
00857 return '';
00858
00859 $options = '';
00860 while ($row = mysql_fetch_array($result))
00861 {
00862 $options .= '<option value="'.$row[0].'">'.
00863 $row[1].'</option>';
00864 }
00865 mysql_free_result($result);
00866
00867 return '<select name="random_items" multiple="multiple" size="12" style="width: 90%;">'.
00868 $options.'</select>';
00869 }
00870
00871
00872 function searchMusic($terms, $option)
00873 {
00874 $sql_terms = "'%".mysql_real_escape_string($terms)."%'";
00875 $query = 'SELECT ms.song_id, ma.album_name, ms.track, mt.artist_name, ms.name, ms.rating, '.
00876 'SEC_TO_TIME(ms.length/1000) AS length, genre '.
00877 'FROM music_songs AS ms '.
00878 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
00879 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
00880 'LEFT JOIN music_genres AS mg ON ms.genre_id=mg.genre_id '.
00881 'WHERE 1 AND ';
00882
00883 if ($option == 'all')
00884 {
00885 $query .= '(ms.name LIKE '.$sql_terms.
00886 ' OR mt.artist_name LIKE '.$sql_terms.
00887 ' OR ma.album_name LIKE '.$sql_terms.')';
00888 }
00889 else if ($option == 'artists')
00890 {
00891 $query .= '(mt.artist_name LIKE '.$sql_terms.')';
00892 }
00893 else if ($option == 'albums')
00894 {
00895 $query .= '(ma.album_name LIKE '.$sql_terms.')';
00896 }
00897 else if ($option == 'songs')
00898 {
00899 $query .= '(ms.name LIKE '.$sql_terms.')';
00900 }
00901 $query .= ' ORDER BY mt.artist_name, ma.album_name, ms.track, ms.name';
00902
00903 $result = mysql_query($query);
00904 if (!$result)
00905 return '';
00906
00907 $count = mysql_num_rows($result);
00908
00909 $output = '<div class="head">
00910 <div class="right">
00911 <a class="music" href="#"
00912 onclick="switchPage(\'search\'); return false;"
00913 title="'.t('Begin a New Search').'">'.t('New Search').'</a></div>
00914 <h2 class="music">'.sprintf(t("Found %s results for '%s'"), $count, $terms).'</h2></div>';
00915
00916 if($count > 0)
00917 {
00918 $output .= '<ul class="music">';
00919 while ($row = mysql_fetch_array($result))
00920 {
00921 $output .= getHtmlSong($row['song_id'], $row['artist_name'],
00922 $row['album_name'], $row['track'], $row['name'],
00923 $row['length'], '', $row['genre'], $row['rating']);
00924 }
00925 $output .= '</ul>';
00926 }
00927 return $output;
00928 }
00929
00930 function internalGetPlaylist($plId = 0)
00931 {
00932 $row = array();
00933 if (empty($plId))
00934 {
00935 if (empty($_COOKIE['mp3act_playlist_id']))
00936 return $row;
00937 $plId = $_COOKIE['mp3act_playlist_id'];
00938 }
00939
00940 $query = 'SELECT playlist_id, playlist_name, playlist_songs, songcount, length AS length_in_secs'.
00941 ', SEC_TO_TIME(length/1000) AS length '.
00942 'FROM music_playlists '.
00943 'WHERE playlist_id='.mysql_real_escape_string($plId);
00944
00945 $result = mysql_query($query);
00946 if (!$result)
00947 return $row;
00948
00949 if (mysql_num_rows($result) > 0)
00950 $row = mysql_fetch_array($result);
00951 mysql_free_result($result);
00952
00953
00954
00955 if (MYTH_WEB_PLAYLIST_NAME == $row['playlist_name'])
00956 {
00957 $query = 'UPDATE music_playlists'.
00958 ' SET last_accessed=NULL '.
00959 'WHERE playlist_id='.mysql_real_escape_string($plId);
00960 mysql_query($query);
00961 }
00962
00963 return $row;
00964 }
00965
00966 function internalUpdatePlaylist($songs, $count, $length)
00967 {
00968 $plId = 0;
00969 if (!empty($_COOKIE['mp3act_playlist_id']))
00970 $plId = $_COOKIE['mp3act_playlist_id'];
00971
00972 $songlist = implode(',', $songs);
00973
00974 $query = 'music_playlists SET'.
00975 " playlist_songs='".mysql_real_escape_string($songlist)."'".
00976 ',length='.mysql_real_escape_string($length).
00977 ',songcount='.mysql_real_escape_string($count);
00978
00979 if (empty($plId))
00980 {
00981 $query = 'INSERT INTO '.$query.
00982 ",hostname='".mysql_real_escape_string('mythweb-'.$_SERVER['SERVER_NAME'])."'".
00983 ",playlist_name='".MYTH_WEB_PLAYLIST_NAME."'";
00984 }
00985 else
00986 {
00987 $query = 'UPDATE '.$query.
00988 ' WHERE playlist_id='.mysql_real_escape_string($plId);
00989 }
00990 mysql_query($query);
00991
00992 if (empty($plId))
00993 {
00994 $plId = mysql_insert_id();
00995 if ($plId)
00996 {
00997 setcookie('mp3act_playlist_id', $plId, time()+MYTH_PLAYLIST_SAVE_TIME);
00998 return $plId;
00999 }
01000 }
01001 return false;
01002 }
01003
01004 function viewPlaylist()
01005 {
01006 $pl = internalGetPlaylist();
01007
01008 if (empty($pl['playlist_songs']))
01009 {
01010 return '';
01011 }
01012
01013
01014 $query = 'SELECT ms.song_id, mt.artist_name, ms.name, ma.album_name, ms.track'.
01015 ', SEC_TO_TIME(ms.length/1000) AS length '.
01016 'FROM music_songs AS ms '.
01017 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
01018 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
01019 'WHERE ms.song_id IN ('.$pl['playlist_songs'].');';
01020 $result = mysql_query($query);
01021 if (!$result)
01022 return;
01023
01024 $song_info = array();
01025 while ($row = mysql_fetch_array($result))
01026 {
01027 $song_info[$row['song_id']] = $row;
01028 }
01029 mysql_free_result($result);
01030
01031
01032
01033
01034 $query = 'SELECT playlist_id, playlist_name, SEC_TO_TIME(length/1000) AS length, songcount '.
01035 'FROM music_playlists '.
01036 'WHERE (-1.0 * (playlist_id+0.0)) IN ('.$pl['playlist_songs'].');';
01037 $result = mysql_query($query);
01038 if (!$result)
01039 return;
01040
01041 $pl_info = array();
01042 while ($row = mysql_fetch_array($result))
01043 {
01044 $pl_info[$row['playlist_id']] = $row;
01045 }
01046 mysql_free_result($result);
01047
01048
01049 $songs = explode(',', $pl['playlist_songs']);
01050 $output = '';
01051 $id=0;
01052 foreach ($songs as $song_id)
01053 {
01054
01055 $id = md5($song_id.mt_rand());
01056 if ($song_id > 0)
01057 {
01058 $row = $song_info[$song_id];
01059 $output .= getHtmlPlaylistEntrySong($id, $row['artist_name'],
01060 $row['album_name'], $row['track'], $row['name'], $row['length']);
01061 }
01062 else
01063 {
01064 $row = $pl_info[-1 * $song_id];
01065 $output .= getHtmlPlaylistEntryPlaylist($id, $row['playlist_name'],
01066 $row['songcount'], $row['length']);
01067 }
01068 }
01069 return $output;
01070 }
01071
01072
01073 function playlistInfo()
01074 {
01075 $pl = internalGetPlaylist();
01076
01077 $info = '';
01078
01079 if (!empty($pl) && MYTH_WEB_PLAYLIST_NAME != $pl['playlist_name'])
01080 $info = '<em>'.$pl['playlist_name'].'</em> — ';
01081
01082 if (empty($pl['playlist_songs']))
01083 {
01084 $info .= t('Playlist is empty');
01085 }
01086 else
01087 {
01088 if (1 == $pl['count'])
01089 $info .= sprintf(t('%s Song (%s)'), $pl['songcount'], $pl['length']);
01090 else
01091 $info .= sprintf(t('%s Songs (%s)'), $pl['songcount'], $pl['length']);
01092 }
01093
01094 return $info;
01095 }
01096
01097
01098 function savePlaylist($pl_name, $newpl)
01099 {
01100 $pl = internalGetPlaylist();
01101
01102 if (!empty($pl['playlist_id']))
01103 $pl_id = $pl['playlist_id'];
01104 else
01105 $pl_id = internalUpdatePlaylist(array(), 0, 0);
01106
01107 if (empty($pl_id))
01108 {
01109 $msg = t('There was a problem saving your playlist');
01110 }
01111 else
01112 {
01113 $query = 'UPDATE music_playlists SET'.
01114 ' playlist_name=\''.mysql_real_escape_string($pl_name).'\''.
01115 ",hostname='' ".
01116 'WHERE playlist_id='.mysql_real_escape_string($pl['playlist_id']);
01117
01118 mysql_query($query);
01119
01120 if (MYTH_WEB_PLAYLIST_NAME == $pl['playlist_name'])
01121 $msg = t('Playlist saved successfully');
01122 else
01123 $msg = t('Playlist renamed successfully');
01124 }
01125
01126 return '<h2 class="music">'.$msg.'</h2>';
01127 }
01128
01129 function clearPlaylist()
01130 {
01131 $pl = internalGetPlaylist();
01132
01133
01134 setcookie('mp3act_playlist_id', false, time()-3600);
01135
01136 if (!empty($pl['playlist_name'])
01137 && MYTH_WEB_PLAYLIST_NAME == $pl['playlist_name'])
01138 {
01139 deletePlaylist($pl['playlist_id']);
01140 }
01141
01142 return t('Playlist is empty');
01143 }
01144
01145
01146 function deletePlaylist($id)
01147 {
01148 $rv = 0;
01149 if ($id == $_COOKIE['mp3act_playlist_id'])
01150 {
01151 $rv = 1;
01152 setcookie('mp3act_playlist_id', false, time()-3600);
01153 }
01154
01155 $query = 'DELETE FROM music_playlists '.
01156 'WHERE playlist_id='.mysql_real_escape_string($id);
01157 mysql_query($query);
01158 return $rv;
01159 }
01160
01161
01162 function playlist_rem($itemid)
01163 {
01164 $pl = internalGetPlaylist();
01165
01166 $songs = explode(',', $pl['playlist_songs']);
01167 $idx = intval($itemid);
01168 if (isset($songs[$idx]))
01169 {
01170
01171 $id = $songs[$idx];
01172 if ($id > 0)
01173 {
01174 $query = 'SELECT length, 1 AS songcount FROM music_songs '.
01175 'WHERE song_id='.mysql_real_escape_string($id);
01176 }
01177 else
01178 {
01179 $query = 'SELECT length, songcount FROM music_playlists '.
01180 'WHERE playlist_id='.mysql_real_escape_string(-1 * $id);
01181 }
01182 $result = mysql_query($query);
01183 $length = $count = 0;
01184 if ($result)
01185 {
01186 $row = mysql_fetch_array($result);
01187 mysql_free_result($result);
01188 if ($row)
01189 {
01190 $length = $row['length'];
01191 $count = $row['songcount'];
01192 }
01193 }
01194 unset($songs[$idx]);
01195 internalUpdatePlaylist($songs,
01196 ($pl['songcount'] - $count),
01197 ($pl['length_in_secs'] - $length));
01198 }
01199 return $itemid;
01200 }
01201
01202 function playlist_move($item1,$item2)
01203 {
01204 $pl = internalGetPlaylist();
01205
01206 $idx1 = intval($item1);
01207 $idx2 = intval($item2);
01208
01209 $songs = explode(',',$pl['playlist_songs']);
01210
01211 if (!isset($songs[$idx1]) || !isset($songs[$idx2]))
01212 return;
01213
01214 $tmp = $songs[$idx1];
01215 $songs[$idx1] = $songs[$idx2];
01216 $songs[$idx2] = $tmp;
01217
01218 $query = 'UPDATE music_playlists SET'.
01219 ' playlist_songs=\''.mysql_real_escape_string(implode(',', $songs)).'\' '.
01220 'WHERE playlist_id='.mysql_real_escape_string($pl['playlist_id']).';';
01221 mysql_query($query);
01222 }
01223
01224 function internalPlaylistAddPlaylistCheck($curPlId, $addPlId, $depth = 0)
01225 {
01226 // Infinite loop protection (e.g. if the DB is messed up already)
01227 if ($depth > 25)
01228 return false;
01229
01230 // Looking to see if $addPlId playlist at any point includes $curPlId playlist
01231 if ($curPlId == $addPlId)
01232 return false;
01233
01234 $query = 'SELECT playlist_songs '.
01235 'FROM music_playlists '.
01236 'WHERE playlist_id='.mysql_real_escape_string($addPlId);
01237
01238 $result = mysql_query($query);
01239 if (!$result)
01240 return false;
01241
01242 $row = mysql_fetch_array($result);
01243 mysql_free_result($result);
01244
01245 if (!$row) // Not a real playlist.
01246 return false;
01247
01248 $songs = explode(',', $row['playlist_songs']);
01249 $playlists = array_filter($songs, create_function('$n','return ($n < 0);'));
01250
01251 foreach ($playlists as $playlist_id)
01252 {
01253 if ($playlist_id > 0)
01254 continue; // Shouldn't happen due to the filter above but I'm paranoid.
01255
01256 $playlist_id *= -1;
01257
01258 if (!internalPlaylistAddPlaylistCheck($curPlId, $playlist_id, $depth+1))
01259 return false;
01260 }
01261
01262 return true;
01263 }
01264
01265
01266 function playlist_add($type, $itemid)
01267 {
01268 $output = array(0 => '', 1 => 0);
01269
01270 if ('loadplaylist' == $type)
01271 {
01272 clearPlaylist();
01273 setcookie('mp3act_playlist_id', $itemid, time()+MYTH_PLAYLIST_SAVE_TIME);
01274 $output[0] = 1;
01275 return $output;
01276 }
01277
01278 $pl = internalGetPlaylist();
01279
01280 if (empty($pl['playlist_songs']))
01281 {
01282 $songs = array();
01283 $new_length = 0;
01284 $new_songcount = 0;
01285 }
01286 else
01287 {
01288 $songs = explode(',', $pl['playlist_songs']);
01289 $new_length = $pl['length_in_secs'];
01290 $new_songcount = $pl['songcount'];
01291 }
01292
01293 if ('playlist' == $type)
01294 {
01295 if (!empty($pl['playlist_id']))
01296 {
01297 if (!internalPlaylistAddPlaylistCheck($pl['playlist_id'], $itemid))
01298 {
01299 // Some sort of nasty circular dependancy.
01300 $output[0] = 2;
01301 $output[1] = t('Sorry, but you cannot add this playlist as it would create a circular dependancy.');
01302 return $output;
01303 }
01304 }
01305 $pl_add = internalGetPlaylist($itemid);
01306 if (empty($pl_add['playlist_id']))
01307 {
01308 $output[0] = 2;
01309 $output[1] = t('An error occured while adding your playlist.');
01310 return $output;
01311 }
01312 $new_songcount += $pl_add['songcount'];
01313 $new_length += $pl_add['length_in_secs'];
01314 $songs[] = -1 * $itemid;
01315
01316 $id = md5((-1 * $itemid).mt_rand());
01317 $output[0] .= getHtmlPlaylistEntryPlaylist($id, $pl_add['playlist_name'],
01318 $pl_add['songcount'], $pl_add['length']);
01319 $output[1]++;
01320 $output[] = 'pl'.$id;
01321
01322 internalUpdatePlaylist($songs, $new_songcount, $new_length);
01323
01324 return $output;
01325 }
01326
01327 $sql_itemid = mysql_real_escape_string($itemid);
01328 $query = 'SELECT ms.song_id, mt.artist_name, ma.album_name,'.
01329 ' length AS length_in_secs, SEC_TO_TIME(ms.length/1000) AS length, ms.name, ms.track '.
01330 'FROM music_songs AS ms '.
01331 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
01332 'LEFT JOIN music_albums AS ma ON ms.album_id=ma.album_id '.
01333 'WHERE ';
01334 switch ($type)
01335 {
01336 case 'song':
01337 $query .= 'ms.song_id='.$sql_itemid;
01338 break;
01339 case 'album':
01340 $query .= 'ms.album_id='.$sql_itemid;
01341 break;
01342 default:
01343 return $output;
01344 }
01345
01346 $result = mysql_query($query.' ORDER BY ms.track');
01347 if (!$result)
01348 return $output;
01349
01350 while ($row = mysql_fetch_array($result))
01351 {
01352 $id = md5($row['song_id'].mt_rand());
01353 $output[0] .= getHtmlPlaylistEntrySong($id, $row['artist_name'],
01354 $row['album_name'], $row['track'], $row['name'], $row['length']);
01355 $output[1]++;
01356 $output[] = 'pl'.$id;
01357
01358 $songs[] = $row['song_id'];
01359 $new_length += $row['length_in_secs'];
01360 $new_songcount++;
01361 }
01362 mysql_free_result($result);
01363
01364 internalUpdatePlaylist($songs, $new_songcount, $new_length);
01365
01366 return $output;
01367 }
01368
01369 function randAdd($type,$num=0,$items='')
01370 {
01371 $output = array(0 => 1);
01372
01373 if ($type != 'all' && !preg_match('/^[0-9]+(,[0-9]+)*$/', $items))
01374 {
01375 return $output;
01376 }
01377 $sql_items = mysql_real_escape_string($items);
01378
01379 $query = 'SELECT song_id, length '.
01380 'FROM music_songs ';
01381 switch($type)
01382 {
01383 case 'artists':
01384 $query .= 'WHERE artist_id IN ('.$sql_items.') ';
01385 break;
01386 case 'genre':
01387 $query .= 'WHERE genre_id IN ('.$sql_items.') ';
01388 break;
01389 case 'albums':
01390 $query .= 'WHERE album_id IN ('.$sql_items.') ';
01391 break;
01392 }
01393 $query .= 'ORDER BY RAND()+0 '.
01394 'LIMIT '.mysql_real_escape_string(intval($num));
01395 $result = mysql_query($query);
01396
01397 if (!$result)
01398 return $output;
01399
01400 $pl = internalGetPlaylist();
01401
01402 if (empty($pl['playlist_songs']))
01403 {
01404 $songs = array();
01405 $new_length = 0;
01406 $new_songcount = 0;
01407 }
01408 else
01409 {
01410 $songs = explode(',', $pl['playlist_songs']);
01411 $new_length = $pl['length_in_secs'];
01412 $new_songcount = $pl['songcount'];
01413 }
01414
01415 while ($row = mysql_fetch_array($result))
01416 {
01417 $songs[] = $row['song_id'];
01418 $new_length += $row['length'];
01419 $new_songcount++;
01420 }
01421 mysql_free_result($result);
01422
01423 internalUpdatePlaylist($songs, $new_songcount, $new_length);
01424
01425 return $output;
01426 }
01427
01428 function getPlaylistM3u($id, $quality, $depth = 0)
01429 {
01430 $tmp = '';
01431 if ($depth > 20)
01432 return $tmp;
01433
01434 $pl = internalGetPlaylist($id);
01435 if (empty($pl['playlist_songs']))
01436 {
01437 return $tmp;
01438 }
01439
01440 $query = 'SELECT ms.song_id, artist_name, ms.name, (ms.length/1000) AS length '.
01441 'FROM music_songs AS ms '.
01442 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
01443 'WHERE ms.song_id IN ('.mysql_real_escape_string($pl['playlist_songs']).')';
01444
01445 $song_info = array();
01446 $result = mysql_query($query);
01447 if (!$result)
01448 return $tmp;
01449
01450 while ($row = mysql_fetch_array($result))
01451 {
01452 $song_info[$row['song_id']] = $row;
01453 }
01454 mysql_free_result($result);
01455
01456 $songs = explode(',', $pl['playlist_songs']);
01457 foreach ($songs as $song_id)
01458 {
01459 if ($song_id > 0)
01460 {
01461 $row = $song_info[$song_id];
01462 $tmp .= '#EXTINF:'.intval($row['length']).','.utf8_decode($row['artist_name']).' - '.utf8_decode($row['name'])."\n";
01463 $tmp .= stream_url().'stream?i='.$row['song_id']."\n";
01464 }
01465 else if ($song_id < 1)
01466 {
01467 $tmp .= getPlaylistM3u((-1 * $song_id), $quality, $depth+1);
01468 }
01469 }
01470
01471 return $tmp;
01472 }
01473
01474 function play($type, $id, $quality = 'high')
01475 {
01476 $tmp = '';
01477 $query = '';
01478
01479 if ('pl' == $type)
01480 {
01481 $tmp .= getPlaylistM3u($id, $quality);
01482 }
01483 else
01484 {
01485 $query = 'SELECT ms.song_id, artist_name, ms.name, (ms.length/1000) AS length '.
01486 'FROM music_songs AS ms '.
01487 'LEFT JOIN music_artists AS mt ON ms.artist_id=mt.artist_id '.
01488 'WHERE ';
01489
01490 $sql_id = mysql_real_escape_string($id);
01491 switch ($type)
01492 {
01493 case 'song':
01494 $query .= 'ms.song_id='.$sql_id;
01495 break;
01496 case 'album':
01497 $query .= 'ms.album_id='.$sql_id.' '.
01498 'ORDER BY ms.track';
01499 break;
01500 default:
01501 return '';
01502 }
01503
01504 $result = mysql_query($query);
01505 if ($result)
01506 {
01507 while ($row = mysql_fetch_array($result))
01508 {
01509 $tmp .= '#EXTINF:'.intval($row['length']).','.utf8_decode($row['artist_name']).' - '.utf8_decode($row['name'])."\n";
01510 $tmp .= stream_url().'stream?i='.$row['song_id']."\n";
01511 }
01512 mysql_free_result($result);
01513 }
01514 }
01515
01516 if($tmp == '')
01517 return '';
01518
01519 session_cache_limiter('nocache');
01520 header('Content-Type: audio/mpegurl;');
01521 header('Content-Disposition: inline; filename="playlist.m3u"');
01522 header('Expires: 0');
01523 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
01524 header('Pragma: nocache');
01525
01526 return "#EXTM3U\n".$tmp;
01527 }