basics.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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) {
  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('aero', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info', 'int', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'pro', 'tel', 'travel', 'xxx');
  289. if (in_array($parts[1], $gTLD)) {
  290. return '.' . $host;
  291. }
  292. }
  293. array_shift($parts);
  294. return '.' . implode('.', $parts);
  295. break;
  296. }
  297. return null;
  298. }
  299. /**
  300. * Reads/writes temporary data to cache files or session.
  301. *
  302. * @param string $path File path within /tmp to save the file.
  303. * @param mixed $data The data to save to the temporary file.
  304. * @param mixed $expires A valid strtotime string when the data expires.
  305. * @param string $target The target of the cached data; either 'cache' or 'public'.
  306. * @return mixed The contents of the temporary file.
  307. * @deprecated Please use Cache::write() instead
  308. */
  309. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  310. if (Configure::read('Cache.disable')) {
  311. return null;
  312. }
  313. $now = time();
  314. if (!is_numeric($expires)) {
  315. $expires = strtotime($expires, $now);
  316. }
  317. switch (strtolower($target)) {
  318. case 'cache':
  319. $filename = CACHE . $path;
  320. break;
  321. case 'public':
  322. $filename = WWW_ROOT . $path;
  323. break;
  324. case 'tmp':
  325. $filename = TMP . $path;
  326. break;
  327. }
  328. $timediff = $expires - $now;
  329. $filetime = false;
  330. if (file_exists($filename)) {
  331. $filetime = @filemtime($filename);
  332. }
  333. if ($data === null) {
  334. if (file_exists($filename) && $filetime !== false) {
  335. if ($filetime + $timediff < $now) {
  336. @unlink($filename);
  337. } else {
  338. $data = @file_get_contents($filename);
  339. }
  340. }
  341. } elseif (is_writable(dirname($filename))) {
  342. @file_put_contents($filename, $data);
  343. }
  344. return $data;
  345. }
  346. /**
  347. * Used to delete files in the cache directories, or clear contents of cache directories
  348. *
  349. * @param mixed $params As String name to be searched for deletion, if name is a directory all files in
  350. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  351. * all files in app/tmp/cache/views will be deleted
  352. * @param string $type Directory in tmp/cache defaults to view directory
  353. * @param string $ext The file extension you are deleting
  354. * @return true if files found and deleted false otherwise
  355. */
  356. function clearCache($params = null, $type = 'views', $ext = '.php') {
  357. if (is_string($params) || $params === null) {
  358. $params = preg_replace('/\/\//', '/', $params);
  359. $cache = CACHE . $type . DS . $params;
  360. if (is_file($cache . $ext)) {
  361. @unlink($cache . $ext);
  362. return true;
  363. } elseif (is_dir($cache)) {
  364. $files = glob($cache . '*');
  365. if ($files === false) {
  366. return false;
  367. }
  368. foreach ($files as $file) {
  369. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  370. @unlink($file);
  371. }
  372. }
  373. return true;
  374. } else {
  375. $cache = array(
  376. CACHE . $type . DS . '*' . $params . $ext,
  377. CACHE . $type . DS . '*' . $params . '_*' . $ext
  378. );
  379. $files = array();
  380. while ($search = array_shift($cache)) {
  381. $results = glob($search);
  382. if ($results !== false) {
  383. $files = array_merge($files, $results);
  384. }
  385. }
  386. if (empty($files)) {
  387. return false;
  388. }
  389. foreach ($files as $file) {
  390. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  391. @unlink($file);
  392. }
  393. }
  394. return true;
  395. }
  396. } elseif (is_array($params)) {
  397. foreach ($params as $file) {
  398. clearCache($file, $type, $ext);
  399. }
  400. return true;
  401. }
  402. return false;
  403. }
  404. /**
  405. * Recursively strips slashes from all values in an array
  406. *
  407. * @param array $values Array of values to strip slashes
  408. * @return mixed What is returned from calling stripslashes
  409. * @link http://book.cakephp.org/view/1138/stripslashes_deep
  410. */
  411. function stripslashes_deep($values) {
  412. if (is_array($values)) {
  413. foreach ($values as $key => $value) {
  414. $values[$key] = stripslashes_deep($value);
  415. }
  416. } else {
  417. $values = stripslashes($values);
  418. }
  419. return $values;
  420. }
  421. /**
  422. * Returns a translated string if one is found; Otherwise, the submitted message.
  423. *
  424. * @param string $singular Text to translate
  425. * @param mixed $args Array with arguments or multiple arguments in function
  426. * @return mixed translated string
  427. * @link http://book.cakephp.org/view/1121/__
  428. */
  429. function __($singular, $args = null) {
  430. if (!$singular) {
  431. return;
  432. }
  433. App::uses('I18n', 'I18n');
  434. $translated = I18n::translate($singular);
  435. if ($args === null) {
  436. return $translated;
  437. } elseif (!is_array($args)) {
  438. $args = array_slice(func_get_args(), 1);
  439. }
  440. return vsprintf($translated, $args);
  441. }
  442. /**
  443. * Returns correct plural form of message identified by $singular and $plural for count $count.
  444. * Some languages have more than one form for plural messages dependent on the count.
  445. *
  446. * @param string $singular Singular text to translate
  447. * @param string $plural Plural text
  448. * @param integer $count Count
  449. * @param mixed $args Array with arguments or multiple arguments in function
  450. * @return mixed plural form of translated string
  451. */
  452. function __n($singular, $plural, $count, $args = null) {
  453. if (!$singular) {
  454. return;
  455. }
  456. App::uses('I18n', 'I18n');
  457. $translated = I18n::translate($singular, $plural, null, 6, $count);
  458. if ($args === null) {
  459. return $translated;
  460. } elseif (!is_array($args)) {
  461. $args = array_slice(func_get_args(), 3);
  462. }
  463. return vsprintf($translated, $args);
  464. }
  465. /**
  466. * Allows you to override the current domain for a single message lookup.
  467. *
  468. * @param string $domain Domain
  469. * @param string $msg String to translate
  470. * @param mixed $args Array with arguments or multiple arguments in function
  471. * @return translated string
  472. */
  473. function __d($domain, $msg, $args = null) {
  474. if (!$msg) {
  475. return;
  476. }
  477. App::uses('I18n', 'I18n');
  478. $translated = I18n::translate($msg, null, $domain);
  479. if ($args === null) {
  480. return $translated;
  481. } elseif (!is_array($args)) {
  482. $args = array_slice(func_get_args(), 2);
  483. }
  484. return vsprintf($translated, $args);
  485. }
  486. /**
  487. * Allows you to override the current domain for a single plural message lookup.
  488. * Returns correct plural form of message identified by $singular and $plural for count $count
  489. * from domain $domain.
  490. *
  491. * @param string $domain Domain
  492. * @param string $singular Singular string to translate
  493. * @param string $plural Plural
  494. * @param integer $count Count
  495. * @param mixed $args Array with arguments or multiple arguments in function
  496. * @return plural form of translated string
  497. */
  498. function __dn($domain, $singular, $plural, $count, $args = null) {
  499. if (!$singular) {
  500. return;
  501. }
  502. App::uses('I18n', 'I18n');
  503. $translated = I18n::translate($singular, $plural, $domain, 6, $count);
  504. if ($args === null) {
  505. return $translated;
  506. } elseif (!is_array($args)) {
  507. $args = array_slice(func_get_args(), 4);
  508. }
  509. return vsprintf($translated, $args);
  510. }
  511. /**
  512. * Allows you to override the current domain for a single message lookup.
  513. * It also allows you to specify a category.
  514. *
  515. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  516. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  517. *
  518. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  519. *
  520. * - LC_ALL 0
  521. * - LC_COLLATE 1
  522. * - LC_CTYPE 2
  523. * - LC_MONETARY 3
  524. * - LC_NUMERIC 4
  525. * - LC_TIME 5
  526. * - LC_MESSAGES 6
  527. *
  528. * @param string $domain Domain
  529. * @param string $msg Message to translate
  530. * @param integer $category Category
  531. * @param mixed $args Array with arguments or multiple arguments in function
  532. * @return translated string
  533. */
  534. function __dc($domain, $msg, $category, $args = null) {
  535. if (!$msg) {
  536. return;
  537. }
  538. App::uses('I18n', 'I18n');
  539. $translated = I18n::translate($msg, null, $domain, $category);
  540. if ($args === null) {
  541. return $translated;
  542. } elseif (!is_array($args)) {
  543. $args = array_slice(func_get_args(), 3);
  544. }
  545. return vsprintf($translated, $args);
  546. }
  547. /**
  548. * Allows you to override the current domain for a single plural message lookup.
  549. * It also allows you to specify a category.
  550. * Returns correct plural form of message identified by $singular and $plural for count $count
  551. * from domain $domain.
  552. *
  553. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  554. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  555. *
  556. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  557. *
  558. * - LC_ALL 0
  559. * - LC_COLLATE 1
  560. * - LC_CTYPE 2
  561. * - LC_MONETARY 3
  562. * - LC_NUMERIC 4
  563. * - LC_TIME 5
  564. * - LC_MESSAGES 6
  565. *
  566. * @param string $domain Domain
  567. * @param string $singular Singular string to translate
  568. * @param string $plural Plural
  569. * @param integer $count Count
  570. * @param integer $category Category
  571. * @param mixed $args Array with arguments or multiple arguments in function
  572. * @return plural form of translated string
  573. */
  574. function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
  575. if (!$singular) {
  576. return;
  577. }
  578. App::uses('I18n', 'I18n');
  579. $translated = I18n::translate($singular, $plural, $domain, $category, $count);
  580. if ($args === null) {
  581. return $translated;
  582. } elseif (!is_array($args)) {
  583. $args = array_slice(func_get_args(), 5);
  584. }
  585. return vsprintf($translated, $args);
  586. }
  587. /**
  588. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  589. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  590. *
  591. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  592. *
  593. * - LC_ALL 0
  594. * - LC_COLLATE 1
  595. * - LC_CTYPE 2
  596. * - LC_MONETARY 3
  597. * - LC_NUMERIC 4
  598. * - LC_TIME 5
  599. * - LC_MESSAGES 6
  600. *
  601. * @param string $msg String to translate
  602. * @param integer $category Category
  603. * @param mixed $args Array with arguments or multiple arguments in function
  604. * @return translated string
  605. */
  606. function __c($msg, $category, $args = null) {
  607. if (!$msg) {
  608. return;
  609. }
  610. App::uses('I18n', 'I18n');
  611. $translated = I18n::translate($msg, null, null, $category);
  612. if ($args === null) {
  613. return $translated;
  614. } elseif (!is_array($args)) {
  615. $args = array_slice(func_get_args(), 2);
  616. }
  617. return vsprintf($translated, $args);
  618. }
  619. /**
  620. * Shortcut to Log::write.
  621. *
  622. * @param string $message Message to write to log
  623. */
  624. function LogError($message) {
  625. App::uses('CakeLog', 'Log');
  626. $bad = array("\n", "\r", "\t");
  627. $good = ' ';
  628. CakeLog::write('error', str_replace($bad, $good, $message));
  629. }
  630. /**
  631. * Searches include path for files.
  632. *
  633. * @param string $file File to look for
  634. * @return Full path to file if exists, otherwise false
  635. * @link http://book.cakephp.org/view/1131/fileExistsInPath
  636. */
  637. function fileExistsInPath($file) {
  638. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  639. foreach ($paths as $path) {
  640. $fullPath = $path . DS . $file;
  641. if (file_exists($fullPath)) {
  642. return $fullPath;
  643. } elseif (file_exists($file)) {
  644. return $file;
  645. }
  646. }
  647. return false;
  648. }
  649. /**
  650. * Convert forward slashes to underscores and removes first and last underscores in a string
  651. *
  652. * @param string String to convert
  653. * @return string with underscore remove from start and end of string
  654. * @link http://book.cakephp.org/view/1126/convertSlash
  655. */
  656. function convertSlash($string) {
  657. $string = trim($string, '/');
  658. $string = preg_replace('/\/\//', '/', $string);
  659. $string = str_replace('/', '_', $string);
  660. return $string;
  661. }