basics.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 boolean 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 boolean $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  62. * @param boolean $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 integer $type Type of sorting to perform
  122. * @return mixed Sorted array
  123. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#sortByKey
  124. */
  125. function sortByKey(&$array, $sortBy, $order = 'asc', $type = SORT_NUMERIC) {
  126. if (!is_array($array)) {
  127. return null;
  128. }
  129. foreach ($array as $key => $val) {
  130. $sa[$key] = $val[$sortBy];
  131. }
  132. if ($order === 'asc') {
  133. asort($sa, $type);
  134. } else {
  135. arsort($sa, $type);
  136. }
  137. foreach ($sa as $key => $val) {
  138. $out[] = $array[$key];
  139. }
  140. return $out;
  141. }
  142. }
  143. if (!function_exists('h')) {
  144. /**
  145. * Convenience method for htmlspecialchars.
  146. *
  147. * @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
  148. * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
  149. * implement a `__toString` method. Otherwise the class name will be used.
  150. * @param boolean $double Encode existing html entities
  151. * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
  152. * @return string Wrapped text
  153. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#h
  154. */
  155. function h($text, $double = true, $charset = null) {
  156. if (is_array($text)) {
  157. $texts = array();
  158. foreach ($text as $k => $t) {
  159. $texts[$k] = h($t, $double, $charset);
  160. }
  161. return $texts;
  162. } elseif (is_object($text)) {
  163. if (method_exists($text, '__toString')) {
  164. $text = (string)$text;
  165. } else {
  166. $text = '(object)' . get_class($text);
  167. }
  168. } elseif (is_bool($text)) {
  169. return $text;
  170. }
  171. static $defaultCharset = false;
  172. if ($defaultCharset === false) {
  173. $defaultCharset = Configure::read('App.encoding');
  174. if ($defaultCharset === null) {
  175. $defaultCharset = 'UTF-8';
  176. }
  177. }
  178. if (is_string($double)) {
  179. $charset = $double;
  180. }
  181. return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
  182. }
  183. }
  184. if (!function_exists('pluginSplit')) {
  185. /**
  186. * Splits a dot syntax plugin name into its plugin and 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 boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  193. * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  194. * @return array Array with 2 indexes. 0 => plugin name, 1 => 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. * @see debug()
  216. * @param array $var Variable to print out
  217. * @return void
  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. printf($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/core-libraries/global-constants-and-functions.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 'DOCUMENT_ROOT':
  292. $name = env('SCRIPT_NAME');
  293. $filename = env('SCRIPT_FILENAME');
  294. $offset = 0;
  295. if (!strpos($name, '.php')) {
  296. $offset = 4;
  297. }
  298. return substr($filename, 0, -(strlen($name) + $offset));
  299. case 'PHP_SELF':
  300. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  301. case 'CGI_MODE':
  302. return (PHP_SAPI === 'cgi');
  303. case 'HTTP_BASE':
  304. $host = env('HTTP_HOST');
  305. $parts = explode('.', $host);
  306. $count = count($parts);
  307. if ($count === 1) {
  308. return '.' . $host;
  309. } elseif ($count === 2) {
  310. return '.' . $host;
  311. } elseif ($count === 3) {
  312. $gTLD = array(
  313. 'aero',
  314. 'asia',
  315. 'biz',
  316. 'cat',
  317. 'com',
  318. 'coop',
  319. 'edu',
  320. 'gov',
  321. 'info',
  322. 'int',
  323. 'jobs',
  324. 'mil',
  325. 'mobi',
  326. 'museum',
  327. 'name',
  328. 'net',
  329. 'org',
  330. 'pro',
  331. 'tel',
  332. 'travel',
  333. 'xxx'
  334. );
  335. if (in_array($parts[1], $gTLD)) {
  336. return '.' . $host;
  337. }
  338. }
  339. array_shift($parts);
  340. return '.' . implode('.', $parts);
  341. }
  342. return null;
  343. }
  344. }
  345. if (!function_exists('cache')) {
  346. /**
  347. * Reads/writes temporary data to cache files or session.
  348. *
  349. * @param string $path File path within /tmp to save the file.
  350. * @param mixed $data The data to save to the temporary file.
  351. * @param mixed $expires A valid strtotime string when the data expires.
  352. * @param string $target The target of the cached data; either 'cache' or 'public'.
  353. * @return mixed The contents of the temporary file.
  354. * @deprecated Will be removed in 3.0. Please use Cache::write() instead.
  355. */
  356. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  357. if (Configure::read('Cache.disable')) {
  358. return null;
  359. }
  360. $now = time();
  361. if (!is_numeric($expires)) {
  362. $expires = strtotime($expires, $now);
  363. }
  364. switch (strtolower($target)) {
  365. case 'cache':
  366. $filename = CACHE . $path;
  367. break;
  368. case 'public':
  369. $filename = WWW_ROOT . $path;
  370. break;
  371. case 'tmp':
  372. $filename = TMP . $path;
  373. break;
  374. }
  375. $timediff = $expires - $now;
  376. $filetime = false;
  377. if (file_exists($filename)) {
  378. //@codingStandardsIgnoreStart
  379. $filetime = @filemtime($filename);
  380. //@codingStandardsIgnoreEnd
  381. }
  382. if ($data === null) {
  383. if (file_exists($filename) && $filetime !== false) {
  384. if ($filetime + $timediff < $now) {
  385. //@codingStandardsIgnoreStart
  386. @unlink($filename);
  387. //@codingStandardsIgnoreEnd
  388. } else {
  389. //@codingStandardsIgnoreStart
  390. $data = @file_get_contents($filename);
  391. //@codingStandardsIgnoreEnd
  392. }
  393. }
  394. } elseif (is_writable(dirname($filename))) {
  395. //@codingStandardsIgnoreStart
  396. @file_put_contents($filename, $data, LOCK_EX);
  397. //@codingStandardsIgnoreEnd
  398. }
  399. return $data;
  400. }
  401. }
  402. if (!function_exists('clearCache')) {
  403. /**
  404. * Used to delete files in the cache directories, or clear contents of cache directories
  405. *
  406. * @param string|array $params As String name to be searched for deletion, if name is a directory all files in
  407. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  408. * all files in app/tmp/cache/views will be deleted
  409. * @param string $type Directory in tmp/cache defaults to view directory
  410. * @param string $ext The file extension you are deleting
  411. * @return true if files found and deleted false otherwise
  412. */
  413. function clearCache($params = null, $type = 'views', $ext = '.php') {
  414. if (is_string($params) || $params === null) {
  415. $params = preg_replace('/\/\//', '/', $params);
  416. $cache = CACHE . $type . DS . $params;
  417. if (is_file($cache . $ext)) {
  418. //@codingStandardsIgnoreStart
  419. @unlink($cache . $ext);
  420. //@codingStandardsIgnoreEnd
  421. return true;
  422. } elseif (is_dir($cache)) {
  423. $files = glob($cache . '*');
  424. if ($files === false) {
  425. return false;
  426. }
  427. foreach ($files as $file) {
  428. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  429. //@codingStandardsIgnoreStart
  430. @unlink($file);
  431. //@codingStandardsIgnoreEnd
  432. }
  433. }
  434. return true;
  435. }
  436. $cache = array(
  437. CACHE . $type . DS . '*' . $params . $ext,
  438. CACHE . $type . DS . '*' . $params . '_*' . $ext
  439. );
  440. $files = array();
  441. while ($search = array_shift($cache)) {
  442. $results = glob($search);
  443. if ($results !== false) {
  444. $files = array_merge($files, $results);
  445. }
  446. }
  447. if (empty($files)) {
  448. return false;
  449. }
  450. foreach ($files as $file) {
  451. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  452. //@codingStandardsIgnoreStart
  453. @unlink($file);
  454. //@codingStandardsIgnoreEnd
  455. }
  456. }
  457. return true;
  458. } elseif (is_array($params)) {
  459. foreach ($params as $file) {
  460. clearCache($file, $type, $ext);
  461. }
  462. return true;
  463. }
  464. return false;
  465. }
  466. }
  467. if (!function_exists('stripslashes_deep')) {
  468. /**
  469. * Recursively strips slashes from all values in an array
  470. *
  471. * @param array $values Array of values to strip slashes
  472. * @return mixed What is returned from calling stripslashes
  473. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#stripslashes_deep
  474. */
  475. function stripslashes_deep($values) {
  476. if (is_array($values)) {
  477. foreach ($values as $key => $value) {
  478. $values[$key] = stripslashes_deep($value);
  479. }
  480. } else {
  481. $values = stripslashes($values);
  482. }
  483. return $values;
  484. }
  485. }
  486. if (!function_exists('__')) {
  487. /**
  488. * Returns a translated string if one is found; Otherwise, the submitted message.
  489. *
  490. * @param string $singular Text to translate
  491. * @param mixed $args Array with arguments or multiple arguments in function
  492. * @return mixed translated string
  493. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
  494. */
  495. function __($singular, $args = null) {
  496. if (!$singular) {
  497. return;
  498. }
  499. App::uses('I18n', 'I18n');
  500. $translated = I18n::translate($singular);
  501. if ($args === null) {
  502. return $translated;
  503. } elseif (!is_array($args)) {
  504. $args = array_slice(func_get_args(), 1);
  505. }
  506. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  507. return vsprintf($translated, $args);
  508. }
  509. }
  510. if (!function_exists('__n')) {
  511. /**
  512. * Returns correct plural form of message identified by $singular and $plural for count $count.
  513. * Some languages have more than one form for plural messages dependent on the count.
  514. *
  515. * @param string $singular Singular text to translate
  516. * @param string $plural Plural text
  517. * @param integer $count Count
  518. * @param mixed $args Array with arguments or multiple arguments in function
  519. * @return mixed plural form of translated string
  520. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
  521. */
  522. function __n($singular, $plural, $count, $args = null) {
  523. if (!$singular) {
  524. return;
  525. }
  526. App::uses('I18n', 'I18n');
  527. $translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count);
  528. if ($args === null) {
  529. return $translated;
  530. } elseif (!is_array($args)) {
  531. $args = array_slice(func_get_args(), 3);
  532. }
  533. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  559. return vsprintf($translated, $args);
  560. }
  561. }
  562. if (!function_exists('__dn')) {
  563. /**
  564. * Allows you to override the current domain for a single plural message lookup.
  565. * Returns correct plural form of message identified by $singular and $plural for count $count
  566. * from domain $domain.
  567. *
  568. * @param string $domain Domain
  569. * @param string $singular Singular string to translate
  570. * @param string $plural Plural
  571. * @param integer $count Count
  572. * @param mixed $args Array with arguments or multiple arguments in function
  573. * @return plural form of translated string
  574. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
  575. */
  576. function __dn($domain, $singular, $plural, $count, $args = null) {
  577. if (!$singular) {
  578. return;
  579. }
  580. App::uses('I18n', 'I18n');
  581. $translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count);
  582. if ($args === null) {
  583. return $translated;
  584. } elseif (!is_array($args)) {
  585. $args = array_slice(func_get_args(), 4);
  586. }
  587. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  588. return vsprintf($translated, $args);
  589. }
  590. }
  591. if (!function_exists('__dc')) {
  592. /**
  593. * Allows you to override the current domain for a single message lookup.
  594. * It also allows you to specify a category.
  595. *
  596. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  597. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  598. *
  599. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  600. *
  601. * - LC_ALL I18n::LC_ALL
  602. * - LC_COLLATE I18n::LC_COLLATE
  603. * - LC_CTYPE I18n::LC_CTYPE
  604. * - LC_MONETARY I18n::LC_MONETARY
  605. * - LC_NUMERIC I18n::LC_NUMERIC
  606. * - LC_TIME I18n::LC_TIME
  607. * - LC_MESSAGES I18n::LC_MESSAGES
  608. *
  609. * @param string $domain Domain
  610. * @param string $msg Message to translate
  611. * @param integer $category Category
  612. * @param mixed $args Array with arguments or multiple arguments in function
  613. * @return translated string
  614. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dc
  615. */
  616. function __dc($domain, $msg, $category, $args = null) {
  617. if (!$msg) {
  618. return;
  619. }
  620. App::uses('I18n', 'I18n');
  621. $translated = I18n::translate($msg, null, $domain, $category);
  622. if ($args === null) {
  623. return $translated;
  624. } elseif (!is_array($args)) {
  625. $args = array_slice(func_get_args(), 3);
  626. }
  627. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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 class constant of I18n, instead of the constant name. The values are:
  642. *
  643. * - LC_ALL I18n::LC_ALL
  644. * - LC_COLLATE I18n::LC_COLLATE
  645. * - LC_CTYPE I18n::LC_CTYPE
  646. * - LC_MONETARY I18n::LC_MONETARY
  647. * - LC_NUMERIC I18n::LC_NUMERIC
  648. * - LC_TIME I18n::LC_TIME
  649. * - LC_MESSAGES I18n::LC_MESSAGES
  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. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  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 class constant of I18n, instead of the constant name. The values are:
  681. *
  682. * - LC_ALL I18n::LC_ALL
  683. * - LC_COLLATE I18n::LC_COLLATE
  684. * - LC_CTYPE I18n::LC_CTYPE
  685. * - LC_MONETARY I18n::LC_MONETARY
  686. * - LC_NUMERIC I18n::LC_NUMERIC
  687. * - LC_TIME I18n::LC_TIME
  688. * - LC_MESSAGES I18n::LC_MESSAGES
  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. $translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
  708. return vsprintf($translated, $args);
  709. }
  710. }
  711. if (!function_exists('LogError')) {
  712. /**
  713. * Shortcut to Log::write.
  714. *
  715. * @param string $message Message to write to log
  716. * @return void
  717. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#LogError
  718. */
  719. function LogError($message) {
  720. App::uses('CakeLog', 'Log');
  721. $bad = array("\n", "\r", "\t");
  722. $good = ' ';
  723. CakeLog::write('error', str_replace($bad, $good, $message));
  724. }
  725. }
  726. if (!function_exists('fileExistsInPath')) {
  727. /**
  728. * Searches include path for files.
  729. *
  730. * @param string $file File to look for
  731. * @return Full path to file if exists, otherwise false
  732. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#fileExistsInPath
  733. */
  734. function fileExistsInPath($file) {
  735. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  736. foreach ($paths as $path) {
  737. $fullPath = $path . DS . $file;
  738. if (file_exists($fullPath)) {
  739. return $fullPath;
  740. } elseif (file_exists($file)) {
  741. return $file;
  742. }
  743. }
  744. return false;
  745. }
  746. }
  747. if (!function_exists('convertSlash')) {
  748. /**
  749. * Convert forward slashes to underscores and removes first and last underscores in a string
  750. *
  751. * @param string String to convert
  752. * @return string with underscore remove from start and end of string
  753. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#convertSlash
  754. */
  755. function convertSlash($string) {
  756. $string = trim($string, '/');
  757. $string = preg_replace('/\/\//', '/', $string);
  758. $string = str_replace('/', '_', $string);
  759. return $string;
  760. }
  761. }