php-tools/classes.php

341 lines
6.8 KiB
PHP
Raw Permalink Normal View History

2012-05-23 15:18:49 +00:00
<?php
2013-07-25 21:48:02 +00:00
use \Ospinto\Dbug;
2014-05-13 18:17:41 +00:00
use \SensioLabs\AnsiConverter\AnsiToHtmlConverter;
2013-07-25 21:48:02 +00:00
2012-05-23 15:18:49 +00:00
abstract class AbstractAction
{
protected $value;
2012-05-23 15:18:49 +00:00
protected $container;
2012-05-23 15:18:49 +00:00
protected $formatter;
2012-05-23 15:18:49 +00:00
public function __construct( $value )
{
$this->value = $value;
$this->setUp();
}
2012-05-23 15:18:49 +00:00
abstract public function decode();
abstract public function setUp();
public function setContainer( ContainerInterface $container )
{
$this->container = $container;
}
2012-05-23 15:18:49 +00:00
public function setFormatter( FormatterInterface $formatter )
{
$this->formatter = $formatter;
}
2012-05-23 15:18:49 +00:00
public function esc_raw()
{
return htmlentities( $this->value );
}
2012-05-23 15:18:49 +00:00
public function __toString()
{
$value = $this->decode();
$formatted = $this->formatter->format( $value );
return $this->container->wrap( $formatted );
}
2012-05-23 15:18:49 +00:00
}
class ActionQuotedPrintableDecode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return quoted_printable_decode( $this->value );
}
2012-05-23 15:18:49 +00:00
}
2012-09-17 18:18:32 +00:00
class ActionQuotedPrintableEncode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return quoted_printable_encode( $this->value );
}
2012-09-17 18:18:32 +00:00
}
class ActionBase64Encode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return base64_encode( $this->value );
}
}
class ActionBase64Decode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return base64_decode( $this->value );
}
}
2012-05-23 15:18:49 +00:00
class ActionUrldecode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return urldecode( $this->value );
}
2012-05-23 15:18:49 +00:00
}
class ActionUrlencode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return urlencode( $this->value );
}
2012-05-23 15:18:49 +00:00
}
2013-07-25 17:49:28 +00:00
class ActionSerialize extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return serialize( json_decode( $this->value ) );
2013-07-25 17:49:28 +00:00
}
}
2012-05-23 15:18:49 +00:00
class ActionUnserialize extends AbstractAction
{
public function setUp()
{
$this->setContainer( new DivContainer );
$this->setFormatter( new DbugFormatter );
}
public function decode()
{
return unserialize( $this->value );
}
}
2012-05-23 15:18:49 +00:00
class ActionJsonDecode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new DivContainer );
$this->setFormatter( new VarexportFormatter );
}
public function decode()
{
return json_decode( $this->value, true );
}
}
class ActionJsonEncode extends AbstractAction
{
public function setUp()
{
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return json_encode( $this->value );
}
2012-05-23 15:18:49 +00:00
}
2013-07-25 16:25:49 +00:00
class ActionDateC extends AbstractAction
{
public function setUp()
2013-07-25 16:25:49 +00:00
{
$this->setContainer( new DivContainer );
$this->setFormatter( new DbugFormatter );
}
public function decode()
{
$results = array();
array_map(function($value) use (&$results) {
$results[$value] = date('c', $value);
}, explode("\n", $this->value));
return $results;
}
}
2014-09-03 13:09:17 +00:00
class ActionStrtotime extends AbstractAction
{
public function setUp()
{
$this->setContainer( new DivContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$time = strtotime($this->value);
$str_utc = date('c', $time);
date_default_timezone_set('America/New_York');
$str_ny = date('c', $time);
date_default_timezone_set($tz);
return "$time<br>$str_utc<br>$str_ny";
}
}
2014-05-13 18:17:41 +00:00
class ActionAnsi2Html extends AbstractAction
{
public function setUp()
{
2016-01-25 04:36:41 +00:00
$this->setContainer( new TextareaContainer );
2014-05-13 18:17:41 +00:00
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
$converter = new AnsiToHtmlConverter();
return htmlentities(nl2br($converter->convert($this->value)));
}
}
2014-03-13 16:10:33 +00:00
class ActionParseStr extends AbstractAction
{
public function setUp()
{
$this->setContainer( new DivContainer );
$this->setFormatter( new DbugFormatter );
}
public function decode()
{
parse_str($this->value, $results);
return $results;
}
}
2012-05-23 15:18:49 +00:00
interface ContainerInterface
{
public function wrap( $contents );
2012-05-23 15:18:49 +00:00
}
class DivContainer implements ContainerInterface
{
public function wrap( $contents )
{
return '<div class="col">' . $contents . '</div>';
}
2012-05-23 15:18:49 +00:00
}
class TextareaContainer extends DivContainer
{
public function wrap( $contents )
{
2016-01-25 04:36:41 +00:00
return parent::wrap( '<textarea>' . $contents . '</textarea>' );
}
2012-05-23 15:18:49 +00:00
}
interface FormatterInterface
{
public function format( $value );
2012-05-23 15:18:49 +00:00
}
abstract class BufferedFormatter implements FormatterInterface {
const ESCAPE = false;
public function format( $value ) {
ob_start();
$this->bufferedFormat( $value );
$output = ob_get_clean();
if (static::ESCAPE) {
$output = '<pre>' . htmlentities($output) . '</pre>';
}
return $output;
}
abstract public function bufferedFormat( $value );
}
class DbugFormatter extends BufferedFormatter
2012-05-23 15:18:49 +00:00
{
public function bufferedFormat( $value )
{
new dBug( $value );
}
2012-05-23 15:18:49 +00:00
}
class VardumpFormatter extends BufferedFormatter
2012-05-23 15:18:49 +00:00
{
const ESCAPE = true;
public function bufferedFormat( $value )
{
var_dump( $value );
}
2012-05-23 15:18:49 +00:00
}
class EchoFormatter implements FormatterInterface
{
public function format( $value )
{
return $value;
}
}
class VarexportFormatter extends BufferedFormatter
{
const ESCAPE = true;
public function bufferedFormat( $value )
{
var_export( $value );
}
2012-05-23 15:18:49 +00:00
}