diff --git a/serv.php b/serv.php index 0e021d9..4b75cec 100644 --- a/serv.php +++ b/serv.php @@ -197,239 +197,7 @@ function event () { } unset ($x); return 1; - } - - class mysql { - protected static $conn; - private static $db_host; - private static $db_port; - private static $db_user; - private static $db_pass; - private static $db_db; - - function settings () { - global $db_host; - global $db_port; - global $db_user; - global $db_pass; - global $db_db; - $this->db_host = $db_host; - $this->db_port = $db_port; - $this->db_user = $db_user; - $this->db_pass = $db_pass; - $this->db_db = $db_db; - } - - function connect () { - echo 'connecting to '.$this->db_host.' ...'; - $this->conn = mysql_connect($this->db_host.':'.$this->db_port, $this->db_user, $this->db_pass, true); - if (!$this->conn) { - logit('Not connected : ' . mysql_error()); - return false; -// die('Not connected : ' . mysql_error()); - } - if (!mysql_select_db($this->db_db, $this->conn)) { - logit('Can\'t use '.$this->db_db.' : ' . mysql_error()); - return false; -// die ('Can\'t use '.$this->db_db.' : ' . mysql_error()); - } - return true; - } - - function escape ($data) { - if (get_magic_quotes_gpc()) { - $data = stripslashes($data); - } - if (!is_numeric($data)) { - $data = "'".mysql_real_escape_string($data, $this->conn)."'"; - } - return $data; - } - - function sql ($sql,$tryagain = true) { - $time = microtime(1); - if (strtolower(substr($sql,0,6)) == 'select') - $sql = 'SELECT HIGH_PRIORITY'.substr($sql,6); - - mysql_close($this->conn); - while( !$this->connect() ) usleep(100); - $ret = mysql_query($sql); - if ((microtime(1) - $time) > 1) - echo 'SQL Long Query time: '.(microtime(1) - $time).' Query: '.$sql."\n"; - if (mysql_error() or !$ret) { - if(mysql_error() == 'Lost connection to MySQL server during query' and $tryagain) { - //$this->connect(); - while( !$this->connect() ) usleep(100); - return $this->sql($sql,false); - } else - logit("MySQL Error: ".mysql_error()."\nSQL: ".$sql); - } - return $ret; - } - - function insert ($tbl,$data,$onduplicate = false) { - $sql = 'INSERT INTO `'.$tbl.'`'; - foreach ($data as $name => $val) { - $names[] = '`'.$name.'`'; - if ($val == 'NULL') { - $values[] = 'NULL'; - } elseif (substr($val,0,9) == 'PASSWORD(') { - $values[] = 'PASSWORD('.$this->escape(substr($val,9,-1)).')'; - } else { - $values[] = $this->escape($val); - } - } - $names = implode(',',$names); - $values = implode(',',$values); - $sql .= ' ('.$names.') VALUES ('.$values.')'; - if ($onduplicate) { - $sql .= ' ON DUPLICATE KEY UPDATE '; - $update = array(); - foreach ($data as $name => $value) - $update[] = '`'.$name.'`=VALUES(`'.$name.'`)'; - $sql .= implode(', ', $update); - } - return $this->sql($sql); - } - - function getaccess ($nick) { - $accessid = $this->get($this->sql('SELECT `loggedin` FROM `users` WHERE `nick` = '.$this->escape($nick))); - $accessid = $accessid['loggedin']; - if ($accessid != -1) { - $level = $this->get($this->sql('SELECT `level` FROM `access` WHERE `id` = '.$accessid)); - } else { - $level = 0; - } - return $level['level']; - } - - function setaccess ($id,$level) { - $this->sql('UPDATE `access` SET `level` = '.$this->escape($level).' WHERE `id` = '.$this->escape($id)); - } - - function setaccesspassword ($id,$password) { - $this->sql('UPDATE `access` SET `pass` = PASSWORD('.$this->escape($password).') WHERE `id` = '.$this->escape($id)); - } - - function addaccess ($user,$pass,$level) { - $data = Array( - 'id' => 'NULL', - 'user' => $user, - 'pass' => 'PASSWORD('.$pass.')', - 'level' => $level - ); - $this->insert('access',$data); - } - - function loginaccess ($who,$user,$pass) { - $access = $this->get($this->sql('SELECT * FROM `access` WHERE `user` = '.$this->escape($user).' AND `pass` = PASSWORD('.$this->escape($pass).')')); - if (is_array($access)) { - $this->sql('UPDATE `users` SET `loggedin` = '.$this->escape($access['id']).' WHERE `nick` = '.$this->escape($who)); - event('identify',$who,$access['id']); - return true; - } else { - return false; - } - } - - function logoutaccess ($who) { - $user = $this->get($this->sql('SELECT * FROM `users` WHERE `nick` = '.$this->escape($who))); - $this->sql('UPDATE `users` SET `loggedin` = -1 WHERE `nick` = '.$this->escape($who)); - event('logout',$who,$user); - return true; - } - - function get ($resource) { - return mysql_fetch_array($resource); - } - - function init () { - $this->sql('DELETE FROM `users`'); - $this->sql('DELETE FROM `user_chan`'); - $this->sql('DELETE FROM `channels`'); - $this->sql('DELETE FROM `servers`'); - } - - function getsetting ($name,$section = 'core') { - $tmp = $this->sql('select `value` from `settings` where `name` = '.$this->escape($name).' and `section` = '.$this->escape($section)); - $val = $this->get($tmp); - $val = $val['value']; - return $val; - } - - function setsetting ($name,$value,$section = 'core') { - if($this->issetting($name,$section)) - $this->sql('update `settings` set `value` = '.$this->escape($value).' where `name` = '.$this->escape($name).' and `section` = '.$this->escape($section)); - else - $this->addsetting($name,$value,$section); - } - - function addsetting ($name,$value,$section = 'core') { - $this->sql('insert into `settings` (`name`,`value`,`section`) values ('.$this->escape($name).','.$this->escape($value).','.$this->escape($section).')'); - } - - function delsetting ($name,$section = 'core') { - $this->sql('delete from `settings` where `name` = '.$this->escape($name).' and `section` = '.$this->escape($section)); - } - - function issetting ($name,$section = 'core') { - if($this->get($this->sql('select `id` from `settings` where `name` = '.$this->escape($name).' and `section` = '.$this->escape($section))) === false) - return false; - return true; - } - - function setaccountproperty ($uid,$key,$value,$visibility = 'hidden',$section = 'core') { - $data = array( - 'uid' => $uid, - 'key' => $key, - 'value' => $value, - 'visibility' => $visibility, - 'section' => $section - ); - $this->insert('access_properties',$data,true); - } - - function listaccountproperties ($uid=null,$key=null,$value=null,$visibility=null,$section=null) { - $sql = 'SELECT * FROM `access_properties` WHERE 1=1'; - if($uid !== null) - $sql .= ' AND `uid` = '.$this->escape($uid); - if($key !== null) - $sql .= ' AND `key` = '.$this->escape($key); - if($value !== null) - $sql .= ' AND `value` = '.$this->escape($value); - if($visibility !== null) - $sql .= ' AND `visibility` = '.$this->escape($visibility); - if($section !== null) - $sql .= ' AND `section` = '.$this->escape($section); - - $result = $this->sql($sql); - $results = array(); - while($results[] = $this->get($result)); - return $results; - } - - function getaccountproperty ($uid,$key,$section = 'core') { - $data = $this->listaccountproperties($uid,$key,null,null,$section); - return $data[0]; - } - - function delaccountproperty ($uid=null,$key=null,$value=null,$visibility=null,$section=null) { - $sql = 'DELETE FROM `access_properties` WHERE 1=1'; - if($uid !== null) - $sql .= ' AND `uid` = '.$this->escape($uid); - if($key !== null) - $sql .= ' AND `key` = '.$this->escape($key); - if($value !== null) - $sql .= ' AND `value` = '.$this->escape($value); - if($visibility !== null) - $sql .= ' AND `visibility` = '.$this->escape($visibility); - if($section !== null) - $sql .= ' AND `section` = '.$this->escape($section); - - $this->sql($sql); - } - } - + } class socks { protected static $socket;