basics.php 23 KB

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