basics.php 22 KB

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