Location: PHPKode > projects > Monocle Radio > monocle_radio-1.4.1/include/functions.php
<?
/*
 *  functions.php - Monocle Radio
 *  Copyright (C) 2005  Kurt Gallagher (hide@address.com)
 *  http://proteankungfu.com/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

/*
 *	=================================================
 *	================Utility functions================
 *	=================================================
 */

/*
 *	dump_var() - function for debugging, preserves whitespace
 */
function dump_var($thing) {
	echo '<pre>';
	var_dump($thing);
	echo '</pre>';
}

/*
 *	string format_time(int $s) - changes seconds into mm:ss
 */
function format_time($s) {
	$mins = floor($s/60);
	$secs = $s%60;
	if($secs<10) $secs = "0$secs";
	return "$mins:$secs";
}

/*
 *	string(mixed $n, int $l) - pads a number with zeros on the left
 */
function num($n,$l) {
	$n = explode('/',$n);
	$n = $n[0];
	return str_pad($n,$l,'0',STR_PAD_LEFT);
}

/*
 *	string elipsis(string $str, int $len) - Cuts a string off at len and adds an elipsis
 *		NOTE! results in a string three characters longer than len
 */
function elipsis($str, $len) {
	//Don't need to do anything if it's too short
	if(strlen($str)<=$len)
		return $str;
	
	//Add an obscure character combo, then split it there. Goddamnit.
	$tmp = explode('`|^',chunk_split($str,$len,'`|^'));
	return str_pad($tmp[0],$len+3,'.');
}

function audit($action) {
	global $cfg;
	$action = mysql_escape_string($action);
	$link = newADOConnection('mysql');
	$link->Connect($cfg['mysql_host'],$cfg['mysql_user'],$cfg['mysql_pass'],$cfg['mysql_db']);
	if(!$link->Execute("insert into user_log set uid='{$_SESSION['id']}',action='$action';")) {
		echo $link->ErrorMsg();
		die;
	}
}

function track($page) {
	global $cfg;
	if(!$cfg['mysql_enable']) {
		return;
	}
	
	$link = newADOConnection('mysql');
	$link->Connect($cfg['mysql_host'],$cfg['mysql_user'],$cfg['mysql_pass'],$cfg['mysql_db']);

	if($_SESSION['auth_code']) { //If they are logged in...
		$result = $link->Execute("select * from active_users where uid='{$_SESSION['id']}'");
		if($result->fields === false) { //Person does not exist
			$link->Execute("insert into active_users set page='$page',remote_addr='{$_SERVER['REMOTE_ADDR']}',uid='{$_SESSION['id']}'");
		} else { //Person does exist
			$link->Execute("update active_users set page='$page',remote_addr='{$_SERVER['REMOTE_ADDR']}',time=CURRENT_TIMESTAMP where uid='{$_SESSION['id']}'");
		}
	} else { //A guest, eh? We track them by IP.
		$result = $link->Execute("select * from active_users where remote_addr='{$_SERVER['REMOTE_ADDR']}'");
		if($result->fields === false) {
			$link->Execute("insert into active_users set page='$page',remote_addr='{$_SERVER['REMOTE_ADDR']}',uid='-2'");
		} else {
			$link->Execute("update active_users set page='$page',uid='-2',time=CURRENT_TIMESTAMP where remote_addr='{$_SERVER['REMOTE_ADDR']}'");
		}
	}
}

/*
 *	mixed last(array $ar) - WHY THE HELL ISN'T THIS A STANDARD FUNCTION?! Returns the last element
 *		in an array.
 */
function last($ar) {
	return $ar[count($ar)-1];
}

/*
 *	print_songs(array $songlist, string $here) - prints out a formatted listing of all
 *		songs in $songlist.
 */
function print_songs($songlist,$here='') {
	global $perms,$APP_ROOT,$cfg;
	echo <<<EOF
   <form method="post" action="$APP_ROOT/masscontrol/playlist">
   <table id="songlist" cellspacing="0">
EOF;
	if($perms>0) {
		echo <<<EOF
    <tr>
     <td colspan="2" align="right" style="border-bottom:1px solid black; background:white;"><input type="submit" value="Add selected" name="add" /></td>
     <td style="border-bottom:1px solid black; background:white;"><input type="checkbox" name="checkall" onClick="check(this.form)" /></td>
    </tr>
EOF;
	}
	foreach($songlist as $item) {
		//Directories are simple enough
		if(array_key_exists('directory',$item)) {
			//Strip out the current location
			$newdir = explode('/', $item['directory']);
			
			$temp = preg_replace("/\]/",'\]',preg_replace("/\)/",'\)',preg_replace("/\(/","\(",preg_replace("/\[/",'\[',$here))));
			$newdir = preg_replace("/$temp\//",'',$item['directory']);
			$newdir_url = urlencode(preg_replace("/#/",'```*',$newdir));

			//Get ready for putting it in the url to avoid unwanted conversions
			$dir_url = preg_replace('/\//','|',$item['directory']);
			$dir_url = urlencode(preg_replace('/#/','```*',$dir_url));

			//Get ready to post, see below
			$dir_post = urlencode($item['directory']);
			$dir_post = preg_replace('/\./','```%',$dir_post);
			
			echo <<<EOF
    <tr>
     <td colspan="2" class="sl_dir"><a href="$newdir_url/">$newdir/</a></td>\n
EOF;
	if($perms>0) {
		echo <<<EOF
     <td class="sl_controls">
      <input type="checkbox" name="add`!@#$dir_post" />
      <a href="$APP_ROOT/control/add/$dir_url/playlist">Add</a>
    </td>\n
EOF;
	echo '    </tr>';
	} else {
		echo <<<EOF
     <td colspan="3" style="border-bottom:1px solid black;"></td>
    </tr>\n
EOF;
	}
		//Tracks are more complicated
		} else if(array_key_exists('file',$item)) {
			//Store the track info
		  	$cur_song = array();
		  	$cur_song['Time'] = format_time($item['Time']);

			//Truncate these fields and add elipses if too long
			$cur_song['Artist'] = elipsis($item['Artist'],70);
			if($item['Title']) {
				$cur_song['Title'] = elipsis($item['Title'], 50);
			} else {
				$file = explode('/',$item['file']);
				if($file[count($file)-1]=='') array_pop($file);
				$file = array_pop($file);
				$cur_song['Title'] = elipsis($file,50);
			}

			//Pad the track number with zeros
		  	$cur_song['Track'] = num($item['Track'],2);

			//Make the file name ready to pass to control
			$file_url = preg_replace('/\//','|',$item['file']);
			$file_url = urlencode(preg_replace('/#/','```*',$file_url));
			
			//Make the file name ready to pass to masscontrol, spaces and dots convert to underscores, bad
			$file_post = urlencode($item['file']);
			$file_post = preg_replace('/\./','```%',$file_post);
			
			echo <<<EOF
    <tr>
     <td rowspan="2" class="sl_track">{$cur_song['Track']}</td>
     <td class="sl_title">
      <a href="$APP_ROOT/songinfo/$file_url" title="Song Information">{$cur_song['Title']}</a>
     </td>
     <td class="sl_time">{$cur_song['Time']}</td>
    </tr>
    <tr>
     <td class="sl_artist">{$cur_song['Artist']}</td>
     <td class="sl_controls">\n
EOF;
	if($perms>0) {
		echo <<<EOF
      <input type="checkbox" name="add`!@#$file_post" />
      <a href="$APP_ROOT/control/add/$file_url/playlist" title="Add Song">(+)</a>
EOF;
	}
	echo <<<EOF
     </td>
    </tr>\n
EOF;
		}
	}

	if($perms>0) {
		echo <<<EOF
    <tr>
     <td colspan="2" align="right" style="background:white;"><input type="submit" value="Add selected" name="add" /></td>
     <td style="background:white;"><input type="checkbox" name="checkall" onClick="check(this.form);" /></td>
    </tr>\n
EOF;
	}
	echo <<<EOF
   </table>
   </form>\n
EOF;
}

Return current item: Monocle Radio