Opiniarbo - versikontrolita.
<?php
class Aumodel extends Model
{
private $obj;
function Aumodel()
{
parent::Model();
$this->obj =& get_instance();
$this->config->load('auconfig');
if ( $this->obj->config->item('check_if_tables_exist') )
$this->CheckTablesExist();
if ($this->obj->config->item('au_language') != '')
$curLang = $this->obj->config->item('au_language');
else
$curLang = $this->obj->config->item('language');
$this->obj->lang->load('au', $curLang);
}
/*
* Function for process login/logout etc
*/
function login($name, $pass, $autologin=FALSE)
{
$this->db->where('name', $name);
$this->db->where('password', $pass);
$success = $this->db->count_all_results('auser');
if($success)
{
$this->db->where('name', $name);
$this->db->where('password', $pass);
$user_info = $this->db->get('auser')->result_array();
$user_data = $user_info[0];
if(!$user_data['activate_code'])
{
$data = array('last_visit' => date("Y-m-d H:i:s"));
$this->db->update('auser', $data, array('id' => $user_data['id']));
return $user_data['id'];
}
}
return FALSE;
}
function registers($name, $pass, $email, $confirm)
{
$activation_code = ($confirm)? $this->makeConfirmationCode() : NULL;
$data = array(
'name' => $name ,
'password' => $pass,
'email' => $email,
'activate_code' => $activation_code
);
$this->db->insert('auser', $data);
if(!$confirm) return $this->login($name, $pass, FALSE);
else
{
$this->db->where('name', $name);
$this->db->where('password', $pass);
$user_info = $this->db->get('auser')->result_array();
$user_data = $user_info[0];
$this->_sendConfirmEmail($name, $email, $user_data['id'], $activation_code, NULL);
return TRUE;
}
}
function change($id, $data)
{
$this->db->update('auser', $data, array('id' => $id));
}
function confirmEmail($id, $mess)
{
$this->db->where('id', $id);
$this->db->where('activate_code', $mess);
$success = $this->db->count_all_results('auser');
if($success)
{
$data = array('activate_code' => NULL);
$this->db->update('auser', $data, array('id' => $id));
}
return $success;
}
function forgotten($name, $email)
{
$this->db->where('name', $name);
$this->db->where('email', $email);
$is_exist = $this->db->count_all_results('auser');
if(!$is_exist) return FALSE;
$activation_code = $this->makeConfirmationCode();
$new_pass = $this->makeConfirmationCode();
$this->db->where('name', $name);
$this->db->where('email', $email);
$user_info = $this->db->get('auser')->result_array();
$user_data = $user_info[0];
$data = array(
'password' => md5($new_pass),
'activate_code' => $activation_code
);
$this->db->update('auser', $data, array('id' => $user_data['id']));
$this->_sendConfirmEmail($name, $email, $user_data['id'], $activation_code, $new_pass);
return TRUE;
}
function userInfo($id, $field)
{
$this->db->where('id', $id);
$this->db->select($field);
$user_info = $this->db->get('auser')->result_array();
return (isset($user_info[0][$field])) ? $user_info[0][$field] : '';
}
function userFullInfo($id)
{
$this->db->where('id', $id);
$sql = $this->db->get('auser')->result_array();
return $sql[0];
}
function getRefId($table, $name)
{
$this->db->where('name', $name);
$rez = $this->db->get($table)->result_array();
return $rez[0]['id'];
}
function getRefName($table, $id)
{
$this->db->where('id', $id);
$rez = $this->db->get($table)->result_array();
return $rez[0]['name'];
}
function setRefName($table, $id, $new_name)
{
$data = array('name' => $new_name);
$this->db->update($table, $data, array('id' => $id));
}
function getRefList($table)
{
$result = array();
$sql = $this->db->get($table)->result_array();
foreach($sql as $row) $result[$row['id']] = $row['name'];
return $result;
}
function setRefIdToUser($table, $user_id, $new_value)
{
if($table == 'augroup')
$data = array('group_id' => getRefId($table, $new_value));
else
$data = array('role_id' => getRefId($table, $new_value));
$this->db->update('auser', $data, array('id' => $user_id));
}
/*
*
*/
function delete($table_name, $id)
{
$this->db->delete($table_name, array('id' => $id));
}
function append($table_name)
{
$data = array('name' => 'new...');
$this->db->insert($table_name, $data);
}
function updateUsersRef($ref_name, $old_ref, $new_ref)
{
$this->db->update('auser', array($ref_name.'_id' => $new_ref), array($ref_name.'_id' => $old_ref));
}
/*
* service function
*/
function check_name($name)
{
$this->db->where('name', $name);
return (bool) $this->db->count_all_results('auser');
}
function makeConfirmationCode()
{
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$length = mt_rand(5, 10);
$result = '';
for ($i = 0; $i < $length; $i++)
$result .= $chars[(mt_rand(0, (strlen($chars)-1)))];
return $result;
}
function _sendConfirmEmail($name, $email, $id, $activation_code, $new_pass = NULL)
{
$this->load->library('email');
$this->config->load('email');
$this->email->clear();
$this->email->from($this->obj->config->item('admin_email'));
$this->email->to($email);
$this->email->subject($this->lang->line('email_confirm_sabject'));
$activation_url = site_url('au/confirm/'.$id.'/'.$activation_code);
if(!$new_pass)
$mess_body = str_replace('%name%', $name, $this->lang->line('email_confirm_body'));
else
{
$mess_body = str_replace('%name%', $name, $this->lang->line('email_confirm_pass_body'));
$mess_body = str_replace('%new_pass%', $new_pass, $mess_body);
}
$mess_body = str_replace('%link%', $activation_url, $mess_body);
$this->email->message($mess_body);
$this->email->send();
}
function CheckTablesExist()
{
$this->db->trans_start();
if(!$this->db->table_exists('augroup'))
{
$sql = "CREATE TABLE `augroup` ( `id` int(11) NOT NULL auto_increment,
`name` varchar(128) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8";
$this->db->query($sql);
$sql = "INSERT INTO `augroup` VALUES (1,'users')";
$this->db->query($sql);
$sql = "INSERT INTO `augroup` VALUES (2,'admins')";
$this->db->query($sql);
}
if(!$this->db->table_exists('aurole'))
{
$sql = "CREATE TABLE `aurole` (`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8";
$this->db->query($sql);
$sql = "INSERT INTO `aurole` VALUES (1,'baned')";
$this->db->query($sql);
$sql = "INSERT INTO `aurole` VALUES (2,'user')";
$this->db->query($sql);
$sql = "INSERT INTO `aurole` VALUES (3,'moderator')";
$this->db->query($sql);
$sql = "INSERT INTO `aurole` VALUES (4,'admin')";
$this->db->query($sql);
}
if(!$this->db->table_exists('auser'))
{
$sql = "CREATE TABLE `auser` (`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
`password` varchar(50) default NULL,
`email` varchar(50) default NULL,
`activate_code` varchar(50) default NULL,
`created` timestamp NULL default CURRENT_TIMESTAMP,
`last_visit` datetime default NULL,
`role_id` int(11) unsigned default '1',
`group_id` int(11) unsigned default '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8";
$this->db->query($sql);
/* $sql = "INSERT INTO `auser` VALUES (1,'serg','76419c58730d9f35de7ac538c2fd6737','serg.kosij@list.ru',NULL,'2008-10-12 19:01:03','2008-10-17 23:20:13',4,2)";
$this->db->query($sql);
$sql = "INSERT INTO `auser` VALUES (2,'Сергей','96e79218965eb72c92a549dd5a330112','serg.kosij@list.ru',NULL,'2008-10-17 19:24:39','2008-10-17 22:38:02',1,1)";
$this->db->query($sql);
*/
}
$this->db->trans_complete();
}
}
?>