From e277baed387b642bb7e6707b49e55da4719a9c79 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 5 Nov 2013 20:25:55 +0100 Subject: Working on download of remote configs --- apis/download.inc.php | 34 +++++++++++++++++++++++ config.php | 5 ++++ inc/message.inc.php | 23 +++++++++------- inc/render.inc.php | 3 ++- inc/util.inc.php | 24 +++++++++++++++++ modules/sysconfig.inc.php | 58 ++++++++++++++++++++++++++++++++++++++++ script/custom.js | 6 +++++ templates/download-complete.html | 1 + templates/main-menu.html | 2 +- templates/remote-tgz-list.html | 11 ++++++++ templates/tgz-list.html | 10 +++++++ 11 files changed, 165 insertions(+), 12 deletions(-) create mode 100644 apis/download.inc.php create mode 100644 modules/sysconfig.inc.php create mode 100644 script/custom.js create mode 100644 templates/download-complete.html create mode 100644 templates/remote-tgz-list.html create mode 100644 templates/tgz-list.html diff --git a/apis/download.inc.php b/apis/download.inc.php new file mode 100644 index 00000000..9ba0f008 --- /dev/null +++ b/apis/download.inc.php @@ -0,0 +1,34 @@ + $file)); + diff --git a/config.php b/config.php index dbe75519..5b44d1cf 100644 --- a/config.php +++ b/config.php @@ -13,3 +13,8 @@ define('CONFIG_SQL_USER', 'openslx'); define('CONFIG_SQL_PASS', 'geheim'); //define('CONFIG_SQL_DB', 'openslx'); +define('CONFIG_TGZ_LIST_DIR', '/tmp/configs'); +define('CONFIG_HTTP_DIR', '/tmp/active-config'); + +define('CONFIG_REMOTE_TGZ', 'http://127.0.0.1/fakeremote'); + diff --git a/inc/message.inc.php b/inc/message.inc.php index 4da277e7..62c68e98 100644 --- a/inc/message.inc.php +++ b/inc/message.inc.php @@ -2,16 +2,19 @@ // TODO: Move to extra file $error_text = array( - 'loginfail' => 'Benutzername oder Kennwort falsch', - 'token' => 'Ungültiges Token. CSRF Angriff?', - 'adduser-disabled' => 'Keine ausreichenden Rechte, um weitere Benutzer hinzuzufügen', - 'password-mismatch' => 'Passwort und Passwortbestätigung stimmen nicht überein', - 'empty-field' => 'Ein Feld wurde nicht ausgefüllt', - 'adduser-success' => 'Benutzer erfolgreich hinzugefügt', - 'no-permission' => 'Keine ausreichenden Rechte, um auf diese Seite zuzugreifen', - 'settings-updated' => 'Einstellungen wurden aktualisiert', - 'debug-mode' => 'Der Debug-Modus ist aktiv!', - 'value-invalid' => 'Der Wert {{1}} ist ungültig für die Option {{0}} und wurde ignoriert', + 'loginfail' => 'Benutzername oder Kennwort falsch', + 'token' => 'Ungültiges Token. CSRF Angriff?', + 'adduser-disabled' => 'Keine ausreichenden Rechte, um weitere Benutzer hinzuzufügen', + 'password-mismatch' => 'Passwort und Passwortbestätigung stimmen nicht überein', + 'empty-field' => 'Ein Feld wurde nicht ausgefüllt', + 'adduser-success' => 'Benutzer erfolgreich hinzugefügt', + 'no-permission' => 'Keine ausreichenden Rechte, um auf diese Seite zuzugreifen', + 'settings-updated' => 'Einstellungen wurden aktualisiert', + 'debug-mode' => 'Der Debug-Modus ist aktiv!', + 'value-invalid' => 'Der Wert {{1}} ist ungültig für die Option {{0}} und wurde ignoriert', + 'invalid-action' => 'Ungültige Aktion: {{0}}', + 'remote-timeout' => 'Konnte Ressource {{0}} nicht herunterladen', + 'remote-parse-failed' => 'Parsen der empfangenen Daten fehlgeschlagen', ); class Message diff --git a/inc/render.inc.php b/inc/render.inc.php index 41b8e2b8..dfe91ccb 100644 --- a/inc/render.inc.php +++ b/inc/render.inc.php @@ -52,7 +52,8 @@ class Render , ' - + + ' ; } diff --git a/inc/util.inc.php b/inc/util.inc.php index f456d164..591c7e79 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -72,5 +72,29 @@ class Util return nl2br($string); } + /** + * Download file, obey given timeout in seconds + * Return data on success, false on failure + */ + function download($url, $timeout, &$code) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HEADER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, ceil($timeout / 2)); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $data = curl_exec($ch); + $data = explode("\r\n\r\n", $data, 2); + if (preg_match('#^HTTP/\d+\.\d+ (\d+) #', $data[0], $out)) { + $code = (int)$out[1]; + } else { + $code = 999; + } + curl_close($ch); + if (count($data) < 2) return ''; + return $data[1]; + } + } diff --git a/modules/sysconfig.inc.php b/modules/sysconfig.inc.php new file mode 100644 index 00000000..f8e6c6ad --- /dev/null +++ b/modules/sysconfig.inc.php @@ -0,0 +1,58 @@ + $file + ); + } + Render::addTemplate('tgz-list', array('files' => $files)); +} + +function list_remote_configs() +{ + if (!User::hasPermission('superadmin')) { + Message::addError('no-permission'); + return; + } + $data = Util::download(CONFIG_REMOTE_TGZ . '/list', 4, $code); + if ($code !== 200) { + Message::addError('remote-timeout', CONFIG_REMOTE_TGZ); + return; + } + $list = json_decode($data, true); + if (!is_array($list)) { + Message::addError('remote-parse-failed'); + return; + } + $id = 0; + foreach ($list as &$item) { + $item['id'] = ++$id; + } + Render::addTemplate('remote-tgz-list', array('files' => $list)); +} + diff --git a/script/custom.js b/script/custom.js new file mode 100644 index 00000000..f2906813 --- /dev/null +++ b/script/custom.js @@ -0,0 +1,6 @@ +function loadContent(elem, source) +{ + $(elem).html('
In Progress....
'); + $(elem).load(source); +} + diff --git a/templates/download-complete.html b/templates/download-complete.html new file mode 100644 index 00000000..619f2551 --- /dev/null +++ b/templates/download-complete.html @@ -0,0 +1 @@ +
Download von {{file}} abgeschlossen
diff --git a/templates/main-menu.html b/templates/main-menu.html index d3d88d0f..021daf6d 100644 --- a/templates/main-menu.html +++ b/templates/main-menu.html @@ -17,7 +17,7 @@ Einstellungen