basics.php 23 KB

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