bootstrap.php 7.2 KB

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