PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenVBX/libraries/fooStack/fooBase/Common.php

https://bitbucket.org/gegere/openvbx
PHP | 577 lines | 303 code | 90 blank | 184 comment | 51 complexity | 38171edc8c739aabf3ec4a73a7e6b0c1 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Determines if the current version of PHP is greater then the supplied value
  18. *
  19. * Since there are a few places where we conditionally test for PHP > 5
  20. * we'll set a static variable.
  21. *
  22. * @access public
  23. * @param string
  24. * @return bool
  25. */
  26. function is_php($version = '5.0.0')
  27. {
  28. static $_is_php;
  29. $version = (string)$version;
  30. if ( ! isset($_is_php[$version]))
  31. {
  32. $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  33. }
  34. return $_is_php[$version];
  35. }
  36. // FooStack CiUnit Add:
  37. include(dirname(__FILE__) . '/config.php');
  38. // ------------------------------------------------------------------------
  39. /**
  40. * Tests for file writability
  41. *
  42. * is_writable() returns TRUE on Windows servers when you really can't write to
  43. * the file, based on the read-only attribute. is_writable() is also unreliable
  44. * on Unix servers if safe_mode is on.
  45. *
  46. * @access private
  47. * @return void
  48. */
  49. function is_really_writable($file)
  50. {
  51. // If we're on a Unix server with safe_mode off we call is_writable
  52. if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
  53. {
  54. return is_writable($file);
  55. }
  56. // For windows servers and safe_mode "on" installations we'll actually
  57. // write a file then read it. Bah...
  58. if (is_dir($file))
  59. {
  60. $file = rtrim($file, '/').'/'.md5(rand(1,100));
  61. if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  62. {
  63. return FALSE;
  64. }
  65. fclose($fp);
  66. @chmod($file, DIR_WRITE_MODE);
  67. @unlink($file);
  68. return TRUE;
  69. }
  70. elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  71. {
  72. return FALSE;
  73. }
  74. fclose($fp);
  75. return TRUE;
  76. }
  77. // ------------------------------------------------------------------------
  78. /**
  79. * Class registry
  80. *
  81. * This function acts as a singleton. If the requested class does not
  82. * exist it is instantiated and set to a static variable. If it has
  83. * previously been instantiated the variable is returned.
  84. *
  85. * @access public
  86. * @param string the class name being requested
  87. * @param bool optional flag that lets classes get loaded but not instantiated
  88. * @return object
  89. */
  90. function &load_class($class, $instantiate = TRUE)
  91. {
  92. static $objects = array();
  93. /* fooStack CIUnit Add - Start */
  94. $prefix = config_item('subclass_prefix');
  95. $fs = foo_config();
  96. $fooprefix = $fs['prefix'];
  97. $is_fooclass = FALSE;
  98. //is it in a subfolder?
  99. $folders = explode('/', $class);
  100. if(count($folders) > 1){
  101. $class = array_pop($folders);
  102. $folders = join('/', $folders).'/';
  103. }else{
  104. $folders = '';
  105. }
  106. //echo "folder:".print_r($folders,1);
  107. //echo "class:".print_r($class,1)." *\n";
  108. /* fooStack CIUnit Add - End */
  109. // Does the class exist? If so, we're done...
  110. if (isset($objects[$class]))
  111. {
  112. return $objects[$class];
  113. }
  114. // user extension class?
  115. if (file_exists(APPPATH.'libraries/'.$folders.$prefix.$class.EXT))
  116. {
  117. require(BASEPATH.'libraries/'.$class.EXT);
  118. /* fooStack CIUnit Add - start */
  119. // extend with fooStack class
  120. if(file_exists(FSPATH.$fooprefix.$class.EXT)){
  121. require(FSPATH.$fooprefix.$class.EXT);
  122. $is_fooclass = TRUE;
  123. }
  124. /* fooStack CIUnit Add - End */
  125. // load user class
  126. require(APPPATH.'libraries/'.$folders.$prefix.$class.EXT);
  127. $is_subclass = TRUE;
  128. }
  129. else
  130. {
  131. // independent user class?
  132. if (file_exists(APPPATH.'libraries/'.$folders.$class.EXT))
  133. {
  134. //load it
  135. require(APPPATH.'libraries/'.$folders.$class.EXT);
  136. $is_subclass = FALSE;
  137. /* fooStack CIUnit Add - start */
  138. $is_fooclass = FALSE;
  139. /* fooStack CIUnit Add - end */
  140. }
  141. else
  142. {
  143. // so it must be a base class / or foostack class
  144. require(BASEPATH.'libraries/'.$class.EXT);
  145. /* fooStack CIUnit Add - start */
  146. if(file_exists(FSPATH.$fooprefix.$class.EXT)){
  147. require(FSPATH.$fooprefix.$class.EXT);
  148. $is_fooclass = TRUE;
  149. }
  150. /* fooStack CIUnit Add - end */
  151. $is_subclass = FALSE;
  152. }
  153. }
  154. if ($instantiate == FALSE)
  155. {
  156. $objects[$class] = TRUE;
  157. return $objects[$class];
  158. }
  159. if ($is_subclass == TRUE)
  160. {
  161. $name = $prefix.$class;
  162. $objects[$class] =& instantiate_class(new $name());
  163. return $objects[$class];
  164. }
  165. $prefix = $is_fooclass ? $fooprefix : 'CI_';
  166. //is there a class of this name? if not, add prefix
  167. $name = (class_exists($class)) ? $class : $prefix.$class;
  168. $objects[$class] =& instantiate_class(new $name());
  169. return $objects[$class];
  170. }
  171. /**
  172. * Instantiate Class
  173. *
  174. * Returns a new class object by reference, used by load_class() and the DB class.
  175. * Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
  176. *
  177. * Use: $obj =& instantiate_class(new Foo());
  178. *
  179. * @access public
  180. * @param object
  181. * @return object
  182. */
  183. function &instantiate_class(&$class_object)
  184. {
  185. return $class_object;
  186. }
  187. /**
  188. * fooStack lets plugins each extend their own CI libraries.
  189. * in order to be able to do that we have to find out what files are to be included
  190. * and what objects have to be intermediary
  191. */
  192. function find_load_order($class){
  193. //user prefix
  194. $prefix = config_item('subclass_prefix');
  195. //foostack config var
  196. $fs = foo_config();
  197. //foostack end user class prefix - foostack classes can be exended by the user
  198. $fooprefix = $fs['prefix'];
  199. //main cases:
  200. //base classes - fooextension classes - fooadditional classes - user classes - user extension classes
  201. // load base class (fooconfig decideds if we extend it)
  202. // load non-base class, either fooclass
  203. $is_fooclass = FALSE;
  204. //is it in a subfolder?
  205. $folders = explode('/', $class);
  206. if(count($folders) > 1){
  207. $class = array_pop($folders);
  208. $folders = join('/', $folders).'/';
  209. }else{
  210. $folders = '';
  211. }
  212. //echo "folder:".print_r($folders,1);
  213. //echo "class:".print_r($class,1)." *\n";
  214. // Does the class exist? If so, we're done...
  215. if (isset($objects[$class]))
  216. {
  217. return $objects[$class];
  218. }
  219. // user extension class?
  220. if (file_exists(APPPATH.'libraries/'.$folders.$prefix.$class.EXT))
  221. {
  222. // require Base Class
  223. require(BASEPATH.'libraries/'.$class.EXT);
  224. // extend with fooStack class
  225. if(file_exists(FSPATH.$fooprefix.$class.EXT)){
  226. require(FSPATH.$fooprefix.$class.EXT);
  227. $is_fooclass = TRUE;
  228. }
  229. // load user class
  230. require(APPPATH.'libraries/'.$folders.$prefix.$class.EXT);
  231. $is_subclass = TRUE;
  232. }
  233. else
  234. {
  235. // independent user class?
  236. if (file_exists(APPPATH.'libraries/'.$folders.$class.EXT))
  237. {
  238. //load it
  239. require(APPPATH.'libraries/'.$folders.$class.EXT);
  240. $is_subclass = FALSE;
  241. $is_fooclass = FALSE;
  242. }
  243. else
  244. {
  245. // so it must be a base class / or foostack class
  246. require(BASEPATH.'libraries/'.$class.EXT);
  247. if(file_exists(FSPATH.$fooprefix.$class.EXT)){
  248. require(FSPATH.$fooprefix.$class.EXT);
  249. $is_fooclass = TRUE;
  250. }
  251. $is_subclass = FALSE;
  252. }
  253. }
  254. if ($instantiate == FALSE)
  255. {
  256. $objects[$class] = TRUE;
  257. return $objects[$class];
  258. }
  259. if ($is_subclass == TRUE)
  260. {
  261. $name = $prefix.$class;
  262. echo "Subclass ". $name . " loading..";
  263. $objects[$class] =& instantiate_class(new $name());
  264. return $objects[$class];
  265. }
  266. $prefix = $is_fooclass? $fooprefix:'CI_';
  267. $name = ($class != 'Controller') ? $prefix.$class : $class;
  268. //echo "Class ". $name. " loading..";
  269. $objects[$class] =& instantiate_class(new $name());
  270. return $objects[$class];
  271. }
  272. /**
  273. * Loads the main config.php file
  274. *
  275. * @access private
  276. * @return array
  277. */
  278. function &get_config()
  279. {
  280. static $main_conf;
  281. if ( ! isset($main_conf))
  282. {
  283. if ( ! file_exists(APPPATH.'config/config'.EXT))
  284. {
  285. exit('The configuration file config'.EXT.' does not exist.');
  286. }
  287. require(APPPATH.'config/config'.EXT);
  288. if ( ! isset($config) OR ! is_array($config))
  289. {
  290. exit('Your config file does not appear to be formatted correctly.');
  291. }
  292. $main_conf[0] =& $config;
  293. }
  294. return $main_conf[0];
  295. }
  296. /**
  297. * Gets a config item
  298. *
  299. * @access public
  300. * @return mixed
  301. */
  302. function config_item($item)
  303. {
  304. static $config_item = array();
  305. if ( ! isset($config_item[$item]))
  306. {
  307. $config =& get_config();
  308. if ( ! isset($config[$item]))
  309. {
  310. return FALSE;
  311. }
  312. $config_item[$item] = $config[$item];
  313. }
  314. return $config_item[$item];
  315. }
  316. /**
  317. * Error Handler
  318. *
  319. * This function lets us invoke the exception class and
  320. * display errors using the standard error template located
  321. * in application/errors/errors.php
  322. * This function will send the error page directly to the
  323. * browser and exit.
  324. *
  325. * @access public
  326. * @return void
  327. */
  328. function show_error($message, $status_code = 500)
  329. {
  330. $error =& load_class('Exceptions');
  331. echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
  332. exit;
  333. }
  334. /**
  335. * 404 Page Handler
  336. *
  337. * This function is similar to the show_error() function above
  338. * However, instead of the standard error template it displays
  339. * 404 errors.
  340. *
  341. * @access public
  342. * @return void
  343. */
  344. function show_404($page = '')
  345. {
  346. $error =& load_class('Exceptions');
  347. $error->show_404($page);
  348. exit;
  349. }
  350. /**
  351. * Error Logging Interface
  352. *
  353. * We use this as a simple mechanism to access the logging
  354. * class and send messages to be logged.
  355. *
  356. * @access public
  357. * @return void
  358. */
  359. function log_message($level = 'error', $message, $php_error = FALSE)
  360. {
  361. static $LOG;
  362. $config =& get_config();
  363. if ($config['log_threshold'] == 0)
  364. {
  365. return;
  366. }
  367. $LOG =& load_class('Log');
  368. $LOG->write_log($level, $message, $php_error);
  369. }
  370. /**
  371. * Set HTTP Status Header
  372. *
  373. * @access public
  374. * @param int the status code
  375. * @param string
  376. * @return void
  377. */
  378. function set_status_header($code = 200, $text = '')
  379. {
  380. $stati = array(
  381. 200 => 'OK',
  382. 201 => 'Created',
  383. 202 => 'Accepted',
  384. 203 => 'Non-Authoritative Information',
  385. 204 => 'No Content',
  386. 205 => 'Reset Content',
  387. 206 => 'Partial Content',
  388. 300 => 'Multiple Choices',
  389. 301 => 'Moved Permanently',
  390. 302 => 'Found',
  391. 304 => 'Not Modified',
  392. 305 => 'Use Proxy',
  393. 307 => 'Temporary Redirect',
  394. 400 => 'Bad Request',
  395. 401 => 'Unauthorized',
  396. 403 => 'Forbidden',
  397. 404 => 'Not Found',
  398. 405 => 'Method Not Allowed',
  399. 406 => 'Not Acceptable',
  400. 407 => 'Proxy Authentication Required',
  401. 408 => 'Request Timeout',
  402. 409 => 'Conflict',
  403. 410 => 'Gone',
  404. 411 => 'Length Required',
  405. 412 => 'Precondition Failed',
  406. 413 => 'Request Entity Too Large',
  407. 414 => 'Request-URI Too Long',
  408. 415 => 'Unsupported Media Type',
  409. 416 => 'Requested Range Not Satisfiable',
  410. 417 => 'Expectation Failed',
  411. 500 => 'Internal Server Error',
  412. 501 => 'Not Implemented',
  413. 502 => 'Bad Gateway',
  414. 503 => 'Service Unavailable',
  415. 504 => 'Gateway Timeout',
  416. 505 => 'HTTP Version Not Supported'
  417. );
  418. if ($code == '' OR ! is_numeric($code))
  419. {
  420. show_error('Status codes must be numeric', 500);
  421. }
  422. if (isset($stati[$code]) AND $text == '')
  423. {
  424. $text = $stati[$code];
  425. }
  426. if ($text == '')
  427. {
  428. show_error('No status text available. Please check your status code number or supply your own message text.', 500);
  429. }
  430. $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
  431. if (substr(php_sapi_name(), 0, 3) == 'cgi')
  432. {
  433. header("Status: {$code} {$text}", TRUE);
  434. }
  435. elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
  436. {
  437. header($server_protocol." {$code} {$text}", TRUE, $code);
  438. }
  439. else
  440. {
  441. header("HTTP/1.1 {$code} {$text}", TRUE, $code);
  442. }
  443. }
  444. /**
  445. * Exception Handler
  446. *
  447. * This is the custom exception handler that is declaired at the top
  448. * of Codeigniter.php. The main reason we use this is permit
  449. * PHP errors to be logged in our own log files since we may
  450. * not have access to server logs. Since this function
  451. * effectively intercepts PHP errors, however, we also need
  452. * to display errors based on the current error_reporting level.
  453. * We do that with the use of a PHP error template.
  454. *
  455. * @access private
  456. * @return void
  457. */
  458. function _exception_handler($severity, $message, $filepath, $line)
  459. {
  460. // We don't bother with "strict" notices since they will fill up
  461. // the log file with information that isn't normally very
  462. // helpful. For example, if you are running PHP 5 and you
  463. // use version 4 style class functions (without prefixes
  464. // like "public", "private", etc.) you'll get notices telling
  465. // you that these have been deprecated.
  466. if ($severity == E_STRICT)
  467. {
  468. return;
  469. }
  470. $error =& load_class('Exceptions');
  471. // Should we display the error?
  472. // We'll get the current error_reporting level and add its bits
  473. // with the severity bits to find out.
  474. if (($severity & error_reporting()) == $severity)
  475. {
  476. $error->show_php_error($severity, $message, $filepath, $line);
  477. }
  478. // Should we log the error? No? We're done...
  479. $config =& get_config();
  480. if ($config['log_threshold'] == 0)
  481. {
  482. return;
  483. }
  484. $error->log_exception($severity, $message, $filepath, $line);
  485. }
  486. /* End of file Common.php */
  487. /* Location: ./system/codeigniter/Common.php */