Author: [email protected]
Date: Sun Feb  8 12:55:45 2009
New Revision: 13808

Modified:
    projects/webui/trunk/   (props changed)
    projects/webui/trunk/config.php
    projects/webui/trunk/core_init.php
    projects/webui/trunk/phprd/ajax/ajax.php
    projects/webui/trunk/phprd/error-log/log.php
    projects/webui/trunk/phprd/sql/sql.php
    projects/webui/trunk/phprd/template/template.php
    projects/webui/trunk/phprd/users/users.php

Log:
updated template engine, disabled (temporarily) user sessions (will add  
that later on), and changed the settings for the error reporting to be  
configurable

Modified: projects/webui/trunk/config.php
==============================================================================
--- projects/webui/trunk/config.php     (original)
+++ projects/webui/trunk/config.php     Sun Feb  8 12:55:45 2009
@@ -35,5 +35,7 @@
  $poller_history=50;
  // browser refresh interval
  $browser_refresh=30;
+// reports
+$php_report=E_ERROR | E_WARNING | E_PARSE;

  ?>

Modified: projects/webui/trunk/core_init.php
==============================================================================
--- projects/webui/trunk/core_init.php  (original)
+++ projects/webui/trunk/core_init.php  Sun Feb  8 12:55:45 2009
@@ -14,6 +14,9 @@
  require "config.php";
  require_once "menus.php";

+// what to report?
+error_reporting($php_report);
+
  // phprd
  require "phprd/template/template.php";
  require_once "phprd/users/users.php";
@@ -31,7 +34,7 @@
  $templ=new template;
  $templ->set_template_path($template_path);
  $ajax=new ajax;
-$users=new user_sessions;
+//$users=new user_sessions;
  $webfunc=new webfunc;
  $squid_interface=new squid_interface;
  $user_config=$squid_interface->get_local_config();

Modified: projects/webui/trunk/phprd/ajax/ajax.php
==============================================================================
--- projects/webui/trunk/phprd/ajax/ajax.php    (original)
+++ projects/webui/trunk/phprd/ajax/ajax.php    Sun Feb  8 12:55:45 2009
@@ -42,6 +42,16 @@
        }

  /**
+ * Register custom javascript code
+ *
+ * @custom - custom js code
+ *
+ */    
+       public function register_custom($custom) {
+               return "javascript:$custom";
+       }
+
+/**
   * Register callback on the click
   *
   * @id callback id
@@ -184,6 +194,20 @@
                var http = create_object();
                var request_id='';

+               function trim(str, chars) {
+                   return ltrim(rtrim(str, chars), chars);
+               }
+
+               function ltrim(str, chars) {
+                   chars = chars || "\\s";
+                   return str.replace(new RegExp("^[" + chars + "]+", "g"), 
"");
+               }
+
+               function rtrim(str, chars) {
+                   chars = chars || "\\s";
+               return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
+               }
+
                function create_object()
                {
                        var http;
@@ -246,6 +270,7 @@
                        if (!response) return;
                        if (!document.getElementById(request_id)) return;
                        //alert(request_id);alert(response);
+                       response=ltrim(response," ");
                        if (document.getElementById(request_id).innerHTML)  
document.getElementById(request_id).innerHTML=response;
                        else document.getElementById(request_id).value = 
response;
                }

Modified: projects/webui/trunk/phprd/error-log/log.php
==============================================================================
--- projects/webui/trunk/phprd/error-log/log.php        (original)
+++ projects/webui/trunk/phprd/error-log/log.php        Sun Feb  8 12:55:45 2009
@@ -5,16 +5,16 @@
   * @copyright 2008
   */

-DEFINE (NO_LOGGING,0);
-DEFINE (LOG_FILE,1);
-DEFINE (LOG_OUTPUT,2);
-DEFINE (LOG_SQL,4);
+DEFINE ('NO_LOGGING',0);
+DEFINE ('LOG_FILE',1);
+DEFINE ('LOG_OUTPUT',2);
+DEFINE ('LOG_SQL',4);

-DEFINE (SEVERITY_NOTICE,1);
-DEFINE (SEVERITY_INFO,2);
-DEFINE (SEVERITY_WARNING,3);
-DEFINE (SEVERITY_ERROR,4);
-DEFINE (SEVERITY_CRITICAL,5);
+DEFINE ('SEVERITY_NOTICE',1);
+DEFINE ('SEVERITY_INFO',2);
+DEFINE ('SEVERITY_WARNING',3);
+DEFINE ('SEVERITY_ERROR',4);
+DEFINE ('SEVERITY_CRITICAL',5);

  class Logging {
        

Modified: projects/webui/trunk/phprd/sql/sql.php
==============================================================================
--- projects/webui/trunk/phprd/sql/sql.php      (original)
+++ projects/webui/trunk/phprd/sql/sql.php      Sun Feb  8 12:55:45 2009
@@ -14,6 +14,7 @@
        protected $sqlcaching=false;
        protected $sqlcaching_ttl=60;
        public $lasterror;
+       public $lastsql;
        protected $sql_queries_counter=0;
        protected $method="mysql";
        public $rows_affected=0;
@@ -24,7 +25,7 @@
   * @path - path for cache files, default "cache/"
   */
        public function __construct ($path="cache/") {
-               $this->sqlcache=new SQLCache;   
+               $this->sqlcache=new SQLCache;
                $this->sqlcache->set_path($path);
                }

@@ -82,15 +83,16 @@
   * @returns true if succeed, if not, returns false and lasterror var is  
the error
   *
   */
-public function connect  
($hostname='localhost',$username='root',$password='',$db="test",$caching=false,$method="mysqli",$extras="")
  
{
-               if ($method=="mysqli") $this->method="mysqli"; else  
$this->method="mysql";
+public function connect  
($hostname='localhost',$username='root',$password='',$db="test",$caching=false,$method="mysql",$extras="")
  
{
+               if ($method=="mysqli") $this->method="mysqli"; else  
$this->method="mysql";
                if ($this->method=="mysqli")
                        
$this->dbhand...@mysqli_connect($hostname,$username,$password); else
-                       
$this->dbhand...@mysql_connect($hostname,$username,$password);
+                       
$this->dbhand...@mysql_connect($hostname,$username,$password,true);
                if (!$this->dbhandle) { $this->lasterror="Cannot connect to 
DB!"; return  
false; }
-               if ($this->method=="mysqli") {
+               if ($this->method=="mysqli")
                        if (!...@mysqli_select_db($this->dbhandle,$db)) { 
$this->lasterror="Cannot  
select DB!"; return false; }
-                       } elseif (!...@mysql_select_db($db,$this->dbhandle)) {  
$this->lasterror="Cannot select DB!"; return false; }
+               if ($this->method=="mysql")
+                               if (!...@mysql_select_db($db,$this->dbhandle)) 
{ $this->lasterror="Cannot  
select DB!"; return false; }
                $this->sqlcaching=$caching;
                return true;
        }
@@ -144,6 +146,7 @@
                foreach ($definitions as $var=>$def) $sql.="$var $def,";
                $sql=substr($sql,0,strlen($sql)-1);
                $sql.=") engine=$engine, row_format=$row_format";
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($this->dbhandle,$sql);
                        else $r...@mysql_query($sql,$this->dbhandle);
                $this->sql_queries_counter++;
@@ -172,8 +175,29 @@

        public function delete_table ($tablename) {
                if (!empty($tablename)) {
-                       if ($this->method=="mysqli") $r...@mysqli_query("DROP 
TABLE  
$tablename",$this->dbhandle);
-                               else $r...@mysql_query("DROP TABLE 
$tablename",$this->dbhandle);
+                       $sql="DROP TABLE $tablename";
+                       $this->lastsql=$sql;
+                       if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
+                               else $r...@mysql_query($sql,$this->dbhandle);
+                       $this->sql_queries_counter++;
+                       return true;
+               } else return false;
+       }
+
+/**
+ * Clear table
+ *
+ * @tablename - table name
+ * @returns true if succeed, if not, returns false and lasterror var is  
the error
+ *
+ */
+
+       public function clear_table ($tablename) {
+               if (!empty($tablename)) {
+                       $sql="TRUNCATE TABLE $tablename";
+                       $this->lastsql=$sql;
+                       if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
+                               else $r...@mysql_query($sql,$this->dbhandle);
                        $this->sql_queries_counter++;
                        return true;
                } else return false;
@@ -203,9 +227,10 @@
                        }
                        $sql=substr($sql,0,strlen($sql)-4);     //cut last AND
                }
+               $sql.=" ".$extra;
                $sql.=" LIMIT 1";
                if ($this->sqlcaching &&  
$cache=$this->sqlcache->get_cached_query($sql,$this->sqlcaching_ttl))  
return $cache;
-               $sql.=" ".$extra;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                if (!$res) { $this->lasterror='Query returned null'; return 
false; }
@@ -243,13 +268,13 @@
                }
                $sql.=" ".$extra;
                if ($this->sqlcaching &&  
$cache=$this->sqlcache->get_cached_query($sql,$this->sqlcaching_ttl))  
return $cache;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                if (!$res) { $this->lasterror='Query returned null'; return 
false; }
                $rows=array();
-               while ($r...@mysql_fetch_assoc($res)) {
+               while ($r...@mysql_fetch_assoc($res))
                        $rows[]=$row;
-               }
                $this->sql_queries_counter++;
                if ($this->sqlcaching)  
$this->sqlcache->cache_query($sql,$rows,$this->sqlcaching_ttl);
                return $rows;
@@ -260,11 +285,12 @@
   *
   * @table - table name
   * @like - array of the rows to find
+ * @extra - extra query
   * @returns array of row if succeed, if not returns false
   *
   */

-       public function get_likerows($table,$like=array()) {
+       public function get_likerows($table,$like=array(),$extra="") {
                $sql="SELECT * from $table";
                if (empty($like)) return false;
                        $sql.=" WHERE ";
@@ -273,11 +299,13 @@
                        if (empty($var)) {
                                $this->lasterror='find array misformed';
                                return false;
-                               }
+                               }
                        $sql.="`$var` LIKE '$value' AND";
                        }
                        $sql=substr($sql,0,strlen($sql)-4);     //cut last AND
+                       if (!empty($extra)) $sql.=" ".$extra;
                if ($this->sqlcaching &&  
$cache=$this->sqlcache->get_cached_query($sql,$this->sqlcaching_ttl))  
return $cache;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                if (!$res) { $this->lasterror='Query returned null'; return 
false; }
@@ -294,15 +322,16 @@
   * Simple sql query call
   *
   * @query - table name
-  * @returns array of row if succeed, if not returns false
- *
+ * @returns array of row if succeed, if not returns false
+ *
   */

        public function sql($query) {
                if (empty($query)) return false;
                if ($this->sqlcaching &&  
$cache=$this->sqlcache->get_cached_query($sql,$this->sqlcaching_ttl))  
return $cache;
-               if ($this->method=="mysqli") 
$r...@mysqli_query($query,$this->dbhandle);
-                       else $r...@mysql_query($query,$this->dbhandle);
+               $this->lastsql=$query;
+               if ($this->method=="mysqli") 
$res=mysqli_query($query,$this->dbhandle);
+                       else $res=mysql_query($query,$this->dbhandle);
                if (!$res) return array();
                $rows=array();
                while ($r...@mysql_fetch_assoc($res)) {
@@ -340,6 +369,7 @@
                }
                $sql.=" LIMIT 1";
                $sql.=" ".$extra;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                $this->sql_queries_counter++;
@@ -372,6 +402,7 @@
                        $sql=substr($sql,0,strlen($sql)-4);     //cut last AND
                }
                $sql.=" ".$extra;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                $this->sql_queries_counter++;
@@ -409,6 +440,7 @@
                        $sql=substr($sql,0,strlen($sql)-4);     //cut last AND
                }
                $sql.=" ".$extra;
+               $this->lastsql=$sql;
                if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
                $this->sql_queries_counter++;
@@ -430,17 +462,23 @@
                $vars=''; $values='';
                foreach ($row as $var=>$value) {
                        $vars.="`$var`,";
+                       if (strstr($value,"()"))
+                       $values.="$value,"; else                        
                        $values.="'$value',";
                }
                $vars=substr($vars,0,strlen($vars)-1);
                $values=substr($values,0,strlen($values)-1);
                $sql="insert into `$table` ($vars) values ($values)";
                $sql.=" ".$extra;
-               if ($this->method=="mysqli") 
$r...@mysqli_query($sql,$this->dbhandle);
+               $this->lastsql=$sql;
+               if ($this->method=="mysqli")  
$r...@mysqli_query($this->dbhandle,$sql,$this->dbhandle);
                        else $r...@mysql_query($sql,$this->dbhandle);
+               $this->lastsql=$sql;
                $this->sql_queries_counter++;
                $this->rows_affect...@mysql_affected_rows($this->dbhandle);
-               return true;
+               if (!$res)
+                       $this->lasterror="insert query failed, 
{$this->rows_affected} rows  
affected";
+               return $res;
        }

  /**
@@ -453,25 +491,49 @@
   *
   */

-       public function insert_rows($table,array $vars,array $rows) {
-               if (empty($table) || empty($values) || empty($rows)) return 
false;
+       public function insert_rows($table,array $vars, array $values) {
+               if (empty($table) || empty($values) || empty($vars)) return 
false;
                $insert="insert into `$table`(";
                foreach ($vars as $var) $insert.="`$var`,";
                $insert=substr($insert,0,strlen($insert)-1);
                $insert.=") values ";
-               foreach ($rows as $row) {
+               foreach ($values as $values_packed) {
                         $insert.="(";
-                        foreach($row as $values) $insert.="\"$values\",";
+                        foreach($values_packed as $value) 
$insert.="\"$value\",";
                         $insert=substr($insert,0,strlen($insert)-1);
                         $insert.="),";
                        }
                $insert=substr($insert,0,strlen($insert)-1);
                $insert.=" ".$extra;
-               if ($this->method=="mysqli") 
$r...@mysqli_query($insert,$this->dbhandle);
+               if ($this->method=="mysqli")  
$r...@mysqli_query($this->dbhandle,$insert,$this->dbhandle);
                        else $r...@mysql_query($insert,$this->dbhandle);
+               $this->lastsql=$insert;
                $this->sql_queries_counter++;
                $this->rows_affect...@mysql_affected_rows($this->dbhandle);
-               return true;
+               if (!$res)
+                       $this->lasterror="insert query failed, 
{$this->rows_affected} rows  
affected";
+               return $res;
+       }
+
+/**
+ * Available tables informations
+ *
+ * @returns Tables with their info
+ *
+ */
+
+       public function get_tables() {
+               $sql="show table status";
+               if ($this->method=="mysqli")  
$r...@mysqli_query($this->dbhandle,$sql,$this->dbhandle);
+                       else $r...@mysql_query($sql,$this->dbhandle);
+               $this->lastsql=$sql;
+               $this->sql_queries_counter++;
+               $rows=array();
+               while ($r...@mysql_fetch_assoc($res))
+                       $rows[]=$row;
+               $this->sql_queries_counter++;
+               if ($this->sqlcaching)  
$this->sqlcache->cache_query($sql,$rows,$this->sqlcaching_ttl);
+               return $rows;
        }

  }

Modified: projects/webui/trunk/phprd/template/template.php
==============================================================================
--- projects/webui/trunk/phprd/template/template.php    (original)
+++ projects/webui/trunk/phprd/template/template.php    Sun Feb  8 12:55:45  
2009
@@ -5,22 +5,28 @@
   * @copyright 2008
   */

+DEFINE('PAGE_EXPIRATION_NONE',0);
+DEFINE('PAGE_EXPIRATION_PAST',1);
+DEFINE('PAGE_EXPIRATION_FUTURE',2);
+DEFINE('PAGE_EXPIRATION_NOW',3);
+
  class template {

        private $template_path;
        private $page_buffer;
        private $assignedvars;
+       private $page_expiration=false;

  /**
- * Get file from template (private)
+ * Get file from template (private function)
   *
   * @filename the name of the file
   * @chunk the size of the chunk in which the files will be readed  
(dfefault 1024)
   * @returns file content or false if file does not exists
   *
- */    
+ */
        private function getfile($filename,$chunk=1024) {
-               $buffer='';
+               $buffer='';$cbuf='';
                if (!file_exists($filename)) return false;
                $fhandle=fopen($filename,'r');
                $cbuf.=fread($fhandle,$chunk); $buffer.=$cbuf;
@@ -124,8 +130,8 @@
                switch ($directive) {
                        case "FORWARD_POST":
                        $replace='';
-                       foreach ($_POST as $var=>$value) $replace.="<input 
type='hidden'  
name='$var' value='$value' />";
-                        
$this->page_buffer=str_replace("%&FORWARD_POST&%",$replace,$this->page_buffer);
+                       foreach ($_POST as $var=>$value) $replace.="<input 
type='hidden'  
name='$var' value='$value' />";
+                        
$this->page_buffer=str_replace("%^FORWARD_POST^%",$replace,$this->page_buffer);
                        case "ALL":
                }
        }
@@ -152,16 +158,143 @@
                        $this->replace_tags($var,$value,$tag);
                }
                $dir_offset=0;
-               while ($dir_start=strpos($this->page_buffer,"%&",$dir_offset)) {
-                       $dir_end=strpos($this->page_buffer,"&%",$dir_start+1);
+               while ($dir_start=strpos($this->page_buffer,"%^",$dir_offset)) {
+                       $dir_end=strpos($this->page_buffer,"^%",$dir_start+1);
                        
$dir=substr($this->page_buffer,$dir_start+2,$dir_end-$dir_start-2);
                        self::directive_execute($dir);
                        $dir_offset=$dir_end+1;
                        
                }
+               $this->page_expiration();
                return $this->page_buffer;
        }

-}
+/**
+ * Quick render the page
+ *
+ * @vars vars to be replaced
+ * @returns true if rendered ok, false if not
+ *
+ */
+       public function quickrender(array $vars) {
+               $this->assign_var($vars);
+               $page=$this->finalize_page();
+               print $page;
+               if (!empty($page))
+                       return true; else
+                       return false;
+       }
+
+/**
+ * Return all block names and their content
+ *
+ * @blocksign block sign for execution
+ * @returns array with vars and values in blocks
+ *
+ */
+
+       private function get_allblocks($blocksign) {
+               $result=array();
+               $buffer=$this->page_buffer;
+               while ($blockvar_start=strpos($buffer,"%{$blocksign}")) {
+                       
$blockvar_start_e=strpos($buffer,"%",$blockvar_start+1)+1;
+                       if ($blockvar_start_e<1)
+                               return false;
+                        
$blockvar=substr($buffer,$blockvar_start+2,$blockvar_start_e-$blockvar_start-3);
+                        
$blockvar_end=strpos($buffer,"%/{$blocksign}".$blockvar,$blockvar_start_e+1)+1;
+                       if ($blockvar_start_e<1)
+                               return false;
+                        
$block=substr($buffer,$blockvar_start_e,$blockvar_end-($blockvar_start_e+1));
+                       $result[$blockvar]=$block;
+                       
$buffer=str_replace("%?{$blockvar}%{$block}%/?{$blockvar}%","",$buffer);
+                       }
+               $result=(empty($result))?false:$result;
+               return $result;
+               }
+
+/**
+ * Process blocks
+ *
+ *
+ */
+       public function blocks() {
+               global $template_path;
+               $compiled_hash=md5($this->page_buffer);
+               $compiled_page=$this->page_buffer;
+               
+               if ($block=$this->get_allblocks("?"))
+                       foreach ($block as $var=>$value) {
+                               global $$var;
+                               $blockvar_value=$$var;
+                               if ($blockvar_value)
+                                
$this->page_buffer=str_replace("%?{$var}%{$value}%/?{$var}%",$value,$this->page_buffer);
+                               else
+                                
$this->page_buffer=str_replace("%?{$var}%{$value}%/?{$var}%","",$this->page_buffer);
+                               
$compiled_page=str_replace("%?{$var}%{$value}%/?{$var}%","<?php  
if(\${$var}) { ?>{$value}<?php } ?>",$compiled_page);
+                       }
+               while (strstr($this->page_buffer,"%!")) {
+                       $block_s=strpos($this->page_buffer,"%!");
+                       $block_e=strpos($this->page_buffer,"%",$block_s+1) or 
dir("Bad  
template, no ending sign!");
+                       
$block=substr($this->page_buffer,$block_s+2,$block_e-$block_s-2);
+                       $param_s=strpos($block,"(");
+                       $param_e=strpos($block,")");
+                       $func=substr($block,0,$param_s);
+                       $param=substr($block,$param_s+1,$param_e-$param_s-1);
+                       $param_array=explode("\",\"",$param);
+                       $param_array_cleaned=array();
+                       foreach ($param_array as $param_array_one) {
+                               
$param_array_one=str_replace("\"","",$param_array_one);
+                               $param_array_cleaned[]=$param_array_one;
+                               }
+                       
$result=call_user_func_array($func,$param_array_cleaned);
+                        
$this->page_buffer=str_replace("%!".$block."%",$result,$this->page_buffer);
+                       $compiled_page=str_replace("%!".$block."%","<?php echo 
{$func}  
({$param}); ?>",$compiled_page);
+                       }
+               
file_put_contents($template_path.$compiled_hash.".tpc",$compiled_page);
+       }
+
+/**
+ * Internal page expiration function
+ *
+ *
+ */
+
+       private function page_expiration() {
+               if (!$this->page_expiration)
+                       return false;
+               header('Pragma: public');
+               header($this->page_expiration);
+               header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
+               header('Cache-Control: no-store, no-cache, must-revalidate');   
  //  
HTTP/1.1
+               header('Cache-Control: pre-check=0, post-check=0, max-age=0');  
  //  
HTTP/1.1
+               header ("Pragma: no-cache");
+               }
+
+
+/**
+ * Process blocks
+ *
+ *
+ */
+
+       public function set_pageexpiration($expiration) {
+               switch ($expiration) {
+                       case PAGE_EXPIRATION_PAST:
+                       $this->page_expiration="Expires: Sat, 26 Jul 1997 
05:00:00 GMT";
+                       break;
+                       case PAGE_EXPIRATION_FUTURE:
+                       $this->page_expiration="Expires: Sat, 26 Jul 2097 
05:00:00 GMT";
+                       break;
+                       case PAGE_EXPIRATION_NOW:
+                       $d=date("D, d M Y H:00:00");
+                       $this->page_expiration="Expires: {$d} GMT";
+                       break;
+                       case PAGE_EXPIRATION_NONE:
+                       default:
+                       $this->page_expiration=false;
+                       }
+               }
+
+}      // template class

  ?>

Modified: projects/webui/trunk/phprd/users/users.php
==============================================================================
--- projects/webui/trunk/phprd/users/users.php  (original)
+++ projects/webui/trunk/phprd/users/users.php  Sun Feb  8 12:55:45 2009
@@ -7,55 +7,152 @@

  class user_sessions {
        
-       private $storage="UNDEFINED";
+       private $storage="UNDEFINED";   // file or mysql implemented
+       private $db_handle=NULL;
+       private $db_table=NULL;
        
-       public function __construct() {
-               
+       public function  
__construct($storage_engine,$mysql_server="localhost",$mysql_username="root",$mysql_password="",$mysql_db="phprd",$mysql_table="users")
  
{
+               switch ($storage_engine) {
+                       case "mysql":
+                               $this->db_handle=new mysql;
+                                
$r=$this->db_handle->connect($mysql_server,$mysql_username,$mysql_password,$mysql_db);
+                               if (!$r) {
+                                       $this->db_handle=false;
+                                       return false;
+                               }
+                               $query='CREATE TABLE IF NOT EXISTS `users`(`id` 
serial, `username`  
varchar(32) default "" not null, `password` varchar(32) default "" not  
null, `email` varchar(64) default "" not null, `group` varchar(32)  
default "" not null)';
+                               $r=$this->db_handle->sql($query);
+                               $this->storage=$storage_engine;
+                               $this->db_table=$mysql_table;
+                       break;
+                       case "file":
+                               $this->storage=$storage_engine;
+                       break;
+                       default:
+                               return false;
+                       break;
+                       }
+               return true;
        }
        
-       public function set_storage($storage) {
-               if (empty($storage)) return false;
-               $this->storage=$storage;
-               return true;
+       public function login_http($realm="Restricted") {
+               if (isset($_SERVER['PHP_AUTH_USER']) && 
isset($_SERVER['PHP_AUTH_PW'])) {
+       $username = mysql_escape_string(trim($_SERVER['PHP_AUTH_USER']));
+       $password = mysql_escape_string(trim($_SERVER['PHP_AUTH_PW']));
+       $r=$this->login_user($username,$password);
+       if (!$r) {
+                       header("WWW-Authenticate: Basic realm=\"$realm\"");
+               header("HTTP/1.0 401 Unauthorized");
+               die();
+                       } else return true;
+               } else {
+                       header("WWW-Authenticate: Basic realm=\"$realm\"");
+               header("HTTP/1.0 401 Unauthorized");
+               die();
+               }
        }
        
-       public function check_session($db,$table,$passwordmethod='md5') {
-               switch ($this->storage) {
+       public function login_user($username,$password,$userdb="users.db") {
+                       switch ($this->storage) {
                        case "file":
-                       
+                               
$r=$this->login_file($username,$password,$userdb);
+                               if ($r) {
+                                       if (empty($_SESSION['online']))
+                                               $_SESSION['online']=time();
+                                       return true;
+                               }
                        break;
                        case "mysql":
-                               return 
$this->check_session_mysql($db,$table,$passwordmethod='md5');
-                       break;
+                               $r=$this->login_mysql($username,$password);
+                               if ($r) {
+                                       if (empty($_SESSION['online']))
+                                               $_SESSION['online']=time();
+                                       return true;
+                               }
+                               break;
                        default:
-                               return false;
+                       return false;
                }
+       }
+       
+       private function login_mysql ($s_username,$s_password) {
+               if (empty($s_username) || empty($s_password)) return false;
+               
$finduser=array("username"=>$s_username,"password"=>md5($s_password));
+               $res=$this->db_handle->get_row($this->db_table,$finduser);
+               if (!$res)
+                       return false;
+               $_SESSION['s_username']=$s_username;
+               $_SESSION['s_password']=$s_password;
+               return true;
+       }
+       
+       private function login_file($username,$password,$userdb) {
+               if (empty($username) || empty($password) || empty($userdb))
+                       return false;
+               if (!file_exists($userdb))
+                       return false;
+               $fh=fopen($userdb,"r");                 //open db in read-only 
mode
+               while (!feof($fh))
+                       $users.=fread($fh,999999);      //read all 
accounts-warning, very big userdb  
can eat alot of mem
+               fclose($fh);
+               $users=explode("\r\n",$users);  //parse all users
+               foreach ($users as $user) {
+                       if (!empty($user)) {
+                               $user=unserialize($user);
+                               if ($username==$user["username"] && 
md5($password)==$user["password"])  
return true;
+                       }
                }
-               
-       public function login_user($username,$password,$userdb) {
+               return false;
+       }
+
+       public function create_user($username,$password,$email,$group) {
                        switch ($this->storage) {
                        case "file":
-                               return 
$this->login_file($username,$password,$userdb);
+                               return 
$this->create_user_file($username,$password,$email,$group);
                        break;
                        case "mysql":
-                               return 
$this->login_mysql($db,$table,$passwordmethod='md5');
+                               if (!$this->db_handle)
+                                       return false;
+                               $username=mysql_real_escape_string($username);
+                               $password=md5($password);
+                               $find=array("username"=>$username);
+                               $row=$this->db_handle->get_row("users",$find);
+                               if (!empty($row))
+                                       return false;   // user with same 
username already exists!
+                                
$row=array("username"=>$username,"password"=>$password,"email"=>$email,"group"=>$group);
+                               $this->db_handle->insert_row("users",$row);
+                               if ($this->db_handle->rows_affected<1)
+                                       return false;
+                               else
+                                       return true;
                                break;
                        default:
                        return false;
                }
        }
-       
-       public function create_user($username,$password,$email,$group,$userdb) {
-                       switch ($this->storage) {
+
+       private function create_user_file($username,$password,$email,$group) {
+               $fh=fopen($userdb,"a");
+                
$user=array("username"=>$username,"password"=>md5($password),"email"=>$email,"group"=>$group);
+               fseek($fh,0,SEEK_END);
+               $users=fwrite($fh,serialize($user)."\r\n");
+               fclose($fh);
+               return true;
+       }
+
+       public function check_session($db,$table,$passwordmethod='md5') {
+               switch ($this->storage) {
                        case "file":
-                               return  
$this->create_user_file($username,$password,$email,$group,$userdb);
+                               return 
$this->check_session_file($db,$table,$passwordmethod='md5');
                        break;
                        case "mysql":
-                               break;
+                               return 
$this->check_session_mysql($db,$table,$passwordmethod='md5');
+                       break;
                        default:
-                       return false;
+                               return false;
+               }
                }
-       }       
+
        private function check_session_mysql (mysql  
&$db,$table="users",$passwordmethod='md5') {
                if (!$db) die('No database set!');
                session_start();
@@ -78,53 +175,16 @@
                return true;
        }

-       private function login_mysql (mysql  
&$db,$table,$s_username,$s_password,$passwordmethod='md5') {
-               if (empty($db) || empty($table) || empty($s_username) ||  
empty($s_password)) return false;
-               switch ($passwordmethod) {
-                       case "md5":
-                       
$finduser=array("username"=>$s_username,"password"=>md5($s_password));
-                       break;
-                       case "sha1":
-                       
$finduser=array("username"=>$s_username,"password"=>sha1($s_password));
-                       break;
-                       default:        //plaintext
-                       
$finduser=array("username"=>$s_username,"password"=>$s_password);
-               }
-               $res=$db->get_row($table,$finduser);
-               if (!$res) return false;
-               $_SESSION['s_username']=$s_username;
-               $_SESSION['s_password']=$s_password;            
-               return true;
-       }
-       
-       private function login_file($username,$password,$userdb) {
-               if (empty($username) || empty($password) || empty($userdb))
-                       return false;
-               if (!file_exists($userdb))
-                       return false;
-               $fh=fopen($userdb,"r");                 //open db in read-only 
mode
-               while (!feof($fh))
-                       $users.=fread($fh,999999);      //read all 
accounts-warning, very big userdb  
can eat alot of mem
-               fclose($fh);
-               $users=explode("\r\n",$users);  //parse all users
-               foreach ($users as $user) {
-                       if (!empty($user)) {
-                               $user=unserialize($user);
-                               if ($username==$user["username"] && 
md5($password)==$user["password"])  
return true;
-                       }
-               }
-               return false;
-       }
-       
-       private function  
create_user_file($username,$password,$email,$group,$userdb) {
-               $fh=fopen($userdb,"a");
-                
$user=array("username"=>$username,"password"=>md5($password),"email"=>$email,"group"=>$group);
-               fseek($fh,0,SEEK_END);
-               $users=fwrite($fh,serialize($user)."\r\n");
-               fclose($fh);
-               return true;
+       private function  
check_session_file($db,$table="users",$passwordmethod='md5') {
+               if (!empty($db)) die('No database set!');
+               session_start();
+               $s_username=$_SESSION['s_username'];
+               $s_password=$_SESSION['s_password'];
+               if (empty($s_username) || empty($s_password)) return false;
+               if (empty($_SESSION['online'])) $_SESSION['online']=time();
+               return $this->login_file($s_username,$s_password,$db);
        }
-       
+
        public function logout () {
                $_SESSION['s_username']="";
                $_SESSION['s_password']="";

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"lusca-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/lusca-commit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to