Location: PHPKode > projects > Helium WebMiB > HMM.WebMiB.2007.5630-2.0/functions.php
<?php

// ----------------------------------------------------------------
// Table Names
// ----------------------------------------------------------------
define(ALBUM_TABLE, 'tblalbums');
define(ALBUM_REVIEW_TABLE, 'tblalbumreview');
define(ARTIST_TABLE, 'tblartists');
define(ARTIST_DETAIL_TABLE, 'tblartist_x_detail');
define(ARTIST_RELATION_TABLE, 'tblartistrelations');
define(COUNTRY_TABLE, 'tblcountries');
define(DETAIL_TABLE, 'tbldetail');
define(GENRE_TABLE, 'tblgenres');
define(GENRE_STYLE_TABLE, 'tblgenrestyles');
define(HISTORY_TABLE, 'tblhistory');
define(LABEL_TABLE, 'tbllabels');
define(LYRICS_TABLE, 'tbllyrics');

/*
define(ALBUM_TABLE, 'tblAlbums');
define(ALBUM_REVIEW_TABLE, 'tblAlbumReview');
define(ARTIST_TABLE, 'tblArtists');
define(ARTIST_DETAIL_TABLE, 'tblArtist_x_Detail');
define(ARTIST_RELATION_TABLE, 'tblArtistRelations');
define(COUNTRY_TABLE, 'tblCountries');
define(DETAIL_TABLE, 'tblDetail');
define(GENRE_TABLE, 'tblGenres');
define(GENRE_STYLE_TABLE, 'tblGenreStyles');
define(HISTORY_TABLE, 'tblHistory');
define(LABEL_TABLE, 'tblLabels');
define(LYRICS_TABLE, 'tblLyrics');
*/

/*
define(AUTO_SYNCH_TABLE, 'tblAutoSyncpath');
define(CATEGORY_TABLE, 'tblCategories');
define(CATEGORY_DETAIL_TABLE, 'tblCategory_x_Detail');
define(COMMENT_TABLE, 'tblComments');
define(ENTRY_TABLE, 'tblEntries');
define(GENRE_STYLEMAP_TABLE, 'tblGenreStyleMap');
define(LIBRARY_TABLE, 'tblLibraries');
define(PLAYLIST_FOLDERS_TABLE, 'tblPlaylistFolders');
define(PLAYLIST_TABLE, 'tblPlaylists');
define(VERSION_TABLE, 'tblVersion');
define(VOLUME_PATHS_TABLE, 'tblVolumePaths');
define(VOLUME_TABLE, 'tblVolumes');
define(WEBRADIO_TABLE, 'tblWebradio');
define(WEBRADIO_FOLDERS_TABLE, 'tblWebradioFolders');
define(TMP_TABLE, 'tmp');
*/

// ----------------------------------------------------------------
// Type Names
// ----------------------------------------------------------------
$typenames = array(
	'Single/Maxi/EP', 
	'Album', 
	'Anthology', 
	'Bootleg', 
	'Sampler', 
	'Soundtrack', 
	'Live Performance', 
	'Compiliation', 
	'Audio Book', 
	'Sound Effects',
	'Video',
	'Digital Video'
);

// ----------------------------------------------------------------
// SQL Class
// ----------------------------------------------------------------
class sql_db
{

	var $db_connect_id;
	var $query_result;
	var $row = array();
	var $rowset = array();
	var $num_queries = 0;

	//
	// Constructor
	//
	function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
	{

		$this->persistency = $persistency;
		$this->user = $sqluser;
		$this->password = $sqlpassword;
		$this->server = $sqlserver;
		$this->dbname = $database;

		if($this->persistency)
		{
			$this->db_connect_id = @mysql_pconnect($this->server, $this->user, $this->password);
		}
		else
		{
			$this->db_connect_id = @mysql_connect($this->server, $this->user, $this->password);
		}
		if($this->db_connect_id)
		{
			if($database != "")
			{
				$this->dbname = $database;
				$dbselect = @mysql_select_db($this->dbname);
				if(!$dbselect)
				{
					@mysql_close($this->db_connect_id);
					$this->db_connect_id = $dbselect;
				}
			}
			return $this->db_connect_id;
		}
		else
		{
			return false;
		}
	}

	//
	// Other base methods
	//
	function sql_close()
	{
		if($this->db_connect_id)
		{
			if($this->query_result)
			{
				@mysql_free_result($this->query_result);
			}
			$result = @mysql_close($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}

	//
	// Base query method
	//
	function sql_query($query = "", $transaction = FALSE)
	{
		// Remove any pre-existing queries
		unset($this->query_result);
		if($query != "")
		{
			$this->num_queries++;

			$this->query_result = @mysql_query($query, $this->db_connect_id);
		}
		if($this->query_result)
		{
			unset($this->row[$this->query_result]);
			unset($this->rowset[$this->query_result]);
			return $this->query_result;
		}
		else
		{
			return ( $transaction == END_TRANSACTION ) ? true : false;
		}
	}

	//
	// Other query methods
	//
	function sql_numrows($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = @mysql_num_rows($query_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_affectedrows()
	{
		if($this->db_connect_id)
		{
			$result = @mysql_affected_rows($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_numfields($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = @mysql_num_fields($query_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fieldname($offset, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = @mysql_field_name($query_id, $offset);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fieldtype($offset, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = @mysql_field_type($query_id, $offset);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fetchrow($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$this->row[$query_id] = @mysql_fetch_array($query_id);
			return $this->row[$query_id];
		}
		else
		{
			return false;
		}
	}
	function sql_fetchrowset($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			unset($this->rowset[$query_id]);
			unset($this->row[$query_id]);
			while($this->rowset[$query_id] = @mysql_fetch_array($query_id))
			{
				$result[] = $this->rowset[$query_id];
			}
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fetchfield($field, $rownum = -1, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			if($rownum > -1)
			{
				$result = @mysql_result($query_id, $rownum, $field);
			}
			else
			{
				if(empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
				{
					if($this->sql_fetchrow())
					{
						$result = $this->row[$query_id][$field];
					}
				}
				else
				{
					if($this->rowset[$query_id])
					{
						$result = $this->rowset[$query_id][0][$field];
					}
					else if($this->row[$query_id])
					{
						$result = $this->row[$query_id][$field];
					}
				}
			}
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_rowseek($rownum, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = @mysql_data_seek($query_id, $rownum);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_nextid()
	{
		if($this->db_connect_id)
		{
			$result = @mysql_insert_id($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_freeresult($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}

		if ( $query_id )
		{
			unset($this->row[$query_id]);
			unset($this->rowset[$query_id]);

			@mysql_free_result($query_id);

			return true;
		}
		else
		{
			return false;
		}
	}
	function sql_error($query_id = 0)
	{
		$result["message"] = @mysql_error($this->db_connect_id);
		$result["code"] = @mysql_errno($this->db_connect_id);

		return $result;
	}
}
                                                                                                                                                                                                                                                                                                                                                                                
// ----------------------------------------------------------------
// Make the database connection.
// ----------------------------------------------------------------
$db = new sql_db($dbhost, $dbuser, $dbpass, $dbname, false);
if(!$db->db_connect_id)
{
	die('Could not connect to the database. Check settings in config.php');
}
define('INCLUDED', 1);

// ----------------------------------------------------------------
// Format a filesize (in bytes)
// ----------------------------------------------------------------
function format_size($bytes) 
{
	if ($bytes < 1000 * 1024)
	{
		return number_format($bytes / 1024, 2) . ' kB';
	}
	elseif ($bytes < 1000 * 1048576)
	{
		return number_format($bytes / 1048576, 2) . ' MB';
	}
	elseif ($bytes < 1000 * 1073741824)
	{
		return number_format($bytes / 1073741824, 2) . ' GB';
	}
	else
	{
		return number_format($bytes / 1099511627776, 2) . ' TB';
	}
}

// ----------------------------------------------------------------
// Format a UNIX timestamp (in seconds)
// ----------------------------------------------------------------
function format_time($s) 
{
	if ($s < 0)
	{
		$s = 0;
	}
	$t = array();
	foreach (array('60:sec', '60:min', '24:hour', '0:day') AS $x) 
	{
		$y = explode(':', $x);
		if ($y[0] > 1) 
		{
			$v = $s % $y[0];
			$s = floor($s / $y[0]);
		}
		else
		{
			$v = $s;
		}
		$t[$y[1]] = $v;
	}

	if ($t['day'])
	{
		return $t['day'] . ' days, ' . sprintf('%02d:%02d:%02d', $t['hour'], $t['min'], $t['sec']);
	}
	if ($t['hour'])
	{
		return sprintf('%d:%02d:%02d', $t['hour'], $t['min'], $t['sec']);
	}
	if ($t['min'])
	{
		return sprintf('%d:%02d', $t['min'], $t['sec']);
	}
	return sprintf('0:%02d', $t['sec']);
}

// ----------------------------------------------------------------
// Format time ago from a UNIX timestamp (in seconds)
// ----------------------------------------------------------------
function format_time_ago($s, $tl) 
{
	$sa = $sa2 = time() - $s;
	
	if ($tl > $sa)
	{
		return 1;
	}
	else
	{
		// Convert time left
		$hours = floor($sa / (60 * 60));
		$sa = $sa % (60 * 60);
			
		$mins = floor($sa / 60);
		$sa = $sa % 60;
		
		$secs = $sa;

		$ago .= ( $hours ) ? $hours . (( $hours > 1 ) ? ' hours ' : ' hour ') : '';
		$ago .= ( $mins ) ? $mins . (( $mins > 1 ) ? ' minutes ' : ' minunte ') : '';
		$ago .= ( $secs && !$mins ) ? $secs . (( $secs > 1 ) ? ' seconds ' : ' second ') : '';
	
		if ($sa2 < (30 * 60))
		{
			return  '<span style="color: red">' . $ago . ' ago</span>';
		}
		else
		{
			return $ago . ' ago';
		}		
	}
}

// ----------------------------------------------------------------
// Format an Album Rating
// ----------------------------------------------------------------
function format_rating($rating, $tracks)
{
	if (!empty($rating) && !empty($tracks))
	{
		$rating = round(($rating / 25.5) / $tracks);
	}
	else
	{
		$rating = 0;
	}
	
	return $rating;
}

// ----------------------------------------------------------------
// Format an Album Type
// ----------------------------------------------------------------
function format_type($type)
{
	global $typenames;
	
	for ($i = 0; $i < sizeof($typenames); $i++)
	{
		if ($type == ($i + 1))
		{
			$result = $typenames[$i];
		}
	}

	return $result;
}

// ----------------------------------------------------------------
// General Helium Statistics
// ----------------------------------------------------------------
function get_helium_stat($mode)
{
	global $db;

	switch($mode)
	{
		case 'total_artists':
			$sql = "SELECT COUNT(Artist_ID) AS total
				FROM " . ARTIST_TABLE . "
      			WHERE TotalItems > 0";
			break;
			
		case 'total_labels':
			$sql = "SELECT COUNT(DISTINCT publisher) AS total
				FROM " . DETAIL_TABLE . "
				WHERE publisher != ''";
			break;

		case 'total_tracks':
		case 'total_playtime':
		case 'total_played':
		case 'total_size':
			$sql = "SELECT COUNT(Detail_ID) AS total, SUM(songlength) AS playtime, SUM(PlayCounter) AS played, SUM(filesize) AS size
				FROM " . DETAIL_TABLE;
			break;

		case 'total_albums':
			$sql = "SELECT COUNT(*) AS total
				FROM " . ALBUM_TABLE;
			break;
			
		case 'total_years':
			$sql = "SELECT COUNT(ReleaseYear) AS total
				FROM " . ALBUM_TABLE . "
				WHERE ReleaseYear > 0
				GROUP BY ReleaseYear";
			break;
			
		case 'total_genres':
			$sql = "SELECT COUNT(DISTINCT genre) AS total
				FROM " . DETAIL_TABLE;
			break;
				
		case 'total_countries':
			$sql = "SELECT COUNT(*) AS total
				FROM " . COUNTRY_TABLE . "
				WHERE CountryName != ''";
			break;
	}
	
	if ( !($result = $db->sql_query($sql)) )
	{
		return false;
	}

	$row = $db->sql_fetchrow($result);

	switch ($mode)
	{
		case 'total_artists':
			return $row['total'];
			break;
		case 'total_labels':
			return $row['total'];
			break;
		case 'total_tracks':
			return $row['total'];
			break;
		case 'total_playtime':
			$playtime = format_time($row['playtime']);	
			return $playtime;
			break;
		case 'total_played':
			return $row['played'];
			break;
		case 'total_size':
			$size = format_size($row['size']);	
			return $size;
			break;
		case 'total_albums':
			return $row['total'];
			break;
		case 'total_years':
			$i = 0;
			do
			{		
				$i++;
			}
			while ( $row = $db->sql_fetchrow($result) );
			return ($i);
			break;
		case 'total_genres':
			return $row['total'];
			break;
		case 'total_countries':
			return $row['total'];
			break;
	}

	return false;
}

// ----------------------------------------------------------------
// DB size ... MySQL only
//
// This code is heavily influenced by a similar routine
// in phpMyAdmin 2.2.0
// ----------------------------------------------------------------
function dbsize()
{
	global $dbname, $db;
	
	$sql = "SELECT VERSION() AS mysql_version";
	if ($result = $db->sql_query($sql))
	{
		$row = $db->sql_fetchrow($result);
		$version = $row['mysql_version'];

		if( preg_match("/^(3\.23|4\.|5\.)/", $version) )
		{
			$dbname = ( preg_match("/^(3\.23\.[6-9])|(3\.23\.[1-9][1-9])|(4\.)|(5\.)/", $version) ) ? "`$dbname`" : $dbname;

			$sql = "SHOW TABLE STATUS 
				FROM " . $dbname;
			if ($result = $db->sql_query($sql))
			{
				$tabledata_ary = $db->sql_fetchrowset($result);

				$dbsize = 0;
				for ($i = 0; $i < sizeof($tabledata_ary); $i++)
				{
					if ( $tabledata_ary[$i]['Type'] != "MRG_MyISAM" )
					{
						if ( strstr($tabledata_ary[$i]['Name'], 'tbl') )
						{
							$dbsize += $tabledata_ary[$i]['Data_length'] + $tabledata_ary[$i]['Index_length'];
						}
					}
				}
			}
			else
			{
				$dbsize = 'N/A';
			}
		}
		else
		{
			$dbsize = 'N/A';
		}
	}
	else
	{
		$dbsize = 'N/A';
	}
	
	if ( is_integer($dbsize) )
	{
		if( $dbsize >= 1048576 )
		{
			$dbsize = sprintf("%.2f MB", ( $dbsize / 1048576 ));
		}
		else if( $dbsize >= 1024 )
		{
			$dbsize = sprintf("%.2f KB", ( $dbsize / 1024 ));
		}
		else
		{
			$dbsize = sprintf("%.2f Bytes", $dbsize);
		}
	}

	return $dbsize;
}

?>
Return current item: Helium WebMiB