helpers_default.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. print_r($var);
  10. echo "</pre>";
  11. if ($exit) {
  12. exit;
  13. }
  14. }
  15. }
  16. if (! function_exists('ifEmpty')) {
  17. function ifEmpty($val, $default_value='-1') {
  18. return empty($val) ? $default_value : $val;
  19. }
  20. }
  21. if (! function_exists('errorHttp')) {
  22. function errorHttp($error_code, $message='An error has occured', $title='Error') {
  23. header("HTTP/1.0 " . $error_code . " " . $title);
  24. echo '<h1>' . $title . '</h1>';
  25. echo '<p>' . $message . '</p>';
  26. exit;
  27. }
  28. }
  29. if (! function_exists('redirect')) {
  30. function redirect($url, $http_code=302) {
  31. header('Location: ' . $url, true, $http_code);
  32. exit;
  33. }
  34. }
  35. if (! function_exists('get')) {
  36. function get($key, $default_value=null) {
  37. return isset($_GET[$key]) ? $_GET[$key] : $default_value;
  38. }
  39. }
  40. if (! function_exists('post')) {
  41. function post($key, $default_value=null) {
  42. return isset($_POST[$key]) ? $_POST[$key] : $default_value;
  43. }
  44. }
  45. if (! function_exists('session')) {
  46. function session($key, $default_value=null) {
  47. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default_value;
  48. }
  49. }
  50. if (! function_exists('cookie')) {
  51. function cookie($key, $default_value=null) {
  52. return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default_value;
  53. }
  54. }
  55. if (! function_exists('slugify')) {
  56. function slugify($text, $max_length=null) {
  57. // https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
  58. // replace non letter or digits by -
  59. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  60. // transliterate
  61. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  62. // remove unwanted characters
  63. $text = preg_replace('~[^-\w]+~', '', $text);
  64. // trim
  65. $text = trim($text, '-');
  66. // remove duplicate -
  67. $text = preg_replace('~-+~', '-', $text);
  68. // lowercase
  69. $text = strtolower($text);
  70. if (empty($text)) {
  71. return 'n-a';
  72. }
  73. if (! empty($max_length) && strlen($text) > $max_length) {
  74. $text = substr(0, $max_length);
  75. }
  76. return $text;
  77. }
  78. }
  79. if (! function_exists('geneateGuid')) {
  80. function geneateGuid() {
  81. if (function_exists('com_create_guid')) {
  82. return trim(com_create_guid(), '{}');
  83. }
  84. if (function_exists('openssl_random_pseudo_bytes') === true) {
  85. $data = openssl_random_pseudo_bytes(16);
  86. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  87. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  88. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  89. }
  90. mt_srand((double)microtime()*10000);
  91. $charid = strtoupper(md5(uniqid(rand(), true)));
  92. $uuid = sprintf(
  93. "%s-%s-%s-%s-%s",
  94. substr($charid, 0, 8),
  95. substr($charid, 8, 4),
  96. substr($charid,12, 4),
  97. substr($charid,16, 4),
  98. substr($charid,20,12)
  99. );
  100. return strtolower($uuid);
  101. }
  102. }
  103. if (! function_exists('generatePassword')) {
  104. function generatePassword($nb_chars = 8) {
  105. $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
  106. $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
  107. $ref = str_shuffle($ref);
  108. return substr($ref, 0, $nb_chars);
  109. }
  110. }
  111. if (! function_exists('getRouteUrl')) {
  112. function getRouteUrl($route_name, $urls_args=[]) {
  113. return Router::getRouteUrl($route_name, $urls_args);
  114. }
  115. }