Location: PHPKode > projects > GeekDDB > geekddb/general_functions.php
<?php

//LICENSE


/* Changes

	Changed functions that echo'd to return values, needed for template system


*/
?>
<?php
if (isset($mail_feature) and $mail_feature == 1){
	// include mail classes
	include ($_CONF['path'] . "plugins/geekddb/mail_classes/class.html.mime.mail.inc");
	include ($_CONF['path'] . "plugins/geekddb/mail_classes/class.smtp.inc");
} // and if

function get_user()
// goal: get the current user
// input: nothing
// output: $user
{
	$user = get_current_user();
	return $user;
} // end function get_ID_user

function format_date($date)
// from "2000-12-15" to "15 Dec 2000"
{
	global $date_format, $date_separator;
	$temp_ar=explode("-",$date);
	$temp_ar[2] = substr($temp_ar[2], 0, 2); // e.g. from 11 00:00:00 to 11 if the field is datetime
	switch ($date_format){
		case "literal_english":
			$date=@date("j M Y",mktime(0,0,0,$temp_ar[1],$temp_ar[2],$temp_ar[0]));
			break;
		case "latin":
			$date = $temp_ar[2].$date_separator.$temp_ar[1].$date_separator.$temp_ar[0];
			break;
		case "numeric_english":
			$date = $temp_ar[1].$date_separator.$temp_ar[2].$date_separator.$temp_ar[0];
			break;
	} // end switch
	return $date;
}

function split_date($date, &$day, &$month, &$year)
// goal: split a mysql date returning $day, $mont, $year
// input: $date, a MySQL date, &$day, &$month, &$year
// output: &$day, &$month, &$year
{
	$temp=explode("-",$date); 
	$day=$temp[2];
	$month=$temp[1];
	$year=$temp[0];
} // end function split_date

function build_operator_select($field_name)
// goal: build a select with operators: nothing = > <
// input: $field_name
// output: $operator_select
{
	$operator_select = "";
	$operator_select .= "<select name=\"$field_name\">";
	$operator_select .= "<option value=\"\"></option>";
	$operator_select .= "<option value=\"=\">=</option>";
	$operator_select .= "<option value=\">\">></option>";
	$operator_select .= "<option value=\"<\"><</option>";
	$operator_select .= "</select>";

	return $operator_select;
} // end function build_operator_select

function display_sql($sql)
// goal: display a sql query
// input: $sql
// output: nothing
// global: $display_sql
{
	global $display_sql;
	if ($display_sql == "1"){
		$return .= "<p><font color=\"#ff0000\"><b>Your SQL query (for debugging purpose): </b></font>$sql</p>";
	} // end if
	
	return $return ;
} // end function display_sql

function display_message($message, $font_size, $font_color, $bold)
// goal: display a message
// input: $message, $font_size, $font_color, $bold (1 if bold)
// output: nothing
{
	if ($bold == "1"){
		$return .= "<b>";
	} // end if
	if ($font_size != "" or $font_color != ""){
		$return .= "<font size=\"$font_size\" color=\"$font_color\">$message</font>";
	} // end if
	else{
		$return .= $message;
	} // end else 
	if ($bold == "1"){
		$return .= "</b>";
	} // end if
	
	return $return ;
} // end function display_message

function get_pages_number($results_number, $records_per_page)
// goal: calculate the total number of pages necessary to display results
// input: $results_number, $records_per_page
// ouptut: $pages_number
{
	$pages_number = $results_number / $records_per_page;
	$pages_number = (int)($pages_number);
	if (($results_number % $records_per_page) != 0) $pages_number++; // if the reminder is greater than 0 I have to add a page because I have to round to excess

	return $pages_number;
} // end function get_pages_number

function build_date_select ($field_name, $day, $month, $year)
// goal: build three select to select a data (day, mont, year), if are set $day, $month and $year select them
// input: $field_name, the name of the date field, $day, $month, $year
// output: $date_select, the HTML date select
// global $start_year, $end_year
{
	global $start_year, $end_year;

	$date_select = "";
	$day_select = "";
	$month_select = "";
	$year_select = "";
	
	$day_select .= "<select name=\"".$field_name."_day\">";
	$month_select .= "<select name=\"".$field_name."_month\">";
	$year_select .= "<select name=\"".$field_name."_year\">";

	for ($i=1; $i<=31; $i++){
		$day_select .= "<option value=\"".sprintf("%02d",$i)."\"";
		if($day != "" and $day == $i){
			$day_select .= " selected";
		} // end if
		$day_select .= ">".sprintf("%02d",$i)."</option>";
	} // end for

	for ($i=1; $i<=12; $i++){
		$month_select .= "<option value=\"".sprintf("%02d",$i)."\"";
		if($month != "" and $month == $i){
			$month_select .= " selected";
		} // end if
		$month_select .= ">".sprintf("%02d",$i)."</option>";
	} // end for

	for ($i=$start_year; $i<=$end_year; $i++){
		$year_select .= "<option value=\"$i\"";
		if($year != "" and $year == $i){
			$year_select .= " selected";
		} // end if
		$year_select .= ">$i</option>";
	} // end for

	$day_select .= "</select>";
	$month_select .= "</select>";
	$year_select .= "</select>";

	//$date_select = "<table><tr><td>$day_select</td><td>$month_select</td><td>$year_select</td></tr></table>";
	$date_select = "<td valign=\"top\">$day_select</td><td valign=\"top\">$month_select</td><td valign=\"top\">$year_select</td>";	

	return $date_select;

} // end function build_date_select

function contains_numerics($string)
// goal: verify if a string contains numbers
// input: $string
// output: true if the string contains numbers, false otherwise
{
	for ($i=0; $i<strlen($string); $i++){
		if (is_numeric($string[$i])){
			return true;
		} // end if
	} // end for
	return false;
} // end function contains_numerics

function contains_alphabetics($string)
// goal: verify if a string contains alphabetics or other non-numbers characters (blank spaces and carriage return are admitted)
// input: $string
// output: true if the string contains alphabetics, false otherwise
{
	for ($i=0; $i<strlen($string); $i++){
		if (!is_numeric($string[$i]) and $string[$i] != " " and $string[$i] != "\n" and $string[$i] != "\r"){
			return true;
		} // end if
	} // end for
	return false;
} // end function contains_alphabetics

function is_valid_email($email)
// goal: chek if an email address is valid, according to its syntax
// input: $email
// output: true if it's valid, false otherwise
{
    return (preg_match( 
        '/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+'.   // the user name 
        '@'.                                     // the ubiquitous at-sign 
        '([-0-9A-Z]+\.)+' .                      // host, sub-, and domain names 
        '([0-9A-Z]){2,4}$/i',                    // top-level domain (TLD) 
        trim($email))); 
} // end function is_valid_email

function is_valid_url($url)
// goal: chek if an url address is valid, according to its syntax
// input: $url
// output: true if it's valid, false otherwise
{
	return eregi("^((ht|f)tp://)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)$", $url); 
} // end function is_valid_url

function is_valid_phone($phone)
// goal: chek if a phone numbers is valid, according to its syntax (should be: "+390523599314")
// input: $phone
// output: true if it's valid, false otherwise
{
	if ($phone[0] != "+"){
		return false;
	} // end if
	else{
		$phone = substr($phone, 1, strlen($phone)-1); // delete the "+"
		if (contains_alphabetics($phone)){
			return false;
		} // end if
	} // end else
	return true;
} // end function is_valid_phone

function get_unique_field($conn, $db_name, $table_name)
// goal: get the name of the first uniqe field in a table
// input: $conn, $db_name, $table_name
// output: $unique_field_name, the name of the first unique field in the table
{
	$unique_field_name = "";
	$fields = list_fields_db("$db_name", "$table_name", $conn);
	$columns = num_fields_db($fields);

	for ($i = 0; $i < $columns; $i++) {
		if (strpos(field_flags_db($fields, $i), "primary_key")){ // if the flag contain the word "primary_key"			
			$unique_field_name = field_name_db($fields, $i);
			break;
		} // end if
	}
	return $unique_field_name;
} // end function get_unique_field

// Andrew Removed this becasue of conflict

//function db_error($sql)
// goal: exit the script
// input: $sql
// output: nothing
//{
	// exit;
// } // end function db_error
?>
Return current item: GeekDDB