Location: PHPKode > projects > Cynus > cynus/main_func.php
<?php
/**
 * @file main_func.php -- Provides main functions.
 * @Id $Id: main_func.php,v 1.44 2004/07/30 13:08:51 jason Exp $
 *
 * Cynus - a web-based content manager
 * Copyright (C) 2003 Brett and Jason Profitt
 *
 * 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.
 *
 */
/**************************************
Set Page Title: bool set_page_title(string $title)
Used to set the title of a page. Once the page title is
set, it cannot be changed. The function returns
TRUE if it sets the title, and FALSE if it did not.
Whenever do_header() is called, this is put in the
<title>$title</title> tags
**************************************/

cynus_debug ("Loaded main_func.php", 3);

function set_page_title($title) {
	global $config;
	
	if($config['page_title'] == "") {
		$config['page_title']=$title;
		cynus_debug ("Set page title to $title.");
		return TRUE;
	}
	
	cynus_debug ("Failed to set page title to $title.");
	return FALSE;
}


/**************************************
Add Head Info: add_head_info(string $info)
Allows for information to be added inbetween the <head></head>
tags in do_header() in case there is something a module would
like to have added in there.
**************************************/
function add_head_info($info) {
	global $config;
	
	$config['head_extra'] .= $info;
}


/**************************************
Do Header: do_header()
This prints out the general header information. Included
is the top of the main table where the content should go.
**************************************/
function do_header() {
	global $config;
	
	# letting the debugger know that the header has been set
	# and it's ok to start printing debug info instead of storing it.
	define (HEADER_SENT, 1);
	
	# if we have any debug info that was sent before the header, 
	# send it out to the dbg window now
	if ($config['pre_header_dbg']) {
		$dbg_print = $config['pre_header_dbg'];
		# to avoid confusing.
		unset ($config['pre_header_dgb']);
	}

	print <<<___eofh
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>$config[district] Webpage Administration: $config[page_title]</title>
<link rel="stylesheet" type="text/css" href="$config[url_base_dir]/themes/$config[style]/style.css" />
$config[head_extra]
</head>

<body>
$dbg_print
<div class="logo-area">
<a href="$config[url_base_dir]/"><img src="$config[url_base_dir]/themes/$config[style]/images/crest_logo.png" class="logo" /></a>
$config[district] Webpage Administration
</div>

<table cellspacing="0" cellpadding="0" class="table">
	<tr>
		<td class="table-top-left-corner">&nbsp;</td>
		<td class="table-top">&nbsp;</td>
		<td class="table-top-right-corner">&nbsp;</td>
	</tr>
	<tr>
		<td class="table-left">&nbsp;</td>
		<td class="table-content">\n
___eofh;
}



/**************************************
Do Footer: do_footer()
This prints out the general footer. Included is the end
of the main table where the content goes, and anything else
that should go in the footer.
**************************************/
function do_footer() {
	global $config;
	
	cynus_debug ("Printing footer.  End of output (Should be!)", 3);
	if ($config['debug']==1) {
		$debug=<<<___eofh
		<br /><br />
		<div class="debug">
		Debug:<br />
		Request URI: {$_SERVER['REQUEST_URI']}<br>
		<a href="$config[url_base_dir]/viewsource.php?page=$_SERVER[SCRIPT_NAME]">View Source</a>
		
		</div>
___eofh;
	}

	
	print <<<___eofh
</td>
		<td class="table-right">&nbsp;</td>
	</tr>
	<tr>
		<td class="table-bottom-left-corner">&nbsp;</td>
		<td class="table-bottom">&nbsp;</td>
		<td class="table-bottom-right-corner">&nbsp;</td>
	</tr>
</table>
<div class="footer"><a href="http://cynus.sourceforge.net/">Cynus v$config[version]</a> / 
Maintained by <a href="mailto:$config[webmaster_email]">$config[webmaster]</a>
</div>
</body>
</html>
___eofh;
}




/********************************
Cynus Error: cynus_error(string $error)
A fatal error that the system cannot recover from.
Prints the header, the error, then the footer and 
exits, stopping any further actions.
********************************/
function cynus_error($error) {
	global $config;
	cynus_debug ("Caught Cynus Error", 3);
	cynus_debug ("$error", 1);
	set_page_title("Fatal Error");
	do_header();
	print<<<___eofh
<div class="cynus-error">
<img src="themes/$config[style]/images/cynus_error.png">$error
</div>
___eofh;
	do_footer();
	exit;
}



/***************************
Set Default: string set_default(string $default, string $new_value)
This will check if the $new_value exists, and if it does, it will return
that, otherwise it returns $default
***************************/
function set_default($default, $new_value) {
	if($new_value != "") {
		return $new_value;
	}
	else{
		return $default;
	}
}


/***************************
MySQL Request: array mysql_request(string $query)
This is just a shortcut for doing a mysql query since it
will be done so frequently.
***************************/
function mysql_request($query) {
	cynus_debug ($query);
	$result=mysql_query($query);
	if (mysql_num_rows ($result) < 1) {
		cynus_debug ("MySQL returned a null set.", 2);
	}
	$data =  mysql_fetch_assoc($result);
	cynus_debug ($data, 5);
	mysql_free_result($result);
	return $data;
}


/****************************
Convert Access Level: string convert_access_level(int $level)
Takes the $level and converts it a user-friendly descriptiong
****************************/
function convert_access_level($id) {
	$level_array=array(
					0 => "Not logged in",
					1 => "Logged in with no rights",
					2 => "Moderator",
					3 => "Administrator",
					4 => "Super User"
				);
	return $level_array[$id];
}


/************************
Swap: swap(var &$var1, var &$var2)
Swaps $var1 and $var2
$foo=1;
$bar=2;
swap($foo, $bar);
#$foo equals 2 and $bar equals 1 now
************************/
function swap(&$var1, &$var2) {
	$temp=$var1;
	$var1=$var2;
	$var2=$temp;
}


/********************************
Verify Level: bool verify_level(int $min_level)
Return TRUE if $min_level is less than or equal to
the logged in user's access level.  Returns FALSE 
otherwise. You have to call this *after* calling
check_login(1);
********************************/
function verify_level($min_level) {
	global $user_config;
	if($user_config['level'] >= $min_level) {
		cynus_debug ("Verified user level.  User: {$user_config['level']} / Min: $min_level");
		return TRUE;
	}
	else{
		cynus_debug ("Failed to verified user level.  User: {$user_config['level']} / Min: $min_level");
		return FALSE;
	}
}


/****************************
Verify Permission: bool verify_permission(string $module_name)
Uses the $module_name to get the ID from the modules table, and then
checks that against what modules the user has permissions to.
Returns TRUE if they have permission, FALSE if they don't
*****************************/
function verify_permission($module_name, $uid = NULL) {
	global $config;
	
	if ($uid === NULL) {
		#print "###########################";
		global $user_config;  // If the UID is empty, assume they want
	}				// info for the logged in user.
	else {
		#print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
		$user_config = user_convert ($uid);  // Else grab the UID's info.
		$user_config['permissions_array'] = explode (':', $user_config['permissions']);
		
	}
	
	$query="SELECT `id` from `$config[sql_prefix]modules` WHERE `name`='$module_name'";
	$module=mysql_request($query);

	if(in_array($module['id'], $user_config['permissions_array'])) {
		cynus_debug ("Verified user level for module $module_name");
		return TRUE;
	}
	else{
		cynus_debug ("User level insufficient for module $module_name");
		return FALSE;
	}
}


/*****************************
User Convert: array user_convert(int $user_id)
Returns an array of the user's information with the id $user_id
*****************************/
function user_convert($user_id) {
	global $config, $cache;
	if($cache['user_convert'][$user_id]=='') {
		$query="SELECT * from `$config[sql_prefix]users` WHERE `id`='$user_id'";
		$user_row=mysql_request($query);
		$cache['user_convert'][$user_id]=$user_row;
	}
	return $cache['user_convert'][$user_id];
}


/*****************************
Create Pseudo Module: create_pseudo_module(string $friendly_name, string $name, int $min_level)
Creates a pseudo module in the modules table
*****************************/
function create_pseudo_module($friendly_name, $name, $min_level) {
	global $config;
	$query='INSERT into `$config[sql_prefix]modules` (`friendly_name`, `name`, `type`, `min_level`) ' . 
		"VALUES ('$friendly_name', '$name', 'pseudo', '$min_level)";
	mysql_query($query);
}


/*****************************
Create Config: create_config(string $variable_name, string $value, string $description, 
				string $section, string $field_type, string $extra)
Creates a config variable
*****************************/
function create_config($var_name, $value, $description, $section, $field_type, $extra) {
	global $config;
	$query='INSERT into `$config[sql_prefix]config` (`name`, `value`, `description`, `section`, `field_type`, `extra`) ' .
		"VALUES ('$var_name', '$value', '$description', '$section', '$field_type', '$extra')";
	mysql_query($query);
}


/****************************
Selected - string selected(bool $test)
If $test is true, then it will return the string ' selected', otherwise it will return ''
This is usefull for <select> boxes if you want to test if something needs to be
selected by default.
***************************/
function selected($test) {
	if($test) {
		return ' selected';
	}
	else{
		return '';
	}
}


/**************************
Register Display - 
	bool register_display(id $module_id, string $display_function string $includes 
			[, string $extra_url, int $template, int $active])
This registers a page to be displayed by display.php. $module is the module id that will 
be loaded. $display_function is the function that will be called after the module is loaded, 
but there are no parenthenses used. $includes are the files that need to be included to 
call the function, separated by pipes (|). $extra_url will add this to the url after the 
module name. $template is the template id to be added to the page, and $active is if you 
want the page to be shown in a listing, and if somebody will be able to view it with a 
direct link.
**************************/
function register_display($module_id, $display_function, $includes, $extra_url='', $template=0, $active=1) {
	global $config;
	#so we need to check if a display has already been registered for this module
	$query="SELECT `id` from `$config[sql_prefix]display` WHERE `module`='$module_id'";
	$display_check=mysql_request($query);
	if($display_check['id'] == '') {
		#since it wasn't, we can insert this now
		$query="INSERT into `$config[sql_prefix]display` (`module`,`display_function`, `includes`, `extra_url`, `active`) ". 
			"VALUES ('$module', '$display_function', '$includes', '$extra_url', '$template', '$active')";
		#we'll go ahead and check if the query worked, for good measures...
		if(@mysql_query($query)) {
			return true;
		}
	}
	#if one was already registered, or the query failed, we'll return false
	return false;
}



/****************************
Module Convert (ID) - array module_convert_id(int $id)
Converts a module's ID into the row and returns that to
the user.
*****************************/
function module_convert_id($id) {
	global $config, $cache;
	if(!is_array($cache['modules']['id'][$id])) {
		$query="SELECT * from `$config[sql_prefix]modules` WHERE `id`='$id'";
		$row=mysql_request($query);
		$cache['modules']['id'][$id]=$row;
	}
	return $cache['modules']['id'][$id];
}


/*************************
Get User Config - array get_user_config(mixed $user)
Get's the user's config and returns the array
**************************/
function get_user_config($user) {
	global $config;
	
	#get the user's basic information
	if(eregi("[a-z]", $user)) {
		$query="SELECT * from `$config[sql_prefix]users` WHERE `username`='$user'";
		$user_config=mysql_request($query);
	}
	else{
		$user_config=user_convert($user);
	}
	
	#now we need to give them a permissions array
	if($user_config['permissions'] != '') {$user_config['permissions_array']=split(":", $user_config['permissions']);}
	else{$user_config['permissions_array']=array();}
	
	#all done, give them the info
	return $user_config;
}


/************************
Get Template - string get_template(int $id)
Get's the template based on the id. If it's 0, then it gives
the default. If the id doesn't exist, it gives the default
************************/
function get_template($id=0) {
	global $config, $cache;

	if($cache['templates'][$id] == '') {
		if($id == 0) {
			$where="`default`='1'";
		}
		else{
			$where="`id`='$id'";
		}
		
		$query="SELECT * from `$config[sql_prefix]templates` WHERE $where";
		$template=mysql_request($query);
		if($template['name']=='') {
			$query="SELECT * from `$config[sql_prefix]templates` WHERE `default`='1'";
			$template=mysql_request($query);
			$id=0;
		}
		$cache['templates'][$id]=$template;
	}
	
	if(file_exists("$config[cynus_base_dir]/templates/$template[file]")) {
		$template_file=implode("", file("$config[cynus_base_dir]/templates/$template[file]"));
	}
	
	return $template_file;
	
	
}



?>
Return current item: Cynus