CHips L MINI SHELL

CHips L pro

Current Path : /opt/cpanel/ea-php54/root/usr/share/pear/RVSeagullMod/lib/RVSGL/SitebuilderDao/
Upload File :
Current File : //opt/cpanel/ea-php54/root/usr/share/pear/RVSeagullMod/lib/RVSGL/SitebuilderDao/DbWrapper.php

<?php
require_once 'DB/DataObject.php';
require_once 'SitebuilderInf.php';

class DbWrapper extends SGL_Manager
{
	var $aConf;
    public function DbWrapper() { 
        parent::SGL_Manager(); 
        $this->aConf = $this->conf;
    }
    
    public static function singleton($autoload=false)
    {
        static $instance;
        // If the instance is not there, create one
        if (!isset($instance) || $autoload) {
            $class = __CLASS__;
            $instance = new $class();
        }
        return $instance;
    }
    
    public function registRvsConf($conf = null, $dbh = null)
    {
        if ( $conf != null) {
            $this->aConf = $conf;
        }
        if ( $dbh != null) {
            $this->objDbh = $dbh;
        }
    }
    
    public static function fetchDatas($aFields = array(), $oData = array(), $fetchType='getassoc')
    {
		$aResult = array();
		$aRow = array();

    	switch (RvsLibs_String::strtolower($fetchType)) {
    	    case 'getdataobject':
    	        foreach ($oData as $k => $v) {
    	            $oData[$k]->_resultFields = $aFields;
    	        }
    	        return $oData;
    	        break;
    	    ///dbh->getassco()
    		case 'getassoc':
    		default:

    		    if (is_array($aFields)) {
                    $aKFields = array_keys($aFields);
                    foreach ($oData as $k => $v) {
                        if (count($aKFields) == 2 ) {
                            $oRow = '';
                        } else {
                            $oRow = new stdClass();
                        }
                        foreach ($aFields as $fk => $fv) {
                            if (is_object($oRow)) {
                                $oRow->$fk = $v->$fk;
                            } else {
                                $oRow = $v->$fk;
                            }
                        }
                        $aResult[$v->$aKFields[0]] = $oRow;
                    }
    		    }
    			return $aResult;
    		break;
    		///dbh->getAll()
    		case 'getall':
    		    foreach ($oData as $k => $v) {
                    $oRow = new stdClass();
                    foreach ($aFields as $fk => $fv) {
                        $oRow->$fk = $v->$fk;
                    }
                    $aResult[] = $oRow;
                }
                return $aResult;
    		    break;
    		///dbh->getAll() DB_FETCHMODE_ASSOC
    		case 'getarray':
    			$aResult = array();
    		    foreach ($oData as $k => $v) {
                    $aRow = array();
                    foreach ($aFields as $fk => $fv) {
                        $aRow["{$fk}"] = $v->$fk;
                    }
                    if (count($aRow)) $aResult[] = $aRow;
                }
                return $aResult;
    		    break;
    		///dbh->getRow() DB_FETCHMODE_ASSOC
    		case 'getrow':
    			$aResult = null;
                foreach ($oData as $k => $v) {
    		    	$aResult = array();
                    foreach ($aFields as $fk => $fv) {
                        $aResult[$fk] = $v->$fk;
                    }
                    break;
                }
                return $aResult;
            ///dbh->getassco()
            case 'getvalue':
                foreach ($oData as $k => $v) {
                    $aKFields = array_keys($aFields);
                    $aResult[$v->$aKFields[0]] = $v->$aKFields[1];
                }
                return $aResult;
            case 'getallvalue':
            	if (is_array($aFields)) {
            		$aKFields = array_keys($aFields);
            		foreach ($oData as $k => $v) {
            			$row = array();
            			foreach ($aFields as $fk => $fv) {
            				$aRow["{$fk}"] = $v->$fk;
            			}
            			$aResult[$v->$aKFields[0]] = $aRow;
            			/*
            			 $aKFields = array_keys($aFields);
            			 $aResult[$v->$aKFields[0]] = $v;*/
            		}
            	} 
            	return $aResult;
            ///dbh->getRow()
            case 'getrowobj':
                foreach ($oData as $k => $v) {
                    $oRow = new stdClass();
                    foreach ($aFields as $fk => $fv) {
                        $oRow->$fk = $v->$fk;
                    }
                    break;
                }
                return (isset($oRow)) ? $oRow : null;
                break;
            ///dbh->getOne()
            case 'getone':
                if (is_array($aFields)) {
                    $aKFields = array_keys($aFields);
                    return (isset($oData[0]->$aKFields[0])) ? $oData[0]->$aKFields[0] : null;
                } else {
                    return null;
                }
                break;
    	}
    }
    
    public static function rDebug($aResDef=array(), $aResTest=array(), $debugMode = false)
    {
        if ($debugMode) {
            echo '<pre>';
            if (is_array($aResDef) || is_object($aResDef)) {
                print_r($aResDef); 
            } else {
                echo $aResDef . "\n";
            }
            echo '<hr>';
            if (is_array($aResTest) || is_object($aResTest)) {
                print_r($aResTest);
            } else {
                echo $aResTest . "\n";
            }
            echo '</pre>';
        }
    }
    
    public static function buildResultFields($oDbObj, $aResultFields=array())
    {
        $_aResultFields = array();
        if (count($aResultFields)) { 
            $index = 0;
            foreach ($aResultFields as $v) {
                $_aResultFields[$v] = $index;
                $index++;
            }
        } else {
             $_aResultFields = $oDbObj->_resultFields;
        }    
        return $_aResultFields;
    }
    
    public static function getLinkId($aProjectId , $aUsrId)
    {
        SGL::logMessage(null, PEAR_LOG_DEBUG);
        require_once SGL_MOD_DIR . '/sitebuilder/classes/Common.php';
        $aLinkId = array();
        if (!is_array($aProjectId)) {
            $aProjectId = array('0' => $aProjectId);
        }
        if (!is_array($aUsrId)) {
            $aUsrId = array('0' => $aUsrId);
        }
        foreach ($aProjectId as $key=>$projectId) {
            foreach ($aUsrId as $key=>$usrId) {
                $aPageData[] = RvsLibs_Project::getProjectPageData($projectId , $usrId);                
                $aPageData[] = RvsLibs_Project::getInternalPageData($projectId , $usrId);
            }            
        }
        foreach ($aPageData as $key=>$value) {
            foreach ($value as $subkey=>$subvalue) {
                if ($subvalue->rvs_link_id != '' && $subvalue->rvs_link_id != '0') {
                    $aLinkId[] = $subvalue->rvs_link_id;    
                }
            }
        }
        return $aLinkId;        
    }
    
    public static function resetSqlFile($projectId, $sqlFileName)
    {
        // Reset Sql File
        $fileSqlBackup = RVS_USER_HOME . '/.rvsitebuilder/projects/' . $projectId . '/'. $sqlFileName;
        if (is_file($fileSqlBackup)) {
            unlink($fileSqlBackup);
        }              
        return $fileSqlBackup;
    }   

    public static function writeSqlFile($aContents = array(), $fileSqlBackup)
    {    
        //$handle = fopen($fileSqlBackup, "a" );
        ///The data to write. Can be either a string, an array or a stream resource (explained above).
        //file_put_contents($fileSqlBackup, $aContents);
        //fclose($handle);
        
        $contents = $aContents;
        if (is_array($aContents) == true) {
        	$contents = join('', $aContents);
        }
        $handle = RvsLibs_File::fopen($fileSqlBackup, 'a');
        if (SGL::isError($handle) === true) {
        	return $handle;
        }
        RvsLibs_File::fwrite($handle, $contents);
        RvsLibs_File::fclose($handle);

        if (!is_file($fileSqlBackup)) {
            SGL::logMessage("Cannot open " . $fileSqlBackup, PEAR_LOG_DEBUG);      
        }
    }

    public static function buildSQLFormat($aData = array())
    {
        $field ='';
        $value = '';
        if (isset($aData->_resultFields) === false || isset($aData->__table) === false) {
        	return '';
        }
        $aResultField = $aData->_resultFields;
        $dbh = SGL_DB::singleton(null,$reConnect = true);
        $aTableInfo = $dbh->tableInfo(trim($aData->__table));
        $aFieldInfo = RvsLibs_Util::getFieldInfo($aTableInfo);
        foreach ($aResultField as $k => $v) {
        	if ($k != '') {
	            $field .= sprintf('%s', ($field != '') ? ", `{$k}`" : "`{$k}`");
	            $value .= sprintf('%s', ($value != '') ? ', ' : '');
	            if (is_null($aData->$k)) {
	            	$realValue = RvsLibs_Util::getDefaultFieldValue($aFieldInfo[$k]);
	            	$realValue = ($realValue == 'NULL') ? 'NULL' : RvsLibs_String::quoteSmart($realValue);
	            } else {
	            	$realValue = RvsLibs_String::quoteSmart($aData->$k);
	            }
	            $value .= sprintf('%s', $realValue);
        	}
        }
        if ($field != '') {
        $line = sprintf('REPLACE INTO `%s` (%s) VALUES (%s);', $aData->__table, $field, $value);
        $line .= "\n";
        }
        return $line;
    }
    
    public static function find(&$dataObject, $query=null, &$objDbh) {
    	if (is_null($query) === true) {
    		$result = $dataObject->find();
    	} else {    		
    		$result = $dataObject->find($query);
    	}
    	if (SGL::isError($result) === true && $result->code == -14) {
    		//Error: 2006  (CR_SERVER_GONE_ERROR)
    		//Message: MySQL server has gone away
    		SGL_Error::pop();
    		//TODO :: หาวิธี reconnect DB ใหม่
    		$dbh = SGL_DB::singleton(null,$reConnect = true);
    		if (is_null($query) === true) {
    			$result = $dataObject->find();
    		} else {
    			$result = $dataObject->find($query);
    		}
    	}
    }

    public static function query(&$dataObject, $query=null, &$objDbh) {
    	if (is_null($query) === true) {
    		$result = $dataObject->query();
    	} else {
    		$result = $dataObject->query($query);
    	}

    	self::registerQuery($dataObject,$sql);
    	
    	if (SGL::isError($result) === true && $result->code == -14) {
    		//Error: 2006  (CR_SERVER_GONE_ERROR)
    		//Message: MySQL server has gone away
    		SGL_Error::pop();
    		//TODO :: หาวิธี reconnect DB ใหม่
    		$dbh = SGL_DB::singleton(null,$reConnect = true);
    		$dataObject->query($query);
    	}
    }
    
    public static function registerQuery(&$dataObject,$sql)
    {
    	$sql = preg_replace('/[\r\n]\s*/si', ' ', $sql);
    	// start
    	//$start_time = array_sum(explode(' ',microtime()));
    	$time = @gettimeofday();
        $start_time = ($time['usec'] / 1000);
    	// end
    	if (!$dataObject->options['result_buffering']) {
    		$result = @mysql_unbuffered_query($sql, $dataObject->connection);
    	} else {
    		$result = @mysql_query($sql, $dataObject);
    	}
    	$bt = debug_backtrace();
    	if (isset($bt[1]['class']) && $bt[1]['type'] && isset($bt[1]['function'])) {
    		$callInfo = $bt[1]['class'] . $bt[1]['type'] . $bt[1]['function'] . ': ';
    	}
    	// start
    	//$query_took = array_sum(explode(' ',microtime())) - $start_time;
    	$time = @gettimeofday();
    	$query_took = (($time['usec'] / 1000) - $start_time)/100;
    	@$GLOBALS['global_query_counter']++;
    	//$k = $GLOBALS['global_query_counter'];
    	@$GLOBALS['all_the_queries'][$GLOBALS['global_query_counter'] . ' - ' . $sql] = $query_took . ' ===> ';
    }
    
    
}
?>

Copyright 2K16 - 2K18 Indonesian Hacker Rulez