basics.php 22 KB

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