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

/cake/basics.php

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