[pisa-src] r1145 - in trunk/pairing/pisaum: . .project admin admin/.htpasswd admin/index.php bin css css/global.css include include/management.php include/util.php index.php

  • From: Jan Marten <jan.marten@xxxxxxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Wed, 14 Oct 2009 16:01:11 +0200

Author: marten
Date: Wed Oct 14 16:01:11 2009
New Revision: 1145

Log:
Added website to manage users

Added:
   trunk/pairing/pisaum/
   trunk/pairing/pisaum/.project
   trunk/pairing/pisaum/admin/
   trunk/pairing/pisaum/admin/.htpasswd
   trunk/pairing/pisaum/admin/index.php
   trunk/pairing/pisaum/bin/
   trunk/pairing/pisaum/css/
   trunk/pairing/pisaum/css/global.css
   trunk/pairing/pisaum/include/
   trunk/pairing/pisaum/include/management.php
   trunk/pairing/pisaum/include/util.php
   trunk/pairing/pisaum/index.php

Added: trunk/pairing/pisaum/.project
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/.project       Wed Oct 14 16:01:11 2009        (r1145)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>PISAUM</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>net.sourceforge.phpeclipse.parserbuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>net.sourceforge.phpeclipse.phpnature</nature>
+       </natures>
+</projectDescription>

Added: trunk/pairing/pisaum/admin/.htpasswd
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/admin/.htpasswd        Wed Oct 14 16:01:11 2009        
(r1145)
@@ -0,0 +1 @@
+admin:.zCVAI8wSRiXk

Added: trunk/pairing/pisaum/admin/index.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/admin/index.php        Wed Oct 14 16:01:11 2009        
(r1145)
@@ -0,0 +1,444 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>PISA Usermanagement</title>
+<link rel="stylesheet" type="text/css" href="../css/global.css">
+</head>
+<body>
+<h1>User Management</h1>
+<?php
+include "../include/management.php";
+include "../include/util.php";
+
+define("AUTHORIZED_CFG","/etc/pisa/authorized_hosts.conf");
+define("USERS_CFG", "/etc/pisa/users.conf");
+define("MANAGEMENT_PATH","/var/www/bin/management");
+define("RELAY_CFG","/var/www/bin/relay_config.cfg");
+define("PISA_CONF","/var/www/bin/pisaconf");
+
+$m = new 
Management(AUTHORIZED_CFG,USERS_CFG,MANAGEMENT_PATH,RELAY_CFG,PISA_CONF);
+
+if(isset($_POST["action"]))
+{
+       if($_POST["action"]=="edit")
+       {
+               if(isset($_POST["username"]) && $_POST["username"]!="")
+               {
+                       $entry = $m->getUserByUsername($_POST["username"]);
+                       // Result was found
+                       if(count($entry)!=0)
+                       {
+                               if($entry["Active"]==1)
+                               {
+                                       $entry["Expires"] = 
$m->getExpirationForHit($entry["HIT"]);
+                               }
+                               showUserEditForm($entry);
+                       }else
+                       {
+                               showError("Username was not found.");
+                       }
+               }else
+               {
+                       $entry = $m->getHitEntry($_POST["hit"]);
+                       // Result was found
+                       if(count($entry)!=0)
+                       {
+                               showHitEditForm($entry);
+                       }else
+                       {
+                               showError("HIT was not found.");
+                       }
+               }
+       }else if($_POST["action"]=="remove")
+       {
+               if(!isset($_POST["username"]) && !isset($_POST["username"]))
+               {
+                       showError("No Username or HIT was given.");
+               }else
+               {
+                       if(isset($_POST["username"]) && $_POST["username"]!="")
+                       {
+                               try
+                               {
+                                       
$m->removeEntry(NULL,$_POST["username"]);
+                                       showSuccess();
+                               }catch (Exception $e)
+                               {
+                                       printException($e);
+                               }
+                       }else
+                       {
+                               try
+                               {
+                                       $m->removeEntry($_POST["hit"]);
+                                       showSuccess();
+                               }catch (Exception $e)
+                               {
+                                       printException($e);
+                               }
+                       }
+               }
+
+       }else if($_POST["action"]=="editsubmit")
+       {
+               if(isset($_POST["username"]) && $_POST["username"]!="")
+               {
+                       if(isset($_POST["oldactive"]))
+                       {
+                               if(isset($_POST["active"]) && 
$_POST["active"]==1)
+                               {
+                                       $activechange = 
($_POST["oldactive"]!=1);
+                                       $newactive = 1;
+                               }else
+                               {
+                                       $activechange = 
($_POST["oldactive"]!=0);
+                                       $newactive = 0;
+                               }
+                       }
+                       if(!$activechange)
+                       {
+                               // Just change expiration date
+                               if(isset($_POST["expiration"]))
+                               {
+                                       if($_POST["expiration"]!="")
+                                       {
+                                               try
+                                               {
+                                                       
$m->changeExpirationDate($_POST["username"],NULL,$_POST["expiration"]);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+
+                                                       printException($e);
+                                               }
+                                       }else
+                                       // default expiration
+                                       {
+                                               try
+                                               {
+                                                       
$m->changeExpirationDate($_POST["username"]);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+
+                                                       printException($e);
+                                               }
+                                       }
+                               }
+
+                       }else
+                       {
+                               // Change active status
+                               if(isset($_POST["expiration"]))
+                               {
+                                       if($_POST["expiration"]!="")
+                                       {
+                                               try
+                                               {
+                                                       
$m->changeActive($_POST["username"],NULL,$newactive,$_POST["expiration"]);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+                                                       printException($e);
+                                               }
+                                       }else
+                                       // default expiration
+                                       {
+                                               try
+                                               {
+                                                       
$m->changeActive($_POST["username"],NULL,$newactive);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+
+                                                       printException($e);
+                                               }
+                                       }
+                               }
+                       }
+               }else if(isset($_POST["hit"]) && $_POST["hit"]!="")
+               {
+                       // Just change expiration date
+                       if(isset($_POST["expiration"]))
+                       {
+                               if($_POST["expiration"]!="")
+                               {
+                                       try
+                                       {
+                                               
$m->changeExpirationDate(NULL,$_POST["hit"],$_POST["expiration"]);
+                                               showSuccess();
+                                       }catch (Exception $e)
+                                       {
+                                               printException($e);
+                                       }
+                               }else
+                               // default expiration
+                               {
+                                       try
+                                       {
+                                               
$m->changeExpirationDate(NULL,$_POST["hit"]);
+                                               showSuccess();
+                                       }catch (Exception $e)
+                                       {
+                                               printException($e);
+                                       }
+                               }
+                       }
+               }else
+               {
+                       showError("No Username or HIT was given.");
+               }
+
+       }else if($_POST["action"]=="add")
+       {
+               if(!isset($_POST["hit"]) || $_POST["hit"]=="")
+               {
+                       showError("HIT must be given.");
+               }else
+               {
+                       if(isset($_POST["username"]) && $_POST["username"]!="")
+                       {
+                               if(isset($_POST["active"]) && 
$_POST["active"]==1)
+                               {
+                                       $active = 1;
+                               }else
+                               {
+                                       $active = 0;
+                               }
+                               if(isset($_POST["creditcard"]) && 
$m->isValidCreditCard($_POST["creditcard"]))
+                               {
+                                       if(isset($_POST["expiration"]) && 
$_POST["expiration"]!="")
+                                       {
+                                               try
+                                               {
+                                                       
$m->addEntry($_POST["hit"],$_POST["expiration"],$_POST["username"],$active,$_POST["creditcard"]);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+                                                       printException($e);
+                                               }
+                                       }else
+                                       {
+                                               try
+                                               {
+                                                       
$m->addEntry($_POST["hit"],1,$_POST["username"],$active,$_POST["creditcard"]);
+                                                       showSuccess();
+                                               }catch (Exception $e)
+                                               {
+                                                       printException($e);
+                                               }
+                                       }
+                               }else
+                               {
+                                       showError("No valid credit card 
given.");
+                               }
+                       }else
+                       {
+                               if(isset($_POST["expiration"]) && 
$_POST["expiration"]!="")
+                               {
+                                       try
+                                       {
+                                               
$m->addEntry($_POST["hit"],$_POST["expiration"]);
+                                               showSuccess();
+                                       }catch (Exception $e)
+                                       {
+                                               printException($e);
+                                       }
+                               }else
+                               {
+                                       try
+                                       {
+                                               $m->addEntry($_POST["hit"]);
+                                               showSuccess();
+                                       }catch (Exception $e)
+                                       {
+                                               printException($e);
+                                       }
+                               }
+                       }
+               }
+
+       }
+
+}else
+{
+       // List all entries
+       $entries = $m->getAllUserAndHitEntries();
+       
+       showEntriesTable($entries);
+
+       showAddHitForm();
+}
+?>
+</body>
+</html>
+
+<?
+/* Layout functions:
+ * showSuccess()
+ * showBack()
+ * printException($e)
+ * showHitEditForm($entry)
+ * showUserEditForm($entry)
+ * showEntriesTable($entries)
+ * showAddHitForm()
+ */
+function showSuccess(){
+       ?>
+       Success! <br /> 
+       <?
+       showBack("index.php");
+       redirect("index.php");
+}
+
+function showHitEditForm($entry)
+{
+       ?>
+<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><input
+       type="hidden" name="action" value="editsubmit" />
+<table class="hitedit">
+       <tr>
+               <th>HIT</th>
+               <th>Expiration <small>(empty=default or format: YYYY-MM-DD 
HH:MM:SS)</small></th>
+       </tr>
+       <tr>
+               <td><input type="hidden" name="hit" value="<?=$entry["HIT"];?>" 
/><?=$entry["HIT"];?>
+               </td>
+               <td><input type="text" name="expiration" maxlength="19"
+                       class="expiration" value="<?=$entry["Expires"];?>" 
/></td>
+       </tr>
+</table>
+<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
+</form>
+       <?
+       showBack("index.php");
+}
+
+function showUserEditForm($entry)
+{
+       ?>
+<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><input
+       type="hidden" name="action" value="editsubmit" />
+<table class="hitedit">
+       <tr>
+               <th>Username</th>
+               <th>HIT</th>
+               <th>Active</th>
+               <th>Expiration <small>(empty=default or format: YYYY-MM-DD 
HH:MM:SS)</small></th>
+               <th>Credit Card</th>
+       </tr>
+       <tr>
+               <td><input type="hidden" name="username"
+                       value="<?=$entry["Username"];?>" 
/><?=$entry["Username"];?></td>
+               <td><input type="hidden" name="hit" value="<?=$entry["HIT"];?>" 
/><?=$entry["HIT"];?>
+               </td>
+               <td><input type="hidden" name="oldactive"
+                       value="<?=$entry["Active"];?>" /> <input type="checkbox"
+                       name="active"
+                       <? if($entry["Active"]==1){echo 
"checked=\"checked\"";}?> value="1" />
+               </td>
+               <td><input type="text" name="expiration" class="expiration"
+                       maxlength="19" value="<?=$entry["Expires"];?>" /></td>
+               <td><input type="hidden" name="creditcard"
+                       value="<?=$entry["Creditcard"];?>" 
/><?=$entry["Creditcard"];?></td>
+       </tr>
+</table>
+<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
+</form>
+                       <?
+                       showBack("index.php");
+}
+
+function showEntriesTable($entries)
+{
+       ?>
+<table class="hitlist">
+       <tr>
+               <th><b>Username</b></th>
+               <th><b>HIT</b></th>
+               <th><b>Active</b></th>
+               <th><b>Expiration</b></th>
+               <th><b>Credit Card</b></th>
+               <th colspan="2" />
+       
+       </tr>
+       <?
+       foreach ($entries as $entry)
+       {
+               ?>
+       <tr>
+               <td><?=$entry["Username"];?></td>
+               <td><?=$entry["HIT"];?></td>
+               <td><?=$entry["Active"];?></td>
+               <td><?=$entry["Expires"];?></td>
+               <td><?=$entry["Creditcard"];?></td>
+               <td>
+               <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><input
+                       type="hidden" name="action" value="edit" /> <input 
type="hidden"
+                       name="username" value="<?=$entry["Username"];?>" /> 
<input
+                       type="hidden" name="hit" value="<?=$entry["HIT"];?>" /> 
<input
+                       type="submit" value="Edit" /></form>
+               </td>
+               <td>
+               <form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><input
+                       type="hidden" name="action" value="remove" /> <input 
type="hidden"
+                       name="hit" value="<?=$entry["HIT"];?>" /> <input 
type="hidden"
+                       name="username" value="<?=$entry["Username"];?>" /> 
<input
+                       type="submit" value="Remove" onClick="return 
confirmSubmit();" /></form>
+               </td>
+       </tr>
+       <?
+       }
+       ?>
+</table>
+       <?
+}
+
+function showAddHitForm()
+{
+       ?>
+<br />
+Add HIT:
+<br />
+<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><input
+       type="hidden" name="action" value="add" />
+<table class="hitadd">
+       <tr>
+               <th>Username <br />
+               <small>empty for just adding HIT</small></th>
+               <td><input type="text" name="username" maxlength="100"
+                       class="username" /></td>
+       </tr>
+       <tr>
+               <th>HIT<br />
+               <small>always necessary</small></th>
+               <td><input type="text" name="hit" maxlength="36" class="hit" 
/></td>
+       </tr>
+       <tr>
+               <th>Active<br />
+               <small>only necassary if username is given</small></th>
+               <td><input type="checkbox" name="active" checked="checked" 
value="1">active</input>
+               </td>
+       </tr>
+       <tr>
+               <th>Expiration <br />
+               <small>empty=default or format: YYYY-MM-DD HH:MM:SS</small></th>
+               <td><input type="text" name="expiration" maxlength="19"
+                       class="expiration" /></td>
+       </tr>
+       <tr>
+               <th>Credit Card <br />
+               <small>only necassary if username is given</small></th>
+               <td><input type="text" name="creditcard" maxlength="19"
+                       class="creditcard" /></td>
+       </tr>
+</table>
+<br />
+<input type="submit" value="Submit" /> <input type="reset" value="Reset" />
+</form>
+       <?
+}
+
+
+?>

Added: trunk/pairing/pisaum/css/global.css
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/css/global.css Wed Oct 14 16:01:11 2009        (r1145)
@@ -0,0 +1,119 @@
+table.hitlist {
+       border-width: 1px 1px 1px 1px;
+       border-style: outset outset outset outset;
+       border-color: gray gray gray gray;
+       border-collapse: collapse;
+}
+
+table.hitlist th {
+       border-width: 1px 1px 4px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+table.hitlist td {
+       border-width: 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+table.hitadd {
+       border-width: 1px 1px 1px 1px;
+       border-style: outset outset outset outset;
+       border-color: gray gray gray gray;
+       border-collapse: collapse;
+}
+
+table.hitadd th {
+       font-weight: normal;
+       text-align: left; border-width : 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+       border-width: 1px 1px 1px 1px;
+}
+
+table.hitadd td {
+       border-width: 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+table.hitedit {
+       border-width: 1px 1px 1px 1px;
+       border-style: outset outset outset outset;
+       border-color: gray gray gray gray;
+       border-collapse: collapse;
+}
+
+table.hitedit th {
+       border-width: 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+table.hitedit th small {
+       font-weight: normal;
+}
+
+table.hitedit td {
+       border-width: 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+table.userreg {
+       border-width: 1px 1px 1px 1px;
+       border-style: outset outset outset outset;
+       border-color: gray gray gray gray;
+       border-collapse: collapse;
+}
+
+table.userreg th {
+       font-weight: normal;
+       text-align: left; border-width : 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+       border-width: 1px 1px 1px 1px;
+}
+
+table.userreg td {
+       border-width: 1px 1px 1px 1px;
+       padding: 4px 4px 4px 4px;
+       border-style: inset inset inset inset;
+       border-color: gray gray gray gray;
+}
+
+.hit {
+       width: 270px;
+}
+
+.username {
+       width: 200px;
+}
+
+.expiration {
+       width: 150px;
+}
+.creditcard {
+       width: 170px;
+}
+
+div.serverblock {
+       border: 1px;
+       border-style: solid;
+       float: left;
+       padding: 2px 2px 2px 2px;
+       margin: 2px 2px 2px 2px;
+}
+
+div.serverblock pre{
+       padding-left: 5px;
+       padding-right: 5px;
+}

Added: trunk/pairing/pisaum/include/management.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/include/management.php Wed Oct 14 16:01:11 2009        
(r1145)
@@ -0,0 +1,436 @@
+<?php
+define("ERROR_OPENING_CONFIG", 2);
+define("ERROR_NOT_FOUND", 3);
+define("ERROR_PARSING_COMMANDLINE", 4);
+define("ERROR_WRITING_CONFIG", 5);
+define("ERROR_GENERAL", 6);
+class Management
+{
+       // Debug output
+       private static $debug = false;
+       
+       private $authorized_cfg_path = NULL;
+       private $users_cfg_path = NULL;
+       private $management_path = NULL;
+       private $relay_cfg_path = NULL;
+       private $pisaconf_path = NULL;
+
+       public function 
Management($authorized=NULL,$users=NULL,$management=NULL,$relay=NULL,$pisaconf=NULL)
+       {
+               if(!is_null($authorized))
+               {
+                       $this->authorized_cfg_path = $authorized;
+               }
+               if(!is_null($users))
+               {
+                       $this->users_cfg_path = $users;
+               }
+               if(!is_null($management))
+               {
+                       $this->management_path = $management;
+               }
+               if(!is_null($relay))
+               {
+                       $this->relay_cfg_path = $relay;
+               }
+               if(!is_null($pisaconf))
+               {
+                       $this->pisaconf_path = $pisaconf;
+               }
+       }
+
+       public function getAllUserAndHitEntries()
+       {
+               $exec = $this->getExecPrefix();
+               $exec .= " -l 1 ";
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec);
+
+               $splitresult = split("\n",$output);
+               $arrayresult = array();
+               $j = 0;
+               for ($i=0;$i< sizeof($splitresult);$i+=5 )
+               {
+                       if(!(($splitresult[$i]=="" || is_null($splitresult[$i]))
+                                               && ($splitresult[$i+1]=="" || 
is_null($splitresult[$i+1]))))
+                       {
+                               $arrayresult[$j] = array("Username" => 
$splitresult[$i],
+                                               "HIT" => $splitresult[$i+1],
+                                               "Active" =>$splitresult[$i+2],
+                                               "Expires"=> $splitresult[$i+3],
+                                               "Creditcard" 
=>$splitresult[$i+4]);
+                               $j++;
+                       }
+
+               }
+
+               return $arrayresult;
+       }
+
+       public function getUserByUsername($username)
+       {
+               $exec = $this->getExecPrefix();
+               $exec .= " -l 1 -u " . escapeshellarg($username);
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec);
+
+               $splitresult = split("\n",$output);
+               $i = 0;
+               if(!(($splitresult[$i]=="" || is_null($splitresult[$i]))
+                                       && ($splitresult[$i+1]=="" || 
is_null($splitresult[$i+1]))))
+               {
+                       $arrayresult = array("Username" => $splitresult[$i],
+                                       "HIT" => $splitresult[$i+1],
+                                       "Active" =>$splitresult[$i+2],
+                                       "Creditcard" =>$splitresult[$i+3]);
+               }
+               return $arrayresult;
+       }
+
+       public function getHitEntry($hit)
+       {
+               $exec = $this->getExecPrefix();
+               $exec .= " -l 1 -h " . escapeshellarg($hit);
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec);
+
+               $splitresult = split("\n",$output);
+               //$arrayresult = array();
+               //              $j = 0;
+               $i=0;
+               for ($i=0;$i< sizeof($splitresult);$i+=2 )
+               {
+
+                       if(!(($splitresult[$i]=="" || 
is_null($splitresult[$i]))))
+                       {
+                               $arrayresult = array("HIT" => $splitresult[$i],
+                                               "Expires" =>$splitresult[$i+1]);
+                               //$j++;
+                       }
+
+               }
+               return $arrayresult;
+       }
+
+       public function 
addEntry($hit,$expires=1,$username=NULL,$active=2,$creditcard=NULL)
+       {
+               if(is_null($hit)||$hit=="")
+               {
+                       throw new Exception("No HIT was given.");
+                       return;
+               }
+
+               $exec = $this->getExecPrefix();
+
+               $exec .= " -h " . escapeshellarg($hit) . " -e " . 
escapeshellarg($expires);
+
+               if(!is_null($username))
+               {
+                       if($this->existsUsername($username))
+                       {
+                               throw new Exception("Username already exists.");
+                               return;
+                       }
+                       if(is_null($creditcard))
+                       {
+                               throw new Exception("No Creditcard given.");
+                               return;
+                       }
+
+                       $exec .= " -u " . escapeshellarg($username);
+                       $exec .= " -p " . escapeshellarg($creditcard);
+
+                       if($active>=0 && $active<=1)
+                       {
+                               $exec .= " -a " . escapeshellarg($active);
+                       }
+               }
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec.' > /dev/null; echo $?');
+
+               $output = trim($output);
+
+               // An error occured
+               if($output!=0)
+               {
+                       switch ($output)
+                       {
+                               case ERROR_OPENING_CONFIG:
+                                       throw new Exception ("An error occured 
during opening config file.");
+                                       break;
+                               case ERROR_NOT_FOUND:
+                                       throw new Exception ("HIT or username 
not found.");
+                                       break;
+                               case ERROR_PARSING_COMMANDLINE:
+                                       throw new Exception ("Error parsing 
arguments.");
+                                       break;
+                               case ERROR_WRITING_CONFIG:
+                                       throw new Exception ("Error writing 
config file.");
+                                       break;
+                               case ERROR_GENERAL:
+                               default:
+                                       throw new Exception ("An error occured 
during adding.");
+                                       break;
+                       }
+               }else {
+                       $this->reloadPisaSD();
+               }
+       }
+
+       public function 
changeExpirationDate($username=NULL,$hit=NULL,$expires=1)
+       {
+               if(is_null($username) && is_null($hit))
+               {
+                       throw new Exception("No HIT or Username was given.");
+               }
+
+               $exec = $this->getExecPrefix();
+
+               $exec .= " -e " . escapeshellarg($expires);
+
+               if(!is_null($hit))
+               {
+                       $exec .= " -h " . escapeshellarg($hit);
+               }else
+               {
+                       $exec .= " -u " . escapeshellarg($username);
+               }
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec.' > /dev/null; echo $?');
+
+               $output = trim($output);
+
+               // An error occured
+               if($output!=0)
+               {
+                       switch ($output)
+                       {
+                               case ERROR_OPENING_CONFIG:
+                                       throw new Exception ("An error occured 
during opening config file.");
+                                       break;
+                               case ERROR_NOT_FOUND:
+                                       throw new Exception ("HIT or username 
not found.");
+                                       break;
+                               case ERROR_PARSING_COMMANDLINE:
+                                       throw new Exception ("Error parsing 
arguments.");
+                                       break;
+                               case ERROR_WRITING_CONFIG:
+                                       throw new Exception ("Error writing 
config file.");
+                                       break;
+                               case ERROR_GENERAL:
+                               default:
+                                       throw new Exception ("An error occured 
during changing expiration date.");
+                                       break;
+                       }
+               }else {
+                       $this->reloadPisaSD();
+               }
+       }
+
+       public function 
changeActive($username=NULL,$hit=NULL,$active=2,$expiration=1)
+       {
+               if(is_null($username) && is_null($hit))
+               {
+                       throw new Exception("No HIT or Username was given.");
+               }
+
+               if(!($active>=0 && $active<=1))
+               {
+                       return;
+               }
+               $exec = $this->getExecPrefix();
+
+               if(!is_null($username))
+               {
+                       $exec .= " -u " . escapeshellarg($username) . " -a " . 
escapeshellarg($active);
+               }else
+               {
+                       $exec .= " -u " . escapeshellarg($hit) . " -a " . 
escapeshellarg($active);
+               }
+
+               if($active==1)
+               {
+                       $exec .= " -e " . escapeshellarg($active);
+               }
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec.' > /dev/null; echo $?');
+
+               $output = trim($output);
+
+               // An error occured
+               if($output!=0)
+               {
+                       switch ($output)
+                       {
+                               case ERROR_OPENING_CONFIG:
+                                       throw new Exception ("An error occured 
during opening config file.");
+                                       break;
+                               case ERROR_NOT_FOUND:
+                                       throw new Exception ("HIT or username 
not found.");
+                                       break;
+                               case ERROR_PARSING_COMMANDLINE:
+                                       throw new Exception ("Error parsing 
arguments.");
+                                       break;
+                               case ERROR_WRITING_CONFIG:
+                                       throw new Exception ("Error writing 
config file.");
+                                       break;
+                               case ERROR_GENERAL:
+                               default:
+                                       throw new Exception ("An error occured 
during changing active status.");
+                                       break;
+                       }
+               }else{
+                       $this->reloadPisaSD();
+               }
+       }
+
+       public function removeEntry($hit=NULL,$username=NULL)
+       {
+               if(is_null($username) && is_null($hit))
+               {
+                       throw new Exception("No HIT or Username was given.");
+               }
+
+               $exec = $this->getExecPrefix();
+
+               $exec .= " -r ";
+
+               if(!is_null($username))
+               {
+                       $exec .= " -u " . escapeshellarg($username);
+               }
+
+               if(!is_null($hit))
+               {
+                       $exec .= " -h " . escapeshellarg($hit);
+               }
+
+               $this->print_debug($exec);
+
+               $output = shell_exec($exec.' > /dev/null; echo $?');
+
+               $output = trim($output);
+
+               // An error occured
+               if($output!=0)
+               {
+                       switch ($output)
+                       {
+                               case ERROR_OPENING_CONFIG:
+                                       throw new Exception ("An error occured 
during opening config file.");
+                                       break;
+                               case ERROR_NOT_FOUND:
+                                       throw new Exception ("HIT or username 
not found.");
+                                       break;
+                               case ERROR_PARSING_COMMANDLINE:
+                                       throw new Exception ("Error parsing 
arguments.");
+                                       break;
+                               case ERROR_WRITING_CONFIG:
+                                       throw new Exception ("Error writing 
config file.");
+                                       break;
+                               case ERROR_GENERAL:
+                               default:
+                                       throw new Exception ("An error occured 
during removing.");
+                                       break;
+                       }
+               }else {
+                       $this->reloadPisaSD();
+               }
+       }
+
+       private function print_debug($output)
+       {
+               if(self::$debug)
+               {
+                       print("<b>DEBUG:</b><pre>" . $output);
+                       print("</pre><br />");
+               }
+       }
+
+       private function getExecPrefix()
+       {
+               $exec = "./management ";
+               if(!is_null($this->management_path))
+               {
+                       $exec = $this->management_path . " ";
+               }
+
+               if(!is_null($this->authorized_cfg_path))
+               {
+                       $exec .= " -f " . 
escapeshellarg($this->authorized_cfg_path);
+               }
+
+               if(!is_null($this->users_cfg_path))
+               {
+                       $exec .= " -c " . escapeshellarg($this->users_cfg_path);
+               }
+
+               if(!is_null($this->relay_cfg_path))
+               {
+                       $exec .= " -s " . escapeshellarg($this->relay_cfg_path);
+               }
+
+               return $exec;
+       }
+
+       public function getExpirationForHit($hit)
+       {
+               $entry = $this->getHitEntry($hit);
+               $result = "";
+               if(count($entry)!=0)
+               {
+                       $result = $entry["Expires"];
+               }
+               return $result;
+       }
+       
+       private function reloadPisaSD()
+       {
+               if(!is_null($this->pisaconf_path))
+               {
+                       $exec = escapeshellarg($this->pisaconf_path) . " 
reload";
+                       shell_exec($exec);
+               }
+       }
+
+       private function existsHit($hit)
+       {
+               return count($this->getHitEntry($hit))!=0;
+       }
+       
+       private function existsUsername($username)
+       {
+               return count($this->getUserByUsername($username))!=0;
+       }
+
+       public static function isValidCreditCard($number)
+       {
+               return $number!="";
+       }
+
+       public static function getServerHit($hipconfpath=NULL)
+       {
+               if(is_null($hipconfpath))
+               {
+                       $exec = "/usr/local/sbin/hipconf";
+               }else{
+                       $exec = $hipconfpath;
+               }
+               $exec .=" get hi default 2>&1 | grep \"Default HIT\"|cut -d ' ' 
 -f3";
+               $output = shell_exec($exec);
+               return $output;
+       }
+}
+?>

Added: trunk/pairing/pisaum/include/util.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/include/util.php       Wed Oct 14 16:01:11 2009        
(r1145)
@@ -0,0 +1,33 @@
+<?php
+
+function printException($e)
+{
+       echo "Caught exception: ",  $e->getMessage(), "<br />";
+}
+
+function showError($errorMessage)
+{
+       echo "Error: " . $errorMessage;
+       showBack("index.php");
+}
+
+function showBack($url)
+{
+       ?>
+<br />
+<a href="<?=$url;?>">Back to Index</a>
+       <?
+}
+
+function redirect($url, $seconds=3) {
+       echo "<br />";
+       echo "Redirecting in " . $seconds . " seconds.";
+       echo "<script language=\"JavaScript\">\n";
+       echo "function redirect() {\n";
+       echo "window.location = \"" . $url . "\";\n";
+       echo "}\n\n";
+       echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";
+       echo "</script>\n";
+
+}
+?>

Added: trunk/pairing/pisaum/index.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/pairing/pisaum/index.php      Wed Oct 14 16:01:11 2009        (r1145)
@@ -0,0 +1,90 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>User Registration</title>
+<link rel="stylesheet" type="text/css" href="css/global.css">
+</head>
+<body>
+<?
+include "include/management.php";
+include "include/util.php";
+
+define("AUTHORIZED_CFG","/etc/pisa/authorized_hosts.conf");
+define("USERS_CFG", "/etc/pisa/users.conf");
+define("MANAGEMENT_PATH","/var/www/bin/management");
+define("RELAY_CFG","/var/www/bin/relay_config.cfg");
+define("PISA_CONF","/var/www/bin/pisaconf");
+
+if(isset($_POST["action"]) && $_POST["action"]=="register")
+{
+       
+       if(isset($_POST["hit"]) && isset($_POST["username"]) && 
isset($_POST["creditcard"])
+          && $_POST["hit"]!="" && $_POST["username"]!="" && 
$_POST["creditcard"])
+       {
+               $m = new 
Management(AUTHORIZED_CFG,USERS_CFG,MANAGEMENT_PATH,RELAY_CFG,PISA_CONF);
+               
+               // Check credit card
+
+               if(!$m->isValidCreditCard($_POST["creditcard"]))
+               {
+                       echo "You did not enter a valid credit-card number.";
+                       showBack("index.php");
+               }else{
+
+                       try
+                       {
+                               
$m->addEntry($_POST["hit"],1,$_POST["username"],1,$_POST["creditcard"]);
+                               echo "<b>You successfully registered a new 
account.</b><br />";
+                               $hit = $m->getServerHit();
+                               $hit = str_replace("\n", "", $hit);
+                               if($hit=="") 
+                               {
+                                       throw new Exception ("Server HIT not 
found.");
+                               }
+                               echo "<br />Add the following lines to the 
server block in pisacd.conf: ";
+                               echo "<div class=\"serverblock\"><pre>{\n 
hit=\"" . $hit . "\";\n type = \"relay\";\n}" . "</pre></div><div>&nbsp;</div>";
+
+                       }catch (Exception $e)
+                       {
+                               printException($e);
+                               showBack("index.php");
+                       }
+               }
+       }else{
+               echo "All fields are required.";
+               showBack("index.php");
+       }
+
+}else
+{
+?>
+<h2>User Registration</h2>
+Enter your details:
+<br />
+<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
+       <input type="hidden" name="action" value="register" />
+       <table class="userreg">
+               <tr>
+                       <td>Username:</td>
+                       <td><input type="text" name="username" class="username" 
maxlength="100"/></td>
+               </tr>
+               <tr>
+                       <td>Credit-Card Number:</td>
+                       <td><input type="text" name="creditcard" 
class="creditcard" maxlength="19" /></td>
+               </tr>
+               <tr>
+                       <td>HIT:<br />
+                       <small>Execute: <pre>hipconf get hi default 2>&1 | grep 
"Default HIT"|cut -d\&nbsp;&nbsp;-f 3</pre>
+                       And paste the result</small></td>
+                       <td><input type="text" name="hit" maxlength="36" 
class="hit"/></td>
+               </tr>
+       </table>
+       <br />
+       <input type="submit" value="Submit" /> <input type="reset" 
value="Reset" />
+</form>
+<?
+}
+?>
+</body>
+</html>

Other related posts:

  • » [pisa-src] r1145 - in trunk/pairing/pisaum: . .project admin admin/.htpasswd admin/index.php bin css css/global.css include include/management.php include/util.php index.php - Jan Marten