2
0

file.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. class file
  3. {
  4. public static function record(array $_temp, string $_folderFiles = DIR_DATAS_FILES)
  5. {
  6. $md5 = md5_file($_temp["tmp_name"]);
  7. if(self::findM5($md5) == FALSE){
  8. if (copy($_temp["tmp_name"], $_folderFiles . $md5)) {
  9. db::query("INSERT INTO " . DB_T_FILES . " (id, name, size, id_user) VALUES (:id, :name, :size, :id_user)");
  10. db::bind(':id', $md5);
  11. db::bind(':name', $_temp['name']);
  12. db::bind(':size', $_temp['size']);
  13. db::bind(':id_user', session::getId());
  14. try {
  15. db::execute();
  16. return $md5;
  17. } catch (Exception $ex) {
  18. unlink($_folderFiles . $md5);
  19. alert::recError("Erreur #record sur l'import du fichier " . $_temp['name']);
  20. return FALSE;
  21. }
  22. }
  23. } else {
  24. alert::recError("Erreur #record ce fichier a déjà été importé : " . $_temp['name']);
  25. return FALSE;
  26. }
  27. }
  28. public static function findM5(string $_md5)
  29. {
  30. db::query("SELECT "
  31. . "IF(" . DB_T_FILES . ".name IS NOT NULL, TRUE, FALSE) AS exist "
  32. . "FROM " . DB_T_FILES . " "
  33. . "WHERE " . DB_T_FILES . ".id = :md5");
  34. db::bind(':md5', $_md5);
  35. $return = db::single();
  36. return ($return == TRUE) ? TRUE : FALSE;
  37. }
  38. public static function delete(string $_id = NULL, string $_folderFiles = DIR_DATAS_FILES)
  39. {
  40. if (isset($_id) and $_id != NULL and file_exists($_folderFiles . $_id)) {
  41. if (unlink($_folderFiles . $_id)) {
  42. db::query("DELETE FROM " . DB_T_FILES . " WHERE id = :id");
  43. db::bind(':id', $_id);
  44. try {
  45. db::execute();
  46. return TRUE;
  47. } catch (Exception $ex) {
  48. alert::recError("Erreur lors de la désindexation du fichier");
  49. return FALSE;
  50. }
  51. } else {
  52. alert::recError("Erreur sur la suppression du fichier");
  53. return FALSE;
  54. }
  55. } else {
  56. return FALSE;
  57. }
  58. }
  59. public static function download(string $_id, string $_folderFiles = DIR_DATAS_FILES)
  60. {
  61. if (file_exists($_folderFiles . $_id)) {
  62. return $_folderFiles . $_id;
  63. } else {
  64. return FALSE;
  65. }
  66. }
  67. public static function cleanFilesByOrder(string $_path, int $_nbFiles = 5)
  68. {
  69. $return = TRUE;
  70. $cpt = 0;
  71. $files = array();
  72. $dir = new DirectoryIterator($_path);
  73. $blackList = array(
  74. ".",
  75. "..",
  76. "index.html",
  77. "index.php"
  78. );
  79. foreach ($dir as $fileinfo) {
  80. $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
  81. }
  82. krsort($files);
  83. foreach ($files as $file) {
  84. if (!in_array($file, $blackList)) {
  85. if ($cpt++ >= $_nbFiles) {
  86. $return = (unlink($_path . $file)) ? TRUE : FALSE;
  87. }
  88. }
  89. }
  90. return $return;
  91. }
  92. public static function cleanAllFiles(string $_path)
  93. {
  94. $return = TRUE;
  95. $dir = new DirectoryIterator($_path);
  96. $blackList = array(
  97. ".",
  98. "..",
  99. "index.html",
  100. "index.php"
  101. );
  102. foreach ($dir as $fileinfo) {
  103. if (!in_array($fileinfo->getFilename(), $blackList)) {
  104. $return = (unlink($_path . $fileinfo->getFilename())) ? TRUE : FALSE;
  105. }
  106. }
  107. return $return;
  108. }
  109. public static function cleanFilesByTime(string $_path, int $_limitTime = 24 * 3600) // 24*3600 pour une journée
  110. {
  111. if ($handle = opendir($_path)) {
  112. while (false !== ($file = readdir($handle))) {
  113. $filelastmodified = filemtime($_path . $file);
  114. if ((time() - $filelastmodified) > $_limitTime) {
  115. unlink($_path . $file);
  116. }
  117. }
  118. closedir($handle);
  119. }
  120. }
  121. public static function copyFolder(string $_folder, string $_target)
  122. {
  123. if ($dir = opendir($_folder)) {
  124. mkdir($_target);
  125. while (($file = readdir($dir))) {
  126. if (($file != '.') && ($file != '..')) {
  127. if (is_dir($_folder . '/' . $file)) {
  128. self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
  129. } else {
  130. copy($_folder . '/' . $file, $_target . '/' . $file);
  131. }
  132. }
  133. }
  134. closedir($dir);
  135. }
  136. }
  137. public static function deleteFolder(string $_dir)
  138. {
  139. if (is_dir($_dir)) {
  140. $command = "rm -r " . $_dir;
  141. try {
  142. system($command);
  143. return TRUE;
  144. } catch (Exception $ex) {
  145. return FALSE;
  146. }
  147. }
  148. }
  149. public static function zip(string $_dir, string $_name)
  150. {
  151. $zip = new ZipArchive();
  152. $zip_name = $_name . ".zip";
  153. $zip->open($zip_name, ZipArchive::CREATE);
  154. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
  155. foreach ($files as $file) {
  156. if (!$file->isDir()) {
  157. $filePath = $file->getRealPath();
  158. $relativePath = substr($filePath, strlen($_dir));
  159. $zip->addFile($filePath, $relativePath);
  160. }
  161. }
  162. $zip->close();
  163. return $zip_name;
  164. }
  165. public static function unzip(string $_zip, string $_target)
  166. {
  167. if (is_file($_zip)) {
  168. $zipInfo = pathinfo($_zip);
  169. if ($zipInfo["extension"] == "zip") {
  170. $command = "unzip -q " . $_zip . " -d " . $_target . $zipInfo["filename"];
  171. try {
  172. system($command);
  173. return $zipInfo["filename"];
  174. } catch (Exception $ex) {
  175. return FALSE;
  176. }
  177. }
  178. }
  179. return FALSE;
  180. }
  181. public static function sizeFolder(string $_rep)
  182. {
  183. $Racine = opendir($_rep);
  184. $Taille = 0;
  185. while ($Dossier = readdir($Racine)) {
  186. if ($Dossier != '..' and $Dossier != '.') {
  187. //Ajoute la taille du sous dossier
  188. if (is_dir($_rep . '/' . $Dossier)) $Taille += self::sizeFolder($_rep . '/' .
  189. $Dossier);
  190. //Ajoute la taille du fichier
  191. else $Taille += filesize($_rep . '/' . $Dossier);
  192. }
  193. }
  194. closedir($Racine);
  195. return $Taille;
  196. }
  197. }