Skip to content

Commit

Permalink
externalized db-settings; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Nov 10, 2014
1 parent de1b563 commit f45f705
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/config.inc.php
/.project
8 changes: 8 additions & 0 deletions config.inc.php.example
@@ -0,0 +1,8 @@
<?php

$mysql_server = "localhost";
$mysql_db = "fff_xchange";
$mysql_user = "fff_xchange";
$mysql_pass = "xxxxxxxxxxxxxxxxx";

?>
200 changes: 99 additions & 101 deletions index.php
@@ -1,8 +1,5 @@
<?

$mysql_db = "example";
$mysql_user = "example";
$mysql_pass = "example";
<?php
require("config.inc.php");

$delete_time = 60*60*24*30;

Expand All @@ -15,10 +12,11 @@ private function __construct() {

public static function getInstance() {
if (!self::$instance) {
global $mysql_db;
global $mysql_user;
global $mysql_pass;
self::$instance = new PDO("mysql:host=localhost;dbname=$mysql_db", $mysql_user, $mysql_pass);;
global $mysql_server;
global $mysql_db;
global $mysql_user;
global $mysql_pass;
self::$instance = new PDO("mysql:host=$mysql_server;dbname=$mysql_db", $mysql_user, $mysql_pass);;
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
Expand All @@ -31,127 +29,127 @@ private function __clone() {
//if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'])
//$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'])
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (isset($_GET['mac']) && $_GET['mac'])
$mac = $_GET['mac'];
$mac = $_GET['mac'];
else
$mac = '000000000000';
$mac = '000000000000';
if (isset($_GET['name']) && $_GET['name'])
$name = $_GET['name'];
$name = $_GET['name'];
if (isset($_GET['key']) && $_GET['key'])
$key = $_GET['key'];
$key = $_GET['key'];
if (isset($_GET['port']) && $_GET['port'])
$port = $_GET['port'];
$port = $_GET['port'];
else
$port = 10000;

$hood = 1; // default hood
$gateway = false;

if ($ip && $name && $key) {
if (!preg_match('/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}'.
'[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}'.
'[a-zA-Z0-9]))*$/', $name))
exit("invalid name");

if ($mac != '000000000000')
$sql = 'SELECT * FROM nodes WHERE mac = :mac;';
else
$sql = 'SELECT * FROM nodes WHERE mac = \'000000000000\' AND name = :name;';

try {
$rs = db::getInstance()->prepare($sql);
if ($mac != '000000000000')
$rs->bindParam(':mac', $mac);
else
$rs->bindParam(':name', $name);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}

if ($rs->rowCount() > 1)
exit("To much nodes with mac=$mac, name=$name");

if ($rs->rowCount() == 1) {
$result = $rs->fetch(PDO::FETCH_ASSOC);
$hood = $result['hood_ID'];
$gateway = $result['isgateway'];
if (!$result['readonly']) {
$sql = 'UPDATE nodes SET ip = :ip, mac = :mac, name = :name, `key` = :key,'
.' port = :port, timestamp = CURRENT_TIMESTAMP WHERE ID = :id';
try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':id', $result['ID'], PDO::PARAM_INT);
$rs->bindParam(':ip', $ip);
$rs->bindParam(':mac', $mac);
$rs->bindParam(':name', $name);
$rs->bindParam(':key', $key);
$rs->bindParam(':port', $port);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}
}
} else {
$sql = 'INSERT INTO nodes (ip, mac, name, `key`, port, readonly, isgateway, hood_ID)'
.' VALUES (:ip, :mac, :name, :key, :port, 0, 0, :hood);';
try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':ip', $ip);
$rs->bindParam(':mac', $mac);
$rs->bindParam(':name', $name);
$rs->bindParam(':key', $key);
$rs->bindParam(':port', $port);
$rs->bindParam(':hood', $hood);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}
}
if (!preg_match('/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}'.
'[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}'.
'[a-zA-Z0-9]))*$/', $name))
exit("invalid name");

if ($mac != '000000000000')
$sql = 'SELECT * FROM nodes WHERE mac = :mac;';
else
$sql = 'SELECT * FROM nodes WHERE mac = \'000000000000\' AND name = :name;';

try {
$rs = db::getInstance()->prepare($sql);
if ($mac != '000000000000')
$rs->bindParam(':mac', $mac);
else
$rs->bindParam(':name', $name);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}

if ($rs->rowCount() > 1)
exit("To much nodes with mac=$mac, name=$name");

if ($rs->rowCount() == 1) {
$result = $rs->fetch(PDO::FETCH_ASSOC);
$hood = $result['hood_ID'];
$gateway = $result['isgateway'];
if (!$result['readonly']) {
$sql = 'UPDATE nodes SET ip = :ip, mac = :mac, name = :name, `key` = :key,'
.' port = :port, timestamp = CURRENT_TIMESTAMP WHERE ID = :id';
try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':id', $result['ID'], PDO::PARAM_INT);
$rs->bindParam(':ip', $ip);
$rs->bindParam(':mac', $mac);
$rs->bindParam(':name', $name);
$rs->bindParam(':key', $key);
$rs->bindParam(':port', $port);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}
}
} else {
$sql = 'INSERT INTO nodes (ip, mac, name, `key`, port, readonly, isgateway, hood_ID)'
.' VALUES (:ip, :mac, :name, :key, :port, 0, 0, :hood);';
try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':ip', $ip);
$rs->bindParam(':mac', $mac);
$rs->bindParam(':name', $name);
$rs->bindParam(':key', $key);
$rs->bindParam(':port', $port);
$rs->bindParam(':hood', $hood);
$rs->execute();
} catch (PDOException $e) {
exit($e);
}
}
}

if ($gateway)
$sql = 'SELECT * FROM nodes WHERE hood_ID = :hood;';
$sql = 'SELECT * FROM nodes WHERE hood_ID = :hood;';
else
$sql = 'SELECT * FROM nodes WHERE hood_ID = :hood AND isgateway = \'1\';';
$sql = 'SELECT * FROM nodes WHERE hood_ID = :hood AND isgateway = \'1\';';

try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':hood', $hood);
$rs->execute();
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':hood', $hood);
$rs->execute();
} catch (PDOException $e) {
exit($e);
exit($e);
}

if ($rs->rowCount() > 0) {
while($result = $rs->fetch(PDO::FETCH_ASSOC))
{
$filename = $result['mac'];
if ($filename == '000000000000')
$filename = $result['name'];

echo "####".$filename.".conf\n";
echo "#name \"".$result['name']."\";\n";
echo "key \"".$result['key']."\";\n";
if (preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/", $result['ip']))
echo "remote ipv4 \"".$result['ip']."\" port ".$result['port']." float;\n";
else
echo "remote ipv6 \"".$result['ip']."\" port ".$result['port']." float;\n";
echo "\n";
}
echo "###\n";
while($result = $rs->fetch(PDO::FETCH_ASSOC))
{
$filename = $result['mac'];
if ($filename == '000000000000')
$filename = $result['name'];

echo "####".$filename.".conf\n";
echo "#name \"".$result['name']."\";\n";
echo "key \"".$result['key']."\";\n";
if (preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/", $result['ip']))
echo "remote ipv4 \"".$result['ip']."\" port ".$result['port']." float;\n";
else
echo "remote ipv6 \"".$result['ip']."\" port ".$result['port']." float;\n";
echo "\n";
}
echo "###\n";
}

/*
* DEFEKT
$sql = 'DELETE FROM nodes WHERE readonly = 0 AND `timestamp`+'.$delete_time.' < CURRENT_TIMESTAMP;';
try {
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':hood', $hood);
$rs->execute();
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':hood', $hood);
$rs->execute();
} catch (PDOException $e) {
exit($e);
exit($e);
}*/

?>

0 comments on commit f45f705

Please sign in to comment.