Location: PHPKode > projects > JobAppr > Jobappr-1.4/includes/helper.php
<?php

# get full domain & path of main site
function get_site_path() {
	$path_parts = pathinfo($_SERVER['PHP_SELF']);
	$path_parts = str_replace("/includes", "", $path_parts['dirname']);
	$fullpath = "http://". $_SERVER['SERVER_NAME'] . $path_parts ."";	
	return $fullpath;
}

# check radio buttons for checked or not-checked status
# SYNTAX: check_radio(POST_value, radio_value, return_type);
# return_type: return=false, echo=true 
function check_radio($i,$m,$e=true) {
	if ($i != null) { 
		if ( $i == $m ) { 
			$var = ' checked="checked" '; 
		} else {
			$var = '';
		}
	} else {
		$var = '';	
	}
	if(!$e) {
		return $var;
	} else {
		echo $var;
	}
}

# check an array of radio buttons for checked or not-checked status
# SYNTAX: check_radio_array(POST_value, radio_value, return_type);
# return_type: return=false, echo=true 
function check_radio_array($i,$m,$e=true) {
	if ($i != null) { 
		if ( in_array( $m, $i )) { 
			$var = ' checked="checked" '; 
		} else {
			$var = '';
		}
	} else {
		$var = '';	
	}
	if(!$e) {
		return $var;
	} else {
		echo $var;
	}
}

# check select element for selected or not-selected status
# SYNTAX: check_select(POST_value, radio_value, return_type);
# return_type: return=false, echo=true 
function check_select($i,$m,$e=true) {
	if ($i != null) { 
		if ( $i == $m ) { 
			$var = ' selected="selected" '; 
		} else {
			$var = '';
		}
	} else {
		$var = '';	
	}
	if(!$e) {
		return $var;
	} else {
		echo $var;
	}
}

# clean user-submitted data for insert into html or database
# SYNTAX:  clean(input_data, using_a_database?, strip_tags?);
function clean($data, $db=true, $html=false) {
	
	$data = trim($data);
  if (get_magic_quotes_gpc()) { $data = stripslashes($data); } // if get magic quotes is on, stripslashes
  if ($html) { $data = strip_tags($data); } // no html wanted
	
	if (!$db) { // not used in query (just email or display)
		return $data;
	} elseif ($db) { // used in mysql query
		if (is_numeric($data)) {
			return $data;
		} else {
			$data = mysql_real_escape_string($data);
			return $data;
		}
 }
 
}

# function to get template
function get_template($name, $title='Default') {
	$path = htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES);
	$file_p = basename($path,".php");
	if ($file_p == 'index') { 
		$title = meta('site_name') .' '. $title;
	}
	ob_start();
	$file = $name . ".php";
	include($file);
	$template = ob_get_contents();
	ob_end_clean(); 
	echo $template;
}

# function to get sidebar include
function get_sidebar($name=false) {
	if ($name) {
		ob_start();
		$file = "sidebar-". $name . ".php";
		include($file);
		$template = ob_get_contents();
		ob_end_clean(); 
		echo $template;
	}
}

# echo html code with id of filename. usually used in <body> tag
function page_id() {
	$path = htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES);
	$file = basename($path,".php");	
	echo 'id="'. $file .'"';	
}

# returns the abs filepath
function get_filepath() {
	$path = htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES);
	return $path;	
}


# returns the filename
function get_filename() {
	$path = htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES);
	$file = basename($path,".php");	
	return $file .'.php';	
}

# display messages on site
function display_messages($msg=null) {
	global $success;
	global $error;
	global $notice;

	if ($msg) {
		echo $msg;
		exit();
	}
	if ($notice) {
		echo '<div class="notice topmsg">'. $notice .'</div>';
	}
	if ($error) {
		echo '<div class="error topmsg">'. $error .'</div>';
	}
	if ($success) {
		echo '<div class="success topmsg">'. $success .'</div>';
	}
}

# sendmail function
function sendmail($to,$subject,$message) {
	
	if (defined('FROM_EMAIL_ADDRESS')){
  	$fromemail = FROM_EMAIL_ADDRESS; 
  } else {
  	$fromemail = 'hide@address.com';
  }

	$headers  = "From: ".$fromemail."\r\n";
  $headers .= "Reply-To: ".$fromemail."\r\n";
  $headers .= "Return-Path: ".$fromemail."\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=UTF-8\r\n";
  
  if( mail($to,'=?UTF-8?B?'.base64_encode($subject).'?=',"$message",$headers) ) {
     return true;
  } else {
     return false;
  }
}

function fix_url($i) {
	return filter_var($i,FILTER_SANITIZE_URL);
}
function fix_email($i) {
	return filter_var($i,FILTER_SANITIZE_EMAIL);
}

function validate_url($i) {
	return filter_var($i,FILTER_VALIDATE_URL);
}
function validate_email($i) {
	return filter_var($i,FILTER_VALIDATE_EMAIL);
}

# echo one of two elements
function _e ($post=null,$db=null) {
	if($post != '') {
		echo $post;
	} elseif($db != '') {
		echo $db;
	} else {
		echo "";	
	}
}

# return one of two elements
function _r ($post=null,$db=null) {
	if($post != '') {
		return $post;
	} elseif($db != '') {
		return $db;
	} else {
		return "";	
	}
}

# hash your password
function phash($s) {
	$s = sha1($s);
	return $s;
}

# generate a new password
function createRandomPassword() {
  $chars = "Ayz23mFGHBxPQefgnopRScdqrTU4CXYZabstuDEhijkIJKMNVWvw56789";
  srand((double)microtime()*1000000);
  $i = 0;
  $pass = '' ;
  while ($i <= 8) {
      $num = rand() % 33;
      $tmp = substr($chars, $num, 1);
      $pass = $pass . $tmp;
      $i++;
  }
  return $pass;
}

# clean string to be suitable for url
function clean_url($text)  { 
  if (function_exists('mb_strtolower')) {
     $text = strip_tags(mb_strtolower($text)); 
  } else {
     $text = strip_tags(strtolower($text)); 
  }
  $code_entities_match = array(' ?',' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','='); 
  $code_entities_replace = array('','-','-','','','','','','','','','','','','','','','','','','','','','','',''); 
  $text = str_replace($code_entities_match, $code_entities_replace, $text); 
  $text = urlencode($text);
  $text = rtrim($text, "-");
  $text = str_replace('--', '-', $text); 
  return $text; 
} 

# cleans text you want to turn encode from UTF8
function to7bit($text,$from_enc) {
  if (function_exists('mb_convert_encoding')) {
  	$text = mb_convert_encoding($text,'HTML-ENTITIES',$from_enc);
  }
  $text = preg_replace(
      array('/&szlig;/','/&(..)lig;/',
           '/&([aouAOU])uml;/','/&(.)[^;]*;/'),
      array('ss',"$1","$1".'e',"$1"),
      $text);
  return $text;
}

# gets keyname of an array element
function KeyName(array $a, $pos) {
  $temp = array_slice($a, $pos, 1, true);
  return key($temp);
}

# convert mysql timestamp to php readable
# syntax: date("F j, Y, g:i a", convert_datetime($data['registered_date']))
function convert_datetime($str) {
	list($date, $time) = explode(' ', $str);
	list($year, $month, $day) = explode('-', $date);
	list($hour, $minute, $second) = explode(':', $time);
	
	$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
	
	return $timestamp;
}

# pull a meta value from the database (needs to have the admin section associated with it)
function meta($meta) {
	$select = "SELECT * FROM ".TB_SETTINGS." WHERE meta='".$meta."' LIMIT 1";
	$result = mysql_query($select);
	$row = mysql_fetch_assoc($result);
	return stripslashes($row['value']);
}

# truncate string based on second passed value being the amt of characters
function trunc($text,$chars=200) { 
	if (strlen($text) >= $chars) {
		$text = $text." "; 
		$text = substr($text,0,$chars); 
		$text = substr($text,0,strrpos($text,' ')); 
		$text = $text."..."; 
	}
	return $text; 
} 

# prepare query text for mysql LIKE lookup
function cleanLike($q) {
	$text = str_replace('-', '%', clean_url(trim($q)));
	return $text;
}

# is it new?
function is_it_new($t) {
	$diff = date_diff(date('Y-m-d, H:i:s'), $t);
	$warn_threshold = 2;
	
	if($diff <= $warn_threshold) {
		return '<img src="'.BASE_URL.'assets/images/new.png" alt="new" /> ';
	}
}

# is it expired?
function is_it_expired($t,$days) {
	$diff = date_diff(date('Y-m-d, H:i:s'), $t);
	$warn_threshold = $days;
	
	if($diff <= $warn_threshold) {
		return false;
	} else {
		return true;
	}
}

#compare date
function date_diff($date1, $date2) { 
  $current = $date1; 
  $datetime2 = date_create($date2); 
  $count = 0; 
  while(date_create($current) > $datetime2){ 
      $current = gmdate("Y-m-d", strtotime("-1 day", strtotime($current))); 
      $count++; 
  } 
  return $count; 
} 
Return current item: JobAppr