Sometimes when I'm starting a small project from start i always usually have to rewrite all my usually used functions over and over again, so i decided to just gather them up and put them into one file i call the StartFile. The start file is useful for small projects and large projects alike. It just gives you certain helpers that help you build faster. I will add it to my existing repository as that will get updated more than here. http://github.com/CreativityKills
Including the StartFile:
PHP Code:
<?php
/**
* Start file for small projects. This fiule contains helper functions that can be used when
* building your application. Yyou would typically want to include this file at the beginning
* of your application.
*
* @package StartFile
* @author Neo Ighodaro <jeeniors@gmail.com>
* @copyright 2012 CreativityKills
* @link http://github.com/CreativityKills
*/
//
// Prevent an external script from calling this file directly.
//
defined('USE_STARTFILE') or die("Always Smile. :)");
//
// Start file configuration. You can disable any part of the Start file by setting the values
// to false.
//
$configuration = array
(
//
// Start file automatically attempts to disable magic_quotes if its enabled on your server
// if its not enabled, you can set the value to false, if it is, leave the default setting.
//
// NOTE: This feature uses the 'array_strip_slashes' helper and will be forcefully registered, irregardless,
// of what you set in 'use_start_helpers' or 'allowed_helpers;.
//
'disable_magic_quotes' => ! (function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()),
//
// Start file attempts to safe guard your application from register_globals. If register_globals
// is not enabled on your server, set this to false, otherwise leave it at the default setting.
//
// NOTE: if you have access to use "php.ini" files / use "ini_set" function on your server, you should disable
// this and use that instead.
//
'disable_register_globals' => ! ini_get('register_globals'),
//
// Start file provides you with some helper functions that accomplish certain common development
// tasks, if you would like to use these helper functions, then set to true, else set to false.
//
// Scroll down for helper functions. Each function is explained and documented.
//
'use_start_helpers' => true,
//
// Allowed helpers list all the helpers you would like to use, and those not in this list will not be registered
// with PHP. This is useful if you want to remove a particular function without disabling all the helpers totally.
//
'allowed_helpers' => array
(
'array_get',
'array_set',
'array_forget',
'array_strip_slashes',
'array_only',
'array_except',
'magic_quotes',
'starts_with',
'ends_with',
'str_contains',
'str_finish',
'value',
'with',
'has_php',
'get_file_size',
'esc_html',
'd',
),
);
/*
|-------------------------------------------------------------------------------------------------------------------
| Start safer environment.
|-------------------------------------------------------------------------------------------------------------------
|
| Disables register globals and disables magic quotes. Please note that if you disable
| magic_quotes, your application is totally insecure and you have to implement an
| escape mechanism before updating your database with values.
|
*/
//
// Disable the effects of magic quotes.
//
if ($configuration['disable_magic_quotes'] !== false) {
if ( function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc() ) {
//
// Pre load the array_strip_slashes function. Scroll below for documentation.
// This is a stripped / minified, hard-to-read version of the function, for a readable version,
// scroll down.
//
function array_strip_slashes($array)
{
$result = array();
foreach ($array as $key => $value) {
$key = stripslashes($key);
$result[$key] = is_array($value) ? array_strip_slashes($value) : stripslashes($value);
}
return $result;
}
// Sanitize these
$magics = array( &$_GET, &$_POST, &$_COOKIE, &$_REQUEST );
foreach ($magics as &$magic) {
$magic = array_strip_slashes($magic);
}
}
}
//
// Disable the evil effects of register globals.
//
if ( $configuration['disable_register_globals'] !== false and ini_get('register_globals')) {
// Attack detected.
if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS']))
exit( "What was your intention exactly?!" );
// Get the variable names of all globals
$global_variables = array_keys($GLOBALS);
// Remove the standard global variables from the list
$global_variables = array_diff($global_variables, array(
'_COOKIE',
'_ENV',
'_GET',
'_FILES',
'_POST',
'_REQUEST',
'_SERVER',
'_SESSION',
'GLOBALS',
));
foreach ($global_variables as $name) {
// Unset the global variable, effectively disabling register_globals
unset($GLOBALS[$name]);
}
}
//
// Check if we disabled start file helper functions. If we did then stop execution of this script,
// we're done here.
//
if ( ! isset($configuration['use_start_helpers']) or ! $configuration['use_start_helpers'])
return;
/*
|-------------------------------------------------------------------------------------------------------------------
| Start file helpers.
|-------------------------------------------------------------------------------------------------------------------
|
| Helper functions which you may find useful when building your PHP application. Please
| note that yoyu can always remove any you dont like.
|
*/
if ( ! function_exists('array_get') and in_array('array_get', $configuration['allowed_helpers'])) {
/**
* Get an item from an array using "dot" notation.
*
* <code>
* // Get the $array['user']['name'] value from the array
* $name = array_get($array, 'user.name');
*
* // Return a default from if the specified item doesn't exist
* $name = array_get($array, 'user.name', 'Taylor');
* </code>
*
* @param array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
function array_get($array, $key, $default = null )
{
if ( is_null($key ) ) return $array;
$keys = explode( '.', $key );
// To retrieve the array item using dot syntax, we'll iterate through
// each segment in the key and look for that value. If it exists, we
// will return it, otherwise we will set the depth of the array and
// look for the next segment.
foreach ($keys as $segment) {
if ( ! is_array($array ) or ! array_key_exists($segment, $array ) ) {
return (is_callable($default) and ! is_string($default)) ? call_user_func($default) : $default;
}
$array = $array[$segment];
}
return $array;
}
}
if ( ! function_exists('array_set') and in_array('array_set', $configuration['allowed_helpers'])) {
/**
* Set an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* <code>
* // Set the $array['user']['name'] value on the array
* array_set($array, 'user.name', 'Taylor');
*
* // Set the $array['user']['name']['first'] value on the array
* array_set($array, 'user.name.first', 'Michael');
* </code>
*
* @param array $array
* @param string $key
* @param mixed $value
* @return void
*/
function array_set( &$array, $key, $value )
{
if ( is_null($key ) ) return $array = $value;
$keys = explode( '.', $key );
// This loop allows us to dig down into the array to a dynamic depth by
// setting the array value for each level that we dig into. Once there
// is one key left, we can fall out of the loop and set the value as
// we should be at the proper depth.
while ( count($keys ) > 1) {
$key = array_shift($keys );
// If the key doesn't exist at this depth, we will just create an
// empty array to hold the next value, allowing us to create the
// arrays to hold the final value.
if ( ! isset($array[$key] ) or ! is_array($array[$key] ) ) {
$array[$key] = array();
}
$array =& $array[$key];
}
$array[ array_shift($keys) ] = $value;
}
}
if ( ! function_exists('array_forget') and in_array('array_forget', $configuration['allowed_helpers'])) {
/**
* Remove an array item from a given array using "dot" notation.
*
* <code>
* // Remove the $array['user']['name'] item from the array
* array_forget($array, 'user.name');
*
* // Remove the $array['user']['name']['first'] item from the array
* array_forget($array, 'user.name.first');
* </code>
*
* @param array $array
* @param string $key
* @return void
*/
function array_forget(&$array, $key)
{
$keys = explode('.', $key);
// This loop functions very similarly to the loop in the "set" method.
// We will iterate over the keys, setting the array value to the new
// depth at each iteration. Once there is only one key left, we will
// be at the proper depth in the array.
while (count($keys) > 1) {
$key = array_shift($keys);
// Since this method is supposed to remove a value from the array,
// if a value higher up in the chain doesn't exist, there is no
// need to keep digging into the array, since it is impossible
// for the final value to even exist.
if ( ! isset($array[$key]) or ! is_array($array[$key])) {
return;
}
$array =& $array[$key];
}
unset($array[array_shift($keys)]);
}
}
if ( ! function_exists('array_strip_slashes') and in_array('array_strip_slashes', $configuration['allowed_helpers'])) {
/**
* Recursively remove slashes from array keys and values.
*
* @param array $array
* @return array
*/
function array_strip_slashes($array )
{
$result = array();
foreach ($array as $key => $value) {
$key = stripslashes($key);
// If the value is an array, we will just recurse back into the
// function to keep stripping the slashes out of the array,
// otherwise we will set the stripped value.
if ( is_array($value ) ) {
$result[$key] = array_strip_slashes($value );
} else {
$result[$key] = stripslashes($value );
}
}
return $result;
}
}
if ( ! function_exists('array_only') and in_array('array_only', $configuration['allowed_helpers'])) {
/**
* Get a subset of the items from the given array.
*
* @param array $array
* @param array $keys
* @return array
*/
function array_only($array, $keys)
{
return array_intersect_key($array, array_flip((array) $keys) );
}
}
if ( ! function_exists('array_except') and in_array('array_except', $configuration['allowed_helpers'])) {
/**
* Get all of the given array except for a specified array of items.
*
* @param array $array
* @param array $keys
* @return array
*/
function array_except($array, $keys)
{
return array_diff_key($array, array_flip((array) $keys) );
}
}
if ( ! function_exists('magic_quotes') and in_array('magic_quotes', $configuration['allowed_helpers'])) {
/**
* Determine if "Magic Quotes" are enabled on the server.
*
* @return bool
*/
function magic_quotes()
{
return function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc();
}
}
if ( ! function_exists('starts_with') and in_array('starts_with', $configuration['allowed_helpers'])) {
/**
* Determine if a given string begins with a given value.
*
* @param string $haystack
* @param string $needle
* @return bool
*/
function starts_with($haystack, $needle)
{
return strpos($haystack, $needle) === 0;
}
}
if ( ! function_exists('ends_with') and in_array('ends_with', $configuration['allowed_helpers'])) {
/**
* Determine if a given string ends with a given value.
*
* @param string $haystack
* @param string $needle
* @return bool
*/
function ends_with($haystack, $needle)
{
return $needle == substr($haystack, strlen($haystack) - strlen($needle));
}
}
if ( ! function_exists('str_contains') and in_array('str_contains', $configuration['allowed_helpers'])) {
/**
* Determine if a given string contains a given sub-string.
*
* @param string $haystack
* @param string|array $needle
* @return bool
*/
function str_contains($haystack, $needle)
{
foreach ((array) $needle as $n) {
if (strpos($haystack, $n) !== false) return true;
}
return false;
}
}
if ( ! function_exists('str_finish') and in_array('str_finish', $configuration['allowed_helpers'])) {
/**
* Cap a string with a single instance of the given string.
*
* @param string $value
* @param string $cap
* @return string
*/
function str_finish($value, $cap)
{
return rtrim($value, $cap).$cap;
}
}
if ( ! function_exists('value') and in_array('value', $configuration['allowed_helpers'])) {
/**
* Return the value of the given item.
*
* If the given item is a Closure the result of the Closure will be returned.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value;
}
}
if ( ! function_exists('with' ) and in_array('with', $configuration['allowed_helpers'])) {
/**
* Short-cut for constructor method chaining.
*
* @param mixed $object
* @return mixed
*/
function with($object)
{
return $object;
}
}
if ( ! function_exists('has_php') and in_array('has_php', $configuration['allowed_helpers'])) {
/**
* Determine if the current version of PHP is at least the supplied version.
*
* @param string $version
* @return bool
*/
function has_php($version)
{
return version_compare(PHP_VERSION, $version) >= 0;
}
}
if ( ! function_exists('get_file_size') and in_array('get_file_size', $configuration['allowed_helpers'])) {
/**
* Calculate the human-readable file size (with proper units).
*
* @param int $size
* @return string
*/
function get_file_size($size)
{
$units = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB');
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$units[$i];
}
}
if ( ! function_exists('esc_html' ) and in_array('esc_html', $configuration['allowed_helpers'])) {
/**
* Convert HTML characters to entities.
*
* @param string $value
* @param string $encoding
* @return string
*/
function esc_html($value, $encoding = 'utf-8')
{
return htmlentities($value, ENT_QUOTES, $encoding, false );
}
}
if ( ! function_exists('d') and in_array('d', $configuration['allowed_helpers'])) {
/**
* Used when debugging.
*
* @param mixed
* @return string
*/
function d()
{
// Render dump
echo '<pre>';
for ( $i = 0; $i < func_num_args(); ++$i ) {
var_dump(func_get_arg($i));
echo "\n\n";
}
echo '</pre>';
// Abort
exit;
}
}
PHP Code:
<?php
// Stop StartFile from freaking out.
defined( 'USE_STARTFILE' ) or define( 'USE_STARTFILE', true );
// Call start file
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'start.php';