helpers_default.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. use \KarmaFW\Routing\Router;
  3. if (! function_exists('pre')) {
  4. function pre($var, $exit = false, $prefix = '') {
  5. echo "<pre>";
  6. if (!empty($prefix)) {
  7. echo $prefix;
  8. }
  9. if (is_null($var)) {
  10. echo "NULL";
  11. } else if ($var === true) {
  12. echo "TRUE";
  13. } else if ($var === false) {
  14. echo "FALSE";
  15. } else if (is_string($var)) {
  16. echo '"' . $var . '"';
  17. } else {
  18. print_r($var);
  19. }
  20. echo "</pre>";
  21. if ($exit) {
  22. exit;
  23. }
  24. }
  25. }
  26. if (! function_exists('ifEmpty')) {
  27. function ifEmpty($val, $default_value='-1') {
  28. return empty($val) ? $default_value : $val;
  29. }
  30. }
  31. if (! function_exists('errorHttp')) {
  32. function errorHttp($error_code, $message='An error has occured', $title='Error') {
  33. header("HTTP/1.0 " . $error_code . " " . $title);
  34. echo '<h1>' . $title . '</h1>';
  35. echo '<p>' . $message . '</p>';
  36. exit;
  37. }
  38. }
  39. if (! function_exists('redirect')) {
  40. function redirect($url, $http_code=302) {
  41. if ($http_code == 'link' || $http_code == 'debug') {
  42. echo 'continue to <a href="' . $url . '">' . $url . '</a>';
  43. exit;
  44. }
  45. header('Location: ' . $url, true, $http_code);
  46. exit;
  47. }
  48. }
  49. if (! function_exists('get')) {
  50. function get($key, $default_value=null) {
  51. return isset($_GET[$key]) ? $_GET[$key] : $default_value;
  52. }
  53. }
  54. if (! function_exists('post')) {
  55. function post($key, $default_value=null) {
  56. return isset($_POST[$key]) ? $_POST[$key] : $default_value;
  57. }
  58. }
  59. if (! function_exists('session')) {
  60. function session($key, $default_value=null) {
  61. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default_value;
  62. }
  63. }
  64. if (! function_exists('cookie')) {
  65. function cookie($key, $default_value=null) {
  66. return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default_value;
  67. }
  68. }
  69. if (! function_exists('slugify')) {
  70. function slugify($text, $max_length=null) {
  71. // https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
  72. // https://stackoverflow.com/questions/3371697/replacing-accented-characters-php
  73. $unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  74. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  75. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  76. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  77. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  78. $text = strtr( $text, $unwanted_array );
  79. // replace non letter or digits by -
  80. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  81. // transliterate
  82. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  83. // remove unwanted characters
  84. $text = preg_replace('~[^-\w]+~', '', $text);
  85. // trim
  86. $text = trim($text, '-');
  87. // remove duplicate -
  88. $text = preg_replace('~-+~', '-', $text);
  89. // lowercase
  90. $text = strtolower($text);
  91. if (empty($text)) {
  92. return 'n-a';
  93. }
  94. if (! empty($max_length) && strlen($text) > $max_length) {
  95. $text = substr(0, $max_length);
  96. }
  97. return $text;
  98. }
  99. }
  100. if (! function_exists('generate_uid')) {
  101. function generate_uid() {
  102. if (function_exists('com_create_guid')) {
  103. return trim(com_create_guid(), '{}');
  104. }
  105. if (function_exists('openssl_random_pseudo_bytes') === true) {
  106. $data = openssl_random_pseudo_bytes(16);
  107. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  108. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  109. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  110. }
  111. mt_srand((double)microtime()*10000);
  112. $charid = strtoupper(md5(uniqid(rand(), true)));
  113. $uuid = sprintf(
  114. "%s-%s-%s-%s-%s",
  115. substr($charid, 0, 8),
  116. substr($charid, 8, 4),
  117. substr($charid,12, 4),
  118. substr($charid,16, 4),
  119. substr($charid,20,12)
  120. );
  121. return strtolower($uuid);
  122. }
  123. }
  124. if (! function_exists('generate_password')) {
  125. function generate_password($nb_chars = 8) {
  126. $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
  127. $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
  128. $ref = str_shuffle($ref);
  129. return substr($ref, 0, $nb_chars);
  130. }
  131. }
  132. if (! function_exists('getRouteUrl')) {
  133. function getRouteUrl($route_name, $urls_args=[]) {
  134. return Router::getRouteUrl($route_name, $urls_args);
  135. }
  136. }
  137. if (! function_exists('date_us_to_fr')) {
  138. function date_us_to_fr($date_us) {
  139. $date_us = substr($date_us, 0, 10);
  140. $parts = explode('-', $date_us);
  141. return implode('/', array_reverse($parts));
  142. }
  143. }
  144. if (! function_exists('truncate_str')) {
  145. function truncate_str($str, $max_length) {
  146. if (strlen($str) > $max_length) {
  147. $str = substr($str, 0, $max_length-1) . '…';
  148. }
  149. return $str;
  150. }
  151. }
  152. if (! function_exists('get_url_path')) {
  153. function get_url_path($url, $with_querystring=true, $with_url_hash=false) {
  154. $url_parts = parse_url($url);
  155. $url = $url_parts['path'];
  156. if ($with_querystring && ! empty($url_parts['query'])) {
  157. $url .= '?' . $url_parts['query'];
  158. }
  159. if ($with_url_hash && ! empty($url_parts['fragment'])) {
  160. $url .= '#' . $url_parts['fragment'];
  161. }
  162. return $url;
  163. }
  164. }