ACC SHELL
<?php
/**
* Users model ... z historických důvodů je zde pagination, tak to už spíš jen pro inspiraci
*
* @author Melounek
*/
class Users_model extends Base_model {
public $table = "users";
public function __construct(){
}
public function get_all($options=array()){
$sql = "select * from " . $this->table . $this->left_join($this->table) . "";
// pagination depends on: $options[per_page]
$this->load->library("pagination");
$u = $this->uri->segment_array();
$config['base_url'] = "/";
foreach($u as $uu){
if(strpos($uu,"p:")===false)
$config['base_url'] .= $uu."/";
}
$config['total_rows'] = $this->db->query($sql)->num_rows();
if(isset($options['per_page'])){
$config['per_page'] = $options['per_page'];
}else{
$config['per_page'] = 100;
}
$config['num_links'] = 5;
if(isset($_GET['p'])) $config['cur_page'] = $_GET['p'];
$config['prev_tag_open'] = '<div class="none">'; $config['prev_tag_close'] = '</div>';
$config['next_tag_open'] = '<div class="none">'; $config['next_tag_close'] = '</div>';
$config['first_link'] = '< první'; $config['last_link'] = 'poslední >';
$this->pagination->initialize($config);
$limit = "";
if($config['per_page']) $limit = " limit ".(!empty($_GET['p'])?$_GET['p'].",":"").$config['per_page'];
$q = $this->db->query($sql.$limit);
$data = $q->result_array();
return $data;
}
public function get_one($id=NULL){
$this->load(NULL,$id);
$sql = "select * from ". $this->table . $this->left_join($this->table) ." where id='". $this->id ."'";
$item = $this->db->query($sql)->row_array();
if($item){
$item['password_md5'] = $item['password'];
unset($item['password']);
}
return $item;
}
/**
* @return array user (like get_one)
*/
public function save_user($data,$id=NULL){
$this->load($data,$id);
// var_dump($this);
// var_dump($data);
// exit();
if($this->id){
if(!empty($this->a['password'])){
$this->a['password'] = md5($this->a['password']);
}
// aby se nesmazalo heslo
else{
unset($this->a['password']);
}
// aby se nesmazal pristopovy koren
if(empty($this->a['access_root'])){
unset($this->a['access_root']);
}
// aby slo odstranit priznak programatora
if(empty($this->a['programator'])){
$this->a['programator']=0;
}
// aby slo odstranit priznak admina
if(empty($this->a['admin'])){
$this->a['admin']=0;
}
if($this->update()){
$this->a['id'] = $this->id;
return $this->a;
}
}else{
if(!empty($this->a['password'])){
$this->a['password'] = md5($this->a['password']);
}elseif(empty($this->a['password'])){
$password = Tools::rand_str(6);
$this->a['password'] = md5($password);
$this->a['password_new'] = $password;
}
$this->a['id'] = $this->insert($this->a);
}
return $this->a;
}
/**
* if login is succes, $this->data is filled by user data
* There is no logout method ... for this purpose is Ses::user_destroy() method
* @param array $data required keys are password and login
* @return int id or NULL
*/
public function login($data){
if(!isset($data['password']) || !isset($data['login'])){
Tools::critical("users_model::login() get wrong parametrs");
return false;
}
$md5 = md5($data['password']);
$sql = "select id from users where login='".$data['login']."' && password='".$md5."'";
$q = $this->db->query($sql);
$id = $q->row_array();
if(isset($id['id'])){
$this->id = $id['id'];
$this->data = $this->get_one(); // data is filled by users data
$this->data['hash'] = md5(date("Y-m-d-H:i:s").$this->data['id']);
Ses::user('hash',$this->data['hash']); // for optional better safety
$this->db->query('insert into users_log (hash,id_users) values("'.$this->data['hash'].'","'.$this->id.'")'); // insert access
Tools::log("hash of user id:".$this->id." is now ".$this->data['hash']);
return $this->id;
}else{
return NULL;
}
}
// this method co with Ses_helper
public function is_admin($safety = false){
$u = $this->db->query("select hash from users_log ". $this->left_join("users_log") ."
where id_users='".Ses::user('id')."' && hash='".Ses::user('hash')."' && admin && timestamp > '".date("Y-m-d",time()-3600*24*30)."'
order by timestamp desc")->row_array();
if(!empty($u)){
return true;
}else{
return false;
}
}
public function get_one_transport($id){
$sql = "select * from transport where id='". $id ."'";
$q = $this->db->query($sql);
$data = $q->row_array();
return $data;
}
public function del($id){
$q = $this->db->query("delete from ". $this->table ." where id='". $id ."'");
return $this->db->affected_rows();
}
}
ACC SHELL 2018