1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alvo\User; |
4
|
|
|
|
5
|
|
|
use \Anax\Database\ActiveRecordModel; |
6
|
|
|
use \Anax\DI\InjectionAwareInterface; |
7
|
|
|
use \Anax\DI\InjectionAwareTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A database driven model. |
11
|
|
|
*/ |
12
|
|
|
class User extends ActiveRecordModel implements InjectionAwareInterface |
13
|
|
|
{ |
14
|
|
|
use InjectionAwareTrait; |
15
|
|
|
use UserUtils; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string $tableName name of the database table. |
21
|
|
|
*/ |
22
|
|
|
protected $tableName = "User"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Columns in the table. |
26
|
|
|
* |
27
|
|
|
* @var integer $id primary key auto incremented. |
28
|
|
|
*/ |
29
|
|
|
public $id; |
30
|
|
|
public $email; |
31
|
|
|
public $password; |
32
|
|
|
public $created; |
33
|
|
|
public $updated; |
34
|
|
|
public $deleted; |
35
|
|
|
public $admin; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the password. |
41
|
|
|
* |
42
|
|
|
* @param string $password the password to use. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
1 |
|
public function setPassword($password) |
47
|
|
|
{ |
48
|
1 |
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
|
|
|
|
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Verify the email and the password, if successful the object contains |
55
|
|
|
* all details from the database row. |
56
|
|
|
* |
57
|
|
|
* @param string $email email to check. |
58
|
|
|
* @param string $password the password to use. |
59
|
|
|
* |
60
|
|
|
* @return boolean true if email and password matches, else false. |
61
|
|
|
*/ |
62
|
1 |
|
public function verifyPassword($email, $password) |
63
|
|
|
{ |
64
|
1 |
|
$this->find("email", $email); |
65
|
1 |
|
return password_verify($password, $this->password); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
1 |
|
public function getUser($column = null, $param = null) |
71
|
|
|
{ |
72
|
1 |
|
if (!$column) { |
73
|
1 |
|
$column = "id"; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
if (!$param) { |
77
|
1 |
|
$param = $this->di->get("session")->get("userId"); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $this->find($column, $param); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
public function getAllUsers() |
86
|
|
|
{ |
87
|
1 |
|
return $this->db |
88
|
1 |
|
->connect() |
89
|
1 |
|
->select() |
90
|
1 |
|
->from($this->tableName) |
91
|
1 |
|
->where("deleted is NULL") |
92
|
1 |
|
->execute() |
93
|
1 |
|
->fetchAllClass(get_class($this)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
1 |
|
public function delete($id = null) |
99
|
|
|
{ |
100
|
1 |
|
$id = $id ?: $this->id; |
101
|
|
|
|
102
|
1 |
|
$comment = new User(); |
103
|
1 |
|
$comment->setDb($this->db); |
104
|
1 |
|
$comment->find("id", $id); |
105
|
1 |
|
$comment->deleted = date("Y-m-d H:i:s"); |
106
|
1 |
|
$comment->save(); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
113
|
|
|
* |
114
|
|
|
* @param string $email The email address |
115
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
116
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
117
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
118
|
|
|
* @param boole $img True to return a complete IMG tag False for just the URL |
|
|
|
|
119
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag |
120
|
|
|
* @return String containing either just a URL or a complete image tag |
121
|
|
|
* @source https://gravatar.com/site/implement/images/php/ |
122
|
|
|
*/ |
123
|
1 |
|
public function getGravatar($email = null, $sss = 80, $ddd = 'mm', $rrr = 'g', $img = false, $atts = array()) |
124
|
|
|
{ |
125
|
1 |
|
$email = $email ?? $this->email; |
126
|
1 |
|
$url = 'https://www.gravatar.com/avatar/'; |
127
|
1 |
|
$url .= md5(strtolower(trim($email))); |
128
|
1 |
|
$url .= "?s=$sss&d=$ddd&r=$rrr"; |
129
|
1 |
|
if ($img) { |
130
|
|
|
$url = '<img src="' . $url . '"'; |
131
|
|
|
foreach ($atts as $key => $val) { |
132
|
|
|
$url .= ' ' . $key . '="' . $val . '"'; |
133
|
|
|
} |
134
|
|
|
$url .= ' />'; |
135
|
|
|
} |
136
|
1 |
|
return $url; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|