basics.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. /**
  3. * Basic CakePHP functionality.
  4. *
  5. * Core functions for including other source files, loading models and so forth.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake
  17. * @since CakePHP(tm) v 0.2.9
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Basic defines for timing functions.
  22. */
  23. define('SECOND', 1);
  24. define('MINUTE', 60);
  25. define('HOUR', 3600);
  26. define('DAY', 86400);
  27. define('WEEK', 604800);
  28. define('MONTH', 2592000);
  29. define('YEAR', 31536000);
  30. if (!function_exists('config')) {
  31. /**
  32. * Loads configuration files. Receives a set of configuration files
  33. * to load.
  34. * Example:
  35. *
  36. * `config('config1', 'config2');`
  37. *
  38. * @return bool Success
  39. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#config
  40. */
  41. function config() {
  42. $args = func_get_args();
  43. $count = count($args);
  44. $included = 0;
  45. foreach ($args as $arg) {
  46. if (file_exists(APP . 'Config' . DS . $arg . '.php')) {
  47. include_once APP . 'Config' . DS . $arg . '.php';
  48. $included++;
  49. }
  50. }
  51. return $included === $count;
  52. }
  53. }
  54. if (!function_exists('debug')) {
  55. /**
  56. * Prints out debug information about given variable.
  57. *
  58. * Only runs if debug level is greater than zero.
  59. *
  60. * @param mixed $var Variable to show debug information for.
  61. * @param bool $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  62. * @param bool $showFrom If set to true, the method prints from where the function was called.
  63. * @return void
  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 int $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 bool $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 class name.
  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 bool $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 => class name
  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
  211. *
  212. * In terminals this will act the same as using print_r() directly, when not run on cli
  213. * print_r() will wrap <PRE> tags around the output of given array. Similar to debug().
  214. *
  215. * @param mixed $var Variable to print out
  216. * @return void
  217. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pr
  218. * @see debug()
  219. */
  220. function pr($var) {
  221. if (Configure::read('debug') > 0) {
  222. $template = php_sapi_name() !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
  223. printf($template, print_r($var, true));
  224. }
  225. }
  226. }
  227. if (!function_exists('am')) {
  228. /**
  229. * Merge a group of arrays
  230. *
  231. * Accepts variable arguments. Each argument will be converted into an array and then merged.
  232. *
  233. * @return array All array parameters merged into one
  234. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.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 'DOCUMENT_ROOT':
  290. $name = env('SCRIPT_NAME');
  291. $filename = env('SCRIPT_FILENAME');
  292. $offset = 0;
  293. if (!strpos($name, '.php')) {
  294. $offset = 4;
  295. }
  296. return substr($filename, 0, -(strlen($name) + $offset));
  297. case 'PHP_SELF':
  298. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  299. case 'CGI_MODE':
  300. return (PHP_SAPI === 'cgi');
  301. case 'HTTP_BASE':
  302. $host = env('HTTP_HOST');
  303. $parts = explode('.', $host);
  304. $count = count($parts);
  305. if ($count === 1) {
  306. return '.' . $host;
  307. } elseif ($count === 2) {
  308. return '.' . $host;
  309. } elseif ($count === 3) {
  310. $gTLD = array(
  311. 'aero',
  312. 'asia',
  313. 'biz',
  314. 'cat',
  315. 'com',
  316. 'coop',
  317. 'edu',
  318. 'gov',
  319. 'info',
  320. 'int',
  321. 'jobs',
  322. 'mil',
  323. 'mobi',
  324. 'museum',
  325. 'name',
  326. 'net',
  327. 'org',
  328. 'pro',
  329. 'tel',
  330. 'travel',
  331. 'xxx'
  332. );
  333. if (in_array($parts[1], $gTLD)) {
  334. return '.' . $host;
  335. }
  336. }
  337. array_shift($parts);
  338. return '.' . implode('.', $parts);
  339. }
  340. return null;
  341. }
  342. }
  343. if (!function_exists('cache')) {
  344. /**
  345. * Reads/writes temporary data to cache files or session.
  346. *
  347. * @param string $path File path within /tmp to save the file.
  348. * @param mixed $data The data to save to the temporary file.
  349. * @param mixed $expires A valid strtotime string when the data expires.
  350. * @param string $target The target of the cached data; either 'cache' or 'public'.
  351. * @return mixed The contents of the temporary file.
  352. * @deprecated Will be removed in 3.0. Please use Cache::write() instead.
  353. */
  354. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  355. if (Configure::read('Cache.disable')) {
  356. return null;
  357. }
  358. $now = time();
  359. if (!is_numeric($expires)) {
  360. $expires = strtotime($expires, $now);
  361. }
  362. switch (strtolower($target)) {
  363. case 'cache':
  364. $filename = CACHE . $path;
  365. break;
  366. case 'public':
  367. $filename = WWW_ROOT . $path;
  368. break;
  369. case 'tmp':
  370. $filename = TMP . $path;
  371. break;
  372. }
  373. $timediff = $expires - $now;
  374. $filetime = false;
  375. if (file_exists($filename)) {
  376. //@codingStandardsIgnoreStart
  377. $filetime = @filemtime($filename);
  378. //@codingStandardsIgnoreEnd
  379. }
  380. if ($data === null) {
  381. if (file_exists($filename) && $filetime !== false) {
  382. if ($filetime + $timediff < $now) {
  383. //@codingStandardsIgnoreStart
  384. @unlink($filename);
  385. //@codingStandardsIgnoreEnd
  386. } else {
  387. //@codingStandardsIgnoreStart
  388. $data = @file_get_contents($filename);
  389. //@codingStandardsIgnoreEnd
  390. }
  391. }
  392. } elseif (is_writable(dirname($filename))) {
  393. //@codingStandardsIgnoreStart
  394. @file_put_contents($filename, $data, LOCK_EX);
  395. //@codingStandardsIgnoreEnd
  396. }
  397. return $data;
  398. }
  399. }
  400. if (!function_exists('clearCache')) {
  401. /**
  402. * Used to delete files in the cache directories, or clear contents of cache directories
  403. *
  404. * @param string|array $params As String name to be searched for deletion, if name is a directory all files in
  405. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  406. * all files in app/tmp/cache/views will be deleted
  407. * @param string $type Directory in tmp/cache defaults to view directory
  408. * @param string $ext The file extension you are deleting
  409. * @return true if files found and deleted false otherwise
  410. */
  411. function clearCache($params = null, $type = 'views', $ext = '.php') {
  412. if (is_string($params) || $params === null) {
  413. $params = preg_replace('/\/\//', '/', $params);
  414. $cache = CACHE . $type . DS . $params;
  415. if (is_file($cache . $ext)) {
  416. //@codingStandardsIgnoreStart
  417. @unlink($cache . $ext);
  418. //@codingStandardsIgnoreEnd
  419. return true;
  420. } elseif (is_dir($cache)) {
  421. $files = glob($cache . '*');
  422. if ($files === false) {
  423. return false;
  424. }
  425. foreach ($files as $file) {
  426. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  427. //@codingStandardsIgnoreStart
  428. @unlink($file);
  429. //@codingStandardsIgnoreEnd
  430. }
  431. }
  432. return true;
  433. }
  434. $cache = array(
  435. CACHE . $type . DS . '*' . $params . $ext,
  436. CACHE . $type . DS . '*' . $params . '_*' . $ext
  437. );
  438. $files = array();
  439. while ($search = array_shift($cache)) {
  440. $results = glob($search);
  441. if ($results !== false) {
  442. $files = array_merge($files, $results);
  443. }
  444. }
  445. if (empty($files)) {
  446. return false;
  447. }
  448. foreach ($files as $file) {
  449. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  450. //@codingStandardsIgnoreStart
  451. @unlink($file);
  452. //@codingStandardsIgnoreEnd
  453. }
  454. }
  455. return true;
  456. } elseif (is_array($params)) {
  457. foreach ($params as $file) {
  458. clearCache($file, $type, $ext);
  459. }
  460. return true;
  461. }
  462. return false;
  463. }
  464. }
  465. if (!function_exists('stripslashes_deep')) {
  466. /**
  467. * Recursively strips slashes from all values in an array
  468. *
  469. * @param array $values Array of values to strip slashes
  470. * @return mixed What is returned from calling stripslashes
  471. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#stripslashes_deep
  472. */
  473. function stripslashes_deep($values) {
  474. if (is_array($values)) {
  475. foreach ($values as $key => $value) {
  476. $values[$key] = stripslashes_deep($value);
  477. }
  478. } else {
  479. $values = stripslashes($values);
  480. }
  481. return $values;
  482. }
  483. }
  484. if (!function_exists('__')) {
  485. /**
  486. * Returns a translated string if one is found; Otherwise, the submitted message.
  487. *
  488. * @param string $singular Text to translate
  489. * @param mixed $args Array with arguments or multiple arguments in function
  490. * @return mixed translated string
  491. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
  492. */
  493. function __($singular, $args = null) {
  494. if (!$singular) {
  495. return;
  496. }
  497. App::uses('I18n', 'I18n');
  498. $translated = I18n::translate($singular);
  499. if ($args === null) {
  500. return $translated;
  501. } elseif (!is_array($args)) {
  502. $args = array_slice(func_get_args(), 1);
  503. }
  504. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  505. return vsprintf($translated, $args);
  506. }
  507. }
  508. if (!function_exists('__n')) {
  509. /**
  510. * Returns correct plural form of message identified by $singular and $plural for count $count.
  511. * Some languages have more than one form for plural messages dependent on the count.
  512. *
  513. * @param string $singular Singular text to translate
  514. * @param string $plural Plural text
  515. * @param int $count Count
  516. * @param mixed $args Array with arguments or multiple arguments in function
  517. * @return mixed plural form of translated string
  518. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
  519. */
  520. function __n($singular, $plural, $count, $args = null) {
  521. if (!$singular) {
  522. return;
  523. }
  524. App::uses('I18n', 'I18n');
  525. $translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count);
  526. if ($args === null) {
  527. return $translated;
  528. } elseif (!is_array($args)) {
  529. $args = array_slice(func_get_args(), 3);
  530. }
  531. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  532. return vsprintf($translated, $args);
  533. }
  534. }
  535. if (!function_exists('__d')) {
  536. /**
  537. * Allows you to override the current domain for a single message lookup.
  538. *
  539. * @param string $domain Domain
  540. * @param string $msg String to translate
  541. * @param mixed $args Array with arguments or multiple arguments in function
  542. * @return translated string
  543. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__d
  544. */
  545. function __d($domain, $msg, $args = null) {
  546. if (!$msg) {
  547. return;
  548. }
  549. App::uses('I18n', 'I18n');
  550. $translated = I18n::translate($msg, null, $domain);
  551. if ($args === null) {
  552. return $translated;
  553. } elseif (!is_array($args)) {
  554. $args = array_slice(func_get_args(), 2);
  555. }
  556. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  557. return vsprintf($translated, $args);
  558. }
  559. }
  560. if (!function_exists('__dn')) {
  561. /**
  562. * Allows you to override the current domain for a single plural message lookup.
  563. * Returns correct plural form of message identified by $singular and $plural for count $count
  564. * from domain $domain.
  565. *
  566. * @param string $domain Domain
  567. * @param string $singular Singular string to translate
  568. * @param string $plural Plural
  569. * @param int $count Count
  570. * @param mixed $args Array with arguments or multiple arguments in function
  571. * @return plural form of translated string
  572. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
  573. */
  574. function __dn($domain, $singular, $plural, $count, $args = null) {
  575. if (!$singular) {
  576. return;
  577. }
  578. App::uses('I18n', 'I18n');
  579. $translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count);
  580. if ($args === null) {
  581. return $translated;
  582. } elseif (!is_array($args)) {
  583. $args = array_slice(func_get_args(), 4);
  584. }
  585. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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 class constant of I18n, instead of the constant name. The values are:
  598. *
  599. * - LC_ALL I18n::LC_ALL
  600. * - LC_COLLATE I18n::LC_COLLATE
  601. * - LC_CTYPE I18n::LC_CTYPE
  602. * - LC_MONETARY I18n::LC_MONETARY
  603. * - LC_NUMERIC I18n::LC_NUMERIC
  604. * - LC_TIME I18n::LC_TIME
  605. * - LC_MESSAGES I18n::LC_MESSAGES
  606. *
  607. * @param string $domain Domain
  608. * @param string $msg Message to translate
  609. * @param int $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. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  626. return vsprintf($translated, $args);
  627. }
  628. }
  629. if (!function_exists('__dcn')) {
  630. /**
  631. * Allows you to override the current domain for a single plural message lookup.
  632. * It also allows you to specify a category.
  633. * Returns correct plural form of message identified by $singular and $plural for count $count
  634. * from domain $domain.
  635. *
  636. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  637. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  638. *
  639. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  640. *
  641. * - LC_ALL I18n::LC_ALL
  642. * - LC_COLLATE I18n::LC_COLLATE
  643. * - LC_CTYPE I18n::LC_CTYPE
  644. * - LC_MONETARY I18n::LC_MONETARY
  645. * - LC_NUMERIC I18n::LC_NUMERIC
  646. * - LC_TIME I18n::LC_TIME
  647. * - LC_MESSAGES I18n::LC_MESSAGES
  648. *
  649. * @param string $domain Domain
  650. * @param string $singular Singular string to translate
  651. * @param string $plural Plural
  652. * @param int $count Count
  653. * @param int $category Category
  654. * @param mixed $args Array with arguments or multiple arguments in function
  655. * @return plural form of translated string
  656. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dcn
  657. */
  658. function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
  659. if (!$singular) {
  660. return;
  661. }
  662. App::uses('I18n', 'I18n');
  663. $translated = I18n::translate($singular, $plural, $domain, $category, $count);
  664. if ($args === null) {
  665. return $translated;
  666. } elseif (!is_array($args)) {
  667. $args = array_slice(func_get_args(), 5);
  668. }
  669. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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 class constant of I18n, instead of the constant name. The values are:
  679. *
  680. * - LC_ALL I18n::LC_ALL
  681. * - LC_COLLATE I18n::LC_COLLATE
  682. * - LC_CTYPE I18n::LC_CTYPE
  683. * - LC_MONETARY I18n::LC_MONETARY
  684. * - LC_NUMERIC I18n::LC_NUMERIC
  685. * - LC_TIME I18n::LC_TIME
  686. * - LC_MESSAGES I18n::LC_MESSAGES
  687. *
  688. * @param string $msg String to translate
  689. * @param int $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. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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 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. }