Blame view

modules/admin/model/auth.class.php 1.89 KB
8d65d0ce   andryeyev   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  <?php
  Class Auth{
   private $login = 'adm';
   private $psw = '123';
   private $db;
   
   function __construct(){
    $this->db = sdb::getInstance();
   }
   
   public function valid($data,$id = 0){
    $error = array();
  
    if(!preg_match("/.{1,50}/i",$data['login']))$error[] = "Îøèáêà ââîäà Ëîãèí!";
    elseif($this->is_login($data['login'],$id))$error[] = "Òàêîé Ëîãèí óæå åñòü!";
    
    if(!preg_match("/.{1,50}/i",$data['psw']))$error[] = "Îøèáêà ââîäà Ïàðîëü!";
  
    return $error;
   }
   
   private function is_login($login,$id){
    $sql = "select count(*) from users where login=?";
    if($id>0)$sql .= sprintf(" and id<>'%d'", $id);
    return $this->db->getOne($sql,array($login));
   }
   
   public function save($data){
     $fields_values = array(
     'login'=>$data['login'],
     'psw'=>$data['psw'],
     );
     if(isset($data['update_id']) && $data['update_id']>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$where = "id='{$data['update_id']}'";}
     else $DB_AUTOQUERY = DB_AUTOQUERY_INSERT;
  
     $this->db->autoExecute("users", $fields_values, $DB_AUTOQUERY,$where);
   }
   
   public function delete($id){
    return $this->db->query("delete from users where id=?",array($id));
   }
  
   public function view($id){
    return $this->db->getRow("select * from users where id=?",array($id),DB_FETCHMODE_ASSOC);
   }
   
   public function login($login,$psw){
    if( $this->db->getOne("select count(*) from users where login=? and psw=?",array($login, $psw)) ){
     $_SESSION['login'] = $login;
     $_SESSION['psw'] = $psw;
     return true;
    }
    return false;
   }
   
   public function is_login_session(){
    if( $this->db->getOne("select count(*) from users where login=? and psw=?",array($_SESSION['login'], $_SESSION['psw'])) ) return true;
    return false;
   }
   
   public function _exit(){
    unset($_SESSION['login']);
    unset($_SESSION['psw']);
   }
   
   public function getUsers(){
    $sql = "select * from users order by id";
    return $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
   }
  }
  
  ?>