Index: sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-perf.inc.php
===================================================================
--- sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-perf.inc.php	(revision 6057)
+++ sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-perf.inc.php	(revision 8222)
@@ -1,5 +1,5 @@
 <?php
 /* 
-V4.94 23 Jan 2007  (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
+V5.18 3 Sep 2012   (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved.
   Released under both BSD license and Lesser GPL library license. 
   Whenever there is any discrepancy between the two licenses, 
@@ -23,4 +23,8 @@
 define( 'ADODB_OPT_LOW', 1);
 
+global $ADODB_PERF_MIN;
+$ADODB_PERF_MIN = 0.05; // log only if >= minimum number of secs to run
+
+
 // returns in K the memory of current process, or 0 if not known
 function adodb_getmem()
@@ -54,20 +58,18 @@
 }
 
-/* return microtime value as a float */
+/* obsolete: return microtime value as a float. Retained for backward compat */
 function adodb_microtime()
 {
-	$t = microtime();
-	$t = explode(' ',$t);
-	return (float)$t[1]+ (float)$t[0];
+	return microtime(true);
 }
 
 /* sql code timing */
-function& adodb_log_sql(&$connx,$sql,$inputarr)
+function adodb_log_sql(&$connx,$sql,$inputarr)
 {
     $perf_table = adodb_perf::table();
 	$connx->fnExecute = false;
-	$t0 = microtime();
-	$rs =& $connx->Execute($sql,$inputarr);
-	$t1 = microtime();
+	$a0 = microtime(true);
+	$rs = $connx->Execute($sql,$inputarr);
+	$a1 = microtime(true);
 
 	if (!empty($connx->_logsql) && (empty($connx->_logsqlErrors) || !$rs)) {
@@ -75,5 +77,5 @@
 	
 		if (!empty($ADODB_LOG_CONN)) {
-			$conn = &$ADODB_LOG_CONN;
+			$conn = $ADODB_LOG_CONN;
 			if ($conn->databaseType != $connx->databaseType)
 				$prefix = '/*dbx='.$connx->databaseType .'*/ ';
@@ -81,5 +83,5 @@
 				$prefix = '';
 		} else {
-			$conn =& $connx;
+			$conn = $connx;
 			$prefix = '';
 		}
@@ -87,10 +89,4 @@
 		$conn->_logsql = false; // disable logsql error simulation
 		$dbT = $conn->databaseType;
-		
-		$a0 = preg_split('/ /',$t0);
-		$a0 = (float)$a0[1]+(float)$a0[0];
-		
-		$a1 = preg_split('/ /',$t1);
-		$a1 = (float)$a1[1]+(float)$a1[0];
 		
 		$time = $a1 - $a0;
@@ -114,7 +110,7 @@
 		if (isset($_SERVER['HTTP_HOST'])) {
 			$tracer .= '<br>'.$_SERVER['HTTP_HOST'];
-			if (isset($_SERVER['PHP_SELF'])) $tracer .= $_SERVER['PHP_SELF'];
+			if (isset($_SERVER['PHP_SELF'])) $tracer .= htmlspecialchars($_SERVER['PHP_SELF']);
 		} else 
-			if (isset($_SERVER['PHP_SELF'])) $tracer .= '<br>'.$_SERVER['PHP_SELF'];
+			if (isset($_SERVER['PHP_SELF'])) $tracer .= '<br>'.htmlspecialchars($_SERVER['PHP_SELF']);
 		//$tracer .= (string) adodb_backtrace(false);
 		
@@ -166,5 +162,11 @@
 			$isql = "insert into $perf_table (created,sql0,sql1,params,tracer,timer) values( $d,?,?,?,?,?)";
 		}
-		$ok = $conn->Execute($isql,$arr);
+		
+		global $ADODB_PERF_MIN;
+		if ($errN != 0 || $time >= $ADODB_PERF_MIN) {
+			$ok = $conn->Execute($isql,$arr);
+		} else
+			$ok = true;
+		
 		$conn->debug = $saved;
 		
@@ -174,5 +176,5 @@
 			$err2 = $conn->ErrorMsg();
 			$conn->_logsql = true; // enable logsql error simulation
-			$perf =& NewPerfMonitor($conn);
+			$perf = NewPerfMonitor($conn);
 			if ($perf) {
 				if ($perf->CreateLogTable()) $ok = $conn->Execute($isql,$arr);
@@ -228,5 +230,5 @@
 	
     // Sets the tablename to be used            
-    function table($newtable = false)
+    static function table($newtable = false)
     {
         static $_table;
@@ -255,5 +257,5 @@
 */
 		// Algorithm is taken from
-		// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__obtaining_raw_performance_data.asp
+		// http://social.technet.microsoft.com/Forums/en-US/winservergen/thread/414b0e1b-499c-411e-8a02-6a12e339c0f1/
 		if (strncmp(PHP_OS,'WIN',3)==0) {
 			if (PHP_VERSION == '5.0.0') return false;
@@ -263,12 +265,31 @@
 			if (PHP_VERSION == '4.3.10') return false; # see http://bugs.php.net/bug.php?id=31737
 			
-			@$c = new COM("WinMgmts:{impersonationLevel=impersonate}!Win32_PerfRawData_PerfOS_Processor.Name='_Total'");
-			if (!$c) return false;
-			
-			$info[0] = $c->PercentProcessorTime;
-			$info[1] = 0;
-			$info[2] = 0;
-			$info[3] = $c->TimeStamp_Sys100NS;
-			//print_r($info);
+			static $FAIL = false;
+			if ($FAIL) return false;
+			
+			$objName = "winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\CIMV2";	
+			$myQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'";
+			
+			try {
+				@$objWMIService = new COM($objName);
+				if (!$objWMIService) {
+					$FAIL = true;
+					return false;
+				}
+		
+				$info[0] = -1;
+				$info[1] = 0;
+				$info[2] = 0;
+				$info[3] = 0;
+				foreach($objWMIService->ExecQuery($myQuery) as $objItem)  {
+						$info[0] = $objItem->PercentProcessorTime();
+				}
+			
+			} catch(Exception $e) {
+				$FAIL = true;
+				echo $e->getMessage();
+				return false;
+			}
+			
 			return $info;
 		}
@@ -333,25 +354,24 @@
 		$info = $this->_CPULoad();
 		if (!$info) return false;
-			
-		if (empty($this->_lastLoad)) {
-			sleep(1);
+		
+		if (strncmp(PHP_OS,'WIN',3)==0) {
+			return (integer) $info[0];
+		}else {
+			if (empty($this->_lastLoad)) {
+				sleep(1);
+				$this->_lastLoad = $info;
+				$info = $this->_CPULoad();
+			}
+			
+			$last = $this->_lastLoad;
 			$this->_lastLoad = $info;
-			$info = $this->_CPULoad();
-		}
-		
-		$last = $this->_lastLoad;
-		$this->_lastLoad = $info;
-		
-		$d_user = $info[0] - $last[0];
-		$d_nice = $info[1] - $last[1];
-		$d_system = $info[2] - $last[2];
-		$d_idle = $info[3] - $last[3];
-		
-		//printf("Delta - User: %f  Nice: %f  System: %f  Idle: %f<br>",$d_user,$d_nice,$d_system,$d_idle);
-
-		if (strncmp(PHP_OS,'WIN',3)==0) {
-			if ($d_idle < 1) $d_idle = 1;
-			return 100*(1-$d_user/$d_idle);
-		}else {
+			
+			$d_user = $info[0] - $last[0];
+			$d_nice = $info[1] - $last[1];
+			$d_system = $info[2] - $last[2];
+			$d_idle = $info[3] - $last[3];
+			
+			//printf("Delta - User: %f  Nice: %f  System: %f  Idle: %f<br>",$d_user,$d_nice,$d_system,$d_idle);
+		
 			$total=$d_user+$d_nice+$d_system+$d_idle;
 			if ($total<1) $total=1;
@@ -409,5 +429,5 @@
 		$this->conn->fnExecute = false;
         $perf_table = adodb_perf::table();
-		$rs =& $this->conn->SelectLimit("select distinct count(*),sql1,tracer as error_msg from $perf_table where tracer like 'ERROR:%' group by sql1,tracer order by 1 desc",$numsql);//,$numsql);
+		$rs = $this->conn->SelectLimit("select distinct count(*),sql1,tracer as error_msg from $perf_table where tracer like 'ERROR:%' group by sql1,tracer order by 1 desc",$numsql);//,$numsql);
 		$this->conn->fnExecute = $saveE;
 		if ($rs) {
@@ -443,5 +463,5 @@
 			if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
 			//$this->conn->debug=1;
-			$rs =& $this->conn->SelectLimit(
+			$rs = $this->conn->SelectLimit(
 			"select avg(timer) as avg_timer,$sql1,count(*),max(timer) as max_timer,min(timer) as min_timer
 				from $perf_table
@@ -522,5 +542,5 @@
 			if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
 			
-			$rs =& $this->conn->SelectLimit(
+			$rs = $this->conn->SelectLimit(
 			"select sum(timer) as total,$sql1,count(*),max(timer) as max_timer,min(timer) as min_timer
 				from $perf_table
@@ -571,5 +591,5 @@
 		Raw function returning array of poll paramters
 	*/
-	function &PollParameters()
+	function PollParameters()
 	{
 		$arr[0] = (float)$this->DBParameter('data cache hit ratio');
@@ -641,4 +661,9 @@
 	}
 	
+	function clearsql()
+	{
+		$perf_table = adodb_perf::table();
+		$this->conn->Execute("delete from $perf_table where created<".$this->conn->sysTimeStamp);
+	}
 	/***********************************************************************************************/
 	//                                    HIGH LEVEL UI FUNCTIONS
@@ -648,4 +673,5 @@
 	function UI($pollsecs=5)
 	{
+	global $ADODB_LOG_CONN;
 	
     $perf_table = adodb_perf::table();
@@ -660,5 +686,5 @@
 	$info = $conn->ServerInfo();
 	if (isset($_GET['clearsql'])) {
-		$this->conn->Execute("delete from $perf_table");
+		$this->clearsql();
 	}
 	$this->conn->LogSQL($savelog);
@@ -689,4 +715,6 @@
 	
 	$allowsql = !defined('ADODB_PERF_NO_RUN_SQL');
+	global $ADODB_PERF_MIN;
+	$app .= " (Min sql timing \$ADODB_PERF_MIN=$ADODB_PERF_MIN secs)";
 	
 	if  (empty($_GET['hidem']))
@@ -703,11 +731,14 @@
 		default:
 		case 'stats':
+			if (empty($ADODB_LOG_CONN))
+				echo "<p>&nbsp; <a href=\"?do=viewsql&clearsql=1\">Clear SQL Log</a><br>";
 			echo $this->HealthCheck();
 			//$this->conn->debug=1;
-			echo $this->CheckMemory();
+			echo $this->CheckMemory();		
 			break;
 		case 'poll':
+			$self = htmlspecialchars($_SERVER['PHP_SELF']);
 			echo "<iframe width=720 height=80% 
-				src=\"{$_SERVER['PHP_SELF']}?do=poll2&hidem=1\"></iframe>";
+				src=\"{$self}?do=poll2&hidem=1\"></iframe>";
 			break;
 		case 'poll2':
@@ -744,5 +775,5 @@
 		if ($secs <= 1) $secs = 1;
 		echo "Accumulating statistics, every $secs seconds...\n";flush();
-		$arro =& $this->PollParameters();
+		$arro = $this->PollParameters();
 		$cnt = 0;
 		set_time_limit(0);
@@ -750,5 +781,5 @@
 		while (1) {
 
-			$arr =& $this->PollParameters();
+			$arr = $this->PollParameters();
 			
 			$hits   = sprintf('%2.2f',$arr[0]);
@@ -886,5 +917,5 @@
 	
 		
-		$PHP_SELF = $_SERVER['PHP_SELF'];
+		$PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']);
 		$sql = isset($_REQUEST['sql']) ? $_REQUEST['sql'] : '';
 
