Tiny

A small text editor, allows you to create or edit UTF-8 or Latin-1 encoded files.

Command line interface

Syntax

  1. $ tiny [-e utf8 | latin1 | auto] filename

Arguments

  • -e – (string) Character set encoding. Default: auto; UTF-8 unless latin1 is detected.
  • filename – (file) The file you want to edit.

Example

tiny.php

  1. <?
  2. require("../plugin.php");
  3. if (is_ajax()) {
  4. switch ($_POST['action']) {
  5. case 'cat':
  6. if (!file_exists($_POST['file'])) die("2");
  7. if (!is_file($_POST['file'])) die("3");
  8. $file = file_get_contents($_POST['file']);
  9. switch ($_POST['enc']) {
  10. case 'utf8':
  11. die("1".$file);
  12. break;
  13. case 'latin1':
  14. die("0".utf8_encode($file));
  15. break;
  16. case 'auto':
  17. default:
  18. die(is_utf8($file) ? "1".$file : "0".utf8_encode($file));
  19. break;
  20. }
  21. break;
  22. case 'save':
  23. die(@file_put_contents($_POST['file'], $_POST['utf8']?$_POST['data']:utf8_decode($_POST['data'])) === false ? "-1" : "1");
  24. break;
  25. }
  26. exit;
  27. }
  28. ?>
  29. /**
  30. * Tiny text editor
  31. **/
  32. TinyShell.plugins.tiny = new Class({
  33. description: "Basic text editor",
  34. t : {},
  35. cr : false,
  36. utf8 : true,
  37. force_enc : false,
  38. run : function(t, args) {
  39. this.t = t;
  40. if (args.length) {
  41. if (args[0] == "-e" && args.length > 2) {
  42. this.force_enc = 1;
  43. this.utf8 = args[1].toLowerCase() == 'utf8';
  44. this.file = args[2];
  45. } else this.file = args[0];
  46. this.t.ajax_request(this.open, "<?php echo $_AJAX_URL?>", "action=cat&enc="+(this.force_enc?(this.utf8?"utf8":"latin1"):"auto")+"&file="+encodeURIComponent(this.file));
  47. } else {
  48. this.t.print("tiny: usage: tiny [-e utf8 | latin1] filename");
  49. this.t.resume();
  50. }
  51. },
  52. open: function(r) {
  53. if (r.substring(0,1) == "3") {
  54. this.t.print("tiny: `"+this.file+"': permission denied");
  55. this.t.resume();
  56. return;
  57. }
  58. // force cursor movements
  59. this.onkeydown = function(e) {
  60. switch (e.key) {
  61. case "up":
  62. case "down":
  63. case "tab":
  64. case "enter":
  65. e.shift = true;
  66. break;
  67. case "s": // save
  68. if (e.control) {
  69. e.stop();
  70. this.quit_after_saving = e.shift;
  71. this.save();
  72. return false;
  73. }
  74. break;
  75. case "q": // quit
  76. if (e.control) {
  77. e.stop();
  78. this.quit();
  79. return false;
  80. }
  81. break;
  82. }
  83. return true;
  84. }
  85. // keyboard commands
  86. this.onkeypress = function(e) {
  87. switch (e.key) {
  88. case "s": // save
  89. case "q": // quit
  90. if (e.control) {
  91. // stop event
  92. e.stop();
  93. return false;
  94. }
  95. break;
  96. }
  97. return true;
  98. }
  99. this.t.print('Tiny commands:\nCTRL + S: Save\nCTRL + Q: Quit\nCTRL + SHIFT + S : Save and quit');
  100. if (r.substring(0,1) == "2") {
  101. if (!this.force_enc) this.utf8 = true;
  102. r = "";
  103. this.t.print("`"+this.file+"': New file");
  104. } else {
  105. if (!this.force_enc) this.utf8 = r.substring(0, 1) == 1;
  106. r = r.substring(1);
  107. this.t.print("`"+this.file+"': "+r.length+" bytes read ("+(this.utf8?"UTF-8":"Latin 1")+")");
  108. }
  109. this.t.set_protocol("> ").read_line(this.dummy);
  110. if (r.indexOf("\r") > -1) {
  111. this.cr = true;
  112. r = r.replace(/\r/g, '');
  113. }
  114. this.t.cursor_overwrite(r);
  115. this.t.cursor_move_home();
  116. },
  117. save : function () {
  118. r = this.t.cursor_read_line();
  119. if (this.cr) r = r.replace(/\n/g, "\r\n");
  120. this.bytes_saved = r.length;
  121. this.t.ajax_request(this.save_status, "<?php echo $_AJAX_URL?>", "action=save&utf8="+(this.utf8?"1":"0")+"&file="+encodeURIComponent(this.file)+"&data="+encodeURIComponent(r));
  122. },
  123. save_status : function (r) {
  124. if (r < 1) this.t.print("`"+this.file+"': Permission denied");
  125. else {
  126. this.t.print("`"+this.file+"': "+this.bytes_saved+" bytes written on "+(new Date()).toLocaleTimeString());
  127. if (this.quit_after_saving) this.quit();
  128. }
  129. },
  130. quit : function () {
  131. this.t.resume.delay(100, this.t); // delay to make sure keypress event has fired
  132. },
  133. dummy : function() {
  134. // dummy function
  135. }
  136. });

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>