|
1
|
|
|
<?php |
|
2
|
|
|
namespace Anax\Users; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Model for Users. |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class User extends \Anax\MVC\CDatabaseModel |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Initialize model. |
|
13
|
|
|
* |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public function init() |
|
17
|
|
|
{ |
|
18
|
|
|
// $this->db->setVerbose(); |
|
19
|
|
|
$this->db->dropTableIfExists('user')->execute(); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$this->db->createTable( |
|
|
|
|
|
|
22
|
|
|
'user', |
|
23
|
|
|
[ |
|
24
|
|
|
'id' => ['integer', 'primary key', 'not null', 'auto_increment'], |
|
25
|
|
|
'acronym' => ['varchar(20)', 'unique', 'not null'], |
|
26
|
|
|
'email' => ['varchar(80)'], |
|
27
|
|
|
'name' => ['varchar(80)'], |
|
28
|
|
|
'points' => ['int'], |
|
29
|
|
|
'password' => ['varchar(255)'], |
|
30
|
|
|
'created' => ['datetime'], |
|
31
|
|
|
'updated' => ['datetime'], |
|
32
|
|
|
'deleted' => ['datetime'], |
|
33
|
|
|
'active' => ['datetime'], |
|
34
|
|
|
] |
|
35
|
|
|
)->execute(); |
|
36
|
|
|
$this->db->insert( |
|
|
|
|
|
|
37
|
|
|
'user', |
|
38
|
|
|
['acronym', 'email', 'name', 'points', 'password', 'created', 'active'] |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
|
42
|
|
|
|
|
43
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
44
|
|
|
'admin', |
|
45
|
|
|
'[email protected]', |
|
46
|
|
|
'Administrator', |
|
47
|
|
|
0, |
|
48
|
|
|
md5('admin'), |
|
49
|
|
|
$now, |
|
50
|
|
|
$now |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
54
|
|
|
'bob', |
|
55
|
|
|
'[email protected]', |
|
56
|
|
|
'Byggare Bob', |
|
57
|
|
|
0, |
|
58
|
|
|
md5('bob'), |
|
59
|
|
|
$now, |
|
60
|
|
|
$now |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
64
|
|
|
'fnlive', |
|
65
|
|
|
'[email protected]', |
|
66
|
|
|
'Fredrik Nilsson', |
|
67
|
|
|
0, |
|
68
|
|
|
md5('qwerty'), |
|
69
|
|
|
$now, |
|
70
|
|
|
$now |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function find($id) |
|
76
|
|
|
{ |
|
77
|
|
|
$user = parent::find($id); |
|
78
|
|
|
// Add Gravatar |
|
79
|
|
|
$gravatarSize = 80; |
|
80
|
|
|
$user->gravatar = $this->getGravatar($user->email, $gravatarSize); |
|
81
|
|
|
return $user; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $email The email address |
|
88
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
|
89
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
|
90
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
|
91
|
|
|
* @param boole $img True to return a complete IMG tag False for just the URL |
|
92
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag |
|
93
|
|
|
* @return String containing either just a URL or a complete image tag |
|
94
|
|
|
* @source http://gravatar.com/site/implement/images/php/ |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getGravatar($email, $s = 80, $d = 'monsterid', $r = 'g', $img = false, $atts = array()) |
|
97
|
|
|
{ |
|
98
|
|
|
$url = 'http://www.gravatar.com/avatar/'; |
|
99
|
|
|
$url .= md5(strtolower(trim($email))); |
|
100
|
|
|
$url .= "?s=$s&d=$d&r=$r"; |
|
101
|
|
|
if ($img) { |
|
102
|
|
|
$url = '<img src="' . $url . '"'; |
|
103
|
|
|
foreach ($atts as $key => $val) { |
|
104
|
|
|
$url .= ' ' . $key . '="' . $val . '"'; |
|
105
|
|
|
} |
|
106
|
|
|
$url .= ' />'; |
|
107
|
|
|
} |
|
108
|
|
|
return $url; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function loggedIn() |
|
112
|
|
|
{ |
|
113
|
|
|
if (null!==$this->session->get('user_logged_in')) { |
|
|
|
|
|
|
114
|
|
|
// echo "You are logged in, {$this->session->get('user')}"; |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
// echo "You are logged out"; |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
/** |
|
121
|
|
|
* Get user with acronym. |
|
122
|
|
|
* |
|
123
|
|
|
* @return array $user |
|
124
|
|
|
*/ |
|
125
|
|
|
public function loggedInUser() |
|
126
|
|
|
{ |
|
127
|
|
|
$userAcronym = $this->session->get('user_logged_in'); |
|
|
|
|
|
|
128
|
|
|
$user = $this->query() |
|
129
|
|
|
->where('acronym =' . "'$userAcronym'") |
|
130
|
|
|
->execute()[0]; |
|
131
|
|
|
return $user; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.