PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/basics.php

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