1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Event\Badges; |
5
|
|
|
use App\I18n\Language; |
6
|
|
|
use Cake\Controller\Controller; |
7
|
|
|
use Cake\Event\Event; |
8
|
|
|
use Cake\I18n\Time; |
9
|
|
|
|
10
|
|
|
class AppController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Initialization hook method. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function initialize() |
19
|
|
|
{ |
20
|
|
|
parent::initialize(); |
21
|
|
|
|
22
|
|
|
//$this->loadComponent('Flash'); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Components. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public $components = [ |
31
|
|
|
'Flash', |
32
|
|
|
'Cookie', |
33
|
|
|
'Acl.Acl', |
34
|
|
|
'SessionsActivity', |
35
|
|
|
/** |
36
|
|
|
* If you want enable CSRF uncomment this. |
37
|
|
|
* I recommend to enable it. If i have disable it, it's because |
38
|
|
|
* CloudFlare have some problem with the header X-CSRF-Token (AJAX Request). |
39
|
|
|
*/ |
40
|
|
|
/*'Csrf' => [ |
|
|
|
|
41
|
|
|
'secure' => true |
42
|
|
|
],*/ |
43
|
|
|
'Auth' => [ |
44
|
|
|
'className' => 'AclAuth', |
45
|
|
|
'allowedActionsForBanned' => [ |
46
|
|
|
'Pages' => [ |
47
|
|
|
'home' |
48
|
|
|
] |
49
|
|
|
], |
50
|
|
|
'authenticate' => [ |
51
|
|
|
'Form', |
52
|
|
|
'Xety/Cake3CookieAuth.Cookie' |
53
|
|
|
], |
54
|
|
|
'flash' => [ |
55
|
|
|
'element' => 'error', |
56
|
|
|
'key' => 'flash', |
57
|
|
|
'params' => [ |
58
|
|
|
'class' => 'error' |
59
|
|
|
] |
60
|
|
|
], |
61
|
|
|
'authorize' => [ |
62
|
|
|
'Acl.Actions' => [ |
63
|
|
|
'actionPath' => 'app/' |
64
|
|
|
] |
65
|
|
|
], |
66
|
|
|
'loginAction' => [ |
67
|
|
|
'controller' => 'users', |
68
|
|
|
'action' => 'login', |
69
|
|
|
'prefix' => false |
70
|
|
|
], |
71
|
|
|
'unauthorizedRedirect' => [ |
72
|
|
|
'controller' => 'pages', |
73
|
|
|
'action' => 'home', |
74
|
|
|
'prefix' => false |
75
|
|
|
], |
76
|
|
|
'loginRedirect' => [ |
77
|
|
|
'controller' => 'pages', |
78
|
|
|
'action' => 'home' |
79
|
|
|
], |
80
|
|
|
'logoutRedirect' => [ |
81
|
|
|
'controller' => 'pages', |
82
|
|
|
'action' => 'home' |
83
|
|
|
], |
84
|
|
|
'authError' => 'You are not authorized to access that location !' |
85
|
|
|
] |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Helpers. |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
public $helpers = [ |
94
|
|
|
'Form' => [ |
95
|
|
|
'templates' => 'form-templates' |
96
|
|
|
], |
97
|
|
|
'Paginator' => [ |
98
|
|
|
'templates' => 'paginator-templates' |
99
|
|
|
], |
100
|
|
|
'Acl' |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* beforeFilter handle. |
105
|
|
|
* |
106
|
|
|
* @param Event $event The beforeFilter event that was fired. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function beforeFilter(Event $event) |
111
|
|
|
{ |
112
|
|
|
//Define the language. |
113
|
|
|
$language = new Language($this); |
114
|
|
|
$language->setLanguage(); |
115
|
|
|
|
116
|
|
|
//Check for the Premium. |
117
|
|
|
$premium = $this->request->session()->read('Premium.Check') ? $this->request->session()->read('Premium.Check') : null; |
118
|
|
|
if (!is_null($premium)) { |
119
|
|
|
$this->loadModel('PremiumTransactions'); |
120
|
|
|
|
121
|
|
|
$transaction = $this->PremiumTransactions |
|
|
|
|
122
|
|
|
->find() |
123
|
|
|
->where([ |
124
|
|
|
'txn' => $this->request->session()->read('Premium.Check'), |
125
|
|
|
'user_id' => $this->request->session()->read('Auth.User.id') |
126
|
|
|
]) |
127
|
|
|
->contain(['Users']) |
128
|
|
|
->first(); |
129
|
|
|
|
130
|
|
|
if ($transaction) { |
131
|
|
|
//Write in the session the virtual field. |
132
|
|
|
$this->Auth->setUser($transaction->user->toArray()); |
133
|
|
|
$this->request->session()->write('Auth.User.premium', $transaction->user->premium); |
134
|
|
|
|
135
|
|
|
$this->request->session()->delete('Premium.Check'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
//Set trustProxy or get the original visitor IP. |
140
|
|
|
$this->request->trustProxy = true; |
141
|
|
|
|
142
|
|
|
//Automatically Login. |
143
|
|
|
if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) { |
144
|
|
|
$this->loadModel('Users'); |
145
|
|
|
|
146
|
|
|
$user = $this->Auth->identify(); |
147
|
|
|
if ($user && $user['is_deleted'] == false) { |
148
|
|
|
$this->Auth->setUser($user); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$user = $this->Users->newEntity($user, ['accessibleFields' => ['id' => true]]); |
|
|
|
|
151
|
|
|
$user->isNew(false); |
152
|
|
|
|
153
|
|
|
$user->last_login = new Time(); |
154
|
|
|
$user->last_login_ip = $this->request->clientIp(); |
155
|
|
|
|
156
|
|
|
$this->Users->save($user); |
|
|
|
|
157
|
|
|
|
158
|
|
|
//Write in the session the virtual field. |
159
|
|
|
$this->request->session()->write('Auth.User.premium', $user->premium); |
160
|
|
|
|
161
|
|
|
//Event. |
162
|
|
|
$this->eventManager()->attach(new Badges($this)); |
|
|
|
|
163
|
|
|
|
164
|
|
|
$user = new Event('Model.Users.register', $this, [ |
165
|
|
|
'user' => $user |
166
|
|
|
]); |
167
|
|
|
$this->eventManager()->dispatch($user); |
168
|
|
|
} else { |
169
|
|
|
$this->Cookie->delete('CookieAuth'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (isset($this->request->params['prefix'])) { |
174
|
|
|
$prefix = explode('/', $this->request->params['prefix'])[0]; |
175
|
|
|
|
176
|
|
|
switch ($prefix) { |
177
|
|
|
case 'admin': |
178
|
|
|
$this->viewBuilder()->layout('admin'); |
179
|
|
|
break; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$allowCookies = $this->Cookie->check('allowCookies'); |
184
|
|
|
$this->set(compact('allowCookies')); |
185
|
|
|
|
186
|
|
|
//JavaScript Notifications. |
187
|
|
|
if ($this->request->session()->read('Notification') && !empty($this->request->session()->read('Notification'))) { |
|
|
|
|
188
|
|
|
$notification = $this->request->session()->read('Notification'); |
189
|
|
|
$this->request->session()->delete('Notification'); |
190
|
|
|
|
191
|
|
|
$this->set(compact('notification')); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.