basics.php 20 KB

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