1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A database driven model. |
7
|
|
|
*/ |
8
|
|
|
class User extends ActiveRecordModelExtender |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string $tableName name of the database table. |
13
|
|
|
*/ |
14
|
|
|
protected $tableName = "ramverk1_users"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Columns in the table. |
18
|
|
|
* |
19
|
|
|
* @var integer $id primary key auto incremented. |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
public $name; |
23
|
|
|
public $email; |
24
|
|
|
public $pass; |
25
|
|
|
public $authority = "user"; |
26
|
|
|
public $question; |
27
|
|
|
|
28
|
|
|
|
29
|
7 |
|
public function setupUser($user) |
30
|
|
|
{ |
31
|
7 |
|
$question = new Question($this->db); |
32
|
7 |
|
$post = new Post($this->db); |
33
|
7 |
|
$comment = new Comment($this->db); |
34
|
|
|
|
35
|
7 |
|
$user->questions = $question->getQuestions("user = ?", $user->name); |
36
|
7 |
|
$user->posts = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]); |
37
|
7 |
|
$user->comments = $comment->getComments("user = ?", $user->name); |
38
|
7 |
|
$user->img = $this->getGravatar($user->name); |
39
|
7 |
|
$user->postAmount = count($user->questions) + count($user->posts) + count($user->comments); |
40
|
|
|
|
41
|
|
|
// Get all the questions connected to answers |
42
|
7 |
|
$user->answeredQuestions = array_map(function ($answer) use ($question) { |
43
|
5 |
|
return $question->getQuestion($answer->questionId); |
44
|
7 |
|
}, $user->posts); |
45
|
|
|
|
46
|
7 |
|
return $user; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns post with markdown and gravatar |
51
|
|
|
* @param string $sql |
52
|
|
|
* @param array $param |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
3 |
|
public function getAllUsers($sql = null, $params = null) |
57
|
|
|
{ |
58
|
3 |
|
if ($sql == null) { |
|
|
|
|
59
|
3 |
|
$users = $this->findAll(); |
60
|
3 |
|
} |
61
|
3 |
|
if ($sql != null) { |
|
|
|
|
62
|
1 |
|
$users = $this->findAllWhere($sql, $params); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
3 |
|
return array_map(array($this, 'setupUser'), $users); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* return question/answer, three attributes are set, comments connected to them is an array. |
70
|
|
|
* |
71
|
|
|
* @return object |
72
|
|
|
*/ |
73
|
4 |
|
public function getUser($name) |
74
|
|
|
{ |
75
|
4 |
|
$user = $this->find("name", $name); |
76
|
4 |
|
$user = $this->setupUser($user); |
77
|
4 |
|
return $user; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if user exists |
83
|
|
|
* |
84
|
|
|
* @param string $name |
85
|
|
|
* |
86
|
|
|
* @return boolean true if user exists in database else false |
87
|
|
|
*/ |
88
|
1 |
|
public function userExists($name) |
89
|
|
|
{ |
90
|
1 |
|
$user = $this->find("name", $name); |
91
|
1 |
|
return !$user ? false : true; |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* Returns gravatar link |
95
|
|
|
* |
96
|
|
|
* @param string $email |
|
|
|
|
97
|
|
|
* |
98
|
|
|
* @return string as gravatar link |
99
|
|
|
*/ |
100
|
1 |
|
public function setGravatar() |
101
|
|
|
{ |
102
|
1 |
|
$this->img = $this->gravatar($this->email); |
|
|
|
|
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns gravatar link |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* |
110
|
|
|
* @return string as gravatar link |
111
|
|
|
*/ |
112
|
10 |
|
public function getGravatar($name) |
113
|
|
|
{ |
114
|
10 |
|
$this->find("name", $name); |
115
|
10 |
|
return $this->gravatar($this->email); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the pass. |
120
|
|
|
* |
121
|
|
|
* @param string $pass the pass to use. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
4 |
|
public function setPassword($pass) |
126
|
|
|
{ |
127
|
4 |
|
$this->pass = password_hash($pass, PASSWORD_DEFAULT); |
128
|
4 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Verify the name and the pass, if successful the object contains |
132
|
|
|
* all details from the database row. |
133
|
|
|
* |
134
|
|
|
* @param string $name name to check. |
135
|
|
|
* @param string $pass the pass to use. |
136
|
|
|
* |
137
|
|
|
* @return boolean true if name and pass matches, else false. |
138
|
|
|
*/ |
139
|
3 |
|
public function verifyPassword($name, $pass) |
140
|
|
|
{ |
141
|
3 |
|
$this->find("name", $name); |
142
|
3 |
|
return password_verify($pass, $this->pass); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Verify the name and the anaswer, if successful the object contains |
147
|
|
|
* all details from the database row. |
148
|
|
|
* |
149
|
|
|
* @param string $name name to check. |
150
|
|
|
* @param string $answer the answer. |
151
|
|
|
* |
152
|
|
|
* @return boolean true if name and pass matches, else false. |
153
|
|
|
*/ |
154
|
1 |
|
public function verifyQuestion($name, $answer) |
155
|
|
|
{ |
156
|
1 |
|
$this->find("name", $name); |
157
|
1 |
|
return ($this->question == $answer); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check if a post belongs to user |
163
|
|
|
* |
164
|
|
|
* |
165
|
|
|
* @return boolean |
166
|
|
|
*/ |
167
|
1 |
|
public function controlAuthority($loggedUser, $name) |
168
|
|
|
{ |
169
|
1 |
|
$this->find("name", $loggedUser); |
170
|
|
|
// IF AUTHORITY == admin, then continue |
171
|
1 |
|
if ($this->authority != "admin") { |
172
|
1 |
|
return ($this->name == $name); |
173
|
|
|
} |
174
|
1 |
|
return true; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.