From 1f37f024ca3f159e2c3b0f6bf3d274cf5721929e Mon Sep 17 00:00:00 2001
From: Ralf Becker <ralfbecker@outdoor-training.de>
Date: Sun, 13 Nov 2005 09:47:05 +0000
Subject: [PATCH] phpgw_access_log --> egw_access_log

---
 admin/inc/class.soaccess_history.inc.php | 38 ++++++++++------------
 phpgwapi/inc/class.sessions.inc.php      | 40 +++++++++++++++++-------
 phpgwapi/setup/setup.inc.php             |  4 +--
 phpgwapi/setup/tables_current.inc.php    |  2 +-
 phpgwapi/setup/tables_update.inc.php     |  9 ++++++
 setup/setup_demo.php                     |  5 ++-
 6 files changed, 58 insertions(+), 40 deletions(-)

diff --git a/admin/inc/class.soaccess_history.inc.php b/admin/inc/class.soaccess_history.inc.php
index 7a650c5e2a..0a3c3ece8d 100644
--- a/admin/inc/class.soaccess_history.inc.php
+++ b/admin/inc/class.soaccess_history.inc.php
@@ -14,35 +14,31 @@
 	class soaccess_history
 	{
 		var $db;
+		var $table = 'egw_access_log';
 
 		function soaccess_history()
 		{
-			$this->db       = clone($GLOBALS['egw']->db);
+			$this->db = clone($GLOBALS['egw']->db);
+			$this->db->set_app('phpgwapi');
 		}
 
 		function test_account_id($account_id)
 		{
 			if ($account_id)
 			{
-				return " where account_id='$account_id'";
+				return array('account_id' => $account_id);
 			}
+			return false;
 		}
 
-		function list_history($account_id,$start,$order,$sort)
+		function &list_history($account_id,$start,$order,$sort)
 		{
 			$where = $this->test_account_id($account_id);
 
-			$this->db->limit_query("select loginid,ip,li,lo,account_id,sessionid from phpgw_access_log $where order by li desc",$start,__LINE__,__FILE__);
-			while ($this->db->next_record())
+			$this->db->select($this->table,'loginid,ip,li,lo,account_id,sessionid',$where,__LINE__,__FILE__,(int) $start,'ORDER BY li DESC');
+			while (($row = $this->db->row(true)))
 			{
-				$records[] = array(
-					'loginid'    => $this->db->f('loginid'),
-					'ip'         => $this->db->f('ip'),
-					'li'         => $this->db->f('li'),
-					'lo'         => $this->db->f('lo'),
-					'account_id' => $this->db->f('account_id'),
-					'sessionid'  => $this->db->f('sessionid')
-				);
+				$records[] = $row;
 			}
 			return $records;
 		}
@@ -51,22 +47,20 @@
 		{
 			$where = $this->test_account_id($account_id);
 
-			$this->db->query("select count(*) from phpgw_access_log $where");
-			$this->db->next_record();
+			$this->db->select($this->table,'COUNT(*)',$where,__LINE__,__FILE__);
 
-			return $this->db->f(0);
+			return $this->db->next_record() ? $this->db->f(0) : 0;
 		}
 
 		function return_logged_out($account_id)
 		{
+			$where = array('lo != 0');
 			if ($account_id)
 			{
-				$where = "and account_id='$account_id'";
+				$where['account_id'] = $account_id;
 			}
-
-			$this->db->query("select count(*) from phpgw_access_log where lo!=0 $where");
-			$this->db->next_record();
-
-			return $this->db->f(0);
+			$this->db->select($this->table,'COUNT(*)',$where,__LINE__,__FILE__);
+			
+			return $this->db->next_record() ? $this->db->f(0) : 0;
 		}
 	}
diff --git a/phpgwapi/inc/class.sessions.inc.php b/phpgwapi/inc/class.sessions.inc.php
index 7a288553d8..eceab1d95b 100644
--- a/phpgwapi/inc/class.sessions.inc.php
+++ b/phpgwapi/inc/class.sessions.inc.php
@@ -104,6 +104,11 @@
 		* @var object holder for the database object
 		*/
 		var $db;
+		
+		/**
+		 * @var $access_table name of access-log table
+		 */
+		var $access_table = 'egw_access_log';
         
 		/**
 		* @var array publicly available methods
@@ -136,13 +141,14 @@
 		function sessions_($domain_names=null)
 		{
 			$this->db = clone($GLOBALS['egw']->db);
+			$this->db->set_app('phpgwapi');
 			$this->sessionid = get_var('sessionid',array('GET','COOKIE'));
 			$this->kp3       = get_var('kp3',array('GET','COOKIE'));
 
 			$this->phpgw_domains = $domain_names;
 
 			/* Create the crypto object */
-			$GLOBALS['egw']->crypto = CreateObject('phpgwapi.crypto');
+			$GLOBALS['egw']->crypto =& CreateObject('phpgwapi.crypto');
 			if ($GLOBALS['egw_info']['server']['usecookies'])
 			{
 				$this->phpgw_set_cookiedomain();
@@ -600,26 +606,30 @@
 		{
 			$now = time();
 
-			if ($login != '')
+			if ($login)
 			{
 				if (strlen($login) > 30)
 				{
 					$login = substr($login,0,30);
 				}
-				$GLOBALS['egw']->db->query('INSERT INTO phpgw_access_log(sessionid,loginid,ip,li,lo,account_id)'
-					. " VALUES ('" . $sessionid . "','" . $this->db->db_addslashes($login). "','"
-					. $this->db->db_addslashes($user_ip) . "',$now,0," . (int)$account_id .')',__LINE__,__FILE__);
+				$GLOBALS['egw']->db->insert($this->access_table,array(
+					'sessionid' => $sessionid,
+					'loginid'   => $login,
+					'ip'        => $user_ip,
+					'li'        => $now,
+					'lo'        => 0,
+					'account_id'=> $account_id,
+				),__LINE__,__FILE__);
 			}
 			else
 			{
-				$GLOBALS['egw']->db->query("UPDATE phpgw_access_log SET lo=" . $now . " WHERE sessionid='"
-					. $sessionid . "'",__LINE__,__FILE__);
+				$GLOBALS['egw']->db->update($this->access_table,array('lo' => $now),array('sessionid' => $sessionid),__LINE__,__FILE__);
 			}
 			if ($GLOBALS['egw_info']['server']['max_access_log_age'])
 			{
 				$max_age = $now - $GLOBALS['egw_info']['server']['max_access_log_age'] * 24 * 60 * 60;
 
-				$GLOBALS['egw']->db->query("DELETE FROM phpgw_access_log WHERE li < $max_age");
+				$GLOBALS['egw']->db->delete($this->access_table,"li < $max_age",__LINE__,__FILE__);
 			}
 		}
 
@@ -635,16 +645,22 @@
 			$blocked = False;
 			$block_time = time() - $GLOBALS['egw_info']['server']['block_time'] * 60;
 
-			$ip = $this->db->db_addslashes($ip);
-			$this->db->query("SELECT count(*) FROM phpgw_access_log WHERE account_id=0 AND ip='$ip' AND li > $block_time",__LINE__,__FILE__);
+			$this->db->select($this->access_table,'COUNT(*)',array(
+				'account_id = 0',
+				'ip'         => $ip,
+				"li > $block_time",
+			),__LINE__,__FILE__);
 			$this->db->next_record();
 			if (($false_ip = $this->db->f(0)) > $GLOBALS['egw_info']['server']['num_unsuccessful_ip'])
 			{
 				//echo "<p>login_blocked: ip='$ip' ".$this->db->f(0)." trys (".$GLOBALS['egw_info']['server']['num_unsuccessful_ip']." max.) since ".date('Y/m/d H:i',$block_time)."</p>\n";
 				$blocked = True;
 			}
-			$login = $this->db->db_addslashes($login);
-			$this->db->query("SELECT count(*) FROM phpgw_access_log WHERE account_id=0 AND (loginid='$login' OR loginid LIKE '$login@%') AND li > $block_time",__LINE__,__FILE__);
+			$this->db->select($this->access_table,'COUNT(*)',array(
+				'account_id = 0',
+				'(loginid = '.$this->db->quote($login).' OR loginid LIKE '.$this->db->quote($login.'@%').')',
+				"li > $block_time",
+			),__LINE__,__FILE__);
 			$this->db->next_record();
 			if (($false_id = $this->db->f(0)) > $GLOBALS['egw_info']['server']['num_unsuccessful_id'])
 			{
diff --git a/phpgwapi/setup/setup.inc.php b/phpgwapi/setup/setup.inc.php
index b113331daf..ab6efcab76 100755
--- a/phpgwapi/setup/setup.inc.php
+++ b/phpgwapi/setup/setup.inc.php
@@ -14,7 +14,7 @@
 	/* Basic information about this app */
 	$setup_info['phpgwapi']['name']      = 'phpgwapi';
 	$setup_info['phpgwapi']['title']     = 'eGroupWare API';
-	$setup_info['phpgwapi']['version']   = '1.0.1.024';
+	$setup_info['phpgwapi']['version']   = '1.0.1.025';
 	$setup_info['phpgwapi']['versions']['current_header'] = '1.28';
 	$setup_info['phpgwapi']['enable']    = 3;
 	$setup_info['phpgwapi']['app_order'] = 1;
@@ -27,7 +27,7 @@
 	$setup_info['phpgwapi']['tables'][]  = 'egw_preferences';
 	$setup_info['phpgwapi']['tables'][]  = 'egw_sessions';
 	$setup_info['phpgwapi']['tables'][]  = 'egw_app_sessions';
-	$setup_info['phpgwapi']['tables'][]  = 'phpgw_access_log';
+	$setup_info['phpgwapi']['tables'][]  = 'egw_access_log';
 	$setup_info['phpgwapi']['tables'][]  = 'egw_hooks';
 	$setup_info['phpgwapi']['tables'][]  = 'egw_languages';
 	$setup_info['phpgwapi']['tables'][]  = 'egw_lang';
diff --git a/phpgwapi/setup/tables_current.inc.php b/phpgwapi/setup/tables_current.inc.php
index 4b12d5d840..8849b4aa03 100644
--- a/phpgwapi/setup/tables_current.inc.php
+++ b/phpgwapi/setup/tables_current.inc.php
@@ -112,7 +112,7 @@
 			'ix' => array(),
 			'uc' => array()
 		),
-		'phpgw_access_log' => array(
+		'egw_access_log' => array(
 			'fd' => array(
 				'sessionid' => array('type' => 'char','precision' => '32','nullable' => False),
 				'loginid' => array('type' => 'varchar','precision' => '64','nullable' => False),
diff --git a/phpgwapi/setup/tables_update.inc.php b/phpgwapi/setup/tables_update.inc.php
index d4eeda4c10..78942f32f0 100644
--- a/phpgwapi/setup/tables_update.inc.php
+++ b/phpgwapi/setup/tables_update.inc.php
@@ -861,4 +861,13 @@
 
 		return $GLOBALS['setup_info']['phpgwapi']['currentver'] = '1.0.1.024';
 	}
+
+
+	$test[] = '1.0.1.024';
+	function phpgwapi_upgrade1_0_1_024()
+	{
+		$GLOBALS['egw_setup']->oProc->RenameTable('phpgw_access_log','egw_access_log');
+
+		return $GLOBALS['setup_info']['phpgwapi']['currentver'] = '1.0.1.025';
+	}
 ?>
diff --git a/setup/setup_demo.php b/setup/setup_demo.php
index 386814e02e..5174fa4580 100644
--- a/setup/setup_demo.php
+++ b/setup/setup_demo.php
@@ -95,6 +95,8 @@
 			{
 				$GLOBALS['egw_setup']->db->delete($table,'1=1');
 			}
+			/* Clear the access log, since these are all new users anyway */
+			$GLOBALS['egw_setup']->db->query('DELETE FROM egw_access_log');
 		}
 		/* Create the demo groups */
 		$defaultgroupid = (int)$GLOBALS['egw_setup']->add_account('Default','Default','Group',False,False);
@@ -187,9 +189,6 @@
 		$GLOBALS['egw_setup']->add_acl('phpgw_group',$admingroupid,$accountid);
 		$GLOBALS['egw_setup']->add_acl('phpgw_group',$defaultgroupid,$accountid);
 
-		/* Clear the access log, since these are all new users anyway */
-		$GLOBALS['egw_setup']->db->query('DELETE FROM phpgw_access_log');
-
 		$GLOBALS['egw_setup']->db->transaction_commit();
 
 		Header('Location: index.php');