Created
October 12, 2011 15:45
-
-
Save codersatx/1281570 to your computer and use it in GitHub Desktop.
Random Procedural Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Functions | |
* | |
* This file contains numerous helper functions. | |
* @author Alex Garcia | |
* @version 0.1 | |
*/ | |
//-------------------------------------------------------------------------- | |
/** | |
* Includes a header into a front end template file. By default it looks for a | |
* file named header.php in the active theme. | |
* @param string $file Optionally set the name of the file you want to include. | |
* @return void | |
*/ | |
function get_header($file = 'header') | |
{ | |
if (file_exists($file .'.php')) | |
{ | |
include_once($file .'.php'); | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Includes a footer into a front end template file. By default it looks for a | |
* file named footer.php in the active theme. | |
* @param string $file Optionally set the name of the file you want to include. | |
* @return void | |
*/ | |
function get_footer($file = 'footer') | |
{ | |
if (file_exists($file .'.php')) | |
{ | |
include_once($file .'.php'); | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a div container | |
* | |
* @param string $string | |
* @param array $attributes | |
* @return string | |
*/ | |
function div($string, $attributes = array()) | |
{ | |
$out = '<div '; | |
$out .= parse_attributes($attributes); | |
$out .= '>'. $string .'</div>'; | |
return $out; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a span container | |
* | |
* @param string $string | |
* @param array $attributes | |
* @return string | |
*/ | |
function span($string, $attributes = array()) | |
{ | |
$out = '<span '; | |
$out .= parse_attributes($attributes); | |
$out .= '>'. $string .'</span>'; | |
return $out; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Takes any associative array and converts to a key value pair in the form | |
* if key = "value". | |
* | |
* @param array $attributes | |
* @return string | |
*/ | |
function parse_attributes($attributes) | |
{ | |
$out = ''; | |
foreach($attributes as $key=>$value) | |
{ | |
$out .= $key .'="'. $value .'" '; | |
} | |
return $out; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates an opening div tag with an optionial array of attributes. | |
* | |
* @param array $attributes | |
* @return type | |
*/ | |
function div_open($attributes = array()) | |
{ | |
return '<div '. parse_attributes($attributes) .'>'; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a closing div tag. Used in conjunction with the div_open function. | |
* | |
* @return type | |
*/ | |
function div_close() | |
{ | |
return '</div>'; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Takes an array and prints it to the screen in an easy to read format including | |
* line number and file. | |
* | |
* Adapted from Phil Sturgeon: http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications | |
* | |
* @param array $array | |
* @return string | |
*/ | |
function debug($array) | |
{ | |
list($callee) = debug_backtrace(); | |
$arguments = func_get_args(); | |
$total_arguments = count($arguments); | |
echo '<div style="background: #EEE !important; border:1px solid #666; padding:10px;">'; | |
echo '<h1 style="border-bottom: 1px solid #CCC; padding: 0 0 5px 0; margin: 0 0 5px 0; font: bold 18px sans-serif;">'.$callee['file'].' @ line: '.$callee['line'].'</h1><pre>'; | |
$i = 0; | |
foreach ($arguments as $argument) | |
{ | |
echo '<strong>Debug #'.(++$i).' of '.$total_arguments.'</strong>:<br />'; | |
print_r($argument); | |
echo '<br />'; | |
} | |
echo "</pre>"; | |
echo "</div>"; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a string wrapped in any html element that is passed to it. | |
* | |
* @example: element('Some String', 'h1'); | |
* | |
* @param type $string | |
* @param type $element | |
* @return type | |
*/ | |
function element($string, $element) | |
{ | |
return '<'. $element .'>'. $string .'</'. $element .'>'; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Get's the name of the current php file. | |
* | |
* @param boolean $remove_extension | |
* @return type | |
*/ | |
function get_current_file_name($remove_extension = TRUE) | |
{ | |
$current_file = $_SERVER["SCRIPT_NAME"]; | |
$parts = explode('/', $current_file); | |
$current_file = $parts[count($parts) - 1]; | |
if ($remove_extension == TRUE) | |
{ | |
return str_replace('.php', '', $current_file); | |
} | |
return $current_file; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates an anchor tag. | |
* | |
* @param string $url | |
* @param string $link_text | |
* @param array $attributes | |
* @return string | |
*/ | |
function anchor($url, $link_text, $attributes = array()) | |
{ | |
$out = '<a href="' . $url .'"'; | |
$out .= parse_attributes($attributes); | |
$out .= '>'. $link_text .'</a>'; | |
return $out; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Checks to see if the request method is POST. | |
* | |
* @return boolean | |
*/ | |
function is_post() | |
{ | |
$method = $_SERVER['REQUEST_METHOD']; | |
if ($method == 'POST') | |
{ | |
return TRUE; | |
} | |
return FALSE; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Performs a redirect to a new page. | |
* | |
* @param string $url | |
* @param mixed $http_response_code | |
* @return void | |
*/ | |
function redirect($url = '', $http_response_code = 302) | |
{ | |
header("Location: ".$url, TRUE, $http_response_code); | |
exit; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Gets the result of a POST or GET array key. | |
* | |
* @param string $key The name of the input field or query string key. | |
* @param string $method The array to check. Default is get. | |
* @return mixed Value of the field or key or false if the key provided is not found. | |
*/ | |
function input($key, $method = 'get') | |
{ | |
switch($method) | |
{ | |
case 'get': | |
if (isset($_GET[$key])) | |
{ | |
return $_GET[$key]; | |
} | |
return FALSE; | |
break; | |
case 'post': | |
if (isset($_POST[$key])) | |
{ | |
return $_POST[$key]; | |
} | |
return FALSE; | |
break; | |
} | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Gets the result of a select form element | |
* | |
* @param string $key The name of the input field or query string key. | |
* @param string $selected_value The value to check for. | |
* @param string $method The array to check. Default is get. | |
* @return mixed String or false if the value and selected value do not match | |
*/ | |
function input_select($key, $selected_value = NULL, $method = 'get') | |
{ | |
$value = input($key, $method); | |
if ($value == $selected_value) | |
{ | |
return 'selected="selected"'; | |
} | |
return FALSE; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Gets the result of a checkbox form element | |
* | |
* @param string $key The name of the input field or query string key. | |
* @param string $selected_value The value to check for. | |
* @param string $method The array to check. Default is get. | |
* @return mixed String or false if the value and selected value do not match | |
*/ | |
function input_checkbox($key, $selected_value = NULL, $method = 'get') | |
{ | |
$value = input($key, $method); | |
if ($value == $selected_value) | |
{ | |
return 'checked="checked"'; | |
} | |
return FALSE; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a css include tag. | |
* | |
* @param string $file | |
* @param string $path | |
* @return string | |
*/ | |
function css($file, $path = 'css/') | |
{ | |
$file = str_replace('.css', '', $file); | |
$code = '<link href="'; | |
$code .= $path . $file .'.css'; | |
$code .= '" type="text/css" rel="stylesheet"/>'; | |
$code .= "\n"; | |
return $code; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates a js include tag. | |
* | |
* @param string $file | |
* @param string $path | |
* @return string | |
*/ | |
function js($file, $path = 'js/') | |
{ | |
$file = str_replace('.js', '', $file); | |
$code = '<script src="'; | |
$code .= $path . $file .'.js'; | |
$code .= '" type="text/javascript"></script>'; | |
$code .= "\n"; | |
return $code; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Preps an email message and sends it. | |
* | |
* @param string $from_name :the name of the person the email is coming from | |
* @param string $from_email :the email address of the person sending the email | |
* @param string $to_email :the email address of the recipient | |
* @param string $subject :the subject of the email message | |
* @param string $message :the body of the message being sent | |
* @return boolean TRUE if sent FALSE if not | |
* @uses send_email('Some Name','fromemail@isp.com','toemail@isp.com','Some Message Subject','The Message Body') | |
*/ | |
function send_email($from_name, $from_email, $to_email, $subject, $message) | |
{ | |
$headers = "MIME-Version: 1.0 \n"; | |
$headers .= "Content-type: text/html; charset=iso-8859-1 \n"; | |
$headers .= "From: $from_email \n"; | |
$headers .= "Reply-To: $from_email \n"; | |
$headers .= "X-mailer: PHP's mail() Function \n"; | |
if (mail($to_email, $subject, $message, $headers, $from_email)) | |
{ | |
return TRUE; | |
} | |
return FALSE; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Uploads a file to a given directory on a server. | |
* | |
* @param string $key The name of the file upload field in the form. | |
* @param string $target_path The server path where the files should be saved. | |
* @param array $accepted An array of mime types allowed. | |
* @return array | |
*/ | |
function upload($key = 'file', $target_path = NULL, $accepted = array('application/pdf','image/jpeg','image/gif','image/png')) | |
{ | |
if (in_array($_FILES[$key]['type'], $accepted)) | |
{ | |
$file = slugify(($_FILES[$key]['name'])); | |
$new_file_name .= strtotime(date('Y-m-d')) .'-'. $file; | |
$target_path = $target_path . $new_file_name; | |
if ( move_uploaded_file($_FILES[$key]['tmp_name'], $target_path)) | |
{ | |
return array('status'=>'success','details'=>$new_file_name); | |
} | |
return array('status'=>'error','details'=>'There was a problem uploading your file.'); | |
} | |
return array('status'=>'error','details'=>'File types of '. $_FILES[$key]['type'] .' not allowed.'); | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Takes a string with spaces and capital words and converts spaces to | |
* dashes and lower cases the text. | |
* | |
* @param string $string | |
* @return string | |
*/ | |
function slugify($string) | |
{ | |
$string = str_replace(' ', '-', $string); | |
$string = str_replace('_', '-', $string); | |
$string = strtolower($string); | |
return $string; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Takes a string that has been slugified and removes the dashes and caps the | |
* first letters of each word. | |
* | |
* @param string $string | |
* @return string | |
*/ | |
function readable($string) | |
{ | |
$string = str_replace('_', ' ', $string); | |
$string = str_replace('-', ' ', $string); | |
$string = ucwords($string); | |
return $string; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Creates an image tag. | |
* | |
* @example: image_tag('images/some-image.jpg', array('alt'=>'Some Alt Text')); | |
* | |
* @param string $file_path | |
* @param array $attributes | |
* @return string | |
*/ | |
function image_tag($file_path, $attributes = array('alt'=>'empty')) | |
{ | |
$code = '<img src="'. $file_path .'" '. parse_attributes($attributes) .'/>'; | |
return $code; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Returns all querystring params as a string. | |
* | |
* This is useful in applications where we need to set a class on the body tag | |
* based on the current uri. | |
* | |
* @return string | |
*/ | |
function get_array_to_class_names() | |
{ | |
$out = ''; | |
foreach($_GET as $item) | |
{ | |
$out .= $item .' '; | |
} | |
return $out; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Create a summary from a large paragraph of text. | |
* | |
* @param string $string | |
* @param mixed $limit How many words to include. | |
* @param string $end_char How to end the string. Default is ... | |
* @return string | |
*/ | |
function create_summary($string, $limit = 100, $end_char = '…') | |
{ | |
$string = word_limiter($string, $limit, $end_char); | |
$string = str_replace('<p>', '', $string); | |
$string = str_replace('</p>', '', $string); | |
$string = str_replace('<br/>', '', $string); | |
$string = str_replace('<br>', '', $string); | |
return $string; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* Limits the number of words in a given string. | |
* | |
* @param string $string | |
* @param mixed $limit | |
* @param string $end_char | |
* @return string | |
*/ | |
function word_limiter($string, $limit = 100, $end_char = '…') | |
{ | |
if (trim($string) == '') | |
{ | |
return $string; | |
} | |
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $string, $matches); | |
if (strlen($string) == strlen($matches[0])) | |
{ | |
$end_char = ''; | |
} | |
return rtrim($matches[0]).$end_char; | |
} | |
//-------------------------------------------------------------------------- | |
/** | |
* | |
* @return type | |
*/ | |
function func_get_class() | |
{ | |
$strip = array('.php', '/'); | |
$file = $_SERVER["SCRIPT_NAME"]; | |
foreach($strip as $string) | |
{ | |
$file = str_replace($string, '', $file); | |
} | |
return $file; | |
} | |
//--------------------------------------------------------------------------- | |
function process_post() | |
{ | |
if (is_post()) | |
{ | |
$skip = array('submit','submit_x', 'submit_y'); | |
$message = element(input('location', 'post') . ' Subject', 'h2'); | |
foreach($_POST as $key=>$val) | |
{ | |
if ( ! in_array($key, $skip)) | |
{ | |
$message .= element(readable($key), 'strong') .': '. input($key, 'post') . '<br/>'; | |
} | |
} | |
return send_email('from-name', 'from-email', 'to-email', 'subject', $message); | |
} | |
return FALSE; | |
} | |
function session($key) | |
{ | |
session_start(); | |
if (isset($_SESSION[$key])) | |
{ | |
return $_SESSION[$key]; | |
} | |
return FALSE; | |
} | |
//--------------------------------------------------------------------------- | |
function session_set($key, $value) | |
{ | |
session_start(); | |
$_SESSION[$key] = $value; | |
return TRUE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment