summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2014-05-28 18:18:34 +0200
committerSimon Rettberg2014-05-28 18:18:34 +0200
commite4e79982dd3c447a4ced762a6069db553e246f59 (patch)
treed17f74565ed5347350edeab412a07c77bd80a636 /inc
parentWIP (diff)
downloadslx-admin-e4e79982dd3c447a4ced762a6069db553e246f59.tar.gz
slx-admin-e4e79982dd3c447a4ced762a6069db553e246f59.tar.xz
slx-admin-e4e79982dd3c447a4ced762a6069db553e246f59.zip
Fixed some bugs from Sateserver v05
Diffstat (limited to 'inc')
-rw-r--r--inc/database.inc.php34
-rw-r--r--inc/message.inc.php2
-rw-r--r--inc/property.inc.php57
-rw-r--r--inc/trigger.inc.php4
4 files changed, 59 insertions, 38 deletions
diff --git a/inc/database.inc.php b/inc/database.inc.php
index a646e823..e7a16ba1 100644
--- a/inc/database.inc.php
+++ b/inc/database.inc.php
@@ -6,6 +6,7 @@
*/
class Database
{
+
private static $dbh = false;
private static $statements = array();
@@ -14,7 +15,8 @@ class Database
*/
private static function init()
{
- if (self::$dbh !== false) return;
+ if (self::$dbh !== false)
+ return;
try {
self::$dbh = new PDO(CONFIG_SQL_DSN, CONFIG_SQL_USER, CONFIG_SQL_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $e) {
@@ -26,24 +28,31 @@ class Database
* If you just need the first row of a query you can use this.
* Will return an associative array, or false if no row matches the query
*/
- public static function queryFirst($query, $args = array())
+ public static function queryFirst($query, $args = array(), $ignoreError = false)
{
- $res = self::simpleQuery($query, $args);
- if ($res === false) return false;
+ $res = self::simpleQuery($query, $args, $ignoreError);
+ if ($res === false)
+ return false;
return $res->fetch(PDO::FETCH_ASSOC);
}
+
/**
* Execute the given query and return the number of rows affected.
* Mostly useful for UPDATEs or INSERTs
+ *
+ * @param string $query Query to run
+ * @param array $args Arguments to query
+ * @return int|boolean Number of rows affected, or false on error
*/
- public static function exec($query, $args = array())
+ public static function exec($query, $args = array(), $ignoreError = false)
{
- $res = self::simpleQuery($query, $args);
- if ($res === false) return false;
+ $res = self::simpleQuery($query, $args, $ignoreError);
+ if ($res === false)
+ return false;
return $res->rowCount();
}
-
+
/**
* Get id (promary key) of last row inserted.
*
@@ -61,7 +70,7 @@ class Database
* still being valid. If you need to do something fancy, use Database::prepare
* @return \PDOStatement The query result object
*/
- public static function simpleQuery($query, $args = array())
+ public static function simpleQuery($query, $args = array(), $ignoreError = false)
{
self::init();
try {
@@ -71,11 +80,15 @@ class Database
self::$statements[$query]->closeCursor();
}
if (self::$statements[$query]->execute($args) === false) {
+ if ($ignoreError)
+ return false;
Util::traceError("Database Error: \n" . implode("\n", self::$statements[$query]->errorInfo()));
}
return self::$statements[$query];
} catch (Exception $e) {
- return false;
+ if ($ignoreError)
+ return false;
+ Util::traceError("Database Error: \n" . $e->getMessage());
}
}
@@ -90,4 +103,3 @@ class Database
}
}
-
diff --git a/inc/message.inc.php b/inc/message.inc.php
index 75292e09..b62f234f 100644
--- a/inc/message.inc.php
+++ b/inc/message.inc.php
@@ -93,6 +93,7 @@ class Message
public static function renderList()
{
global $error_text;
+ if (!self::$flushed) Render::openTag('div', array('class' => 'container'));
foreach (self::$list as $item) {
$message = $error_text[$item['id']];
foreach ($item['params'] as $index => $text) {
@@ -101,6 +102,7 @@ class Message
Render::addTemplate('messagebox-' . $item['type'], array('message' => $message));
self::$alreadyDisplayed[] = $item;
}
+ if (!self::$flushed) Render::closeTag('div');
self::$list = array();
self::$flushed = true;
}
diff --git a/inc/property.inc.php b/inc/property.inc.php
index 5c316517..77d2b985 100644
--- a/inc/property.inc.php
+++ b/inc/property.inc.php
@@ -6,8 +6,9 @@
*/
class Property
{
+
private static $cache = false;
-
+
/**
* Retrieve value from property store.
*
@@ -18,90 +19,96 @@ class Property
private static function get($key, $default = false)
{
if (self::$cache === false) {
+ if (mt_rand(1, 20) === 10) {
+ Database::exec("DELETE FROM property WHERE dateline <> 0 AND dateline < UNIX_TIMESTAMP()");
+ }
$res = Database::simpleQuery("SELECT name, value FROM property");
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
self::$cache[$row['name']] = $row['value'];
}
}
- if (!isset(self::$cache[$key])) return $default;
+ if (!isset(self::$cache[$key]))
+ return $default;
return self::$cache[$key];
}
-
+
/**
* Set value in property store.
*
* @param string $key key of value to set
* @param type $value the value to store for $key
+ * @param int minage how long to keep this entry around at least, in minutes. 0 for infinite
*/
- private static function set($key, $value)
+ private static function set($key, $value, $minage = 0)
{
- Database::exec("INSERT INTO property (name, value) VALUES (:key, :value)"
- . " ON DUPLICATE KEY UPDATE value = VALUES(value)", array(
- 'key' => $key,
- 'value' => $value
- ));
+ Database::exec("INSERT INTO property (name, value, dateline) VALUES (:key, :value, :dateline)"
+ . " ON DUPLICATE KEY UPDATE value = VALUES(value), dateline = VALUES(dateline)", array(
+ 'key' => $key,
+ 'value' => $value,
+ 'dateline' => time() + ($minage * 60)
+ ));
if (self::$cache !== false) {
self::$cache[$key] = $value;
}
}
-
+
public static function getServerIp()
{
return self::get('server-ip', 'none');
}
-
+
public static function setServerIp($value)
{
self::set('server-ip', $value);
}
-
+
public static function getIPxeIp()
{
- return self::get('ipxe-ip', 'none');
+ return self::get('ipxe-ip', 'not-set');
}
-
+
public static function setIPxeIp($value)
{
self::set('ipxe-ip', $value);
}
-
+
public static function getIPxeTaskId()
{
return self::get('ipxe-task');
}
-
+
public static function setIPxeTaskId($value)
{
self::set('ipxe-task', $value);
}
-
+
public static function getBootMenu()
{
return json_decode(self::get('ipxe-menu'), true);
}
-
+
public static function setBootMenu($value)
{
self::set('ipxe-menu', json_encode($value));
}
-
+
public static function getVersionCheckTaskId()
{
return self::get('versioncheck-task');
}
-
+
public static function setVersionCheckTaskId($value)
{
self::set('versioncheck-task', $value);
}
-
+
public static function getVersionCheckInformation()
{
$data = json_decode(self::get('versioncheck-data'), true);
if (isset($data['time']) && $data['time'] + 120 > time())
return $data;
$task = Taskmanager::submit('DownloadText', array(
- 'url' => CONFIG_REMOTE_ML . '/list.php'
+ 'url' => CONFIG_REMOTE_ML . '/list.php'
));
if (!isset($task['id']))
return false;
@@ -116,17 +123,17 @@ class Property
self::setVersionCheckInformation($data);
return $data;
}
-
+
public static function setVersionCheckInformation($value)
{
self::set('versioncheck-data', json_encode($value));
}
-
+
public static function getVmStoreConfig()
{
return json_decode(self::get('vmstore-config'), true);
}
-
+
public static function setVmStoreConfig($value)
{
self::set('vmstore-config', json_encode($value));
diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php
index 102f7987..b7cc67cc 100644
--- a/inc/trigger.inc.php
+++ b/inc/trigger.inc.php
@@ -39,7 +39,7 @@ class Trigger
public static function ldadp()
{
- $res = Database::simpleQuery("SELECT moduleid, filepath FROM configtgz_module"
+ $res = Database::simpleQuery("SELECT moduleid, configtgz.filepath FROM configtgz_module"
. " INNER JOIN configtgz_x_module USING (moduleid)"
. " INNER JOIN configtgz USING (configid)"
. " WHERE moduletype = 'AD_AUTH'");
@@ -65,7 +65,7 @@ class Trigger
if (!is_array($vmstore)) return;
$storetype = $vmstore['storetype'];
if ($storetype === 'nfs') $addr = $vmstore['nfsaddr'];
- if ($storetype === 'cifs') $addr = $vmstore['nfsaddr'];
+ if ($storetype === 'cifs') $addr = $vmstore['cifsaddr'];
if ($storetype === 'internal') $addr = 'none';
$this->mountTask = Taskmanager::submit('MountVmStore', array(
'address' => $addr,