myCodeMirrorPHP=null,dialogShare=null,dialogPwd=null,dialogAdded=null;const dropZonePHP=document.getElementById("dropZonePHP");function outputChange(){["raw","html"].forEach(function(e){document.getElementById("results-"+e).style.display=document.getElementById("output").value==e?"":"none"})}function check(){document.getElementById("results-usage").style.display="none",document.getElementById("results-html").innerHTML="Execution in progress ... ",document.getElementById("results-raw").innerHTML="Execution in progress ... ",document.getElementById("results-usage").style.display="",document.getElementById("results-memory").innerHTML="",document.getElementById("results-ctime").innerHTML="",document.getElementById("results-elapsed").innerHTML="";let e=myCodeMirrorPHP.getValue("\n");services.http.post(myDomainAPI+"/php/"+document.getElementById("version").value,e,function(e){try{e=JSON.parse(e),document.getElementById("results-html").innerHTML=e.stdout,document.getElementById("results-raw").innerHTML=e.stdout,e&&e.usage&&(document.getElementById("results-usage").style.display="",document.getElementById("results-memory").innerHTML=e.usage.memory?function e(t){var n=-1;do t/=1024,n++;while(t>1024);return Math.max(t,.1).toFixed(1)+[" kB"," MB"," GB"," TB","PB","EB","ZB","YB"][n]}(e.usage.memory):"",document.getElementById("results-ctime").innerHTML=e.usage.ctime?e.usage.ctime:"",document.getElementById("results-elapsed").innerHTML=e.usage.elapsed?e.usage.elapsed:"")}catch(t){document.getElementById("results-html").innerHTML=e,document.getElementById("results-raw").innerHTML=e}},function(e){e&&408==e.status?(document.getElementById("results-html").innerHTML="Timeout! Your PHP code is taking too long.",document.getElementById("results-raw").innerHTML="Timeout! Your PHP code is taking too long."):(document.getElementById("results-html").innerHTML="An error has occured. Please, try later.",document.getElementById("results-raw").innerHTML="An error has occured. Please, try later.")})}function PHPPwd(e,t){["save","update","fork","delete"].forEach(function(t){document.getElementById("message-"+t).style.display=e==t?"":"none"}),currentAction=t,dialogPwd.show()}function doIt(){if(""==document.getElementById("pwd").value){alert("You must fill password input");return}dialogPwd.hide(),currentAction()}function getData(){return{version:document.getElementById("version").value,output:document.getElementById("output").value,code:pako.deflate(myCodeMirrorPHP.getValue("\n"),{to:"string"})}}function forkPHP(){let e=document.getElementById("pwd").value,t=getData();var n=new XMLHttpRequest;n.open("POST",myDomain+"/share/php",!0),n.setRequestHeader("Content-type","application/json"),n.setRequestHeader("Security-key",e),n.onreadystatechange=function(){if(4===this.readyState){var e=JSON.parse(this.responseText);201!==this.status?e&&e.message?alert(e.message):alert("An error has occured. Please, try later."):(history.pushState({},"",prefixLang+"/php-bin/"+e.id),document.getElementById("link-id").value=myDomain+prefixLang+"/php-bin/"+e.id,document.getElementById("link-id").select(),document.getElementById("php-save").style.display="none",document.getElementById("message-save").style.display="none",document.getElementById("message-update").style.display="",document.getElementById("message-delete").style.display="",document.getElementById("php-update").style.display="",document.getElementById("php-delete").style.display="",document.getElementById("php-fork").style.display="",dialogAdded.show())}},n.send(JSON.stringify(t))}function savePHP(){try{let e=document.getElementById("pwd").value,t=getData();var n=new XMLHttpRequest;n.open("POST",myDomain+"/share/php",!0),n.setRequestHeader("Content-type","application/json"),n.setRequestHeader("Security-key",e),n.onreadystatechange=function(){if(4===this.readyState){var e=JSON.parse(this.responseText);201!==this.status?e&&e.message?alert(e.message):alert("An error has occured. Please, try later."):(history.pushState({},"",prefixLang+"/php-bin/"+e.id),document.getElementById("link-id").value=myDomain+prefixLang+"/php-bin/"+e.id,document.getElementById("link-id").select(),document.getElementById("php-save").style.display="none",document.getElementById("message-save").style.display="none",document.getElementById("message-update").style.display="",document.getElementById("message-delete").style.display="",document.getElementById("php-update").style.display="",document.getElementById("php-delete").style.display="",document.getElementById("php-fork").style.display="",dialogAdded.show())}},n.send(JSON.stringify(t))}catch(s){console.log(s)}}function updatePHP(){let e=document.getElementById("pwd").value,t=getData();t.id=getAnchor();var n=new XMLHttpRequest;n.open("PUT",myDomain+"/share/php/"+t.id,!0),n.setRequestHeader("Content-type","application/json"),n.setRequestHeader("Security-key",e),n.onreadystatechange=function(){if(4===this.readyState){if(200!==this.status){var e=JSON.parse(this.responseText);e.message?alert(e.message):alert("An error has occured. Please, try later.")}else alert("Your PHP snippet has been updated.")}},n.send(JSON.stringify(t))}function deletePHP(){var e=document.getElementById("pwd").value,t=this.getAnchor(),n=new XMLHttpRequest;n.open("DELETE",myDomain+"/share/php/"+t,!0),n.setRequestHeader("Content-type","application/json"),n.setRequestHeader("Security-key",e),n.onreadystatechange=function(){if(4===this.readyState){if(200!==this.status){var e=JSON.parse(this.responseText);e.message?alert(e.message):alert("An error has occured. Please, try later.")}else alert("Your PHP snippet has been deleted."),document.location.href=myDomain+prefixLang+"/php.html"}},n.send()}function getAnchor(){return shareId}function init(){let e=getAnchor();if(null!==e){document.getElementById("php-save").style.display="none",document.getElementById("message-save").style.display="none";let t=function(e,t){t?(alert(t),document.getElementById("php-save").style.display="",document.getElementById("message-save").style.display="",document.getElementById("message-update").style.display="none",document.getElementById("message-delete").style.display="none",document.getElementById("php-update").style.display="none",document.getElementById("php-delete").style.display="none",document.getElementById("php-fork").style.display="none"):(myCodeMirrorPHP.setValue(pako.inflate(e.code,{to:"string"})),e.version&&(document.getElementById("version").value=e.version),e.output&&(document.getElementById("output").value=e.output))};var n=new XMLHttpRequest;n.open("GET",myDomain+"/share/php/"+e,!0),n.setRequestHeader("Content-type","application/json"),n.onreadystatechange=function(){if(4===this.readyState){var e=null,n=JSON.parse(this.responseText);200!==this.status&&(e=n.message?n.message:"An error has occured. Please, try later."),t(n,e)}},n.send()}else document.getElementById("message-update").style.display="none",document.getElementById("message-delete").style.display="none",document.getElementById("php-update").style.display="none",document.getElementById("php-delete").style.display="none",document.getElementById("php-fork").style.display="none"}function closeMenu(e=null){document.querySelectorAll(".sub-menu ul").forEach(function(t){e&&e===t.parentNode||(t.style.display="none",t.parentNode.querySelectorAll("i.arrow").forEach(function(e){e.classList.remove("up"),e.classList.add("down")}))})}function loadExample(e){switch(e){case"pdo-mysql":myCodeMirrorPHP.setValue("").replace(/</g,"<")+"\n?>"),document.getElementById("output").value="raw";break;case"sodium-keys":myCodeMirrorPHP.setValue("").replace(/</g,"<")+"\n?>"),document.getElementById("output").value="raw",document.getElementById("version").value="8.0.0";break;case"mongodb":myCodeMirrorPHP.setValue("").replace(/</g,"<")+"\n?>"),document.getElementById("output").value="raw",document.getElementById("version").value="7.4.8"}outputChange()}document.getElementById("file-input-php").addEventListener("change",function(e){services.file.readSingleFile(e,ReadModeEnum.TEXT,function(e){myCodeMirrorPHP.setValue(e)})},!1),document.getElementById("output").addEventListener("change",function(e){outputChange()},!1),document.getElementById("menu-check").addEventListener("click",function(e){check()},!1),document.getElementById("browse-php").addEventListener("click",function(e){document.getElementById("file-input-php").click()},!1),document.getElementById("clipboard-php").addEventListener("click",function(e){services.copyToClipBoard(myCodeMirrorPHP.getValue("\n"))},!1),document.getElementById("clipboard-result").addEventListener("click",function(e){services.copyToClipBoard(document.getElementById("results-html").innerHTML)},!1),addEvent(window,"load",function(){myCodeMirrorPHP=services.codeMirror.create("editor-container-php",{matchBrackets:!0,mode:"application/x-httpd-php",lineWrapping:!0,continuousScanning:500,lineNumbers:!0,viewportMargin:1/0}),services.file.dragAndDrop(dropZonePHP,ReadModeEnum.TEXT,function(e){e&&myCodeMirrorPHP.setValue(e)}),dialogShare=new A11yDialog(document.getElementById("dialog-share")),dialogPwd=new A11yDialog(document.getElementById("dialog-pwd")),dialogAdded=new A11yDialog(document.getElementById("dialog-added")),document.getElementById("menu-share").addEventListener("click",function(e){closeMenu(),dialogShare.show()}),init(),myCodeMirrorPHP.on("mousedown",function(e,t){""==myCodeMirrorPHP.getValue("\n")&&myCodeMirrorPHP.setValue("")}),document.getElementById("example-pdo-mysql").addEventListener("click",function(e){loadExample("pdo-mysql")}),document.getElementById("example-sodium-keys").addEventListener("click",function(e){loadExample("sodium-keys")}),outputChange(),document.querySelectorAll(".sub-menu a").forEach(function(e){e.onclick=function(){let t=e.parentNode;for(;t&&!t.classList.contains("sub-menu");)t=t.parentNode;t&&(closeMenu(t),t.querySelectorAll("ul").forEach(function(e){""==e.style.display?e.style.display="none":e.style.display=""}),t.querySelectorAll("i.arrow").forEach(function(e){e.classList.toggle("up"),e.classList.toggle("down")}))}})});