masConnectionData[$sName])) return $this->masConnectionData[$sName]; } return false; } function CDatabase($sDBAccessKey,$lCount=0) { $this->oDT=new CDateTime(); $this->gbDBConnected=false; $this->mlResults=0; $this->msAccessKey=$sDBAccessKey; $this->msPath=PATHENV; if($asData=$this->gasGetConnectionData($sDBAccessKey)) { $this->masConnectionData=$asData; if ($this->mpDBLink=mysql_pconnect($asData["host"],$asData["user"],$asData["pass"])) { if(@mysql_select_db($asData["dbname"])) { $this->msDatabase=$asData["dbname"]; $this->gbDBConnected=true; } else { echo "NODB"; echo mysql_error(); } } else { $lCount++; if($lCount<5) { $this->CDatabase($sDBAccessKey,$lCount); } else { echo "Tried to Connect ".$lCount." times."; } } } } function goGetRecord($sTable,$sWhere=false,$sField="*") { if($sWhere===false) $sQuery=$sTable; else $sQuery="SELECT ".$sField." FROM ".$sTable." WHERE ".$sWhere; if($oResult=$this->goQuery($sQuery)) { if($oRec=$oResult->goGetRecord()) { return $oRec; } } } function gasGetRecord($sTable,$sWhere=false,$sField="*") { if($sWhere===false) $sQuery=$sTable; else $sQuery="SELECT ".$sField." FROM ".$sTable." WHERE ".$sWhere; if($oResult=$this->goQuery($sQuery)) { if($as=$oResult->gasGetRecord()) { return $as; } } } function glGetSum($sTable,$sField,$sWhere) { $sQuery="SELECT SUM(".$sField.") as SUMME FROM ".$sTable." WHERE ".$sWhere; if($oResult=$this->goQuery($sQuery)) { $oRec=$oResult->goGetRecord(); return $oRec->SUMME; } return false; } function goDelete($sTable,$sWhere) { if($sWhere=="") return false; $sQuery="DELETE FROM $sTable WHERE ".$sWhere; return $this->goQuery($sQuery); } function goGetTableInfo($sTable) { $sQuery="SHOW FIELDS FROM ".$sTable; if($oResult=$this->goQuery($sQuery)) { return new CDBTableInfo($sTable, $oResult); } else return false; } function gbIsConnected() { return $this->gbDBConnected; } function gbClose() { mysql_close($this->mpDBLink); } function gasGetConnectionData($sKey) { $sFileName=$this->msPath.$sKey.".env"; if(file_exists($sFileName)) { if($fp=fopen($sFileName,"r")) { if($sXML=@fread($fp,filesize($sFileName))) { fclose($fp); if($asData=unserialize($sXML)) { return $asData; } } } } return false; } function goInsertObject($sTable,$oData,$bEscape=false) { $asData=get_object_vars($oData); return $this->goInsertArray($sTable,$asData,$bEscape=false); } function goUpdate($sTable,$sData,$sClause) { $sClause=$sClause!=""?" WHERE ".$sClause:""; $sQuery="UPDATE ".$sTable." SET ".$sData.$sClause; if ($lRequestID=$this->mlQuery($sQuery)) { return $this->maoRequests[$lRequestID]; } return false; } function goUpdateArray($sTable,$asData,$sClause,$bEscape=false) { $asSetList=array(); if(is_array($asData)){ foreach($asData as $sFieldName=>$sValue){ if($bEscape===true) $sValue=$this->gsString2SQL($sValue); array_push($asSetList,"$sFieldName=".$sValue); } $sSet=join(",",$asSetList); return $this->goUpdate($sTable,$sSet,$sClause); } return false; } function goQuery($sQuery) { if ($lRequestID=$this->mlQuery($sQuery)) { return $this->maoRequests[$lRequestID]; } return false; } function goInsertArray($sTable,$asData,$bEscape=false) { if (is_array($asData)) { $asFields=array(); $asValues=array(); foreach ($asData as $sFieldName=>$sValue) { array_push($asFields,$sFieldName); if($bEscape===true) $sValue=$this->gsString2SQL($sValue); array_push($asValues,$sValue); } $sFieldlist=join(",\n",$asFields); $sValues=join(",\n",$asValues); return $this->goInsert($sTable,$sFieldlist,$sValues); } } function goInsert($sTable,$sFieldList,$sValues) { $sFieldList=$sFieldList==""?"":" (".$sFieldList.") "; $sQuery="INSERT INTO ".$sTable.$sFieldList." VALUES (".$sValues.")"; if ($lRequestID=$this->mlQuery($sQuery)) { return $this->maoRequests[$lRequestID]; } return false; } function goGetResult($lRequestID) { if($lRequestID!=false) { if(isset($this->maoRequests[$lRequestID])) { return $this->maoRequests[$lRequestID]; } } return false; } function mlQuery($sQuery) { global $oSystem; $this->mlResults++; $this->msQuery=$sQuery; mysql_select_db($this->msDatabase); $sStart=microtime(); if($pResult=@mysql_query($sQuery,$this->mpDBLink)) { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblDurance+=$dblDurance; // incrementing mlResults (initialised with 0) before prevents from confusing with false=0! $this->maoRequests[$this->mlResults]=&new CDBResult($this->mlResults,$pResult,$this->mpDBLink); return $this->mlResults; } else { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblDurance+=$dblDurance; $this->maoRequests[$this->mlResults]=false; $this->maoErrors[$this->mlResults]=new CDBError(mysql_error(),mysql_errno()); $this->mlLastResult=$this->mlResults; return false; } } function goGetError($mlResult=false) { $mlResult=$mlResult===false?$this->mlLastResult:$mlResult; if(isset($this->maoErrors[$mlResult])) { $this->maoErrors[$mlResult]; } return false; } function glGetNumberOfRecords($sTable,$sClause="1") { $sQuery="SELECT COUNT(*) AS NOR FROM ".$sTable." WHERE ".$sClause; if($oResult=$this->goQuery($sQuery)) { if($oResult->glGetNumberOfRecords()==1) { $asData=$oResult->gasGetRecord(); return $asData["NOR"]; } } return false; } function gbIsNull($sTable,$sField,$sClause) { $sQuery="SELECT IFNULL(".$sField.",'___ISNULL___') as ISNULLVALUE FROM ".$sTable." WHERE ".$sClause; if($oResult=$this->goGetResult($this->mlQuery($sQuery))) { if($oResult->mlCountRows==1) { $as=$oResult->gasGetRecord(); return $as["ISNULLVALUE"]=="___ISNULL___"; } } return false; } function glGetMax($sTable,$sField,$sWhere) { $sQuery="SELECT IF(IFNULL(".$sField.",-1)=-1,'__false__',MAX(".$sField.")) as MaxVal FROM ".$sTable." WHERE ".$sWhere; if($oResult=$this->goQuery($sQuery)) { $oErg=$oResult->goGetRecord(); $Return=$oErg->MaxVal==="__false__"?false:$oErg->MaxVal; return $Return; } return false; } function goSetValue($sTable,$sField,$lPK,$sValue) { if($oTableInfo=$this->goGetTableInfo($sTable)) { if($oTableInfo->PrimaryKey!="") { $sQuery="UPDATE ".$sTable." SET ".$sField."=".$sValue." WHERE ".$oTableInfo->PrimaryKey."=".$lPK; return $this->goQuery($sQuery); } } return false; } function goSetNULL($sTable,$sField,$lPK) { if($oTableInfo=$this->goGetTableInfo($sTable)) { if($oTableInfo->PrimaryKey!="") { if($oTableInfo->gbCanBeNULL($sField)) { $sQuery="UPDATE ".$sTable." SET ".$sField."=NULL WHERE ".$oTableInfo->PrimaryKey."=".$lPK; return $this->goQuery($sQuery); } } } return false; } function glGetMin($sTable,$sField,$sWhere) { $sQuery="SELECT IF(IFNULL(".$sField.",-1)=-1,'__false__',MIN(".$sField.")) as MinVal FROM ".$sTable." WHERE ".$sWhere; if($oResult=$this->goQuery($sQuery)) { $oErg=$oResult->goGetRecord(); $Return=$oErg->MinVal==="__false__"?false:$oErg->MinVal; return $Return; } return false; } function gsGetFieldValue($sTable,$sField,$sClause="1",$sWhat="*") { $sQuery="SELECT ".$sWhat." FROM ".$sTable." WHERE ".$sClause; if($oResult=$this->goGetResult($this->mlQuery($sQuery))) { if($oResult->mlCountRows==1) { $as=$oResult->gasGetRecord(); return $as[$sField]; } } return false; } function gdblGetDouble($sZahl) { return (double)str_replace(",",".",$sZahl); } function gsEscapeString($sString,$bAddApostrophe=true,$nullWhenEmpty=false) { if($sString==""&& $nullWhenEmpty===true) { return "NULL"; } $sString=mysql_real_escape_string($sString); if($bAddApostrophe===true) return "'".$sString."'"; else return $sString; } function gsString2SQL2($sValue,$bAddApostrophe=true,$bCanBeNull=false) { $sString=""; if($sValue==""&&$bCanBeNull) $sString="NULL"; else { //$sString=str_replace("\\", "\\\\", $sValue); //$sString=str_replace("'","\\'",$sString); $sString=($sValue); } if($bAddApostrophe===false) return $sString; else return "'".addslashes($sString)."'"; } function gsString2SQL($sValue,$bAddApostrophe=true,$bCanBeNull=false) { $sString=""; if($sValue==""&&$bCanBeNull) $sString="NULL"; else { //$sString=str_replace("\\", "\\\\", $sValue); //$sString=str_replace("'","\\'",$sString); $sString=($sValue); } if($bAddApostrophe===false) return $sString; else return "'".$sString."'"; } function gbDeleteResult($lIndex) { unset($this->maoRequests[$lIndex]); } function goGetView($sView,$asCondition,$msDBAccessKey) { $oDBViews=new CDBViews($this); return $oDBViews->goGetView($sView,$asCondition,$msDBAccessKey); } function goGetResultView($sName,$asData=array(),$sKey="__db__") { $oDBViews=new CDBViews($this); return $oDBViews->goGetResult($sName,$asData,$sKey); } } class CDBResult { function CDBResult($lIndex,$pResult,$pLink) { $this->mlIndex=$lIndex; $this->mpResult=$pResult; $this->mlRowsAffected=@mysql_affected_rows($pLink); $this->mlCountRows=@mysql_num_rows($pResult); $this->mlFields=@mysql_num_fields($pResult); $this->mlInsertID=@mysql_insert_id($pLink); } function glGetAffectedRows() { return $this->mlRowsAffected; } function glGetNumberOfRecords() { return $this->mlCountRows; } function goGetFieldInfo($lOffset) { if($lOffset<$this->mlFields) return mysql_fetch_field($this->mpResult,$lOffset); return false; } function goGetRecord($bFree=false) { $p=@mysql_fetch_object($this->mpResult); if($p!==false) return $p; if($bFree===true) @$this->mbFreeResult(); return false; } function mbFreeResult() { @mysql_free_result($this->mpResult); $this->mpResult=false; unset($this); } function gasGetRecord($bFree=true) { $p=@mysql_fetch_array($this->mpResult); if($p!==false) return $p; if($bFree===true) $this->mbFreeResult(); return false; } function gaaGetAllRecords($bReturnFalse=false,$bFree=true) { $asReturn=array(); // passing bFree to gasGetRecord frees the resultset while($asPush=$this->gasGetRecord($bFree)) { array_push($asReturn,$this->gasAssociativeOnly($asPush)); } if($bReturnFalse===true && sizeof($asReturn)==0) return false; return $asReturn; } function glGetInsertID() { return $this->mlInsertID; } function gasAssociativeOnly($asData) { // $asReturn=array(); if(is_array($asData)) { foreach($asData as $sKey=>$sValue) if(!is_int($sKey)) $asReturn[$sKey]=$sValue; } return $asReturn; } function gaoGetAllRecords($bReturnFalse=false,$bFree=false) { $aoReturn=array(); // passing bFree to gasGetRecord frees the resultset while($oPush=$this->goGetRecord(false)) { array_push($aoReturn,$oPush); } if($bReturnFalse===true && sizeof($asReturn)==0) return false; if($bFree===true) @$this->mbFreeResult(); return $aoReturn; } } class CDBViews { function CDBViews($oDB ) { $this->oDB=&$oDB; } function goGetResult($sName,$asData=array(),$sKey) { global $oSystem; $this->oSystem=&$oSystem; $oDataSource=$this->oDB; if($sKey!=="__db__") { $oDataSource=new CDatabase($sKey); } if($oResult=$this->oDB->goQuery("SELECT * FROM tdDBViews WHERE ViewName='".$sName."'")) { if($oResult->glGetNumberOfRecords()>0) { $oView=$oResult->goGetRecord(); if($oView->ViewSource!="__db__") { if(!class_exists("CRemoteDatabase")) require_once(PATHFW."class.db.remote.php"); $oDB=new CRemoteDatabase($oView->ViewSource); } $asUData=$this->oSystem->oUser->gasGetAccessData(); if($asData["ClientPK"]=="") $asData["ClientPK"]=$asUData["ClientPK"]; if($asData["UserPK"]=="") $asData["UserPK"]=$asUData["UserPK"]; foreach($asData as $sKey=>$sValue) { $oView->Query=str_replace("{".$sKey."}",$sValue,$oView->Query); } if($oRetResult=$oDataSource->goQuery($oView->Query)) { return $oRetResult; } } } return false; } function getFile($sFile) { if($fp=fopen($sFile,"r")) { $sString=fread($fp,filesize($sFile)); fclose($fp); return $sString; } return false; } function goGetView($sName,$asData=array(),$sDBAccessKey=false) { global $oSystem; $this->oSystem=&$oSystem; if($oResult=$this->oDB->goQuery("SELECT *,ViewSource,Query,IFNULL(CountQuery,'__false__') as CountQuery ,IFNULL(ShowPerPage,'__false__') as ShowPerPage FROM tdDBViews WHERE ViewName='".$sName."'")) { if($oResult->glGetNumberOfRecords()>0) { $oView=$oResult->goGetRecord(); // check for newer version! $sQueryF=PATHQS."views/queries/view.".strtolower($sName).".sql"; $sCountF=PATHQS."views/queries/view.".strtolower($sName).".count.sql"; $sPerPageF=PATHQS."views/queries/view.".strtolower($sName).".perpage.sql"; if(file_exists($sQueryF)) { if(isset($oView->FILETIME)||$oView->FILETIME==NULL) { $dayOfQuery=CDateTime::glGetTimeStamp($oView->FILETIME); $lLastUpdate=fileatime($sQueryF); if($lLastUpdate>$dayOfQuery) { $oView->Query=$this->getFile($sQueryF); $oView->CountQuery=$this->getFile($sCountF); $asUpdateView["Query"]=$this->oDB->gsEscapeString($oView->Query); $asUpdateView["CountQuery"]=$this->oDB->gsEscapeString($oView->CountQuery); if($lNum=$this->getFile($sPerPageF)) { $oView->ShowPerPage=$lNum; $asUpdateView["ShowPerPage"]=$lNum; } $asUpdateView["FILETIME"]="from_unixtime(".$lLastUpdate.")"; $this->oDB->goUpdateArray("tdDBViews",$asUpdateView,"ViewName='".$sName."'"); echo mysql_error(); } } } $oDB=&$this->oDB; if($oView->ViewSource!="__db__") { if(!class_exists("CRemoteDatabase")) require_once(PATHFW."class.db.remote.php"); $oDB=new CRemoteDatabase($oView->ViewSource); } elseif($sDBAccessKey!==false) { $oDB=new CDatabase($sDBAccessKey); } if($oView->ShowPerPage!="__false__") $asData["Count"]=$oView->ShowPerPage; $asUserData=$this->oSystem->oUser->gasGetAccessData(); $asData["UserPK"]=$asData["UserPK"]==""?$asUserData["UserPK"]:$asData["UserPK"]; $asData["ClientPK"]=$asData["ClientPK"]==""?$asUserData["ClientPK"]:$asData["ClientPK"]; foreach($asData as $sKey=>$sValue) { $oView->Query=str_replace("{".$sKey."}",$sValue,$oView->Query); $oView->CountQuery=str_replace("{".$sKey."}",$sValue,$oView->CountQuery); } //echo "
";
					//echo $oView->Query;
					if($oRetResult=$oDB->goQuery($oView->Query))
						{

						if($oView->CountQuery!="__false__")
						{

							if($oCountResult=$oDB->goQuery($oView->CountQuery))
							{
								$oCount=$oCountResult->goGetRecord();
							}
							else
							{
								echo "GEMEINER FEHLER BEIM Z�HLEN
".$oDB->msQuery; echo "
"; echo mysql_error(); echo "
"; } } return new CDBViewReturn($oRetResult,$oCount->NOR,$asData["Count"]); } else { } } } echo mysql_error(); echo $oDB->msQuery; return false; } } class CDBViewReturn { var $moResult; var $mlGesamt=false; var $mlShowPerPage; function CDBViewReturn($oResult,$lGesamt,$lShowPerPage) { $this->mlGesamt=$lGesamt; $this->moResult=$oResult; $this->mlShowPerPage=$lShowPerPage; } } class CDBTableInfo { var $PrimaryKey; var $maoFields; var $Table; function CDBTableInfo($sTable,$oResult) { $this->Table=$sTable; $aoData=$oResult->gaoGetAllRecords(); foreach($aoData as $oFieldInfo) { if(strtolower($oFieldInfo->Key)=="pri") { $this->PrimaryKey=$oFieldInfo->Field; } $this->maoFields[$oFieldInfo->Field]=($oFieldInfo); } } function goGetFieldInfo($sField) { if(isset($this->maoFields[$sField])) return $this->maoFields[$sField]; return false; } function gbCanBeNULL($sField) { if($oField=$this->goGetFieldInfo($sField)) { if(strtolower($oField->Null)=="yes") return true; return false; } return false; } function gbFieldExists($sField) { return isset($this->maoFields[$sField]); } function gbRecordDeletable() { return isset($this->maoFields["DeleteDate"])&&isset($this->maoFields["DeletedBy"]); } } class CDBError { function CDBError($sText,$lNo) { $this->gsMessage=$sText; $this->glNumber=$lNo; } }