From fe6ac16498b05d0f0c8ed7fda394273815d3d6da Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 23 May 2014 20:49:02 +0200 Subject: Stuff (WIP) --- inc/configmodule.inc.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ inc/database.inc.php | 1 + inc/message.inc.php | 9 ++++++--- inc/property.inc.php | 22 ++++++++++++++++++++- inc/taskmanager.inc.php | 7 ++++++- inc/trigger.inc.php | 40 ++++++++++++++++++++++++++++++++++++++ inc/util.inc.php | 43 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 inc/configmodule.inc.php create mode 100644 inc/trigger.inc.php (limited to 'inc') 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 @@ + $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 {{0}} wird noch durch Konfiguration {{1}} 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 . '}}', '' . htmlspecialchars($text) . '', $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 @@ + 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; + } } -- cgit v1.2.3-55-g7522