PageRenderTime 189ms CodeModel.GetById 28ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/file-control.php

https://github.com/dydycloud/ICEcoder
PHP | 375 lines | 322 code | 25 blank | 28 comment | 102 complexity | aceb74ce7bed6c7327a38dfc04c61f4c MD5 | raw file
  1. <?php include("settings.php");?>
  2. <?php
  3. // Get the save type if any
  4. $saveType = "";
  5. if (isset($_GET['saveType'])) {$saveType = strClean($_GET['saveType']);};
  6. // Establish the filename/new filename
  7. $file = str_replace("|","/",strClean(
  8. isset($_POST['newFileName']) && $_POST['newFileName']!=""
  9. ? $_POST['newFileName']
  10. : $_GET['file']
  11. ));
  12. // Make $file a full path and establish the $fileLoc and $fileName
  13. if (strpos($file,$docRoot)===false) {$file=str_replace("|","/",$docRoot.$iceRoot.$file);};
  14. $fileLoc = substr(str_replace($docRoot,"",$file),0,strrpos(str_replace($docRoot,"",$file),"/"));
  15. $fileName = basename($file);
  16. // If we're due to open a file...
  17. if ($_GET['action']=="load") {
  18. echo '<script>action="load";</script>';
  19. if (file_exists($file)) {
  20. // Determine what to do based on mime type
  21. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  22. if (strpos(finfo_file($finfo, $file),"text")===0) {
  23. echo '<script>fileType="text";';
  24. echo 'top.ICEcoder.shortURL = top.ICEcoder.rightClickedFile = top.ICEcoder.thisFileFolderLink = "'.$fileLoc."/".$fileName.'";';
  25. echo '</script>';
  26. $loadedFile = file_get_contents($file);
  27. echo '<textarea name="loadedFile" id="loadedFile">'.str_replace("</textarea>","<ICEcoder:/:textarea>",htmlentities($loadedFile)).'</textarea>';
  28. } else if (strpos(finfo_file($finfo, $file),"image")===0) {
  29. echo '<script>fileType="image";fileName=\''.$fileLoc."/".$fileName.'\'</script>';
  30. } else {
  31. echo '<script>fileType="other";window.open(\'http://'.$_SERVER['SERVER_NAME'].$fileLoc."/".$fileName.'\');</script>';
  32. };
  33. } else {
  34. echo '<script>fileType="nothing"; top.ICEcoder.message(\'Sorry, '.$fileLoc."/".$fileName.' doesn\\\'t seem to exist on the server\');</script>';
  35. }
  36. finfo_close($finfo);
  37. };
  38. // If we're due to add a new folder...
  39. if ($_GET['action']=="newFolder") {
  40. if (!$demoMode && is_writable($docRoot.$fileLoc)) {
  41. mkdir($file, 0705);
  42. // Reload file manager
  43. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.$fileLoc.'\',\''.$fileName.'\');action="newFolder";</script>';
  44. } else {
  45. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot create folder at\\n".$fileLoc."')</script>";
  46. }
  47. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  48. }
  49. // If we're due to paste a new file...
  50. if ($_GET['action']=="paste") {
  51. $source = $file;
  52. $dest = str_replace("//","/",$docRoot.strClean(str_replace("|","/",$_GET['location']))."/".basename($source));
  53. if (!$demoMode && is_writable(dirname($dest))) {
  54. if (is_dir($source)) {
  55. if (!is_dir($dest)) {
  56. mkdir($dest, 0705);
  57. }
  58. foreach ($iterator = new RecursiveIteratorIterator(
  59. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  60. RecursiveIteratorIterator::SELF_FIRST) as $item
  61. ) {
  62. if ($item->isDir()) {
  63. mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName(), 0705);
  64. } else {
  65. copy($item, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
  66. }
  67. }
  68. } else {
  69. if (!file_exists($dest)) {
  70. copy($source, $dest);
  71. } else {
  72. for ($i=2; $i<1000000000; $i++) {
  73. if (!file_exists($dest." (".$i.")")) {
  74. $dest = $dest." (".$i.")";
  75. copy($source, $dest);
  76. $i=1000000000;
  77. }
  78. }
  79. }
  80. }
  81. // Reload file manager
  82. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.strClean(str_replace("|","/",$_GET['location'])).'\',\''.basename($dest).'\');action="pasteFile";</script>';
  83. } else {
  84. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot copy \\n".str_replace($docRoot,"",$source)."\\n into \\n".str_replace($docRoot,"",$dest)."')</script>";
  85. }
  86. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  87. }
  88. // If we're due to upload files...
  89. if ($_GET['action']=="upload") {
  90. if (!$demoMode) {
  91. class fileUploader {
  92. public function __construct($uploads) {
  93. global $docRoot;
  94. $uploadDir=$docRoot.$iceRoot.str_replace("..","",str_replace("|","/",strClean($_POST['folder'])."/"));
  95. foreach($uploads as $current) {
  96. $this->uploadFile=$uploadDir.$current->name;
  97. $fileName = $current->name;
  98. if ($this->upload($current,$this->uploadFile)) {
  99. echo '<script>action="upload"; top.ICEcoder.updateFileManagerList(\'add\',top.ICEcoder.rightClickedFile.replace(/\|/g,\'/\'),\''.$fileName.'\',false,false,true); top.ICEcoder.serverMessage("Uploaded file(s) OK");setTimeout(function(){top.ICEcoder.serverMessage();},2000);</script>';
  100. } else {
  101. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot upload \\n".$fileName."\\n into \\n'+top.ICEcoder.rightClickedFile.replace(/\|/g,'/'))</script>";
  102. }
  103. }
  104. }
  105. public function upload($current,$uploadFile){
  106. if(move_uploaded_file($current->tmp_name,$uploadFile)){
  107. return true;
  108. }
  109. }
  110. }
  111. function getDetails($fileArr) {
  112. foreach($fileArr['name'] as $keyee => $info) {
  113. $uploads[$keyee]->name=$fileArr['name'][$keyee];
  114. $uploads[$keyee]->type=$fileArr['type'][$keyee];
  115. $uploads[$keyee]->tmp_name=$fileArr['tmp_name'][$keyee];
  116. $uploads[$keyee]->error=$fileArr['error'][$keyee];
  117. }
  118. return $uploads;
  119. }
  120. if($_FILES['filesInput']){
  121. $uploads = getDetails($_FILES['filesInput']);
  122. $fileUploader=new fileUploader($uploads);
  123. }
  124. } else {
  125. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot upload whilst in demo mode');</script>";
  126. }
  127. echo "<script>top.ICEcoder.hideFileMenu();top.ICEcoder.showHide('hide',top.document.getElementById('loadingMask'));</script>";
  128. }
  129. // If we're due to rename a file/folder...
  130. if ($_GET['action']=="rename") {
  131. if (!$demoMode && is_writable($docRoot.$iceRoot.str_replace("|","/",strClean($_GET['oldFileName'])))) {
  132. if(rename($docRoot.$iceRoot.str_replace("|","/",strClean($_GET['oldFileName'])),$docRoot.$fileLoc."/".$fileName)) {
  133. // Reload file manager
  134. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'rename\',\''.$fileLoc.'\',\''.$fileName.'\',\'\',\''.str_replace($iceRoot,"",strClean($_GET['oldFileName'])).'\');';
  135. echo 'action="rename";</script>';
  136. $renamed=true;
  137. } else {
  138. $renamed=false;
  139. }
  140. } else {
  141. $renamed=false;
  142. }
  143. if (!$renamed) {
  144. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot rename\\n".strClean($_GET['oldFileName'])."\\n\\nMaybe public write permissions needed on this or parent folder?');</script>";
  145. }
  146. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  147. }
  148. // If we're due to replace text in a file...
  149. if ($_GET['action']=="replaceText") {
  150. if (!$demoMode && is_writable(str_replace("|","/",strClean($_GET['fileRef'])))) {
  151. $file = str_replace("|","/",strClean($_GET['fileRef']));
  152. $loadedFile = file_get_contents($file);
  153. $newContent = str_replace(strClean($_GET['find']),strClean($_GET['replace']),$loadedFile);
  154. $fh = fopen($file, 'w') or die("Sorry, cannot save");
  155. fwrite($fh, $newContent);
  156. fclose($fh);
  157. echo '<script>action="replaceText";</script>';
  158. } else {
  159. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot replace text in\\n".strClean($_GET['fileRef'])."');</script>";
  160. }
  161. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  162. }
  163. // If we're due to change permissions on a file/folder...
  164. if ($_GET['action']=="perms") {
  165. if (!$demoMode && is_writable($file)) {
  166. chmod($file,octdec(numClean($_GET['perms'])));
  167. // Reload file manager
  168. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'chmod\',\''.$fileLoc.'\',\''.$fileName.'\',\''.numClean($_GET['perms']).'\');';
  169. echo 'action="perms";</script>';
  170. } else {
  171. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot change permissions on \\n".strClean($file)."');</script>";
  172. }
  173. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  174. }
  175. // If we're due to delete a file...
  176. if ($_GET['action']=="delete") {
  177. $filesArray = explode(";",$file); // May contain more than one file here
  178. for ($i=0;$i<=count($filesArray)-1;$i++) {
  179. $fullPath = str_replace($docRoot,"",$filesArray[$i]);
  180. $fullPath = str_replace($iceRoot,"",$fullPath);
  181. $fullPath = $docRoot.$iceRoot.$fullPath;
  182. if (!$demoMode && is_writable($fullPath)) {
  183. is_dir($fullPath)
  184. ? rrmdir($fullPath)
  185. : unlink($fullPath);
  186. $fileName = basename($fullPath);
  187. $fileLoc = dirname(str_replace($docRoot,"",$fullPath));
  188. // Reload file manager
  189. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'delete\',\''.$fileLoc.'\',\''.$fileName.'\');';
  190. echo 'action="delete";</script>';
  191. } else {
  192. echo "<script>top.ICEcoder.message('Sorry can\\'t delete\\n".str_replace($docRoot,"",$fullPath)."');</script>";
  193. }
  194. echo '<script>action="nothing";</script>';
  195. }
  196. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  197. }
  198. // The function to recursively remove folders & files
  199. function rrmdir($dir) {
  200. if (is_dir($dir)) {
  201. $objects = scandir($dir);
  202. foreach ($objects as $object) {
  203. if ($object != "." && $object != "..") {
  204. filetype($dir."/".$object) == "dir"
  205. ? rrmdir($dir."/".$object)
  206. : unlink($dir."/".$object);
  207. }
  208. }
  209. reset($objects);
  210. rmdir($dir);
  211. }
  212. }
  213. if ($_GET['action']=="save") {
  214. echo '<script>action="save";</script>';
  215. // on the form posting via a reload, save the file
  216. if (isset($_POST['contents'])) {
  217. if (!$demoMode && ((file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && $_POST['newFileName']!="")) {
  218. if (filemtime($file)==$_GET['fileMDT']||!(isset($_GET['fileMDT']))) {
  219. $fh = fopen($file, 'w') or die("Sorry, cannot save");
  220. fwrite($fh, $_POST['contents']);
  221. fclose($fh);
  222. clearstatcache();
  223. echo '<script>top.ICEcoder.openFileMDTs[top.ICEcoder.selectedTab-1]="'.filemtime($file).'";</script>';
  224. // Reload file manager & rename tab if it was a new file
  225. if (isset($_POST['newFileName']) && $_POST['newFileName']!="") {
  226. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.$fileLoc.'\',\''.$fileName.'\');';
  227. echo 'top.ICEcoder.renameTab(top.ICEcoder.selectedTab,\''.$fileLoc."/".$fileName.'\');</script>';
  228. }
  229. // Reload stickytab window
  230. echo '<script>if (top.ICEcoder.stickyTab.location) {top.ICEcoder.stickyTab.location.reload()};action="doneSave";</script>';
  231. } else {
  232. $loadedFile = file_get_contents($file);
  233. echo '<textarea name="loadedFile" id="loadedFile">'.str_replace("</textarea>","<ICEcoder:/:textarea>",htmlentities($loadedFile)).'</textarea>';
  234. echo '<textarea name="userVersionFile" id="userVersionFile"></textarea>';
  235. ?>
  236. <script>
  237. var refreshFile = top.ICEcoder.ask('Sorry, this file has changed, cannot save\n<?php echo $file;?>\n\nReload this file and copy your version to a new document?');
  238. if (refreshFile) {
  239. var cM = top.ICEcoder.getcMInstance();
  240. var thisTab = top.ICEcoder.selectedTab;
  241. document.getElementById('userVersionFile').value = cM.getValue();
  242. // Revert back to original
  243. cM.setValue(document.getElementById('loadedFile').value);
  244. top.ICEcoder.changedContent[thisTab-1] = 0;
  245. top.ICEcoder.openFileMDTs[top.ICEcoder.selectedTab-1] = "<?php echo filemtime($file); ?>";
  246. cM.clearHistory();
  247. // Now for the new file
  248. top.ICEcoder.newTab();
  249. cM = top.ICEcoder.getcMInstance();
  250. cM.setValue(document.getElementById('userVersionFile').value);
  251. cM.clearHistory();
  252. // Finally, switch back to original tab
  253. top.ICEcoder.switchTab(thisTab);
  254. }
  255. action='nothing';
  256. </script>
  257. <?php
  258. }
  259. } else {
  260. echo "<script>action='nothing';top.ICEcoder.message('Sorry, cannot write\\n".$file."')</script>";
  261. }
  262. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  263. }
  264. };
  265. ?>
  266. <script>
  267. if (action=="load") {
  268. if (fileType=="text") {
  269. setTimeout(function() {
  270. if (!top.ICEcoder.content.contentWindow.createNewCMInstance) {
  271. console.log('There was tech hiccup, likely something wasn\'t quite ready. So ICEcoder reloaded it\'s file control again.');
  272. window.location.reload();
  273. <?php
  274. if (file_exists($file)) {
  275. ?>
  276. } else {
  277. top.ICEcoder.loadingFile = true;
  278. // Reset the various states back to their initial setting
  279. selectedTab = top.ICEcoder.openFiles.length; // The tab that's currently selected
  280. // Finally, store all data, show tabs etc
  281. top.ICEcoder.createNewTab();
  282. top.ICEcoder.cMInstances.push(top.ICEcoder.nextcMInstance);
  283. top.ICEcoder.setLayout();
  284. top.ICEcoder.content.contentWindow.createNewCMInstance(top.ICEcoder.nextcMInstance);
  285. // Set the value & innerHTML of the code textarea to that of our loaded file plus make it visible (it's hidden on _coder's load)
  286. top.ICEcoder.switchMode();
  287. cM = top.ICEcoder.getcMInstance();
  288. cM.setValue(document.getElementById('loadedFile').value);
  289. top.document.getElementById('content').style.visibility='visible';
  290. top.ICEcoder.switchTab(top.ICEcoder.selectedTab);
  291. cM.focus();
  292. // Then clean it up, set the text cursor, update the display and get the character data
  293. top.ICEcoder.contentCleanUp();
  294. top.ICEcoder.content.contentWindow['cM'+top.ICEcoder.cMInstances[top.ICEcoder.selectedTab-1]].removeLineClass(top.ICEcoder['cMActiveLine'+top.ICEcoder.selectedTab], "background");
  295. top.ICEcoder['cMActiveLine'+top.ICEcoder.selectedTab] = top.ICEcoder.content.contentWindow['cM'+top.ICEcoder.cMInstances[top.ICEcoder.selectedTab-1]].addLineClass(0, "background", "cm-s-activeLine");
  296. top.ICEcoder.nextcMInstance++;
  297. top.ICEcoder.openFileMDTs.push('<?php echo filemtime($file); ?>');
  298. top.ICEcoder.loadingFile = false;
  299. <?php
  300. ;};
  301. ?>
  302. }
  303. },4);
  304. }
  305. if (fileType=="image") {
  306. top.document.getElementById('blackMask').style.visibility = "visible";
  307. top.document.getElementById('mediaContainer').innerHTML = "<img src=\"<?php echo $fileLoc."/".$fileName;?>\" class=\"whiteGlow\" style=\"border: solid 10px #fff; max-width: 700px; max-height: 500px; background-color: #000; background-image: url('images/checkerboard.png')\" onClick=\"return false\"><br><span class=\"whiteGlow\" style=\"border: solid 10px #fff; color: #000; background-color: #fff\" onClick=\"return false\"><?php echo $fileLoc."/".$fileName;?></span>";
  308. }
  309. top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);
  310. }
  311. </script>
  312. <form name="saveFile" action="file-control.php?action=save&file=<?php if (isset($file)) {echo $file;}; if (isset($_GET['fileMDT']) && $_GET['fileMDT']!="undefined") {echo "&fileMDT=".numClean($_GET['fileMDT']);};?>" method="POST">
  313. <textarea name="contents"></textarea>
  314. <input type="hidden" name="newFileName" value="">
  315. </form>
  316. <script>
  317. if (action=="save") {
  318. <?php
  319. if (strpos($file,"[NEW]")>0||$saveType=="saveAs") {
  320. if (strpos($fileName,"[NEW]")>0) {echo "fileLoc = '".$fileLoc."';";} else {echo "fileLoc = '';";};
  321. ?>
  322. newFileName = top.ICEcoder.getInput(fileLoc != ""
  323. ? 'Enter filename to save at '+fileLoc
  324. : 'Enter filename (including path, prefixed with /)'
  325. ,'');
  326. if (newFileName.substr(0,1)!="/") {newFileName = "/" + newFileName}
  327. if (newFileName) {
  328. newFileName = fileLoc == "" ? newFileName : fileLoc + "/" + fileName;
  329. }
  330. if (newFileName && top.document.getElementById('filesFrame').contentWindow.document.getElementById(newFileName.replace(/\//g,"|"))) {
  331. overwriteOK = top.ICEcoder.ask('That file exists already, overwrite?');
  332. }
  333. document.saveFile.newFileName.value = '<?php echo $docRoot; ?>' + newFileName;
  334. <?php ;};?>
  335. if ("undefined" == typeof newFileName || (newFileName && "undefined" == typeof overwriteOK) || ("undefined" != typeof overwriteOK && overwriteOK)) {
  336. top.ICEcoder.serverMessage('<b>Saving</b><br>'+ <?php echo strpos($file,"[NEW]")>0 ? "newFileName" : "'$file'"; ?>);
  337. document.saveFile.contents.value = top.document.getElementById('saveTemp1').value;
  338. document.saveFile.submit();
  339. } else {
  340. top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);
  341. action=="nothing";
  342. }
  343. }
  344. </script>
  345. <script>
  346. if (action=="doneSave") {
  347. top.ICEcoder.changedContent[top.ICEcoder.selectedTab-1] = 0;
  348. top.ICEcoder.redoTabHighlight(top.ICEcoder.selectedTab);
  349. }
  350. </script>