Location: PHPKode > projects > WB News > includes/function.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v1.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2005                          #||
||#     Created: 31st July 2005                          #||
||#     Filename: function.php                           #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: function.php,v 1.2.2.5.2.1 2008/07/14 11:09:30 pmcilwaine Exp $
 */

/**
 *	Builds the absolute template path
 *
 *	@param string $filename
 */
function BuildPath( $filename )
{
	global $config, $auth;

	if ( is_null( $config ) && !is_object( $auth ) )
	{
		return NULL;
	}

	if ( !file_exists($config["installdir"] . "/templates/" . $config["themepath"] . "/$filename")
		&& !$auth->has_perm( "canconfig" ) )
	{
		return FALSE;
	}
	else if ( !file_exists($config["installdir"] . "/templates/" . $config["themepath"] . "/$filename")
		&& $auth->has_perm( "canconfig" ) )
	{
		if ( function_exists( "Message" ) )
		{
			Message( "Using 'default' theme, you may experience issues with the admin. We suggest you
				update the theme back to 'default' or another working template theme", TRUE );
		}

		return $config["installdir"] . "/templates/default/$filename";
	}

	return $config["installdir"] . "/templates/" . $config["themepath"] . "/$filename";
}

/**
 *	Returns an array of information about the user, returns false if they are
 *	not logged in
 *
 *	@param int $session
 *	@return mixed
 */
function userinfo( $userid )
{
	global $DB, $permissions;

	$DB2 = $DB;

	$uid = intval( $userid );
	$cond = array();
	$cond[] = "\"userid\"=$uid";
	$cond[] = "u.\"usergroupid\"=g.\"usergroupid\"";
	$cond = join( " AND ", $cond );

	$tables = array(
		TBL_USERS . " u",
		TBL_UGROUPS . " g"
		);
	
	$ids = $DB2->ListBy( $tables, $cond, array("*") );
	
	return array(
		"userid" => $ids[0]["userid"],
		"groupid" => $ids[0]["usergroupid"],
		"username" => $ids[0]["username"],
		"group_bit" => $ids[0]["group_bit"]
		);
}

/**
 * Returns a date specified by the timezone if set, if no timezone then use the server timezone
 *
 * @param string $format
 * @param int $timestamp
 * @param string $timezone
 */
function tz_date( $format = NULL, $timestamp )
{
	global $config;
	
	$timezone = $config["timezone"];
	if ( NULL == $timezone )
	{
		/** server default **/
		$timezone = date("O");
		$tmp_tz = substr( $timezone, 0, 3 );
		$timezone = $tmp_tz . (substr( $timezone, 3, 1 ) == 5 ? ".5" : "" );
	}
	
	if ( NULL == $format )
	{
		$format = "j/n/Y g:i:sa";
	}

	return gmdate( $format, $timestamp + ( 3600 * $timezone ) );
}

function filter( $string )
{
	global $config;
	$words = explode( " ", $config["badwords"] );

	foreach ( $words as $word )
	{
		$len = strlen( $word );
		$replacement = substr( str_repeat( $config["badwords_replacement"], $len ), 0, $len );
		$string = str_replace( $word, $replacement, $string );
	}

	return $string;
}

/**
 *      Redirects the user to the $url specified
 *
 *      @param string $url
 *      @return void
 */
function redirect( $url )
{
        header( "location: $url" );
        return;
}

/**
 *	Makes posted data SQL ready
 *
 *	@param string $var
 *	@param string $type
 *	@param array $postdata
 */
function sanitize_post( $var, $type=NULL, $postdata=array() )
{
	if ( $type == NULL )
	{
		$type = "single";
	}

	if ( count($postdata) == 0 )
	{
		$postdata = $_POST;
	}

	if ( !isset($postdata[$var]) )
	{
		return FALSE;
	}

	switch ( $type )
	{
	case "rte":
		$postdata[$var] = stripslashes(trim( $postdata[$var] ));
		break;
	case "multiline":
		$postdata[$var] = stripslashes(trim( $postdata[$var] ));
		break;
	case "single" :
	default :

		$postdata[$var] = stripslashes(trim( $postdata[$var] ));
		$index = strpos( $postdata[$var], "\n" );

		if ( FALSE !== $index )
		{
			$postdata[$var] = substr( $postdata[$var], 0, $index );
		}
		
		break;
	}

	return $postdata[$var];
}

/**
 *	Makes posted data HTML ready (redisplay data in form)
 *
 *	@param string $var
 *	@param string $type
 *	@param array $postdata
 */
function sanitize_post_html( $var, $type=NULL, $postdata=array() )
{
	if ( $type == NULL )
	{
		$type = "single";
	}

	if ( count($postdata) == 0 )
	{
		$postdata = $_POST;
	}

	if ( !isset($postdata[$var]) )
	{
		return FALSE;
	}

	switch ( $type )
	{
	case "rte" :
		$postdata[$var] = trim( $postdata[$var] );
		break;
	case "single" :
	default :
		$postdata[$var] = htmlspecialchars(trim( $postdata[$var] ));
		break;
	}
	
	return $postdata[$var];
}

function merge_array( $a, $b )
{
	$ret = array();

	foreach ( $a as $k => $v )
	{
		$ret[ $k ] = $v;
	}
	
	foreach ( $b as $k => $v )
	{
		$ret[ $k ] = $v;
	}

	return $ret;
}

/**
 * Function is used for debugging purposes, allows any number of parameters passed through
 * @param $var any
 */
function plain_print_r( $var )
{
	header( "Content-type: text/plain" );

	if ( is_string($var) || is_null($var) || is_bool($var) )
	{
		var_dump($var);	
	}
	else
	{
		print_r( $var );
	}
	
	exit;
}

/**
 *	Maximum of 3 args
 *	URL
 *	
 *	Array (Query String)
 *	URL
 *
 *	@return string
 */
function make_url()
{
	$url = "";
	switch ( func_num_args() )
	{
	case 0 :
	
		if ( defined( "ENABLE_REWRITE" ) && TRUE == ENABLE_REWRITE )
		{
			return $_SERVER["REQUEST_URI"];
		}
	
		$query = $_SERVER["QUERY_STRING"];
		$path = $_SERVER["REQUEST_URI"];

		$url = $path;
		if ( "" != $query )
		{
			$url .= "?" . $query;
		}
		break;
	case 1 :
		$query = func_get_arg(0);
		$qs = array();
		foreach ( $query as $key => $value )
		{
			if ( FALSE == $value )
			{
				continue;
			}
			
			$qs[] = "$key=$value";
		}

		$url = $_SERVER["REQUEST_URI"] . ( $qs ? "?" . join( "&", $qs ) : NULL );
		
		break;
	case 2 :
		$url = func_get_arg(1);
		$query = func_get_arg(0);

		if ( is_array($query) && count($query) != 0 )
		{
			$url .= "?";
			$qs = array();
			foreach ( $query as $key => $value )
			{
				$qs[] = "$key=$value";
			}
			$url .= join( "&", $qs );
		}
		break;
	case 3 :
		$url = func_get_arg(1);
		$url = NULL == $url ? $_SERVER["REQUEST_URI"] : $url;
		$query = func_get_arg(0);

		if ( NULL != $_SERVER["QUERY_STRING"] )
		{
			$qsa = explode( "&", $_SERVER["QUERY_STRING"] );
			
			$qs = array();
			foreach ( $qsa as $q )
			{
				list( $k, $v ) = explode( "=", $q );
				$qs[ $k ] = $v;
			}
			
			$query = array_merge( $qs, $query );
		}

		if ( is_array($query) )
		{
			$url .= "?";
			$qs = array();
			foreach ( $query as $key => $value )
			{
				$qs[] = "$key=$value";
			}
			$url .= join( "&", $qs );
		}
		break;
	default:
	 	return NULL;
	}

	return $url;
}

/**
 *	a htmlspecialchar wrapper to make_url
 */
function make_url_html()
{
	switch ( func_num_args() )
	{
	case 0 :
		return htmlspecialchars(make_url());
	case 1 :
		return  htmlspecialchars(make_url( func_get_arg(0) ));
	case 2 :
		$args = array();
		$args[] = func_get_arg(0);
		$args[] = func_get_arg(1);
		return  htmlspecialchars(make_url( $args[0] , $args[1] ));
	case 3 :
		$args = array();
		$args[] = func_get_arg(0);
		$args[] = func_get_arg(1);
		return  htmlspecialchars(make_url( $args[0] , $args[1], $args[2] ));
	default :
		return NULL;
	}
}

function Submit()
{
	$submit = array_values(array_filter( array_keys($_POST), create_function('$value', 'return preg_match( "/submit_.+?/i", $value );') ));
	if ( count($submit) == 0 )
	{
		return FALSE;
	}

	$submit = $submit[0];

	return substr( $submit, 7 );
}

return;

?>
Return current item: WB News