PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/_utilities.php

https://bitbucket.org/snowqbe/playhp
PHP | 177 lines | 112 code | 15 blank | 50 comment | 7 complexity | fa68870697a926e0c405c686ffac77fa MD5 | raw file
  1. <?php
  2. /**
  3. * Global, utility functions, PHP additions
  4. */
  5. if (!defined('P_MODE')) {
  6. define('P_MODE', 'prod');
  7. }
  8. define('P_DEV', '0');
  9. define('P_USER', '1');
  10. // Root path
  11. $currentDir = str_replace('\\', '/', __DIR__);
  12. $currentDir = substr($currentDir, 0, strpos($currentDir, '/src'));
  13. // Default paths
  14. define('P_ROOT', $currentDir . '/');
  15. define('P_SRC_PATH', P_ROOT . 'src/main/');
  16. define('P_TEST_PATH', P_ROOT . 'src/test/');
  17. define('P_LOCALE_PATH', P_ROOT . 'locale/');
  18. define('P_VIEWS_PATH', P_ROOT . 'views/');
  19. define('P_LAYOUTS_PATH', P_ROOT . 'views/layouts/');
  20. // Cancel magic quotes: it sucks
  21. if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
  22. unslashArray($_GET);
  23. unslashArray($_POST);
  24. unslashArray($_COOKIE);
  25. }
  26. /**
  27. * Lists the subclasses of a given class
  28. * @param string $parent Parent class name
  29. * @return array List of subclasses
  30. */
  31. function getSubclassesOf($parent)
  32. {
  33. $result = array();
  34. foreach (get_declared_classes() as $class) {
  35. if (is_subclass_of($class, $parent))
  36. $result[] = $class;
  37. }
  38. return $result;
  39. }
  40. /**
  41. * Validates an email
  42. * @param string $email Email to validate
  43. * @return bool Status of email validation
  44. */
  45. function isEmailValid($email)
  46. {
  47. $atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';
  48. $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
  49. $regex = '/^' . $atom . '+(\.' . $atom . '+)*@(' . $domain . '{1,63}\.)+{2,63}$/i';
  50. return preg_match($regex, $email) == 1;
  51. }
  52. /**
  53. * Debugging function
  54. * @param $msg
  55. * @param bool $bufferingActive
  56. */
  57. function trace($msg, $bufferingActive = false)
  58. {
  59. \PlayHP\PlayHP::trace($msg, $bufferingActive);
  60. }
  61. /**
  62. * Recursive glob call.
  63. * Does not support flag GLOB_BRACE
  64. * @link http://php.net/manual/fr/function.glob.php
  65. */
  66. function globRecursive($pattern, $flags = 0)
  67. {
  68. $files = glob($pattern, $flags);
  69. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  70. $files = array_merge($files, globRecursive($dir . '/' . basename($pattern), $flags));
  71. }
  72. return $files;
  73. }
  74. /**
  75. * Gettext shortcut for PlayHP strings
  76. * @param string $text Text to grab
  77. * @return string Localized text
  78. */
  79. function _p($text)
  80. {
  81. return dgettext(\PlayHP\PlayHP::GETTEXT_DOMAIN, $text);
  82. }
  83. /**
  84. * Returns a possible name based on the given filename and path.
  85. * If filename already exists in path, the function proposes a new filename with a counter.
  86. * Result can be retrieved with path in it or not.
  87. * @param string $filename The file name to test
  88. * @param string $path The path where the file should be
  89. * @param bool|string $get_as_path Flag telling to return the path with the new filename
  90. * @return string
  91. */
  92. function getPossibleName($filename, $path, $get_as_path = true)
  93. {
  94. if (substr($path, -1) == '/') {
  95. $path = substr($path, 0, strlen($path) - 1);
  96. }
  97. if (!file_exists("$path/$filename")) {
  98. return $get_as_path ? "$path/$filename" : $filename;
  99. }
  100. $i = 1;
  101. while (file_exists("$path/$filename")) {
  102. $i++;
  103. $res = pathinfo("$path/$filename");
  104. $filename = substr($res['basename'], 0, strrpos($res['basename'], '.')) . $i . '.' . $res['extension'];
  105. }
  106. return $get_as_path ? "$path/$filename" : $filename;
  107. }
  108. /**
  109. * Handles unslashing magic quoted elements
  110. */
  111. function unslashArray(&$array)
  112. {
  113. foreach ($array as $key => $val) {
  114. if (is_array($val))
  115. unslashArray($array[$key]);
  116. else
  117. $array[stripslashes($key)] = stripslashes($val);
  118. }
  119. }
  120. /**
  121. * @param string $key Key of the parameter to fetch
  122. * @param null $default Default value to use otherwise
  123. * @return mixed|null The parameter's value
  124. */
  125. function mapParam($key, $default = null)
  126. {
  127. return isset($_GET[$key]) ? $_GET[$key] : (isset($_POST[$key]) ? $_POST[$key] : $default);
  128. }
  129. /**
  130. * Implements default entities behavior as of PHP 5.4
  131. * @param string $toencode String to encode
  132. * @return string Encoded string (in UTF-8)
  133. */
  134. function entities($toencode)
  135. {
  136. return htmlentities($toencode, null, 'UTF-8');
  137. }
  138. /**
  139. * Returns a formatted messages based on an exception
  140. * @param Exception $e
  141. * @return string Formatted message
  142. */
  143. function fmt(\Exception $e)
  144. {
  145. return '<pre>' .
  146. $e->getMessage() . "\nIn" .
  147. $e->getFile() . ' at line ' . $e->getLine() . "\n" . $e->getTraceAsString() . '</pre>';
  148. }
  149. /**
  150. * Redirects response to a new URL using `header Location: $url`
  151. * @param string $url URL to redirect to
  152. */
  153. function redirect($url)
  154. {
  155. $redirect = 'Location: ' . filter_var($url, FILTER_SANITIZE_URL);
  156. header($redirect);
  157. exit;
  158. }