startLog(); $lg->setCallParameters(__FILE__, __LINE__); } class mysqldb { var $dbhost; var $db; var $dbuser; var $dbpassword; var $sql; var $numberrows; var $affectedRows; var $dbopenstatus; var $dbconnection; var $qry; var $result; var $lg; function __construct($host = false, $db = false, $user = false, $pass = false) { global $HOST, $DB, $WEBUSER, $WEBPASSWORD, $lg, $s; if($lg instanceof ALog) { $this->lg = &$lg; } else { die("Nincs beállítva a logger."); } if($host == false && $s instanceof MxPlugin) { $host = &$s; } $this->sortField = ""; if( $host instanceof MxConfig && isset($host->database["server"]) && isset($host->database["database"]) && isset($host->database["user"]) && isset($host->database["password"]) ) { $this->sethost($host->database["server"]); $this->setdb($host->database["database"]); $this->setdbuser($host->database["user"]); $this->setdbpassword($host->database["password"]); $this->setdbconnection(false); } elseif( $host !== false && $db !== false && $user !== false && $pass !== false ) { $this->sethost($host); $this->setdb($db); $this->setdbuser($user); $this->setdbpassword($pass); $this->setdbconnection(false); } elseif( isset($HOST) && isset($DB) && isset($WEBUSER) && isset($WEBPASSWORD) && isset($lg) && $HOST != "" && $DB != "" && $WEBUSER != "" && $WEBPASSWORD != "" ) { $this->sethost($HOST); $this->setdb($DB); $this->setdbuser($WEBUSER); $this->setdbpassword($WEBPASSWORD); $this->setdbconnection(false); } else { $lg->log("Nincsenek beállítva az adatbázis változók."); die("Adatbázis csatlakozási hiba."); } } function gethost() { return $this->dbhost; } function sethost($req_host) { $this->dbhost = $req_host; } function getdb() { return $this->db; } function setdb($req_db) { $this->db = $req_db; } function getdbuser() { return $this->dbuser; } function setdbuser($req_user) { $this->dbuser = $req_user; } function getdbpassword() { return $this->dbpassword; } function setdbpassword($req_password) { $this->dbpassword = $req_password; } function getsql() { return $this->sql; } function setsql($req_sql) { $this->sql = $req_sql; } function getnumberrows() { return $this->numberrows; } function getAffectedRows() { return $this->affectedRows = mysql_affected_rows($this->dbconnection); } function setnumberrows($req_numberresults) { $this->numberesults = $req_numberresults; } function setdbconnection($req_dbconnection) { $this->dbconnection = $req_dbconnection; } function getdbconnection() { return $this->dbconnection; } function opendbconnection() { $this->dbconnection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword); if ($this->dbconnection) { mysql_query("SET NAMES 'utf8'",$this->dbconnection); mysql_query("SET CHARACTER SET 'utf8'",$this->dbconnection); $this->db = mysql_select_db($this->db,$this->dbconnection); //$this->setdbconnection(true); } else { $this->setdbconnection(false); $this->lg->log($this->dbhost . $this->dbuser . $this->dbpassword, "adatbázis csatlakozási hiba!"); die("adatbázis csatlakozási hiba!"); return false; } return true; } function closedbconnection() { if ($this->dbconnection) { mysql_close($this->dbconnection); } } function escape($string) { if(!$this->dbconnection) $this->opendbconnection(); if(is_array($string)) return $this->escapeArray($string); return mysql_real_escape_string($string,$this->dbconnection); } function escapeArray(&$array) { if(!$this->dbconnection) $this->opendbconnection(); if(is_array($array)) { $ak = array_keys($array); foreach($ak as $k) { if(is_array($array[$k])) { $array[$k] = $this->escapeArray($array[$k]); } else { $array[$k] = mysql_real_escape_string($array[$k],$this->dbconnection); } } return $array; } else { return false; } } function selectquery($assoc = true) { if(!$this->dbconnection) $this->opendbconnection(); $this->result = false; $this->qry = mysql_query($this->sql,$this->dbconnection); if ($this->qry === false) return false; else { $this->result = array(); $this->numberrows = mysql_num_rows($this->qry); if ($this->numberrows >= 0) { if ($assoc){ for($x = 0; $x < $this->numberrows; $x++) { $this->result[$x] = mysql_fetch_array($this->qry, MYSQL_ASSOC); } } else { for($x = 0; $x < $this->numberrows; $x++) { $this->result[$x] = mysql_fetch_row($this->qry); } } } else { $this->lg->logError($this->getsql(),__FILE__,__LINE__); return false; } return true; } } function select($sql) { $this->setsql($sql); return $this->selectquery(); } function selectFirst($sql) { $this->setsql($sql); $res = $this->selectquery(); if ($res && $this->numberrows > 0) $this->result = $this->result[0]; return $res; } function selectFirstResult($sql) { $this->setsql($sql); $res = $this->selectquery(); if ($res && $this->numberrows > 0) return $this->result = $this->result[0]; else return false; } function selectOne($sql, $match = 1) { $this->setsql($sql); $res = $this->selectquery(); if ($this->numberrows == $match) $this->result = $this->result[$match-1]; else $this->result = false; return ($res && ($this->numberrows == $match)) ? $this->result : false; } function selectAll($sql) { if($this->select($sql)) return $this->result; else return false; } function selectOneField($sql) { $this->setsql($sql); $res = $this->selectquery(false); if($res && $this->numberrows > 0) return $this->result[0][0]; else return false; } function updatequery() { if(!$this->dbconnection) $this->opendbconnection(); $this->qry = mysql_query($this->sql,$this->dbconnection); if (!$this->qry) { $this->lg->logError("UPDATEQUERY failed: " . $this->getsql(),__FILE__,__LINE__); //die('SQL error'); return false; } else { return true; } } function insertquery() { return $this->updatequery(); } function insert($sql) { $this->setsql($sql); return $this->insertquery(); } /** * Sor beszúrása asszociatív tömbből, az id mezőt kihagyja * * @param mixed $table * @param mixed $data * @param mixed $idField * @param mixed $allowedFields */ function insertRow($table, $data, $idField = "id", $allowedFields = false) { if(!is_array($data)) return false; if($allowedFields === false) { if($this->select("DESCRIBE {$table}")) foreach($this->result as $r) { if($r["Field"] == $idField) continue; $allowedFields[] = $r["Field"]; } else { $this->lg->logError("insertRow failed: " . $this->getsql(),__FILE__,__LINE__); die('SQL error'); } } $this->sql = "INSERT INTO {$table} SET "; $s = ""; foreach($data as $k => $v) { if($k == $idField) continue; if($allowedFields && !in_array($k, $allowedFields)) continue; if(is_array($v)) continue; $this->sql .= $s . $this->escape($k) ." = '".$this->escape($v)."'"; $s = ", "; } return $this->updatequery(); } function getInsertID() { return mysql_insert_id($this->dbconnection); } function update($sql) { $this->setsql($sql); return $this->updatequery(); } /** * Frissít egy sort a megadott paraméterek alapján * $idFieldOrWhereCond: * - egy mező neve => WHERE id = $data[id] * - tomb => WHERE t1 = '$data[t1]' AND t2 = '$data[t2]' ... * - sztring: saját WHERE felétellel a WHERE kulcsszóval * * @param mixed $table tábla neve * @param mixed $data adatok * @param mixed $idFieldOrWhereCond * @param mixed $allowedFields */ function updateRow($table, $data, $idFieldOrWhereCond = "id", $allowedFields = false) { if(!is_array($data)) return false; if($allowedFields === false) { if($this->select("DESCRIBE {$table}")) foreach($this->result as $r) { if( is_array($idFieldOrWhereCond) ? in_array($r["Field"], $idFieldOrWhereCond) : $r["Field"] == $idFieldOrWhereCond ) continue; $allowedFields[] = $r["Field"]; } else { $this->lg->logError("insertRow failed: " . $this->getsql(),__FILE__,__LINE__); die('SQL error'); } } $this->sql = "UPDATE {$table} SET "; $s = ""; foreach($data as $k => $v) { if( ( is_array($idFieldOrWhereCond) ? in_array($k, $idFieldOrWhereCond) : ($k == $idFieldOrWhereCond) ) || ( is_array($allowedFields) && !in_array($k, $allowedFields) ) ) continue; $this->sql .= $s . $this->escape($k) ." = '".$this->escape($v)."'"; $s = ", "; } if(is_array($idFieldOrWhereCond)) { $where = " WHERE "; $s = ""; foreach($idFieldOrWhereCond as $idfld) { if(isset($data[$idfld])) { $where .= $s . " $idfld = '{$data[$idfld]}' "; $s = " AND "; } else { $this->sql .= $where; $this->lg->logError("Hiányzik ez a mező az update adatok közül: '{$idfld}'.\nQuery: " . $this->getsql(),__FILE__,__LINE__); return false; } } $this->sql .= $where; } elseif(stripos($idFieldOrWhereCond, "WHERE") !== false) { $this->sql .= $idFieldOrWhereCond; } else $this->sql .= " WHERE $idFieldOrWhereCond = '{$data[$idFieldOrWhereCond]}'"; return $this->updatequery(); } /** * A merge to arrayhoz fuzi az aktualis eremdenyt (rekordot), * * @param mixed $merge_to reference * @param mixed $idField ha meg van adva, akkor diszjunkt lesz az idra (az utolso idfield rekord lesz az eredmenyben) */ function result_merge(&$merge_to, $idField = false) { if(!$idField) return array_merge($merge_to, $this->result); //print count($merge_to) . "mergeto\n"; foreach($this->result as $r) { if(($g = $this->findField($idField, $r[$idField], $merge_to)) === false) { //print "csati"; $merge_to[] = $r; } else { //print "$g sht $idField " . $r[$idField] . " " . count($merge_to); } } return $merge_to; } /** * result id-val ter vissza * * @param mixed $fieldName oszlop * @param mixed $findValue keresendo * @param array $where reference ownResult ha kell */ function findField($fieldName, $findValue, &$where = false) { if($where === false) $where = &$this->result; if(count($where) == 0 || !isset($where[0][$fieldName])) return false; for($x=0;$xsortField == "") return 0; return strcmp($a[$this->sortField], $b[$this->sortField]); } function sortByGlobalDesc($a, $b) { if($this->sortField == "") return 0; return strcmp($b[$this->sortField], $a[$this->sortField]); } /** * Rendezi a result-ot a mezo alapjan * * @param mixed $field * @param mixed $asc true novekvo, false csokkeno * @param mixed $data ownResult ha kell, ezen (!) végzi a rendezést, +visszatér vele */ function sortByField($field, $asc = true, &$data = false) { $this->sortField = $field; if($data) { usort( $data, $asc ? array($this, "sortByGlobal") : array($this, "sortByGlobalDesc")); } else { usort( $this->result, $asc ? array($this, "sortByGlobal") : array($this, "sortByGlobalDesc")); } $this->sortField = ""; if($data) return $data; else return true; } /** * Resultból id => mező-érték párokat készít * * @param mixed $valueField * @param mixed $idField lehet mas mezo is az id */ function associativeField($valueField, $idField = "id") { $res = array(); if(is_array($this->result)) foreach($this->result as $r) { if(!isset($r[$idField]) || !isset($r[$valueField])) return false; $res[$r[$idField]] = $r[$valueField]; } return $res; } /** * Resultból id => teljes sor párokat készít * * @param mixed $idField lehet mas mezo is az id */ function associativeRow($idField = "id") { $res = array(); if(is_array($this->result)) foreach($this->result as $r) { if(!isset($r[$idField])) return false; $res[$r[$idField]] = $r; } return $res; } /** * A mezőhöz tartozó adatokat sorszámozott (by fetch order) tömbbbe teszi * A resulton végzi a műveletet * * @param mixed $name */ function getFieldToArray($name, $ownResult = false) { if($ownResult !== false) $result = $ownResult; else $result = &$this->result; if(!is_string($name)) { $this->lg->logError("getFieldToArray failed: " . $name,__FILE__, __LINE__); return false; } $a = array(); if(!$result) return $a; foreach($result as $num => $row) { if(isset($row[$name])) { $a[$num] = $row[$name]; } } return $a; } function logsql($title = "sql") { $this->lg->log($this->getsql(),$title); } function lognum($title = "numberrows") { $this->lg->log($this->numberrows,$title); } function logresult($title = "result", $first = -1) { if($first == -1) $this->lg->log($this->result,$title); else { $csak = array_chunk($this->result, $first, true); $this->lg->log($csak,"First $first " . $title); } } function log($title1 = "sql", $title2 = "numberrows", $title3 = "result") { $this->logsql($title1); $this->lognum($title2); $this->logresult($title3); } function logFirst($first = 1, $title1 = "sql", $title2 = "numberrows", $title3 = "result") { $this->logsql($title1); $this->lognum($title2); $this->logresult($title3, $first); } // return "" if not set function getifset(&$arr, $key) { if(isset($arr) && is_array($arr) && isset($arr[$key])) return $arr[$key]; else return ""; } } function db_getOne ($sql) { global $db; if( !($db instanceof mysqldb) ) $db = new mysqldb(); $db->setsql($sql); if ($db->selectquery(false) && isset($db->result[0][0])) return $db->result[0][0]; else return false; } function db_getRow ($sql, $label = true) { global $db; if( !($db instanceof mysqldb) ) $db = new mysqldb(); $db->setsql($sql); if ($db->selectquery($label) && isset($db->result[0])) return $db->result[0]; else return false; } function db_getAll ($sql, $label = true) { global $db; if( !($db instanceof mysqldb) ) $db = new mysqldb(); $db->setsql($sql); if ($db->selectquery($label)) return $db->result; else return false; } function db_Query ($sql, $debug=false){ global $db; if( !($db instanceof mysqldb) ) $db = new mysqldb(); $db->setsql($sql); return $db->updatequery(); } function db_getAlltoRow ($sql) { global $db; if( !($db instanceof mysqldb) ) $db = new mysqldb(); $db->setsql($sql); $db->selectquery(false); $return = array(); if ($db->result) foreach ($db->result as $res) $return[] = $res[0]; return $return; } function db_getsql () { global $db; return $db->getsql(); } function db_fieldToArray($result, $name) { $a = array(); if(!$result) return $a; foreach($result as $num => $row) { if(isset($row[$name])) { $a[$num] = $row[$name]; } } return $a; } ?>
Parse error: syntax error, unexpected end of file in /var/www/omega/data/www/frontvonal.hu/include/functions.php on line 735