PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/basics.php

https://github.com/jph98/pastebin
PHP | 1164 lines | 753 code | 58 blank | 353 comment | 206 complexity | e61640091921e4603bbd6ef217c1769b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id: basics.php 4786 2007-04-05 17:57:00Z phpnut $ */
  3. /**
  4. * Basic Cake functionality.
  5. *
  6. * Core functions for including other source files, loading models and so forth.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The MIT License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.cake
  23. * @since CakePHP(tm) v 0.2.9
  24. * @version $Revision: 4786 $
  25. * @modifiedby $LastChangedBy: phpnut $
  26. * @lastmodified $Date: 2007-04-05 12:57:00 -0500 (Thu, 05 Apr 2007) $
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. /**
  30. * Basic defines for timing functions.
  31. */
  32. define('SECOND', 1);
  33. define('MINUTE', 60 * SECOND);
  34. define('HOUR', 60 * MINUTE);
  35. define('DAY', 24 * HOUR);
  36. define('WEEK', 7 * DAY);
  37. define('MONTH', 30 * DAY);
  38. define('YEAR', 365 * DAY);
  39. /**
  40. * Patch for PHP < 4.3
  41. */
  42. if (!function_exists("ob_get_clean")) {
  43. function ob_get_clean() {
  44. $ob_contents = ob_get_contents();
  45. ob_end_clean();
  46. return $ob_contents;
  47. }
  48. }
  49. /**
  50. * Patch for PHP < 4.3
  51. */
  52. if (version_compare(phpversion(), '5.0') < 0) {
  53. eval ('
  54. function clone($object) {
  55. return $object;
  56. }');
  57. }
  58. /**
  59. * Computes the difference of arrays using keys for comparison
  60. *
  61. * @param array
  62. * @param array
  63. * @return array
  64. */
  65. if (!function_exists('array_diff_key')) {
  66. function array_diff_key() {
  67. $valuesDiff = array();
  68. if (func_num_args() < 2) {
  69. return false;
  70. }
  71. foreach (func_get_args() as $param) {
  72. if (!is_array($param)) {
  73. return false;
  74. }
  75. }
  76. $args = func_get_args();
  77. foreach ($args[0] as $valueKey => $valueData) {
  78. for ($i = 1; $i < func_num_args(); $i++) {
  79. if (isset($args[$i][$valueKey])) {
  80. continue 2;
  81. }
  82. }
  83. $valuesDiff[$valueKey] = $valueData;
  84. }
  85. return $valuesDiff;
  86. }
  87. }
  88. /**
  89. * Computes the intersection of arrays using keys for comparison
  90. *
  91. * @param array
  92. * @param array
  93. * @return array
  94. */
  95. if (!function_exists('array_intersect_key')) {
  96. function array_intersect_key($arr1, $arr2) {
  97. $res = array();
  98. foreach($arr1 as $key=>$value) {
  99. if(array_key_exists($key, $arr2)) {
  100. $res[$key] = $arr1[$key];
  101. }
  102. }
  103. return $res;
  104. }
  105. }
  106. /**
  107. * Loads all models.
  108. */
  109. function loadModels() {
  110. if(!class_exists('Model')){
  111. require LIBS . 'model' . DS . 'model.php';
  112. }
  113. $path = Configure::getInstance();
  114. if (!class_exists('AppModel')) {
  115. if (file_exists(APP . 'app_model.php')) {
  116. require(APP . 'app_model.php');
  117. } else {
  118. require(CAKE . 'app_model.php');
  119. }
  120. if (phpversion() < 5 && function_exists("overload")) {
  121. overload('AppModel');
  122. }
  123. }
  124. foreach($path->modelPaths as $path) {
  125. foreach(listClasses($path)as $model_fn) {
  126. list($name) = explode('.', $model_fn);
  127. $className = Inflector::camelize($name);
  128. if (!class_exists($className)) {
  129. require($path . $model_fn);
  130. if (phpversion() < 5 && function_exists("overload")) {
  131. overload($className);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Loads all plugin models.
  139. *
  140. * @param string $plugin Name of plugin
  141. * @return
  142. */
  143. function loadPluginModels($plugin) {
  144. if(!class_exists('AppModel')){
  145. loadModel();
  146. }
  147. $pluginAppModel = Inflector::camelize($plugin . '_app_model');
  148. $pluginAppModelFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_model.php';
  149. if (!class_exists($pluginAppModel)) {
  150. if (file_exists($pluginAppModelFile)) {
  151. require($pluginAppModelFile);
  152. } else {
  153. die('Plugins must have a class named ' . $pluginAppModel);
  154. }
  155. if (phpversion() < 5 && function_exists("overload")) {
  156. overload($pluginAppModel);
  157. }
  158. }
  159. $pluginModelDir = APP . 'plugins' . DS . $plugin . DS . 'models' . DS;
  160. foreach(listClasses($pluginModelDir)as $modelFileName) {
  161. list($name) = explode('.', $modelFileName);
  162. $className = Inflector::camelize($name);
  163. if (!class_exists($className)) {
  164. require($pluginModelDir . $modelFileName);
  165. if (phpversion() < 5 && function_exists("overload")) {
  166. overload($className);
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * Loads custom view class.
  173. *
  174. */
  175. function loadView($viewClass) {
  176. if (!class_exists($viewClass . 'View')) {
  177. $paths = Configure::getInstance();
  178. $file = Inflector::underscore($viewClass) . '.php';
  179. foreach($paths->viewPaths as $path) {
  180. if (file_exists($path . $file)) {
  181. return require($path . $file);
  182. }
  183. }
  184. if ($viewFile = fileExistsInPath(LIBS . 'view' . DS . $file)) {
  185. if (file_exists($viewFile)) {
  186. require($viewFile);
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * Loads a model by CamelCase name.
  196. */
  197. function loadModel($name = null) {
  198. if(!class_exists('Model')){
  199. require LIBS . 'model' . DS . 'model.php';
  200. }
  201. if (!class_exists('AppModel')) {
  202. if (file_exists(APP . 'app_model.php')) {
  203. require(APP . 'app_model.php');
  204. } else {
  205. require(CAKE . 'app_model.php');
  206. }
  207. if (phpversion() < 5 && function_exists("overload")) {
  208. overload('AppModel');
  209. }
  210. }
  211. if (!is_null($name) && !class_exists($name)) {
  212. $className = $name;
  213. $name = Inflector::underscore($name);
  214. $paths = Configure::getInstance();
  215. foreach($paths->modelPaths as $path) {
  216. if (file_exists($path . $name . '.php')) {
  217. require($path . $name . '.php');
  218. if (phpversion() < 5 && function_exists("overload")) {
  219. overload($className);
  220. }
  221. return true;
  222. }
  223. }
  224. return false;
  225. } else {
  226. return true;
  227. }
  228. }
  229. /**
  230. * Loads all controllers.
  231. */
  232. function loadControllers() {
  233. $paths = Configure::getInstance();
  234. if (!class_exists('AppController')) {
  235. if (file_exists(APP . 'app_controller.php')) {
  236. require(APP . 'app_controller.php');
  237. } else {
  238. require(CAKE . 'app_controller.php');
  239. }
  240. }
  241. $loadedControllers = array();
  242. foreach($paths->controllerPaths as $path) {
  243. foreach(listClasses($path) as $controller) {
  244. list($name) = explode('.', $controller);
  245. $className = Inflector::camelize($name);
  246. if (loadController($name)) {
  247. $loadedControllers[$controller] = $className;
  248. }
  249. }
  250. }
  251. return $loadedControllers;
  252. }
  253. /**
  254. * Loads a controller and its helper libraries.
  255. *
  256. * @param string $name Name of controller
  257. * @return boolean Success
  258. */
  259. function loadController($name) {
  260. $paths = Configure::getInstance();
  261. if (!class_exists('AppController')) {
  262. if (file_exists(APP . 'app_controller.php')) {
  263. require(APP . 'app_controller.php');
  264. } else {
  265. require(CAKE . 'app_controller.php');
  266. }
  267. }
  268. if ($name === null) {
  269. return true;
  270. }
  271. if (!class_exists($name . 'Controller')) {
  272. $name = Inflector::underscore($name);
  273. foreach($paths->controllerPaths as $path) {
  274. if (file_exists($path . $name . '_controller.php')) {
  275. require($path . $name . '_controller.php');
  276. return true;
  277. }
  278. }
  279. if ($controller_fn = fileExistsInPath(LIBS . 'controller' . DS . $name . '_controller.php')) {
  280. if (file_exists($controller_fn)) {
  281. require($controller_fn);
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. }
  287. } else {
  288. return false;
  289. }
  290. }
  291. /**
  292. * Loads a plugin's controller.
  293. *
  294. * @param string $plugin Name of plugin
  295. * @param string $controller Name of controller to load
  296. * @return boolean Success
  297. */
  298. function loadPluginController($plugin, $controller) {
  299. $pluginAppController = Inflector::camelize($plugin . '_app_controller');
  300. $pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php';
  301. if (!class_exists($pluginAppController)) {
  302. if (file_exists($pluginAppControllerFile)) {
  303. require($pluginAppControllerFile);
  304. } else {
  305. return false;
  306. }
  307. }
  308. if (empty($controller)) {
  309. if (!class_exists($plugin . 'Controller')) {
  310. if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
  311. require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
  312. return true;
  313. }
  314. }
  315. }
  316. if (!class_exists($controller . 'Controller')) {
  317. $controller = Inflector::underscore($controller);
  318. $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $controller . '_controller.php';
  319. if (file_exists($file)) {
  320. require($file);
  321. return true;
  322. } elseif (!class_exists(Inflector::camelize($plugin . '_controller'))) {
  323. if(file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
  324. require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
  325. return true;
  326. } else {
  327. return false;
  328. }
  329. }
  330. }
  331. return true;
  332. }
  333. /**
  334. * Loads a helper
  335. *
  336. * @param string $name Name of helper
  337. * @return boolean Success
  338. */
  339. function loadHelper($name) {
  340. $paths = Configure::getInstance();
  341. if ($name === null) {
  342. return true;
  343. }
  344. if (!class_exists($name . 'Helper')) {
  345. $name=Inflector::underscore($name);
  346. foreach($paths->helperPaths as $path) {
  347. if (file_exists($path . $name . '.php')) {
  348. require($path . $name . '.php');
  349. return true;
  350. }
  351. }
  352. if ($helper_fn = fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $name . '.php')) {
  353. if (file_exists($helper_fn)) {
  354. require($helper_fn);
  355. return true;
  356. } else {
  357. return false;
  358. }
  359. }
  360. } else {
  361. return true;
  362. }
  363. }
  364. /**
  365. * Loads a plugin's helper
  366. *
  367. * @param string $plugin Name of plugin
  368. * @param string $helper Name of helper to load
  369. * @return boolean Success
  370. */
  371. function loadPluginHelper($plugin, $helper) {
  372. if (!class_exists($helper . 'Helper')) {
  373. $helper = Inflector::underscore($helper);
  374. $file = APP . 'plugins' . DS . $plugin . DS . 'views' . DS . 'helpers' . DS . $helper . '.php';
  375. if (file_exists($file)) {
  376. require($file);
  377. return true;
  378. } else {
  379. return false;
  380. }
  381. } else {
  382. return true;
  383. }
  384. }
  385. /**
  386. * Loads a component
  387. *
  388. * @param string $name Name of component
  389. * @return boolean Success
  390. */
  391. function loadComponent($name) {
  392. $paths = Configure::getInstance();
  393. if ($name === null) {
  394. return true;
  395. }
  396. if (!class_exists($name . 'Component')) {
  397. $name=Inflector::underscore($name);
  398. foreach($paths->componentPaths as $path) {
  399. if (file_exists($path . $name . '.php')) {
  400. require($path . $name . '.php');
  401. return true;
  402. }
  403. }
  404. if ($component_fn = fileExistsInPath(LIBS . 'controller' . DS . 'components' . DS . $name . '.php')) {
  405. if (file_exists($component_fn)) {
  406. require($component_fn);
  407. return true;
  408. } else {
  409. return false;
  410. }
  411. }
  412. } else {
  413. return true;
  414. }
  415. }
  416. /**
  417. * Loads a plugin's component
  418. *
  419. * @param string $plugin Name of plugin
  420. * @param string $helper Name of component to load
  421. * @return boolean Success
  422. */
  423. function loadPluginComponent($plugin, $component) {
  424. if (!class_exists($component . 'Component')) {
  425. $component = Inflector::underscore($component);
  426. $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . 'components' . DS . $component . '.php';
  427. if (file_exists($file)) {
  428. require($file);
  429. return true;
  430. } else {
  431. return false;
  432. }
  433. } else {
  434. return true;
  435. }
  436. }
  437. /**
  438. * Returns an array of filenames of PHP files in given directory.
  439. *
  440. * @param string $path Path to scan for files
  441. * @return array List of files in directory
  442. */
  443. function listClasses($path) {
  444. $dir = opendir($path);
  445. $classes=array();
  446. while(false !== ($file = readdir($dir))) {
  447. if ((substr($file, -3, 3) == 'php') && substr($file, 0, 1) != '.') {
  448. $classes[] = $file;
  449. }
  450. }
  451. closedir($dir);
  452. return $classes;
  453. }
  454. /**
  455. * Loads configuration files
  456. *
  457. * @return boolean Success
  458. */
  459. function config() {
  460. $args = func_get_args();
  461. foreach($args as $arg) {
  462. if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
  463. include_once(CONFIGS . $arg . '.php');
  464. } elseif (file_exists(CONFIGS . $arg . '.php')) {
  465. include_once(CONFIGS . $arg . '.php');
  466. if (count($args) == 1) {
  467. return true;
  468. }
  469. } else {
  470. if (count($args) == 1) {
  471. return false;
  472. }
  473. }
  474. }
  475. return true;
  476. }
  477. /**
  478. * Loads component/components from LIBS.
  479. *
  480. * Example:
  481. * <code>
  482. * uses('flay', 'time');
  483. * </code>
  484. *
  485. * @uses LIBS
  486. */
  487. function uses() {
  488. $args = func_get_args();
  489. foreach($args as $arg) {
  490. require_once(LIBS . strtolower($arg) . '.php');
  491. }
  492. }
  493. /**
  494. * Require given files in the VENDORS directory. Takes optional number of parameters.
  495. *
  496. * @param string $name Filename without the .php part.
  497. *
  498. */
  499. function vendor($name) {
  500. $args = func_get_args();
  501. foreach($args as $arg) {
  502. if (file_exists(APP . 'vendors' . DS . $arg . '.php')) {
  503. require_once(APP . 'vendors' . DS . $arg . '.php');
  504. } else {
  505. require_once(VENDORS . $arg . '.php');
  506. }
  507. }
  508. }
  509. /**
  510. * Prints out debug information about given variable.
  511. *
  512. * Only runs if DEBUG level is non-zero.
  513. *
  514. * @param boolean $var Variable to show debug information for.
  515. * @param boolean $show_html If set to true, the method prints the debug data in a screen-friendly way.
  516. */
  517. function debug($var = false, $showHtml = false) {
  518. if (Configure::read() > 0) {
  519. print "\n<pre class=\"cake_debug\">\n";
  520. ob_start();
  521. print_r($var);
  522. $var = ob_get_clean();
  523. if ($showHtml) {
  524. $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
  525. }
  526. print "{$var}\n</pre>\n";
  527. }
  528. }
  529. /**
  530. * Returns microtime for execution time checking
  531. *
  532. * @return integer
  533. */
  534. if (!function_exists('getMicrotime')) {
  535. function getMicrotime() {
  536. list($usec, $sec) = explode(" ", microtime());
  537. return ((float)$usec + (float)$sec);
  538. }
  539. }
  540. /**
  541. * Sorts given $array by key $sortby.
  542. *
  543. * @param array $array
  544. * @param string $sortby
  545. * @param string $order Sort order asc/desc (ascending or descending).
  546. * @param integer $type
  547. * @return mixed
  548. */
  549. if (!function_exists('sortByKey')) {
  550. function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
  551. if (!is_array($array)) {
  552. return null;
  553. }
  554. foreach($array as $key => $val) {
  555. $sa[$key] = $val[$sortby];
  556. }
  557. if ($order == 'asc') {
  558. asort($sa, $type);
  559. } else {
  560. arsort($sa, $type);
  561. }
  562. foreach($sa as $key => $val) {
  563. $out[] = $array[$key];
  564. }
  565. return $out;
  566. }
  567. }
  568. /**
  569. * Combines given identical arrays by using the first array's values as keys,
  570. * and the second one's values as values. (Implemented for back-compatibility with PHP4)
  571. *
  572. * @param array $a1
  573. * @param array $a2
  574. * @return mixed Outputs either combined array or false.
  575. */
  576. if (!function_exists('array_combine')) {
  577. function array_combine($a1, $a2) {
  578. $a1 = array_values($a1);
  579. $a2 = array_values($a2);
  580. $c1 = count($a1);
  581. $c2 = count($a2);
  582. if ($c1 != $c2) {
  583. return false;
  584. }
  585. if ($c1 <= 0) {
  586. return false;
  587. }
  588. $output=array();
  589. for($i = 0; $i < $c1; $i++) {
  590. $output[$a1[$i]] = $a2[$i];
  591. }
  592. return $output;
  593. }
  594. }
  595. /**
  596. * Convenience method for htmlspecialchars.
  597. *
  598. * @param string $text
  599. * @return string
  600. */
  601. function h($text) {
  602. if (is_array($text)) {
  603. return array_map('h', $text);
  604. }
  605. return htmlspecialchars($text);
  606. }
  607. /**
  608. * Returns an array of all the given parameters.
  609. *
  610. * Example:
  611. * <code>
  612. * a('a', 'b')
  613. * </code>
  614. *
  615. * Would return:
  616. * <code>
  617. * array('a', 'b')
  618. * </code>
  619. *
  620. * @return array
  621. */
  622. function a() {
  623. $args = func_get_args();
  624. return $args;
  625. }
  626. /**
  627. * Constructs associative array from pairs of arguments.
  628. *
  629. * Example:
  630. * <code>
  631. * aa('a','b')
  632. * </code>
  633. *
  634. * Would return:
  635. * <code>
  636. * array('a'=>'b')
  637. * </code>
  638. *
  639. * @return array
  640. */
  641. function aa() {
  642. $args = func_get_args();
  643. for($l = 0, $c = count($args); $l < $c; $l++) {
  644. if ($l + 1 < count($args)) {
  645. $a[$args[$l]] = $args[$l + 1];
  646. } else {
  647. $a[$args[$l]] = null;
  648. }
  649. $l++;
  650. }
  651. return $a;
  652. }
  653. /**
  654. * Convenience method for echo().
  655. *
  656. * @param string $text String to echo
  657. */
  658. function e($text) {
  659. echo $text;
  660. }
  661. /**
  662. * Convenience method for strtolower().
  663. *
  664. * @param string $str String to lowercase
  665. */
  666. function low($str) {
  667. return strtolower($str);
  668. }
  669. /**
  670. * Convenience method for strtoupper().
  671. *
  672. * @param string $str String to uppercase
  673. */
  674. function up($str) {
  675. return strtoupper($str);
  676. }
  677. /**
  678. * Convenience method for str_replace().
  679. *
  680. * @param string $search String to be replaced
  681. * @param string $replace String to insert
  682. * @param string $subject String to search
  683. */
  684. function r($search, $replace, $subject) {
  685. return str_replace($search, $replace, $subject);
  686. }
  687. /**
  688. * Print_r convenience function, which prints out <PRE> tags around
  689. * the output of given array. Similar to debug().
  690. *
  691. * @see debug()
  692. * @param array $var
  693. */
  694. function pr($var) {
  695. if (Configure::read() > 0) {
  696. echo "<pre>";
  697. print_r($var);
  698. echo "</pre>";
  699. }
  700. }
  701. /**
  702. * Display parameter
  703. *
  704. * @param mixed $p Parameter as string or array
  705. * @return string
  706. */
  707. function params($p) {
  708. if (!is_array($p) || count($p) == 0) {
  709. return null;
  710. } else {
  711. if (is_array($p[0]) && count($p) == 1) {
  712. return $p[0];
  713. } else {
  714. return $p;
  715. }
  716. }
  717. }
  718. /**
  719. * Merge a group of arrays
  720. *
  721. * @param array First array
  722. * @param array Second array
  723. * @param array Third array
  724. * @param array Etc...
  725. * @return array All array parameters merged into one
  726. */
  727. function am() {
  728. $r = array();
  729. foreach(func_get_args()as $a) {
  730. if (!is_array($a)) {
  731. $a = array($a);
  732. }
  733. $r = array_merge($r, $a);
  734. }
  735. return $r;
  736. }
  737. /**
  738. * Returns the REQUEST_URI from the server environment, or, failing that,
  739. * constructs a new one, using the PHP_SELF constant and other variables.
  740. *
  741. * @return string URI
  742. */
  743. function setUri() {
  744. if (env('HTTP_X_REWRITE_URL')) {
  745. $uri = env('HTTP_X_REWRITE_URL');
  746. } elseif(env('REQUEST_URI')) {
  747. $uri = env('REQUEST_URI');
  748. } else {
  749. if (env('argv')) {
  750. $uri = env('argv');
  751. if (defined('SERVER_IIS')) {
  752. $uri = BASE_URL . $uri[0];
  753. } else {
  754. $uri = env('PHP_SELF') . '/' . $uri[0];
  755. }
  756. } else {
  757. $uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
  758. }
  759. }
  760. return $uri;
  761. }
  762. /**
  763. * Gets an environment variable from available sources.
  764. * Used as a backup if $_SERVER/$_ENV are disabled.
  765. *
  766. * @param string $key Environment variable name.
  767. * @return string Environment variable setting.
  768. */
  769. function env($key) {
  770. if ($key == 'HTTPS') {
  771. if (isset($_SERVER) && !empty($_SERVER)) {
  772. return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
  773. } else {
  774. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  775. }
  776. }
  777. if (isset($_SERVER[$key])) {
  778. return $_SERVER[$key];
  779. } elseif (isset($_ENV[$key])) {
  780. return $_ENV[$key];
  781. } elseif (getenv($key) !== false) {
  782. return getenv($key);
  783. }
  784. if ($key == 'DOCUMENT_ROOT') {
  785. $offset = 0;
  786. if (!strpos(env('SCRIPT_NAME'), '.php')) {
  787. $offset = 4;
  788. }
  789. return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
  790. }
  791. if ($key == 'PHP_SELF') {
  792. return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  793. }
  794. return null;
  795. }
  796. /**
  797. * Returns contents of a file as a string.
  798. *
  799. * @param string $fileName Name of the file.
  800. * @param boolean $useIncludePath Wheter the function should use the include path or not.
  801. * @return mixed Boolean false or contents of required file.
  802. */
  803. if (!function_exists('file_get_contents')) {
  804. function file_get_contents($fileName, $useIncludePath = false) {
  805. $res=fopen($fileName, 'rb', $useIncludePath);
  806. if ($res === false) {
  807. trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
  808. return false;
  809. }
  810. clearstatcache();
  811. if ($fileSize = @filesize($fileName)) {
  812. $data = fread($res, $fileSize);
  813. } else {
  814. $data = '';
  815. while(!feof($res)) {
  816. $data .= fread($res, 8192);
  817. }
  818. }
  819. return "$data\n";
  820. }
  821. }
  822. /**
  823. * Writes data into file.
  824. *
  825. * If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
  826. *
  827. * @param string $fileName File name.
  828. * @param mixed $data String or array.
  829. */
  830. if (!function_exists('file_put_contents')) {
  831. function file_put_contents($fileName, $data) {
  832. if (is_array($data)) {
  833. $data = join('', $data);
  834. }
  835. $res = @fopen($fileName, 'w+b');
  836. if ($res) {
  837. $write = @fwrite($res, $data);
  838. if($write === false) {
  839. return false;
  840. } else {
  841. return $write;
  842. }
  843. }
  844. }
  845. }
  846. /**
  847. * Reads/writes temporary data to cache files or session.
  848. *
  849. * @param string $path File path within /tmp to save the file.
  850. * @param mixed $data The data to save to the temporary file.
  851. * @param mixed $expires A valid strtotime string when the data expires.
  852. * @param string $target The target of the cached data; either 'cache' or 'public'.
  853. * @return mixed The contents of the temporary file.
  854. */
  855. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  856. $now = time();
  857. if (!is_numeric($expires)) {
  858. $expires = strtotime($expires, $now);
  859. }
  860. switch(strtolower($target)) {
  861. case 'cache':
  862. $filename = CACHE . $path;
  863. break;
  864. case 'public':
  865. $filename = WWW_ROOT . $path;
  866. break;
  867. }
  868. $timediff = $expires - $now;
  869. $filetime = false;
  870. if(file_exists($filename)) {
  871. $filetime = @filemtime($filename);
  872. }
  873. if ($data === null) {
  874. // Read data from file
  875. if (file_exists($filename) && $filetime !== false) {
  876. if ($filetime + $timediff < $now) {
  877. // File has expired
  878. @unlink($filename);
  879. } else {
  880. $data = file_get_contents($filename);
  881. }
  882. }
  883. } else {
  884. file_put_contents($filename, $data);
  885. }
  886. return $data;
  887. }
  888. /**
  889. * Used to delete files in the cache directories, or clear contents of cache directories
  890. *
  891. * @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
  892. * If array, names to be searched for deletion.
  893. * If clearCache() without params, all files in app/tmp/cache/views will be deleted
  894. *
  895. * @param string $type Directory in tmp/cache defaults to view directory
  896. * @param string $ext The file extension you are deleting
  897. * @return true if files found and deleted false otherwise
  898. */
  899. function clearCache($params = null, $type = 'views', $ext = '.php') {
  900. if (is_string($params) || $params === null) {
  901. $params = preg_replace('/\/\//', '/', $params);
  902. $cache = CACHE . $type . DS . $params;
  903. if (is_file($cache . $ext)) {
  904. @unlink($cache . $ext);
  905. return true;
  906. } else if(is_dir($cache)) {
  907. $files = glob("$cache*");
  908. if ($files === false) {
  909. return false;
  910. }
  911. foreach($files as $file) {
  912. if (is_file($file)) {
  913. @unlink($file);
  914. }
  915. }
  916. return true;
  917. } else {
  918. $cache = CACHE . $type . DS . '*' . $params . '*' . $ext;
  919. $files = glob($cache);
  920. if ($files === false) {
  921. return false;
  922. }
  923. foreach($files as $file) {
  924. if (is_file($file)) {
  925. @unlink($file);
  926. }
  927. }
  928. return true;
  929. }
  930. } elseif (is_array($params)) {
  931. foreach($params as $key => $file) {
  932. $file = preg_replace('/\/\//', '/', $file);
  933. $cache = CACHE . $type . DS . '*' . $file . '*' . $ext;
  934. $files[] = glob($cache);
  935. }
  936. if (!empty($files)) {
  937. foreach($files as $key => $delete) {
  938. if (is_array($delete)) {
  939. foreach($delete as $file) {
  940. if (is_file($file)) {
  941. @unlink($file);
  942. }
  943. }
  944. }
  945. }
  946. return true;
  947. } else {
  948. return false;
  949. }
  950. } else {
  951. return false;
  952. }
  953. }
  954. /**
  955. * Recursively strips slashes from all values in an array
  956. *
  957. * @param unknown_type $value
  958. * @return unknown
  959. */
  960. function stripslashes_deep($value) {
  961. if (is_array($value)) {
  962. $return = array_map('stripslashes_deep', $value);
  963. return $return;
  964. } else {
  965. $return = stripslashes($value);
  966. return $return ;
  967. }
  968. }
  969. /**
  970. * Returns a translated string if one is found,
  971. * or the submitted message if not found.
  972. *
  973. * @param unknown_type $msg
  974. * @param unknown_type $return
  975. * @return unknown
  976. * @todo Not implemented fully till 2.0
  977. */
  978. function __($msg, $return = null) {
  979. if (is_null($return)) {
  980. echo($msg);
  981. } else {
  982. return $msg;
  983. }
  984. }
  985. /**
  986. * Counts the dimensions of an array
  987. *
  988. * @param array $array
  989. * @return int The number of dimensions in $array
  990. */
  991. function countdim($array) {
  992. if (is_array(reset($array))) {
  993. $return = countdim(reset($array)) + 1;
  994. } else {
  995. $return = 1;
  996. }
  997. return $return;
  998. }
  999. /**
  1000. * Shortcut to Log::write.
  1001. */
  1002. function LogError($message) {
  1003. if (!class_exists('CakeLog')) {
  1004. uses('cake_log');
  1005. }
  1006. $bad = array("\n", "\r", "\t");
  1007. $good = ' ';
  1008. CakeLog::write('error', str_replace($bad, $good, $message));
  1009. }
  1010. /**
  1011. * Searches include path for files
  1012. *
  1013. * @param string $file
  1014. * @return Full path to file if exists, otherwise false
  1015. */
  1016. function fileExistsInPath($file) {
  1017. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  1018. foreach($paths as $path) {
  1019. $fullPath = $path . DIRECTORY_SEPARATOR . $file;
  1020. if (file_exists($fullPath)) {
  1021. return $fullPath;
  1022. } elseif (file_exists($file)) {
  1023. return $file;
  1024. }
  1025. }
  1026. return false;
  1027. }
  1028. /**
  1029. * Convert forward slashes to underscores and removes first and last underscores in a string
  1030. *
  1031. * @param string
  1032. * @return string with underscore remove from start and end of string
  1033. */
  1034. function convertSlash($string) {
  1035. $string = trim($string,"/");
  1036. $string = preg_replace('/\/\//', '/', $string);
  1037. $string = str_replace('/', '_', $string);
  1038. return $string;
  1039. }
  1040. /**
  1041. * chmod recursively on a directory
  1042. *
  1043. * @param string $path
  1044. * @param int $mode
  1045. * @return boolean
  1046. */
  1047. function chmodr($path, $mode = 0755) {
  1048. if (!is_dir($path)) {
  1049. return chmod($path, $mode);
  1050. }
  1051. $dir = opendir($path);
  1052. while($file = readdir($dir)) {
  1053. if ($file != '.' && $file != '..') {
  1054. $fullpath = $path . '/' . $file;
  1055. if (!is_dir($fullpath)) {
  1056. if (!chmod($fullpath, $mode)) {
  1057. return false;
  1058. }
  1059. } else {
  1060. if (!chmodr($fullpath, $mode)) {
  1061. return false;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. closedir($dir);
  1067. if (chmod($path, $mode)) {
  1068. return true;
  1069. } else {
  1070. return false;
  1071. }
  1072. }
  1073. /**
  1074. * removed the plugin name from the base url
  1075. *
  1076. * @param string $base
  1077. * @param string $plugin
  1078. * @return base url with plugin name removed if present
  1079. */
  1080. function strip_plugin($base, $plugin){
  1081. if ($plugin != null) {
  1082. $base = preg_replace('/' . $plugin . '/', '', $base);
  1083. $base = str_replace('//', '', $base);
  1084. $pos1 = strrpos($base, '/');
  1085. $char = strlen($base) - 1;
  1086. if ($pos1 == $char) {
  1087. $base = substr($base, 0, $char);
  1088. }
  1089. }
  1090. return $base;
  1091. }
  1092. /**
  1093. * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
  1094. *
  1095. * @param mixed $condition Conditional expression
  1096. * @param mixed $val1
  1097. * @param mixed $val2
  1098. * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
  1099. */
  1100. function ife($condition, $val1 = null, $val2 = null) {
  1101. if (!empty($condition)) {
  1102. return $val1;
  1103. }
  1104. return $val2;
  1105. }
  1106. ?>