michaelbeers

Common Functions

Aug 22nd, 2011
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.28 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The Common Functions
  4.  *
  5.  * @package     CloudCMS
  6.  * @author      Michael Beers
  7.  */
  8.  
  9. /**
  10.  * Check the php version
  11.  * @param $version
  12.  */
  13. function is_php($version = '5.0.0')
  14. {
  15.     static $_is_php;
  16.     $version = (string)$version;
  17.  
  18.     if ( ! isset($_is_php[$version]))
  19.     {
  20.         $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  21.     }
  22.  
  23.     return $_is_php[$version];
  24. }
  25.  
  26. /**
  27.  * Check if file is really writable
  28.  * @param $file
  29.  */
  30. function is_really_writable($file)
  31. {
  32.     if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
  33.     {
  34.         return is_writable($file);
  35.     }
  36.  
  37.     //Fix for windows servers
  38.     if (is_dir($file))
  39.     {
  40.         $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
  41.  
  42.         if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  43.         {
  44.             return FALSE;
  45.         }
  46.  
  47.         fclose($fp);
  48.         @chmod($file, DIR_WRITE_MODE);
  49.         @unlink($file);
  50.         return TRUE;
  51.     }
  52.     elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  53.     {
  54.         return FALSE;
  55.     }
  56.  
  57.     fclose($fp);
  58.     return TRUE;
  59. }
  60.  
  61. /**
  62.  * Loading a class, this acts like a singleton
  63.  * @param $class
  64.  * @param $directory
  65.  * @param $prefix
  66.  */
  67. function &load_class($class, $directory = 'libraries', $prefix = 'CLOUD_')
  68. {
  69.     static $_classes = array();
  70.  
  71.     if (isset($_classes[$class]))
  72.     {
  73.         return $_classes[$class];
  74.     }
  75.  
  76.     $name = FALSE;
  77.  
  78.     foreach (array(BASEPATH, APPPATH) as $path)
  79.     {
  80.         if (file_exists($path.$directory.'/'.$class.EXT))
  81.         {
  82.             $name = $prefix.$class;
  83.  
  84.             if (class_exists($name) === FALSE)
  85.             {
  86.                 require($path.$directory.'/'.$class.EXT);
  87.             }
  88.             break;
  89.         }
  90.     }
  91.  
  92.     if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT))
  93.     {
  94.         $name = config_item('subclass_prefix').$class;
  95.  
  96.         if (class_exists($name) === FALSE)
  97.         {
  98.             require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.EXT);
  99.         }
  100.     }
  101.  
  102.     if ($name === FALSE)
  103.     {
  104.         exit('Unable to locate the specified class: '.$class.EXT);
  105.     }
  106.  
  107.     // Keep track of what we just loaded
  108.     is_loaded($class);
  109.  
  110.     $_classes[$class] = new $name();
  111.     return $_classes[$class];
  112. }
  113.  
  114. /**
  115.  * Track the loaded classes
  116.  * @param $class
  117.  */
  118. function is_loaded($class = '')
  119. {
  120.     static $_is_loaded = array();
  121.  
  122.     if ($class != '')
  123.     {
  124.         $_is_loaded[strtolower($class)] = $class;
  125.     }
  126.  
  127.     return $_is_loaded;
  128. }
  129.  
  130. /**
  131.  * Load the main config file
  132.  * @param $replace
  133.  */
  134. function &get_config($replace = array())
  135. {
  136.     static $_config;
  137.  
  138.     if (isset($_config))
  139.     {
  140.         return $_config[0];
  141.     }
  142.  
  143.     if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT))
  144.     {
  145.         $file_path = APPPATH.'config/config'.EXT;
  146.     }
  147.  
  148.     if ( ! file_exists($file_path))
  149.     {
  150.         exit('The configuration file does not exist.');
  151.     }
  152.  
  153.     require($file_path);
  154.  
  155.     if ( ! isset($config) OR ! is_array($config))
  156.     {
  157.         exit('Your config file does not appear to be formatted correctly.');
  158.     }
  159.  
  160.     if (count($replace) > 0)
  161.     {
  162.         foreach ($replace as $key => $val)
  163.         {
  164.             if (isset($config[$key]))
  165.             {
  166.                 $config[$key] = $val;
  167.             }
  168.         }
  169.     }
  170.  
  171.     return $_config[0] =& $config;
  172. }
  173.  
  174. /**
  175.  * Return a specified config file
  176.  * @param $item
  177.  */
  178. function config_item($item)
  179. {
  180.     static $_config_item = array();
  181.  
  182.     if ( ! isset($_config_item[$item]))
  183.     {
  184.         $config =& get_config();
  185.  
  186.         if ( ! isset($config[$item]))
  187.         {
  188.             return FALSE;
  189.         }
  190.         $_config_item[$item] = $config[$item];
  191.     }
  192.  
  193.     return $_config_item[$item];
  194. }
  195.  
  196. /**
  197.  * Error handler
  198.  * @param $message
  199.  * @param $status_code
  200.  * @param $heading
  201.  */
  202. function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
  203. {
  204.     $_error =& load_class('Exceptions', 'core');
  205.     echo $_error->show_error($heading, $message, 'error_general', $status_code);
  206.     exit;
  207. }
  208.  
  209. /**
  210.  * 404 page handler
  211.  * @param $page
  212.  * @param $log_error
  213.  */
  214. function show_404($page = '', $log_error = TRUE)
  215. {
  216.     $_error =& load_class('Exceptions', 'core');
  217.     $_error->show_404($page, $log_error);
  218.     exit;
  219. }
  220.  
  221. /**
  222.  * Error logging interface
  223.  * @param $level
  224.  * @param $message
  225.  * @param $php_error
  226.  */
  227. function log_message($level = 'error', $message, $php_error = FALSE)
  228. {
  229.     static $_log;
  230.  
  231.     if (config_item('log_threshold') == 0)
  232.     {
  233.         return;
  234.     }
  235.  
  236.     $_log =& load_class('Log');
  237.     $_log->write_log($level, $message, $php_error);
  238. }
  239.  
  240. /**
  241.  * Set HTTP Status headers
  242.  * @param $code
  243.  * @param $text
  244.  */
  245. function set_status_header($code = 200, $text = '')
  246. {
  247.     $stati = array(
  248.         200 => 'OK',
  249.         201 => 'Created',
  250.         202 => 'Accepted',
  251.         203 => 'Non-Authoritative Information',
  252.         204 => 'No Content',
  253.         205 => 'Reset Content',
  254.         206 => 'Partial Content',
  255.  
  256.         300 => 'Multiple Choices',
  257.         301 => 'Moved Permanently',
  258.         302 => 'Found',
  259.         304 => 'Not Modified',
  260.         305 => 'Use Proxy',
  261.         307 => 'Temporary Redirect',
  262.  
  263.         400 => 'Bad Request',
  264.         401 => 'Unauthorized',
  265.         403 => 'Forbidden',
  266.         404 => 'Not Found',
  267.         405 => 'Method Not Allowed',
  268.         406 => 'Not Acceptable',
  269.         407 => 'Proxy Authentication Required',
  270.         408 => 'Request Timeout',
  271.         409 => 'Conflict',
  272.         410 => 'Gone',
  273.         411 => 'Length Required',
  274.         412 => 'Precondition Failed',
  275.         413 => 'Request Entity Too Large',
  276.         414 => 'Request-URI Too Long',
  277.         415 => 'Unsupported Media Type',
  278.         416 => 'Requested Range Not Satisfiable',
  279.         417 => 'Expectation Failed',
  280.  
  281.         500 => 'Internal Server Error',
  282.         501 => 'Not Implemented',
  283.         502 => 'Bad Gateway',
  284.         503 => 'Service Unavailable',
  285.         504 => 'Gateway Timeout',
  286.         505 => 'HTTP Version Not Supported'
  287.     );
  288.  
  289.     if ($code == '' OR ! is_numeric($code))
  290.     {
  291.         show_error('Status codes must be numeric', 500);
  292.     }
  293.  
  294.     if (isset($stati[$code]) AND $text == '')
  295.     {
  296.         $text = $stati[$code];
  297.     }
  298.  
  299.     if ($text == '')
  300.     {
  301.         show_error('No status text available.  Please check your status code number or supply your own message text.', 500);
  302.     }
  303.  
  304.     $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  305.  
  306.     if (substr(php_sapi_name(), 0, 3) == 'cgi')
  307.     {
  308.         header("Status: {$code} {$text}", TRUE);
  309.     }
  310.     elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  311.     {
  312.         header($server_protocol." {$code} {$text}", TRUE, $code);
  313.     }
  314.     else
  315.     {
  316.         header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  317.     }
  318. }
  319.  
  320. /**
  321.  * Exception handler
  322.  * @param $severity
  323.  * @param $message
  324.  * @param $filepath
  325.  * @param $line
  326.  */
  327. function _exception_handler($severity, $message, $filepath, $line)
  328. {
  329.     if ($severity == E_STRICT)
  330.     {
  331.         return;
  332.     }
  333.  
  334.     $_error =& load_class('Exceptions', 'core');
  335.  
  336.     if (($severity & error_reporting()) == $severity)
  337.     {
  338.         $_error->show_php_error($severity, $message, $filepath, $line);
  339.     }
  340.  
  341.     if (config_item('log_threshold') == 0)
  342.     {
  343.         return;
  344.     }
  345.  
  346.     $_error->log_exception($severity, $message, $filepath, $line);
  347. }
  348.  
  349. /**
  350.  * Remove invisible characters
  351.  * @param $str
  352.  * @param $url_encoded
  353.  */
  354. function remove_invisible_characters($str, $url_encoded = TRUE)
  355. {
  356.     $non_displayables = array();
  357.            
  358.     if ($url_encoded)
  359.     {
  360.         $non_displayables[] = '/%0[0-8bcef]/';  // url encoded 00-08, 11, 12, 14, 15
  361.         $non_displayables[] = '/%1[0-9a-f]/';   // url encoded 16-31
  362.     }
  363.    
  364.     $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';   // 00-08, 11, 12, 14-31, 127
  365.  
  366.     do
  367.     {
  368.         $str = preg_replace($non_displayables, '', $str, -1, $count);
  369.     }
  370.     while ($count);
  371.  
  372.     return $str;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment