1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class cPasswordRequest {
31:
32: 33: 34: 35: 36:
37: protected $_db;
38:
39: 40: 41: 42: 43:
44: protected $_cfg;
45:
46: 47: 48: 49: 50:
51: protected $_tpl;
52:
53: 54: 55: 56: 57:
58: protected $_username;
59:
60: 61: 62: 63: 64:
65: protected $_email;
66:
67: 68: 69: 70: 71:
72: protected $_reloadTime;
73:
74: 75: 76: 77: 78:
79: protected $_passLength;
80:
81: 82: 83: 84: 85: 86:
87: protected $_isEnabled;
88:
89: 90: 91: 92: 93:
94: protected $_sendermail;
95:
96: 97: 98: 99: 100:
101: protected $_sendername;
102:
103: 104: 105: 106: 107:
108: protected $_mailhost;
109:
110: 111: 112: 113: 114: 115:
116: public function __construct($db, $cfg) {
117:
118: if (!is_object($db)) {
119: $this->_db = cRegistry::getDb();
120: } else {
121: $this->_db = $db;
122: }
123:
124:
125: $this->_cfg = $cfg;
126: $this->_tpl = new cTemplate();
127: $this->_username = '';
128: $this->_email = '';
129:
130:
131: $this->_reloadTime = 30;
132:
133:
134: $this->_passLength = 14;
135:
136:
137:
138: $sEnable = getSystemProperty('pw_request', 'enable');
139: if ($sEnable == 'false') {
140: $this->_isEnabled = false;
141: } else {
142: $this->_isEnabled = true;
143: }
144:
145:
146:
147: $sendermail = getSystemProperty('system', 'mail_sender');
148: if (preg_match("/^.+@.+\.([A-Za-z0-9\-_]{1,20})$/", $sendermail)) {
149: $this->_sendermail = $sendermail;
150: } else {
151: $this->_sendermail = 'noreply@contenido-passwordservice.de';
152: }
153:
154:
155: $sendername = getSystemProperty('system', 'mail_sender_name');
156: if ($sendername != '') {
157: $this->_sendername = $sendername;
158: } else {
159: $this->_sendername = 'CONTENIDO Backend';
160: }
161:
162:
163:
164: $mailhost = getSystemProperty('system', 'mail_host');
165: if ($mailhost != '') {
166: $this->_mailhost = $mailhost;
167: } else {
168: $this->_mailhost = 'localhost';
169: }
170: }
171:
172: 173: 174: 175: 176: 177: 178:
179: public function renderForm($return = 0) {
180:
181: if (!$this->_isEnabled) {
182: return;
183: }
184:
185: $message = '';
186:
187:
188:
189: if (isset($_POST['action']) && $_POST['action'] == 'request_pw') {
190:
191:
192: $this->_username = $_POST['request_username'];
193:
194: $message = $this->_handleNewPassword();
195:
196: $this->_tpl->set('s', 'JS_CALL', 'showRequestLayer();');
197: } else {
198:
199: $this->_tpl->set('s', 'JS_CALL', '');
200: }
201:
202:
203: $form = new cHTMLForm('request_pw', 'index.php', 'post');
204:
205:
206: $inputUsername = new cHTMLTextbox('request_username', stripslashes($_POST['request_username']), '', '', 'request_username');
207: $inputUsername->setStyle('width:215px;');
208:
209:
210: $form->setVar('action', 'request_pw');
211: $form->setVar('belang', $GLOBALS['belang']);
212:
213:
214: $form->setContent('<input class="password_request_input" type="image" src="images/submit.gif" alt="' . i18n('Submit') . '" title="' . i18n('Submit') . '">' . $inputUsername->render());
215: $this->_tpl->set('s', 'FORM', $form->render());
216: $this->_tpl->set('s', 'MESSAGE', $message);
217: $this->_tpl->set('s', 'LABEL', i18n('Please enter your login') . ':');
218:
219:
220: if ($return) {
221: return $this->_tpl->generate($this->_cfg['path']['contenido'] . $this->_cfg['path']['templates'] . $this->_cfg['templates']['request_password'], 1);
222: } else {
223: return $this->_tpl->generate($this->_cfg['path']['contenido'] . $this->_cfg['path']['templates'] . $this->_cfg['templates']['request_password']);
224: }
225: }
226:
227: 228: 229: 230:
231: protected function _handleNewPassword() {
232:
233: $message = '';
234: $this->_username = stripslashes($this->_username);
235:
236:
237:
238: $sql = "SELECT username, last_pw_request, email FROM " . $this->_cfg['tab']['user'] . "
239: WHERE username = '" . $this->_db->escape($this->_username) . "'
240: AND (valid_from <= NOW() OR valid_from = '0000-00-00' OR valid_from IS NULL)
241: AND (valid_to >= NOW() OR valid_to = '0000-00-00' OR valid_to IS NULL)";
242:
243: $this->_db->query($sql);
244: if ($this->_db->nextRecord() && md5($this->_username) == md5($this->_db->f('username'))) {
245:
246: $isAllowed = true;
247: $lastPwRequest = $this->_db->f('last_pw_request');
248:
249: $this->_email = $this->_db->f('email');
250:
251:
252: if (preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $lastPwRequest, $aMatches)) {
253: $lastRequest = mktime($aMatches[4], $aMatches[5], $aMatches[6], $aMatches[2], $aMatches[3], $aMatches[1]);
254:
255:
256: if ((time() - $lastRequest) < (60 * $this->_reloadTime)) {
257:
258:
259: $isAllowed = false;
260: $message = sprintf(i18n('Password requests are allowed every %s minutes.'), $this->_reloadTime);
261: }
262: }
263:
264:
265:
266:
267: if ((!preg_match("/^.+@.+\.([A-Za-z0-9\-_]{1,20})$/", $this->_email) || $this->_email == 'sysadmin@IhreSite.de' || $this->_email == 'admin_kunde@IhreSite.de') && $isAllowed) {
268: $isAllowed = false;
269:
270:
271:
272: $message = i18n('No matching data found. Please contact your systemadministrator.');
273: }
274:
275:
276:
277: if ($isAllowed) {
278: $this->_setNewPassword();
279: $message = i18n('New password was submitted to your e-mail address.');
280: } else {
281: sleep(5);
282: }
283: } else {
284:
285:
286: $message = i18n('No matching data found. Please contact your systemadministrator.');
287: sleep(5);
288: }
289: return $message;
290: }
291:
292: 293: 294:
295: protected function _setNewPassword() {
296:
297: $password = $this->_generatePassword();
298:
299:
300: $sql = "SELECT salt FROM " . $this->_cfg['tab']['user'] . " WHERE username = '" . $this->_username . "'";
301: $this->_db->query($sql);
302: $this->_db->nextRecord();
303:
304:
305: $password_hash = hash("sha256", md5($password) . $this->_db->f("salt"));
306:
307:
308: $sql = "UPDATE " . $this->_cfg['tab']['user'] . "
309: SET last_pw_request = '" . date('Y-m-d H:i:s') . "',
310: tmp_pw_request = '" . $password_hash . "',
311: password = '" . $password_hash . "'
312: WHERE username = '" . $this->_username . "'";
313: $this->_db->query($sql);
314:
315:
316: $this->_submitMail($password);
317: }
318:
319: 320: 321: 322: 323:
324: protected function _submitMail($password) {
325: $password = (string) $password;
326:
327:
328: $mailBody = sprintf(i18n("Dear CONTENIDO-User %s,\n\nYour password to log in Content Management System CONTENIDO is: %s\n\nBest regards\n\nYour CONTENIDO sysadmin"), $this->_username, $password);
329:
330: $mailer = new cMailer();
331: $from = array(
332: $this->_sendermail => $this->_sendername
333: );
334: $subject = utf8_encode(conHtmlEntityDecode(stripslashes(i18n('Your new password for CONTENIDO Backend'))));
335: $body = utf8_encode(conHtmlEntityDecode($mailBody));
336: $mailer->sendMail($from, $this->_email, $subject, $body);
337: }
338:
339: 340: 341: 342: 343:
344: protected function _generatePassword() {
345:
346: $chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghjkmnopqrstuvwxyz123456789";
347:
348: $password = "";
349:
350:
351: for ($i = 0; $i < $this->_passLength; $i++) {
352: $password .= $chars[rand(0, strlen($chars))];
353: }
354:
355: return $password;
356: }
357:
358: }
359: