oDT=new CDateTime(); $this->gbDBConnected=false; $this->mlResults=0; $this->msPath=PATHENVRDB; if($this->masConnectionData=$this->gasGetConnectionData($sDBAccessKey)) { $this->msRemoteDBKey=$this->masConnectionData["db.access.key"]; $this->gbDBConnected=true; $this->oDBViews=new CDBViews($this); } } 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 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=@wddx_deserialize($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(",",$asFields); $sValues=join(",",$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; $sStart=microtime(); // hier webservice aufrufen // daten preparieren $asData["SQLQUERY"]=$sQuery; $asData["DBKEY"]=$this->msRemoteDBKey; $asData["crc32"]=crc32($sQuery); $sPostData=base64_encode(gzcompress(wddx_serialize_value($asData))); $lCheckSum=crc32($sPostData); if($mpCurl=curl_init($this->masConnectionData["host"])) { curl_setopt($mpCurl,CURLOPT_RETURNTRANSFER,1); if($this->masConnectionData["user"]!="" && $this->masConnectionData!="") { curl_setopt($mpCurl,CURLOPT_USERPWD,$this->masConnectionData["user"].":".$this->masConnectionData["pass"]); } if($sPostData!="") { curl_setopt($mpCurl,CURLOPT_POSTFIELDS,$sPostData); } if($sReturnData=@curl_exec($mpCurl)) { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblRDBDurance+=$dblDurance; // daten entpacken und an cdbRemoteResult übergeben! if($wddxData=@gzuncompress(@base64_decode($sReturnData))) { $asReturn=wddx_deserialize($wddxData); if($asReturn["error"]===false) { if($asReturn["dbError"]===false) { $this->maoRequests[$this->mlResults]=&new CDBRemoteResult($this->mlResults,$asReturn["RESULTOBJECT"]); return $this->mlResults; $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblRDBDurance+=$dblDurance; } else { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblRDBDurance+=$dblDurance; $this->maoRequests[$this->mlResults]=false; $this->maoErrors[$this->mlResults]=$asReturn["dbError"]; $this->mlLastResult=$this->mlResults; @curl_close($mpCurl); return false; } } else { } } curl_close($mpCurl); } else { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblRDBDurance+=$dblDurance; $this->maoRequests[$this->mlResults]=false; $this->maoErrors[$this->mlResults]=new CDBRemoteError(curl_error($mpCurl),curl_errno($mpCurl),curl_getinfo($mpCurl)); $this->mlLastResult=$this->mlResults; @curl_close($mpCurl); return false; } } else { $dblDurance=$this->oDT->gsGetMicroDifference($sStart,microtime()); $oSystem->mdblRDBDurance+=$dblDurance; $this->maoRequests[$this->mlResults]=false; $this->maoErrors[$this->mlResults]=new CDBRemoteError(curl_error($mpCurl),curl_errno($mpCurl),curl_getinfo($mpCurl)); $this->mlLastResult=$this->mlResults; @curl_close($mpCurl); 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 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 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 goGetResultView($sName,$asData=array()) { return $this->oDBViews->goGetResult($sName,$asData); } } class CDBRemoteResult { function CDBRemoteResult($lIndex,$asData) { $this->mlIndex=$lIndex; $this->mpResult=false; $this->masData =$asData["SQLRESULTDATA"]; $this->mlRowsAffected =$asData["mlRowsAffected"]; $this->mlCountRows =$asData["mlCountRows"]; $this->mlFields =$asData["mlFields"]; $this->mlInsertID =$asData["mlInsertID"]; } function glGetAffectedRows() { return $this->mlRowsAffected; } function glGetNumberOfRecords() { return $this->mlCountRows; } function goGetFieldInfo($lOffset) { return false; if($lOffset<$this->mlFields) return mysql_fetch_field($this->mpResult,$lOffset); return false; } function goGetRecord($bFree=false) { $asData=@array_shift($this->masData); if($asData!==false) return new CDBRemoteArrayToObject($asData); return false; } function mbFreeResult() { unset($this); } function gasGetRecord($bFree=true) { $asData=@array_shift($this->masData); if($asData!==false) return $asData; return false; } function gaaGetAllRecords($bReturnFalse=false,$bFree=true) { return $this->masData; } 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 CDBRemoteError { function CDBRemoteError($sText,$lNo,$status) { $this->gsMessage=$sText; $this->glNumber=$lNo; $this->msStatus=$status; } } class CDBRemoteArrayToObject { function CDBRemoteArrayToObject($asData) { if(is_array($asData)) foreach($asData as $sKey=>$sValue) { $check=(integer)$sKey; if($check==0) { $this->$sKey=$sValue; } } } }