File.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. class File{
  3. //Gère l'upload d'un fichier des vérifications au stockage dans le dossier file
  4. public static function upload($index, $path, $maxSize=1048576, $extensions=array('jpg','png','jpeg','gif','bmp'), $trimParentDir=true){
  5. if($trimParentDir) $path = str_replace('..','',$path);
  6. if(!isset($_FILES[$index])) throw new Exception("L'index fichier '".$index."' n'existe pas");
  7. if($_FILES[$index]['error'] != 0) throw new Exception("Erreur lors de l'envois du fichier : N° ".$_FILES[$index]['error']);
  8. $extension = getExt($_FILES[$index]['name']);
  9. if($_FILES[$index]['size'] > $maxSize) throw new Exception("Taille du fichier trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)');
  10. if(is_array($extensions) && !in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier non permise, autorisé :".implode(', ',$extensions));
  11. $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path);
  12. $fileName = basename($filePath);
  13. $folderPath = dirname($filePath);
  14. $folderAbsolutePath = self::dir().$folderPath;
  15. $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName;
  16. if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true);
  17. if(!move_uploaded_file($_FILES[$index]['tmp_name'], $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé");
  18. return array(
  19. 'absolute' => $fileAbsolutePath,
  20. 'relative' => $filePath,
  21. 'name' => $fileName
  22. );
  23. }
  24. //récupere le type mime du fichier
  25. public static function mime($path){
  26. $infos = new finfo();
  27. $mime = $infos->file($path, FILEINFO_MIME_TYPE);
  28. if($mime == 'text/x-asm') $mime = 'text/css';
  29. return $mime;
  30. }
  31. public static function move($file,$path,$trimParentDir = true){
  32. if($trimParentDir){
  33. $file = str_replace('..','',$file);
  34. $path = str_replace('..','',$path);
  35. }
  36. $extension = getExt($path);
  37. $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path);
  38. $fileName = basename($filePath);
  39. $folderPath = dirname($filePath);
  40. $folderAbsolutePath = self::dir().$folderPath;
  41. $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName;
  42. if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true);
  43. if(!file_exists($file)) throw new Exception("Le fichier ".$file." n'existe pas",404);
  44. if(!rename($file, $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé");
  45. return array(
  46. 'absolute' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileAbsolutePath)) : $fileAbsolutePath),
  47. 'relative' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($filePath)) : $filePath),
  48. 'name' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileName)) : $fileName)
  49. );
  50. }
  51. public static function delete($namespace,$file,$trimParentDir = true,$ignoreExistence = false){
  52. if($trimParentDir) $file = str_replace('..','',$file);
  53. $namespace = trim($namespace, SLASH);
  54. $file = $namespace.SLASH.substr($file, strlen($namespace)+1);
  55. $file = File::dir().$file;
  56. $file = str_replace(array('\\','/'),array(SLASH,SLASH),$file);
  57. if(!file_exists($file)){
  58. if($ignoreExistence){
  59. return;
  60. } else {
  61. throw new Exception("Le fichier '".$file."' n'existe pas");
  62. }
  63. }
  64. if(is_file($file)){
  65. unlink($file);
  66. }else{
  67. delete_folder_tree($file, true);
  68. }
  69. }
  70. public static function downloadFile($path,$name=null,$mime = null,$forceDownload = null,$trimParentDir = true,$cache = true){
  71. if($trimParentDir) $path = str_replace('..','',$path);
  72. $hasFile = glob($path);
  73. if(count($hasFile)==0) throw new Exception("Fichier inexistant :".$path);
  74. $path = $hasFile[0];
  75. $stream = file_get_contents($path);
  76. $mime = !isset($mime) ? self::mime($path) : $mime;
  77. if(!isset($name)) $name = (get_OS() === 'WIN') ? utf8_encode(basename($path)) : basename($path);
  78. if(ob_get_contents()) ob_end_clean();
  79. header("Content-Type: ".$mime);
  80. header("Content-Length: " . strlen($stream));
  81. header("Content-Disposition: ".($forceDownload?"attachment":"inline")."; filename=\"".utf8_decode($name)."\"");
  82. if($cache){
  83. header('Expires: Sun, 24 Jan 1988 00:00:00 GMT');
  84. header('Cache-Control: no-store, no-cache, must-revalidate');
  85. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  86. header('Pragma: no-cache');
  87. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  88. }
  89. echo $stream;
  90. }
  91. public static function downloadStream($stream, $name='Sans titre', $mime='application/octet-stream'){
  92. if (ob_get_contents()) ob_end_clean();
  93. header("Content-Type: ".$mime);
  94. header("Content-Length: " . strlen($stream));
  95. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  96. header('Cache-Control: no-store, no-cache, must-revalidate');
  97. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  98. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  99. header('Pragma: no-cache');
  100. header("Content-Disposition: attachment; filename=\"".$name."\"");
  101. echo $stream;
  102. }
  103. public static function dir(){
  104. return __ROOT__.FILE_PATH;
  105. }
  106. //Return core folder's path
  107. public static function core(){
  108. return self::dir().'core';
  109. }
  110. public static function temp(){
  111. $path = self::dir().SLASH.'tmp'.SLASH;
  112. if(!file_exists($path)) mkdir($path,0755,true);
  113. return $path;
  114. }
  115. public static function clear_temp(){
  116. $time = time();
  117. foreach(glob(self::temp().'*.*') as $file){
  118. if($time - filemtime($file) <= 3600) return;
  119. !is_dir($file) ? unlink($file) : delete_folder_tree($file, true);
  120. }
  121. }
  122. public static function copy($source,$destination,$trimParentDir = true){
  123. if($trimParentDir) $source = str_replace('..','',$source);
  124. if($trimParentDir) $destination = str_replace('..','',$destination);
  125. $result = true;
  126. if(is_file($source)) return copy($source,$destination);
  127. $dir = opendir($source);
  128. if(!file_exists($destination)) mkdir($destination,0755,true);
  129. while(false !== ( $file = readdir($dir)) ) {
  130. if (( $file != '.' ) && ( $file != '..' )) {
  131. if ( is_dir($source . SLASH . $file) ) {
  132. $result = self::copy($source.SLASH.$file,$destination.SLASH.$file);
  133. } else {
  134. $copyResult = copy($source.SLASH.$file,$destination.SLASH.$file);
  135. if(!$copyResult) $result = false;
  136. }
  137. }
  138. }
  139. closedir($dir);
  140. return $result;
  141. }
  142. //Fusionne deux repertoires
  143. //N'ecrase rien : Si deux fichiers sont identiques, ajoute un (2...) a la fin d'un des deux fichiers
  144. //originalDirectory -> Destination
  145. //toMerge -> Source (celui qui se fait delete)
  146. public static function merge($originalDirectory,$toMergeDirectory, $delete = true){
  147. if(!file_exists($toMergeDirectory)) return;
  148. foreach(glob($toMergeDirectory.SLASH.'*') as $element){
  149. if(is_file($element)){
  150. $name = explode('.',basename($element));
  151. $ext = strtolower(array_pop($name));
  152. $name = $name[0];
  153. $destination = $originalDirectory.SLASH.$name.'.'.$ext;
  154. $increment = 1;
  155. while(file_exists($destination)){
  156. $increment++;
  157. $destination = $originalDirectory.SLASH.$name.'('.$increment.').'.$ext;
  158. }
  159. copy($element, $destination);
  160. } else {
  161. $name = basename($element);
  162. $destination = $originalDirectory.SLASH.$name;
  163. if(!file_exists($destination)) mkdir($destination,0755,true);
  164. self::merge($destination,$element,$delete);
  165. }
  166. }
  167. if ($delete) delete_folder_tree($toMergeDirectory, true);
  168. }
  169. //retourne un tableau avec les meta infos déduits du fichier
  170. public static function toArray($path,$label=null){
  171. $label = isset($label) ? $label : basename($path);
  172. $extension = getExt($label);
  173. $size = filesize($path);
  174. return array(
  175. 'path' => str_replace(self::dir(),'',$path),
  176. 'label' => $label,
  177. 'extension' => $extension,
  178. 'readableSize' => readable_size($size),
  179. 'size' => $size,
  180. 'icon' => getExtIcon($extension)
  181. );
  182. }
  183. public static function save_component($index,$destination){
  184. global $_;
  185. if(!isset($_[$index])) return;
  186. $destinationTpl = str_replace('/',SLASH,$destination);
  187. $saveds = array();
  188. foreach($_[$index] as $file){
  189. $destination = template($destinationTpl,$file,true);
  190. $file = self::dir().$file['path'];
  191. if(file_exists($file)) self::move($file,$destination);
  192. $saveds[] = array(
  193. 'absolute' => self::dir().$destination,
  194. 'relative' => $destination,
  195. 'name' => basename($destination)
  196. );
  197. }
  198. return $saveds;
  199. }
  200. public static function handle_component($options,&$response){
  201. global $myUser,$_;
  202. $options = array_merge(array(
  203. 'action' => $_['action'],
  204. 'extension' => 'jpg,png,svg,bmp,gif,jpeg,doc,xls,ppt,docx,xlsx,pptx,pdf,msg,eml',
  205. 'size' => 2000000,
  206. 'limit' => 0
  207. ),$options);
  208. switch($_['type']){
  209. case 'search':
  210. User::check_access($options['access'],'read');
  211. $response['options'] = array(
  212. 'extension' => $options['extension'],
  213. 'size' => $options['size'],
  214. );
  215. $response['files'] = array();
  216. $storage = str_replace('/',SLASH,$options['storage']);
  217. $storage = self::dir().template($storage,$_,true);
  218. //si l'url contient // on considere qu'il lui manque des élements de masque et on annule le browse
  219. //utile quand l'action est appellée alors qu'un parametre manque (ex: id de l'entité liée au fichier)
  220. if(strpos($storage, '//') === false){
  221. foreach(glob($storage, GLOB_BRACE) as $i=>$file){
  222. if(!is_file($file)) continue;
  223. $fileArray = self::toArray($file);
  224. $fileArray['url'] = 'action.php?action='.$options['action'].'&type=download&name='.urlencode($fileArray['label']).'&path='.base64_encode($fileArray['path']);
  225. if( in_array($fileArray['extension'], array('png','gif','jpg','jpeg','bmp')) ) $fileArray['preview'] = $fileArray['url'];
  226. $response['files'][] = $fileArray;
  227. }
  228. }
  229. break;
  230. case 'download':
  231. User::check_access($options['access'],'read');
  232. $name = isset($_['name']) ? $_['name'] : null;
  233. $path = self::dir().base64_decode($_['path']);
  234. try{
  235. self::downloadFile($path,$name);
  236. }catch(Exception $e){
  237. self::downloadFile(__ROOT__.SLASH.'img'.SLASH.'image-not-found.jpg',$name);
  238. }
  239. break;
  240. case 'delete':
  241. User::check_access($options['access'],'delete');
  242. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié");
  243. $namespace = $options['namespace'];
  244. if(strlen($_['path'])>5 && substr($_['path'],0,5) == '/tmp/' ) $namespace = 'tmp';
  245. self::delete($namespace,$_['path']);
  246. break;
  247. case 'upload':
  248. User::check_access($options['access'],'edit');
  249. try{
  250. self::clear_temp();
  251. $file = $_FILES[$_['index']];
  252. $tempPath = self::temp().basename($file['tmp_name']);
  253. move_uploaded_file($file['tmp_name'], $tempPath);
  254. $response = self::toArray($tempPath,$file['name']);
  255. $response['url'] = 'action.php?action='.$options['action'].'&type=download&name='.urlencode($response['label']).'&path='.base64_encode($response['path']);
  256. $response['sort'] = $_['sort'];
  257. if(in_array($response['extension'], array('png','gif','jpg','jpeg','bmp'))) $response['preview'] = $response['url'];
  258. }catch(Exception $e){
  259. $response['error'] = $e->getMessage();
  260. }
  261. break;
  262. }
  263. return $response;
  264. }
  265. public static function convert_encoding($path){
  266. return get_OS() === 'WIN' ? utf8_encode($path) : $path;
  267. }
  268. public static function convert_decoding($path){
  269. return get_OS() === 'WIN' ? utf8_decode($path) : $path;
  270. }
  271. }