basics.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. * @since CakePHP(tm) v 0.2.9
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. use Cake\Cache\Cache;
  20. use Cake\Core\Configure;
  21. use Cake\I18n\I18n;
  22. use Cake\Log\Log;
  23. use Cake\Utility\Debugger;
  24. /**
  25. * Basic defines for timing functions.
  26. */
  27. define('SECOND', 1);
  28. define('MINUTE', 60);
  29. define('HOUR', 3600);
  30. define('DAY', 86400);
  31. define('WEEK', 604800);
  32. define('MONTH', 2592000);
  33. define('YEAR', 31536000);
  34. if (!function_exists('config')) {
  35. /**
  36. * Loads configuration files. Receives a set of configuration files
  37. * to load.
  38. * Example:
  39. *
  40. * `config('config1', 'config2');`
  41. *
  42. * @return boolean Success
  43. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#config
  44. */
  45. function config() {
  46. $args = func_get_args();
  47. $count = count($args);
  48. $included = 0;
  49. foreach ($args as $arg) {
  50. if (file_exists(APP . 'Config/' . $arg . '.php')) {
  51. include_once APP . 'Config/' . $arg . '.php';
  52. $included++;
  53. }
  54. }
  55. return $included === $count;
  56. }
  57. }
  58. if (!function_exists('debug')) {
  59. /**
  60. * Prints out debug information about given variable.
  61. *
  62. * Only runs if debug level is greater than zero.
  63. *
  64. * @param mixed $var Variable to show debug information for.
  65. * @param boolean $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  66. * @param boolean $showFrom If set to true, the method prints from where the function was called.
  67. * @return void
  68. * @link http://book.cakephp.org/2.0/en/development/debugging.html#basic-debugging
  69. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
  70. */
  71. function debug($var, $showHtml = null, $showFrom = true) {
  72. if (Configure::read('debug') > 0) {
  73. $file = '';
  74. $line = '';
  75. $lineInfo = '';
  76. if ($showFrom) {
  77. $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
  78. $file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
  79. $line = $trace[0]['line'];
  80. }
  81. $html = <<<HTML
  82. <div class="cake-debug-output">
  83. %s
  84. <pre class="cake-debug">
  85. %s
  86. </pre>
  87. </div>
  88. HTML;
  89. $text = <<<TEXT
  90. %s
  91. ########## DEBUG ##########
  92. %s
  93. ###########################
  94. TEXT;
  95. $template = $html;
  96. if (php_sapi_name() === 'cli' || $showHtml === false) {
  97. $template = $text;
  98. if ($showFrom) {
  99. $lineInfo = sprintf('%s (line %s)', $file, $line);
  100. }
  101. }
  102. if ($showHtml === null && $template !== $text) {
  103. $showHtml = true;
  104. }
  105. $var = Debugger::exportVar($var, 25);
  106. if ($showHtml) {
  107. $template = $html;
  108. $var = h($var);
  109. if ($showFrom) {
  110. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  111. }
  112. }
  113. printf($template, $lineInfo, $var);
  114. }
  115. }
  116. }
  117. if (!function_exists('sortByKey')) {
  118. /**
  119. * Sorts given $array by key $sortBy.
  120. *
  121. * @param array $array Array to sort
  122. * @param string $sortBy Sort by this key
  123. * @param string $order Sort order asc/desc (ascending or descending).
  124. * @param integer $type Type of sorting to perform
  125. * @return mixed Sorted array
  126. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#sortByKey
  127. */
  128. function sortByKey(&$array, $sortBy, $order = 'asc', $type = SORT_NUMERIC) {
  129. if (!is_array($array)) {
  130. return null;
  131. }
  132. foreach ($array as $key => $val) {
  133. $sa[$key] = $val[$sortBy];
  134. }
  135. if ($order === 'asc') {
  136. asort($sa, $type);
  137. } else {
  138. arsort($sa, $type);
  139. }
  140. foreach ($sa as $key => $val) {
  141. $out[] = $array[$key];
  142. }
  143. return $out;
  144. }
  145. }
  146. if (!function_exists('h')) {
  147. /**
  148. * Convenience method for htmlspecialchars.
  149. *
  150. * @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
  151. * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
  152. * implement a `__toString` method. Otherwise the class name will be used.
  153. * @param boolean $double Encode existing html entities
  154. * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
  155. * @return string Wrapped text
  156. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#h
  157. */
  158. function h($text, $double = true, $charset = null) {
  159. if (is_array($text)) {
  160. $texts = array();
  161. foreach ($text as $k => $t) {
  162. $texts[$k] = h($t, $double, $charset);
  163. }
  164. return $texts;
  165. } elseif (is_object($text)) {
  166. if (method_exists($text, '__toString')) {
  167. $text = (string)$text;
  168. } else {
  169. $text = '(object)' . get_class($text);
  170. }
  171. } elseif (is_bool($text)) {
  172. return $text;
  173. }
  174. static $defaultCharset = false;
  175. if ($defaultCharset === false) {
  176. $defaultCharset = Configure::read('App.encoding');
  177. if ($defaultCharset === null) {
  178. $defaultCharset = 'UTF-8';
  179. }
  180. }
  181. if (is_string($double)) {
  182. $charset = $double;
  183. }
  184. return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, ($charset) ? $charset : $defaultCharset, $double);
  185. }
  186. }
  187. if (!function_exists('pluginSplit')) {
  188. /**
  189. * Splits a dot syntax plugin name into its plugin and class name.
  190. * If $name does not have a dot, then index 0 will be null.
  191. *
  192. * Commonly used like `list($plugin, $name) = pluginSplit($name);`
  193. *
  194. * @param string $name The name you want to plugin split.
  195. * @param boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  196. * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  197. * @return array Array with 2 indexes. 0 => plugin name, 1 => class name
  198. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pluginSplit
  199. */
  200. function pluginSplit($name, $dotAppend = false, $plugin = null) {
  201. if (strpos($name, '.') !== false) {
  202. $parts = explode('.', $name, 2);
  203. if ($dotAppend) {
  204. $parts[0] .= '.';
  205. }
  206. return $parts;
  207. }
  208. return array($plugin, $name);
  209. }
  210. }
  211. if (!function_exists('namespaceSplit')) {
  212. /**
  213. * Split the namespace from the classname.
  214. *
  215. * Commonly used like `list($namespace, $classname) = namespaceSplit($class);`
  216. *
  217. * @param string $class The full class name, ie `Cake\Core\App`
  218. * @return array Array with 2 indexes. 0 => namespace, 1 => classname
  219. */
  220. function namespaceSplit($class) {
  221. $pos = strrpos($class, '\\');
  222. if ($pos === false) {
  223. return array('', $class);
  224. }
  225. return array(substr($class, 0, $pos), substr($class, $pos + 1));
  226. }
  227. }
  228. if (!function_exists('pr')) {
  229. /**
  230. * print_r() convenience function
  231. *
  232. * In terminals this will act the same as using print_r() directly, when not run on cli
  233. * print_r() will wrap <PRE> tags around the output of given array. Similar to debug().
  234. *
  235. * @see debug()
  236. * @param array $var Variable to print out
  237. * @return void
  238. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pr
  239. */
  240. function pr($var) {
  241. if (Configure::read('debug') > 0) {
  242. $template = php_sapi_name() !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
  243. printf($template, print_r($var, true));
  244. }
  245. }
  246. }
  247. if (!function_exists('am')) {
  248. /**
  249. * Merge a group of arrays, accepts an unlimited amount of parameters
  250. *
  251. * @return array All array parameters merged into one
  252. * @link http://book.cakephp.org/2.0/en/development/debugging.html#am
  253. */
  254. function am() {
  255. $r = array();
  256. $args = func_get_args();
  257. foreach ($args as $a) {
  258. if (!is_array($a)) {
  259. $a = array($a);
  260. }
  261. $r = array_merge($r, $a);
  262. }
  263. return $r;
  264. }
  265. }
  266. if (!function_exists('env')) {
  267. /**
  268. * Gets an environment variable from available sources, and provides emulation
  269. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  270. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  271. * environment information.
  272. *
  273. * @param string $key Environment variable name.
  274. * @return string Environment variable setting.
  275. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#env
  276. */
  277. function env($key) {
  278. if ($key === 'HTTPS') {
  279. if (isset($_SERVER['HTTPS'])) {
  280. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  281. }
  282. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  283. }
  284. if ($key === 'SCRIPT_NAME') {
  285. if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  286. $key = 'SCRIPT_URL';
  287. }
  288. }
  289. $val = null;
  290. if (isset($_SERVER[$key])) {
  291. $val = $_SERVER[$key];
  292. } elseif (isset($_ENV[$key])) {
  293. $val = $_ENV[$key];
  294. } elseif (getenv($key) !== false) {
  295. $val = getenv($key);
  296. }
  297. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  298. $addr = env('HTTP_PC_REMOTE_ADDR');
  299. if ($addr !== null) {
  300. $val = $addr;
  301. }
  302. }
  303. if ($val !== null) {
  304. return $val;
  305. }
  306. switch ($key) {
  307. case 'DOCUMENT_ROOT':
  308. $name = env('SCRIPT_NAME');
  309. $filename = env('SCRIPT_FILENAME');
  310. $offset = 0;
  311. if (!strpos($name, '.php')) {
  312. $offset = 4;
  313. }
  314. return substr($filename, 0, -(strlen($name) + $offset));
  315. case 'PHP_SELF':
  316. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  317. case 'CGI_MODE':
  318. return (PHP_SAPI === 'cgi');
  319. case 'HTTP_BASE':
  320. $host = env('HTTP_HOST');
  321. $parts = explode('.', $host);
  322. $count = count($parts);
  323. if ($count === 1) {
  324. return '.' . $host;
  325. } elseif ($count === 2) {
  326. return '.' . $host;
  327. } elseif ($count === 3) {
  328. $gTLD = array(
  329. 'aero',
  330. 'asia',
  331. 'biz',
  332. 'cat',
  333. 'com',
  334. 'coop',
  335. 'edu',
  336. 'gov',
  337. 'info',
  338. 'int',
  339. 'jobs',
  340. 'mil',
  341. 'mobi',
  342. 'museum',
  343. 'name',
  344. 'net',
  345. 'org',
  346. 'pro',
  347. 'tel',
  348. 'travel',
  349. 'xxx'
  350. );
  351. if (in_array($parts[1], $gTLD)) {
  352. return '.' . $host;
  353. }
  354. }
  355. array_shift($parts);
  356. return '.' . implode('.', $parts);
  357. }
  358. return null;
  359. }
  360. }
  361. if (!function_exists('cache')) {
  362. /**
  363. * Reads/writes temporary data to cache files or session.
  364. *
  365. * @param string $path File path within /tmp to save the file.
  366. * @param mixed $data The data to save to the temporary file.
  367. * @param mixed $expires A valid strtotime string when the data expires.
  368. * @param string $target The target of the cached data; either 'cache' or 'public'.
  369. * @return mixed The contents of the temporary file.
  370. * @deprecated Will be removed in 3.0. Please use Cache::write() instead.
  371. */
  372. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  373. if (!Cache::enabled()) {
  374. return null;
  375. }
  376. $now = time();
  377. if (!is_numeric($expires)) {
  378. $expires = strtotime($expires, $now);
  379. }
  380. switch (strtolower($target)) {
  381. case 'cache':
  382. $filename = CACHE . $path;
  383. break;
  384. case 'public':
  385. $filename = WWW_ROOT . $path;
  386. break;
  387. case 'tmp':
  388. $filename = TMP . $path;
  389. break;
  390. }
  391. $timediff = $expires - $now;
  392. $filetime = false;
  393. if (file_exists($filename)) {
  394. //@codingStandardsIgnoreStart
  395. $filetime = @filemtime($filename);
  396. //@codingStandardsIgnoreEnd
  397. }
  398. if ($data === null) {
  399. if (file_exists($filename) && $filetime !== false) {
  400. if ($filetime + $timediff < $now) {
  401. //@codingStandardsIgnoreStart
  402. @unlink($filename);
  403. //@codingStandardsIgnoreEnd
  404. } else {
  405. //@codingStandardsIgnoreStart
  406. $data = @file_get_contents($filename);
  407. //@codingStandardsIgnoreEnd
  408. }
  409. }
  410. } elseif (is_writable(dirname($filename))) {
  411. //@codingStandardsIgnoreStart
  412. @file_put_contents($filename, $data, LOCK_EX);
  413. //@codingStandardsIgnoreEnd
  414. }
  415. return $data;
  416. }
  417. }
  418. if (!function_exists('clearCache')) {
  419. /**
  420. * Used to delete files in the cache directories, or clear contents of cache directories
  421. *
  422. * @param string|array $params As String name to be searched for deletion, if name is a directory all files in
  423. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  424. * all files in app/tmp/cache/views will be deleted
  425. * @param string $type Directory in tmp/cache defaults to view directory
  426. * @param string $ext The file extension you are deleting
  427. * @return true if files found and deleted false otherwise
  428. */
  429. function clearCache($params = null, $type = 'views', $ext = '.php') {
  430. if (is_string($params) || $params === null) {
  431. $params = preg_replace('/\/\//', '/', $params);
  432. $cache = CACHE . $type . DS . $params;
  433. if (is_file($cache . $ext)) {
  434. //@codingStandardsIgnoreStart
  435. @unlink($cache . $ext);
  436. //@codingStandardsIgnoreEnd
  437. return true;
  438. } elseif (is_dir($cache)) {
  439. $files = glob($cache . '*');
  440. if ($files === false) {
  441. return false;
  442. }
  443. foreach ($files as $file) {
  444. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  445. //@codingStandardsIgnoreStart
  446. @unlink($file);
  447. //@codingStandardsIgnoreEnd
  448. }
  449. }
  450. return true;
  451. }
  452. $cache = array(
  453. CACHE . $type . DS . '*' . $params . $ext,
  454. CACHE . $type . DS . '*' . $params . '_*' . $ext
  455. );
  456. $files = array();
  457. while ($search = array_shift($cache)) {
  458. $results = glob($search);
  459. if ($results !== false) {
  460. $files = array_merge($files, $results);
  461. }
  462. }
  463. if (empty($files)) {
  464. return false;
  465. }
  466. foreach ($files as $file) {
  467. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  468. //@codingStandardsIgnoreStart
  469. @unlink($file);
  470. //@codingStandardsIgnoreEnd
  471. }
  472. }
  473. return true;
  474. } elseif (is_array($params)) {
  475. foreach ($params as $file) {
  476. clearCache($file, $type, $ext);
  477. }
  478. return true;
  479. }
  480. return false;
  481. }
  482. }
  483. if (!function_exists('__')) {
  484. /**
  485. * Returns a translated string if one is found; Otherwise, the submitted message.
  486. *
  487. * @param string $singular Text to translate
  488. * @param mixed $args Array with arguments or multiple arguments in function
  489. * @return mixed translated string
  490. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
  491. */
  492. function __($singular, $args = null) {
  493. if (!$singular) {
  494. return;
  495. }
  496. $translated = I18n::translate($singular);
  497. if ($args === null) {
  498. return $translated;
  499. } elseif (!is_array($args)) {
  500. $args = array_slice(func_get_args(), 1);
  501. }
  502. return vsprintf($translated, $args);
  503. }
  504. }
  505. if (!function_exists('__n')) {
  506. /**
  507. * Returns correct plural form of message identified by $singular and $plural for count $count.
  508. * Some languages have more than one form for plural messages dependent on the count.
  509. *
  510. * @param string $singular Singular text to translate
  511. * @param string $plural Plural text
  512. * @param integer $count Count
  513. * @param mixed $args Array with arguments or multiple arguments in function
  514. * @return mixed plural form of translated string
  515. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
  516. */
  517. function __n($singular, $plural, $count, $args = null) {
  518. if (!$singular) {
  519. return;
  520. }
  521. $translated = I18n::translate($singular, $plural, null, 6, $count);
  522. if ($args === null) {
  523. return $translated;
  524. } elseif (!is_array($args)) {
  525. $args = array_slice(func_get_args(), 3);
  526. }
  527. return vsprintf($translated, $args);
  528. }
  529. }
  530. if (!function_exists('__d')) {
  531. /**
  532. * Allows you to override the current domain for a single message lookup.
  533. *
  534. * @param string $domain Domain
  535. * @param string $msg String to translate
  536. * @param mixed $args Array with arguments or multiple arguments in function
  537. * @return translated string
  538. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__d
  539. */
  540. function __d($domain, $msg, $args = null) {
  541. if (!$msg) {
  542. return;
  543. }
  544. $translated = I18n::translate($msg, null, $domain);
  545. if ($args === null) {
  546. return $translated;
  547. } elseif (!is_array($args)) {
  548. $args = array_slice(func_get_args(), 2);
  549. }
  550. return vsprintf($translated, $args);
  551. }
  552. }
  553. if (!function_exists('__dn')) {
  554. /**
  555. * Allows you to override the current domain for a single plural message lookup.
  556. * Returns correct plural form of message identified by $singular and $plural for count $count
  557. * from domain $domain.
  558. *
  559. * @param string $domain Domain
  560. * @param string $singular Singular string to translate
  561. * @param string $plural Plural
  562. * @param integer $count Count
  563. * @param mixed $args Array with arguments or multiple arguments in function
  564. * @return plural form of translated string
  565. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
  566. */
  567. function __dn($domain, $singular, $plural, $count, $args = null) {
  568. if (!$singular) {
  569. return;
  570. }
  571. $translated = I18n::translate($singular, $plural, $domain, 6, $count);
  572. if ($args === null) {
  573. return $translated;
  574. } elseif (!is_array($args)) {
  575. $args = array_slice(func_get_args(), 4);
  576. }
  577. return vsprintf($translated, $args);
  578. }
  579. }
  580. if (!function_exists('__dc')) {
  581. /**
  582. * Allows you to override the current domain for a single message lookup.
  583. * It also allows you to specify a category.
  584. *
  585. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  586. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  587. *
  588. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  589. *
  590. * - LC_ALL 0
  591. * - LC_COLLATE 1
  592. * - LC_CTYPE 2
  593. * - LC_MONETARY 3
  594. * - LC_NUMERIC 4
  595. * - LC_TIME 5
  596. * - LC_MESSAGES 6
  597. *
  598. * @param string $domain Domain
  599. * @param string $msg Message to translate
  600. * @param integer $category Category
  601. * @param mixed $args Array with arguments or multiple arguments in function
  602. * @return translated string
  603. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dc
  604. */
  605. function __dc($domain, $msg, $category, $args = null) {
  606. if (!$msg) {
  607. return;
  608. }
  609. $translated = I18n::translate($msg, null, $domain, $category);
  610. if ($args === null) {
  611. return $translated;
  612. } elseif (!is_array($args)) {
  613. $args = array_slice(func_get_args(), 3);
  614. }
  615. return vsprintf($translated, $args);
  616. }
  617. }
  618. if (!function_exists('__dcn')) {
  619. /**
  620. * Allows you to override the current domain for a single plural message lookup.
  621. * It also allows you to specify a category.
  622. * Returns correct plural form of message identified by $singular and $plural for count $count
  623. * from domain $domain.
  624. *
  625. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  626. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  627. *
  628. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  629. *
  630. * - LC_ALL 0
  631. * - LC_COLLATE 1
  632. * - LC_CTYPE 2
  633. * - LC_MONETARY 3
  634. * - LC_NUMERIC 4
  635. * - LC_TIME 5
  636. * - LC_MESSAGES 6
  637. *
  638. * @param string $domain Domain
  639. * @param string $singular Singular string to translate
  640. * @param string $plural Plural
  641. * @param integer $count Count
  642. * @param integer $category Category
  643. * @param mixed $args Array with arguments or multiple arguments in function
  644. * @return plural form of translated string
  645. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dcn
  646. */
  647. function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
  648. if (!$singular) {
  649. return;
  650. }
  651. $translated = I18n::translate($singular, $plural, $domain, $category, $count);
  652. if ($args === null) {
  653. return $translated;
  654. } elseif (!is_array($args)) {
  655. $args = array_slice(func_get_args(), 5);
  656. }
  657. return vsprintf($translated, $args);
  658. }
  659. }
  660. if (!function_exists('__c')) {
  661. /**
  662. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  663. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  664. *
  665. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  666. *
  667. * - LC_ALL 0
  668. * - LC_COLLATE 1
  669. * - LC_CTYPE 2
  670. * - LC_MONETARY 3
  671. * - LC_NUMERIC 4
  672. * - LC_TIME 5
  673. * - LC_MESSAGES 6
  674. *
  675. * @param string $msg String to translate
  676. * @param integer $category Category
  677. * @param mixed $args Array with arguments or multiple arguments in function
  678. * @return translated string
  679. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__c
  680. */
  681. function __c($msg, $category, $args = null) {
  682. if (!$msg) {
  683. return;
  684. }
  685. $translated = I18n::translate($msg, null, null, $category);
  686. if ($args === null) {
  687. return $translated;
  688. } elseif (!is_array($args)) {
  689. $args = array_slice(func_get_args(), 2);
  690. }
  691. return vsprintf($translated, $args);
  692. }
  693. }
  694. if (!function_exists('fileExistsInPath')) {
  695. /**
  696. * Searches include path for files.
  697. *
  698. * @param string $file File to look for
  699. * @return Full path to file if exists, otherwise false
  700. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#fileExistsInPath
  701. */
  702. function fileExistsInPath($file) {
  703. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  704. foreach ($paths as $path) {
  705. $fullPath = $path . DS . $file;
  706. if (file_exists($fullPath)) {
  707. return $fullPath;
  708. } elseif (file_exists($file)) {
  709. return $file;
  710. }
  711. }
  712. return false;
  713. }
  714. }
  715. if (!function_exists('convertSlash')) {
  716. /**
  717. * Convert forward slashes to underscores and removes first and last underscores in a string
  718. *
  719. * @param string String to convert
  720. * @return string with underscore remove from start and end of string
  721. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#convertSlash
  722. */
  723. function convertSlash($string) {
  724. $string = trim($string, '/');
  725. $string = preg_replace('/\/\//', '/', $string);
  726. $string = str_replace('/', '_', $string);
  727. return $string;
  728. }
  729. }