PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/file-control.php

https://github.com/Amsoft-Systems/ICEcoder
PHP | 284 lines | 237 code | 20 blank | 27 comment | 75 complexity | 6b31bff0ad9e075f7e820fb5657ccdaa 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. // Determine what to do based on mime type
  20. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  21. if (strpos(finfo_file($finfo, $file),"text")===0) {
  22. if (file_exists($file)) {
  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 {
  29. echo '<script>fileType="nothing"; top.ICEcoder.message(\'Sorry, '.$fileLoc."/".$fileName.' doesn\\\'t seem to exist on the server\');</script>';
  30. }
  31. } else if (strpos(finfo_file($finfo, $file),"image")===0) {
  32. echo '<script>fileType="image";fileName=\''.$fileLoc."/".$fileName.'\'</script>';
  33. } else {
  34. echo '<script>fileType="other";window.open(\'http://'.$_SERVER['SERVER_NAME'].$fileLoc."/".$fileName.'\');</script>';
  35. };
  36. finfo_close($finfo);
  37. };
  38. // If we're due to add a new folder...
  39. if ($_GET['action']=="newFolder") {
  40. if (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. $location = $docRoot.strClean(str_replace("|","/",$_GET['location']));
  52. if (is_writable($location)) {
  53. copy($file, $location."/".basename($file));
  54. // Reload file manager
  55. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.str_replace($docRoot,"",$location).'\',\''.$fileName.'\');action="pasteFile";</script>';
  56. } else {
  57. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot copy file into \\n".$location."')</script>";
  58. }
  59. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  60. }
  61. // If we're due to rename a file/folder...
  62. if ($_GET['action']=="rename") {
  63. if (is_writable($docRoot.$iceRoot.str_replace("|","/",strClean($_GET['oldFileName'])))) {
  64. rename($docRoot.$iceRoot.str_replace("|","/",strClean($_GET['oldFileName'])),$docRoot.$fileLoc."/".$fileName);
  65. // Reload file manager
  66. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'rename\',\''.$fileLoc.'\',\''.$fileName.'\',\'\',\''.str_replace($iceRoot,"",strClean($_GET['oldFileName'])).'\');';
  67. echo 'action="rename";</script>';
  68. } else {
  69. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot rename\\n".strClean($_GET['oldFileName'])."');</script>";
  70. }
  71. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  72. }
  73. // If we're due to replace text in a file...
  74. if ($_GET['action']=="replaceText") {
  75. if (is_writable(str_replace("|","/",strClean($_GET['fileRef'])))) {
  76. $file = str_replace("|","/",strClean($_GET['fileRef']));
  77. $loadedFile = file_get_contents($file);
  78. $newContent = str_replace(strClean($_GET['find']),strClean($_GET['replace']),$loadedFile);
  79. $fh = fopen($file, 'w') or die("Sorry, cannot save");
  80. fwrite($fh, $newContent);
  81. fclose($fh);
  82. echo '<script>action="replaceText";</script>';
  83. } else {
  84. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot replace text in\\n".strClean($_GET['fileRef'])."');</script>";
  85. }
  86. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  87. }
  88. // If we're due to change permissions on a file/folder...
  89. if ($_GET['action']=="perms") {
  90. if (is_writable($file)) {
  91. chmod($file,octdec(numClean($_GET['perms'])));
  92. // Reload file manager
  93. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'chmod\',\''.$fileLoc.'\',\''.$fileName.'\',\''.numClean($_GET['perms']).'\');';
  94. echo 'action="perms";</script>';
  95. } else {
  96. echo "<script>action='nothing'; top.ICEcoder.message('Sorry, cannot change permissions on \\n".strClean($file)."');</script>";
  97. }
  98. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  99. }
  100. // If we're due to delete a file...
  101. if ($_GET['action']=="delete") {
  102. $filesArray = explode(";",$file); // May contain more than one file here
  103. for ($i=0;$i<=count($filesArray)-1;$i++) {
  104. if (is_writable($iceRoot.$filesArray[$i])) {
  105. is_dir($iceRoot.$filesArray[$i])
  106. ? rrmdir($iceRoot.$filesArray[$i])
  107. : unlink($iceRoot.$filesArray[$i]);
  108. // Reload file manager
  109. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'delete\',\''.$fileLoc.'\',\''.$fileName.'\');';
  110. echo 'action="delete";</script>';
  111. } else {
  112. echo "<script>top.ICEcoder.message('Sorry can\\'t delete\\n".$filesArray[$i]."');</script>";
  113. }
  114. echo '<script>action="nothing";</script>';
  115. }
  116. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  117. }
  118. // The function to recursively remove folders & files
  119. function rrmdir($dir) {
  120. if (is_dir($dir)) {
  121. $objects = scandir($dir);
  122. foreach ($objects as $object) {
  123. if ($object != "." && $object != "..") {
  124. filetype($dir."/".$object) == "dir"
  125. ? rrmdir($dir."/".$object)
  126. : unlink($dir."/".$object);
  127. }
  128. }
  129. reset($objects);
  130. rmdir($dir);
  131. }
  132. }
  133. if ($_GET['action']=="save") {
  134. echo '<script>action="save";</script>';
  135. // on the form posting via a reload, save the file
  136. if (isset($_POST['contents'])) {
  137. if ((file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && $_POST['newFileName']!="") {
  138. if (filemtime($file)==$_GET['fileMDT']||!(isset($_GET['fileMDT']))) {
  139. $fh = fopen($file, 'w') or die("Sorry, cannot save");
  140. fwrite($fh, $_POST['contents']);
  141. fclose($fh);
  142. clearstatcache();
  143. echo '<script>top.ICEcoder.openFileMDTs[top.ICEcoder.selectedTab-1]="'.filemtime($file).'";</script>';
  144. // Reload file manager & rename tab if it was a new file
  145. if (isset($_POST['newFileName']) && $_POST['newFileName']!="") {
  146. echo '<script>top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.$fileLoc.'\',\''.$fileName.'\');';
  147. echo 'top.ICEcoder.renameTab(top.ICEcoder.selectedTab,\''.$fileLoc."/".$fileName.'\');</script>';
  148. }
  149. // Reload stickytab window
  150. echo '<script>if (top.ICEcoder.stickyTab.location) {top.ICEcoder.stickyTab.location.reload()};action="doneSave";</script>';
  151. } else {
  152. $loadedFile = file_get_contents($file);
  153. echo '<textarea name="loadedFile" id="loadedFile">'.str_replace("</textarea>","<ICEcoder:/:textarea>",htmlentities($loadedFile)).'</textarea>';
  154. echo '<textarea name="userVersionFile" id="userVersionFile"></textarea>';
  155. ?>
  156. <script>
  157. 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?');
  158. if (refreshFile) {
  159. var cM = top.ICEcoder.getcMInstance();
  160. var thisTab = top.ICEcoder.selectedTab;
  161. document.getElementById('userVersionFile').value = cM.getValue();
  162. // Revert back to original
  163. cM.setValue(document.getElementById('loadedFile').value);
  164. top.ICEcoder.changedContent[thisTab-1] = 0;
  165. top.ICEcoder.openFileMDTs[top.ICEcoder.selectedTab-1] = "<?php echo filemtime($file); ?>";
  166. cM.clearHistory();
  167. // Now for the new file
  168. top.ICEcoder.newTab();
  169. cM = top.ICEcoder.getcMInstance();
  170. cM.setValue(document.getElementById('userVersionFile').value);
  171. cM.clearHistory();
  172. // Finally, switch back to original tab
  173. top.ICEcoder.switchTab(thisTab);
  174. }
  175. action='nothing';
  176. </script>
  177. <?php
  178. }
  179. } else {
  180. echo "<script>action='nothing';top.ICEcoder.message('Sorry, cannot write\\n".$file."')</script>";
  181. }
  182. echo '<script>top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);</script>';
  183. }
  184. };
  185. ?>
  186. <script>
  187. if (action=="load") {
  188. if (fileType=="text") {
  189. setTimeout(function() {
  190. if (!top.ICEcoder.content.contentWindow.createNewCMInstance) {
  191. console.log('There was tech hiccup, likely something wasn\'t quite ready. So ICEcoder reloaded it\'s file control again.');
  192. window.location.reload();
  193. } else {
  194. top.ICEcoder.loadingFile = true;
  195. // Reset the various states back to their initial setting
  196. selectedTab = top.ICEcoder.openFiles.length; // The tab that's currently selected
  197. // Finally, store all data, show tabs etc
  198. top.ICEcoder.createNewTab();
  199. top.ICEcoder.cMInstances.push(top.ICEcoder.nextcMInstance);
  200. top.ICEcoder.setLayout();
  201. top.ICEcoder.content.contentWindow.createNewCMInstance(top.ICEcoder.nextcMInstance);
  202. // 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)
  203. top.ICEcoder.switchMode();
  204. cM = top.ICEcoder.getcMInstance();
  205. cM.setValue(document.getElementById('loadedFile').value);
  206. top.document.getElementById('content').style.visibility='visible';
  207. top.ICEcoder.switchTab(top.ICEcoder.selectedTab);
  208. cM.focus();
  209. // Then clean it up, set the text cursor, update the display and get the character data
  210. top.ICEcoder.contentCleanUp();
  211. top.ICEcoder.content.contentWindow['cM'+top.ICEcoder.cMInstances[top.ICEcoder.selectedTab-1]].setLineClass(top.ICEcoder['cMActiveLine'+top.ICEcoder.selectedTab], null);
  212. top.ICEcoder['cMActiveLine'+top.ICEcoder.selectedTab] = top.ICEcoder.content.contentWindow['cM'+top.ICEcoder.cMInstances[top.ICEcoder.selectedTab-1]].setLineClass(0, "cm-s-activeLine");
  213. top.ICEcoder.nextcMInstance++;
  214. top.ICEcoder.openFileMDTs.push('<?php echo filemtime($file); ?>');
  215. top.ICEcoder.loadingFile = false;
  216. }
  217. },4);
  218. }
  219. if (fileType=="image") {
  220. top.document.getElementById('blackMask').style.visibility = "visible";
  221. top.document.getElementById('mediaContainer').innerHTML = "<img src=\"<?php echo $fileLoc."/".$fileName;?>\" class=\"whiteGlow\" style=\"border: solid 10px #fff; max-width: 700px; max-height: 500px\" 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>";
  222. }
  223. top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);
  224. }
  225. </script>
  226. <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">
  227. <textarea name="contents"></textarea>
  228. <input type="hidden" name="newFileName" value="">
  229. </form>
  230. <script>
  231. if (action=="save") {
  232. <?php
  233. if (strpos($file,"[NEW]")>0||$saveType=="saveAs") {
  234. if (strpos($fileName,"[NEW]")>0) {echo "fileLoc = '".$fileLoc."';";} else {echo "fileLoc = '';";};
  235. ?>
  236. newFileName = top.ICEcoder.getInput(fileLoc != ""
  237. ? 'Enter filename to save at '+fileLoc
  238. : 'Enter filename (including path, prefixed with /)'
  239. ,'');
  240. if (newFileName.substr(0,1)!="/") {newFileName = "/" + newFileName}
  241. if (newFileName) {
  242. newFileName = fileLoc == "" ? newFileName : fileLoc + "/" + fileName;
  243. }
  244. if (newFileName && top.document.getElementById('filesFrame').contentWindow.document.getElementById(newFileName.replace(/\//g,"|"))) {
  245. overwriteOK = top.ICEcoder.ask('That file exists already, overwrite?');
  246. }
  247. document.saveFile.newFileName.value = '<?php echo $docRoot; ?>' + newFileName;
  248. <?php ;};?>
  249. if ("undefined" == typeof newFileName || (newFileName && "undefined" == typeof overwriteOK) || ("undefined" != typeof overwriteOK && overwriteOK)) {
  250. top.ICEcoder.serverMessage('<b>Saving</b><br>'+ <?php echo strpos($file,"[NEW]")>0 ? "newFileName" : "'$file'"; ?>);
  251. document.saveFile.contents.value = top.document.getElementById('saveTemp1').value;
  252. document.saveFile.submit();
  253. } else {
  254. top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);
  255. action=="nothing";
  256. }
  257. }
  258. </script>
  259. <script>
  260. if (action=="doneSave") {
  261. top.ICEcoder.changedContent[top.ICEcoder.selectedTab-1] = 0;
  262. top.ICEcoder.redoTabHighlight(top.ICEcoder.selectedTab);
  263. }
  264. </script>