1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oenstrom\User; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \Oenstrom\User\HTMLForm\LoginForm; |
8
|
|
|
use \Oenstrom\User\HTMLForm\RegisterForm; |
9
|
|
|
use \Oenstrom\User\HTMLForm\ProfileForm; |
10
|
|
|
|
11
|
|
|
// use \Anax\Book\HTMLForm\UpdateForm; |
12
|
|
|
/** |
13
|
|
|
* A User controller class. |
14
|
|
|
*/ |
15
|
|
|
class UserController implements InjectionAwareInterface |
16
|
|
|
{ |
17
|
|
|
use InjectionAwareTrait; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var $data description |
23
|
|
|
*/ |
24
|
|
|
//private $data; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Handler with form to register an user. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
1 |
View Code Duplication |
public function getPostRegister() |
|
|
|
|
34
|
|
|
{ |
35
|
1 |
|
$title = "Sign up"; |
36
|
1 |
|
$view = $this->di->get("view"); |
37
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
38
|
1 |
|
$form = new RegisterForm($this->di); |
39
|
|
|
|
40
|
1 |
|
$form->check(); |
41
|
|
|
|
42
|
|
|
$data = [ |
43
|
1 |
|
"form" => $form->getHTML(["use_buttonbar" => false]), |
44
|
1 |
|
"title" => $title, |
45
|
1 |
|
]; |
46
|
1 |
|
$view->add("user/user-register", $data); |
47
|
1 |
|
$pageRender->renderPage(["title" => $title]); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handler with form to login an user. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
1 |
View Code Duplication |
public function getPostLogin() |
|
|
|
|
58
|
|
|
{ |
59
|
1 |
|
$title = "Sign in"; |
60
|
1 |
|
$view = $this->di->get("view"); |
61
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
62
|
1 |
|
$form = new LoginForm($this->di); |
63
|
|
|
|
64
|
1 |
|
$form->check(); |
65
|
|
|
|
66
|
|
|
$data = [ |
67
|
1 |
|
"form" => $form->getHTML(["use_fieldset" => false, "use_buttonbar" => false]), |
68
|
1 |
|
]; |
69
|
1 |
|
$view->add("user/user-login", $data); |
70
|
1 |
|
$pageRender->renderPage(["title" => $title]); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Handler to logout an user. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
1 |
|
public function getLogout() |
81
|
|
|
{ |
82
|
1 |
|
$session = $this->di->get("session"); |
83
|
1 |
|
$session->destroy(); |
84
|
1 |
|
$this->di->get("response")->redirect(""); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get all users. |
91
|
|
|
*/ |
92
|
1 |
|
public function getUsers() |
93
|
|
|
{ |
94
|
1 |
|
$title = "Users"; |
95
|
1 |
|
$view = $this->di->get("view"); |
96
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
97
|
1 |
|
$user = $this->di->get("user"); |
98
|
|
|
|
99
|
1 |
|
$view->add("user/user-list", ["users" => $user->findAll()]); |
100
|
1 |
|
return $pageRender->renderPage(["title" => $title]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Handler with form to update an user profile. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
1 |
|
public function getPostProfile() |
111
|
|
|
{ |
112
|
1 |
|
$title = "Your profile"; |
113
|
1 |
|
$view = $this->di->get("view"); |
114
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
115
|
1 |
|
$auth = $this->di->get("authHelper"); |
116
|
1 |
|
$form = new ProfileForm($this->di); |
117
|
|
|
|
118
|
1 |
|
$form->check(); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
$data = [ |
122
|
1 |
|
"form" => $form->getHTML(["use_buttonbar" => false]), |
123
|
1 |
|
"gravatar" => $this->getGravatar($this->di->get("session")->get("email"), true, 256) |
|
|
|
|
124
|
1 |
|
]; |
125
|
|
|
|
126
|
1 |
|
if ($auth->isAdmin()) { |
127
|
1 |
|
$view->add("user/admin/admin-links", []); |
128
|
1 |
|
} |
129
|
1 |
|
$view->add("user/profile", $data); |
130
|
1 |
|
$pageRender->renderPage(["title" => $title]); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
137
|
|
|
* |
138
|
|
|
* @param string $email The email address |
139
|
|
|
* @param boole $img True to return a complete IMG tag False for just the URL |
140
|
|
|
* @param string $size Size in pixels, defaults to 80px [ 1 - 2048 ] |
141
|
|
|
* @param string $default Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
142
|
|
|
* @param string $rating Maximum rating (inclusive) [ g | pg | r | x ] |
143
|
|
|
* @param array $atts Optional, additional key/value attributes to include in the IMG tag |
144
|
|
|
* @return String containing either just a URL or a complete image tag |
145
|
|
|
* @source https://gravatar.com/site/implement/images/php/ |
146
|
|
|
*/ |
147
|
2 |
|
public function getGravatar($email, $img = false, $size = 80, $default = 'mm', $rating = 'g', $atts = array()) |
148
|
|
|
{ |
149
|
2 |
|
$url = 'https://www.gravatar.com/avatar/'; |
150
|
2 |
|
$url .= md5(strtolower(trim($email))); |
151
|
2 |
|
$url .= "?s=$size&d=$default&r=$rating"; |
152
|
2 |
View Code Duplication |
if ($img) { |
|
|
|
|
153
|
2 |
|
$url = '<img src="' . $url . '" alt="' . $email . '"'; |
154
|
2 |
|
foreach ($atts as $key => $val) { |
155
|
1 |
|
$url .= ' ' . $key . '="' . $val . '"'; |
156
|
2 |
|
} |
157
|
2 |
|
$url .= ' />'; |
158
|
2 |
|
} |
159
|
2 |
|
return $url; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.