summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2014-05-23 20:49:02 +0200
committerSimon Rettberg2014-05-23 20:49:02 +0200
commitfe6ac16498b05d0f0c8ed7fda394273815d3d6da (patch)
treecadf5f103ef3db7ba1b40d59d85937c998aad22f /inc
parentServer Setup page (diff)
downloadslx-admin-fe6ac16498b05d0f0c8ed7fda394273815d3d6da.tar.gz
slx-admin-fe6ac16498b05d0f0c8ed7fda394273815d3d6da.tar.xz
slx-admin-fe6ac16498b05d0f0c8ed7fda394273815d3d6da.zip
Stuff (WIP)
Diffstat (limited to 'inc')
-rw-r--r--inc/configmodule.inc.php50
-rw-r--r--inc/database.inc.php1
-rw-r--r--inc/message.inc.php9
-rw-r--r--inc/property.inc.php22
-rw-r--r--inc/taskmanager.inc.php7
-rw-r--r--inc/trigger.inc.php40
-rw-r--r--inc/util.inc.php43
7 files changed, 167 insertions, 5 deletions
diff --git a/inc/configmodule.inc.php b/inc/configmodule.inc.php
new file mode 100644
index 00000000..55f76cf3
--- /dev/null
+++ b/inc/configmodule.inc.php
@@ -0,0 +1,50 @@
+<?php
+
+class ConfigModule
+{
+
+ public static function insertAdConfig($title, $server, $searchbase, $binddn, $bindpw)
+ {
+ // TODO: Lock table, race condition if about 500 admins insert a config at the same time
+ Database::exec("INSERT INTO configtgz_module (title, moduletype, filepath, contents) "
+ . " VALUES (:title, 'AD_AUTH', '', '')", array('title' => $title));
+ $id = Database::lastInsertId();
+ if (!is_numeric($id)) Util::traceError('Inserting new AD config to DB did not yield a numeric insert id');
+ // Entry created, now try to get a free port for the proxy
+ $res = Database::simpleQuery("SELECT moduleid, contents FROM configtgz_module");
+ $ports = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($row['moduleid'] == $id) {
+ // ...
+ } else {
+ $data = json_decode($row['contents'], true);
+ if (isset($data['proxyport'])) $ports[] = $data['proxyport'];
+ }
+ }
+ $port = 3300;
+ while (in_array($port, $ports)) {
+ $port++;
+ }
+ // Port determined, carry on...
+ $ownEntry = array(
+ 'server' => $server,
+ 'searchbase' => $searchbase,
+ 'binddn' => $binddn,
+ 'bindpw' => $bindpw,
+ 'proxyport' => $port
+ );
+ $data = json_encode($ownEntry);
+ if ($data === false) Util::traceError('Serializing the AD data failed.');
+ $name = CONFIG_TGZ_LIST_DIR . '/modules/AD_AUTH_id_' . $id . '.' . mt_rand() . '.tgz';
+ Database::exec("UPDATE configtgz_module SET filepath = :filename, contents = :contents WHERE moduleid = :id LIMIT 1", array(
+ 'id' => $id,
+ 'filename' => $name,
+ 'contents' => json_encode($ownEntry)
+ ));
+ // Add archive file name to array before returning it
+ $ownEntry['moduleid'] = $id;
+ $ownEntry['filename'] = $name;
+ return $ownEntry;
+ }
+
+}
diff --git a/inc/database.inc.php b/inc/database.inc.php
index 70f50116..a646e823 100644
--- a/inc/database.inc.php
+++ b/inc/database.inc.php
@@ -59,6 +59,7 @@ class Database
* Note that this will re-use PDOStatements, so if you run the same
* query again with different params, do not rely on the first PDOStatement
* 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())
{
diff --git a/inc/message.inc.php b/inc/message.inc.php
index cab8fbd3..2ed9e79a 100644
--- a/inc/message.inc.php
+++ b/inc/message.inc.php
@@ -19,8 +19,9 @@ $error_text = array(
'missing-file' => 'Es wurde keine Datei ausgewählt!',
'invalid-file' => 'Die Datei {{0}} existiert nicht!',
'upload-complete' => 'Upload von {{0}} war erfolgreich',
- 'upload-failed' => 'Upload von {{0}} schlug fehl!',
- 'config-activated' => 'Konfiguration wurde aktiviert',
+ 'upload-failed' => 'Upload schlug fehl: {{0}}',
+ 'config-activated' => 'Konfiguration {{0}} wurde aktiviert',
+ 'config-invalid' => 'Konfiguration mit ID {{0}} existiert nicht',
'error-write' => 'Fehler beim Schreiben von {{0}}',
'error-read' => 'Fehler beim Lesen von {{0}}',
'error-archive' => 'Korruptes Archiv oder nicht unterstütztes Format',
@@ -29,6 +30,8 @@ $error_text = array(
'empty-archive' => 'Das Archiv enthält keine Dateien oder Verzeichnisse',
'error-extract' => 'Konnte Archiv nicht nach {{0}} entpacken - {{1}}',
'module-added' => 'Modul erfolgreich hinzugefügt',
+ 'module-deleted' => 'Modul {{0}} wurde gelöscht',
+ 'module-in-use' => 'Modul <b>{{0}}</b> wird noch durch Konfiguration <b>{{1}}</b> verwendet',
'taskmanager-error' => 'Verbindung zum Taskmanager fehlgeschlagen',
'taskmanager-format' => 'Taskmanager hat ungültige Daten zurückgeliefert',
'task-error' => 'Ausführung fehlgeschlagen: {{0}}',
@@ -93,7 +96,7 @@ class Message
foreach (self::$list as $item) {
$message = $error_text[$item['id']];
foreach ($item['params'] as $index => $text) {
- $message = str_replace('{{' . $index . '}}', $text, $message);
+ $message = str_replace('{{' . $index . '}}', '<b>' . htmlspecialchars($text) . '</b>', $message);
}
Render::addTemplate('messagebox-' . $item['type'], array('message' => $message));
self::$alreadyDisplayed[] = $item;
diff --git a/inc/property.inc.php b/inc/property.inc.php
index a1c252a5..c6f3e8ad 100644
--- a/inc/property.inc.php
+++ b/inc/property.inc.php
@@ -54,5 +54,25 @@ class Property
{
self::set('server-ip', $value);
}
+
+ public static function getIPxeIp()
+ {
+ return self::get('ipxe-ip', 'none');
+ }
+
+ 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);
+ }
-} \ No newline at end of file
+}
diff --git a/inc/taskmanager.inc.php b/inc/taskmanager.inc.php
index 27e79dea..e93cc696 100644
--- a/inc/taskmanager.inc.php
+++ b/inc/taskmanager.inc.php
@@ -66,12 +66,17 @@ class Taskmanager
public static function waitComplete($taskId)
{
+ $done = false;
for ($i = 0; $i < 10; ++$i) {
$status = self::status($taskId);
if (!isset($status['statusCode'])) break;
- if ($status['statusCode'] != TASK_PROCESSING && $status['statusCode'] != TASK_WAITING) break;
+ if ($status['statusCode'] != TASK_PROCESSING && $status['statusCode'] != TASK_WAITING) {
+ $done = true;
+ break;
+ }
usleep(150000);
}
+ if ($done) self::release ($taskId);
return $status;
}
diff --git a/inc/trigger.inc.php b/inc/trigger.inc.php
new file mode 100644
index 00000000..40d9c491
--- /dev/null
+++ b/inc/trigger.inc.php
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * This is one giant class containing various functions that will generate
+ * required config files, daemon instances and more, mostly through the Taskmanager.
+ * Most function *should* only actually do something if it is required to do so.
+ * eg. a "launchSomething" function should only launch something if it isn't already
+ * running. Checking if something is running can happen in that very function, or in
+ * a task that the function is calling.
+ */
+class Trigger
+{
+
+ /**
+ * Compile iPXE pxelinux menu. Needs to be done whenever the server's IP
+ * address changes.
+ *
+ * @param boolean $force force recompilation even if it seems up to date
+ * @return boolean|string true if already up to date, false if launching task failed, task-id otherwise
+ */
+ public static function ipxe($force = false)
+ {
+ if (!$force && Property::getIPxeIp() === Property::getServerIp())
+ return true; // Nothing to do
+ $last = Property::getIPxeTaskId();
+ if ($last !== false) {
+ $status = Taskmanager::status($last);
+ if (isset($status['statusCode']) && ($status['statusCode'] === TASK_WAITING || $status['statusCode'] === TASK_RUNNING))
+ return false; // Already compiling
+ }
+ $task = Taskmanager::submit('CompileIPxe', array(
+ 'ip' => Property::getServerIp()
+ ));
+ if (!isset($task['id']))
+ return false;
+ Property::setIPxeTaskId($task['id']);
+ return $task['id'];
+ }
+
+} \ No newline at end of file
diff --git a/inc/util.inc.php b/inc/util.inc.php
index 4b974f6d..63680023 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -173,6 +173,49 @@ class Util
}
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . $sz[$factor];
}
+
+ public static function sanitizeFilename($name)
+ {
+ return preg_replace('/[^a-zA-Z0-9_\-]+/', '_', $name);
+ }
+
+ /**
+ * Create human readable error description from a $_FILES[<..>]['error'] code
+ *
+ * @param int $code the code to turn into an error description
+ * @return string the error description
+ */
+ public static function uploadErrorString($code)
+ {
+ switch ($code) {
+ case UPLOAD_ERR_INI_SIZE:
+ $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
+ break;
+ case UPLOAD_ERR_FORM_SIZE:
+ $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
+ break;
+ case UPLOAD_ERR_PARTIAL:
+ $message = "The uploaded file was only partially uploaded";
+ break;
+ case UPLOAD_ERR_NO_FILE:
+ $message = "No file was uploaded";
+ break;
+ case UPLOAD_ERR_NO_TMP_DIR:
+ $message = "Missing a temporary folder";
+ break;
+ case UPLOAD_ERR_CANT_WRITE:
+ $message = "Failed to write file to disk";
+ break;
+ case UPLOAD_ERR_EXTENSION:
+ $message = "File upload stopped by extension";
+ break;
+
+ default:
+ $message = "Unknown upload error";
+ break;
+ }
+ return $message;
+ }
}