* @link http://www.steffenvogel.de
*/
/*
* This file is part of sddns
*
* sddns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* sddns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sddns. If not, see .
*/
class JsonOutput extends Output {
public function __construct($debug) {
parent::__construct('application/json', 'UTF-8', $debug);
}
protected function getOutput() {
$json = array();
foreach ($this->getMessages() as $index => $message) {
array_push($json, $message);
}
// JSONP
if (!empty($_REQUEST['callback'])) {
return $_REQUEST['callback'] . '(' . json_encode($json) . ');';
}
else {
return json_encode($json);
}
}
}
class XmlOutput extends Output {
public function __construct($debug) {
parent::__construct('text/xml', 'UTF-8', $debug);
}
protected function getOutput() {
$doc = new DomDocument('1.0', $this->encoding);
$doc->formatOutput = true;
$xmlSddns = $doc->createElement('sddns');
$doc->appendChild($xmlSddns);
foreach ($this->getMessages() as $index => $message) {
$xmlMessage = $doc->createElement('message');
$xmlMessage->setAttribute('type', $message['type']);
if ($message['type'] == 'debug') {
$xmlMessage->setAttribute('level', $message['level']);
}
$xmlMessage->appendChild($doc->createElement('description', $message['description']));
foreach ($message['data'] as $object) {
if (method_exists($object, 'toXml')) {
$xmlMessage->appendChild($object->toXml($doc));
}
else {
$xmlMessage->appendChild($doc->createElement(gettype($object), $object));
}
}
$xmlSddns->appendChild($xmlMessage);
}
return $doc->saveXML();
}
}
// needs JPGraph >= 3.0.7!
class GraphOutput extends Output {
private $graph;
public function __construct($debug) {
parent::__construct('text/html', 'UTF-8', $debug);
global $site;
require_once 'jpgraph/jpgraph.php';
}
public function getGraph($width, $height, $type = 'line') {
switch ($type) {
case 'line':
$this->graph = new Graph($width, $height);
break;
case 'pie':
$this->graph = new PieGraph($width, $height);
break;
}
return $this->graph;
}
public function showGraph() {
if (isset($this->graph))
$this->graph->Stroke();
}
protected function getOutput() { // TODO
if (count($this->getMessages()) > 0) {
echo '
';
print_r($this->getMessages());
echo '
';
}
}
}
class GifOutput extends Output {
public function __construct() {
parent::__construct('image/gif');
}
protected function getOutput() {
$im = imagecreate(1, 1);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 255, 0);
imagefill($im, 0, 0, (count($this->getMessages(false, array('error', 'exception'))) > 0) ? $red : $green);
imagegif($im);
imagedestroy($im);
}
}
class PlainLineOutput extends Output {
public function __construct($debug, $fields = array('index', 'time', 'type', 'description', 'data'), $delimiter = "\t", $lineDelimiter = "\n", $escape = true) {
parent::__construct('text/plain', 'UTF-8', $debug);
$this->fields = $fields;
$this->delimiter = $delimiter;
$this->lineDelimiter = $lineDelimiter;
}
protected function getOutput() {
global $argv;
$fd = fopen('php://memory', 'w+');
foreach($this->getMessages() as $index => $message) {
if (isset($argv)) {
$fd = (in_array($message['type'], array('error', 'exception', 'warning'))) ? STDERR : STDOUT;
}
foreach ($this->fields as $fieldIndex => $field) {
switch ($field) {
case 'type':
fwrite($fd, '[' . $message['type'] . ']');
break;
case 'index':
fwrite($fd, $index);
break;
case 'time':
fwrite($fd, date('Y-m-d H:i:s', $message['time']));
break;
case 'description':
fwrite($fd, $message['description']);
break;
case 'data':
fwrite($fd, implode(', ', $message['data']));
break;
default:
fwrite($fd, $message[$field]);
}
fwrite($fd, ($fieldIndex == count($this->fields) - 1) ? $this->lineDelimiter : $this->delimiter);
}
}
if (isset($argv)) {
exit( (count($this->getMessages(false, array('error', 'exception')))) ? 1 : 0 );
}
else {
rewind($fd);
$str = stream_get_contents($fd);
fclose($fd);
return $str;
}
}
}
class HtmlOutput extends Output {
public function __construct($debug) {
parent::__construct('text/html', 'UTF-8', $debug);
ob_start();
}
protected function getOutput() {
global $site;
$columnCount = 0;
$messages = $this->getMessages();
$html = ob_get_clean();
foreach ($messages as $message) {
if (count($message['data']) > $columnCount)
$columnCount = count($message['data']);
}
$str = '
/dev/nulll - Simple Dynamic Domain Name Service