diff --git a/lib/file-control-xhr.php b/lib/file-control-xhr.php
new file mode 100644
index 0000000..4e185aa
--- /dev/null
+++ b/lib/file-control-xhr.php
@@ -0,0 +1,279 @@
+0||$saveType=="saveAs") {
+ $finalAction = strpos($fileOrig,"[NEW]")>0 ? "save as" : "save";
+ $fileURLPart = isset($file) ? $file : "";
+ $fileMDTURLPart = isset($_GET["fileMDT"]) && $_GET["fileMDT"]!="undefined" ? "&fileMDT=".numClean($_GET['fileMDT']) : "";
+ $doNext = '
+ top.ICEcoder.serverMessage();
+ fileLoc = "'.$fileLoc.'";
+ newFileName = top.ICEcoder.getInput("'.$t['Enter filename to...'].' "+(fileLoc!="" ? fileLoc : "/"),"");
+ if (newFileName) {
+ if (newFileName.substr(0,1)!="/") {newFileName = "/" + newFileName};
+ newFileName = fileLoc + newFileName;
+ if (top.document.getElementById("filesFrame").contentWindow.document.getElementById(newFileName.replace(/\\\//g,"|"))) {
+ overwriteOK = top.ICEcoder.ask("'.$t['That file exists...'].'");
+ }
+ };
+
+ if ("undefined" == typeof newFileName || (newFileName && "undefined" == typeof overwriteOK) || ("undefined" != typeof overwriteOK && overwriteOK)) {
+ saveURL = "lib/file-control-xhr.php?action=save&file='.$fileURLPart.$fileMDTURLPart.'&csrf='.$_GET["csrf"].'";
+
+ var xhr = top.ICEcoder.xhrObj();
+
+ xhr.onreadystatechange=function() {
+ if (xhr.readyState==4 && xhr.status==200) {
+ /* console.log(xhr.responseText); */
+ var statusArray = JSON.parse(xhr.responseText);
+ /* console.log(statusArray); */
+
+ if (statusArray.status.error) {
+ top.ICEcoder.message(statusArray.status.errorMsg);
+ } else {
+ eval(statusArray.action.doNext);
+ }
+
+
+ }
+ };
+ /* console.log(\'Calling \'+saveURL+\' via XHR\'); */
+ xhr.open("POST",saveURL,true);
+ xhr.setRequestHeader(\'Content-type\', \'application/x-www-form-urlencoded\');
+ xhr.send(\'newFileName=\'+newFileName+\'&contents=\'+top.document.getElementById(\'saveTemp1\').value);
+ top.ICEcoder.serverMessage("'.$t['Saving'].'
" + "'.($finalAction == "Save" ? "newFileName" : "'".$fileName."'").'");
+ } else {
+ top.ICEcoder.serverMessage();top.ICEcoder.serverQueue("del",0);
+ }';
+
+ // ===================
+ // FILE CONTENT SAVING
+ // ===================
+
+ } elseif (isset($_POST['contents'])) {
+ $finalAction = isset($_POST["newFileName"]) ? "save as" : "save";
+
+ // =================
+ // FILE IS WRITEABLE
+ // =================
+
+ if (!$demoMode && ((file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && $_POST['newFileName']!="")) {
+ $filemtime = $serverType=="Linux" ? filemtime($file) : "1000000";
+
+ // =======================
+ // MDT'S MATCH, WRITE FILE
+ // =======================
+
+ if (!(isset($_GET['fileMDT']))||$filemtime==$_GET['fileMDT']) {
+ // Newly created files have the perms set too
+ $setPerms = (!file_exists($file)) ? true : false;
+ $fh = fopen($file, 'w') or die($t['Sorry, cannot save']);
+ // replace \r\n (Windows), \r (old Mac) and \n (Linux) line endings with whatever we chose to be lineEnding
+ $contents = $_POST['contents'];
+ $contents = str_replace("\r\n", $ICEcoder["lineEnding"], $contents);
+ $contents = str_replace("\r", $ICEcoder["lineEnding"], $contents);
+ $contents = str_replace("\n", $ICEcoder["lineEnding"], $contents);
+ // Now write that content, close the file and clear the statcache
+ fwrite($fh, $contents);
+ fclose($fh);
+ if ($setPerms) {
+ chmod($file,octdec($ICEcoder['newFilePerms']));
+ }
+ clearstatcache();
+ $filemtime = $serverType=="Linux" ? filemtime($file) : "1000000";
+ $doNext .= 'top.ICEcoder.openFileMDTs[top.ICEcoder.selectedTab-1]="'.$filemtime.'";';
+ // Reload file manager, rename tab & remove old file highlighting if it was a new file
+ if (isset($_POST['newFileName']) && $_POST['newFileName']!="") {
+ $doNext .= 'top.ICEcoder.selectedFiles=[];top.ICEcoder.updateFileManagerList(\'add\',\''.$fileLoc.'\',\''.$fileName.'\',false,false,false,\'file\');';
+ $doNext .= 'top.ICEcoder.renameTab(top.ICEcoder.selectedTab,\''.$fileLoc."/".$fileName.'\');';
+ if (!strpos($_GET['file'],"[NEW]")) {
+ // We're saving as a new file, so unhighlight the old name in the file manager if visible
+ $doNext .= "fileLink = top.ICEcoder.filesFrame.contentWindow.document.getElementById('".str_replace("/","|",$fileLoc)."|".basename($_GET['file'])."');";
+ $doNext .= "if (fileLink) {fileLink.style.backgroundColor = top.ICEcoder.tabBGnormal; fileLink.style.color = top.ICEcoder.tabFGnormalFile};";
+ }
+ }
+ // Reload previewWindow window if not a Markdown file
+ // In doing this, we check on an interval for the page to be complete and if we last saw it loading
+ // When we are done loading, so set the loading status to false and load plugins ontop...
+ $doNext .= 'if (top.ICEcoder.previewWindow.location && top.ICEcoder.previewWindow.location.pathname.indexOf(".md")==-1) {
+ top.ICEcoder.previewWindowLoading = false;
+ top.ICEcoder.previewWindow.location.reload(true);
+
+ top.ICEcoder.checkPreviewWindowLoadingInt = setInterval(function() {
+ if (top.ICEcoder.previewWindow.document.readyState != "loading" && top.ICEcoder.previewWindowLoading) {
+ top.ICEcoder.previewWindowLoading = false;
+ try {top.ICEcoder.doPesticide();} catch(err) {};
+ try {top.ICEcoder.doStatsJS(\'save\');} catch(err) {};
+ clearInterval(top.ICEcoder.checkPreviewWindowLoadingInt);
+ } else {
+ top.ICEcoder.previewWindowLoading = top.ICEcoder.previewWindow.document.readyState == "loading" ? true : false;
+ }
+ },4);
+
+ };';
+
+
+ // Finally, set previous files, indicate changes, set saved points and redo tabs
+ $doNext .= '
+ top.ICEcoder.setPreviousFiles();
+ setTimeout(function(){top.ICEcoder.indicateChanges()},4);
+ cM = top.ICEcoder.getcMInstance();
+ top.ICEcoder.savedPoints[top.ICEcoder.selectedTab-1] = cM.changeGeneration();
+ top.ICEcoder.redoTabHighlight(top.ICEcoder.selectedTab);';
+
+ // Run our custom processes
+ include_once("../processes/on-file-save.php");
+
+ // ======================================================
+ // MDT'S DON'T MATCH, OFFER TO LOAD FILE & SHOW DIFF VIEW
+ // ======================================================
+
+ } else {
+ $loadedFile = toUTF8noBOM(file_get_contents($file,false,$context),true);
+ echo '","",htmlentities($loadedFile)).'';
+ echo '