basics.php 23 KB

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