bootstrap.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. // For date()
  3. define('FORMAT_DB_DATETIME', 'Y-m-d H:i:s');
  4. define('FORMAT_DB_DATE', 'Y-m-d');
  5. define('FORMAT_DB_TIME', 'H:i:s');
  6. # Make the app and l10n play nice with Windows.
  7. if (substr(PHP_OS, 0, 3) === 'WIN') { // || strpos(@php_uname(), 'ARCH')
  8. define('WINDOWS', true);
  9. } else {
  10. define('WINDOWS', false);
  11. }
  12. /**
  13. * Convenience function to check on "empty()"
  14. *
  15. * @param mixed $var
  16. * @return bool Result
  17. */
  18. function isEmpty($var = null) {
  19. if (empty($var)) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. /**
  25. * Return of what type the specific value is
  26. *
  27. * //TODO: use Debugger::exportVar() instead?
  28. *
  29. * @param mixed $value
  30. * @return type (NULL, array, bool, float, int, string, object, unknown) + value
  31. */
  32. function returns($value) {
  33. if ($value === null) {
  34. return 'NULL';
  35. } elseif (is_array($value)) {
  36. return '(array)' . '<pre>' . print_r($value, true) . '</pre>';
  37. } elseif ($value === true) {
  38. return '(bool)TRUE';
  39. } elseif ($value === false) {
  40. return '(bool)FALSE';
  41. } elseif (is_numeric($value) && is_float($value)) {
  42. return '(float)' . $value;
  43. } elseif (is_numeric($value) && is_int($value)) {
  44. return '(int)' . $value;
  45. } elseif (is_string($value)) {
  46. return '(string)' . $value;
  47. } elseif (is_object($value)) {
  48. return '(object)' . get_class($value) . '<pre>' . print_r($value, true) .
  49. '</pre>';
  50. } else {
  51. return '(unknown)' . $value;
  52. }
  53. }
  54. /**
  55. * Returns htmlentities - string
  56. *
  57. * ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
  58. * ENT_QUOTES = Will convert both double and single quotes. !!!
  59. * ENT_NOQUOTES = Will leave both double and single quotes unconverted.
  60. *
  61. * @param string $text
  62. * @return string Converted text
  63. */
  64. function ent($text) {
  65. return (!empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '');
  66. }
  67. /**
  68. * Convenience method for htmlspecialchars_decode
  69. *
  70. * @param string $text Text to wrap through htmlspecialchars_decode
  71. * @return string Converted text
  72. */
  73. function hDec($text, $quoteStyle = ENT_QUOTES) {
  74. if (is_array($text)) {
  75. return array_map('hDec', $text);
  76. }
  77. return trim(htmlspecialchars_decode($text, $quoteStyle));
  78. }
  79. /**
  80. * Convenience method for html_entity_decode
  81. *
  82. * @param string $text Text to wrap through htmlspecialchars_decode
  83. * @return string Converted text
  84. */
  85. function entDec($text, $quoteStyle = ENT_QUOTES) {
  86. if (is_array($text)) {
  87. return array_map('entDec', $text);
  88. }
  89. return (!empty($text) ? trim(html_entity_decode($text, $quoteStyle, 'UTF-8')) : '');
  90. }
  91. /**
  92. * Focus is on the filename (without path)
  93. *
  94. * @param string filename to check on
  95. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  96. * @return mixed
  97. */
  98. function extractFileInfo($filename, $type = null) {
  99. if ($info = extractPathInfo($filename, $type)) {
  100. return $info;
  101. }
  102. $pos = strrpos($filename, '.');
  103. $res = '';
  104. switch ($type) {
  105. case 'extension':
  106. case 'ext':
  107. $res = ($pos !== false) ? substr($filename, $pos + 1) : '';
  108. break;
  109. case 'filename':
  110. case 'file':
  111. $res = ($pos !== false) ? substr($filename, 0, $pos) : '';
  112. break;
  113. default:
  114. break;
  115. }
  116. return $res;
  117. }
  118. /**
  119. * Uses native PHP function to retrieve infos about a filename etc.
  120. * Improves it by not returning non-file-name characters from url files if specified.
  121. * So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
  122. *
  123. * @param string filename to check on
  124. * @param string type (extension/ext, filename/file, basename/base, dirname/dir)
  125. * @param bool $fromUrl
  126. * @return mixed
  127. */
  128. function extractPathInfo($filename, $type = null, $fromUrl = false) {
  129. switch ($type) {
  130. case 'extension':
  131. case 'ext':
  132. $infoType = PATHINFO_EXTENSION;
  133. break;
  134. case 'filename':
  135. case 'file':
  136. $infoType = PATHINFO_FILENAME;
  137. break;
  138. case 'basename':
  139. case 'base':
  140. $infoType = PATHINFO_BASENAME;
  141. break;
  142. case 'dirname':
  143. case 'dir':
  144. $infoType = PATHINFO_DIRNAME;
  145. break;
  146. default:
  147. $infoType = null;
  148. }
  149. $result = pathinfo($filename, $infoType);
  150. if ($fromUrl) {
  151. if (($pos = strpos($result, '#')) !== false) {
  152. $result = substr($result, 0, $pos);
  153. }
  154. if (($pos = strpos($result, '?')) !== false) {
  155. $result = substr($result, 0, $pos);
  156. }
  157. }
  158. return $result;
  159. }
  160. /**
  161. * Shows pr() messages, even with debug = 0.
  162. * Also allows additional customization.
  163. *
  164. * @param mixed $content
  165. * @param bool $collapsedAndExpandable
  166. * @param array $options
  167. * - class, showHtml, showFrom, jquery, returns, debug
  168. * @return string HTML
  169. */
  170. function pre($var, $collapsedAndExpandable = false, $options = array()) {
  171. $defaults = array(
  172. 'class' => 'cake-debug',
  173. 'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
  174. 'showFrom' => false, // Display file + line
  175. 'jquery' => null, // null => Auto - use jQuery (true/false to manually decide),
  176. 'returns' => false, // Use returns(),
  177. 'debug' => false // Show only with debug > 0
  178. );
  179. $options += $defaults;
  180. if ($options['debug'] && !Configure::read('debug')) {
  181. return '';
  182. }
  183. if (php_sapi_name() === 'cli') {
  184. return sprintf("\n%s\n", $options['returns'] ? returns($var) : print_r($var, true));
  185. }
  186. $res = '<div class="' . $options['class'] . '">';
  187. $pre = '';
  188. if ($collapsedAndExpandable) {
  189. $js = 'if (this.parentNode.getElementsByTagName(\'pre\')[0].style.display==\'block\') {this.parentNode.getElementsByTagName(\'pre\')[0].style.display=\'none\'} else {this.parentNode.getElementsByTagName(\'pre\')[0].style.display=\'block\'}';
  190. $jsJquery = 'jQuery(this).parent().children(\'pre\').slideToggle(\'fast\')';
  191. if ($options['jquery'] === true) {
  192. $js = $jsJquery;
  193. } elseif ($options['jquery'] !== false) {
  194. // auto
  195. $js = 'if (typeof jQuery == \'undefined\') {' . $js . '} else {' . $jsJquery . '}';
  196. }
  197. $res .= '<span onclick="' . $js . '" style="cursor: pointer; font-weight: bold">Debug</span>';
  198. if ($options['showFrom']) {
  199. $calledFrom = debug_backtrace();
  200. $from = '<em>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</em>';
  201. $from .= ' (line <em>' . $calledFrom[0]['line'] . '</em>)';
  202. $res .= '<div>' . $from . '</div>';
  203. }
  204. $pre = ' style="display: none"';
  205. }
  206. if ($options['returns']) {
  207. $var = returns($var);
  208. } else {
  209. $var = print_r($var, true);
  210. }
  211. $res .= '<pre' . $pre . '>' . $var . '</pre>';
  212. $res .= '</div>';
  213. return $res;
  214. }
  215. /**
  216. * Checks if the string [$haystack] contains [$needle]
  217. *
  218. * @param string $haystack Input string.
  219. * @param string $needle Needed char or string.
  220. * @return bool
  221. */
  222. function contains($haystack, $needle, $caseSensitive = false) {
  223. $result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);
  224. return ($result !== false);
  225. }
  226. /**
  227. * Checks if the string [$haystack] starts with [$needle]
  228. *
  229. * @param string $haystack Input string.
  230. * @param string $needle Needed char or string.
  231. * @return bool
  232. */
  233. function startsWith($haystack, $needle, $caseSensitive = false) {
  234. if ($caseSensitive) {
  235. return (mb_strpos($haystack, $needle) === 0);
  236. }
  237. return (mb_stripos($haystack, $needle) === 0);
  238. }
  239. /**
  240. * Checks if the String [$haystack] ends with [$needle]
  241. *
  242. * @param string $haystack Input string.
  243. * @param string $needle Needed char or string
  244. * @return bool
  245. */
  246. function endsWith($haystack, $needle, $caseSensitive = false) {
  247. if ($caseSensitive) {
  248. return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  249. }
  250. return mb_strripos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
  251. }