PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/basics.php

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