-
Notifications
You must be signed in to change notification settings - Fork 1
/
enhance_password.php
194 lines (138 loc) · 6.49 KB
/
enhance_password.php
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
<?php
/*
Developer: SS88 LLC
Developer Website: https://ss88.us
Licensed under MIT License
*/
class enhance_password extends rcube_plugin {
private $rc;
public $task = '?(?!logout).*';
public $noframe = true;
public $noajax = true;
function init() {
$this->rc = rcmail::get_instance();
$this->load_config();
$this->add_texts('localization/');
if ($this->rc->task == 'settings') {
$this->add_hook('settings_actions', [$this, 'settings_actions']);
$this->register_action('plugin.enhance_password', [$this, 'password_init']);
$this->register_action('plugin.enhance_password-save', [$this, 'password_save']);
}
}
function settings_actions($args) {
$args['actions'][] = [
'action' => 'plugin.enhance_password',
'class' => 'password',
'label' => 'password',
'title' => 'password',
'domain' => 'password',
];
return $args;
}
function password_init() {
$this->register_handler('plugin.body', [$this, 'show_password_form']);
$this->rc->output->send('plugin');
}
function show_password_form() {
$this->rc->output->add_label(
'enhance_password.nopassword',
'enhance_password.passwordsdonotmatch',
'enhance_password.noapiset',
'enhnace_password.passwordvalidation'
);
$submit_button = $this->rc->output->button([
'command' => 'plugin.enhance_password-save',
'class' => 'button mainaction submit',
'label' => 'save',
]);
$form_buttons = html::p(['class' => 'formbuttons footerleft'], $submit_button);
$table = new html_table(['cols' => 2, 'class' => 'propform']);
$field_id = 'newpasswd';
$input_newpasswd = new html_passwordfield([
'name' => '_newpasswd',
'id' => $field_id,
'size' => 20,
'autocomplete' => 'off',
]);
$table->add('title', html::label($field_id, $this->gettext('newpassword')));
$table->add(null, $input_newpasswd->show());
$field_id = 'confpasswd';
$input_confpasswd = new html_passwordfield([
'name' => '_confpasswd',
'id' => $field_id,
'size' => 20,
'autocomplete' => 'off',
]);
$table->add('title', html::label($field_id, $this->gettext('confirmpassword')));
$table->add(null, $input_confpasswd->show());
$this->rc->output->add_gui_object('passform', 'password-form');
$form = $this->rc->output->form_tag([
'id' => 'password-form',
'name' => 'password-form',
'method' => 'post',
'action' => './?_task=settings&_action=plugin.enhance_password-save',
],
$table->show()
);
$this->include_script('change_password.js');
return html::div(['class' => 'box formcontainer scroller'], html::div(['class' => 'boxcontent formcontent'], html::p(NULL, $this->gettext('infotext')) . $form) . $form_buttons);
}
function password_save() {
$this->register_handler('plugin.body', [$this, 'show_password_form']);
$this->rc->output->set_pagetitle($this->gettext('changepassword'));
$orchd_url = $this->rc->config->get('orchd_url');
$orchd_key = $this->rc->config->get('orchd_key');
if (!isset($_POST['_newpasswd']) || !strlen($_POST['_newpasswd'])) {
$this->rc->output->command('display_message', $this->gettext('nopassword'), 'error');
}
elseif(empty($orchd_key) || empty($orchd_url)) {
$this->rc->output->command('display_message', $this->gettext('noapiset'), 'error');
}
else {
$email = $this->rc->get_user_name();
$curpwd = $this->rc->get_user_password();
$newpwd = rcube_utils::get_input_value('_newpasswd', rcube_utils::INPUT_POST, true);
$conpwd = rcube_utils::get_input_value('_confpasswd', rcube_utils::INPUT_POST, true);
if ($conpwd != $newpwd) {
$this->rc->output->command('display_message', $this->gettext('passwordsdonotmatch'), 'error');
}
elseif (strlen($newpwd) < 10 || !preg_match('/[^a-zA-Z\d]/', $newpwd)) {
$this->rc->output->command('display_message', $this->gettext('passwordvalidation'), 'error');
}
else {
$headers = array(
'Address: ' . $email,
'Password: ' . $curpwd,
'Authorization: Bearer ' . $orchd_key,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $orchd_url . '/email-client/password');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['password' => $newpwd]));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$json = json_decode($data, true);
if (!$data) {
$this->rc->output->command('display_message', $this->gettext('internalerror'), 'error');
}
if($httpcode==200) {
rcube::write_log('enhance_password', sprintf('Password changed for user %s (ID: %d)', $email, $this->rc->user->ID));
$_SESSION['password'] = $this->rc->encrypt($newpwd);
$this->rc->output->command('display_message', $this->gettext('saved'), 'confirmation');
}
else {
$err_msg = 'API Error: (Code ' . $httpcode . ') ' . $json['message'];
rcube::write_log('enhance_password', sprintf('Failed to change password for user %s (ID: %d), reason: %s', $email, $this->rc->user->ID, $err_msg));
$this->rc->output->command('display_message', $err_msg, 'error');
}
}
}
$this->rc->overwrite_action('plugin.enhance_password');
$this->rc->output->send('plugin');
}
}
?>