Location: PHPKode > projects > Fusion Board > v_1-0-0/includes/functions.inc.php
<?php
/**********************************************************************/
/*                 Powered By Fusion Board V 1.0.0
/**********************************************************************/
/* Author:               Keven Brochu
/* Copyright:            © 2004/2005 - KevBrok, The Dynamic Fusion Team
/* Web:                  http://www.dynamic-fusion.net
/* E-mail:               hide@address.com
/* Begin:                10 Aug 2004 03:45 GMT
/* Last Update:          17 Feb 2006 16:52 GMT
/* Fusion Board Version: 1.0.0
/* File Version:         1.0.0
/**********************************************************************/
/* Visit Dynamic-Fusion website for details, updates, support and more.
/**********************************************************************/
/* classes/functions.class.php
/* General Fusion Board functions.
/**********************************************************************/

if ( !defined('FILE_ACCESS') ) exit;

/*
* Add colors in SQL queries
*/
function color_sql( $query, $put_entities = TRUE )
{
	if ( $put_entities )
	{
		$query = htmlentities( $query, ENT_QUOTES ); // Put HTML entities
	}

	$query = preg_replace( '#([\s<>=][^\\\])(&\#039;&\#039;|&\#039;.*?(([^\\\]{1,2}\\\\\\\|\\\&\#039;)&\#039;|[^\\\]&\#039;))#s', '\\1<span style="color: #999;">\\2</span>', $query );
	$query = preg_replace( '#([a-zA-Z_][a-zA-Z0-9_]*[\s]|\)\s)(as)([\s][a-zA-Z0-9_])#i', '\\1<strong><em>\\2</em></strong>\\3', $query );
	$query = preg_replace( '#(\s)(into|from|where|and|or|by|limit|not)(\s)#i', '\\1<strong style="color: #F90;">\\2</strong>\\3', $query );
	$query = preg_replace( '#(\s|^)(create|insert|update|select)(\s)#i', '\\1<strong style="color: #090;">\\2</strong>\\3', $query );
	$query = preg_replace( '#(\s|^)(delete|truncate|drop)(\s)#i','\\1<strong style="color: #F00;">\\2</strong>\\3', $query );
	$query = preg_replace( '#(\s)((left\s|right\s|inner\s|natural\s|straight_)join|join|[oi]n|set|values|union)([\s\(])#i', '\\1<strong style="color: #3366FF;">\\2</strong>\\4', $query );
	$query = preg_replace( '#(\s)(group|order|desc|asc|distinct)([\s,]|$)#i', '\\1<strong style="color: #90C;">\\2</strong>\\3', $query );
	$query = preg_replace( '#([\s\.,])(count|max|min|avg|sum|concat|substring|lcase|length|lower|ucase|upper|ascii|char|field|[lr]?trim)(\s*)\(([a-zA-Z0-9_\.,\s]*|\*)\)(\s|$)#is', '\\1<strong style="color: #C60">\\2</strong>\\3(\\4)\\5', $query );

	return $query;
}


/*
* If we are running under PHP 4.3.0, we will add our own implementation of html_entity_decode()
*/
if ( !function_exists('html_entity_decode') )
{
	function html_entity_decode( $text, $quotes = ENT_COMPAT )
	{
		static $trans_tbl;

		/*
		* If the translation table is not defined, we will define it
		*/
		if ( !isset($trans_tbl) )
		{
			$trans_tbl = get_html_translation_table( HTML_ENTITIES, $quotes );

			/*
			* The get_html_translation_table function set the signle quote (')
			* remplacement as &#39;... But, in htmlentities(), it's &#039;
			*/
			if ( $quotes == ENT_QUOTES )
			{
				$trans_tbl['\''] = '&#039;';
			}

			$trans_tbl = array_flip($trans_tbl);
		}

		return strtr( $text, $trans_tbl );
	}
}


/*
* Replace HTML entites by ASCII code entities (Usefull for RSS)
* *** The string should be already encoded with htmlentities() ***
*/
function ascii_entities( $str = '', $quotes = ENT_COMPAT )
{
	static $ascii_trans_tbl;

	/*
	* If the translation table is not defined, we will define it
	*/
	if ( !isset($ascii_trans_tbl) )
	{
		$ascii_trans_tbl_tmp = get_html_translation_table( HTML_ENTITIES, $quotes );

		/*
		* The get_html_translation_table function set the signle quote (')
		* remplacement as &#39;... But, in htmlentities(), it's &#039;
		*/
		if ( $quotes == ENT_QUOTES )
		{
			$ascii_trans_tbl_tmp['\''] = '&#039;';
		}

		$ascii_trans_tbl_tmp = array_flip($ascii_trans_tbl_tmp);

		/*
		* Replace all with ASCII code
		*/
		$ascii_trans_tbl = array();
		foreach ( $ascii_trans_tbl_tmp as $html => $char )
		{
			$ascii_trans_tbl[$html] = '&#' . ord($char) . ';';
		}
	}

	return strtr( $str, $ascii_trans_tbl );
}

/*
* Change number format
*/
function format_number( $number, $decimal = 0 )
{
	if ( !is_numeric($number) )
	{
		return $number;
	}

	global $config;

	return @number_format( $number, $decimal, $config['NB_FORMAT_DEC_POINT'], $config['NB_FORMAT_THOUSANDS_SEP'] );
}


/*
* Disallow spam Bots to see e-mail adresses in the source code
*/
function email_encode( $email = '' )
{
	$new_email = '';
	$chars_count = strlen($email);

	for ( $i = 0; $i < $chars_count; $i++ )
	{
		$new_email .= '&#' . ord($email[$i]) . ';';
	}

	return $new_email;
}


/*
* Add Session ID in each URL if needed
*/
function sessurl( $url, $frames = FALSE, $link = TRUE, $keep_sid = TRUE, $add_after = '' )
{
	global $SESSION;

	/*
	* Build the URL
	*/
	if ( preg_match( '#\?$#', $url ) || preg_match( '#&amp;$#', $url ) )
	{
		/*
		* If the URL ends with ? or &, we will simply add $frames
		*/
		$finalurl = ( $frames === FALSE ) ?
			$url : $url . $frames;

		$amp = TRUE;
	}
	elseif ( !strpos( $url, '?' ) )
	{
		/*
		* No ? in the URL, we will add it
		*/
		$finalurl = ( $frames === FALSE ) ?
			$url : $url . '?' . $frames;

		$amp = ( $frames !== FALSE );
	}
	else
	{
		/*
		* Else, simple add &$frames
		*/
		$finalurl = ( $frames === FALSE ) ?
			$url : $url . '&amp;' . $frames;

		$amp = TRUE;
	}

	/*
	* Add session ID in the URL if needed
	*/
	if ( $SESSION -> use_url && $keep_sid && $SESSION -> sessid != '0' )
	{
		$finalurl = ( $amp ) ?
			$finalurl . '&amp;session=' . $SESSION -> sessid : $finalurl . '?session=' . $SESSION -> sessid;
	}

	/*
	* If it's not a link, we will remove &amp; and replace it by & (for redirect, for eg)
	*/
	if ( !$link )
	{
		$finalurl = str_replace( '&amp;', '&', $finalurl );
	}

	return $finalurl . $add_after;
}


/*
* Change a timestamp to a date
*/
function create_date( $timestamp = 100000, $syntax = 'd F Y, H:i', $gmt = 0, $translate = TRUE )
{
	global $SESSION, $lang, $config;

	/*
	* Microsoft Windows does not support dates prior to January 1st, 1970, 00:00...
	*/
	if ( $timestamp < 100000 )
	{
		$timestamp = 100000;
	}

	$time = @gmdate( $syntax, $timestamp + ( 3600 * ( $gmt + $SESSION -> infos['gmt_adjust'] + $config['GMT_ADJUST'] ) ) );

	/*
	* Translate days and months if needed
	*/
	return ( $translate ) ? strtr( $time, $lang['dates_t'] ) : $time;
}


/*
* This function create a Yes/No radio button (for forms) with a label.
* 1 = yes     0 = no
*/
function create_yes_no_radio( $name = '', $selected = 1 )
{
	global $lang;

	$yes_sel = ( $selected == 1 ) ?
		' checked="checked"' : '';

	$no_sel = ( $selected == 0 ) ?
		' checked="checked"' : '';

	return '<input type="radio" name="' . $name . '" id="' . $name . '_1" class="checkbox" value="1"' . $yes_sel . ' /><label for="' . $name . '_1"> ' . $lang['yes'] .
		'</label>&nbsp;     &nbsp;<input type="radio" name="' . $name . '" id="' . $name . '_0" class="checkbox" value="0"' . $no_sel . ' /><label for="' . $name . '_0"> ' . $lang['no'] . '</label>';
}


/*
* This function create a select box with all available GTM
*/
function create_gmt_box( $name = '', $GMT = 0 )
{
	global $lang;

	$tzl = ''; // Time string

	for ( $num = -12;; $num++)
	{
		$tzs = ( $GMT == $num ) ?
			' selected="selected"' : '';

		$tzl .= '                    <option value="' . $num . '"' . $tzs . '>' . $lang['tz_GMT' . $num] . "</option>\n";

		/*
		* It's a "X:30" GMT
		*/
		if ( $num == -3 || $num == 3 || $num == 5 || $num == 6 || $num == 9 )
		{
			$tzs2 = ( $GMT == $num . '.5' ) ?
				' selected="selected"' : '';

			$tzl .= '                    <option value="' . $num . '.5"' . $tzs2 . '>' . $lang['tz_GMT' . $num . '.5'] . "</option>\n";
		}

		/*
		* End loop
		*/
		if ( $num >= 13 )
		{
			break;
		}
	}

	return '<select name="' . $name . "\">\n$tzl                  </select>";
}


/*
* Check if this user is allowed to show a forum.
* Global perms are based on a letter code.
* Advanced perms are just selected in the DB
*/
function check_forum_view_perms( $fvars )
{
	global $SESSION;

	/*
	* User is offline. Check if this forum is available for guests.
	*/
	if ( $SESSION -> level == 'guest' && strpos( $fvars['read_perms'], 'g' ) !== FALSE )
	{
		return TRUE;
	}
	else if ( !USER_OFFLINE )
	{
		/*
		* Special perms ??
		*/
		if ( $fvars['can_view_forum'] == '0' )
		{
			return FALSE;
		}
		else if ( $fvars['can_view_forum'] == '1' )
		{
			return TRUE;
		}

		/*
		* Else, check global perms. User is innactive
		*/
		else if ( $SESSION -> level == 'inn' && strpos( $fvars['read_perms'], 'v' ) !== FALSE )
		{
			return TRUE;
		}

		/*
		* Normal user
		*/
		else if ( $SESSION -> level == 'memb' && strpos( $fvars['read_perms'], 'u' ) !== FALSE )
		{
			return TRUE;
		}

		/*
		* It's a moderator
		*/
		else if ( $SESSION -> level == 'mod' && strpos( $fvars['read_perms'], 'm' ) !== FALSE )
		{
			return TRUE;
		}

		/*
		* Here, an administrator.
		*/
		else if ( ( $SESSION -> level == 'admin' || $SESSION -> level == 'root' ) && strpos( $fvars['read_perms'], 'a' ) !== FALSE )
		{
			return TRUE;
		}


	}

	/*
	* If PHP parse this line, it's because the user is not allowed to show this forum.
	* So, return FALSE;
	*/
	return FALSE;
}


/*
* Get Fusion Board copyright
*/
function get_c( $ctplvar, $tplname = '' )
{
	/*
	* > YOU ARE NOT ALLOWED TO REMOVE OR EDIT THIS NOTICE
	* ***********************************************************
	* You can't change this line. Fusion Board is free.
	* Keep a copyright is recognition towards the authors.
	* If you remove/edit this notice, you will not be able
	* to receive any support from Dynamic-Fusion any more,
	* and that will be regarded as a violation of the copyright.
	* ***********************************************************
	* > Remember: You didn't create Fusion Board...
	*/

	global $template_id, $$ctplvar;

	if ( !strpos( $$ctplvar -> tplcode, ':copyright(' . $template_id . '):' ) )
	{
		error(':copyright: tag is missing in the ' . $tplname . ' template');
	}

	$$ctplvar -> set_var( 'copyright', 'Powered by &copy; Fusion Board ' . FB_VERSION . ' - The <a href="http://www.dynamic-fusion.net/" title="Dynamic Fusion">Dynamic Fusion</a> Team' );

	return TRUE;
}


/*
* Create cookie with Forum configs
*/
function create_cookie( $name, $value = '', $expire = NULL )
{
	global $sql_infos, $config;

	if ( $expire !== NULL )
	{
		$expire = time() + $expire;
	}

	@setcookie( DB_PREFIX . $config['COOKIE_NAME'] . $name, $value, $expire, $config['COOKIE_PATH'], $config['COOKIE_DOMAIN'], $config['COOKIE_SECURE'] );
}


/*
* Get cookie
*/
function get_cookie( $name )
{
	global $sql_infos, $config, $_COOKIE;

	return ( isset( $_COOKIE[ DB_PREFIX . $config['COOKIE_NAME'] . $name ] ) ) ?
		 $_COOKIE[ DB_PREFIX . $config['COOKIE_NAME'] . $name ] : FALSE;
}


/*
* Delete cookie(s)
*/
function delete_cookie( $cookie )
{
	global $sql_infos, $config, $_COOKIE;

	if ( is_array($cookie) )
	{
		foreach ( $cookie as $name )
		{
			@setcookie( DB_PREFIX . $config['COOKIE_NAME'] . $name, '', 10, $config['COOKIE_PATH'], $config['COOKIE_DOMAIN'], $config['COOKIE_SECURE'] );
		}
	}
	else
	{
		@setcookie( DB_PREFIX . $config['COOKIE_NAME'] . $cookie, '', 10, $config['COOKIE_PATH'], $config['COOKIE_DOMAIN'], $config['COOKIE_SECURE'] );
	}

	return TRUE;
}


/*
* Easy way to get a percentage...
*/
function make_percentage( $number = 0, $total = 100, $format = 0 )
{
	$percentage = $number * 100 / $total;

	/*
	* Formatting ?
	*/
	if ( $format <> 0 )
	{
		if ( $format == -1 )
		{
			return round($percentage); // Round this
		}
		else
		{
			return format_number( $percentage, $format );
		}
	}

	return $percentage;
}


/*
* Generate the page list.
*/
function create_pagelist( $num, $pp, $current, $add = '', $page_jump = TRUE )
{
	global $lang, $skindir;

	$count_pages = ceil( $num / $pp );

	/*
	* Only one page ? No pagination
	*/
	if ( $count_pages == 1 )
	{
		return;
	}

	/*
	* Generate first and previous page links
	*/
	$return = '<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=1' ) . '" title="&lt;&lt;"><strong>&lt&lt;</strong></a> &nbsp;';
	if ( $current > 1 )
	{
		$return .= '<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . ( $current - 1 ) ) . '" title="&lt;"><strong>&lt;</strong></a> &nbsp;';
	}

	if ( $add != '' )
	{
		$add = '&amp;' . $add;
	}

	/*
	* More than 10 pages?
	*/
	if ( $count_pages > 10 )
	{
		$maxend = $count_pages - 3;

		/*
		* Show the 3 first pages
		*/
		for ( $i = 1; $i <= 3; $i++ )
		{
			if ( $i <> 1 )
			{
				$return .= ', ';
			}

			$return .= ( $i <> $current ) ?
				'<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . $i ) . '" title="' . $i . '">' . $i . '</a>' : '[<strong>' . $i . '</strong>]';

			if ( $i == $current && $current == 3 )
			{
				$return .= ', <a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=4' ) . '" title="4">4</a>';
			}
		}

		/*
		* The current page is in the middle...
		*/
		if ( $current >= 4 && $current <= $maxend )
		{
			$expl = TRUE;

			/*
			* If the current page is beetween 3 and 5, show 3 pages after and add  ... later
			*/
			if ( $current > 4 )
			{
				if ( $current - 2 > 3 )
				{
					$return .= ' ... ';
				}
				else
				{
					$return .= ', ';
				}

				$return .= '<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . ( $current - 1 ) ) . '" title="' . ( $current - 1 ) . '">' . ( $current - 1 ) . '</a>, ';
			}

			/*
			* Show Current page
			*/
			$return .= ' [<strong>' . $current . '</strong>]';

			if ( $current < $maxend )
			{
				$return .= ', <a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . ( $current + 1 ) ) . '" title="' . ( $current + 1 ) . '">' . ( $current + 1 ) . '</a>';
			}

			/*
			* If the current page is close to the max page
			*/
			if ( $current + 2 <= $maxend )
			{
				$return .= ' ... ';
			}
			else
			{
				$return .= ', ';
			}
		}
		else
		{
			/*
			* Simply add  ...
			*/
			$return .= ' ... ';
			$expl = FALSE;
		}

		for ( $i = $maxend + 1; $i <= $count_pages; $i++ )
		{
			if ( $i == $current && $current == $maxend + 1 )
			{
				$return .= '<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . $maxend ) . '" title="' . $maxend . '">' . $maxend . '</a>, ';
			}

			/*
			* Add a comma if the current page is close to the last page
			*/
			if ( $expl && ( $i <> $maxend + 1 || $current < 4 ) )
			{
				$return .= ', ';
			}

			$return .= ( $i <> $current ) ?
				'<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . $i ) . '" title="' . $i . '">' . $i . '</a>' : '[<strong>' . $i . '</strong>]';

			$expl = TRUE;
		}
	}
	else
	{
		/*
		* 10 pages or less. Show all pages. Easy.
		*/
		for ( $i = 1; $i <= $count_pages; $i++ )
		{
			if ( $i <> 1 )
			{
				$return .= ', ';
			}

			$return .= ( $i <> $current ) ?
				'<a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . $i ) . '" title="' . $i . '">' . $i . '</a>' : '[<strong>' . $i . '</strong>]';
		}
	}

	/*
	* Generate next and last page links
	*/
	if ( $current < $count_pages )
	{
		$return .= '&nbsp; <a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . ( $current + 1 ) ) . '" title="&gt;"><strong>&gt;</strong></a>';
	}

	$return .= '&nbsp; <a href="' .  sessurl( INDEX_FILE . EXT, $add . '&amp;page=' . $count_pages ) . '" title="&gt;&gt;"><strong>&gt;&gt;</strong></a>';

	/*
	* Need to add the javascript page jump ?
	*/
	if ( $page_jump )
	{
		global $SESSION;
		$return = '<img src="' . $skindir . '/folders/page_jump.gif" style="border: 0; cursor: pointer;" onclick="page_jump(\'' . sessurl( INDEX_FILE . EXT, $add, TRUE, FALSE ) . '\', ' . $count_pages . ', \'' . $current . '\', \'' . $SESSION -> sessid . "')\" alt=\"{$lang['pages']}:\" title=\"{$lang['page_jump_icon']}\" /> &nbsp; $return";
	}

	return $return;
}

/*
* Redirect
*/
function redirect( $url, $path = TRUE )
{
	global $SQL, $config;

	/*
	* Close DB Connection
	*/
	@$SQL -> close();

	if ( $path )
	{
		$url = $config['BOARD_URL'] . $url;

		/*
		* We are using SSL and the user cannot use it ?
		*/
		if ( $url[4] == 's' && empty($_SERVER['HTTPS']) )
		{
			unset($url[4]);
		}
	}
	else if ( !$path && !preg_match( '#^((https?|ftp|news)://|about:)#i', $url ) )
	{
		$url = 'http://' . $url;
	}

	/*
	* Insecure URL ...?
	*/
	if ( preg_match( "#(\n|\r|\t|\x0B)#i", urldecode($url) ) )
	{
		error('Unable to redirect: Insecure URL');
	}

	/*
	* Redirect
	*/
	@header('Location: ' . $url);

	/*
	* The browser/server is too old and does not support Location redirect ?
	*/
	@header('Refresh: 0; URL=' . $url);

	/*
	* For.. crap browsers/servers (Or if headers are already defined... It should not happen)
	*/
	exit( '<html><head><meta http-equiv="refresh" content="0; url=' . $url . '"></head><body><a href="' . $url . '">&gt;&gt;&gt;&gt;&gt;&gt;</a></body></html>' );
}

/*
* Generate hased MD5 password with a salt
*/
function hash_pass( $pass = '', $salt = '' )
{
	return md5( md5($pass) . $salt );
}

/*
* Generate a salt for hash_pass() function
*/
function generate_hash_salt()
{
	mt_srand( ( double ) microtime() * 1000000 );

	$salt = chr( mt_rand(33, 126) );

	for ( $i = 1; $i < 10; $i++ )
	{
		$salt .= chr( mt_rand(32, 126) );
	}

	return $salt;
}

/*
* Remove slashes added in strings if magic_quotes_gpc is ON
*/
function unprotect_gpc($str)
{
	return ( MAGIC_QUOTES ) ? stripslashes($str) : $str;
}

/*
* Add slashes in strings if magic_quotes_gpc is OFF
*/
function protect_gpc($str)
{
	return ( !MAGIC_QUOTES ) ? addslashes($str) : $str;
}


/*
* Show a critical error Message
*/
function error( $msg )
{
	global $SQL;

	@$SQL -> close(); // Close SQL connection

	$msg = htmlentities( $msg, ENT_QUOTES );
	exit( '<span style="font-family: verdana, thaoma, arial"><strong>FUSION BOARD ERROR:</strong> <br />' . "\n" . $msg );
}

?>
Return current item: Fusion Board