1 | <?php
|
---|
2 |
|
---|
3 | include_once('System.php');
|
---|
4 |
|
---|
5 | class UserNotFoundException extends Exception {}
|
---|
6 |
|
---|
7 | class User extends Object
|
---|
8 | {
|
---|
9 | var $Id;
|
---|
10 |
|
---|
11 | function LoadData()
|
---|
12 | {
|
---|
13 | $DbResult = $this->System->Database->select('User', '*', 'Id='.$this->Id);
|
---|
14 | if($DbResult->num_rows > 0)
|
---|
15 | {
|
---|
16 | $this->Data = $DbResult->fetch_assoc();
|
---|
17 | if($this->Data['Rank'] == '') $this->Data['Rank'] = 0;
|
---|
18 | } else throw new Exception('User with id '.$this->Id.' not found');
|
---|
19 | }
|
---|
20 |
|
---|
21 | function GetByName($Name)
|
---|
22 | {
|
---|
23 | $DbResult = $this->System->Database->select('User', 'Id', 'Name="'.$Name.'"');
|
---|
24 | if($DbResult > 0)
|
---|
25 | {
|
---|
26 | $DbRow = $DbResult->fetch_assoc();
|
---|
27 | return($DbRow['Id']);
|
---|
28 | } else
|
---|
29 | throw new UserNotFoundException('User not found');
|
---|
30 | }
|
---|
31 |
|
---|
32 | function GetByNamePassword($Name, $Password)
|
---|
33 | {
|
---|
34 | $DbResult = $this->System->Database->select('User', 'Id', 'Name="'.$Name.
|
---|
35 | '" AND Password=SHA1(CONCAT("'.$Password.'", Salt))');
|
---|
36 | if($DbResult->num_rows > 0)
|
---|
37 | {
|
---|
38 | $DbRow = $DbResult->fetch_assoc();
|
---|
39 | return($DbRow['Id']);
|
---|
40 | } else
|
---|
41 | throw new UserNotFoundException('User not found');
|
---|
42 | }
|
---|
43 |
|
---|
44 | function Add($Name, $Password, $Email)
|
---|
45 | {
|
---|
46 | $Salt = GetPasswordSalt();
|
---|
47 | $this->System->Database->insert('User', array('Name' => '"'.$Name.'"',
|
---|
48 | 'Salt' => '"'.$Salt.'"', 'Password' => 'SHA1(CONCAT("'.$Password.'", "'.$Salt.'"))',
|
---|
49 | 'Email' => '"'.$Email.'"', 'RegistrationTime' => 'NOW()'));
|
---|
50 | }
|
---|
51 |
|
---|
52 | function GetPasswordSalt()
|
---|
53 | {
|
---|
54 | $Salt = uniqid(mt_rand(), true);
|
---|
55 | return(base64_encode(substr($Salt, 0, 8)));
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | ?>
|
---|