basics.php 20 KB

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