diff options
author | Simon Rettberg | 2014-05-15 18:28:24 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-05-15 18:28:24 +0200 |
commit | 63c0cf521f8097b0dadaf1228176dc38c7d897f6 (patch) | |
tree | 83f5da6dc130ac7db575b0eee41ed6c7a2f994fb /inc | |
parent | Fix handle leak in downloading, better error reporting on failed downloads, a... (diff) | |
download | slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.tar.gz slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.tar.xz slx-admin-63c0cf521f8097b0dadaf1228176dc38c7d897f6.zip |
Working on config.tgz composition through config modules
Diffstat (limited to 'inc')
-rw-r--r-- | inc/database.inc.php (renamed from inc/db.inc.php) | 31 | ||||
-rw-r--r-- | inc/message.inc.php | 14 | ||||
-rw-r--r-- | inc/render.inc.php | 18 | ||||
-rw-r--r-- | inc/request.inc.php | 45 | ||||
-rw-r--r-- | inc/session.inc.php | 6 | ||||
-rw-r--r-- | inc/taskmanager.inc.php | 86 | ||||
-rw-r--r-- | inc/util.inc.php | 19 |
7 files changed, 208 insertions, 11 deletions
diff --git a/inc/db.inc.php b/inc/database.inc.php index a797ae93..70f50116 100644 --- a/inc/db.inc.php +++ b/inc/database.inc.php @@ -43,6 +43,16 @@ class Database if ($res === false) return false; return $res->rowCount(); } + + /** + * Get id (promary key) of last row inserted. + * + * @return int the id + */ + public static function lastInsertId() + { + return self::$dbh->lastInsertId(); + } /** * Execute the given query and return the corresponding PDOStatement object @@ -53,16 +63,19 @@ class Database public static function simpleQuery($query, $args = array()) { self::init(); - //if (empty($args)) Util::traceError('Query with zero arguments!'); - if (!isset(self::$statements[$query])) { - self::$statements[$query] = self::$dbh->prepare($query); - } else { - self::$statements[$query]->closeCursor(); - } - if (self::$statements[$query]->execute($args) === false) { - Util::traceError("Database Error: \n" . implode("\n", self::$statements[$query]->errorInfo())); + try { + if (!isset(self::$statements[$query])) { + self::$statements[$query] = self::$dbh->prepare($query); + } else { + self::$statements[$query]->closeCursor(); + } + if (self::$statements[$query]->execute($args) === false) { + Util::traceError("Database Error: \n" . implode("\n", self::$statements[$query]->errorInfo())); + } + return self::$statements[$query]; + } catch (Exception $e) { + return false; } - return self::$statements[$query]; } /** diff --git a/inc/message.inc.php b/inc/message.inc.php index 3117630b..6c95764c 100644 --- a/inc/message.inc.php +++ b/inc/message.inc.php @@ -1,6 +1,7 @@ <?php // TODO: Move to extra file +global $error_text; $error_text = array( 'loginfail' => 'Benutzername oder Kennwort falsch', 'token' => 'Ungültiges Token. CSRF Angriff?', @@ -21,11 +22,21 @@ $error_text = array( 'upload-failed' => 'Upload von {{0}} schlug fehl!', 'config-activated' => 'Konfiguration wurde aktiviert', 'error-write' => 'Fehler beim Schreiben von {{0}}', + 'error-read' => 'Fehler beim Lesen von {{0}}', + 'error-archive' => 'Korruptes Archiv oder nicht unterstütztes Format', + 'error-rename' => 'Konnte {{0}} nicht in {{1}} umbenennen', + 'error-nodir' => 'Das Verzeichnis {{0}} existiert nicht.', + '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', + 'taskmanager-error' => 'Verbindung zum Taskmanager fehlgeschlagen', + 'task-error' => 'Ausführung fehlgeschlagen: {{0}}', ); class Message { private static $list = array(); + private static $alreadyDisplayed = array(); private static $flushed = false; /** @@ -83,6 +94,7 @@ class Message $message = str_replace('{{' . $index . '}}', $text, $message); } Render::addTemplate('messagebox-' . $item['type'], array('message' => $message)); + self::$alreadyDisplayed[] = $item; } self::$list = array(); self::$flushed = true; @@ -109,7 +121,7 @@ class Message public static function toRequest() { $parts = array(); - foreach (self::$list as $item) { + foreach (array_merge(self::$list, self::$alreadyDisplayed) as $item) { $str = 'message[]=' . urlencode($item['type'] . '|' .$item['id']); if (!empty($item['params'])) { $str .= '|' . implode('|', $item['params']); diff --git a/inc/render.inc.php b/inc/render.inc.php index a3cef516..dff32798 100644 --- a/inc/render.inc.php +++ b/inc/render.inc.php @@ -39,6 +39,7 @@ class Render <html> <head> <title>', RENDER_DEFAULT_TITLE, self::$title, '</title> + <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap --> <link href="style/bootstrap.min.css" rel="stylesheet" media="screen"> @@ -93,6 +94,23 @@ class Render { self::$body .= self::$mustache->render(self::getTemplate($template), $params); } + + /** + * Add a dialog to the page output. + * + * @param string $title Title of the dialog window + * @param boolean $next URL to next dialog step, or false to hide the next button + * @param string $template template used to fill the dialog body + * @param array $params parameters for rendering the body template + */ + public static function addDialog($title, $next, $template, $params = false) + { + self::addTemplate('dialog-generic', array( + 'title' => $title, + 'next' => $next, + 'body' => self::$mustache->render(self::getTemplate($template), $params) + )); + } /** * Add error message to page diff --git a/inc/request.inc.php b/inc/request.inc.php new file mode 100644 index 00000000..bb212dfd --- /dev/null +++ b/inc/request.inc.php @@ -0,0 +1,45 @@ +<?php + +/** + * Wrapper for getting fields from the request (GET, POST, ...) + */ +class Request +{ + + /** + * + * @param string $key Key of field to get from $_GET + * @param string $default Value to return if $_GET does not contain $key + * @return mixed Field from $_GET, or $default if not set + */ + public static function get($key, $default = false) + { + if (!isset($_GET[$key])) return $default; + return $_GET[$key]; + } + + /** + * + * @param string $key Key of field to get from $_POST + * @param string $default Value to return if $_POST does not contain $key + * @return mixed Field from $_POST, or $default if not set + */ + public static function post($key, $default = false) + { + if (!isset($_POST[$key])) return $default; + return $_POST[$key]; + } + + /** + * + * @param string $key Key of field to get from $_REQUEST + * @param string $default Value to return if $_REQUEST does not contain $key + * @return mixed Field from $_REQUEST, or $default if not set + */ + public static function any($key, $default = false) + { + if (!isset($_REQUEST[$key])) return $default; + return $_REQUEST[$key]; + } + +} diff --git a/inc/session.inc.php b/inc/session.inc.php index 3ba614f2..a0f8ab4c 100644 --- a/inc/session.inc.php +++ b/inc/session.inc.php @@ -52,7 +52,11 @@ class Session public static function set($key, $value) { if (self::$data === false) Util::traceError('Tried to set session data with no active session'); - self::$data[$key] = $value; + if ($value === false) { + unset(self::$data[$key]); + } else { + self::$data[$key] = $value; + } } private static function loadSessionId() diff --git a/inc/taskmanager.inc.php b/inc/taskmanager.inc.php new file mode 100644 index 00000000..f2f337be --- /dev/null +++ b/inc/taskmanager.inc.php @@ -0,0 +1,86 @@ +<?php + +/** + * Interface to the external task manager. + */ +class Taskmanager +{ + + private static $sock = false; + + private static function init() + { + if (self::$sock !== false) return; + self::$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + socket_set_option(self::$sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0, 'usec' => 100000)); + socket_set_option(self::$sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 0, 'usec' => 100000)); + socket_connect(self::$sock, '127.0.0.1', 9215); + } + + public static function submit($task, $data, $async) + { + self::init(); + $seq = (string)mt_rand(); + $data = json_encode($data); + $message = "$seq, $task, $data"; + $sent = socket_send(self::$sock, $message, strlen($message), 0); + if ($async) return true; + $reply = self::readReply($seq); + if (!is_array($reply)) return false; + return $reply; + } + + public static function status($taskId) + { + self::init(); + $seq = (string)mt_rand(); + $message = "$seq, status, $taskId"; + $sent = socket_send(self::$sock, $message, strlen($message), 0); + $reply = self::readReply($seq); + if (!is_array($reply)) return false; + return $reply; + } + + public static function waitComplete($taskId) + { + 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; + usleep(150000); + } + return $status; + } + + public static function release($taskId) + { + self::init(); + $seq = (string)mt_rand(); + $message = "$seq, release, $taskId"; + socket_send(self::$sock, $message, strlen($message), 0); + } + + /** + * + * @param type $seq + * @return mixed the decoded json data for that message as an array, or null on error + */ + private static function readReply($seq) + { + $tries = 0; + while (($bytes = socket_recvfrom(self::$sock, $buf, 90000, 0, $bla1, $bla2)) !== false) { + $parts = explode(',', $buf, 2); + if (count($parts) == 2 && $parts[0] == $seq) { + return json_decode($parts[1], true); + } + if (++$tries > 10) return false; + } + //error_log(socket_strerror(socket_last_error(self::$sock))); + return false; + } + +} + +foreach (array('TASK_FINISHED', 'TASK_ERROR', 'TASK_WAITING', 'NO_SUCH_TASK', 'TASK_PROCESSING') as $i) { + define($i, $i); +} diff --git a/inc/util.inc.php b/inc/util.inc.php index 67e4b73d..8235edd0 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -150,6 +150,25 @@ class Util } return true; } + + /** + * Convert given number to human readable file size string. + * Will append Bytes, KiB, etc. depending on magnitude of number. + * + * @param type $bytes numeric value of the filesize to make readable + * @param type $decimals number of decimals to show, -1 for automatic + * @return type human readable string representing the given filesize + */ + public static function readableFileSize($bytes, $decimals = -1) { + static $sz = array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); + $factor = floor((strlen($bytes) - 1) / 3); + if ($factor == 0) { + $decimals = 0; + } elseif ($decimals === -1) { + $decimals = 2 - floor((strlen($bytes) - 1) % 3); + } + return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . $sz[$factor]; + } } |