Location: PHPKode > projects > Lan Action > lanaction-0.6-rc1/include/functions.php
<?php
/* $Id: functions.php,v 1.36 2004/01/25 17:50:57 pbaecher Exp $ */
/*
          LanAction - PHP/MySQL Lan orga system
        ==============================================
            (c) 2002-2004 by
               Paul Baecher       <hide@address.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.

        ==============================================

*/

class template
{
	var		$a_template = array();

	function precache_global($name)
	{
		$filename = 'templates/'.$name.'.html';
		$this->precache($name, $filename);
	}

	function precache_mod($module, $name)
	{
		$filename = 'modules/'.$module.'/templates/'.$name.'.html';
		$this->precache($name, $filename);
	}

	function precache($name, $filename)
	{
		if( !file_exists($filename) )
		{
			print 'template::precache(): could not load template "'.$filename.'" - exiting..';
			exit;
		}
		else
		{
			$this->a_template[$name] = implode('', file($filename));
		}
	}

	function get_vars($name)
	{
		if( !isset($this->a_template[$name]) )
		{
			print 'template::parse(): template "'.$name.'" is not precached - exiting..';
			exit;
		}
		else
		{
			$output = $this->a_template[$name];
			preg_match_all ( '/mod_[a-zA-Z]+::[a-zA-Z]+_[a-zA-Z_]+/i', $output, $a_matches);

			return $a_matches;
		}
	}

	function parse($name, $a_replace)
	{
		if( !isset($this->a_template[$name]) )
		{
			print 'template::parse(): template "'.$name.'" is not precached - exiting..';
			exit;
		}
		else
		{
			$output = $this->a_template[$name];
			while( list($k, $v) = each($a_replace) )
			{
				if( is_string($k) )
					$output = str_replace('{'.$k.'}', $v, $output);
			}

			return $output;
		}
	}
}

function query($query)
{
	$r = mysql_query($query);
	if( mysql_error() )
	{
		print '<pre>MySQL query error: <b>'.$query.'</b><br>';
		print 'Error: '.mysql_error().'<br><br>exiting.<br></pre>';

		exit;
	}
	return $r;
}

function r_stripslashes(&$array)
{
	while( list($k, $v) = each($array) )
	{
		if( strtoupper($k) != $k || ''.intval($k) == "$k" )
		{
			if( is_string($v) )
			{
				$array[$k] = stripslashes($v);
			}
			if( is_array($v) )
			{
				$array[$k] = r_stripslashes($v);
			}
		}
	}
	return $array;
}

function get_var($name, $type = TYPE_AUTO)
{
	$var = '';

	switch( $type )
	{
		case TYPE_GET:
			if( isset($_GET[$name]) )
				$var = $_GET[$name];
			break;
		case TYPE_POST:
			if( isset($_POST[$name]) )
				$var = $_POST[$name];
			break;
		case TYPE_AUTO:
			if( isset($_REQUEST[$name]) )
				$var = $_REQUEST[$name];
			break;
	}

	if( is_string($var) )
		$var = stripslashes($var);
	elseif( is_array($var) )
		$var = r_stripslashes($var);

	return $var;
}

function addslashes_array($input_array)
{
	if( !is_array($input_array) )
		return addslashes($input_array);

	$return_array = array();
	foreach( $input_array AS $key => $value )
	{
		$return_array[$key] =addslashes($value);
	}

	return $return_array;
}

function create_pages_string($mod_name, $action, $offset, $max_number, $entries_per_page, $extra_param = '')
{
	$nav = '';

	if ( $offset >= $entries_per_page)
	{
		$nav .= '<a href="index.php?s={s}&module='.$mod_name.'&action='.$action.'&offset='.($offset - $entries_per_page).$extra_param.'">&lt;&lt;</a>';
	}

	$nav .= '&nbsp; [&nbsp;&nbsp;';
	for ($i = 0; ($i * $entries_per_page) < $max_number ; $i++)
	{
		if ( ($i * $entries_per_page) == $offset )
		{
			$nav .= '<b>'.($i + 1).'</b>&nbsp;&nbsp;';
		}
    	else
		{
			$nav .= '<a href="index.php?s={s}&module='.$mod_name.'&action='.$action.'&offset='.($i * $entries_per_page).$extra_param.'">'.($i + 1).'</a>&nbsp;&nbsp;';
		}
	}
	$nav .= "] &nbsp;";

    if ( $max_number > ($offset + $entries_per_page) )
	{
		$nav .= '<a href="index.php?s={s}&module='.$mod_name.'&action='.$action.'&offset='.($offset + $entries_per_page).$extra_param.'">&gt;&gt;</a>';
	}

	return $nav;
}

function message($headline, $text, $link, $type=-1)
{
	global $T;

	if($link == '')
	{
		$link = 'javascript:history.go(-1)';
	}
	if($type == 0)
	{
		$text = '<font color="red">'.$text.'</font>';
	}
	if($type == 1)
	{
		$text = '<font color="green">'.$text.'</font>';
	}
	
	$T->precache_global('message');
	$message = $T->parse('message', array(
		'headline' 	=> $headline,
		'text'		=> $text,
		'link'		=> $link
		));

		return $message;
}

function header_redirect($url)
{
	header('Location: '.$url);
	exit();
}

function compress_page($output)
{
	global $HTTP_SERVER_VARS;

	if( headers_sent() )
	{
		$output .= '<!-- Warning: Could not compress page, headers already sent. -->';
		return $output;
	}
	if( isset($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING']) &&
		strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip')
		&& function_exists('gzcompress') )
	{
/*		$compressed_len = strlen(gzcompress($output, 1));
		$original_len = strlen($output);
		$output .= '<!-- gzcompress()ed, factor: '.($compressed_len/$original_len).' ('.$original_len.' -> '.$compressed_len.') -->';*/

		// http://www.p hp.net/manual/en/function.gzcompress.p hp -- comments
		header('Content-Encoding: gzip');
		$size = strlen($output);
		$crc = crc32($output);
		$output = gzcompress($output, 1);

		print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
		print substr($output, 0, strlen($output) - 4);
		print pack('V', $crc);
		print pack('V', $size);
	}
	else
	{
		print $output;
	}
}


function form_date($time, $verbose = 1)
{
	if( $time < (60 * 60 * 24) )
	{
		return "N/A";
	}

	if( date("d.m.Y", time()) == date('d.m.Y', $time) && $verbose )
	{
		return "<b>Heute</b>, " . date("H:i", $time) . " Uhr";
	}
	else
	{
		return date("d.m.Y, H:i", $time) . " Uhr";
	}

}


/* checks if user is in group */
function in_group($group)
{
	global $g_group;

	if(in_array($group, $g_group))
		return true;
	else
		return false;
}

function user_add_group($userid, $group)
{
	/*test if var are defined */
	if(!$userid || !$group)
		return false;

	$userid = (int)$userid;
	
	/* make groupid from groupname */
	if(!is_numeric($group))
	{
		$r_groupid = query("SELECT groupid FROM ".PREF."group WHERE groupname='".addslashes($group)."'");
		list($groupid) = mysql_fetch_array($r_groupid);
	}

	/* test if user is already in group */
	$r_exist = query("SELECT userid FROM ".PREF."usergroup WHERE userid='".$userid."' AND groupid='".$groupid."'");
	
	if(!mysql_num_rows($r_exist))
	{
		query("INSERT INTO ".PREF."usergroup VALUES ('".$userid."', '".$groupid."')");
		return true;
	}
	
	return false;
}

function user_remove_group($userid, $group)
{
	/*test if var are defined */
	if(!$userid || !$group)
		return false;

	$userid = (int)$userid;
	
	/* make groupid from groupname */
	if(!is_numeric($group))
	{
		$r_groupid = query("SELECT groupid FROM ".PREF."group WHERE groupname='".addslashes($group)."'");
		list($groupid) = mysql_fetch_array($r_groupid);
	}

	/* root can't be removed from group root */
	if(!($userid == 1 || $group == 'root'))
	{
		query("DELETE FROM ".PREF."usergroup WHERE userid='".$userid."' AND groupid='".$groupid."'");
		return true;
	}
	return false;
}


function upload( $form_name, $mod = null, $dest_dir = null ) 
{
	/* if var/ direcory doesn't exist */
	if( !is_writable('var/') )
		return false;

	if( $mod !== null )
	{
		$dest = 'var/' . $mod;
		/* check if we need to create new dir */
		if( !is_dir('var/' . $mod) ) 
		{
			mkdir ('var/' . $mod, 0777);
		}
		
		if( !is_writable('var/' . $mod) )
			return false;

		if( $dest_dir !== null )
		{
			$dest = 'var/' . $mod . '/' . $dest_dir;
			/* do we need to create a subdir ? */
			if( !is_dir('var/' . $mod . '/' . $dest_dir) )
			{
				mkdir ('var/' . $mod . '/' . $dest_dir, 0777);
			}
			if( !is_writable('var/' . $mod . '/' . $dest_dir) )
				return false;
		}
 		else 
		{
			$dest = 'var/' . $mod;
		}
	}
	else 
	{
		$dest = 'var';
	}
	
	/* check for multiple file uploads */
	if( is_array($_FILES[$form_name]['tmp_name']) )
	{
		/* upload multiple files */
		//echo "multiple files: ".count($_FILES[$form_name]['tmp_name'])."<br>\n";
		for( $i = 0; $i < count($_FILES[$form_name]['tmp_name']); ++$i )
		{
			//echo "File[".$i."]: ".$_FILES[$form_name]['name'][$i]."<br>\n";
			if( !empty($_FILES[$form_name]['name'][$i]) )
			{
				if( !move_uploaded_file($_FILES[$form_name]['tmp_name'][$i], $dest . '/' . $_FILES[$form_name]['name'][$i]) )
				{
					return false;
				}
			}
		}
		return true;	
	}
	else 
	{
		/* upload only one file */	
		/* copy the upload-file */
		if( !empty($_FILES[$form_name]['name']) ) 
		{
			if( !move_uploaded_file($_FILES[$form_name]['tmp_name'], $dest.'/'.$_FILES[$form_name]['name']) )
			{
				return false;
			}
			else 
			{
				if( file_exists($dest . '/' . $_FILES[$form_name]['name']) )
				{
					return true;
				}
				else 
				{
					return false;
				}
			}
		}
	}
}

function image_resize($image_src, $image_dest, $max_height, $max_width)
{
	if($image_src=="")
		return false;

	if($imagedest = '')
		$image_dest = $image_src;

	$extension = array_pop(explode('.', strtolower(basename($image_src))));

	$a_image_info = GetImageSize($image_src);
	$image_width =  $a_image_info[0];
	$image_height = $a_image_info[1];

	//resize image to max height or width
	if($image_width > $max_width && $image_width != 0)
	{
		$percent=$max_width/$image_width;
		$image_width=$max_width;
		$image_height=$percent * $image_height;
	}
	if($image_height > $max_height && $image_height != 0)
	{
			$percent=$max_height/$image_height;
			$image_height=$max_height;
			$image_width=$percent * $image_width;
	}

	if($extension=="jpg" or $extension=="jpeg")
	{
		$src_image = imagecreatefromjpeg($image_src);
	}
	else if($extension=="png")
	{
		$src_image = imagecreatefrompng($image_src);
	}
	else if($extension=="gif")
	{
		$src_image = imagecreatefromgif($image_src);
	}
	else if($extension=="bmp")
	{
		$src_image = imagecreatefromwbmp($image_src);
	}

	$dst_image = imagecreatetruecolor($image_width, $image_height);
	imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $image_width, $image_height, $a_image_info[0], $a_image_info[1]);
	imagejpeg($dst_image, $image_dest, 100);

	ImageDestroy($src_image);
	ImageDestroy($dst_image);
}

?>
Return current item: Lan Action