functions.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // CODE LOCALE (locale -a)
  3. $langueEtLocalDispo=array( 'fr' => 'fr_FR',
  4. 'en' => 'en_US',
  5. 'oc' => 'oc_FR',
  6. );
  7. function convertHumain2octect($value) {
  8. if (preg_match('/[0-9]+[Kk]$/', $value)) {
  9. return intval($value) * 1024;
  10. } elseif (preg_match('/[0-9]+[Mm]$/', $value)) {
  11. return intval($value) * 1024 * 1024;
  12. } elseif (preg_match('/[0-9]+[Gg]$/', $value)) {
  13. return intval($value) * 1024 * 1024 * 1024;
  14. } else {
  15. return intval($value);
  16. }
  17. }
  18. function convertOctect2humain($value) {
  19. if ($value > 1000000000) {
  20. $return=round($value/1024/1024/1024, 1).'Go';
  21. }elseif ($value > 1000000) {
  22. $return=round($value/1024/1024, 1).'Mo';
  23. }elseif ($value > 1000) {
  24. $return=round($value/1024, 1).'Ko';
  25. } else {
  26. $return=$value;
  27. }
  28. return $return;
  29. }
  30. function checkMimeTypes($mimeTypesTest) {
  31. global $config;
  32. $mimeDetect=false;
  33. foreach ($config['mimeTypes'] as $mimeTypes) {
  34. if (preg_match('/'.$mimeTypes.'/', $mimeTypesTest)) {
  35. $mimeDetect = true;
  36. }
  37. }
  38. if (($config['mimeTypesConduct'] == 'allow' && $mimeDetect)
  39. || ($config['mimeTypesConduct'] == 'deny' && !$mimeDetect)) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. return $return;
  45. }
  46. function genZip($id) {
  47. global $config;
  48. $uploadDirId=$config['uploadDir'].'/'.$id;
  49. $zipFile = $uploadDirId.'/'.$id.'.zip';
  50. if (!is_file($zipFile)) {
  51. $zip = new ZipArchive();
  52. if ($zip->open($zipFile, ZipArchive::CREATE)!==TRUE) {
  53. exit('Error in open <$zipFile>\n');
  54. }
  55. foreach (scandir($uploadDirId) as $file) {
  56. if (is_file($uploadDirId.'/'.$file)
  57. && !preg_match('/^\.(.+)\.cfg$/', $file)
  58. && !preg_match('/^\.(.+)\.small$/', $file)) {
  59. $zip->addFile($uploadDirId.'/'.$file,$file);
  60. }
  61. }
  62. $zip->close();
  63. }
  64. }
  65. function rrmdir($dir) {
  66. if (is_dir($dir)) {
  67. $objects = scandir($dir);
  68. foreach ($objects as $object) {
  69. if ($object != "." && $object != "..") {
  70. if (is_dir($dir."/".$object) && !is_link($dir."/".$object))
  71. rrmdir($dir."/".$object);
  72. else
  73. unlink($dir."/".$object);
  74. }
  75. }
  76. rmdir($dir);
  77. }
  78. }
  79. function cronExpire() {
  80. global $config;
  81. foreach (scandir($config['uploadDir']) as $uploadDirId) {
  82. if (!preg_match('/^\./', $uploadDirId) && is_dir($config['uploadDir'].'/'.$uploadDirId)) {
  83. $uploadDirIdExplode = explode("-", $uploadDirId);
  84. if ($uploadDirIdExplode[0] < time()) {
  85. if ($config['expireCron'] == 'cli') {
  86. echo $config['uploadDir'].'/'.$uploadDirId." "._('Expired')."\n";
  87. }
  88. rrmdir($config['uploadDir'].'/'.$uploadDirId);
  89. }
  90. }
  91. }
  92. }
  93. // https://www.binarytides.com/php-resize-large-images-imagemagick/
  94. function resize_image($src , $dest , $toWidth , $toHeight ) {
  95. if(!file_exists($src)) {
  96. echo '$src file does not exist';
  97. return false;
  98. } else {
  99. //OPEN THE IMAGE INTO A RESOURCE
  100. $img = imagecreatefromjpeg ($src); //try jpg
  101. if(!$img) {
  102. $img = imagecreatefromgif ($src); //try gif
  103. }
  104. if(!$img) {
  105. $img = imagecreatefrompng ($src); //try png
  106. }
  107. if(!$img) {
  108. die('Could Not create image resource $src');
  109. }
  110. //ORIGINAL DIMENTIONS
  111. list( $width , $height ) = getimagesize( $src );
  112. //ORIGINAL SCALE
  113. $xscale=$width/$toWidth;
  114. $yscale=$height/$toHeight;
  115. //NEW DIMENSIONS WITH SAME SCALE
  116. if ($yscale > $xscale) {
  117. $new_width = round($width * (1/$yscale));
  118. $new_height = round($height * (1/$yscale));
  119. } else {
  120. $new_width = round($width * (1/$xscale));
  121. $new_height = round($height * (1/$xscale));
  122. }
  123. //NEW IMAGE RESOURCE
  124. if(!($imageResized = imagecreatetruecolor($new_width, $new_height))) {
  125. die('Could not create new image resource of width : $new_width , height : $new_height');
  126. }
  127. //RESIZE IMAGE
  128. if(! imagecopyresampled($imageResized, $img , 0 , 0 , 0 , 0 , $new_width , $new_height , $width , $height)) {
  129. die('Resampling failed');
  130. }
  131. //STORE IMAGE INTO DESTINATION
  132. if(! imagejpeg($imageResized , $dest)) {
  133. die('Could not save new file');
  134. }
  135. //Free the memory
  136. imagedestroy($img);
  137. imagedestroy($imageResized);
  138. return true;
  139. }
  140. }
  141. function truncate($string, $max_length = 30, $replacement = '', $trunc_at_space = false) {
  142. $max_length -= strlen($replacement);
  143. $string_length = strlen($string);
  144. if($string_length <= $max_length)
  145. return $string;
  146. if( $trunc_at_space && ($space_position = strrpos($string, ' ', $max_length-$string_length)) )
  147. $max_length = $space_position;
  148. return substr_replace($string, $replacement, $max_length);
  149. }
  150. function lang2locale($langue) {
  151. global $langueEtLocalDispo;
  152. if ($langueEtLocalDispo[$langue] != '') {
  153. return $langueEtLocalDispo[$langue];
  154. } else {
  155. // par défaut
  156. return 'en_US';
  157. }
  158. }
  159. function locale2lang($localeRecherche) {
  160. global $langueEtLocalDispo;
  161. foreach($langueEtLocalDispo as $code=>$locale) {
  162. if ($locale == $localeRecherche) {
  163. return $code;
  164. break;
  165. }
  166. }
  167. // par défaut
  168. return 'en';
  169. }
  170. ?>