tableNameDB = $this->db->tblzz_User; $this->tableName = '`User`'; } function initFromForm(&$frm) { $files = array(); $data = $frm->exportValues(); $data['login'] = $data['u_login']; $data['password'] = $data['u_password']; unset($data['u_re_password'], $data['u_password'], $data['u_login'], $data['action'] ); if ($data['password'] == '') unset($data['password']); else $data['password'] = md5($data['password']); $this->arrayParseUrl2Tag($data); $this->initFromArray($data, $files); return true; } function _getWhat4Grid($opt = array()) { return "{$this->tableName}.*, CONCAT({$this->tableName}.firstname, ' ', {$this->tableName}.lastname) as name "; } function _getWhere4Grid($opt = array()) { $where = parent::_getWhere4Grid($opt); return $where; } function DBLogin($user) { $sql = "SELECT * FROM {$this->tableNameDB} as User WHERE User.password = ".$this->db->quote(($user['password'])) ." AND User.login = ".$this->db->quote($user['login']); $userData = $this->db->queryRow($sql); if (MDB2::isError($userData) || !isset($userData['id'])) { $userData = array(); } else { $this->id = $userData['id']; } $this->userData = $userData; return $userData; } function getUserByLogin($login) { $sql = "SELECT * FROM {$this->tableNameDB} as User WHERE User.login = ".$this->db->quote($login); $data = $this->db->queryRow($sql); $this->id = @$data['id']; return $data; } function getUserByEmail($email) { $sql = "SELECT * FROM {$this->tableNameDB} as User WHERE User.email = ".$this->db->quote($email); $data = $this->db->queryRow($sql); $this->id = @$data['id']; return $data; } function getUserByAuth($auth) { $sql = "SELECT * FROM {$this->tableNameDB} as User WHERE User.auth = ".$this->db->quote($auth); $data = $this->db->queryRow($sql); $this->id = @$data['id']; return $data; } function isLoginUnique($name) { $sql = "SELECT COUNT(*) FROM {$this->tableNameDB} WHERE login = ".$this->db->quote($name); if ($this->id){ $sql .= ' AND id != '.$this->db->quote($this->id); } return !(bool)$this->db->queryOne($sql) ; } function isEmailUnique($name) { $sql = "SELECT COUNT(*) FROM {$this->tableNameDB} WHERE email = ".$this->db->quote($name); if ($this->id){ $sql .= ' AND id != '.$this->db->quote($this->id); } return !(bool)$this->db->queryOne($sql) ; } function changeOption($opt = NULL){ if (!strlen($opt)) return false; $opt = $this->db->quote($opt); $opt = substr($opt, 1, strlen($opt)-2); $field = $this->db->queryRow("SHOW COLUMNS FROM {$this->tableNameDB} LIKE '%{$opt}%'"); if (!isset($field['field'])) return false; if (strncmp($field['type'], 'enum', 4)!=0) return false; eval("\$arrValues = ".str_replace('enum', 'array', $field['type']).';'); $val = $this->db->queryOne("SELECT `$opt` FROM {$this->tableNameDB} WHERE id = ".$this->db->quote($this->id)); $valPos = array_search($val, $arrValues); $val = ((isset($arrValues[$valPos+1])))?$arrValues[$valPos+1]:$arrValues[0]; $this->db->query("UPDATE {$this->tableNameDB} SET `$opt` = ".$this->db->quote($val).' WHERE id='.$this->db->quote($this->id).' LIMIT 1'); } function generateNewAuthCode($email) { if (empty($email)) return; if (!$email) return; $data = $this->getUserByEmail($email); $possible = "0123456789abcdfghjkmnpqrstvwxyz"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < 8) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); $pwd .= $char; $i++; } $sql = "SELECT COUNT(id) FROM {$this->tableNameDB} WHERE auth = '".md5($pwd)."'"; if (intval($this->db->queryOne($sql)) > 0) { $this->generateNewAuthCode($email); } $data['auth'] = md5($pwd); $this->update($data); return $this->sendEmailPWD($data); } function sendEmailPWD($data = null) { if (empty($data) || !is_array($data)) { return false; } if (!$data['email']) return; $Settings = SiteMap::getObj('Settings/Settings.php'); $siteUrl = $Settings->get('site_url'); $retEmail = "info@$siteUrl"; require_once 'class/Doc/Doc.php'; $smarty = Doc::getSmarty(); $smarty->assign('user', $data); $smarty->assign('site_url', $siteUrl); $html = $smarty->fetch(BASE_PATH.'/tpl/User/forgot_mail.tpl'); require_once(BASE_PATH.'/lib/htmlMimeMail/htmlMimeMail.php'); $mail = new htmlMimeMail(); $text = stripcslashes($html); $mail->setHtml($html, $text); $mail->setReturnPath($retEmail); $mail->setFrom('"'.$retEmail.'" <'.$retEmail.'>'); $mail->setSubject('Lost password on '.$siteUrl); $mail->setHeader('X-Mailer', 'HTML Mime mail class'); $result = $mail->send(array($data['email'])); return true; } function checkAuthCode($code) { $flag = $this->db->queryOne("SELECT id FROM {$this->tableNameDB} WHERE auth = '$code'"); return $flag; } function changePassword($arr) { if (!is_array($arr) || empty($arr)) return; if (!$arr['id']) return; $this->id = $arr['id']; $data = $this->getFromDB($arr['id']); $data['password'] = md5($arr['password']); $data['auth'] = $arr['auth']; unset($data['changed']); unset($data['added']); $this->update($data); return true; } function insert($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $data = $this->_data; unset($data['id']); $fields = $this->getSimpleFieldList(); if (isset($fields['sorter'])) { $data['sorter'] = $this->getSorter(); } $this->table->table = $this->tableNameDB; $this->id = $this->table->insert($data); $this->handleFiles(); return $this->id; } function update($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $this->table->table = $this->tableNameDB; $this->table->update($this->_data, 'id = '. $this->db->quote($this->id, 'integer') ); $this->handleFiles(); return true; } function delete() { $this->_initTable(); $this->table->table = $this->tableNameDB; switch ($this->delMethod) { case DB_OBJ_DEL_METHOD_LOGICAL: $data = array('deleted' => 'y'); $this->table->update($data, 'id = '. $this->db->quote($this->id, 'integer')); break; case DB_OBJ_DEL_METHOD_PHYSICAL: $this->handleFiles(); $this->table->delete('id = '. $this->db->quote($this->id, 'integer') ); break; } return true; } } ?>