editor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /*NotepadA editor.php
  3. Special modification was made to the script and make the ace editor to work with AOB system.
  4. This is just part of the system and require php scripts outside this folder to work.
  5. ** DO NOT EDIT THIS PAGE WITH NOTEPADA ONLINE EDITOR. **
  6. ** DO NOT EDIT THIS PAGE WITH NOTEPADA ONLINE EDITOR. **
  7. ** DO NOT EDIT THIS PAGE WITH NOTEPADA ONLINE EDITOR. **
  8. */
  9. include '../../auth.php';
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="en">
  13. <!-- Special modification for NotepadA system, ArOZ Online Beta-->
  14. <head>
  15. <?php
  16. //Init of the editor
  17. function mv($var){
  18. if (isset($_GET[$var]) == true && $_GET[$var] != ""){
  19. return $_GET[$var];
  20. }else{
  21. return "";
  22. }
  23. }
  24. //Default value
  25. $filename = "";
  26. $displayName = "";
  27. $fileExt = "";
  28. $theme = "tomorrow_night";
  29. $mode = "php";
  30. $fontsize = "12";
  31. //check if a correct filename has been provided
  32. if (mv("filename") != ""){
  33. //Editing an existed file
  34. $filename = mv("filename");
  35. if (file_exists(dirname($filename)) == false){
  36. //If the file that should be containing this file is not fonund
  37. echo "<code>Requested Directory: " . dirname($filename) . '<br>';
  38. echo "<script src='../../script/jquery.min.js'></script><script>$('body').css('background-color','white');function checkIsSaved(){return true;}</script>";
  39. $date = new DateTime(); //this returns the current date time
  40. $result = $date->format('Y-m-d H:i:s');
  41. die("Error. Target folder was not found on the host device. Please close this tab and reopen if needed.<br><br><hr>Request Time: " . $result . '</code>');
  42. }
  43. if (!file_exists($filename)){
  44. //File not found, creating a new one instead
  45. $file = fopen($filename, 'w') or die('Error opening file: ' + $filename);
  46. fclose($file);
  47. }
  48. $displayName = $filename;
  49. $fileExt = pathinfo($filename, PATHINFO_EXTENSION);
  50. //Handle the AOB Upload Manger type of naming methods
  51. if (substr($filename,0,5) == "inith"){
  52. $nameOnly = basename($filename, "." . $fileExt);
  53. $nameOnly = str_replace("inith","",$nameOnly);
  54. $displayName = hex2bin($nameOnly) . "." . $fileExt . " (Encoded Filename)";
  55. }else{
  56. $displayName = basename($filename);
  57. }
  58. //Set the mode of the editor to the extension of the file, but something this might be incorrect.
  59. $mode = $fileExt;
  60. }else{
  61. //Creating a new file instead
  62. $filename = "";
  63. $displayName = "untitled";
  64. }
  65. if (mv("theme") != ""){
  66. $theme = mv("theme");
  67. }
  68. if (mv("fontsize") != ""){
  69. $fontsize = mv("fontsize");
  70. }
  71. ?>
  72. <meta charset="UTF-8">
  73. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  74. <title><?php echo "Editing " . $displayName;?></title>
  75. <script src="../../script/jquery.min.js"></script>
  76. <style type="text/css" media="screen">
  77. body {
  78. overflow: hidden;
  79. padding-bottom:5px;
  80. background-color:#2b2b2b;
  81. }
  82. #editor {
  83. margin: 0;
  84. position: absolute;
  85. top: 0;
  86. bottom: 15px;
  87. left: 0;
  88. right: 0;
  89. }
  90. </style>
  91. </head>
  92. <body>
  93. <pre id="editor"><?php
  94. if ($filename != ""){
  95. echo str_replace("<","&lt;",file_get_contents($filename));
  96. }
  97. ?></pre>
  98. <script src="src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
  99. <script>
  100. var filename = "<?php echo $filename;?>";
  101. var filepath = "<?php echo str_replace("\\",'/',realpath($filename));?>";
  102. var lastSave = "";
  103. //Init editor
  104. var editor = ace.edit("editor");
  105. editor.setTheme("ace/theme/<?php echo $theme;?>");
  106. var detectMode = getMode(filepath);
  107. //console.log("[NotepadA] Chaning mode to " + detectMode.toLowerCase());
  108. if ( detectMode != undefined){
  109. editor.session.setMode("ace/mode/" + detectMode.toLowerCase());
  110. }else{
  111. //Default mode: php
  112. editor.session.setMode("ace/mode/php");
  113. }
  114. editor.setOptions({
  115. //fontFamily: "tahoma",
  116. fontSize: "<?php echo $fontsize;?>pt"
  117. });
  118. $(document).ready(function(){
  119. //Initial assume saved
  120. lastSave = editor.getValue();
  121. });
  122. //Listener for Ctrl+S
  123. $(window).keypress(function(event) {
  124. if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
  125. //console.log(filepath);
  126. Save();
  127. event.preventDefault();
  128. return false;
  129. });
  130. $(document).on("click","#editor",function() {
  131. //Hide the parent window in NotepadA condition
  132. window.parent.hideToggleMenu();
  133. });
  134. function Print() {
  135. try {
  136. var printWindow = window.open("", "", "height=400,width=800");
  137. printWindow.document.write("<html><head><title>NotepadA Print Window</title>");
  138. printWindow.document.write("</head><xmp>");
  139. printWindow.document.write("filename: " + filename + " \nprint-time: " + new Date().toLocaleString() + "\n");
  140. printWindow.document.write(editor.getSession().getDocument().getValue());
  141. printWindow.document.write("</xmp></html>");
  142. printWindow.document.close();
  143. printWindow.print();
  144. }catch (ex) {
  145. console.error("Error: " + ex.message);
  146. }
  147. }
  148. //Absolute path is used for saving. So no need to worry about the relative path over php issues
  149. function Save(){
  150. code = editor.getValue();
  151. $.post("../writeCode.php", { filename: filepath, content: code })
  152. .done(function( data ) {
  153. if (data.includes("ERROR") == false){
  154. console.log("%c[NotepadA] " + filename + " -> Saved!","color:#1265ea");
  155. lastSave = code;
  156. }else{
  157. console.log(data);
  158. }
  159. });
  160. }
  161. function getDocWidth(){
  162. return $(document).width();
  163. }
  164. function checkIsSaved(){
  165. if (lastSave == editor.getValue()){
  166. return true;
  167. }else{
  168. return false;
  169. }
  170. }
  171. function insertChar(text){
  172. editor.session.insert(editor.getCursorPosition(), text);
  173. }
  174. function getEditorContenet(){
  175. return editor.getValue();
  176. }
  177. function getFilepath(){
  178. return (filename.replace("../",""));
  179. }
  180. function openInNewTab(){
  181. if (filename.substring(0,14) == "/media/storage"){
  182. window.open("../SystemAOB/functions/extDiskAccess.php?file=" + filename);
  183. }else{
  184. window.open(filename.replace("../",""));
  185. }
  186. }
  187. function getSelectedText(){
  188. return editor.getSession().doc.getTextRange(editor.selection.getRange());
  189. }
  190. function insertGivenText(text){
  191. editor.session.insert(editor.getCursorPosition(), text);
  192. }
  193. function callUndo(){
  194. editor.getSession().getUndoManager().undo();
  195. }
  196. function callRedo(){
  197. editor.getSession().getUndoManager().redo();
  198. }
  199. var nameOverrides = {
  200. ObjectiveC: "Objective-C",
  201. CSharp: "C#",
  202. golang: "Go",
  203. C_Cpp: "C and C++",
  204. Csound_Document: "Csound Document",
  205. Csound_Orchestra: "Csound",
  206. Csound_Score: "Csound Score",
  207. coffee: "CoffeeScript",
  208. HTML_Ruby: "HTML (Ruby)",
  209. HTML_Elixir: "HTML (Elixir)",
  210. FTL: "FreeMarker"
  211. };
  212. function startSearchBox(){
  213. editor.execCommand("find");
  214. }
  215. function getMode(filePath){
  216. var ext = filePath.split(".").pop();
  217. var supportedModes = {
  218. ABAP: ["abap"],
  219. ABC: ["abc"],
  220. ActionScript:["as"],
  221. ADA: ["ada|adb"],
  222. Apache_Conf: ["^htaccess|^htgroups|^htpasswd|^conf|htaccess|htgroups|htpasswd"],
  223. AsciiDoc: ["asciidoc|adoc"],
  224. ASL: ["dsl|asl"],
  225. Assembly_x86:["asm|a"],
  226. AutoHotKey: ["ahk"],
  227. BatchFile: ["bat|cmd"],
  228. Bro: ["bro"],
  229. C_Cpp: ["cpp|c|cc|cxx|h|hh|hpp|ino"],
  230. C9Search: ["c9search_results"],
  231. Cirru: ["cirru|cr"],
  232. Clojure: ["clj|cljs"],
  233. Cobol: ["CBL|COB"],
  234. coffee: ["coffee|cf|cson|^Cakefile"],
  235. ColdFusion: ["cfm"],
  236. CSharp: ["cs"],
  237. Csound_Document: ["csd"],
  238. Csound_Orchestra: ["orc"],
  239. Csound_Score: ["sco"],
  240. CSS: ["css"],
  241. Curly: ["curly"],
  242. D: ["d|di"],
  243. Dart: ["dart"],
  244. Diff: ["diff|patch"],
  245. Dockerfile: ["^Dockerfile"],
  246. Dot: ["dot"],
  247. Drools: ["drl"],
  248. Edifact: ["edi"],
  249. Eiffel: ["e|ge"],
  250. EJS: ["ejs"],
  251. Elixir: ["ex|exs"],
  252. Elm: ["elm"],
  253. Erlang: ["erl|hrl"],
  254. Forth: ["frt|fs|ldr|fth|4th"],
  255. Fortran: ["f|f90"],
  256. FTL: ["ftl"],
  257. Gcode: ["gcode"],
  258. Gherkin: ["feature"],
  259. Gitignore: ["^.gitignore"],
  260. Glsl: ["glsl|frag|vert"],
  261. Gobstones: ["gbs"],
  262. golang: ["go"],
  263. GraphQLSchema: ["gql"],
  264. Groovy: ["groovy"],
  265. HAML: ["haml"],
  266. Handlebars: ["hbs|handlebars|tpl|mustache"],
  267. Haskell: ["hs"],
  268. Haskell_Cabal: ["cabal"],
  269. haXe: ["hx"],
  270. Hjson: ["hjson"],
  271. HTML: ["html|htm|xhtml|vue|we|wpy"],
  272. HTML_Elixir: ["eex|html.eex"],
  273. HTML_Ruby: ["erb|rhtml|html.erb"],
  274. INI: ["ini|conf|cfg|prefs"],
  275. Io: ["io"],
  276. Jack: ["jack"],
  277. Jade: ["jade|pug"],
  278. Java: ["java"],
  279. JavaScript: ["js|jsm|jsx"],
  280. JSON: ["json"],
  281. JSONiq: ["jq"],
  282. JSP: ["jsp"],
  283. JSSM: ["jssm|jssm_state"],
  284. JSX: ["jsx"],
  285. Julia: ["jl"],
  286. Kotlin: ["kt|kts"],
  287. LaTeX: ["tex|latex|ltx|bib"],
  288. LESS: ["less"],
  289. Liquid: ["liquid"],
  290. Lisp: ["lisp"],
  291. LiveScript: ["ls"],
  292. LogiQL: ["logic|lql"],
  293. LSL: ["lsl"],
  294. Lua: ["lua"],
  295. LuaPage: ["lp"],
  296. Lucene: ["lucene"],
  297. Makefile: ["^Makefile|^GNUmakefile|^makefile|^OCamlMakefile|make"],
  298. Markdown: ["md|markdown"],
  299. Mask: ["mask"],
  300. MATLAB: ["matlab"],
  301. Maze: ["mz"],
  302. MEL: ["mel"],
  303. MIXAL: ["mixal"],
  304. MUSHCode: ["mc|mush"],
  305. MySQL: ["mysql"],
  306. Nix: ["nix"],
  307. NSIS: ["nsi|nsh"],
  308. ObjectiveC: ["m|mm"],
  309. OCaml: ["ml|mli"],
  310. Pascal: ["pas|p"],
  311. Perl: ["pl|pm"],
  312. pgSQL: ["pgsql"],
  313. PHP: ["php|phtml|shtml|php3|php4|php5|phps|phpt|aw|ctp|module"],
  314. Pig: ["pig"],
  315. Powershell: ["ps1"],
  316. Praat: ["praat|praatscript|psc|proc"],
  317. Prolog: ["plg|prolog"],
  318. Properties: ["properties"],
  319. Protobuf: ["proto"],
  320. Python: ["py"],
  321. R: ["r"],
  322. Razor: ["cshtml|asp"],
  323. RDoc: ["Rd"],
  324. Red: ["red|reds"],
  325. RHTML: ["Rhtml"],
  326. RST: ["rst"],
  327. Ruby: ["rb|ru|gemspec|rake|^Guardfile|^Rakefile|^Gemfile"],
  328. Rust: ["rs"],
  329. SASS: ["sass"],
  330. SCAD: ["scad"],
  331. Scala: ["scala"],
  332. Scheme: ["scm|sm|rkt|oak|scheme"],
  333. SCSS: ["scss"],
  334. SH: ["sh|bash|^.bashrc"],
  335. SJS: ["sjs"],
  336. Smarty: ["smarty|tpl"],
  337. snippets: ["snippets"],
  338. Soy_Template:["soy"],
  339. Space: ["space"],
  340. SQL: ["sql"],
  341. SQLServer: ["sqlserver"],
  342. Stylus: ["styl|stylus"],
  343. SVG: ["svg"],
  344. Swift: ["swift"],
  345. Tcl: ["tcl"],
  346. Tex: ["tex"],
  347. Text: ["txt"],
  348. Textile: ["textile"],
  349. Toml: ["toml"],
  350. TSX: ["tsx"],
  351. Twig: ["twig|swig"],
  352. Typescript: ["ts|typescript|str"],
  353. Vala: ["vala"],
  354. VBScript: ["vbs|vb"],
  355. Velocity: ["vm"],
  356. Verilog: ["v|vh|sv|svh"],
  357. VHDL: ["vhd|vhdl"],
  358. Wollok: ["wlk|wpgm|wtest"],
  359. XML: ["xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl|xaml"],
  360. XQuery: ["xq"],
  361. YAML: ["yaml|yml"],
  362. Django: ["html"]
  363. };
  364. for (var key in supportedModes) {
  365. if (supportedModes.hasOwnProperty(key)) {
  366. var thisExtension = supportedModes[key][0].split("|");
  367. if (thisExtension.length == 1){
  368. if (ext == thisExtension[0]){
  369. return key;
  370. }
  371. }else{
  372. for(var i =0; i < thisExtension.length;i++){
  373. if (ext == thisExtension[i]){
  374. return key;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. $(window).bind('keydown', function(event) {
  382. if (event.ctrlKey || event.metaKey) {
  383. switch (String.fromCharCode(event.which).toLowerCase()) {
  384. case 's':
  385. event.preventDefault();
  386. Save();
  387. break;
  388. }
  389. }
  390. });
  391. </script>
  392. </body>
  393. </html>