You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
6.8KB

  1. <?php
  2. use \Ospinto\Dbug;
  3. use \SensioLabs\AnsiConverter\AnsiToHtmlConverter;
  4. abstract class AbstractAction
  5. {
  6. protected $value;
  7. protected $container;
  8. protected $formatter;
  9. public function __construct( $value )
  10. {
  11. $this->value = $value;
  12. $this->setUp();
  13. }
  14. abstract public function decode();
  15. abstract public function setUp();
  16. public function setContainer( ContainerInterface $container )
  17. {
  18. $this->container = $container;
  19. }
  20. public function setFormatter( FormatterInterface $formatter )
  21. {
  22. $this->formatter = $formatter;
  23. }
  24. public function esc_raw()
  25. {
  26. return htmlentities( $this->value );
  27. }
  28. public function __toString()
  29. {
  30. $value = $this->decode();
  31. $formatted = $this->formatter->format( $value );
  32. return $this->container->wrap( $formatted );
  33. }
  34. }
  35. class ActionQuotedPrintableDecode extends AbstractAction
  36. {
  37. public function setUp()
  38. {
  39. $this->setContainer( new TextareaContainer );
  40. $this->setFormatter( new EchoFormatter );
  41. }
  42. public function decode()
  43. {
  44. return quoted_printable_decode( $this->value );
  45. }
  46. }
  47. class ActionQuotedPrintableEncode extends AbstractAction
  48. {
  49. public function setUp()
  50. {
  51. $this->setContainer( new TextareaContainer );
  52. $this->setFormatter( new EchoFormatter );
  53. }
  54. public function decode()
  55. {
  56. return quoted_printable_encode( $this->value );
  57. }
  58. }
  59. class ActionBase64Encode extends AbstractAction
  60. {
  61. public function setUp()
  62. {
  63. $this->setContainer( new TextareaContainer );
  64. $this->setFormatter( new EchoFormatter );
  65. }
  66. public function decode()
  67. {
  68. return base64_encode( $this->value );
  69. }
  70. }
  71. class ActionBase64Decode extends AbstractAction
  72. {
  73. public function setUp()
  74. {
  75. $this->setContainer( new TextareaContainer );
  76. $this->setFormatter( new EchoFormatter );
  77. }
  78. public function decode()
  79. {
  80. return base64_decode( $this->value );
  81. }
  82. }
  83. class ActionUrldecode extends AbstractAction
  84. {
  85. public function setUp()
  86. {
  87. $this->setContainer( new TextareaContainer );
  88. $this->setFormatter( new EchoFormatter );
  89. }
  90. public function decode()
  91. {
  92. return urldecode( $this->value );
  93. }
  94. }
  95. class ActionUrlencode extends AbstractAction
  96. {
  97. public function setUp()
  98. {
  99. $this->setContainer( new TextareaContainer );
  100. $this->setFormatter( new EchoFormatter );
  101. }
  102. public function decode()
  103. {
  104. return urlencode( $this->value );
  105. }
  106. }
  107. class ActionSerialize extends AbstractAction
  108. {
  109. public function setUp()
  110. {
  111. $this->setContainer( new TextareaContainer );
  112. $this->setFormatter( new EchoFormatter );
  113. }
  114. public function decode()
  115. {
  116. return serialize( json_decode( $this->value ) );
  117. }
  118. }
  119. class ActionUnserialize extends AbstractAction
  120. {
  121. public function setUp()
  122. {
  123. $this->setContainer( new DivContainer );
  124. $this->setFormatter( new DbugFormatter );
  125. }
  126. public function decode()
  127. {
  128. return unserialize( $this->value );
  129. }
  130. }
  131. class ActionJsonDecode extends AbstractAction
  132. {
  133. public function setUp()
  134. {
  135. $this->setContainer( new DivContainer );
  136. $this->setFormatter( new VarexportFormatter );
  137. }
  138. public function decode()
  139. {
  140. return json_decode( $this->value, true );
  141. }
  142. }
  143. class ActionJsonEncode extends AbstractAction
  144. {
  145. public function setUp()
  146. {
  147. $this->setContainer( new TextareaContainer );
  148. $this->setFormatter( new EchoFormatter );
  149. }
  150. public function decode()
  151. {
  152. return json_encode( $this->value );
  153. }
  154. }
  155. class ActionDateC extends AbstractAction
  156. {
  157. public function setUp()
  158. {
  159. $this->setContainer( new DivContainer );
  160. $this->setFormatter( new DbugFormatter );
  161. }
  162. public function decode()
  163. {
  164. $results = array();
  165. array_map(function($value) use (&$results) {
  166. $results[$value] = date('c', $value);
  167. }, explode("\n", $this->value));
  168. return $results;
  169. }
  170. }
  171. class ActionStrtotime extends AbstractAction
  172. {
  173. public function setUp()
  174. {
  175. $this->setContainer( new DivContainer );
  176. $this->setFormatter( new EchoFormatter );
  177. }
  178. public function decode()
  179. {
  180. $tz = date_default_timezone_get();
  181. date_default_timezone_set('UTC');
  182. $time = strtotime($this->value);
  183. $str_utc = date('c', $time);
  184. date_default_timezone_set('America/New_York');
  185. $str_ny = date('c', $time);
  186. date_default_timezone_set($tz);
  187. return "$time<br>$str_utc<br>$str_ny";
  188. }
  189. }
  190. class ActionAnsi2Html extends AbstractAction
  191. {
  192. public function setUp()
  193. {
  194. $this->setContainer( new TextareaContainer );
  195. $this->setFormatter( new EchoFormatter );
  196. }
  197. public function decode()
  198. {
  199. $converter = new AnsiToHtmlConverter();
  200. return htmlentities(nl2br($converter->convert($this->value)));
  201. }
  202. }
  203. class ActionParseStr extends AbstractAction
  204. {
  205. public function setUp()
  206. {
  207. $this->setContainer( new DivContainer );
  208. $this->setFormatter( new DbugFormatter );
  209. }
  210. public function decode()
  211. {
  212. parse_str($this->value, $results);
  213. return $results;
  214. }
  215. }
  216. interface ContainerInterface
  217. {
  218. public function wrap( $contents );
  219. }
  220. class DivContainer implements ContainerInterface
  221. {
  222. public function wrap( $contents )
  223. {
  224. return '<div class="col">' . $contents . '</div>';
  225. }
  226. }
  227. class TextareaContainer extends DivContainer
  228. {
  229. public function wrap( $contents )
  230. {
  231. return parent::wrap( '<textarea>' . $contents . '</textarea>' );
  232. }
  233. }
  234. interface FormatterInterface
  235. {
  236. public function format( $value );
  237. }
  238. abstract class BufferedFormatter implements FormatterInterface {
  239. const ESCAPE = false;
  240. public function format( $value ) {
  241. ob_start();
  242. $this->bufferedFormat( $value );
  243. $output = ob_get_clean();
  244. if (static::ESCAPE) {
  245. $output = '<pre>' . htmlentities($output) . '</pre>';
  246. }
  247. return $output;
  248. }
  249. abstract public function bufferedFormat( $value );
  250. }
  251. class DbugFormatter extends BufferedFormatter
  252. {
  253. public function bufferedFormat( $value )
  254. {
  255. new dBug( $value );
  256. }
  257. }
  258. class VardumpFormatter extends BufferedFormatter
  259. {
  260. const ESCAPE = true;
  261. public function bufferedFormat( $value )
  262. {
  263. var_dump( $value );
  264. }
  265. }
  266. class EchoFormatter implements FormatterInterface
  267. {
  268. public function format( $value )
  269. {
  270. return $value;
  271. }
  272. }
  273. class VarexportFormatter extends BufferedFormatter
  274. {
  275. const ESCAPE = true;
  276. public function bufferedFormat( $value )
  277. {
  278. var_export( $value );
  279. }
  280. }