ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilPasswordAssistanceGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
17  protected $ctrl;
18 
22  protected $lng;
23 
27  protected $rbacreview;
28 
32  protected $tpl;
33 
37  protected $settings;
38 
42  protected $ilias;
43 
47  protected $https;
48 
52  public function __construct()
53  {
64 
65  $this->ctrl = $ilCtrl;
66  $this->lng = $lng;
67  $this->rbacreview = $rbacreview;
68  $this->tpl = $tpl;
69  $this->settings = $ilSetting;
70  $this->ilias = $ilias;
71  $this->https = $https;
72  }
73 
77  public function executeCommand()
78  {
79  // check hack attempts
80  if(!$this->settings->get('password_assistance')) // || AUTH_DEFAULT != AUTH_LOCAL)
81  {
82  //
83  #if(empty($_SESSION['AccountId']) && $_SESSION['AccountId'] !== false)
84  {
85  #$this->ilias->error_obj->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->WARNING);
86  }
87  }
88 
89  // check correct setup
90  if(!$this->settings->get('setup_ok'))
91  {
92  die('Setup is not completed. Please run setup routine again.');
93  }
94 
95  // Change the language, if necessary.
96  // And load the 'pwassist' language module
97  $lang = $_GET['lang'];
98  if($lang != null && $lang != '' && $this->lng->getLangKey() != $lang)
99  {
100  $lng = new ilLanguage($lang);
101  }
102  $this->lng->loadLanguageModule('pwassist');
103 
104  $cmd = $this->ctrl->getCmd();
105  $next_class = $this->ctrl->getNextClass($this);
106 
107  switch($next_class)
108  {
109  default:
110  if($cmd != '')
111  {
112  return $this->$cmd();
113  }
114  else
115  {
116  if(!empty($_GET['key']))
117  {
118  $this->showAssignPasswordForm();
119  }
120  else
121  {
122  $this->showAssistanceForm();
123  }
124  }
125  break;
126  }
127  }
128 
132  protected function getAssistanceForm()
133  {
134  require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
135  $form = new ilPropertyFormGUI();
136 
137  $form->setFormAction($this->ctrl->getFormAction($this, 'submitAssistanceForm'));
138  $form->setTarget('_parent');
139 
140  $username = new ilTextInputGUI($this->lng->txt('username'), 'username');
141  $username->setRequired(true);
142  $form->addItem($username);
143 
144  $email = new ilTextInputGUI($this->lng->txt('email'), 'email');
145  $email->setRequired(true);
146  $form->addItem($email);
147 
148  $form->addCommandButton('submitAssistanceForm', $this->lng->txt('submit'));
149 
150  return $form;
151  }
152 
156  public function showAssistanceForm(ilPropertyFormGUI $form = null)
157  {
158  ilStartUpGUI::initStartUpTemplate('tpl.pwassist_assistance.html', true);
159  $this->tpl->setVariable('IMG_PAGEHEADLINE', ilUtil::getImagePath('icon_auth.svg'));
160  $this->tpl->setVariable('TXT_PAGEHEADLINE', $this->lng->txt('password_assistance'));
161 
162  $this->tpl->setVariable
163  (
164  'TXT_ENTER_USERNAME_AND_EMAIL',
165  str_replace
166  (
167  "\\n", '<br />',
168  sprintf
169  (
170  $this->lng->txt('pwassist_enter_username_and_email'),
171  '<a href="mailto:' . ilUtil::prepareFormOutput($this->settings->get('admin_email')) . '">' . ilUtil::prepareFormOutput($this->settings->get('admin_email')) . '</a>'
172  )
173  )
174  );
175 
176  if(!$form)
177  {
178  $form = $this->getAssistanceForm();
179  }
180  $this->tpl->setVariable('FORM', $form->getHTML());
181  $this->tpl->show();
182  }
183 
195  public function submitAssistanceForm()
196  {
197  $form = $this->getAssistanceForm();
198  if(!$form->checkInput())
199  {
200  $form->setValuesByPost();
201  $this->showAssistanceForm($form);
202  return;
203  }
204 
205  $username = $form->getInput('username');
206  $email = $form->getInput('email');
207 
208  $userObj = null;
209  $userid = ilObjUser::getUserIdByLogin($username);
210  $txt_key = 'pwassist_invalid_username_or_email';
211  if($userid != 0)
212  {
213  $userObj = new ilObjUser($userid);
214  if(strcasecmp($userObj->getEmail(), $email) != 0)
215  {
216  $userObj = null;
217  }
218  elseif(!strlen($email))
219  {
220  $userObj = null;
221  $txt_key = 'pwassist_no_email_found';
222  }
223  else if(
224  $userObj->getAuthMode(true) != AUTH_LOCAL ||
225  ($userObj->getAuthMode(true) == AUTH_DEFAULT && AUTH_DEFAULT != AUTH_LOCAL)
226  )
227  {
228  $userObj = null;
229  $txt_key = 'pwassist_invalid_auth_mode';
230  }
231  }
232 
233  // No matching user object found?
234  // Show the password assistance form again, and display an error message.
235  if($userObj == null)
236  {
237  ilUtil::sendFailure(str_replace("\\n", '', $this->lng->txt($txt_key)));
238  $form->setValuesByPost();
239  $this->showAssistanceForm($form);
240  }
241  else
242  {
243  // Matching user object found?
244  // Check if the user is permitted to use the password assistance function,
245  // and then send a password assistance mail to the email address.
246  // FIXME: Extend this if-statement to check whether the user
247  // has the permission to use the password assistance function.
248  // The anonymous user and users who are system administrators are
249  // not allowed to use this feature
250  if(
251  $this->rbacreview->isAssigned($userObj->getId(), ANONYMOUS_ROLE_ID) ||
252  $this->rbacreview->isAssigned($userObj->getId(), SYSTEM_ROLE_ID)
253  )
254  {
255  ilUtil::sendFailure(str_replace("\\n", '', $this->lng->txt('pwassist_not_permitted')));
256  $form->setValuesByPost();
257  $this->showAssistanceForm($form);
258  }
259  else
260  {
261  $this->sendPasswordAssistanceMail($userObj);
262  $this->showMessageForm(sprintf($this->lng->txt('pwassist_mail_sent'), $email));
263  }
264  }
265  }
266 
278  public function sendPasswordAssistanceMail(ilObjUser $userObj)
279  {
280  global $DIC;
281 
282  require_once 'Services/Mail/classes/class.ilMailbox.php';
283  require_once 'Services/Mail/classes/class.ilMail.php';
284  require_once 'Services/Mail/classes/class.ilMimeMail.php';
285  require_once 'include/inc.pwassist_session_handler.php';
286 
287  // Check if we need to create a new session
288  $pwassist_session = db_pwassist_session_find($userObj->getId());
289  if(
290  count($pwassist_session) == 0 ||
291  $pwassist_session['expires'] < time() ||
292  true // comment by mjansen: wtf? :-)
293  )
294  {
295  // Create a new session id
296  // #9700 - this didn't do anything before?!
297  // db_set_save_handler();
298  session_start();
299  $pwassist_session['pwassist_id'] = db_pwassist_create_id();
300  session_destroy();
302  $pwassist_session['pwassist_id'],
303  3600,
304  $userObj->getId()
305  );
306  }
307  $protocol = $this->https->isDetected() ? 'https://' : 'http://';
308  // Compose the mail
309  $server_url = $protocol . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')) . '/';
310  // XXX - Werner Randelshofer - Insert code here to dynamically get the
311  // the delimiter. For URL's that are sent by e-mail to a user,
312  // it is best to use semicolons as parameter delimiter
313  $delimiter = '&';
314  $pwassist_url = $protocol . $_SERVER['HTTP_HOST']
315  . str_replace('ilias.php', 'pwassist.php', $_SERVER['PHP_SELF'])
316  . '?client_id=' . $this->ilias->getClientId()
317  . $delimiter . 'lang=' . $this->lng->getLangKey()
318  . $delimiter . 'key=' . $pwassist_session['pwassist_id'];
319  $alternative_pwassist_url = $protocol . $_SERVER['HTTP_HOST']
320  . str_replace('ilias.php', 'pwassist.php', $_SERVER['PHP_SELF'])
321  . '?client_id=' . $this->ilias->getClientId()
322  . $delimiter . 'lang=' . $this->lng->getLangKey()
323  . $delimiter . 'key=' . $pwassist_session['pwassist_id'];
324 
325  $contact_address = ilMail::getIliasMailerAddress();
326 
327  $mm = new ilMimeMail();
328  $mm->Subject($this->lng->txt('pwassist_mail_subject'));
329  $mm->From($contact_address);
330  $mm->To($userObj->getEmail());
331  $mm->Body
332  (
333  str_replace
334  (
335  array("\\n", "\\t"),
336  array("\n", "\t"),
337  sprintf
338  (
339  $this->lng->txt('pwassist_mail_body'),
340  $pwassist_url,
341  $server_url,
342  $_SERVER['REMOTE_ADDR'],
343  $userObj->getLogin(),
344  'mailto:' . $DIC['ilSetting']->get("admin_email"),
345  $alternative_pwassist_url
346  )
347  )
348  );
349  $mm->Send();
350  }
351 
356  protected function getAssignPasswordForm($pwassist_id)
357  {
358  require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
359  $form = new ilPropertyFormGUI();
360 
361  $form->setFormAction($this->ctrl->getFormAction($this, 'submitAssignPasswordForm'));
362  $form->setTarget('_parent');
363 
364  $username = new ilTextInputGUI($this->lng->txt('username'), 'username');
365  $username->setRequired(true);
366  $form->addItem($username);
367 
368  $password = new ilPasswordInputGUI($this->lng->txt('password'), 'password');
369  $password->setRequired(true);
370  $form->addItem($password);
371 
372  $key = new ilHiddenInputGUI('key');
373  $key->setValue($pwassist_id);
374  $form->addItem($key);
375 
376  $form->addCommandButton('submitAssignPasswordForm', $this->lng->txt('submit'));
377 
378  return $form;
379  }
380 
393  public function showAssignPasswordForm(ilPropertyFormGUI $form = null, $pwassist_id = '')
394  {
395  require_once 'include/inc.pwassist_session_handler.php';
396  require_once 'Services/Language/classes/class.ilLanguage.php';
397 
398  // Retrieve form data
399  if(!$pwassist_id)
400  {
401  $pwassist_id = $_GET['key'];
402  }
403 
404  // Retrieve the session, and check if it is valid
405  $pwassist_session = db_pwassist_session_read($pwassist_id);
406  if(
407  count($pwassist_session) == 0 ||
408  $pwassist_session['expires'] < time()
409  )
410  {
411  ilUtil::sendFailure($this->lng->txt('pwassist_session_expired'));
412  $this->showAssistanceForm(null);
413  }
414  else
415  {
416  ilStartUpGUI::initStartUpTemplate('tpl.pwassist_assignpassword.html', true);
417  $this->tpl->setVariable('IMG_PAGEHEADLINE', ilUtil::getImagePath('icon_auth.svg'));
418  $this->tpl->setVariable('TXT_PAGEHEADLINE', $this->lng->txt('password_assistance'));
419 
420  $this->tpl->setVariable('TXT_ENTER_USERNAME_AND_NEW_PASSWORD', $this->lng->txt('pwassist_enter_username_and_new_password'));
421 
422  if(!$form)
423  {
424  $form = $this->getAssignPasswordForm($pwassist_id);
425  }
426  $this->tpl->setVariable('FORM', $form->getHTML());
427  $this->tpl->show();
428  }
429  }
430 
447  public function submitAssignPasswordForm()
448  {
449 
450  require_once 'include/inc.pwassist_session_handler.php';
451 
452  // We need to fetch this before form instantiation
453  $pwassist_id = ilUtil::stripSlashes($_POST['key']);
454 
455  $form = $this->getAssignPasswordForm($pwassist_id);
456  if(!$form->checkInput())
457  {
458  $form->setValuesByPost();
459  $this->showAssistanceForm($form);
460  return;
461  }
462 
463  $username = $form->getInput('username');
464  $password = $form->getInput('password');
465  $pwassist_id = $form->getInput('key');
466 
467  // Retrieve the session
468  $pwassist_session = db_pwassist_session_read($pwassist_id);
469 
470  if(
471  count($pwassist_session) == 0 ||
472  $pwassist_session['expires'] < time()
473  )
474  {
475  ilUtil::sendFailure(str_replace("\\n", '', $this->lng->txt('pwassist_session_expired')));
476  $form->setValuesByPost();
477  $this->showAssistanceForm($form);
478  return;
479  }
480  else
481  {
482  $is_successful = true;
483  $message = '';
484 
485  $userObj = new ilObjUser($pwassist_session['user_id']);
486  if($userObj == null)
487  {
488  $message = $this->lng->txt('user_does_not_exist');
489  $is_successful = false;
490  }
491 
492  // check if the username entered by the user matches the
493  // one of the user object.
494  if($is_successful && strcasecmp($userObj->getLogin(), $username) != 0)
495  {
496  $message = $this->lng->txt('pwassist_login_not_match');
497  $is_successful = false;
498  }
499 
500  $error_lng_var = '';
501  if(!ilUtil::isPasswordValidForUserContext($password, $userObj, $error_lng_var))
502  {
503  $message = $this->lng->txt($error_lng_var);
504  $is_successful = false;
505  }
506 
507  // End of validation
508  // If the validation was successful, we change the password of the
509  // user.
510  // ------------------
511  if($is_successful)
512  {
513  $is_successful = $userObj->resetPassword($password, $password);
514  if(!$is_successful)
515  {
516  $message = $this->lng->txt('passwd_invalid');
517  }
518  }
519 
520  // If we are successful so far, we update the user object.
521  // ------------------
522  if($is_successful)
523  {
524  $userObj->update();
525  }
526 
527  // If we are successful, we destroy the password assistance
528  // session and redirect to the login page.
529  // Else we display the form again along with an error message.
530  // ------------------
531  if($is_successful)
532  {
533  db_pwassist_session_destroy($pwassist_id);
534  $this->showMessageForm(sprintf($this->lng->txt('pwassist_password_assigned'), $username));
535  }
536  else
537  {
538  ilUtil::sendFailure(str_replace("\\n", '', $message));
539  $form->setValuesByPost();
540  $this->showAssignPasswordForm($form, $pwassist_id);
541  }
542  }
543  }
544 
548  protected function getUsernameAssistanceForm()
549  {
550  require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
551  $form = new ilPropertyFormGUI();
552 
553  $form->setFormAction($this->ctrl->getFormAction($this, 'submitUsernameAssistanceForm'));
554  $form->setTarget('_parent');
555 
556  $email = new ilTextInputGUI($this->lng->txt('email'), 'email');
557  $email->setRequired(true);
558  $form->addItem($email);
559 
560  $form->addCommandButton('submitUsernameAssistanceForm', $this->lng->txt('submit'));
561 
562  return $form;
563  }
564 
575  public function showUsernameAssistanceForm(ilPropertyFormGUI $form = null)
576  {
577  ilStartUpGUI::initStartUpTemplate('tpl.pwassist_username_assistance.html', true);
578  $this->tpl->setVariable('IMG_PAGEHEADLINE', ilUtil::getImagePath('icon_auth.svg'));
579  $this->tpl->setVariable('TXT_PAGEHEADLINE', $this->lng->txt('password_assistance'));
580 
581  $this->tpl->setVariable
582  (
583  'TXT_ENTER_USERNAME_AND_EMAIL',
584  str_replace
585  (
586  "\\n", '<br />',
587  sprintf
588  (
589  $this->lng->txt('pwassist_enter_email'),
590  '<a href="mailto:' . ilUtil::prepareFormOutput($this->settings->get('admin_email')) . '">' . ilUtil::prepareFormOutput($this->settings->get('admin_email')) . '</a>'
591  )
592  )
593  );
594 
595  if(!$form)
596  {
597  $form = $this->getUsernameAssistanceForm();
598  }
599  $this->tpl->setVariable('FORM', $form->getHTML());
600  $this->tpl->show();
601  }
602 
615  {
616  require_once 'Services/User/classes/class.ilObjUser.php';
617  require_once 'Services/Utilities/classes/class.ilUtil.php';
618 
619  $form = $this->getUsernameAssistanceForm();
620  if(!$form->checkInput())
621  {
622  $form->setValuesByPost();
623  $this->showUsernameAssistanceForm($form);
624  return;
625  }
626 
627  // Retrieve form data
628  $email = $form->getInput('email');
629 
630  // Retrieve a user object with matching user name and email address.
631  $logins = ilObjUser::_getUserIdsByEmail($email);
632 
633  // No matching user object found?
634  // Show the password assistance form again, and display an error message.
635  if(!is_array($logins) || count($logins) < 1)
636  {
637  ilUtil::sendFailure(str_replace("\\n", '', $this->lng->txt('pwassist_invalid_email')));
638  $form->setValuesByPost();
639  $this->showUsernameAssistanceForm($form);
640  }
641  else
642  {
643  // Matching user object found?
644  // Check if the user is permitted to use the password assistance function,
645  // and then send a password assistance mail to the email address.
646 
647  // FIXME: Extend this if-statement to check whether the user
648  // has the permission to use the password assistance function.
649  // The anonymous user and users who are system administrators are
650  // not allowed to use this feature
651  /* if ($rbacreview->isAssigned($userObj->getID, ANONYMOUS_ROLE_ID)
652  || $rbacreview->isAssigned($userObj->getID, SYSTEM_ROLE_ID)
653  )
654  {
655  $this->showAssistanceForm
656  (
657  $lng->txt("pwassist_not_permitted"),
658  $username,
659  $email
660  );
661  }
662  else */
663  {
664  $this->sendUsernameAssistanceMail($email, $logins);
665  $this->showMessageForm(sprintf($this->lng->txt('pwassist_mail_sent'), $email));
666  }
667  }
668  }
669 
682  public function sendUsernameAssistanceMail($email, array $logins)
683  {
684  global $DIC;
685 
686  require_once 'Services/Mail/classes/class.ilMailbox.php';
687  require_once 'Services/Mail/classes/class.ilMail.php';
688  require_once 'Services/Mail/classes/class.ilMimeMail.php';
689  require_once 'include/inc.pwassist_session_handler.php';
690 
691  $protocol = $this->https->isDetected() ? 'https://' : 'http://';
692 
693  $server_url = $protocol . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')) . '/';
694  $login_url = $server_url . 'pwassist.php' . '?client_id=' . $this->ilias->getClientId() . '&lang=' . $this->lng->getLangKey();
695  $contact_address = ilMail::getIliasMailerAddress();
696 
697  $mm = new ilMimeMail();
698  $mm->Subject($this->lng->txt('pwassist_mail_subject'));
699  $mm->From($contact_address);
700  $mm->To($email);
701  $mm->Body
702  (
703  str_replace
704  (
705  array("\\n", "\\t"),
706  array("\n", "\t"),
707  sprintf
708  (
709  $this->lng->txt('pwassist_username_mail_body'),
710  join($logins, ",\n"),
711  $server_url,
712  $_SERVER['REMOTE_ADDR'],
713  $email,
714  'mailto:' . $DIC['ilSetting']->get("admin_email"),
715  $login_url
716  )
717  )
718  );
719  $mm->Send();
720  }
721 
726  public function showMessageForm($text)
727  {
728  ilStartUpGUI::initStartUpTemplate('tpl.pwassist_message.html', true);
729  $this->tpl->setVariable('TXT_PAGEHEADLINE', $this->lng->txt('password_assistance'));
730  $this->tpl->setVariable('IMG_PAGEHEADLINE', ilUtil::getImagePath('icon_auth.svg'));
731 
732  $this->tpl->setVariable('TXT_TEXT', str_replace("\\n", '<br />', $text));
733  $this->tpl->show();
734  }
735 }
static getUserIdByLogin($a_login)
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
getLogin()
get login / username public
db_pwassist_session_destroy($pwassist_id)
destroy session
sendPasswordAssistanceMail(ilObjUser $userObj)
Creates (or reuses) a password assistance session, and sends a password assistance mail to the specif...
db_pwassist_session_read($pwassist_id)
This class represents a property form user interface.
$_GET["client_id"]
static isPasswordValidForUserContext($clear_text_password, $user, &$error_language_variable=null)
$cmd
Definition: sahs_server.php:35
global $ilCtrl
Definition: ilias.php:18
showAssignPasswordForm(ilPropertyFormGUI $form=null, $pwassist_id='')
Assign password form.
This class represents a hidden form property in a property form.
getEmail()
get email address public
db_pwassist_session_find($user_id)
this class encapsulates the PHP mail() function.
getId()
get object id public
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
submitUsernameAssistanceForm()
Reads the submitted data from the password assistance form.
This class represents a text property in a property form.
This class represents a password property in a property form.
redirection script todo: (a better solution should control the processing via a xml file) ...
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
const AUTH_LOCAL
showMessageForm($text)
This form is used to show a message to the user.
foreach( $name as $i=> $nameSection)( $i==count( $name) - 1)( $nameSection) ?></span ><?php else from https
Definition: header.html.php:3
Create styles array
The data for the language used.
Password assistance facility for users who have forgotten their password or for users for whom no pas...
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
showAssistanceForm(ilPropertyFormGUI $form=null)
submitAssignPasswordForm()
Reads the submitted data from the password assistance form.
settings()
Definition: settings.php:2
global $ilSetting
Definition: privfeed.php:17
submitAssistanceForm()
Reads the submitted data from the password assistance form.
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
$text
sendUsernameAssistanceMail($email, array $logins)
Creates (or reuses) a password assistance session, and sends a password assistance mail to the specif...
language handling
static _getUserIdsByEmail($a_email)
STATIC METHOD get all user_ids of an email address.
global $DIC
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
db_pwassist_session_write($pwassist_id, $maxlifetime, $user_id)
Writes serialized session data to the database.
$_POST["username"]
setRequired($a_required)
Set Required.
showUsernameAssistanceForm(ilPropertyFormGUI $form=null)
Shows the password assistance form.