function.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. function secondToTime($seconds) {
  3. $t = round($seconds);
  4. return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
  5. }
  6. function app_autoloader($class_name) {
  7. if(file_exists(__DIR__.'/class/'.$class_name.'.class.php'))
  8. require_once(__DIR__.'/class/'.$class_name.'.class.php');
  9. if(file_exists(__DIR__.'/type/'.$class_name.'.class.php'))
  10. require_once(__DIR__.'/type/'.$class_name.'.class.php');
  11. }
  12. function errorToException( $errno, $errstr, $errfile, $errline, $errcontext)
  13. {
  14. if(strpos($errstr,'disk_')!==false) return;
  15. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  16. }
  17. function slugify($text)
  18. {
  19. // replace non letter or digits by -
  20. $text = preg_replace('~[^\\pL\.\d]+~u', '-', $text);
  21. // trim
  22. $text = trim($text, '-');
  23. // transliterate
  24. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  25. // lowercase
  26. $text = strtolower($text);
  27. // remove unwanted characters
  28. $text = preg_replace('~[^-\.\w]+~', '', $text);
  29. if (empty($text))
  30. return 'n-a';
  31. return $text;
  32. }
  33. function secure_user_vars($var){
  34. if(is_array($var)){
  35. $array = array();
  36. foreach($var as $key=>$value):
  37. $array[secure_user_vars($key)] = secure_user_vars($value);
  38. endforeach;
  39. return $array;
  40. }else{
  41. return htmlspecialchars($var, ENT_NOQUOTES, "UTF-8");
  42. }
  43. }
  44. function base64_to_image($base64_string, $output_file) {
  45. $ifp = fopen($output_file, "wb");
  46. $data = explode(',', $base64_string);
  47. fwrite($ifp, base64_decode($data[1]));
  48. fclose($ifp);
  49. return $output_file;
  50. }
  51. function getExt($file){
  52. $ext = explode('.',$file);
  53. return strtolower(array_pop($ext));
  54. }
  55. function getExtIcon($ext){
  56. $icon = '';
  57. switch($ext){
  58. case '7z':
  59. case 'rar':
  60. case 'gz':
  61. case 'zip':
  62. $icon = 'fa-file-archive-o';
  63. break;
  64. case 'php':
  65. case 'js':
  66. case 'py':
  67. case 'c':
  68. case 'cpp':
  69. case 'css':
  70. case 'h':
  71. case 'hpp':
  72. case 'html':
  73. case 'htm':
  74. case 'asp':
  75. case 'jsp':
  76. $icon = 'fa-file-code-o';
  77. break;
  78. case 'xls':
  79. case 'xlsx':
  80. case 'csv':
  81. $icon = 'fa-file-excel-o';
  82. break;
  83. case 'bmp':
  84. case 'jpg':
  85. case 'jpeg':
  86. case 'ico':
  87. case 'gif':
  88. case 'png':
  89. case 'svg':
  90. $icon = 'fa-file-image-o';
  91. break;
  92. case 'pdf':
  93. $icon = 'fa-file-pdf-o';
  94. break;
  95. case 'ppt':
  96. case 'pptx':
  97. $icon = 'fa-file-powerpoint-o';
  98. break;
  99. case 'txt':
  100. case 'htaccess':
  101. case 'md':
  102. $icon = 'fa-file-text-o';
  103. break;
  104. case 'doc':
  105. case 'docx':
  106. case 'word':
  107. $icon = 'fa-file-word-o';
  108. break;
  109. case 'avi':
  110. case 'wmv':
  111. case 'mov':
  112. case 'divx':
  113. case 'xvid':
  114. case 'mkv':
  115. case 'flv':
  116. case 'mpeg':
  117. case 'h264':
  118. case 'rmvb':
  119. case 'mp4':
  120. $icon = 'fa-file-movie-o';
  121. break;
  122. case 'wav':
  123. case 'ogg':
  124. case 'ogv':
  125. case 'ogx':
  126. case 'oga':
  127. case 'riff':
  128. case 'bwf':
  129. case 'wma':
  130. case 'flac':
  131. case 'aac':
  132. case 'mp3':
  133. $icon = 'fa-file-audio-o';
  134. break;
  135. default:
  136. $icon = 'fa-file-o';
  137. break;
  138. }
  139. return $icon;
  140. };
  141. function max_upload_size($limits = array()){
  142. $limits[]= str_replace('M','',ini_get('post_max_size')) *1048576;
  143. $limits[]= str_replace('M','',ini_get('upload_max_filesize')) *1048576;
  144. return readable_size(min($limits));
  145. }
  146. function readable_size($bytes)
  147. {
  148. if($bytes<1024){
  149. return round(($bytes / 1024), 2).' o';
  150. }elseif(1024<$bytes && $bytes<1048576){
  151. return round(($bytes / 1024), 2).' ko';
  152. }elseif(1048576<$bytes && $bytes<1073741824){
  153. return round(($bytes / 1024)/1024, 2).' Mo';
  154. }elseif(1073741824<$bytes){
  155. return round(($bytes / 1024)/1024/1024, 2).' Go';
  156. }
  157. }
  158. function imageResize($image,$w,$h){
  159. $resource = imagecreatefromstring(file_get_contents($image));
  160. $size = getimagesize($image);
  161. $h = (($size[1] * (($w)/$size[0])));
  162. $thumbnail = imagecreatetruecolor($w , $h);
  163. imagecopyresampled($thumbnail ,$resource, 0,0, 0,0, $w, $h, $size[0],$size[1]);
  164. imagedestroy($resource);
  165. imagejpeg($thumbnail , $image, 100);
  166. }
  167. ?>