Base for a static organization website

basics.php 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. <?php
  2. /**
  3. * Basic CakePHP functionality.
  4. *
  5. * Core functions for including other source files, loading models and so forth.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake
  17. * @since CakePHP(tm) v 0.2.9
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Basic defines for timing functions.
  22. */
  23. define('SECOND', 1);
  24. define('MINUTE', 60);
  25. define('HOUR', 3600);
  26. define('DAY', 86400);
  27. define('WEEK', 604800);
  28. define('MONTH', 2592000);
  29. define('YEAR', 31536000);
  30. if (!function_exists('config')) {
  31. /**
  32. * Loads configuration files. Receives a set of configuration files
  33. * to load.
  34. * Example:
  35. *
  36. * `config('config1', 'config2');`
  37. *
  38. * @return bool 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. $count = count($args);
  44. $included = 0;
  45. foreach ($args as $arg) {
  46. if (file_exists(APP . 'Config' . DS . $arg . '.php')) {
  47. include_once APP . 'Config' . DS . $arg . '.php';
  48. $included++;
  49. }
  50. }
  51. return $included === $count;
  52. }
  53. }
  54. if (!function_exists('debug')) {
  55. /**
  56. * Prints out debug information about given variable.
  57. *
  58. * Only runs if debug level is greater than zero.
  59. *
  60. * @param mixed $var Variable to show debug information for.
  61. * @param bool $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  62. * @param bool $showFrom If set to true, the method prints from where the function was called.
  63. * @return void
  64. * @link http://book.cakephp.org/2.0/en/development/debugging.html#basic-debugging
  65. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
  66. */
  67. function debug($var, $showHtml = null, $showFrom = true) {
  68. if (!Configure::read('debug')) {
  69. return;
  70. }
  71. App::uses('Debugger', 'Utility');
  72. $file = '';
  73. $line = '';
  74. $lineInfo = '';
  75. if ($showFrom) {
  76. $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
  77. $file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
  78. $line = $trace[0]['line'];
  79. }
  80. $html = <<<HTML
  81. <div class="cake-debug-output">
  82. %s
  83. <pre class="cake-debug">
  84. %s
  85. </pre>
  86. </div>
  87. HTML;
  88. $text = <<<TEXT
  89. %s
  90. ########## DEBUG ##########
  91. %s
  92. ###########################
  93. TEXT;
  94. $template = $html;
  95. if (PHP_SAPI === 'cli' || $showHtml === false) {
  96. $template = $text;
  97. if ($showFrom) {
  98. $lineInfo = sprintf('%s (line %s)', $file, $line);
  99. }
  100. }
  101. if ($showHtml === null && $template !== $text) {
  102. $showHtml = true;
  103. }
  104. $var = Debugger::exportVar($var, 25);
  105. if ($showHtml) {
  106. $template = $html;
  107. $var = h($var);
  108. if ($showFrom) {
  109. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  110. }
  111. }
  112. printf($template, $lineInfo, $var);
  113. }
  114. }
  115. if (!function_exists('stackTrace')) {
  116. /**
  117. * Outputs a stack trace based on the supplied options.
  118. *
  119. * ### Options
  120. *
  121. * - `depth` - The number of stack frames to return. Defaults to 999
  122. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  123. * will be displayed.
  124. * - `start` - The stack frame to start generating a trace from. Defaults to 1
  125. *
  126. * @param array $options Format for outputting stack trace
  127. * @return mixed Formatted stack trace
  128. * @see Debugger::trace()
  129. */
  130. function stackTrace(array $options = array()) {
  131. if (!Configure::read('debug')) {
  132. return;
  133. }
  134. App::uses('Debugger', 'Utility');
  135. $options += array('start' => 0);
  136. $options['start']++;
  137. echo Debugger::trace($options);
  138. }
  139. }
  140. if (!function_exists('sortByKey')) {
  141. /**
  142. * Sorts given $array by key $sortBy.
  143. *
  144. * @param array &$array Array to sort
  145. * @param string $sortBy Sort by this key
  146. * @param string $order Sort order asc/desc (ascending or descending).
  147. * @param int $type Type of sorting to perform
  148. * @return array|null Sorted array, or null if not an array.
  149. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#sortByKey
  150. */
  151. function sortByKey(&$array, $sortBy, $order = 'asc', $type = SORT_NUMERIC) {
  152. if (!is_array($array)) {
  153. return null;
  154. }
  155. foreach ($array as $key => $val) {
  156. $sa[$key] = $val[$sortBy];
  157. }
  158. if ($order === 'asc') {
  159. asort($sa, $type);
  160. } else {
  161. arsort($sa, $type);
  162. }
  163. foreach ($sa as $key => $val) {
  164. $out[] = $array[$key];
  165. }
  166. return $out;
  167. }
  168. }
  169. if (!function_exists('h')) {
  170. /**
  171. * Convenience method for htmlspecialchars.
  172. *
  173. * @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
  174. * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
  175. * implement a `__toString` method. Otherwise the class name will be used.
  176. * @param bool $double Encode existing html entities
  177. * @param string $charset Character set to use when escaping. Defaults to config value in 'App.encoding' or 'UTF-8'
  178. * @return string Wrapped text
  179. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#h
  180. */
  181. function h($text, $double = true, $charset = null) {
  182. if (is_string($text)) {
  183. //optimize for strings
  184. } elseif (is_array($text)) {
  185. $texts = array();
  186. foreach ($text as $k => $t) {
  187. $texts[$k] = h($t, $double, $charset);
  188. }
  189. return $texts;
  190. } elseif (is_object($text)) {
  191. if (method_exists($text, '__toString')) {
  192. $text = (string)$text;
  193. } else {
  194. $text = '(object)' . get_class($text);
  195. }
  196. } elseif (is_bool($text)) {
  197. return $text;
  198. }
  199. static $defaultCharset = false;
  200. if ($defaultCharset === false) {
  201. $defaultCharset = Configure::read('App.encoding');
  202. if ($defaultCharset === null) {
  203. $defaultCharset = 'UTF-8';
  204. }
  205. }
  206. if (is_string($double)) {
  207. $charset = $double;
  208. }
  209. return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
  210. }
  211. }
  212. if (!function_exists('pluginSplit')) {
  213. /**
  214. * Splits a dot syntax plugin name into its plugin and class name.
  215. * If $name does not have a dot, then index 0 will be null.
  216. *
  217. * Commonly used like `list($plugin, $name) = pluginSplit($name);`
  218. *
  219. * @param string $name The name you want to plugin split.
  220. * @param bool $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  221. * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  222. * @return array Array with 2 indexes. 0 => plugin name, 1 => class name
  223. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pluginSplit
  224. */
  225. function pluginSplit($name, $dotAppend = false, $plugin = null) {
  226. if (strpos($name, '.') !== false) {
  227. $parts = explode('.', $name, 2);
  228. if ($dotAppend) {
  229. $parts[0] .= '.';
  230. }
  231. return $parts;
  232. }
  233. return array($plugin, $name);
  234. }
  235. }
  236. if (!function_exists('pr')) {
  237. /**
  238. * print_r() convenience function
  239. *
  240. * In terminals this will act the same as using print_r() directly, when not run on cli
  241. * print_r() will wrap <PRE> tags around the output of given array. Similar to debug().
  242. *
  243. * @param mixed $var Variable to print out
  244. * @return void
  245. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pr
  246. * @see debug()
  247. */
  248. function pr($var) {
  249. if (Configure::read('debug') > 0) {
  250. $template = PHP_SAPI !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
  251. printf($template, print_r($var, true));
  252. }
  253. }
  254. }
  255. if (!function_exists('am')) {
  256. /**
  257. * Merge a group of arrays
  258. *
  259. * Accepts variable arguments. Each argument will be converted into an array and then merged.
  260. *
  261. * @return array All array parameters merged into one
  262. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#am
  263. */
  264. function am() {
  265. $r = array();
  266. $args = func_get_args();
  267. foreach ($args as $a) {
  268. if (!is_array($a)) {
  269. $a = array($a);
  270. }
  271. $r = array_merge($r, $a);
  272. }
  273. return $r;
  274. }
  275. }
  276. if (!function_exists('env')) {
  277. /**
  278. * Gets an environment variable from available sources, and provides emulation
  279. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  280. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  281. * environment information.
  282. *
  283. * @param string $key Environment variable name.
  284. * @return string|bool|null Environment variable setting.
  285. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#env
  286. */
  287. function env($key) {
  288. if ($key === 'HTTPS') {
  289. if (isset($_SERVER['HTTPS'])) {
  290. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  291. }
  292. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  293. }
  294. if ($key === 'SCRIPT_NAME') {
  295. if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  296. $key = 'SCRIPT_URL';
  297. }
  298. }
  299. $val = null;
  300. if (isset($_SERVER[$key])) {
  301. $val = $_SERVER[$key];
  302. } elseif (isset($_ENV[$key])) {
  303. $val = $_ENV[$key];
  304. } elseif (getenv($key) !== false) {
  305. $val = getenv($key);
  306. }
  307. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  308. $addr = env('HTTP_PC_REMOTE_ADDR');
  309. if ($addr !== null) {
  310. $val = $addr;
  311. }
  312. }
  313. if ($val !== null) {
  314. return $val;
  315. }
  316. switch ($key) {
  317. case 'DOCUMENT_ROOT':
  318. $name = env('SCRIPT_NAME');
  319. $filename = env('SCRIPT_FILENAME');
  320. $offset = 0;
  321. if (!strpos($name, '.php')) {
  322. $offset = 4;
  323. }
  324. return substr($filename, 0, -(strlen($name) + $offset));
  325. case 'PHP_SELF':
  326. return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  327. case 'CGI_MODE':
  328. return (PHP_SAPI === 'cgi');
  329. case 'HTTP_BASE':
  330. $host = env('HTTP_HOST');
  331. $parts = explode('.', $host);
  332. $count = count($parts);
  333. if ($count === 1) {
  334. return '.' . $host;
  335. } elseif ($count === 2) {
  336. return '.' . $host;
  337. } elseif ($count === 3) {
  338. $gTLD = array(
  339. 'aero',
  340. 'asia',
  341. 'biz',
  342. 'cat',
  343. 'com',
  344. 'coop',
  345. 'edu',
  346. 'gov',
  347. 'info',
  348. 'int',
  349. 'jobs',
  350. 'mil',
  351. 'mobi',
  352. 'museum',
  353. 'name',
  354. 'net',
  355. 'org',
  356. 'pro',
  357. 'tel',
  358. 'travel',
  359. 'xxx'
  360. );
  361. if (in_array($parts[1], $gTLD)) {
  362. return '.' . $host;
  363. }
  364. }
  365. array_shift($parts);
  366. return '.' . implode('.', $parts);
  367. }
  368. return null;
  369. }
  370. }
  371. if (!function_exists('cache')) {
  372. /**
  373. * Reads/writes temporary data to cache files or session.
  374. *
  375. * @param string $path File path within /tmp to save the file.
  376. * @param mixed $data The data to save to the temporary file.
  377. * @param mixed $expires A valid strtotime string when the data expires.
  378. * @param string $target The target of the cached data; either 'cache' or 'public'.
  379. * @return mixed The contents of the temporary file.
  380. * @deprecated 3.0.0 Will be removed in 3.0. Please use Cache::write() instead.
  381. */
  382. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  383. if (Configure::read('Cache.disable')) {
  384. return null;
  385. }
  386. $now = time();
  387. if (!is_numeric($expires)) {
  388. $expires = strtotime($expires, $now);
  389. }
  390. switch (strtolower($target)) {
  391. case 'cache':
  392. $filename = CACHE . $path;
  393. break;
  394. case 'public':
  395. $filename = WWW_ROOT . $path;
  396. break;
  397. case 'tmp':
  398. $filename = TMP . $path;
  399. break;
  400. }
  401. $timediff = $expires - $now;
  402. $filetime = false;
  403. if (file_exists($filename)) {
  404. //@codingStandardsIgnoreStart
  405. $filetime = @filemtime($filename);
  406. //@codingStandardsIgnoreEnd
  407. }
  408. if ($data === null) {
  409. if (file_exists($filename) && $filetime !== false) {
  410. if ($filetime + $timediff < $now) {
  411. //@codingStandardsIgnoreStart
  412. @unlink($filename);
  413. //@codingStandardsIgnoreEnd
  414. } else {
  415. //@codingStandardsIgnoreStart
  416. $data = @file_get_contents($filename);
  417. //@codingStandardsIgnoreEnd
  418. }
  419. }
  420. } elseif (is_writable(dirname($filename))) {
  421. //@codingStandardsIgnoreStart
  422. @file_put_contents($filename, $data, LOCK_EX);
  423. //@codingStandardsIgnoreEnd
  424. }
  425. return $data;
  426. }
  427. }
  428. if (!function_exists('clearCache')) {
  429. /**
  430. * Used to delete files in the cache directories, or clear contents of cache directories
  431. *
  432. * @param string|array $params As String name to be searched for deletion, if name is a directory all files in
  433. * directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
  434. * all files in app/tmp/cache/views will be deleted
  435. * @param string $type Directory in tmp/cache defaults to view directory
  436. * @param string $ext The file extension you are deleting
  437. * @return true if files found and deleted false otherwise
  438. */
  439. function clearCache($params = null, $type = 'views', $ext = '.php') {
  440. if (is_string($params) || $params === null) {
  441. $params = preg_replace('/\/\//', '/', $params);
  442. $cache = CACHE . $type . DS . $params;
  443. if (is_file($cache . $ext)) {
  444. //@codingStandardsIgnoreStart
  445. @unlink($cache . $ext);
  446. //@codingStandardsIgnoreEnd
  447. return true;
  448. } elseif (is_dir($cache)) {
  449. $files = glob($cache . '*');
  450. if ($files === false) {
  451. return false;
  452. }
  453. foreach ($files as $file) {
  454. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  455. //@codingStandardsIgnoreStart
  456. @unlink($file);
  457. //@codingStandardsIgnoreEnd
  458. }
  459. }
  460. return true;
  461. }
  462. $cache = array(
  463. CACHE . $type . DS . '*' . $params . $ext,
  464. CACHE . $type . DS . '*' . $params . '_*' . $ext
  465. );
  466. $files = array();
  467. while ($search = array_shift($cache)) {
  468. $results = glob($search);
  469. if ($results !== false) {
  470. $files = array_merge($files, $results);
  471. }
  472. }
  473. if (empty($files)) {
  474. return false;
  475. }
  476. foreach ($files as $file) {
  477. if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
  478. //@codingStandardsIgnoreStart
  479. @unlink($file);
  480. //@codingStandardsIgnoreEnd
  481. }
  482. }
  483. return true;
  484. } elseif (is_array($params)) {
  485. foreach ($params as $file) {
  486. clearCache($file, $type, $ext);
  487. }
  488. return true;
  489. }
  490. return false;
  491. }
  492. }
  493. if (!function_exists('stripslashes_deep')) {
  494. /**
  495. * Recursively strips slashes from all values in an array
  496. *
  497. * @param array $values Array of values to strip slashes
  498. * @return mixed What is returned from calling stripslashes
  499. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#stripslashes_deep
  500. */
  501. function stripslashes_deep($values) {
  502. if (is_array($values)) {
  503. foreach ($values as $key => $value) {
  504. $values[$key] = stripslashes_deep($value);
  505. }
  506. } else {
  507. $values = stripslashes($values);
  508. }
  509. return $values;
  510. }
  511. }
  512. if (!function_exists('__')) {
  513. /**
  514. * Returns a translated string if one is found; Otherwise, the submitted message.
  515. *
  516. * @param string $singular Text to translate
  517. * @param mixed $args Array with arguments or multiple arguments in function
  518. * @return mixed translated string
  519. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
  520. */
  521. function __($singular, $args = null) {
  522. if (!$singular) {
  523. return null;
  524. }
  525. App::uses('I18n', 'I18n');
  526. $translated = I18n::translate($singular);
  527. $arguments = func_get_args();
  528. return I18n::insertArgs($translated, array_slice($arguments, 1));
  529. }
  530. }
  531. if (!function_exists('__n')) {
  532. /**
  533. * Returns correct plural form of message identified by $singular and $plural for count $count.
  534. * Some languages have more than one form for plural messages dependent on the count.
  535. *
  536. * @param string $singular Singular text to translate
  537. * @param string $plural Plural text
  538. * @param int $count Count
  539. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  540. * @return mixed plural form of translated string
  541. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
  542. */
  543. function __n($singular, $plural, $count, $args = null) {
  544. if (!$singular) {
  545. return null;
  546. }
  547. App::uses('I18n', 'I18n');
  548. $translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count);
  549. $arguments = func_get_args();
  550. return I18n::insertArgs($translated, array_slice($arguments, 3));
  551. }
  552. }
  553. if (!function_exists('__d')) {
  554. /**
  555. * Allows you to override the current domain for a single message lookup.
  556. *
  557. * @param string $domain Domain
  558. * @param string $msg String to translate
  559. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  560. * @return string translated string
  561. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__d
  562. */
  563. function __d($domain, $msg, $args = null) {
  564. if (!$msg) {
  565. return null;
  566. }
  567. App::uses('I18n', 'I18n');
  568. $translated = I18n::translate($msg, null, $domain);
  569. $arguments = func_get_args();
  570. return I18n::insertArgs($translated, array_slice($arguments, 2));
  571. }
  572. }
  573. if (!function_exists('__dn')) {
  574. /**
  575. * Allows you to override the current domain for a single plural message lookup.
  576. * Returns correct plural form of message identified by $singular and $plural for count $count
  577. * from domain $domain.
  578. *
  579. * @param string $domain Domain
  580. * @param string $singular Singular string to translate
  581. * @param string $plural Plural
  582. * @param int $count Count
  583. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  584. * @return string plural form of translated string
  585. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
  586. */
  587. function __dn($domain, $singular, $plural, $count, $args = null) {
  588. if (!$singular) {
  589. return null;
  590. }
  591. App::uses('I18n', 'I18n');
  592. $translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count);
  593. $arguments = func_get_args();
  594. return I18n::insertArgs($translated, array_slice($arguments, 4));
  595. }
  596. }
  597. if (!function_exists('__dc')) {
  598. /**
  599. * Allows you to override the current domain for a single message lookup.
  600. * It also allows you to specify a category.
  601. *
  602. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  603. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  604. *
  605. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  606. *
  607. * - LC_ALL I18n::LC_ALL
  608. * - LC_COLLATE I18n::LC_COLLATE
  609. * - LC_CTYPE I18n::LC_CTYPE
  610. * - LC_MONETARY I18n::LC_MONETARY
  611. * - LC_NUMERIC I18n::LC_NUMERIC
  612. * - LC_TIME I18n::LC_TIME
  613. * - LC_MESSAGES I18n::LC_MESSAGES
  614. *
  615. * @param string $domain Domain
  616. * @param string $msg Message to translate
  617. * @param int $category Category
  618. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  619. * @return string translated string
  620. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dc
  621. */
  622. function __dc($domain, $msg, $category, $args = null) {
  623. if (!$msg) {
  624. return null;
  625. }
  626. App::uses('I18n', 'I18n');
  627. $translated = I18n::translate($msg, null, $domain, $category);
  628. $arguments = func_get_args();
  629. return I18n::insertArgs($translated, array_slice($arguments, 3));
  630. }
  631. }
  632. if (!function_exists('__dcn')) {
  633. /**
  634. * Allows you to override the current domain for a single plural message lookup.
  635. * It also allows you to specify a category.
  636. * Returns correct plural form of message identified by $singular and $plural for count $count
  637. * from domain $domain.
  638. *
  639. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  640. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  641. *
  642. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  643. *
  644. * - LC_ALL I18n::LC_ALL
  645. * - LC_COLLATE I18n::LC_COLLATE
  646. * - LC_CTYPE I18n::LC_CTYPE
  647. * - LC_MONETARY I18n::LC_MONETARY
  648. * - LC_NUMERIC I18n::LC_NUMERIC
  649. * - LC_TIME I18n::LC_TIME
  650. * - LC_MESSAGES I18n::LC_MESSAGES
  651. *
  652. * @param string $domain Domain
  653. * @param string $singular Singular string to translate
  654. * @param string $plural Plural
  655. * @param int $count Count
  656. * @param int $category Category
  657. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  658. * @return string plural form of translated string
  659. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dcn
  660. */
  661. function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
  662. if (!$singular) {
  663. return null;
  664. }
  665. App::uses('I18n', 'I18n');
  666. $translated = I18n::translate($singular, $plural, $domain, $category, $count);
  667. $arguments = func_get_args();
  668. return I18n::insertArgs($translated, array_slice($arguments, 5));
  669. }
  670. }
  671. if (!function_exists('__c')) {
  672. /**
  673. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  674. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  675. *
  676. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  677. *
  678. * - LC_ALL I18n::LC_ALL
  679. * - LC_COLLATE I18n::LC_COLLATE
  680. * - LC_CTYPE I18n::LC_CTYPE
  681. * - LC_MONETARY I18n::LC_MONETARY
  682. * - LC_NUMERIC I18n::LC_NUMERIC
  683. * - LC_TIME I18n::LC_TIME
  684. * - LC_MESSAGES I18n::LC_MESSAGES
  685. *
  686. * @param string $msg String to translate
  687. * @param int $category Category
  688. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  689. * @return string translated string
  690. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__c
  691. */
  692. function __c($msg, $category, $args = null) {
  693. if (!$msg) {
  694. return null;
  695. }
  696. App::uses('I18n', 'I18n');
  697. $translated = I18n::translate($msg, null, null, $category);
  698. $arguments = func_get_args();
  699. return I18n::insertArgs($translated, array_slice($arguments, 2));
  700. }
  701. }
  702. if (!function_exists('__x')) {
  703. /**
  704. * Returns a translated string if one is found; Otherwise, the submitted message.
  705. *
  706. * @param string $context Context of the text
  707. * @param string $singular Text to translate
  708. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  709. * @return mixed translated string
  710. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
  711. */
  712. function __x($context, $singular, $args = null) {
  713. if (!$singular) {
  714. return null;
  715. }
  716. App::uses('I18n', 'I18n');
  717. $translated = I18n::translate($singular, null, null, null, null, null, $context);
  718. $arguments = func_get_args();
  719. return I18n::insertArgs($translated, array_slice($arguments, 2));
  720. }
  721. }
  722. if (!function_exists('__xn')) {
  723. /**
  724. * Returns correct plural form of message identified by $singular and $plural for count $count.
  725. * Some languages have more than one form for plural messages dependent on the count.
  726. *
  727. * @param string $context Context of the text
  728. * @param string $singular Singular text to translate
  729. * @param string $plural Plural text
  730. * @param int $count Count
  731. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  732. * @return mixed plural form of translated string
  733. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
  734. */
  735. function __xn($context, $singular, $plural, $count, $args = null) {
  736. if (!$singular) {
  737. return null;
  738. }
  739. App::uses('I18n', 'I18n');
  740. $translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count, null, $context);
  741. $arguments = func_get_args();
  742. return I18n::insertArgs($translated, array_slice($arguments, 4));
  743. }
  744. }
  745. if (!function_exists('__dx')) {
  746. /**
  747. * Allows you to override the current domain for a single message lookup.
  748. *
  749. * @param string $domain Domain
  750. * @param string $context Context of the text
  751. * @param string $msg String to translate
  752. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  753. * @return string translated string
  754. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__d
  755. */
  756. function __dx($domain, $context, $msg, $args = null) {
  757. if (!$msg) {
  758. return null;
  759. }
  760. App::uses('I18n', 'I18n');
  761. $translated = I18n::translate($msg, null, $domain, null, null, null, $context);
  762. $arguments = func_get_args();
  763. return I18n::insertArgs($translated, array_slice($arguments, 3));
  764. }
  765. }
  766. if (!function_exists('__dxn')) {
  767. /**
  768. * Allows you to override the current domain for a single plural message lookup.
  769. * Returns correct plural form of message identified by $singular and $plural for count $count
  770. * from domain $domain.
  771. *
  772. * @param string $domain Domain
  773. * @param string $context Context of the text
  774. * @param string $singular Singular string to translate
  775. * @param string $plural Plural
  776. * @param int $count Count
  777. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  778. * @return string plural form of translated string
  779. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
  780. */
  781. function __dxn($domain, $context, $singular, $plural, $count, $args = null) {
  782. if (!$singular) {
  783. return null;
  784. }
  785. App::uses('I18n', 'I18n');
  786. $translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count, null, $context);
  787. $arguments = func_get_args();
  788. return I18n::insertArgs($translated, array_slice($arguments, 5));
  789. }
  790. }
  791. if (!function_exists('__dxc')) {
  792. /**
  793. * Allows you to override the current domain for a single message lookup.
  794. * It also allows you to specify a category.
  795. *
  796. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  797. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  798. *
  799. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  800. *
  801. * - LC_ALL I18n::LC_ALL
  802. * - LC_COLLATE I18n::LC_COLLATE
  803. * - LC_CTYPE I18n::LC_CTYPE
  804. * - LC_MONETARY I18n::LC_MONETARY
  805. * - LC_NUMERIC I18n::LC_NUMERIC
  806. * - LC_TIME I18n::LC_TIME
  807. * - LC_MESSAGES I18n::LC_MESSAGES
  808. *
  809. * @param string $domain Domain
  810. * @param string $context Context of the text
  811. * @param string $msg Message to translate
  812. * @param int $category Category
  813. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  814. * @return string translated string
  815. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dc
  816. */
  817. function __dxc($domain, $context, $msg, $category, $args = null) {
  818. if (!$msg) {
  819. return null;
  820. }
  821. App::uses('I18n', 'I18n');
  822. $translated = I18n::translate($msg, null, $domain, $category, null, null, $context);
  823. $arguments = func_get_args();
  824. return I18n::insertArgs($translated, array_slice($arguments, 4));
  825. }
  826. }
  827. if (!function_exists('__dxcn')) {
  828. /**
  829. * Allows you to override the current domain for a single plural message lookup.
  830. * It also allows you to specify a category.
  831. * Returns correct plural form of message identified by $singular and $plural for count $count
  832. * from domain $domain.
  833. *
  834. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  835. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  836. *
  837. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  838. *
  839. * - LC_ALL I18n::LC_ALL
  840. * - LC_COLLATE I18n::LC_COLLATE
  841. * - LC_CTYPE I18n::LC_CTYPE
  842. * - LC_MONETARY I18n::LC_MONETARY
  843. * - LC_NUMERIC I18n::LC_NUMERIC
  844. * - LC_TIME I18n::LC_TIME
  845. * - LC_MESSAGES I18n::LC_MESSAGES
  846. *
  847. * @param string $domain Domain
  848. * @param string $context Context of the text
  849. * @param string $singular Singular string to translate
  850. * @param string $plural Plural
  851. * @param int $count Count
  852. * @param int $category Category
  853. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  854. * @return string plural form of translated string
  855. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dcn
  856. */
  857. function __dxcn($domain, $context, $singular, $plural, $count, $category, $args = null) {
  858. if (!$singular) {
  859. return null;
  860. }
  861. App::uses('I18n', 'I18n');
  862. $translated = I18n::translate($singular, $plural, $domain, $category, $count, null, $context);
  863. $arguments = func_get_args();
  864. return I18n::insertArgs($translated, array_slice($arguments, 6));
  865. }
  866. }
  867. if (!function_exists('__xc')) {
  868. /**
  869. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  870. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  871. *
  872. * Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
  873. *
  874. * - LC_ALL I18n::LC_ALL
  875. * - LC_COLLATE I18n::LC_COLLATE
  876. * - LC_CTYPE I18n::LC_CTYPE
  877. * - LC_MONETARY I18n::LC_MONETARY
  878. * - LC_NUMERIC I18n::LC_NUMERIC
  879. * - LC_TIME I18n::LC_TIME
  880. * - LC_MESSAGES I18n::LC_MESSAGES
  881. *
  882. * @param string $context Context of the text
  883. * @param string $msg String to translate
  884. * @param int $category Category
  885. * @param mixed $args Array with arguments or multiple arguments in function, otherwise null.
  886. * @return string translated string
  887. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__c
  888. */
  889. function __xc($context, $msg, $category, $args = null) {
  890. if (!$msg) {
  891. return null;
  892. }
  893. App::uses('I18n', 'I18n');
  894. $translated = I18n::translate($msg, null, null, $category, null, null, $context);
  895. $arguments = func_get_args();
  896. return I18n::insertArgs($translated, array_slice($arguments, 3));
  897. }
  898. }
  899. if (!function_exists('LogError')) {
  900. /**
  901. * Shortcut to Log::write.
  902. *
  903. * @param string $message Message to write to log
  904. * @return void
  905. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#LogError
  906. */
  907. function LogError($message) {
  908. App::uses('CakeLog', 'Log');
  909. $bad = array("\n", "\r", "\t");
  910. $good = ' ';
  911. CakeLog::write('error', str_replace($bad, $good, $message));
  912. }
  913. }
  914. if (!function_exists('fileExistsInPath')) {
  915. /**
  916. * Searches include path for files.
  917. *
  918. * @param string $file File to look for
  919. * @return string Full path to file if exists, otherwise false
  920. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#fileExistsInPath
  921. */
  922. function fileExistsInPath($file) {
  923. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  924. foreach ($paths as $path) {
  925. $fullPath = $path . DS . $file;
  926. if (file_exists($fullPath)) {
  927. return $fullPath;
  928. } elseif (file_exists($file)) {
  929. return $file;
  930. }
  931. }
  932. return false;
  933. }
  934. }
  935. if (!function_exists('convertSlash')) {
  936. /**
  937. * Convert forward slashes to underscores and removes first and last underscores in a string
  938. *
  939. * @param string $string String to convert
  940. * @return string with underscore remove from start and end of string
  941. * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#convertSlash
  942. */
  943. function convertSlash($string) {
  944. $string = trim($string, '/');
  945. $string = preg_replace('/\/\//', '/', $string);
  946. $string = str_replace('/', '_', $string);
  947. return $string;
  948. }
  949. }
  950. if (!function_exists('json_last_error_msg')) {
  951. /**
  952. * Provides the fallback implementation of json_last_error_msg() available in PHP 5.5 and above.
  953. *
  954. * @return string Error message.
  955. */
  956. function json_last_error_msg() {
  957. static $errors = array(
  958. JSON_ERROR_NONE => '',
  959. JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
  960. JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
  961. JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
  962. JSON_ERROR_SYNTAX => 'Syntax error',
  963. JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
  964. );
  965. $error = json_last_error();
  966. return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
  967. }
  968. }