PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public/filemanager/upload.php

https://gitlab.com/cdix/mirum-app
PHP | 277 lines | 229 code | 36 blank | 12 comment | 61 complexity | 31d4b9fdd91134a5663843b743290778 MD5 | raw file
  1. <?php
  2. if (!isset($config)){
  3. $config = include 'config/config.php';
  4. //TODO switch to array
  5. extract($config, EXTR_OVERWRITE);
  6. }
  7. include 'include/utils.php';
  8. if ($_SESSION['RF']["verify"] != "RESPONSIVEfilemanager")
  9. {
  10. response(trans('forbiden').AddErrorLocation(), 403)->send();
  11. exit;
  12. }
  13. include 'include/mime_type_lib.php';
  14. if (isset($_POST['path']))
  15. {
  16. $storeFolder = $_POST['path'];
  17. $storeFolderThumb = $_POST['path_thumb'];
  18. }
  19. else
  20. {
  21. $storeFolder = $current_path.$_POST["fldr"]; // correct for when IE is in Compatibility mode
  22. $storeFolderThumb = $thumbs_base_path.$_POST["fldr"];
  23. }
  24. $ftp=ftp_con($config);
  25. if($ftp){
  26. $source_base = $ftp_base_folder.$upload_dir;
  27. $thumb_base = $ftp_base_folder.$ftp_thumbs_dir;
  28. $path_pos = strpos($storeFolder,$source_base);
  29. $thumb_pos = strpos($storeFolderThumb,$thumb_base);
  30. }else{
  31. $source_base = $current_path;
  32. $thumb_base = $thumbs_base_path;
  33. $path_pos = strpos($storeFolder,$source_base);
  34. $thumb_pos = strpos($storeFolderThumb,$thumb_base);
  35. }
  36. if ($path_pos!==0
  37. || $thumb_pos !==0
  38. || strpos($storeFolderThumb,'../',strlen($thumb_base)) !== FALSE
  39. || strpos($storeFolderThumb,'./',strlen($thumb_base)) !== FALSE
  40. || strpos($storeFolder,'../',strlen($source_base)) !== FALSE
  41. || strpos($storeFolder,'./',strlen($source_base)) !== FALSE )
  42. {
  43. response(trans('wrong path'.AddErrorLocation()))->send();
  44. exit;
  45. }
  46. $path = $storeFolder;
  47. $cycle = TRUE;
  48. $max_cycles = 50;
  49. $i = 0;
  50. while ($cycle && $i < $max_cycles)
  51. {
  52. $i++;
  53. if ($path == $current_path) $cycle = FALSE;
  54. if (file_exists($path."config.php"))
  55. {
  56. require_once $path."config.php";
  57. $cycle = FALSE;
  58. }
  59. $path = fix_dirname($path).'/';
  60. }
  61. if ( ! empty($_FILES) || isset($_POST['url']))
  62. {
  63. if(isset($_POST['url'])){
  64. $temp = tempnam('/tmp','RF');
  65. $handle = fopen($temp, "w");
  66. fwrite($handle, file_get_contents($_POST['url']));
  67. fclose($handle);
  68. $_FILES['file']= array(
  69. 'name' => basename($_POST['url']),
  70. 'tmp_name' => $temp,
  71. 'size' => filesize($temp)
  72. );
  73. }
  74. $info = pathinfo($_FILES['file']['name']);
  75. $mime_type = $_FILES['file']['type'];
  76. if (function_exists('mime_content_type')){
  77. $mime_type = mime_content_type($_FILES['file']['tmp_name']);
  78. }elseif(function_exists('finfo_open')){
  79. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  80. $mime_type = finfo_file($finfo, $_FILES['file']['tmp_name']);
  81. }else{
  82. include 'include/mime_type_lib.php';
  83. $mime_type = get_file_mime_type($_FILES['file']['tmp_name']);
  84. }
  85. $extension = get_extension_from_mime($mime_type);
  86. if($extension=='so'){
  87. $extension = $info['extension'];
  88. }
  89. if (in_array(fix_strtolower($extension), $ext))
  90. {
  91. $tempFile = $_FILES['file']['tmp_name'];
  92. $targetPath = $storeFolder;
  93. $targetPathThumb = $storeFolderThumb;
  94. $_FILES['file']['name'] = fix_filename($info['filename'].".".$extension,$transliteration,$convert_spaces, $replace_with);
  95. // LowerCase
  96. if ($lower_case)
  97. {
  98. $_FILES['file']['name'] = fix_strtolower($_FILES['file']['name']);
  99. }
  100. // Gen. new file name if exists
  101. if (file_exists($targetPath.$_FILES['file']['name']))
  102. {
  103. $i = 1;
  104. $info = pathinfo($_FILES['file']['name']);
  105. // append number
  106. while(file_exists($targetPath.$info['filename']."_".$i.".".$extension)) {
  107. $i++;
  108. }
  109. $_FILES['file']['name'] = $info['filename']."_".$i.".".$extension;
  110. }
  111. $targetFile = $targetPath. $_FILES['file']['name'];
  112. $targetFileThumb = $targetPathThumb. $_FILES['file']['name'];
  113. // check if image (and supported)
  114. if (in_array(fix_strtolower($extension),$ext_img)) $is_img=TRUE;
  115. else $is_img=FALSE;
  116. if (!checkresultingsize($_FILES['file']['size'])) {
  117. response(sprintf(trans('max_size_reached'),$MaxSizeTotal).AddErrorLocation(), 406)->send();
  118. exit;
  119. }
  120. // upload
  121. if($ftp){
  122. $targetFile = tempnam('/tmp','RF').$_FILES['file']['name'];
  123. if ($is_img)
  124. {
  125. $targetFileThumb = tempnam('/tmp','RF').$_FILES['file']['name'];
  126. }
  127. }
  128. if(is_uploaded_file($tempFile)){
  129. move_uploaded_file($tempFile,$targetFile);
  130. }else{
  131. copy($tempFile,$targetFile);
  132. unlink($tempFile);
  133. }
  134. chmod($targetFile, $fileFolderPermission);
  135. if ($is_img)
  136. {
  137. if(isset($image_watermark) && $image_watermark){
  138. require_once('include/php_image_magician.php');
  139. $magicianObj = new imageLib($targetFile);
  140. $magicianObj -> addWatermark($image_watermark, $image_watermark_position, $image_watermark_padding);
  141. $magicianObj -> saveImage($targetFile);
  142. }
  143. $memory_error = FALSE;
  144. if ( ! create_img($targetFile, $targetFileThumb, 122, 91))
  145. {
  146. $memory_error = TRUE;
  147. }
  148. else
  149. {
  150. // TODO something with this long function baaaah...
  151. if( !$ftp && ! new_thumbnails_creation($targetPath,$targetFile,$_FILES['file']['name'],$current_path,$relative_image_creation,$relative_path_from_current_pos,$relative_image_creation_name_to_prepend,$relative_image_creation_name_to_append,$relative_image_creation_width,$relative_image_creation_height,$relative_image_creation_option,$fixed_image_creation,$fixed_path_from_filemanager,$fixed_image_creation_name_to_prepend,$fixed_image_creation_to_append,$fixed_image_creation_width,$fixed_image_creation_height,$fixed_image_creation_option))
  152. {
  153. $memory_error = TRUE;
  154. }
  155. else
  156. {
  157. $imginfo = getimagesize($targetFile);
  158. $srcWidth = $imginfo[0];
  159. $srcHeight = $imginfo[1];
  160. // resize images if set
  161. if ($image_resizing)
  162. {
  163. if ($image_resizing_width == 0) // if width not set
  164. {
  165. if ($image_resizing_height == 0)
  166. {
  167. $image_resizing_width = $srcWidth;
  168. $image_resizing_height = $srcHeight;
  169. }
  170. else
  171. {
  172. $image_resizing_width = $image_resizing_height*$srcWidth/$srcHeight;
  173. }
  174. }
  175. elseif ($image_resizing_height == 0) // if height not set
  176. {
  177. $image_resizing_height = $image_resizing_width*$srcHeight/$srcWidth;
  178. }
  179. // new dims and create
  180. $srcWidth = $image_resizing_width;
  181. $srcHeight = $image_resizing_height;
  182. create_img($targetFile, $targetFile, $image_resizing_width, $image_resizing_height, $image_resizing_mode);
  183. }
  184. //max resizing limit control
  185. $resize = FALSE;
  186. if ($image_max_width != 0 && $srcWidth > $image_max_width && $image_resizing_override === FALSE)
  187. {
  188. $resize = TRUE;
  189. $srcWidth = $image_max_width;
  190. if ($image_max_height == 0) $srcHeight = $image_max_width*$srcHeight/$srcWidth;
  191. }
  192. if ($image_max_height != 0 && $srcHeight > $image_max_height && $image_resizing_override === FALSE){
  193. $resize = TRUE;
  194. $srcHeight = $image_max_height;
  195. if ($image_max_width == 0) $srcWidth = $image_max_height*$srcWidth/$srcHeight;
  196. }
  197. if ($resize){ create_img($targetFile, $targetFile, $srcWidth, $srcHeight, $image_max_mode); }
  198. }
  199. }
  200. // not enough memory
  201. if ($memory_error)
  202. {
  203. unlink($targetFile);
  204. response(trans("Not enought Memory").AddErrorLocation(), 406)->send();
  205. exit();
  206. }
  207. }
  208. if($ftp){
  209. $ftp->put($targetPath. $_FILES['file']['name'], $targetFile, FTP_BINARY);
  210. unlink($targetFile);
  211. if ($is_img)
  212. {
  213. $ftp->put($targetPathThumb. $_FILES['file']['name'], $targetFileThumb, FTP_BINARY);
  214. unlink($targetFileThumb);
  215. }
  216. }
  217. echo $_FILES['file']['name'];
  218. }
  219. else // file ext. is not in the allowed list
  220. {
  221. response("Thumbnails creation: ".trans("Error_extension").AddErrorLocation(), 406)->send();
  222. exit();
  223. }
  224. }
  225. else // no files to upload
  226. {
  227. response(trans("no file").AddErrorLocation(), 405)->send();
  228. exit();
  229. }
  230. // redirect
  231. if (isset($_POST['submit']))
  232. {
  233. $query = http_build_query(array(
  234. 'type' => $_POST['type'],
  235. 'lang' => $_POST['lang'],
  236. 'popup' => $_POST['popup'],
  237. 'field_id' => $_POST['field_id'],
  238. 'fldr' => $_POST['fldr'],
  239. ));
  240. header("location: dialog.php?" . $query);
  241. }