PageRenderTime 67ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/coaster/filemanager/execute.php

https://bitbucket.org/satumaa/mellow-cms
PHP | 437 lines | 361 code | 55 blank | 21 comment | 111 complexity | c3a864062e6a5e6b42a9cd87cf3862d1 MD5 | raw file
Possible License(s): MIT, JSON, BSD-3-Clause, MPL-2.0-no-copyleft-exception, BSD-2-Clause, GPL-2.0, GPL-3.0, 0BSD, Apache-2.0, CC-BY-SA-3.0, CC-BY-4.0, Unlicense
  1. <?php
  2. $config = include 'config/config.php';
  3. //TODO switch to array
  4. extract($config, EXTR_OVERWRITE);
  5. include 'include/utils.php';
  6. if ($_SESSION['RF']["verify"] != "RESPONSIVEfilemanager")
  7. {
  8. response(transfm('forbiden').AddErrorLocation())->send();
  9. exit;
  10. }
  11. if (strpos($_POST['path'],'/')===0
  12. || strpos($_POST['path'],'../')!==FALSE
  13. || strpos($_POST['path'],'./')===0)
  14. {
  15. response(transfm('wrong path'.AddErrorLocation()))->send();
  16. exit;
  17. }
  18. if (isset($_SESSION['RF']['language_file']) && file_exists($_SESSION['RF']['language_file']))
  19. {
  20. //TODO Very bad practice
  21. require_once $_SESSION['RF']['language_file'];
  22. }
  23. else
  24. {
  25. response(transfm('Lang_Not_Found').AddErrorLocation())->send();
  26. exit;
  27. }
  28. \CoasterCms\Helpers\Admin\FileManager::setSecureUpload($_POST['path']);
  29. $base = $current_path;
  30. $path = $base.$_POST['path'];
  31. $cycle = TRUE;
  32. $max_cycles = 50;
  33. $i = 0;
  34. while($cycle && $i<$max_cycles)
  35. {
  36. $i++;
  37. if ($path == $base) $cycle=FALSE;
  38. if (file_exists($path."config.php"))
  39. {
  40. require_once $path."config.php";
  41. $cycle = FALSE;
  42. }
  43. $path = fix_dirname($path)."/";
  44. $cycle = FALSE;
  45. }
  46. $path = $current_path.$_POST['path'];
  47. $path_thumb = $thumbs_base_path.$_POST['path'];
  48. if (isset($_POST['name']))
  49. {
  50. $name = fix_filename($_POST['name'],$transliteration,$convert_spaces, $replace_with);
  51. if (strpos($name,'../') !== FALSE)
  52. {
  53. response(transfm('wrong name').AddErrorLocation())->send();
  54. exit;
  55. }
  56. }
  57. $info = pathinfo($path);
  58. if (isset($info['extension']) && !(isset($_GET['action']) && $_GET['action']=='delete_folder') && !in_array(strtolower($info['extension']), $ext) && $_GET['action'] != 'create_file')
  59. {
  60. response(transfm('wrong extension').AddErrorLocation())->send();
  61. exit;
  62. }
  63. if (isset($_GET['action']))
  64. {
  65. switch($_GET['action'])
  66. {
  67. case 'delete_file':
  68. if ($delete_files){
  69. unlink($path);
  70. if (file_exists($path_thumb)) unlink($path_thumb);
  71. $info=pathinfo($path);
  72. if ($relative_image_creation){
  73. foreach($relative_path_from_current_pos as $k=>$path)
  74. {
  75. if ($path!="" && $path[strlen($path)-1]!="/") $path.="/";
  76. if (file_exists($info['dirname']."/".$path.$relative_image_creation_name_to_prepend[$k].$info['filename'].$relative_image_creation_name_to_append[$k].".".$info['extension']))
  77. {
  78. unlink($info['dirname']."/".$path.$relative_image_creation_name_to_prepend[$k].$info['filename'].$relative_image_creation_name_to_append[$k].".".$info['extension']);
  79. }
  80. }
  81. }
  82. if ($fixed_image_creation)
  83. {
  84. foreach($fixed_path_from_filemanager as $k=>$path)
  85. {
  86. if ($path!="" && $path[strlen($path)-1] != "/") $path.="/";
  87. $base_dir=$path.substr_replace($info['dirname']."/", '', 0, strlen($current_path));
  88. if (file_exists($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension']))
  89. {
  90. unlink($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension']);
  91. }
  92. }
  93. }
  94. }
  95. break;
  96. case 'delete_folder':
  97. if ($delete_folders){
  98. if (is_dir($path_thumb))
  99. {
  100. deleteDir($path_thumb);
  101. }
  102. if (is_dir($path))
  103. {
  104. deleteDir($path);
  105. if ($fixed_image_creation)
  106. {
  107. foreach($fixed_path_from_filemanager as $k=>$paths){
  108. if ($paths!="" && $paths[strlen($paths)-1] != "/") $paths.="/";
  109. $base_dir=$paths.substr_replace($path, '', 0, strlen($current_path));
  110. if (is_dir($base_dir)) deleteDir($base_dir);
  111. }
  112. }
  113. }
  114. }
  115. break;
  116. case 'create_folder':
  117. if ($create_folders)
  118. {
  119. $name = fix_filename($_POST['name'],$transliteration,$convert_spaces, $replace_with);
  120. $path .= $name;
  121. $path_thumb .= $name;
  122. create_folder(fix_path($path,$transliteration,$convert_spaces, $replace_with),fix_path($path_thumb,$transliteration,$convert_spaces, $replace_with));
  123. }
  124. break;
  125. case 'rename_folder':
  126. if ($rename_folders){
  127. $name=fix_filename($name,$transliteration,$convert_spaces, $replace_with);
  128. $name=str_replace('.','',$name);
  129. if (!empty($name)){
  130. if (!rename_folder($path,$name,$transliteration,$convert_spaces))
  131. {
  132. response(transfm('Rename_existing_folder').AddErrorLocation())->send();
  133. exit;
  134. }
  135. rename_folder($path_thumb,$name,$transliteration,$convert_spaces);
  136. if ($fixed_image_creation){
  137. foreach($fixed_path_from_filemanager as $k=>$paths){
  138. if ($paths!="" && $paths[strlen($paths)-1] != "/") $paths.="/";
  139. $base_dir=$paths.substr_replace($path, '', 0, strlen($current_path));
  140. rename_folder($base_dir,$name,$transliteration,$convert_spaces);
  141. }
  142. }
  143. } else {
  144. response(transfm('Empty_name').AddErrorLocation())->send();
  145. exit;
  146. }
  147. }
  148. break;
  149. case 'create_file':
  150. if ($create_text_files === FALSE) {
  151. response(sprintf(transfm('File_Open_Edit_Not_Allowed'), strtolower(transfm('Edit'))).AddErrorLocation())->send();
  152. exit;
  153. }
  154. if (!isset($editable_text_file_exts) || !is_array($editable_text_file_exts)){
  155. $editable_text_file_exts = array();
  156. }
  157. // check if user supplied extension
  158. if (strpos($name, '.') === FALSE){
  159. response(transfm('No_Extension').' '.sprintf(transfm('Valid_Extensions'), implode(', ', $editable_text_file_exts)).AddErrorLocation())->send();
  160. exit;
  161. }
  162. // correct name
  163. $old_name = $name;
  164. $name=fix_filename($name,$transliteration,$convert_spaces, $replace_with);
  165. if (empty($name))
  166. {
  167. response(transfm('Empty_name').AddErrorLocation())->send();
  168. exit;
  169. }
  170. // check extension
  171. $parts = explode('.', $name);
  172. if (!in_array(end($parts), $editable_text_file_exts)) {
  173. response(transfm('Error_extension').' '.sprintf(transfm('Valid_Extensions'), implode(', ', $editable_text_file_exts)), 400)->send();
  174. exit;
  175. }
  176. // file already exists
  177. if (file_exists($path.$name)) {
  178. response(transfm('Rename_existing_file').AddErrorLocation())->send();
  179. exit;
  180. }
  181. $content = $_POST['new_content'];
  182. if (!checkresultingsize(strlen($content))) {
  183. response(sprintf(transfm('max_size_reached'),$MaxSizeTotal).AddErrorLocation())->send();
  184. exit;
  185. }
  186. if (@file_put_contents($path.$name, $content) === FALSE) {
  187. response(transfm('File_Save_Error').AddErrorLocation())->send();
  188. exit;
  189. } else {
  190. if (is_function_callable('chmod') !== FALSE){
  191. chmod($path.$name, 0644);
  192. }
  193. response(transfm('File_Save_OK'))->send();
  194. exit;
  195. }
  196. break;
  197. case 'rename_file':
  198. if ($rename_files){
  199. $name=fix_filename($name,$transliteration,$convert_spaces, $replace_with);
  200. if (!empty($name))
  201. {
  202. if (!rename_file($path,$name,$transliteration))
  203. {
  204. response(transfm('Rename_existing_file').AddErrorLocation())->send();
  205. exit;
  206. }
  207. rename_file($path_thumb,$name,$transliteration);
  208. if ($fixed_image_creation)
  209. {
  210. $info=pathinfo($path);
  211. foreach($fixed_path_from_filemanager as $k=>$paths)
  212. {
  213. if ($paths!="" && $paths[strlen($paths)-1] != "/") $paths.="/";
  214. $base_dir = $paths.substr_replace($info['dirname']."/", '', 0, strlen($current_path));
  215. if (file_exists($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension']))
  216. {
  217. rename_file($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension'],$fixed_image_creation_name_to_prepend[$k].$name.$fixed_image_creation_to_append[$k],$transliteration);
  218. }
  219. }
  220. }
  221. } else {
  222. response(transfm('Empty_name').AddErrorLocation())->send();
  223. exit;
  224. }
  225. }
  226. break;
  227. case 'duplicate_file':
  228. if ($duplicate_files)
  229. {
  230. $name=fix_filename($name,$transliteration,$convert_spaces, $replace_with);
  231. if (!empty($name))
  232. {
  233. if (!checkresultingsize(filesize($path))) {
  234. response(sprintf(transfm('max_size_reached'),$MaxSizeTotal).AddErrorLocation())->send();
  235. exit;
  236. }
  237. if (!duplicate_file($path,$name))
  238. {
  239. response(transfm('Rename_existing_file').AddErrorLocation())->send();
  240. exit;
  241. }
  242. duplicate_file($path_thumb,$name);
  243. if ($fixed_image_creation)
  244. {
  245. $info=pathinfo($path);
  246. foreach($fixed_path_from_filemanager as $k=>$paths)
  247. {
  248. if ($paths!="" && $paths[strlen($paths)-1] != "/") $paths.= "/";
  249. $base_dir=$paths.substr_replace($info['dirname']."/", '', 0, strlen($current_path));
  250. if (file_exists($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension']))
  251. {
  252. duplicate_file($base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension'],$fixed_image_creation_name_to_prepend[$k].$name.$fixed_image_creation_to_append[$k]);
  253. }
  254. }
  255. }
  256. } else {
  257. response(transfm('Empty_name').AddErrorLocation())->send();
  258. exit;
  259. }
  260. }
  261. break;
  262. case 'paste_clipboard':
  263. if ( ! isset($_SESSION['RF']['clipboard_action'], $_SESSION['RF']['clipboard']['path'])
  264. || $_SESSION['RF']['clipboard_action'] == ''
  265. || $_SESSION['RF']['clipboard']['path'] == '')
  266. {
  267. response()->send();
  268. exit;
  269. }
  270. $action = $_SESSION['RF']['clipboard_action'];
  271. $data = $_SESSION['RF']['clipboard'];
  272. $data['path_thumb'] = $thumbs_base_path.$data['path'];
  273. $data['path'] = $current_path.$data['path'];
  274. $pinfo = pathinfo($data['path']);
  275. // user wants to paste to the same dir. nothing to do here...
  276. if ($pinfo['dirname'] == rtrim($path, '/')) {
  277. response()->send();
  278. exit;
  279. }
  280. // user wants to paste folder to it's own sub folder.. baaaah.
  281. if (is_dir($data['path']) && strpos($path, $data['path']) !== FALSE){
  282. response()->send();
  283. exit;
  284. }
  285. // something terribly gone wrong
  286. if ($action != 'copy' && $action != 'cut'){
  287. response(transfm('wrong action').AddErrorLocation())->send();
  288. exit;
  289. }
  290. // check for writability
  291. if (is_really_writable($path) === FALSE || is_really_writable($path_thumb) === FALSE){
  292. response(transfm('Dir_No_Write').'<br/>'.str_replace('../','',$path).'<br/>'.str_replace('../','',$path_thumb).AddErrorLocation())->send();
  293. exit;
  294. }
  295. // check if server disables copy or rename
  296. if (is_function_callable(($action == 'copy' ? 'copy' : 'rename')) === FALSE){
  297. response(sprintf(transfm('Function_Disabled'), ($action == 'copy' ? (transfm('Copy')) : (transfm('Cut')))).AddErrorLocation())->send();
  298. exit;
  299. }
  300. if ($action == 'copy')
  301. {
  302. list($sizeFolderToCopy,$fileNum,$foldersCount) = folder_info($path,false);
  303. if (!checkresultingsize($sizeFolderToCopy)) {
  304. response(sprintf(transfm('max_size_reached'),$MaxSizeTotal).AddErrorLocation())->send();
  305. exit;
  306. }
  307. rcopy($data['path'], $path);
  308. rcopy($data['path_thumb'], $path_thumb);
  309. } elseif ($action == 'cut') {
  310. rrename($data['path'], $path);
  311. rrename($data['path_thumb'], $path_thumb);
  312. // cleanup
  313. if (is_dir($data['path']) === TRUE){
  314. rrename_after_cleaner($data['path']);
  315. rrename_after_cleaner($data['path_thumb']);
  316. }
  317. }
  318. // cleanup
  319. $_SESSION['RF']['clipboard']['path'] = NULL;
  320. $_SESSION['RF']['clipboard_action'] = NULL;
  321. break;
  322. case 'chmod':
  323. $mode = $_POST['new_mode'];
  324. $rec_option = $_POST['is_recursive'];
  325. $valid_options = array('none', 'files', 'folders', 'both');
  326. $chmod_perm = (is_dir($path) ? $chmod_dirs : $chmod_files);
  327. // check perm
  328. if ($chmod_perm === FALSE) {
  329. response(sprintf(transfm('File_Permission_Not_Allowed'), (is_dir($path) ? (transfm('Folders')) : (transfm('Files')) )).AddErrorLocation())->send();
  330. exit;
  331. }
  332. // check mode
  333. if (!preg_match("/^[0-7]{3}$/", $mode)){
  334. response(transfm('File_Permission_Wrong_Mode').AddErrorLocation())->send();
  335. exit;
  336. }
  337. // check recursive option
  338. if (!in_array($rec_option, $valid_options)){
  339. response(transfm("wrong option").AddErrorLocation())->send();
  340. exit;
  341. }
  342. // check if server disabled chmod
  343. if (is_function_callable('chmod') === FALSE){
  344. response(sprintf(transfm('Function_Disabled'), 'chmod').AddErrorLocation())->send();
  345. exit;
  346. }
  347. $mode = "0".$mode;
  348. $mode = octdec($mode);
  349. rchmod($path, $mode, $rec_option);
  350. break;
  351. case 'save_text_file':
  352. $content = $_POST['new_content'];
  353. // $content = htmlspecialchars($content); not needed
  354. // $content = stripslashes($content);
  355. // no file
  356. if (!file_exists($path)) {
  357. response(transfm('File_Not_Found').AddErrorLocation())->send();
  358. exit;
  359. }
  360. // not writable or edit not allowed
  361. if (!is_writable($path) || $edit_text_files === FALSE) {
  362. response(sprintf(transfm('File_Open_Edit_Not_Allowed'), strtolower(transfm('Edit'))).AddErrorLocation())->send();
  363. exit;
  364. }
  365. if (!checkresultingsize(strlen($content))) {
  366. response(sprintf(transfm('max_size_reached'),$MaxSizeTotal).AddErrorLocation())->send();
  367. exit;
  368. }
  369. if (@file_put_contents($path, $content) === FALSE) {
  370. response(transfm('File_Save_Error').AddErrorLocation())->send();
  371. exit;
  372. } else {
  373. response(transfm('File_Save_OK'))->send();
  374. exit;
  375. }
  376. break;
  377. default:
  378. response(transfm('wrong action').AddErrorLocation())->send();
  379. exit;
  380. }
  381. }
  382. ?>