file.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 getErrorUpload(array $_file){
  29. $return = [];
  30. if ($_file['error'] === UPLOAD_ERR_INI_SIZE) {
  31. $return = [
  32. "status" => "error",
  33. "description" => "Le fichier téléchargé dépasse la taille maximale autorisée fixé à " . ini_get('upload_max_filesize')
  34. ];
  35. } elseif ($_file['error'] !== UPLOAD_ERR_OK) {
  36. $phpFileUploadErrors = array(
  37. 1 => 'Le fichier téléchargé dépasse la directive upload_max_filesize dans php.ini',
  38. 2 => 'Le fichier téléchargé dépasse la directive MAX_FILE_SIZE spécifiée dans le formulaire HTML',
  39. 3 => 'Le fichier n\'a été que partiellement téléchargé',
  40. 4 => 'Aucun fichier n\'a été téléchargé',
  41. 6 => 'Il manque un dossier temporaire',
  42. 7 => 'Échec de l\'écriture du fichier sur le disque',
  43. 8 => 'Une extension PHP a arrêté le téléchargement du fichier',
  44. );
  45. $return = [
  46. "status" => "error",
  47. "description" => $phpFileUploadErrors[$_file['error']]
  48. ];
  49. } else {
  50. $return = [
  51. "status" => "success"
  52. ];
  53. }
  54. return $return;
  55. }
  56. public static function findM5(string $_md5)
  57. {
  58. db::query("SELECT "
  59. . "IF(" . DB_T_FILES . ".name IS NOT NULL, TRUE, FALSE) AS exist "
  60. . "FROM " . DB_T_FILES . " "
  61. . "WHERE " . DB_T_FILES . ".id = :md5");
  62. db::bind(':md5', $_md5);
  63. $return = db::single();
  64. return ($return == TRUE) ? TRUE : FALSE;
  65. }
  66. public static function delete(string $_id = NULL, string $_folderFiles = DIR_DATAS_FILES)
  67. {
  68. if (isset($_id) and $_id != NULL and file_exists($_folderFiles . $_id)) {
  69. if (unlink($_folderFiles . $_id)) {
  70. db::query("DELETE FROM " . DB_T_FILES . " WHERE id = :id");
  71. db::bind(':id', $_id);
  72. try {
  73. db::execute();
  74. return TRUE;
  75. } catch (Exception $ex) {
  76. alert::recError("Erreur lors de la désindexation du fichier");
  77. return FALSE;
  78. }
  79. } else {
  80. alert::recError("Erreur sur la suppression du fichier");
  81. return FALSE;
  82. }
  83. } else {
  84. return FALSE;
  85. }
  86. }
  87. public static function download(string $_id, string $_folderFiles = DIR_DATAS_FILES)
  88. {
  89. if (file_exists($_folderFiles . $_id)) {
  90. return $_folderFiles . $_id;
  91. } else {
  92. return FALSE;
  93. }
  94. }
  95. public static function cleanFilesByOrder(string $_path, int $_nbFiles = 5)
  96. {
  97. $return = TRUE;
  98. $cpt = 0;
  99. $files = array();
  100. $dir = new DirectoryIterator($_path);
  101. $blackList = array(
  102. ".",
  103. "..",
  104. "index.html",
  105. "index.php"
  106. );
  107. foreach ($dir as $fileinfo) {
  108. $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
  109. }
  110. krsort($files);
  111. foreach ($files as $file) {
  112. if (!in_array($file, $blackList)) {
  113. if ($cpt++ >= $_nbFiles) {
  114. $return = (unlink($_path . $file)) ? TRUE : FALSE;
  115. }
  116. }
  117. }
  118. return $return;
  119. }
  120. public static function cleanAllFiles(string $_path)
  121. {
  122. $return = TRUE;
  123. $dir = new DirectoryIterator($_path);
  124. $blackList = array(
  125. ".",
  126. "..",
  127. "index.html",
  128. "index.php"
  129. );
  130. foreach ($dir as $fileinfo) {
  131. if (!in_array($fileinfo->getFilename(), $blackList)) {
  132. $return = (unlink($_path . $fileinfo->getFilename())) ? TRUE : FALSE;
  133. }
  134. }
  135. return $return;
  136. }
  137. public static function cleanFilesByTime(string $_path, int $_limitTime = 24 * 3600) // 24*3600 pour une journée
  138. {
  139. if ($handle = opendir($_path)) {
  140. while (false !== ($file = readdir($handle))) {
  141. $filelastmodified = filemtime($_path . $file);
  142. if ((time() - $filelastmodified) > $_limitTime) {
  143. unlink($_path . $file);
  144. }
  145. }
  146. closedir($handle);
  147. }
  148. }
  149. public static function copyFolder(string $_folder, string $_target)
  150. {
  151. if ($dir = opendir($_folder)) {
  152. mkdir($_target);
  153. while (($file = readdir($dir))) {
  154. if (($file != '.') && ($file != '..')) {
  155. if (is_dir($_folder . '/' . $file)) {
  156. self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
  157. } else {
  158. copy($_folder . '/' . $file, $_target . '/' . $file);
  159. }
  160. }
  161. }
  162. closedir($dir);
  163. }
  164. }
  165. public static function deleteFolder(string $_dir)
  166. {
  167. if (is_dir($_dir)) {
  168. $command = "rm -r " . $_dir;
  169. try {
  170. system($command);
  171. return TRUE;
  172. } catch (Exception $ex) {
  173. return FALSE;
  174. }
  175. }
  176. }
  177. public static function zip(string $_dir, string $_name)
  178. {
  179. $zip = new ZipArchive();
  180. $zip_name = $_name . ".zip";
  181. $zip->open($zip_name, ZipArchive::CREATE);
  182. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
  183. foreach ($files as $file) {
  184. if (!$file->isDir()) {
  185. $filePath = $file->getRealPath();
  186. $relativePath = substr($filePath, strlen($_dir));
  187. $zip->addFile($filePath, $relativePath);
  188. }
  189. }
  190. $zip->close();
  191. return $zip_name;
  192. }
  193. public static function unzip(string $_zip, string $_target)
  194. {
  195. if (is_file($_zip)) {
  196. $zipInfo = pathinfo($_zip);
  197. if ($zipInfo["extension"] == "zip") {
  198. $command = "unzip -q " . $_zip . " -d " . $_target . $zipInfo["filename"];
  199. try {
  200. system($command);
  201. return $zipInfo["filename"];
  202. } catch (Exception $ex) {
  203. return FALSE;
  204. }
  205. }
  206. }
  207. return FALSE;
  208. }
  209. public static function sizeFolder(string $_rep)
  210. {
  211. $Racine = opendir($_rep);
  212. $Taille = 0;
  213. while ($Dossier = readdir($Racine)) {
  214. if ($Dossier != '..' and $Dossier != '.') {
  215. //Ajoute la taille du sous dossier
  216. if (is_dir($_rep . '/' . $Dossier)) $Taille += self::sizeFolder($_rep . '/' .
  217. $Dossier);
  218. //Ajoute la taille du fichier
  219. else $Taille += filesize($_rep . '/' . $Dossier);
  220. }
  221. }
  222. closedir($Racine);
  223. return $Taille;
  224. }
  225. }