-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathProcessLogin.module
More file actions
211 lines (173 loc) · 6.89 KB
/
ProcessLogin.module
File metadata and controls
211 lines (173 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* ProcessWire Login Process
*
* Provides Login capability for ProcessWire Admin
*
* For more details about how Process modules work, please see:
* /wire/core/Process.php
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class ProcessLogin extends Process {
protected $nameField;
protected $passField;
protected $submitField;
protected $form;
protected $id;
public static function getModuleInfo() {
return array(
'title' => 'Login',
'summary' => 'Login to ProcessWire',
'version' => 101,
'permanent' => true,
'permission' => 'page-view',
);
}
/**
* Build the login form
*
*/
public function init() {
$this->id = isset($_GET['id']) ? (int) $_GET['id'] : '';
$this->allowForgot = $this->modules->isInstalled('ProcessForgotPassword');
return parent::init();
}
/**
* Check if login posted and attempt login, otherwise render the login form
*
*/
public function ___execute() {
if($this->user->isLoggedin()) {
$this->message($this->_("You are logged in."));
if($this->user->hasPermission('page-edit')) $this->afterLoginRedirect();
$url = $this->config->urls->root;
return "<p><a href='$url'>" . $this->_('Continue') . "</a></p>";
}
if($this->input->get->forgot && $this->allowForgot) {
$process = $this->modules->get("ProcessForgotPassword");
return $process->execute();
}
$this->buildLoginForm();
if(isset($_POST['login_submit'])) $this->form->processInput($this->input->post);
if(!$this->nameField->value || !$this->passField->value) return $this->renderLoginForm();
$name = $this->fuel('sanitizer')->username($this->nameField->value);
$pass = substr($this->passField->value, 0, 50);
if($this->fuel('session')->login($name, $pass)) {
$this->session->message($name . ' - ' . $this->_("Successful login"));
$this->session->remove('error');
$this->performSystemChecks();
$this->session->redirect("./?login=1" . ($this->id ? "&id={$this->id}" : ''));
} else {
$this->error($name . " - " . $this->_("Login failed"));
}
return $this->renderLoginForm();
}
protected function performSystemChecks() {
if(!$this->user->isSuperuser()) return;
$indexVersion = ProcessWire::indexVersion;
if(PROCESSWIRE < $indexVersion) {
$this->error(
"Not urgent, but note that your root index.php file is not up-to-date with this ProcessWire version - please update it when possible. " .
"<br /><small>Required version: $indexVersion, Found version: " . PROCESSWIRE . "</small>", Notice::log | Notice::allowMarkup
);
}
$htaccessFile = $this->wire('config')->paths->root . '.htaccess';
if(is_readable($htaccessFile)) {
$htaccessData = file_get_contents($htaccessFile);
if(!preg_match('/@indexVersion\s+(\d+)\b/', $htaccessData, $matches) || ((int) $matches[1]) < $indexVersion) {
$this->error(
"Not urgent, but note that your root .htaccess file is not up-to-date with this ProcessWire version - please update it when possible.<br />" .
"<small>To ignore this warning, replace or add the following in the top of your existing .htaccess file:</small> " .
"<span style='font-family: monospace;'># @indexVersion $indexVersion</span>", Notice::log | Notice::allowMarkup
);
}
}
// if($this->config->showSecurityWarnings === false) return;
// if(is_writable($this->config->paths->root . "site/config.php")) $this->error("Security Warning: /site/config.php is writable and ideally should not be.");
// if(is_writable($this->config->paths->root . "index.php")) $this->error("Security Warning: /index.php is writable and ideally should not be.");
$warningText = $this->_("Security Warning: %s exists and should be deleted as soon as possible.");
if(is_file($this->config->paths->root . "install.php")) $this->error(sprintf($warningText, '/install.php'), Notice::log);
$file = $this->config->paths->assets . "active.php";
if(!is_file($file)) {
$data = "<?php // Do not delete this file. " .
"The existence of this file indicates the site is confirmed active " .
"and first-time use errors may be suppressed. Installed at: " .
"[{$this->config->paths->root}]";
file_put_contents($file, $data);
}
}
protected function ___buildLoginForm() {
$this->nameField = $this->modules->get('InputfieldText');
$this->nameField->set('label', $this->_('Username')); // Login form: username field label
$this->nameField->attr('id+name', 'login_name');
$this->nameField->attr('class', $this->className() . 'Name');
$this->passField = $this->modules->get('InputfieldText');
$this->passField->set('label', $this->_('Password')); // Login form: password field label
$this->passField->attr('id+name', 'login_pass');
$this->passField->attr('type', 'password');
$this->passField->attr('class', $this->className() . 'Pass');
$this->submitField = $this->modules->get('InputfieldSubmit');
$this->submitField->attr('name', 'login_submit');
$this->submitField->attr('value', $this->_('Login')); // Login form: submit login button
$this->form = $this->modules->get('InputfieldForm');
// we'll retain an ID field in the GET url, if it was there
$this->form->attr('action', "./" . ($this->id ? "?id={$this->id}" : ''));
$this->form->attr('id', $this->className() . 'Form');
$this->form->add($this->nameField);
$this->form->add($this->passField);
$this->form->add($this->submitField);
return $this->form;
}
/**
* Render the login form
*
*/
protected function ___renderLoginForm() {
if(isset($_GET['login'])) {
$this->afterLoginRedirect();
} else {
// note the space after 'Login ' is intentional to separate it from the Login button for translation purposes
$this->setFuel('processHeadline', $this->_('Login ')); // Headline for login form page
$this->passField->attr('value', '');
$out = $this->form->render();
$links = '';
if($this->allowForgot) {
$links .= "<div><a href='./?forgot=1'><i class='fa fa-question-circle'></i> " . $this->_("Forgot your password?") . "</a></div>"; // Forgot password link text
}
$home = $this->pages->get("/");
$links .= "<div><a href='{$home->url}'><i class='fa fa-home'></i> {$home->title}</a></div>";
if($links) $out .= $links;
return $out;
}
}
/**
* Log the user out
*
*/
public function ___executeLogout() {
if($this->user->hasPermission('page-edit')) {
$url = $this->config->urls->admin;
$this->message($this->_("You have logged out"));
} else {
$url = $this->config->urls->root;
}
$this->session->logout();
$this->session->redirect($url);
}
/**
* Redirect to admin root after login
*
* Called only if the login request originated on the actual login page.
*
*/
protected function ___afterLoginRedirect() {
$this->session->redirect($this->pages->get($this->config->adminRootPageID)->url);
}
}