basics.php 21 KB

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