basics.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /**
  3. * Basic Cake functionality.
  4. *
  5. * Core functions for including other source files, loading models and so forth.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @since CakePHP(tm) v 0.2.9
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Basic defines for timing functions.
  23. */
  24. define('SECOND', 1);
  25. define('MINUTE', 60);
  26. define('HOUR', 3600);
  27. define('DAY', 86400);
  28. define('WEEK', 604800);
  29. define('MONTH', 2592000);
  30. define('YEAR', 31536000);
  31. /**
  32. * Loads configuration files. Receives a set of configuration files
  33. * to load.
  34. * Example:
  35. *
  36. * `config('config1', 'config2');`
  37. *
  38. * @return boolean Success
  39. * @link http://book.cakephp.org/view/1125/config
  40. */
  41. function config() {
  42. $args = func_get_args();
  43. foreach ($args as $arg) {
  44. if ($arg === 'database' && file_exists(CONFIGS . 'database.php')) {
  45. include_once(CONFIGS . $arg . '.php');
  46. } elseif (file_exists(CONFIGS . $arg . '.php')) {
  47. include_once(CONFIGS . $arg . '.php');
  48. if (count($args) == 1) {
  49. return true;
  50. }
  51. } else {
  52. if (count($args) == 1) {
  53. return false;
  54. }
  55. }
  56. }
  57. return true;
  58. }
  59. /**
  60. * Prints out debug information about given variable.
  61. *
  62. * Only runs if debug level is greater than zero.
  63. *
  64. * @param boolean $var Variable to show debug information for.
  65. * @param boolean $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  66. * @param boolean $showFrom If set to true, the method prints from where the function was called.
  67. * @link http://book.cakephp.org/view/1190/Basic-Debugging
  68. * @link http://book.cakephp.org/view/1128/debug
  69. */
  70. function debug($var = false, $showHtml = null, $showFrom = true) {
  71. if (Configure::read('debug') > 0) {
  72. $file = '';
  73. $line = '';
  74. if ($showFrom) {
  75. $calledFrom = debug_backtrace();
  76. $file = substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1);
  77. $line = $calledFrom[0]['line'];
  78. }
  79. $html = <<<HTML
  80. <strong>%s</strong> (line <strong>%s</strong>)
  81. <pre class="cake-debug">
  82. %s
  83. </pre>
  84. HTML;
  85. $text = <<<TEXT
  86. %s (line %s)
  87. ########## DEBUG ##########
  88. %s
  89. ###########################
  90. TEXT;
  91. $template = $html;
  92. if (php_sapi_name() == 'cli') {
  93. $template = $text;
  94. }
  95. if ($showHtml === null && $template !== $text) {
  96. $showHtml = true;
  97. }
  98. $var = print_r($var, true);
  99. if ($showHtml) {
  100. $var = str_replace(array('<', '>'), array('&lt;', '&gt;'), $var);
  101. }
  102. printf($template, $file, $line, $var);
  103. }
  104. }
  105. if (!function_exists('sortByKey')) {
  106. /**
  107. * Sorts given $array by key $sortby.
  108. *
  109. * @param array $array Array to sort
  110. * @param string $sortby Sort by this key
  111. * @param string $order Sort order asc/desc (ascending or descending).
  112. * @param integer $type Type of sorting to perform
  113. * @return mixed Sorted array
  114. */
  115. function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
  116. if (!is_array($array)) {
  117. return null;
  118. }
  119. foreach ($array as $key => $val) {
  120. $sa[$key] = $val[$sortby];
  121. }
  122. if ($order == 'asc') {
  123. asort($sa, $type);
  124. } else {
  125. arsort($sa, $type);
  126. }
  127. foreach ($sa as $key => $val) {
  128. $out[] = $array[$key];
  129. }
  130. return $out;
  131. }
  132. }
  133. /**
  134. * Convenience method for htmlspecialchars.
  135. *
  136. * @param string $text Text to wrap through htmlspecialchars
  137. * @param boolean $double Encode existing html entities
  138. * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
  139. * @return string Wrapped text
  140. * @link http://book.cakephp.org/view/1132/h
  141. */
  142. function h($text, $double = true, $charset = null) {
  143. if (is_array($text)) {
  144. $texts = array();
  145. foreach ($text as $k => $t) {
  146. $texts[$k] = h($t, $double, $charset);
  147. }
  148. return $texts;
  149. }
  150. static $defaultCharset = false;
  151. if ($defaultCharset === false) {
  152. $defaultCharset = Configure::read('App.encoding');
  153. if ($defaultCharset === null) {
  154. $defaultCharset = 'UTF-8';
  155. }
  156. }
  157. if (is_string($double)) {
  158. $charset = $double;
  159. }
  160. return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
  161. }
  162. /**
  163. * Splits a dot syntax plugin name into its plugin and classname.
  164. * If $name does not have a dot, then index 0 will be null.
  165. *
  166. * Commonly used like `list($plugin, $name) = pluginSplit($name);`
  167. *
  168. * @param string $name The name you want to plugin split.
  169. * @param boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  170. * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  171. * @return array Array with 2 indexes. 0 => plugin name, 1 => classname
  172. */
  173. function pluginSplit($name, $dotAppend = false, $plugin = null) {
  174. if (strpos($name, '.') !== false) {
  175. $parts = explode('.', $name, 2);
  176. if ($dotAppend) {
  177. $parts[0] .= '.';
  178. }
  179. return $parts;
  180. }
  181. return array($plugin, $name);
  182. }
  183. /**
  184. * Print_r convenience function, which prints out <PRE> tags around
  185. * the output of given array. Similar to debug().
  186. *
  187. * @see debug()
  188. * @param array $var Variable to print out
  189. * @link http://book.cakephp.org/view/1136/pr
  190. */
  191. function pr($var) {
  192. if (Configure::read('debug') > 0) {
  193. echo '<pre>';
  194. print_r($var);
  195. echo '</pre>';
  196. }
  197. }
  198. /**
  199. * Merge a group of arrays
  200. *
  201. * @param array First array
  202. * @param array Second array
  203. * @param array Third array
  204. * @param array Etc...
  205. * @return array All array parameters merged into one
  206. * @link http://book.cakephp.org/view/1124/am
  207. */
  208. function am() {
  209. $r = array();
  210. $args = func_get_args();
  211. foreach ($args as $a) {
  212. if (!is_array($a)) {
  213. $a = array($a);
  214. }
  215. $r = array_merge($r, $a);
  216. }
  217. return $r;
  218. }
  219. /**
  220. * Gets an environment variable from available sources, and provides emulation
  221. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  222. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  223. * environment information.
  224. *
  225. * @param string $key Environment variable name.
  226. * @return string Environment variable setting.
  227. * @link http://book.cakephp.org/view/1130/env
  228. */
  229. function env($key) {
  230. if ($key === 'HTTPS') {
  231. if (isset($_SERVER['HTTPS'])) {
  232. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  233. }
  234. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  235. }
  236. if ($key === 'SCRIPT_NAME') {
  237. if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  238. $key = 'SCRIPT_URL';
  239. }
  240. }
  241. $val = null;
  242. if (isset($_SERVER[$key])) {
  243. $val = $_SERVER[$key];
  244. } elseif (isset($_ENV[$key])) {
  245. $val = $_ENV[$key];
  246. } elseif (getenv($key) !== false) {
  247. $val = getenv($key);
  248. }
  249. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  250. $addr = env('HTTP_PC_REMOTE_ADDR');
  251. if ($addr !== null) {
  252. $val = $addr;
  253. }
  254. }
  255. if ($val !== null) {
  256. return $val;
  257. }
  258. switch ($key) {
  259. case 'SCRIPT_FILENAME':
  260. if (defined('SERVER_IIS') && SERVER_IIS === true) {
  261. return str_replace('\\\\', '\\', env('PATH_TRANSLATED'));
  262. }
  263. break;
  264. case 'DOCUMENT_ROOT':
  265. $name = env('SCRIPT_NAME');
  266. $filename = env('SCRIPT_FILENAME');
  267. $offset = 0;
  268. if (!strpos($name, '.php')) {
  269. $offset = 4;
  270. }
  271. return substr($filename, 0, strlen($filename) - (strlen($name) + $offset));
  272. break;
  273. case 'PHP_SELF':
  274. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  275. break;
  276. case 'CGI_MODE':
  277. return (PHP_SAPI === 'cgi');
  278. break;
  279. case 'HTTP_BASE':
  280. $host = env('HTTP_HOST');
  281. $parts = explode('.', $host);
  282. $count = count($parts);
  283. if ($count === 1) {
  284. return '.' . $host;
  285. } elseif ($count === 2) {
  286. return '.' . $host;
  287. } elseif ($count === 3) {
  288. $gTLD = array(
  289. 'aero',
  290. 'asia',
  291. 'biz',
  292. 'cat',
  293. 'com',
  294. 'coop',
  295. 'edu',
  296. 'gov',
  297. 'info',
  298. 'int',
  299. 'jobs',
  300. 'mil',
  301. 'mobi',
  302. 'museum',
  303. 'name',
  304. 'net',
  305. 'org',
  306. 'pro',
  307. 'tel',
  308. 'travel',
  309. 'xxx'
  310. );
  311. if (in_array($parts[1], $gTLD)) {
  312. return '.' . $host;
  313. }
  314. }
  315. array_shift($parts);
  316. return '.' . implode('.', $parts);
  317. break;
  318. }
  319. return null;
  320. }
  321. /**
  322. * Reads/writes temporary data to cache files or session.
  323. *
  324. * @param string $path File path within /tmp to save the file.
  325. * @param mixed $data The data to save to the temporary file.
  326. * @param mixed $expires A valid strtotime string when the data expires.
  327. * @param string $target The target of the cached data; either 'cache' or 'public'.
  328. * @return mixed The contents of the temporary file.
  329. * @deprecated Please use Cache::write() instead
  330. */
  331. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  332. if (Configure::read('Cache.disable')) {
  333. return null;
  334. }
  335. $now = time();
  336. if (!is_numeric($expires)) {
  337. $expires = strtotime($expires, $now);
  338. }
  339. switch (strtolower($target)) {
  340. case 'cache':
  341. $filename = CACHE . $path;
  342. break;
  343. case 'public':
  344. $filename = WWW_ROOT . $path;
  345. break;
  346. case 'tmp':
  347. $filename = TMP . $path;
  348. break;
  349. }
  350. $timediff = $expires - $now;
  351. $filetime = false;
  352. if (file_exists($filename)) {
  353. $filetime = @filemtime($filename);
  354. }
  355. if ($data === null) {
  356. if (file_exists($filename) && $filetime !== false) {
  357. if ($filetime + $timediff < $now) {
  358. @unlink($filename);
  359. } else {
  360. $data = @file_get_contents($filename);
  361. }
  362. }
  363. } elseif (is_writable(dirname($filename))) {
  364. @file_put_contents($filename, $data);
  365. }
  366. return $data;
  367. }
  368. /**
  369. * Used to delete files in the cache directories, or clear contents of cache directories
  370. *
  371. * @param mixed $params As String name to be searched for deletion, if name is a directory all files in
  372. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  373. * all files in app/tmp/cache/views will be deleted
  374. * @param string $type Directory in tmp/cache defaults to view directory
  375. * @param string $ext The file extension you are deleting
  376. * @return true if files found and deleted false otherwise
  377. */
  378. function clearCache($params = null, $type = 'views', $ext = '.php') {
  379. if (is_string($params) || $params === null) {
  380. $params = preg_replace('/\/\//', '/', $params);
  381. $cache = CACHE . $type . DS . $params;
  382. if (is_file($cache . $ext)) {
  383. @unlink($cache . $ext);
  384. return true;
  385. } elseif (is_dir($cache)) {
  386. $files = glob($cache . '*');
  387. if ($files === false) {
  388. return false;
  389. }
  390. foreach ($files as $file) {
  391. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  392. @unlink($file);
  393. }
  394. }
  395. return true;
  396. } else {
  397. $cache = array(
  398. CACHE . $type . DS . '*' . $params . $ext,
  399. CACHE . $type . DS . '*' . $params . '_*' . $ext
  400. );
  401. $files = array();
  402. while ($search = array_shift($cache)) {
  403. $results = glob($search);
  404. if ($results !== false) {
  405. $files = array_merge($files, $results);
  406. }
  407. }
  408. if (empty($files)) {
  409. return false;
  410. }
  411. foreach ($files as $file) {
  412. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  413. @unlink($file);
  414. }
  415. }
  416. return true;
  417. }
  418. } elseif (is_array($params)) {
  419. foreach ($params as $file) {
  420. clearCache($file, $type, $ext);
  421. }
  422. return true;
  423. }
  424. return false;
  425. }
  426. /**
  427. * Recursively strips slashes from all values in an array
  428. *
  429. * @param array $values Array of values to strip slashes
  430. * @return mixed What is returned from calling stripslashes
  431. * @link http://book.cakephp.org/view/1138/stripslashes_deep
  432. */
  433. function stripslashes_deep($values) {
  434. if (is_array($values)) {
  435. foreach ($values as $key => $value) {
  436. $values[$key] = stripslashes_deep($value);
  437. }
  438. } else {
  439. $values = stripslashes($values);
  440. }
  441. return $values;
  442. }
  443. /**
  444. * Returns a translated string if one is found; Otherwise, the submitted message.
  445. *
  446. * @param string $singular Text to translate
  447. * @param mixed $args Array with arguments or multiple arguments in function
  448. * @return mixed translated string
  449. * @link http://book.cakephp.org/view/1121/__
  450. */
  451. function __($singular, $args = null) {
  452. if (!$singular) {
  453. return;
  454. }
  455. App::uses('I18n', 'I18n');
  456. $translated = I18n::translate($singular);
  457. if ($args === null) {
  458. return $translated;
  459. } elseif (!is_array($args)) {
  460. $args = array_slice(func_get_args(), 1);
  461. }
  462. return vsprintf($translated, $args);
  463. }
  464. /**
  465. * Returns correct plural form of message identified by $singular and $plural for count $count.
  466. * Some languages have more than one form for plural messages dependent on the count.
  467. *
  468. * @param string $singular Singular text to translate
  469. * @param string $plural Plural text
  470. * @param integer $count Count
  471. * @param mixed $args Array with arguments or multiple arguments in function
  472. * @return mixed plural form of translated string
  473. */
  474. function __n($singular, $plural, $count, $args = null) {
  475. if (!$singular) {
  476. return;
  477. }
  478. App::uses('I18n', 'I18n');
  479. $translated = I18n::translate($singular, $plural, null, 6, $count);
  480. if ($args === null) {
  481. return $translated;
  482. } elseif (!is_array($args)) {
  483. $args = array_slice(func_get_args(), 3);
  484. }
  485. return vsprintf($translated, $args);
  486. }
  487. /**
  488. * Allows you to override the current domain for a single message lookup.
  489. *
  490. * @param string $domain Domain
  491. * @param string $msg String to translate
  492. * @param mixed $args Array with arguments or multiple arguments in function
  493. * @return translated string
  494. */
  495. function __d($domain, $msg, $args = null) {
  496. if (!$msg) {
  497. return;
  498. }
  499. App::uses('I18n', 'I18n');
  500. $translated = I18n::translate($msg, null, $domain);
  501. if ($args === null) {
  502. return $translated;
  503. } elseif (!is_array($args)) {
  504. $args = array_slice(func_get_args(), 2);
  505. }
  506. return vsprintf($translated, $args);
  507. }
  508. /**
  509. * Allows you to override the current domain for a single plural message lookup.
  510. * Returns correct plural form of message identified by $singular and $plural for count $count
  511. * from domain $domain.
  512. *
  513. * @param string $domain Domain
  514. * @param string $singular Singular string to translate
  515. * @param string $plural Plural
  516. * @param integer $count Count
  517. * @param mixed $args Array with arguments or multiple arguments in function
  518. * @return plural form of translated string
  519. */
  520. function __dn($domain, $singular, $plural, $count, $args = null) {
  521. if (!$singular) {
  522. return;
  523. }
  524. App::uses('I18n', 'I18n');
  525. $translated = I18n::translate($singular, $plural, $domain, 6, $count);
  526. if ($args === null) {
  527. return $translated;
  528. } elseif (!is_array($args)) {
  529. $args = array_slice(func_get_args(), 4);
  530. }
  531. return vsprintf($translated, $args);
  532. }
  533. /**
  534. * Allows you to override the current domain for a single message lookup.
  535. * It also allows you to specify a category.
  536. *
  537. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  538. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  539. *
  540. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  541. *
  542. * - LC_ALL 0
  543. * - LC_COLLATE 1
  544. * - LC_CTYPE 2
  545. * - LC_MONETARY 3
  546. * - LC_NUMERIC 4
  547. * - LC_TIME 5
  548. * - LC_MESSAGES 6
  549. *
  550. * @param string $domain Domain
  551. * @param string $msg Message to translate
  552. * @param integer $category Category
  553. * @param mixed $args Array with arguments or multiple arguments in function
  554. * @return translated string
  555. */
  556. function __dc($domain, $msg, $category, $args = null) {
  557. if (!$msg) {
  558. return;
  559. }
  560. App::uses('I18n', 'I18n');
  561. $translated = I18n::translate($msg, null, $domain, $category);
  562. if ($args === null) {
  563. return $translated;
  564. } elseif (!is_array($args)) {
  565. $args = array_slice(func_get_args(), 3);
  566. }
  567. return vsprintf($translated, $args);
  568. }
  569. /**
  570. * Allows you to override the current domain for a single plural message lookup.
  571. * It also allows you to specify a category.
  572. * Returns correct plural form of message identified by $singular and $plural for count $count
  573. * from domain $domain.
  574. *
  575. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  576. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  577. *
  578. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  579. *
  580. * - LC_ALL 0
  581. * - LC_COLLATE 1
  582. * - LC_CTYPE 2
  583. * - LC_MONETARY 3
  584. * - LC_NUMERIC 4
  585. * - LC_TIME 5
  586. * - LC_MESSAGES 6
  587. *
  588. * @param string $domain Domain
  589. * @param string $singular Singular string to translate
  590. * @param string $plural Plural
  591. * @param integer $count Count
  592. * @param integer $category Category
  593. * @param mixed $args Array with arguments or multiple arguments in function
  594. * @return plural form of translated string
  595. */
  596. function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
  597. if (!$singular) {
  598. return;
  599. }
  600. App::uses('I18n', 'I18n');
  601. $translated = I18n::translate($singular, $plural, $domain, $category, $count);
  602. if ($args === null) {
  603. return $translated;
  604. } elseif (!is_array($args)) {
  605. $args = array_slice(func_get_args(), 5);
  606. }
  607. return vsprintf($translated, $args);
  608. }
  609. /**
  610. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  611. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  612. *
  613. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  614. *
  615. * - LC_ALL 0
  616. * - LC_COLLATE 1
  617. * - LC_CTYPE 2
  618. * - LC_MONETARY 3
  619. * - LC_NUMERIC 4
  620. * - LC_TIME 5
  621. * - LC_MESSAGES 6
  622. *
  623. * @param string $msg String to translate
  624. * @param integer $category Category
  625. * @param mixed $args Array with arguments or multiple arguments in function
  626. * @return translated string
  627. */
  628. function __c($msg, $category, $args = null) {
  629. if (!$msg) {
  630. return;
  631. }
  632. App::uses('I18n', 'I18n');
  633. $translated = I18n::translate($msg, null, null, $category);
  634. if ($args === null) {
  635. return $translated;
  636. } elseif (!is_array($args)) {
  637. $args = array_slice(func_get_args(), 2);
  638. }
  639. return vsprintf($translated, $args);
  640. }
  641. /**
  642. * Shortcut to Log::write.
  643. *
  644. * @param string $message Message to write to log
  645. */
  646. function LogError($message) {
  647. App::uses('CakeLog', 'Log');
  648. $bad = array("\n", "\r", "\t");
  649. $good = ' ';
  650. CakeLog::write('error', str_replace($bad, $good, $message));
  651. }
  652. /**
  653. * Searches include path for files.
  654. *
  655. * @param string $file File to look for
  656. * @return Full path to file if exists, otherwise false
  657. * @link http://book.cakephp.org/view/1131/fileExistsInPath
  658. */
  659. function fileExistsInPath($file) {
  660. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  661. foreach ($paths as $path) {
  662. $fullPath = $path . DS . $file;
  663. if (file_exists($fullPath)) {
  664. return $fullPath;
  665. } elseif (file_exists($file)) {
  666. return $file;
  667. }
  668. }
  669. return false;
  670. }
  671. /**
  672. * Convert forward slashes to underscores and removes first and last underscores in a string
  673. *
  674. * @param string String to convert
  675. * @return string with underscore remove from start and end of string
  676. * @link http://book.cakephp.org/view/1126/convertSlash
  677. */
  678. function convertSlash($string) {
  679. $string = trim($string, '/');
  680. $string = preg_replace('/\/\//', '/', $string);
  681. $string = str_replace('/', '_', $string);
  682. return $string;
  683. }