basics.php 23 KB

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