From 7db5081fcdb4d89e58b31964240c4e65e70e5dde Mon Sep 17 00:00:00 2001
From: alvaro <alvaro@alia.(none)>
Date: Tue, 8 May 2012 00:38:55 -0700
Subject: [PATCH] Added session

---
 classes/HTTPStatus.php            |  6 +++
 classes/modules/sessionModule.php | 83 +++++++++++++++++++++++++++++++
 common.inc.php                    | 11 ++++
 3 files changed, 100 insertions(+)
 create mode 100644 classes/modules/sessionModule.php

diff --git a/classes/HTTPStatus.php b/classes/HTTPStatus.php
index e0624306..705a9a4b 100644
--- a/classes/HTTPStatus.php
+++ b/classes/HTTPStatus.php
@@ -9,6 +9,12 @@ class HTTPStatus{
   	echo $uri."\n\n";
   	exit(0);
   }
+
+  public static function send401($msg){
+  	header("HTTP/1.0 401 Forbidden");
+  	echo $msg;
+  	exit(0);
+  }
   
   public static function send404($uri){
   	header("HTTP/1.0 404 Not Found");
diff --git a/classes/modules/sessionModule.php b/classes/modules/sessionModule.php
new file mode 100644
index 00000000..bf2367e6
--- /dev/null
+++ b/classes/modules/sessionModule.php
@@ -0,0 +1,83 @@
+<?
+require_once('abstractModule.php');
+class SessionModule extends abstractModule{
+  //Session module
+  private $sessionUri = "session";
+  
+  public function match($uri){
+  	global $conf; 
+    global $localUri;
+    global $lodspk;
+    $method = ucwords($_SERVER['REQUEST_METHOD']);
+    $uriSegment = str_replace($conf['basedir'], '', $localUri);
+    //Check if looking for session validation
+    if($uriSegment === $this->sessionUri){
+      //GET will return the form
+      if($method == "GET"){
+        $this->showSessionForm();
+        return true;
+      }      
+      //POST will take the data and validate it
+      if($method == "POST"){
+        if($this->validateAuthentication($_POST)){
+          session_start();
+          $_SESSION['lodspk'] = 1;
+          Utils::send303($conf['basedir'], '');
+        }else{
+          Utils::send401("Authentication not valid.");
+          return true;
+        }
+      }
+    }else{
+      session_start();
+      if(isset($_SESSION['lodspk'])){
+        return false;
+      }else{
+        Utils::send303($conf['basedir'].$this->sessionUri, '');
+        return true;
+      }
+    }
+    
+  }
+  
+  public function execute($uri){
+  	global $conf;
+  	global $localUri;
+  	global $uri;
+  	global $acceptContentType;
+  	global $endpoints;
+  	global $lodspk;
+  	global $firstResults;
+  	return true;
+  }
+  
+  
+  private function showSessionForm(){
+    echo "<html>
+    <head>
+    <title>Login</title>
+    </head>
+    <body>
+    <form action='".$this->sessionUri."' method='POST'>
+    <input name='user' type='text' />
+    <input name='password' type='password' /><br/>
+    <input name='submit' type='submit' />
+    </form>
+    </body>
+    </html>";
+    exit(0);    
+  }
+  
+  private function validateAuthentication($data){
+    global $conf;
+    if(isset($conf['session']['user']) && isset($conf['session']['password'])){
+      if($data['user'] == $conf['session']['user'] && $data['password'] == $conf['session']['password']){
+        return true;
+      }
+      
+      return false;
+    }
+    return false;
+  }
+}
+?>
diff --git a/common.inc.php b/common.inc.php
index 7618f5ff..fb075a4b 100644
--- a/common.inc.php
+++ b/common.inc.php
@@ -48,11 +48,22 @@ $conf['type']['priorities']['rdfs:Resource'] = -1;
 //Debug
 $conf['debug'] = false;
 
+//Session module
+//First version: really simple user/pass
+$conf['session']['user'] = 'admin';
+$conf['session']['password'] = 'admin';
+
+
 //Modules: LODSPeaKr will try to match the requested URI
 //using the modules in the following order
 $conf['modules'] = array();
 $conf['modules']['directory'] = 'classes/modules/';
+
 $conf['modules']['available'] = array('static','uri', 'type', 'service');
+//Uncomment next line to enable sessions
+//$conf['modules']['available'] = array('session', 'static','uri', 'type', 'service');
+
+
 global $lodspk;
 include_once('settings.inc.php');
 $conf['view']['standard']['baseUrl'] = $conf['basedir'];
-- 
GitLab