/* -*- c -*- */
$debug = 0;
$db_host = "localhost";
$db_user = "music";
$db_pass = "IwmMTV!";
$db_name = "music";
$db_conn;
$uname = posix_uname();
$hostname = $uname["nodename"];
$revision = '$Revision: 1.6 $';
$url_host = "http://$hostname.zawodny.com";
$url_home = "$url_host/~jzawodn";
$url_prefix = "$url_host/~jzawodn/music";
$rs;
$arrow_img = "";
/********************************************************************/
function get_param ()
{
global $PHP_SELF;
$info = $_SERVER["PHP_SELF"];
debug("get_param() start.");
debug("PHP_SELF: ");
debug($info);
$list = explode("/", $info);
debug(count($list));
$item = $list[count($list)-1];
debug($item);
debug("get_param() end.");
return $item;
}
/********************************************************************/
function get_all_genres () {
global $db_conn, $debug;
$sql = "select * from genres order by name";
debug($sql);
if ($rs = mysql_query($sql, $db_conn)) {
debug("Query Worked. [rs is $rs]");
} else {
debug("Query Failed. [rs is $rs]");
}
return $rs;
}
/********************************************************************/
function get_all_genres_count () {
$sql = "select g.name, g.id, count(g.id) as cnt from albums a, genres g where g.id = a.genre_id group by g.id order by g.name;";
return run_query($sql);
}
/********************************************************************/
function get_mp3_album_count () {
$sql = "select count(*) as cnt from albums where encoded = 'Y';";
$rs = run_query($sql);
$obj = mysql_fetch_object($rs);
return $obj->cnt;
}
/********************************************************************/
function get_album_artists () {
$sql = "select distinct(R.name) from artists R, albums A where R.id = A.artist_id order by R.name";
return run_query($sql);
}
/********************************************************************/
function get_genre_name ($id) {
$sql = "select name from genres where id = $id;";
$rs = run_query($sql);
$obj = mysql_fetch_object($rs);
return $obj->name;
}
/********************************************************************/
function get_albums_in_genre ($id) {
$sql = "select * from albums where genre_id = $id;";
return run_query($sql);
}
/********************************************************************/
function get_albums_by_artist ($id) {
$sql = "select A.title, A.id, G.id as genre_id, G.name as genre from albums A, genres G where A.artist_id = $id and A.genre_id = G.id order by title";
return run_query($sql);
}
/********************************************************************/
function search_album_titles ($terms) {
$sql = "select a.id, a.title, g.name as genre, r.name as artist from albums a, genres g, artists r where a.title like '%$terms%' and a.genre_id = g.id and a.artist_id = r.id order by a.title;";
return run_query($sql);
}
/********************************************************************/
function search_track_titles ($terms) {
$sql = "select T.id, T.num, T.title, T.artist_id, T.album_id, R.name as artist, A.title as album from tracks T, albums A, artists R where T.title like '%$terms%' and T.album_id = A.id and T.artist_id = R.id order by T.title;";
return run_query($sql);
}
/********************************************************************/
function get_album ($id) {
$sql = "select A.title, A.id, A.encoded, R.name, G.name as genre, A.genre_id, A.artist_id from albums A, artists R, genres G where A.id = '$id' and R.id = A.artist_id and A.genre_id = G.id";
return run_query($sql);
}
/********************************************************************/
function get_album_tracks ($id) {
$sql = "select T.id, T.title, T.artist_id, TIME_FORMAT(T.length, '%i:%s') as length, R.name as artist from tracks T, artists R where album_id = $id and T.artist_id = R.id order by num asc;";
return run_query($sql);
}
/********************************************************************/
function get_playlist () {
$sql = "select p.*, t.title, r.name from playlist p, tracks t, artists r where p.track_id = t.id and t.artist_id = r.id order by rank desc, add_time asc;";
return run_query($sql);
}
/********************************************************************/
function get_now_playing () {
$sql = "select p.*, t.title, r.name, r.id as artist_id from now_playing p, tracks t, artists r where p.track_id = t.id and t.artist_id = r.id;";
return run_query($sql);
}
/********************************************************************/
function get_random_track_id () {
$sql = "select id from tracks;";
$rs = run_query($sql);
$cnt = mysql_num_rows($rs);
debug("random record cnt: $cnt");
for ($i=1; $i<=$cnt; $i++) {
$row = mysql_fetch_row($rs);
$ids[] = $row[0];
}
srand(time());
$num = rand(0, $cnt-1);
debug("random num: $num");
return $ids[$num];
}
/********************************************************************/
function playlist_add ($track_id) {
$sql = "insert into playlist values (NULL, $track_id, 10, NULL);";
run_query($sql);
}
/********************************************************************/
function playlist_delete($id) {
$sql = "delete from playlist where id = $id;";
run_query($sql);
}
/********************************************************************/
function playlist_increment ($id) {
$sql = "update playlist set rank = rank+1 where id = $id;";
run_query($sql);
}
/********************************************************************/
function playlist_minus ($id) {
$sql = "update playlist set rank = rank-1 where id = $id;";
run_query($sql);
}
/********************************************************************/
function get_album_count() {
global $db_conn, $debug;
$sql = "select count(*) as cnt from albums;";
$rs = run_query($sql);
$obj = mysql_fetch_object($rs);
$count = $obj->cnt;
return $count;
}
/********************************************************************/
function get_artist ($id) {
$sql = "select * from artists where id = $id;";
return run_query($sql);
}
/********************************************************************/
/* Start Wedding Stuff
/********************************************************************/
function get_weddinglist_f () {
$sql = "select p.*, t.title, TIME_FORMAT(t.length, '%i:%s') as length, r.name from wedding_tracks p, tracks t, artists r where p.track_id = t.id and t.artist_id = r.id and p.speed = 'F' order by title;";
return run_query($sql);
}
/********************************************************************/
function get_weddinglist_s () {
$sql = "select p.*, t.title, TIME_FORMAT(t.length, '%i:%s') as length, r.name from wedding_tracks p, tracks t, artists r where p.track_id = t.id and t.artist_id = r.id and p.speed = 'S' order by title;";
return run_query($sql);
}
/********************************************************************/
function wedding_add ($track_id) {
$sql = "insert into playlist values (NULL, $track_id, 10, NULL);";
run_query($sql);
}
/********************************************************************/
function wedding_delete($id) {
$sql = "delete from playlist where id = $id;";
run_query($sql);
}
/********************************************************************/
function wedding_fast ($id) {
$sql = "update playlist set rank = rank+1 where id = $id;";
run_query($sql);
}
/********************************************************************/
function wedding_slow ($id) {
$sql = "update playlist set rank = rank-1 where id = $id;";
run_query($sql);
}
/********************************************************************/
/* End Wedding Stuff
/********************************************************************/
function run_query ($sql) {
global $db_conn, $debug;
debug($sql);
if ($rs = mysql_query($sql, $db_conn)) {
debug("Query Worked. [rs is $rs]");
} else {
debug("Query Failed. [rs is $rs]");
}
return $rs;
}
/********************************************************************/
/* connect() */
/* */
/* Connect to the database if we're not already connected */
/********************************************************************/
function connect () {
global $db_name, $db_conn, $db_host, $db_user, $db_pass, $debug;
if ($db_conn) {
debug("Already Connected");
return;
}
debug("Connecting");
if ($db_conn = mysql_connect($db_host, $db_user, $db_pass)) {
debug("Connected");
if (mysql_select_db($db_name, $db_conn)) {
debug("Database Selected");
return 1;
} else {
debug("Database Not Selected");
return 0;
}
} else {
debug("Not Connected");
return 0;
}
}
/********************************************************************/
/* disconnect() */
/* */
/* Drop the database connection if it's open. */
/********************************************************************/
function disconnect () {
global $db_conn, $debug;
if($db_conn) {
debug("Disconnecting");
mysql_close();
}
}
/********************************************************************/
/* fetch_object() */
/* */
/* Fetch the next object in the resuts set. */
/********************************************************************/
function fetch_object ($rs) {
debug("Fetching Object for $rs");
$obj = mysql_fetch_object($rs);
return $obj;
}
/********************************************************************/
/* debug() */
/* */
/* Print out HTMLish debugging message if debugging is on. */
/********************************************************************/
function debug ($message) {
global $debug;
if ($debug) {
echo '
' . $message . '.
'; } } if(!connect()) { echo "Can't connect."; exit; } ?>