functions.php 5.0 KB

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