merged changes from live/online installation

This commit is contained in:
Steffen Vogel 2010-10-07 11:50:09 +02:00
parent 85846beb7b
commit f71ac411d6
5 changed files with 118 additions and 102 deletions

View file

@ -7,10 +7,10 @@ $sql = 'SELECT *
WHERE
program = \'named\'
LIMIT 10000';
$result = $db->query($sql);
$pattern = '/^named\[(\d+)\]: queries: info: client ([.\d]+)#(\d+): query: ([.-\w]+) ([A-Z]+) ([A-Z]+) (.*)$/';
$pattern = '/^named\[(\d+)\]: queries: info: client ([.\d]+)#(\d+): query: ([.-+\w]+) ([A-Z]+) ([A-Z]+) (.*)$/';
$sqlDelete = 'DELETE FROM logs WHERE id IN (';
$queries = array();
$c = 0; $u = 0;
@ -33,21 +33,21 @@ foreach ($result as $log) {
foreach ($config['sddns']['zones'] as $zone) {
if (substr($query['hostname'], -strlen($zone->name)) == $zone->name && strlen($query['hostname']) > strlen($zone->name)) {
$filter = array('class' => $query['class'],
'type' => $query['type'],
'host' => substr($query['hostname'], 0, -(strlen($zone->name) + 1)),
'zone' => $zone);
$records = DBRecord::get($db, $filter);
foreach ($records as $record) {
$record->lastAccessed = $query['queried'];
$record->update();
$output->add('record renewed', 'notice', 1,$record);
$output->add('record renewed', 'debug', 1, $record);
}
}
}
}
}
}
@ -56,7 +56,7 @@ if ($c > 0) {
$output->add('parsed queries', 'success', $c);
}
else {
$output->add('no queries to parse', 'warning');
$output->add('no queries to parse', 'debug', 1);
}
?>

View file

@ -1,6 +1,6 @@
<?php
$config['path']['web'] = '';
$config['path']['web'] = '';
$config['db']['host'] = 'localhost';
$config['db']['user'] = 'sddns';
@ -14,8 +14,8 @@ $config['db']['tbl']['uris'] = $config['db']['tbl']['prefix'] . 'uris';
$config['sddns']['htpasswd'] = $site['path']['server'] . '/../.htpasswd';
$key = array('hmac' => 'gDlXSZtESw78I47O68UEigpPofn0XbpSpo5Vba+9IY38EYagPO/2C2Ch lZL+AvtN/ozRdra+p3+wLOKvVvqdrA==', 'name' => 'info.steffenvogel.de.');
$zones = array('0l.de', 'd.eta.li');
$config['sddns']['ns']['hostname'] = 'ns0.0l.de';
$zones = array('0l.de', 'd.eta.li'); // , 'griesm.de', 'dynamic.steffenvogel.de');
$config['sddns']['ns']['hostname'] = 'ns0.0l.de';
$config['sddns']['ns']['port'] = 53;
foreach ($zones as $zone) {

View file

@ -1 +1 @@
20624
58901

View file

@ -10,17 +10,17 @@ class XmlOutput extends Output {
$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->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));
@ -43,7 +43,7 @@ class GraphOutput extends Output {
public function __construct($debug) {
parent::__construct('text/html', 'UTF-8', $debug);
$site = Registry::get('site');
require_once 'jpgraph/jpgraph.php';
}
@ -55,7 +55,7 @@ class GraphOutput extends Output {
case 'pie':
$this->graph = new PieGraph($width, $height);
break;
}
}
return $this->graph;
}
@ -63,15 +63,15 @@ class GraphOutput extends Output {
public function showGraph() {
if (@isset($this->graph))
$this->graph->Stroke();
$this->graph->Stroke();
}
protected function getOutput() { // TODO beautify
if (count($this->getMessages()) > 0) {
echo '<pre>';
print_r($this->getMessages());
echo '</pre>';
}
if (count($this->getMessages()) > 0) {
echo '<pre>';
print_r($this->getMessages());
echo '</pre>';
}
}
}
@ -84,7 +84,7 @@ class GifOutput extends Output {
$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);
@ -100,47 +100,62 @@ class PlainLineOutput extends Output {
}
protected function getOutput() {
$str = '';
global $argv;
$fd = fopen('php://memory', 'w+');
foreach($this->getMessages() as $index => $message) {
foreach ($this->fields as $field) {
if (isset($argv)) {
$fd = (in_array($message['type'], array('error', 'exception', 'warning'))) ? STDERR : STDOUT;
}
foreach ($this->fields as $fieldIndex => $field) {
switch ($field) {
case 'type':
$str .= $message['type'];
fwrite($fd, $message['type']);
break;
case 'index':
$str .= $index;
fwrite($fd, $index);
break;
case 'time':
$str .= date('Y-m-d H:i:s', $message['time']);
fwrite($fd, date('Y-m-d H:i:s', $message['time']));
break;
case 'description':
$str .= $message['description'];
fwrite($fd, $message['description']);
break;
case 'data':
foreach ($message['data'] as $object) {
$str .= $this->delimiter . $object;
fwrite($fd, $this->delimiter . $object);
}
break;
default:
fwrite($fd, $message[$field]);
}
$str .= $this->delimiter;
fwrite($fd, ($fieldIndex == count($this->fields) - 1) ? $this->lineDelimiter : $this->delimiter);
}
$str = substr($str, 0, -1) . $this->lineDelimiter;
}
return $str;
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 DynDnsOutput extends Output {
public function __construct() {
parent::__construct('text/plain');
if (!isset($_SERVER['PHP_AUTH_PW']))
header('WWW-Authenticate: Basic realm="DynDNS API Access"');
header('WWW-Authenticate: Basic realm="DynDNS API Access"');
}
protected function getOutput() {
@ -152,13 +167,13 @@ class DynDnsOutput extends Output {
class HtmlOutput extends Output {
public function __construct($debug) {
parent::__construct('text/html', 'UTF-8', $debug);
ob_start();
}
protected function getOutput() {
$site = Registry::get('site');
$str = '<?xml version="1.0" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
@ -172,25 +187,25 @@ class HtmlOutput extends Output {
<link rel="search" type="application/opensearchdescription+xml" title="Tiny DNS &amp; URL" href="' . $site['path']['web'] . '/opensearch.xml" />
</head>
<body>';
$maxDataCount = 0;
$messages = $this->getMessages();
foreach ($messages as $message) {
if (count($message['data']) > $maxDataCount)
$maxDataCount = count($message['data']);
$maxDataCount = count($message['data']);
}
if (count($messages)) {
$str .= '<table id="messages">
<tr><th>type</th><th>index</th><th>time</th><th>description</th><th colspan="100">data</th></tr>';
foreach ($messages as $index => $message) {
$str .= '<tr class="' . $message['type'] . '">
<td><img alt="' . $message['type'] . '" src="' . $site['path']['web'] . '/images/' . $message['type'] . '.png" title="' . @$message['level'] . '" /></td>
<td>#' . $index . '</td>
<td>' . date('Y-m-d H:i:s', $message['time']) . '</td>
<td>' . $message['description'] . '</td>';
for($i = 0; $i < $maxDataCount; $i++) {
$str .= '<td>';
if (isset($message['data'][$i])) {
@ -204,19 +219,19 @@ class HtmlOutput extends Output {
}
$str .= '</td>';
}
$str .= '</tr>';
}
$str .= '</table>';
}
$str .= '<div id="content">';
$str .= ob_get_clean();
$str .= '</div>
</body>
</html>';
return $str;
}
}
@ -227,14 +242,14 @@ abstract class Output {
public $format;
protected $contentType;
protected $encoding;
function __construct($contentType = 'text/plain', $encoding = 'UTF-8', $debug = 0) {
$this->contentType = $contentType;
$this->encoding = $encoding;
$this->encoding = $encoding;
$this->debug = $debug;
if ($this->contentType != null)
header('Content-type: ' . $this->contentType . (($this->encoding != null) ? '; charset=' . $this->encoding : ''));
header('Content-type: ' . $this->contentType . (($this->encoding != null) ? '; charset=' . $this->encoding : ''));
}
function add($description, $type = 'notice') {
@ -242,16 +257,17 @@ abstract class Output {
$message['description'] = $description;
$message['type'] = $type;
$message['data'] = array();
$argv = func_get_args();
$argc = count($argv);
if ($type == 'debug')
if ($type == 'debug') {
$message['level'] = $argv[2];
}
for ($i = ($type == 'debug') ? 3 : 2; $i < $argc; $i++) {
if (empty($argv[$i]))
continue;
continue;
if (!is_array($argv[$i])) {
$message['data'][] = $argv[$i];
@ -263,25 +279,25 @@ abstract class Output {
array_push($this->messages, $message);
}
protected function getMessages($exclude = true, $args = null) {
$types = array('notice', 'success', 'error', 'exception', 'warning', 'data'); // 'debug');
if ($args == null)
$args = array();
$args = array();
if ($exclude)
$types = array_diff($types, $args);
$types = array_diff($types, $args);
else
$types = $args;
$types = $args;
$messages = array();
foreach ($this->messages as $message) {
if (in_array($message['type'], $types) || ($message['type'] == 'debug' && $message['level'] <= $this->debug)) {
$messages[] = $message;
}
}
return $messages;
}
@ -290,15 +306,15 @@ abstract class Output {
case 'xml':
return new XmlOutput($debug);
break;
case 'txt':
return new PlainLineOutput($debug, array('index', 'time', 'type', 'description', 'data'), "\t", "\n");
break;
case 'csv':
return new PlainLineOutput($debug, array('time', 'type', 'description', 'data'), ";", "\n");
break;
case 'dyndns':
return new DynDnsOutput();
break;
@ -340,18 +356,18 @@ abstract class Output {
$type = 'warning';
$str = 'notice';
break;
case E_USER_ERROR:
case E_ERROR:
default:
$type = 'error';
$str = $type;
break;
}
}
$this->add($str . ' in script', $type, $errstr . ' in ' . $errfile . ':' . $errline);
}
}
abstract protected function getOutput();
public function __destruct() {

View file

@ -32,11 +32,11 @@ class Zone extends NameServer implements Object {
WHERE
((r.last_accessed + INTERVAL r.lifetime SECOND) < NOW() &&
h.zone = \'' . $db->escape($this->name) . '\') || h.id IS NULL';
$db->execute($sql);
if ($db->affectedRows() > 0)
$output->add('records deleted from db', 'success', $db->affectedRows(), $this);
// expired urls & uris without host
$sql = 'DELETE u FROM ' . $config['db']['tbl']['uris'] . ' AS u
LEFT JOIN ' . $config['db']['tbl']['hosts'] . ' AS h
@ -44,11 +44,11 @@ class Zone extends NameServer implements Object {
WHERE
((u.last_accessed + INTERVAL u.lifetime SECOND) < NOW()&&
h.zone = \'' . $db->escape($this->name) . '\') || h.id IS NULL';
$db->execute($sql);
if ($db->affectedRows() > 0)
$output->add('urls deleted from db', 'success', $db->affectedRows(), $this);
// hosts without records or url
$sql = 'DELETE h
FROM ' . $config['db']['tbl']['hosts'] . ' AS h
@ -59,7 +59,7 @@ class Zone extends NameServer implements Object {
WHERE
(r.id IS NULL && u.id IS NULL) &&
h.zone = \'' . $db->escape($this->name) . '\'';
$db->execute($sql);
if ($db->affectedRows() > 0)
$output->add('hosts deleted from db', 'success', $db->affectedRows(), $this);
@ -67,17 +67,17 @@ class Zone extends NameServer implements Object {
public function sync(Database $db) {
$output = Registry::get('output');
$nsRecords = $this->getRecordsFromNS();
$dbRecords = $this->getRecordsFromDB($db);
$delete = array_diff($nsRecords, $dbRecords);
$add = array_diff($dbRecords, $nsRecords);
foreach ($add as $record) {
$this->add($record);
$output->add('record added to ns', 'success', $record);
}
foreach ($delete as $record) {
$this->delete($record);
$output->add('record deleted from ns', 'success', $record);
@ -90,14 +90,14 @@ class Zone extends NameServer implements Object {
if ($record->host->zone->name != $this->name) {
throw new NameServerException('zone mismatch: trying to add record "' . $record . '" to zone "' . $this . '"');
}
parent::add($record);
$nsresult = $this->sendQueue();
if($nsresult['code'] != 0) {
throw new NameServerException('error during nameserver update: ' . $nsresult['stderr']);
}
return true;
}
@ -107,16 +107,16 @@ class Zone extends NameServer implements Object {
if ($record->host->zone->name != $this->name) {
throw new NameServerException('zone mismatch: trying to delete record ' . $record . ' from zone ' . $this);
}
parent::delete($record);
$nsresult = $this->sendQueue();
if($nsresult['code'] != 0) {
throw new NameServerException('error during nameserver update: ' . $nsresult['stderr']);
}
return true;
}
return true;
}
/*
* Getter
@ -124,28 +124,28 @@ class Zone extends NameServer implements Object {
public function getRecordsFromNS() {
$config = Registry::get('config');
$output = Registry::get('output');
$records = array();
foreach (parent::query($this->name, 'AXFR') as $result) {
if (in_array($result[3], $config['sddns']['types']) && strlen($result[0]) > strlen($this->name) + 1) {
$hostname = substr($result[0], 0, -(strlen($this->name) + 2));
switch ($result[3]) {
case 'NS':
case 'MX':
case 'CNAME':
$rdata = substr($result[4], 0, -1);
break;
case 'TXT':
$rdata = trim($result[4], '"');
break;
default:
$rdata = $result[4];
}
try {
$host = new Host($hostname, $this);
$records[] = new Record($host, (int) $result[1], $result[2], $result[3], $rdata);
@ -160,30 +160,30 @@ class Zone extends NameServer implements Object {
public function getRecordsFromDB(Database $db) {
return DBRecord::get($db, array('zone' => $this));
}
public function getUrisFromDB(Database $db) {
return DBUri::get($db, array('zone' => $this));
}
public function getHostsFromDB(Database $db) {
return DBHost::get($db, array('zone' => $this));
}
/*
* Output
*/
public function __toString() {
return parent::__toString() . '/' . $this->name;
}
public function toXml(DOMDocument $doc) {
$xmlZone = parent::toXml($doc, 'zone');
$xmlZone->appendChild($doc->createElement('zone', $this->name));
return $xmlZone;
}
public function toHtml() {
return $this;
}