functions.php 5.7 KB

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