PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/basics.php

https://github.com/cgajardo/repositorium
PHP | 1026 lines | 550 code | 74 blank | 402 comment | 135 complexity | 32ba318eaaf15d33dff7817b3a688fd7 MD5 | raw file
  1. <?php
  2. /**
  3. * Basic Cake functionality.
  4. *
  5. * Core functions for including other source files, loading models and so forth.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.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. /**
  33. * Patch for PHP < 5.0
  34. */
  35. if (!function_exists('clone')) {
  36. if (version_compare(PHP_VERSION, '5.0') < 0) {
  37. eval ('
  38. function clone($object)
  39. {
  40. return $object;
  41. }');
  42. }
  43. }
  44. /**
  45. * Loads configuration files. Receives a set of configuration files
  46. * to load.
  47. * Example:
  48. *
  49. * `config('config1', 'config2');`
  50. *
  51. * @return boolean Success
  52. * @link http://book.cakephp.org/view/1125/config
  53. */
  54. function config() {
  55. $args = func_get_args();
  56. foreach ($args as $arg) {
  57. if ($arg === 'database' && file_exists(CONFIGS . 'database.php')) {
  58. include_once(CONFIGS . $arg . '.php');
  59. } elseif (file_exists(CONFIGS . $arg . '.php')) {
  60. include_once(CONFIGS . $arg . '.php');
  61. if (count($args) == 1) {
  62. return true;
  63. }
  64. } else {
  65. if (count($args) == 1) {
  66. return false;
  67. }
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * Loads component/components from LIBS. Takes optional number of parameters.
  74. *
  75. * Example:
  76. *
  77. * `uses('flay', 'time');`
  78. *
  79. * @param string $name Filename without the .php part
  80. * @deprecated Will be removed in 2.0
  81. * @link http://book.cakephp.org/view/1140/uses
  82. */
  83. function uses() {
  84. $args = func_get_args();
  85. foreach ($args as $file) {
  86. require_once(LIBS . strtolower($file) . '.php');
  87. }
  88. }
  89. /**
  90. * Prints out debug information about given variable.
  91. *
  92. * Only runs if debug level is greater than zero.
  93. *
  94. * @param boolean $var Variable to show debug information for.
  95. * @param boolean $showHtml If set to true, the method prints the debug data in a screen-friendly way.
  96. * @param boolean $showFrom If set to true, the method prints from where the function was called.
  97. * @link http://book.cakephp.org/view/1190/Basic-Debugging
  98. * @link http://book.cakephp.org/view/1128/debug
  99. */
  100. function debug($var = false, $showHtml = false, $showFrom = true) {
  101. if (Configure::read() > 0) {
  102. if ($showFrom) {
  103. $calledFrom = debug_backtrace();
  104. echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
  105. echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
  106. }
  107. echo "\n<pre class=\"cake-debug\">\n";
  108. $var = print_r($var, true);
  109. if ($showHtml) {
  110. $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
  111. }
  112. echo $var . "\n</pre>\n";
  113. }
  114. }
  115. if (!function_exists('getMicrotime')) {
  116. /**
  117. * Returns microtime for execution time checking
  118. *
  119. * @return float Microtime
  120. */
  121. function getMicrotime() {
  122. list($usec, $sec) = explode(' ', microtime());
  123. return ((float)$usec + (float)$sec);
  124. }
  125. }
  126. if (!function_exists('sortByKey')) {
  127. /**
  128. * Sorts given $array by key $sortby.
  129. *
  130. * @param array $array Array to sort
  131. * @param string $sortby Sort by this key
  132. * @param string $order Sort order asc/desc (ascending or descending).
  133. * @param integer $type Type of sorting to perform
  134. * @return mixed Sorted array
  135. */
  136. function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
  137. if (!is_array($array)) {
  138. return null;
  139. }
  140. foreach ($array as $key => $val) {
  141. $sa[$key] = $val[$sortby];
  142. }
  143. if ($order == 'asc') {
  144. asort($sa, $type);
  145. } else {
  146. arsort($sa, $type);
  147. }
  148. foreach ($sa as $key => $val) {
  149. $out[] = $array[$key];
  150. }
  151. return $out;
  152. }
  153. }
  154. if (!function_exists('array_combine')) {
  155. /**
  156. * Combines given identical arrays by using the first array's values as keys,
  157. * and the second one's values as values. (Implemented for backwards compatibility with PHP4)
  158. *
  159. * @param array $a1 Array to use for keys
  160. * @param array $a2 Array to use for values
  161. * @return mixed Outputs either combined array or false.
  162. * @deprecated Will be removed in 2.0
  163. */
  164. function array_combine($a1, $a2) {
  165. $a1 = array_values($a1);
  166. $a2 = array_values($a2);
  167. $c1 = count($a1);
  168. $c2 = count($a2);
  169. if ($c1 != $c2) {
  170. return false;
  171. }
  172. if ($c1 <= 0) {
  173. return false;
  174. }
  175. $output = array();
  176. for ($i = 0; $i < $c1; $i++) {
  177. $output[$a1[$i]] = $a2[$i];
  178. }
  179. return $output;
  180. }
  181. }
  182. /**
  183. * Convenience method for htmlspecialchars.
  184. *
  185. * @param string $text Text to wrap through htmlspecialchars
  186. * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
  187. * @return string Wrapped text
  188. * @link http://book.cakephp.org/view/1132/h
  189. */
  190. function h($text, $charset = null) {
  191. if (is_array($text)) {
  192. return array_map('h', $text);
  193. }
  194. static $defaultCharset = false;
  195. if ($defaultCharset === false) {
  196. $defaultCharset = Configure::read('App.encoding');
  197. if ($defaultCharset === null) {
  198. $defaultCharset = 'UTF-8';
  199. }
  200. }
  201. if ($charset) {
  202. return htmlspecialchars($text, ENT_QUOTES, $charset);
  203. } else {
  204. return htmlspecialchars($text, ENT_QUOTES, $defaultCharset);
  205. }
  206. }
  207. /**
  208. * Splits a dot syntax plugin name into its plugin and classname.
  209. * If $name does not have a dot, then index 0 will be null.
  210. *
  211. * Commonly used like `list($plugin, $name) = pluginSplit($name);`
  212. *
  213. * @param string $name The name you want to plugin split.
  214. * @param boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  215. * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  216. * @return array Array with 2 indexes. 0 => plugin name, 1 => classname
  217. */
  218. function pluginSplit($name, $dotAppend = false, $plugin = null) {
  219. if (strpos($name, '.') !== false) {
  220. $parts = explode('.', $name, 2);
  221. if ($dotAppend) {
  222. $parts[0] .= '.';
  223. }
  224. return $parts;
  225. }
  226. return array($plugin, $name);
  227. }
  228. /**
  229. * Returns an array of all the given parameters.
  230. *
  231. * Example:
  232. *
  233. * `a('a', 'b')`
  234. *
  235. * Would return:
  236. *
  237. * `array('a', 'b')`
  238. *
  239. * @return array Array of given parameters
  240. * @link http://book.cakephp.org/view/1122/a
  241. * @deprecated Will be removed in 2.0
  242. */
  243. function a() {
  244. $args = func_get_args();
  245. return $args;
  246. }
  247. /**
  248. * Constructs associative array from pairs of arguments.
  249. *
  250. * Example:
  251. *
  252. * `aa('a','b')`
  253. *
  254. * Would return:
  255. *
  256. * `array('a'=>'b')`
  257. *
  258. * @return array Associative array
  259. * @link http://book.cakephp.org/view/1123/aa
  260. * @deprecated Will be removed in 2.0
  261. */
  262. function aa() {
  263. $args = func_get_args();
  264. $argc = count($args);
  265. for ($i = 0; $i < $argc; $i++) {
  266. if ($i + 1 < $argc) {
  267. $a[$args[$i]] = $args[$i + 1];
  268. } else {
  269. $a[$args[$i]] = null;
  270. }
  271. $i++;
  272. }
  273. return $a;
  274. }
  275. /**
  276. * Convenience method for echo().
  277. *
  278. * @param string $text String to echo
  279. * @link http://book.cakephp.org/view/1129/e
  280. * @deprecated Will be removed in 2.0
  281. */
  282. function e($text) {
  283. echo $text;
  284. }
  285. /**
  286. * Convenience method for strtolower().
  287. *
  288. * @param string $str String to lowercase
  289. * @return string Lowercased string
  290. * @link http://book.cakephp.org/view/1134/low
  291. * @deprecated Will be removed in 2.0
  292. */
  293. function low($str) {
  294. return strtolower($str);
  295. }
  296. /**
  297. * Convenience method for strtoupper().
  298. *
  299. * @param string $str String to uppercase
  300. * @return string Uppercased string
  301. * @link http://book.cakephp.org/view/1139/up
  302. * @deprecated Will be removed in 2.0
  303. */
  304. function up($str) {
  305. return strtoupper($str);
  306. }
  307. /**
  308. * Convenience method for str_replace().
  309. *
  310. * @param string $search String to be replaced
  311. * @param string $replace String to insert
  312. * @param string $subject String to search
  313. * @return string Replaced string
  314. * @link http://book.cakephp.org/view/1137/r
  315. * @deprecated Will be removed in 2.0
  316. */
  317. function r($search, $replace, $subject) {
  318. return str_replace($search, $replace, $subject);
  319. }
  320. /**
  321. * Print_r convenience function, which prints out <PRE> tags around
  322. * the output of given array. Similar to debug().
  323. *
  324. * @see debug()
  325. * @param array $var Variable to print out
  326. * @link http://book.cakephp.org/view/1136/pr
  327. */
  328. function pr($var) {
  329. if (Configure::read() > 0) {
  330. echo '<pre>';
  331. print_r($var);
  332. echo '</pre>';
  333. }
  334. }
  335. /**
  336. * Display parameters.
  337. *
  338. * @param mixed $p Parameter as string or array
  339. * @return string
  340. * @deprecated Will be removed in 2.0
  341. */
  342. function params($p) {
  343. if (!is_array($p) || count($p) == 0) {
  344. return null;
  345. }
  346. if (is_array($p[0]) && count($p) == 1) {
  347. return $p[0];
  348. }
  349. return $p;
  350. }
  351. /**
  352. * Merge a group of arrays
  353. *
  354. * @param array First array
  355. * @param array Second array
  356. * @param array Third array
  357. * @param array Etc...
  358. * @return array All array parameters merged into one
  359. * @link http://book.cakephp.org/view/1124/am
  360. */
  361. function am() {
  362. $r = array();
  363. $args = func_get_args();
  364. foreach ($args as $a) {
  365. if (!is_array($a)) {
  366. $a = array($a);
  367. }
  368. $r = array_merge($r, $a);
  369. }
  370. return $r;
  371. }
  372. /**
  373. * Gets an environment variable from available sources, and provides emulation
  374. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  375. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  376. * environment information.
  377. *
  378. * @param string $key Environment variable name.
  379. * @return string Environment variable setting.
  380. * @link http://book.cakephp.org/view/1130/env
  381. */
  382. function env($key) {
  383. if ($key == 'HTTPS') {
  384. if (isset($_SERVER['HTTPS'])) {
  385. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  386. }
  387. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  388. }
  389. if ($key == 'SCRIPT_NAME') {
  390. if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  391. $key = 'SCRIPT_URL';
  392. }
  393. }
  394. $val = null;
  395. if (isset($_SERVER[$key])) {
  396. $val = $_SERVER[$key];
  397. } elseif (isset($_ENV[$key])) {
  398. $val = $_ENV[$key];
  399. } elseif (getenv($key) !== false) {
  400. $val = getenv($key);
  401. }
  402. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  403. $addr = env('HTTP_PC_REMOTE_ADDR');
  404. if ($addr !== null) {
  405. $val = $addr;
  406. }
  407. }
  408. if ($val !== null) {
  409. return $val;
  410. }
  411. switch ($key) {
  412. case 'SCRIPT_FILENAME':
  413. if (defined('SERVER_IIS') && SERVER_IIS === true) {
  414. return str_replace('\\\\', '\\', env('PATH_TRANSLATED'));
  415. }
  416. break;
  417. case 'DOCUMENT_ROOT':
  418. $name = env('SCRIPT_NAME');
  419. $filename = env('SCRIPT_FILENAME');
  420. $offset = 0;
  421. if (!strpos($name, '.php')) {
  422. $offset = 4;
  423. }
  424. return substr($filename, 0, strlen($filename) - (strlen($name) + $offset));
  425. break;
  426. case 'PHP_SELF':
  427. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  428. break;
  429. case 'CGI_MODE':
  430. return (PHP_SAPI === 'cgi');
  431. break;
  432. case 'HTTP_BASE':
  433. $host = env('HTTP_HOST');
  434. $parts = explode('.', $host);
  435. $count = count($parts);
  436. if ($count === 1) {
  437. return '.' . $host;
  438. } elseif ($count === 2) {
  439. return '.' . $host;
  440. } elseif ($count === 3) {
  441. $gTLD = array('aero', 'asia', 'biz', 'cat', 'com', 'coop', 'edu', 'gov', 'info', 'int', 'jobs', 'mil', 'mobi', 'museum', 'name', 'net', 'org', 'pro', 'tel', 'travel', 'xxx');
  442. if (in_array($parts[1], $gTLD)) {
  443. return '.' . $host;
  444. }
  445. }
  446. array_shift($parts);
  447. return '.' . implode('.', $parts);
  448. break;
  449. }
  450. return null;
  451. }
  452. if (!function_exists('file_put_contents')) {
  453. /**
  454. * Writes data into file.
  455. *
  456. * If file exists, it will be overwritten. If data is an array, it will be implode()ed with an empty string.
  457. *
  458. * @param string $fileName File name.
  459. * @param mixed $data String or array.
  460. * @return boolean Success
  461. * @deprecated Will be removed in 2.0
  462. */
  463. function file_put_contents($fileName, $data) {
  464. if (is_array($data)) {
  465. $data = implode('', $data);
  466. }
  467. $res = @fopen($fileName, 'w+b');
  468. if ($res) {
  469. $write = @fwrite($res, $data);
  470. if ($write === false) {
  471. return false;
  472. } else {
  473. @fclose($res);
  474. return $write;
  475. }
  476. }
  477. return false;
  478. }
  479. }
  480. /**
  481. * Reads/writes temporary data to cache files or session.
  482. *
  483. * @param string $path File path within /tmp to save the file.
  484. * @param mixed $data The data to save to the temporary file.
  485. * @param mixed $expires A valid strtotime string when the data expires.
  486. * @param string $target The target of the cached data; either 'cache' or 'public'.
  487. * @return mixed The contents of the temporary file.
  488. * @deprecated Please use Cache::write() instead
  489. */
  490. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  491. if (Configure::read('Cache.disable')) {
  492. return null;
  493. }
  494. $now = time();
  495. if (!is_numeric($expires)) {
  496. $expires = strtotime($expires, $now);
  497. }
  498. switch (strtolower($target)) {
  499. case 'cache':
  500. $filename = CACHE . $path;
  501. break;
  502. case 'public':
  503. $filename = WWW_ROOT . $path;
  504. break;
  505. case 'tmp':
  506. $filename = TMP . $path;
  507. break;
  508. }
  509. $timediff = $expires - $now;
  510. $filetime = false;
  511. if (file_exists($filename)) {
  512. $filetime = @filemtime($filename);
  513. }
  514. if ($data === null) {
  515. if (file_exists($filename) && $filetime !== false) {
  516. if ($filetime + $timediff < $now) {
  517. @unlink($filename);
  518. } else {
  519. $data = @file_get_contents($filename);
  520. }
  521. }
  522. } elseif (is_writable(dirname($filename))) {
  523. @file_put_contents($filename, $data);
  524. }
  525. return $data;
  526. }
  527. /**
  528. * Used to delete files in the cache directories, or clear contents of cache directories
  529. *
  530. * @param mixed $params As String name to be searched for deletion, if name is a directory all files in
  531. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  532. * all files in app/tmp/cache/views will be deleted
  533. * @param string $type Directory in tmp/cache defaults to view directory
  534. * @param string $ext The file extension you are deleting
  535. * @return true if files found and deleted false otherwise
  536. */
  537. function clearCache($params = null, $type = 'views', $ext = '.php') {
  538. if (is_string($params) || $params === null) {
  539. $params = preg_replace('/\/\//', '/', $params);
  540. $cache = CACHE . $type . DS . $params;
  541. if (is_file($cache . $ext)) {
  542. @unlink($cache . $ext);
  543. return true;
  544. } elseif (is_dir($cache)) {
  545. $files = glob($cache . '*');
  546. if ($files === false) {
  547. return false;
  548. }
  549. foreach ($files as $file) {
  550. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  551. @unlink($file);
  552. }
  553. }
  554. return true;
  555. } else {
  556. $cache = array(
  557. CACHE . $type . DS . '*' . $params . $ext,
  558. CACHE . $type . DS . '*' . $params . '_*' . $ext
  559. );
  560. $files = array();
  561. while ($search = array_shift($cache)) {
  562. $results = glob($search);
  563. if ($results !== false) {
  564. $files = array_merge($files, $results);
  565. }
  566. }
  567. if (empty($files)) {
  568. return false;
  569. }
  570. foreach ($files as $file) {
  571. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  572. @unlink($file);
  573. }
  574. }
  575. return true;
  576. }
  577. } elseif (is_array($params)) {
  578. foreach ($params as $file) {
  579. clearCache($file, $type, $ext);
  580. }
  581. return true;
  582. }
  583. return false;
  584. }
  585. /**
  586. * Recursively strips slashes from all values in an array
  587. *
  588. * @param array $values Array of values to strip slashes
  589. * @return mixed What is returned from calling stripslashes
  590. * @link http://book.cakephp.org/view/1138/stripslashes_deep
  591. */
  592. function stripslashes_deep($values) {
  593. if (is_array($values)) {
  594. foreach ($values as $key => $value) {
  595. $values[$key] = stripslashes_deep($value);
  596. }
  597. } else {
  598. $values = stripslashes($values);
  599. }
  600. return $values;
  601. }
  602. /**
  603. * Returns a translated string if one is found; Otherwise, the submitted message.
  604. *
  605. * @param string $singular Text to translate
  606. * @param boolean $return Set to true to return translated string, or false to echo
  607. * @return mixed translated string if $return is false string will be echoed
  608. * @link http://book.cakephp.org/view/1121/__
  609. */
  610. function __($singular, $return = false) {
  611. if (!$singular) {
  612. return;
  613. }
  614. if (!class_exists('I18n')) {
  615. App::import('Core', 'i18n');
  616. }
  617. if ($return === false) {
  618. echo I18n::translate($singular);
  619. } else {
  620. return I18n::translate($singular);
  621. }
  622. }
  623. /**
  624. * Returns correct plural form of message identified by $singular and $plural for count $count.
  625. * Some languages have more than one form for plural messages dependent on the count.
  626. *
  627. * @param string $singular Singular text to translate
  628. * @param string $plural Plural text
  629. * @param integer $count Count
  630. * @param boolean $return true to return, false to echo
  631. * @return mixed plural form of translated string if $return is false string will be echoed
  632. */
  633. function __n($singular, $plural, $count, $return = false) {
  634. if (!$singular) {
  635. return;
  636. }
  637. if (!class_exists('I18n')) {
  638. App::import('Core', 'i18n');
  639. }
  640. if ($return === false) {
  641. echo I18n::translate($singular, $plural, null, 6, $count);
  642. } else {
  643. return I18n::translate($singular, $plural, null, 6, $count);
  644. }
  645. }
  646. /**
  647. * Allows you to override the current domain for a single message lookup.
  648. *
  649. * @param string $domain Domain
  650. * @param string $msg String to translate
  651. * @param string $return true to return, false to echo
  652. * @return translated string if $return is false string will be echoed
  653. */
  654. function __d($domain, $msg, $return = false) {
  655. if (!$msg) {
  656. return;
  657. }
  658. if (!class_exists('I18n')) {
  659. App::import('Core', 'i18n');
  660. }
  661. if ($return === false) {
  662. echo I18n::translate($msg, null, $domain);
  663. } else {
  664. return I18n::translate($msg, null, $domain);
  665. }
  666. }
  667. /**
  668. * Allows you to override the current domain for a single plural message lookup.
  669. * Returns correct plural form of message identified by $singular and $plural for count $count
  670. * from domain $domain.
  671. *
  672. * @param string $domain Domain
  673. * @param string $singular Singular string to translate
  674. * @param string $plural Plural
  675. * @param integer $count Count
  676. * @param boolean $return true to return, false to echo
  677. * @return plural form of translated string if $return is false string will be echoed
  678. */
  679. function __dn($domain, $singular, $plural, $count, $return = false) {
  680. if (!$singular) {
  681. return;
  682. }
  683. if (!class_exists('I18n')) {
  684. App::import('Core', 'i18n');
  685. }
  686. if ($return === false) {
  687. echo I18n::translate($singular, $plural, $domain, 6, $count);
  688. } else {
  689. return I18n::translate($singular, $plural, $domain, 6, $count);
  690. }
  691. }
  692. /**
  693. * Allows you to override the current domain for a single message lookup.
  694. * It also allows you to specify a category.
  695. *
  696. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  697. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  698. *
  699. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  700. *
  701. * - LC_ALL 0
  702. * - LC_COLLATE 1
  703. * - LC_CTYPE 2
  704. * - LC_MONETARY 3
  705. * - LC_NUMERIC 4
  706. * - LC_TIME 5
  707. * - LC_MESSAGES 6
  708. *
  709. * @param string $domain Domain
  710. * @param string $msg Message to translate
  711. * @param integer $category Category
  712. * @param boolean $return true to return, false to echo
  713. * @return translated string if $return is false string will be echoed
  714. */
  715. function __dc($domain, $msg, $category, $return = false) {
  716. if (!$msg) {
  717. return;
  718. }
  719. if (!class_exists('I18n')) {
  720. App::import('Core', 'i18n');
  721. }
  722. if ($return === false) {
  723. echo I18n::translate($msg, null, $domain, $category);
  724. } else {
  725. return I18n::translate($msg, null, $domain, $category);
  726. }
  727. }
  728. /**
  729. * Allows you to override the current domain for a single plural message lookup.
  730. * It also allows you to specify a category.
  731. * Returns correct plural form of message identified by $singular and $plural for count $count
  732. * from domain $domain.
  733. *
  734. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  735. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  736. *
  737. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  738. *
  739. * - LC_ALL 0
  740. * - LC_COLLATE 1
  741. * - LC_CTYPE 2
  742. * - LC_MONETARY 3
  743. * - LC_NUMERIC 4
  744. * - LC_TIME 5
  745. * - LC_MESSAGES 6
  746. *
  747. * @param string $domain Domain
  748. * @param string $singular Singular string to translate
  749. * @param string $plural Plural
  750. * @param integer $count Count
  751. * @param integer $category Category
  752. * @param boolean $return true to return, false to echo
  753. * @return plural form of translated string if $return is false string will be echoed
  754. */
  755. function __dcn($domain, $singular, $plural, $count, $category, $return = false) {
  756. if (!$singular) {
  757. return;
  758. }
  759. if (!class_exists('I18n')) {
  760. App::import('Core', 'i18n');
  761. }
  762. if ($return === false) {
  763. echo I18n::translate($singular, $plural, $domain, $category, $count);
  764. } else {
  765. return I18n::translate($singular, $plural, $domain, $category, $count);
  766. }
  767. }
  768. /**
  769. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  770. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  771. *
  772. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  773. *
  774. * - LC_ALL 0
  775. * - LC_COLLATE 1
  776. * - LC_CTYPE 2
  777. * - LC_MONETARY 3
  778. * - LC_NUMERIC 4
  779. * - LC_TIME 5
  780. * - LC_MESSAGES 6
  781. *
  782. * @param string $msg String to translate
  783. * @param integer $category Category
  784. * @param string $return true to return, false to echo
  785. * @return translated string if $return is false string will be echoed
  786. */
  787. function __c($msg, $category, $return = false) {
  788. if (!$msg) {
  789. return;
  790. }
  791. if (!class_exists('I18n')) {
  792. App::import('Core', 'i18n');
  793. }
  794. if ($return === false) {
  795. echo I18n::translate($msg, null, null, $category);
  796. } else {
  797. return I18n::translate($msg, null, null, $category);
  798. }
  799. }
  800. /**
  801. * Computes the difference of arrays using keys for comparison.
  802. *
  803. * @param array First array
  804. * @param array Second array
  805. * @return array Array with different keys
  806. * @deprecated Will be removed in 2.0
  807. */
  808. if (!function_exists('array_diff_key')) {
  809. function array_diff_key() {
  810. $valuesDiff = array();
  811. $argc = func_num_args();
  812. if ($argc < 2) {
  813. return false;
  814. }
  815. $args = func_get_args();
  816. foreach ($args as $param) {
  817. if (!is_array($param)) {
  818. return false;
  819. }
  820. }
  821. foreach ($args[0] as $valueKey => $valueData) {
  822. for ($i = 1; $i < $argc; $i++) {
  823. if (array_key_exists($valueKey, $args[$i])) {
  824. continue 2;
  825. }
  826. }
  827. $valuesDiff[$valueKey] = $valueData;
  828. }
  829. return $valuesDiff;
  830. }
  831. }
  832. /**
  833. * Computes the intersection of arrays using keys for comparison
  834. *
  835. * @param array First array
  836. * @param array Second array
  837. * @return array Array with interesected keys
  838. * @deprecated Will be removed in 2.0
  839. */
  840. if (!function_exists('array_intersect_key')) {
  841. function array_intersect_key($arr1, $arr2) {
  842. $res = array();
  843. foreach ($arr1 as $key => $value) {
  844. if (array_key_exists($key, $arr2)) {
  845. $res[$key] = $arr1[$key];
  846. }
  847. }
  848. return $res;
  849. }
  850. }
  851. /**
  852. * Shortcut to Log::write.
  853. *
  854. * @param string $message Message to write to log
  855. */
  856. function LogError($message) {
  857. if (!class_exists('CakeLog')) {
  858. App::import('Core', 'CakeLog');
  859. }
  860. $bad = array("\n", "\r", "\t");
  861. $good = ' ';
  862. CakeLog::write('error', str_replace($bad, $good, $message));
  863. }
  864. /**
  865. * Searches include path for files.
  866. *
  867. * @param string $file File to look for
  868. * @return Full path to file if exists, otherwise false
  869. * @link http://book.cakephp.org/view/1131/fileExistsInPath
  870. */
  871. function fileExistsInPath($file) {
  872. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  873. foreach ($paths as $path) {
  874. $fullPath = $path . DS . $file;
  875. if (file_exists($fullPath)) {
  876. return $fullPath;
  877. } elseif (file_exists($file)) {
  878. return $file;
  879. }
  880. }
  881. return false;
  882. }
  883. /**
  884. * Convert forward slashes to underscores and removes first and last underscores in a string
  885. *
  886. * @param string String to convert
  887. * @return string with underscore remove from start and end of string
  888. * @link http://book.cakephp.org/view/1126/convertSlash
  889. */
  890. function convertSlash($string) {
  891. $string = trim($string, '/');
  892. $string = preg_replace('/\/\//', '/', $string);
  893. $string = str_replace('/', '_', $string);
  894. return $string;
  895. }
  896. /**
  897. * Implements http_build_query for PHP4.
  898. *
  899. * @param string $data Data to set in query string
  900. * @param string $prefix If numeric indices, prepend this to index for elements in base array.
  901. * @param string $argSep String used to separate arguments
  902. * @param string $baseKey Base key
  903. * @return string URL encoded query string
  904. * @see http://php.net/http_build_query
  905. * @deprecated Will be removed in 2.0
  906. */
  907. if (!function_exists('http_build_query')) {
  908. function http_build_query($data, $prefix = null, $argSep = null, $baseKey = null) {
  909. if (empty($argSep)) {
  910. $argSep = ini_get('arg_separator.output');
  911. }
  912. if (is_object($data)) {
  913. $data = get_object_vars($data);
  914. }
  915. $out = array();
  916. foreach ((array)$data as $key => $v) {
  917. if (is_numeric($key) && !empty($prefix)) {
  918. $key = $prefix . $key;
  919. }
  920. $key = urlencode($key);
  921. if (!empty($baseKey)) {
  922. $key = $baseKey . '[' . $key . ']';
  923. }
  924. if (is_array($v) || is_object($v)) {
  925. $out[] = http_build_query($v, $prefix, $argSep, $key);
  926. } else {
  927. $out[] = $key . '=' . urlencode($v);
  928. }
  929. }
  930. return implode($argSep, $out);
  931. }
  932. }
  933. /**
  934. * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
  935. * Don't use for isset() conditions, or wrap your variable with @ operator:
  936. * Example:
  937. *
  938. * `ife(isset($variable), @$variable, 'default');`
  939. *
  940. * @param mixed $condition Conditional expression
  941. * @param mixed $val1 Value to return in case condition matches
  942. * @param mixed $val2 Value to return if condition doesn't match
  943. * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
  944. * @link http://book.cakephp.org/view/1133/ife
  945. * @deprecated Will be removed in 2.0
  946. */
  947. function ife($condition, $val1 = null, $val2 = null) {
  948. if (!empty($condition)) {
  949. return $val1;
  950. }
  951. return $val2;
  952. }