improved usability and coding style

This commit is contained in:
Steffen Vogel 2011-08-19 20:55:53 +02:00
parent 1bf7aa8b7a
commit 30ab353a10
14 changed files with 133 additions and 93 deletions

View File

@ -1,9 +1,9 @@
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (/(?:add|delete|update|ip))\.(xml|html|gif|txt|csv) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/(?:cleanup|sync|parse))\.(xml|html|gif|txt|csv) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/get)\.(xml|html|gif|txt|csv|png) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/stats/types)\.(xml|html|gif|txt|csv|png)
RewriteCond %{REQUEST_FILENAME} (/(?:add|delete|update|ip))\.(xml|html|gif|txt|csv|json) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/(?:cleanup|sync|parse))\.(xml|html|gif|txt|csv|json) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/get)\.(xml|html|gif|txt|csv|png|json) [OR]
RewriteCond %{REQUEST_FILENAME} (/admin/stats/types)\.(xml|html|gif|txt|csv|png|json)
RewriteRule .* %1.php?format=%2 [QSA]
RewriteCond %{QUERY_STRING} !zone=(0l.de|d.eta.li)

20
add.php
View File

@ -9,18 +9,18 @@ if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$rdata = (empty($_REQUEST['rdata']) && $type = 'A') ? $_SERVER['REMOTE_ADDR'] : $_REQUEST['rdata'];
$host = (empty($_REQUEST['host'])) ? Host::unique($zone, $db) : new Host($_REQUEST['host'], $zone);
$pw = (empty($_REQUEST['pw'])) ? randomString(8) : $_REQUEST['pw'];
if (empty($_REQUEST['lifetime']) || !is_int($_REQUEST['lifetime'])) {
$lifetime = $config['sddns']['std']['lifetime'];
}
else {
$lifetime = (int) $_REQUEST['lifetime'];
}
if (($lifetime > $config['sddns']['max_lifetime'] && !isAuthentificated()) || $lifetime < 0) {
$output->add('invalid lifetime', 'error', $lifetime);
}
if ($host->isRegistred($db)) {
if ($type == 'URL') {
$output->add('host is already registred', 'error', $host);
@ -30,8 +30,8 @@ if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$host = new DBHost($host->isRegistred($db), $db);
$output->add('found existing host' ,'notice', $host);
if (!$host->checkPassword($pw)) {
if (!$host->checkPassword($pw) && !isAuthentificated()) {
$output->add('not authentificated for host', 'error', $host);
$output->send();
die();
@ -40,21 +40,21 @@ if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
else {
$host = $host->add($pw, $db); // returns new DBHost
$output->add('host added to db' ,'notice', $host);
if (empty($_REQUEST['pw']))
$output->add('generated password' ,'notice', $pw);
}
if ($type != 'URL') { // pseudo type to create url redirection
$ttl = (empty($_REQUEST['ttl'])) ? $config['sddns']['std']['ttl'] : (int) $_REQUEST['ttl'];
$class = (empty($_REQUEST['class'])) ? $config['sddns']['std']['class'] : $_REQUEST['class'];
$record = new Record($host, $ttl, $class, $type, $rdata);
if (!$record->isRegistred($db)) {
$record = $record->add($db, $lifetime);
$output->add('record added to db', 'success', $record);
$zone->cleanup($db);
$zone->sync($db);
}

View File

@ -4,28 +4,28 @@ require_once '../include/init.php';
$output = Output::start();
$dataTables = array(
'hosts' => null, // table name => date field
'records' => 'created',
'queries' => 'queried',
'logs' => 'logged',
'uris' => 'created'
);
'hosts' => null, // table name => date field
'records' => 'created',
'queries' => 'queried',
'logs' => 'logged',
'uris' => 'created'
);
$perModes = array(
'hour' => false, // mySQL funtion => is timestamp
'day' => false,
'date' => true,
'week' => false,
'month' => false,
'weekday' => false
);
'hour' => false, // mySQL funtion => is timestamp
'day' => false,
'date' => true,
'week' => false,
'month' => false,
'weekday' => false
);
$colors = array(
'records' => 'blue',
'queries' => 'red',
'logs' => 'orange',
'uris' => 'black'
);
'records' => 'blue',
'queries' => 'red',
'logs' => 'orange',
'uris' => 'black'
);
$get = array();
if (isset($_REQUEST['data'])) {
@ -37,11 +37,11 @@ if (isset($_REQUEST['data'])) {
$output->add('invalid data', 'error', $tmp);
$output->send();
die();
}
}
}
}
else {
$get = array('queries');
$get = array('records');
}
if ($output instanceof GraphOutput) {
@ -64,7 +64,7 @@ if ($output instanceof GraphOutput) {
$graph = $output->getGraph(700, 300);
$graph->img->SetAntiAliasing();
if ($perModes[$per]) {
$graph->SetScale('datint');
$graph->xaxis->scale->SetTimeAlign(HOURADJ_1);
@ -123,8 +123,8 @@ if ($output instanceof GraphOutput) {
array_walk($plotData['x'], function(&$value) { $value = strtotime($value); });
$plot = new LinePlot($plotData['y'], $plotData['x']);
$plot->SetColor($colors[$table]);
$plot->SetLegend($table);
$plot->SetColor($colors[$table]);
$plot->SetLegend($table);
$plot->SetLineWeight(2);
$graph->Add($plot);
@ -138,51 +138,51 @@ else {
$filter = array();
if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$filter['zone'] = $config['sddns']['zones'][$_REQUEST['zone']];
if (!empty($_REQUEST['host'])) {
$filter['host'] = $_REQUEST['host'];
}
}
$data = DBHost::get($db, $filter);
$dateField = null;
break;
case 'logs':
$data = $db->query('SELECT logged, id, program, message FROM logs ORDER BY logged DESC', 1000);
$dateField = 'logged';
break;
case 'queries':
$data = $db->query('SELECT queried, id, ip, port, hostname, class, type, options FROM queries ORDER BY queried DESC', 1000);
$dateField = 'queried';
break;
case 'uris':
$filter = array();
if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$filter['zone'] = $config['sddns']['zones'][$_REQUEST['zone']];
if (!empty($_REQUEST['host'])) {
$filter['host'] = $_REQUEST['host'];
}
}
$data = DBUri::get($db, $filter);
$dateField = 'created';
break;
case 'records':
default:
$filter = array();
if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$filter['zone'] = $config['sddns']['zones'][$_REQUEST['zone']];
if (!empty($_REQUEST['host'])) {
$filter['host'] = $_REQUEST['host'];
}
}
if (!empty($_REQUEST['class']) && in_array($_REQUEST['class'], $config['sddns']['classes']))
$filter['class'] = $_REQUEST['class'];
if (!empty($_REQUEST['ttl']))
@ -192,22 +192,41 @@ else {
if (!empty($_REQUEST['rdata']) && Record::isRData($_REQUEST['rdata'], $filter['type']))
$filter['rdata'] = $_REQUEST['rdata'];
}
$data = DBRecord::get($db, $filter);
$dateField = 'created';
}
foreach ($data as $row) {
switch (@$_REQUEST['data']) {
switch ($get[0]) {
case 'uris':
$output->add('', 'data', $row->host, $row);
$params = 'host=' . $row->host->toPunycode() . '&zone=' . $row->host->zone->name . '&type=URL&rdata=' . $row->uri;
$actions = '<a href="../delete.php?' . $params . '"><img alt="delete" src="../images/delete.png" /></a>';
$actions .= '<a href="../expert.php?' . $params . '&command=update"><img alt="edit" src="../images/edit.png" /></a>';
if ($output instanceof HtmlOutput) $output->add(get_class($row), 'data', $row->host, $row, $actions);
else $output->add(get_class($row), 'data', $row->host, $row);
break;
case 'hosts':
$params = 'host=' . $row->toPunycode() . '&zone=' . $row->zone->name;
$actions = '<a href="../delete.php?' . $params . '"><img alt="delete" src="../images/delete.png" /></a>';
$actions .= '<a href="../expert.php?' . $params . '&command=update"><img alt="edit" src="../images/edit.png" /></a>';
if ($output instanceof HtmlOutput) $output->add(get_class($row), 'data', $row, $actions);
else $output->add(get_class($row), 'data', $row);
break;
case 'records':
$params = 'host=' . $row->host->toPunycode() . '&zone=' . $row->host->zone->name . '&type=' . $row->type . '&class=' . $row->class . '&rdata=' . $row->rdata;
$actions = '<a href="../delete.php?' . $params . '"><img alt="delete" src="../images/delete.png" /></a>';
$actions .= '<a href="../expert.php?' . $params . '&command=update"><img alt="edit" src="../images/edit.png" /></a>';
if ($output instanceof HtmlOutput) $output->add(get_class($row), 'data', $row, $actions);
else $output->add(get_class($row), 'data', $row);
break;
case 'logs':
case 'queries':
case 'records':
default:
$output->add('', 'data', $row);
$output->add('data', 'data', $row);
}
}
}

View File

@ -2,25 +2,24 @@
require_once 'include/init.php';
$output = Output::start();
$pw = @$_REQUEST['pw'];
if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$zone = $config['sddns']['zones'][$_REQUEST['zone']];
if (!empty($_REQUEST['host'])) {
if ($host = reset(DBHost::get($db, array('host' => $_REQUEST['host'], 'zone' => $zone)))) {
if ($host->checkPassword($pw) || isAuthentificated()) {
if (isset($_REQUEST['class']) && in_array($_REQUEST['class'], $config['sddns']['classes']))
$class = $_REQUEST['class'];
if (isset($_REQUEST['type']) && in_array($_REQUEST['type'], $config['sddns']['types'])) {
$type = $_REQUEST['type'];
if (isset($_REQUEST['rdata']) && Record::isRData($_REQUEST['rdata'], $type))
$rdata = $_REQUEST['rdata'];
}
if (@$type == 'URL' || empty($type)) {
$uris = DBUri::get($db, array('zone' => $zone, 'host' => $host));
foreach ($uris as $uri) {
@ -28,7 +27,7 @@ if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$output->add('uri deleted from db', 'success', $uri);
}
}
if (@$type != 'URL' || empty($type)) {
$records = DBRecord::get($db, array('zone' => $zone, 'host' => $host, 'type' => @$type, 'class' => @$class, 'rdata' => @$rdata));
foreach ($records as $record) {
@ -36,7 +35,7 @@ if (array_key_exists($_REQUEST['zone'], $config['sddns']['zones'])) {
$output->add('record deleted from db', 'success', $record);
}
}
$zone->cleanup($db);
$zone->sync($db);
}

View File

@ -84,12 +84,13 @@ $checkedType = (isset($_REQUEST['type'])) ? $_REQUEST['type'] : $config['sddns']
<option><i>all</i></option></select></td>
</tr>
<tr><td><label for="rdata">rdata</label></td><td><input value="<?php echo (empty($_REQUEST['rdata']) && $checkedType == 'A') ? $_SERVER['REMOTE_ADDR'] : @$_REQUEST['rdata']; ?>" type="text" name="rdata" /></td><td><input type="checkbox" value="1" name="frame" /> hide uri in a frameset</td></tr>
<tr><td><label for="pw">password</label></td><td><input type="password" name="pw" /></td><td>optional; random generated</td></tr>
<?php if (!isAuthentificated()) echo '<tr><td><label for="pw">password</label></td><td><input type="password" name="pw" /></td><td>optional; random generated</td></tr>'; ?>
</table>
<input type="submit" />
<p>
<a href="simple">simple mode</a> -
<?php if (isAuthentificated()) echo '<a href="admin/">admin</a> - '; ?>
<a href="http://0l.de/projects/sddns/usage">usage</a> -
<a href="http://0l.de/projects/sddns/">wiki</a> -
<a href="javascript:u='http://d.0l.de/add.html?type=URL&rdata='+encodeURIComponent(location.href);h=encodeURIComponent(window.getSelection().toString().replace(/[\s\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2E\x2F\x3A\x3B\x3C\x3D\x3F\x40\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7C\x7D\x7E]+/gi,'-').replace(/^\-+/,'').replace(/\-+$/,''));if(!h){h=prompt('Subdomain','');}if(h){u+='&host='+h;}location.href=u">bookmarklet</a> -

BIN
images/delete.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 715 B

BIN
images/edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 714 B

View File

@ -64,15 +64,15 @@ abstract class DBResultSet implements Iterator {
public function valid() {
return (bool) is_array($this->current());
}
public function first() {
return (isset($this->_rows[0])) ? $this->_rows[0] : null;
}
public function last() {
return $this->_rows[$this->_num_rows - 1];
}
public function count() {
return $this->_num_rows;
}
@ -141,4 +141,4 @@ abstract class Database implements IDatabase {
protected $statements = array();
}
?>
?>

View File

@ -2,7 +2,7 @@
function isAuthentificated() {
$config = Registry::get('config');
$htpasswd = file('../.htpasswd');
$htpasswd = file('/var/www/nulll/.htpasswd');
foreach ($htpasswd as $line) {
list($user, $crypt) = explode(':', $line);

View File

@ -2,16 +2,16 @@
class Host implements Object {
private $punycode;
public $zone;
public $zone;
public $generated;
/*
* Constructor & Factory
*/
public function __construct($hostname, Zone $zone, $generated = false) {
$hostname = strtolower($hostname);
if (self::isValid(idn_to_ascii($hostname))) {
$this->punycode = idn_to_ascii($hostname);
$this->punycode = idn_to_ascii($hostname);
$this->zone = $zone;
$this->generated = $generated;
}
@ -22,7 +22,7 @@ class Host implements Object {
throw new UserException('Invalid hostname: ' . idn_to_ascii($hostname));
}
}
public static function unique(Zone $zone, Database $db) {
$config = Registry::get('config');
@ -32,25 +32,25 @@ class Host implements Object {
generated = TRUE &&
zone = \'' . $db->escape($zone->name) . '\'
ORDER BY id DESC';
$result = $db->query($sql, 1);
$first = $result->first();
$last_id = base_convert($first['hostname'], 36, 10);
while ($result->count() > 0 || !Host::isValid(base_convert($last_id, 10, 36))) {
$sql = 'SELECT hostname
FROM ' . $config['db']['tbl']['hosts'] . '
WHERE
hostname = \'' . base_convert(++$last_id, 10, 36) . '\' &&
zone = \'' . $db->escape($zone->name) . '\'';
$result = $db->query($sql, 1);
}
return new self(base_convert($last_id, 10, 36), $zone, true);
}
/*
* Checks
*/
@ -58,7 +58,7 @@ class Host implements Object {
$hostExpr = '[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?';
return preg_match('/^(?:\*|(\*\.)?(' . $hostExpr . '\.)*(' . $hostExpr . '))$/i', $hostname);
}
public function isRegistred(Database $db) {
$config = Registry::get('config');
@ -71,7 +71,7 @@ class Host implements Object {
return ($result->count() > 0) ? $host['id'] : false;
}
/*
* Database
*/
@ -86,35 +86,35 @@ class Host implements Object {
' . (int) $this->generated . ')';
$db->execute($sql);
return new DBHost($db->lastId(), $db);
}
/*
* Output
*/
public function toUnicode() {
return idn_to_utf8($this->punycode);
}
public function toPunycode() {
return $this->punycode;
}
public function __toString() {
return $this->toPunycode() . '.' . $this->zone->name;
}
public function toXml(DOMDocument $doc) {
$xmlHost = $doc->createElement('host');
$xmlHost->appendChild($doc->createElement('hostname', $this->toPunycode()));
$xmlHost->appendChild($doc->createElement('idn', $this->toUnicode()));
$xmlHost->appendChild($this->zone->toXml($doc));
return $xmlHost;
}
public function toHtml() {
return '<a target="_blank" href="http://' . $this . '">' . $this->toUnicode() . '.' . $this->zone->name . '</a>';
}

View File

@ -42,11 +42,11 @@ class MySql extends Database {
$this->connect($host, $user, $pw);
$this->select($db);
}
function __destruct() {
$this->close();
}
/**
* @brief create database connection
* @param string $host IP or domain of the database host
@ -60,7 +60,7 @@ class MySql extends Database {
error_reporting($__er);
throw new MySqlException();
}
error_reporting($__er);
}
@ -90,6 +90,8 @@ class MySql extends Database {
* @return mixed
*/
public function execute($sql) {
if ($output = Registry::get('output')) $output->add('db query', 'debug', 8, $sql);
if (!($result = mysql_unbuffered_query($sql, $this->resource)))
throw new MySqlException();
return $result;
@ -107,7 +109,7 @@ class MySql extends Database {
$sql .= sprintf(' LIMIT %d, %d', $offset, $limit);
return new MySqlResult($this->execute($sql));
}
/**
* @brief mysql escape
* @param string $sql query
@ -115,11 +117,11 @@ class MySql extends Database {
public function escape($sql) {
return mysql_real_escape_string($sql, $this->resource);
}
public function lastId() {
return mysql_insert_id($this->resource);
}
public function affectedRows() {
return mysql_affected_rows($this->resource);
}

View File

@ -1,5 +1,21 @@
<?php
class JsonOutput extends Output {
public function __construct($debug) {
parent::__construct('application/json', 'UTF-8', $debug);
}
protected function getOutput() {
return json_encode($this->getMessages());
$json = array();
foreach ($this->getMessages() as $index => $message) {
}
}
}
class XmlOutput extends Output {
public function __construct($debug) {
parent::__construct('text/xml', 'UTF-8', $debug);
@ -327,6 +343,9 @@ abstract class Output {
return new GifOutput();
break;
case 'json':
return new JsonOutput($debug);
case 'html':
case 'php':
default:
@ -339,7 +358,7 @@ abstract class Output {
global $argc;
$site = Registry::get('site');
if (isset($forced))
$format = $forced;
elseif (isset($argc))

View File

@ -2,7 +2,7 @@
class Zone extends NameServer implements Object {
public $name;
public $key;
private $key;
function __construct($nserver, $name, $key, $nsport = 53) {
parent::__construct($nserver, $nsport);
@ -42,7 +42,7 @@ class Zone extends NameServer implements Object {
LEFT JOIN ' . $config['db']['tbl']['hosts'] . ' AS h
ON h.id = u.host_id
WHERE
((u.last_accessed + INTERVAL u.lifetime SECOND) < NOW()&&
((u.last_accessed + INTERVAL u.lifetime SECOND) < NOW() &&
h.zone = \'' . $db->escape($this->name) . '\') || h.id IS NULL';
$db->execute($sql);

View File

@ -68,12 +68,12 @@ if (!empty($zone)) {
$records[0]->lastAccessed = time();
$records[0]->update();
$output->add('record updated in db', 'success', $records[0]);
for ($i = 1; $i < count($records); $i++) {
$records[$i]->delete();
$output->add('record deleted from db', 'warning', $records[$i]);
}
$zone->cleanup($db);
$zone->sync($db);
}