PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/basics.php

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