gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r15319 - in gauger: bindings/js web web/css


From: gnunet
Subject: [GNUnet-SVN] r15319 - in gauger: bindings/js web web/css
Date: Thu, 26 May 2011 18:53:10 +0200

Author: bartpolot
Date: 2011-05-26 18:53:10 +0200 (Thu, 26 May 2011)
New Revision: 15319

Modified:
   gauger/bindings/js/example.html
   gauger/bindings/js/gauger.js
   gauger/web/.htaccess
   gauger/web/ajax.php
   gauger/web/css/style.css
   gauger/web/graphs
   gauger/web/hosts
   gauger/web/io.php
   gauger/web/params.php
   gauger/web/postme.html
   gauger/web/template.php
Log:
Added cookie based JS registration, various fixes, refactoring


Modified: gauger/bindings/js/example.html
===================================================================
--- gauger/bindings/js/example.html     2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/bindings/js/example.html     2011-05-26 16:53:10 UTC (rev 15319)
@@ -4,7 +4,7 @@
 <title>Gauger Example</title>
 </head>
 <body>
-<input type="button" value="gauger" 
onclick="GAUGER('http://localhost/gauger/web/', 'testuser', '123456', 
'testhost', 'CAT', 'name', 'unit', 100.4, 1)"/>
+<input type="button" value="gauger" onclick="GAUGER('CAT', 'name', 'unit', 
100.2, 1)"/>
 </form>
 </body>
 </html>

Modified: gauger/bindings/js/gauger.js
===================================================================
--- gauger/bindings/js/gauger.js        2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/bindings/js/gauger.js        2011-05-26 16:53:10 UTC (rev 15319)
@@ -1,12 +1,22 @@
-function GAUGER(url, user, pass, host, category, name, unit, value, rev) {
+function GAUGER_READ_COOKIE(name) {
+        var nameEQ = name + "=";
+        var ca = document.cookie.split(';');
+        for(var i=0;i < ca.length;i++) {
+                var c = ca[i];
+                while (c.charAt(0)==' ') c = c.substring(1,c.length);
+                if (c.indexOf(nameEQ) == 0) return 
c.substring(nameEQ.length,c.length);
+        }
+        return null;
+}
+
+
+function GAUGER(category, name, unit, value, rev) {
     name = category + ":::" + name + "___" + unit;
-    url = url + 'hosts/' + host;
+    host = GAUGER_READ_COOKIE('gauger_host');
+    url = unescape(GAUGER_READ_COOKIE('gauger_url'));
+    url += 'hosts/' + host;
     var client = new XMLHttpRequest();
     client.open("POST", url);
     
client.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
-    client.send(encodeURI("user="+user+
-            "&pass="+pass+
-            "&name="+name+
-            "&value="+value+
-            "&revision="+rev));
+    client.send(encodeURI("name="+name+"&value="+value+"&revision="+rev));
 }

Modified: gauger/web/.htaccess
===================================================================
--- gauger/web/.htaccess        2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/.htaccess        2011-05-26 16:53:10 UTC (rev 15319)
@@ -10,3 +10,7 @@
 <FilesMatch "^graphs$"> 
   ForceType application/x-httpd-php
 </FilesMatch>
+
+<FilesMatch "^registration$"> 
+  ForceType application/x-httpd-php
+</FilesMatch>

Modified: gauger/web/ajax.php
===================================================================
--- gauger/web/ajax.php 2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/ajax.php 2011-05-26 16:53:10 UTC (rev 15319)
@@ -19,6 +19,7 @@
 
 */
 
+include "helper.php";
 include "params.php";   // Manage all parameters given by the user
 include "io.php";       // Gather all existing hosts and counters
 init_params();

Modified: gauger/web/css/style.css
===================================================================
--- gauger/web/css/style.css    2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/css/style.css    2011-05-26 16:53:10 UTC (rev 15319)
@@ -376,6 +376,11 @@
     color:              #000;
 }
 
+#registration {
+/*     text-align:         center; */
+/*     width:              200px; */
+}
+
 .footer-shadow {
     height:             50px;
     background-image:   url("images/f.png");

Modified: gauger/web/graphs
===================================================================
--- gauger/web/graphs   2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/graphs   2011-05-26 16:53:10 UTC (rev 15319)
@@ -4,12 +4,13 @@
 
 switch($_SERVER['REQUEST_METHOD']) {
     case 'GET':
-        $_REQUEST['graph'] = $_GET['graph'] = count($urlpath) > 1 ? 
$urlpath[1] : "";
+        $currentg = $_REQUEST['graph'] = $_GET['graph'] = count($urlpath) > 1 
? $urlpath[1] : "";
 
         include "params.php";   // Manage all parameters given by the user
         include "io.php";       // Gather all existing hosts and counters
         init_params();
         explore();
+        $mode = "graph";
         include "template.php"; // Display info
         break;
     case 'POST':

Modified: gauger/web/hosts
===================================================================
--- gauger/web/hosts    2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/hosts    2011-05-26 16:53:10 UTC (rev 15319)
@@ -4,25 +4,36 @@
 
 switch($_SERVER['REQUEST_METHOD']) {
     case 'GET':
-        $_REQUEST['host'] = $_GET['host'] = count($urlpath) > 1 ? $urlpath[1] 
: "";
+        $current = $_REQUEST['host'] = $_GET['host'] = count($urlpath) > 1 ? 
$urlpath[1] : "";
 
         include "params.php";   // Manage all parameters given by the user
         include "io.php";       // Gather all existing hosts and counters
         init_params();
         explore();
+        $mode = "host";
         include "template.php"; // Display info
         break;
     case 'POST':
         $_REQUEST['host'] = $_POST['host'] = count($urlpath) > 1 ? $urlpath[1] 
: "";
         include "params.php";   // Manage all parameters given by the user
         include "io.php";       // Gather all existing hosts and counters
-        $s = $_POST['user'].','.$_POST['pass'];
-        if(!array_key_exists($s, $CONF) || $CONF[$s] != $_POST['host']) {
-            header('HTTP/1.1 403 Forbidden');
-            echo "user/pass not correct";
-            die();
+        init_params();
+        $host = "";
+        /* try to authenticate based on username/password */
+        if (array_key_exists('pass', $_POST)
+            && check_login($_POST['host'], $_POST['pass']))
+        {
+                $host = $_POST['host'];
         }
-        $res = add_data_to_host($_POST['host'], $_POST['name'], 
$_POST['revision'], $_POST['value']);
+        /* if user/pass failed, try to retrieve from cookie/session */
+        if ($host == "" && array_key_exists('gauger_host', $_SESSION)) {
+            $host = $_SESSION['gauger_user'];
+        }
+        /* if no method succeeded, fail */
+        if ($host == "") {
+            die("Authentication rejected.");
+        }
+        $res = add_data_to_host($host, $_POST['name'], $_POST['revision'], 
$_POST['value']);
         if($res === true) {
             header('HTTP/1.1 201 Created');
             die(url("hosts/".$_POST['host']."/".$_POST['name']));

Modified: gauger/web/io.php
===================================================================
--- gauger/web/io.php   2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/io.php   2011-05-26 16:53:10 UTC (rev 15319)
@@ -22,7 +22,7 @@
 /**
  * CONF: main configuration array
  */
-$CONF = @parse_ini_file('gauger.conf');
+$CONF = @parse_ini_file('gauger.conf', true);
 if($CONF === false) {
     header('HTTP/1.1 500 Internal Server Error');
     die("Cannot read the configuration file. Please create one according to 
the README");
@@ -31,12 +31,12 @@
 /**
  * DATADIR: root data directory
  */
-$DATADIR = array_key_exists('data', $CONF) && $CONF['data'] ? $CONF['data'] : 
'data/';
+$DATADIR = @$CONF['config']['data'] ? $CONF['config']['data'] : 'data/';
 
 /**
  * PAGELENGTH: number of graphs per page in host mode
  */
-$PAGELENGTH = array_key_exists('page_length', $CONF) && $CONF['page_lenght'] ? 
$CONF['page_lenght'] : 5;
+$PAGELENGTH = @$CONF['config']['page_lenght'] ? $CONF['config']['page_lenght'] 
: 5;
 if ($DATADIR[strlen($DATADIR)-1] != '/') $DATADIR .= '/';
 
 /**
@@ -57,8 +57,68 @@
  */
 $metrics_c = array();
 
+/**
+ * Write the configuration back to disk (PHP does not have this built-in)
+ * @param $conf Associative array as returned by parse_ini_file(), with 
sections
+ * @return true on success
+ */
+function write_ini_file($conf) { 
+    $content = "";
+    foreach ($conf as $sect => $e) {
+        $content .= "[" . $sect . "]\n";
+        foreach ($e as $key => $value) {
+            if($value=="")
+                $content .= $key . " = \n";
+            else
+                $content .= $key . " = \"" . $value . "\"\n";
+        }
+    }
 
+    if (!$handle = fopen('gauger.conf', 'w')) {
+        return false;
+    }
+    if (!fwrite($handle, $content)) {
+        fclose($handle);
+        return false;
+    }
+    fclose($handle);
+    return true;
+}
+
 /**
+ * Check if the user and password combination is correct
+ * @param $user username/hostname
+ * @param $pass password
+ * @return true if combination is correct, false otherwise
+ */
+function check_login($user, $pass) {
+    global $CONF;
+    $salt = @$CONF['config']['salt'];
+    $hash = sha1("$pass$salt");
+    if ($hash == @$CONF['hosts'][$user]) {
+        return true;
+    } else if (@$CONF['config']['auto_add'] == 1
+               && !array_key_exists($user, $CONF['hosts']))
+    {
+        return add_login($user, $pass);
+    }
+    return false;
+}
+
+/**
+ * Add a new authorized user to the system or change password for an old one
+ * @param $user username/hostname of the new user
+ * @param $pass new password for the user
+ * @return true on success
+ */
+function add_login($user, $pass) {
+    global $CONF;
+    $salt = @$CONF['config']['salt'];
+    $CONF['hosts'][$user] = sha1("$pass$salt");
+    return write_ini_file($CONF);
+}
+
+/**
  * get_range_global: get global range of data
  * @return size 2 array with [min, max]
  */
@@ -435,8 +495,9 @@
  */
 function add_data_to_host($h, $g, $rev, $value) {
     global $CONF;
+    global $DATADIR;
     $datadir = preg_replace('/(.*)\/.*/', '\1', $_SERVER['SCRIPT_FILENAME']);
-    $datadir .=  '/' . $CONF['data'];
+    $datadir .=  '/' . $DATADIR;
     check_permissions($datadir, 'dir');
     $datadir .= '/' . remove_slashes($h);
     check_permissions($datadir, 'dir');

Modified: gauger/web/params.php
===================================================================
--- gauger/web/params.php       2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/params.php       2011-05-26 16:53:10 UTC (rev 15319)
@@ -22,10 +22,7 @@
 $range_parameters = Array('x_max', 'x_min',
                           'y_max', 'y_min',
                           'png_x_size', 'png_y_size');
-$mode_host = FALSE;
-$mode_graph = FALSE;
-$current = "";
-$currentg = "";
+$mode = "";
 
 function get_param($name, $default = '', $persist = FALSE, $usesession = TRUE) 
{
     if (array_key_exists($name, $_REQUEST)) {
@@ -54,29 +51,19 @@
 }
 
 function init_params() {
-    global $mode_host;
-    global $mode_graph;
-    global $current;
-    global $currentg;
     global $q;
     global $range_parameters;
 
+    session_name('gauger_session');
     session_start();
 
     if (get_param('logout')) {
         session_unset();
+        session_destroy();
         header("Location: " . preg_replace("/\?.*/", "", 
$_SERVER["REQUEST_URI"]));
         die();
     }
 
-    if ($current = get_param('host')) {
-        $mode_host = TRUE;
-    }
-
-    if ($currentg = get_param('graph')) {
-        $mode_graph = TRUE;
-    }
-
     if (get_session('xrange_min') === '') {
         $r = get_range('','');
         $_SESSION['xrange_min'] = $r[0];

Modified: gauger/web/postme.html
===================================================================
--- gauger/web/postme.html      2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/postme.html      2011-05-26 16:53:10 UTC (rev 15319)
@@ -6,7 +6,6 @@
 <body>
 <form action="hosts/testhost" method="post">
 <!-- <form action="http://localhost:8080"; method="post"> -->
-<input name="user" value="testuser" /><br/>
 <input name="pass" value="123456" /><br/>
 <input name="name" value="TEST:::test1___kb/s" /><br/>
 <input name="value" value="100" /><br/>

Modified: gauger/web/template.php
===================================================================
--- gauger/web/template.php     2011-05-26 16:33:47 UTC (rev 15318)
+++ gauger/web/template.php     2011-05-26 16:53:10 UTC (rev 15319)
@@ -23,7 +23,7 @@
 <html lang="en">
     <head>
         <meta http-equiv="content-type" content="text/html; charset=utf-8">
-        <title>Gauger [<?php echo isset($current) && $current ? $current : 
get_counter_name($currentg) ?>]</title>
+        <title>Gauger</title>
 
         <link href="<?php echo 
url("css/smoothness/jquery-ui-1.8.9.custom.css") ?>" rel="stylesheet" 
type="text/css"/> 
         <script type="text/javascript" src="<?php echo 
url("js/jquery-1.4.4.min.js") ?>"></script>
@@ -513,13 +513,14 @@
 
         function test() {
             $.get('ajax.php');
+            alert(unescape(document.cookie));
         }
 
         </script>
 
     </head>
     <body>
-        <div Id="debug"></div>
+        <div id="debug"></div>
         <div class="header">
             <?php include "template_menu.php" ?>
             <a href="<?php echo url() ?>" class="name"><img src="<?php echo 
url("images/l.png") ?>" /></a>
@@ -562,18 +563,26 @@
             </div>
         </div>
         <div class="content">
-            <?php if($mode_host == 1): ?>
-                <?php include "template_host.php" ?>
-            <?php elseif($mode_graph == 1): ?>
-                <?php include "template_graph.php" ?>
-            <?php else: ?>
-                 <?php include "template_welcome.php" ?>
-            <?php endif; ?>
+            <?php switch($mode) {
+                case 'host':
+                    include "template_host.php";
+                    break;
+                case 'graph':
+                    include "template_graph.php";
+                    break;
+                case 'reg':
+                    include "template_reg.php";
+                    break;
+                default:
+                    include "template_welcome.php";
+            } ?>
         </div>
         <div style="clear:both"></div>
         </div>
         <div class="footer-shadow"></div>
         <div id="footer">
+            <p><a href="<?php echo url("registration") ?>">Browser 
registration</a></p>
+            <br>
             <p>Copyright 2011 Bartlomiej Polot (address@hidden)</p>
             <p>This software is distributed under the <a target="_blank" 
href="http://www.gnu.org/licenses/agpl.html";>GNU AGPL</a></p>
             <p>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]