summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2013-11-05 20:25:55 +0100
committerSimon Rettberg2013-11-05 20:25:55 +0100
commite277baed387b642bb7e6707b49e55da4719a9c79 (patch)
tree350343881ddc35fe53862e971506bc405a9a7fad
parentTolle neue APIs Sachen (diff)
downloadslx-admin-e277baed387b642bb7e6707b49e55da4719a9c79.tar.gz
slx-admin-e277baed387b642bb7e6707b49e55da4719a9c79.tar.xz
slx-admin-e277baed387b642bb7e6707b49e55da4719a9c79.zip
Working on download of remote configs
-rw-r--r--apis/download.inc.php34
-rw-r--r--config.php5
-rw-r--r--inc/message.inc.php23
-rw-r--r--inc/render.inc.php3
-rw-r--r--inc/util.inc.php24
-rw-r--r--modules/sysconfig.inc.php58
-rw-r--r--script/custom.js6
-rw-r--r--templates/download-complete.html1
-rw-r--r--templates/main-menu.html2
-rw-r--r--templates/remote-tgz-list.html11
-rw-r--r--templates/tgz-list.html10
11 files changed, 165 insertions, 12 deletions
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 @@
+<?php
+
+User::load();
+
+if (!User::hasPermission('superadmin')) die('No permission');
+if (!isset($_REQUEST['type'])) die('No type');
+if (!isset($_REQUEST['file'])) die('No file');
+if (!isset($_REQUEST['id'])) die('No id');
+
+require_once('inc/render.inc.php');
+
+$type = $_REQUEST['type'];
+$file = preg_replace('/[^a-z0-9\.\-_]/is', '', $_REQUEST['file']);
+$id = $_REQUEST['id'];
+
+switch ($type) {
+case 'tgz':
+ $remote = CONFIG_REMOTE_TGZ;
+ $local = CONFIG_TGZ_LIST_DIR;
+ break;
+default:
+ die('Invalid download type');
+}
+
+if (file_exists($local . '/' . $file) && !isset($_REQUEST['exec'])) {
+ echo 'FILE EXISTS LOCALLY I SHOULD ASK FOR OVERWRITE NOW';
+ exit(0);
+}
+
+sleep(2);
+
+// No execution - just return dialog
+echo Render::parse('download-complete', array('file' => $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
,
' </div>
<script src="script/jquery.js"></script>
- <script src="script/bootstrap.min.js"></script></body>
+ <script src="script/bootstrap.min.js"></script>
+ <script src="script/custom.js"></script></body>
</html>'
;
}
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 @@
+<?php
+
+User::load();
+
+function render_module()
+{
+ if (!isset($_REQUEST['action'])) $_REQUEST['action'] = 'list';
+ switch ($_REQUEST['action']) {
+ case 'remotelist':
+ list_remote_configs();
+ break;
+ case 'list':
+ list_configs();
+ break;
+ default:
+ Message::addError('invalid-action', $_REQUEST['action']);
+ break;
+ }
+}
+
+function list_configs()
+{
+ if (!User::hasPermission('superadmin')) {
+ Message::addError('no-permission');
+ return;
+ }
+ $files = array();
+ foreach (glob(CONFIG_TGZ_LIST_DIR . '/*.tgz') as $file) {
+ $files[] = array(
+ 'file' => $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('<div class="progress progress-striped active"><div class="progress-bar" style="width:100%"><span class="sr-only">In Progress....</span></div></div>');
+ $(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 @@
+<div class="alert alert-success">Download von {{file}} abgeschlossen</div>
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 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Einstellungen<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="?do=baseconfig">Basis</a></li>
- <li><a href="?do=authconfig">Authentifizierung</a></li>
+ <li><a href="?do=sysconfig">System</a></li>
<li><a href="?do=printconfig">Drucken</a></li>
<li class="divider"></li>
<li class="dropdown-header">Nav header</li>
diff --git a/templates/remote-tgz-list.html b/templates/remote-tgz-list.html
new file mode 100644
index 00000000..e7abc78a
--- /dev/null
+++ b/templates/remote-tgz-list.html
@@ -0,0 +1,11 @@
+<div class="container">
+ {{#files}}
+ <div class="panel panel-default">
+ <div class="panel-heading"><h4><a class="btn btn-success" href="#" onclick="this.style.display='none';loadContent('#download{{id}}', 'api.php?do=download&amp;type=tgz&amp;file={{file}}&amp;id={{id}}')">Download</a> &raquo; {{file}}</h4></div>
+ <div class="panel-body" id="download{{id}}">{{description}}</div>
+ </div>
+ {{/files}}
+ {{^files}}
+ <div class="row well well-sm">Keine Konfigurationspakete gefunden!</div>
+ {{/files}}
+</div>
diff --git a/templates/tgz-list.html b/templates/tgz-list.html
new file mode 100644
index 00000000..fec5af63
--- /dev/null
+++ b/templates/tgz-list.html
@@ -0,0 +1,10 @@
+<div class="container">
+ {{#files}}
+ <div class="row well well-sm">{{file}}</div>
+ {{/files}}
+ {{^files}}
+ <div class="row well well-sm">Keine Konfigurationspakete gefunden!</div>
+ {{/files}}
+ <a class="btn btn-lg btn-primary" href="?do=sysconfig&amp;action=remotelist">Konfigurationen herunterladen</a>
+ <a class="btn btn-lg btn-primary" href="?do=sysconfig&amp;action=upload">Eigene Konfiguration hochladen</a>
+</div>