• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

pwassist.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00033 
00034 
00035 // start correct client
00036 // if no client_id is given, default client is loaded (in class.ilias.php)
00037 if (isset($_GET["client_id"]))
00038 {       
00039         setcookie("ilClientId",$_GET["client_id"]);
00040         $_COOKIE["ilClientId"] = $_GET["client_id"];
00041 }
00042 
00043 require_once "include/inc.check_pear.php";
00044 require_once "include/inc.header.php";
00045 
00046 
00047 // catch hack attempts
00048 if (! $ilias->getSetting("password_assistance") || AUTH_DEFAULT != AUTH_LOCAL)
00049 {
00050     if (empty($_SESSION["AccountId"]) and $_SESSION["AccountId"] !== false)
00051     {
00052         $ilias->raiseError($lng->txt("permission_denied"),$ilias->error_obj->WARNING);
00053     }
00054 }
00055 
00056 // check correct setup
00057 if (!$ilias->getSetting("setup_ok"))
00058 {
00059         echo "setup is not completed. Please run setup routine again. (pwassist.php)";
00060         exit();
00061 }
00062 
00063 
00064 // Change the language, if necessary. 
00065 // And load the 'pwassist' language module
00066 $lang = $_GET['lang'];
00067 if ($lang != null && $lang != "" && $lng->getLangKey() != $lang)
00068 {
00069         $lng = new ilLanguage($lang);
00070 }
00071 $lng->loadLanguageModule('pwassist');
00072 
00073 // Determine which function to perform
00074 switch ($_GET["cmd"])
00075 {
00076         case "submitassign":
00077                 submitAssignPasswordForm();
00078                 break;
00079 
00080         case "submitassist":
00081                 submitAssistanceForm();
00082                 break;
00083         
00084         case "submitusernameassign":
00085                 break;
00086                 
00087         case "submitusernameassist":
00088                 submitUsernameAssistanceForm();
00089                 break;
00090                 
00091         case "forgot_username":
00092                 showUsernameAssistanceForm();
00093                 break;
00094         
00095         default :
00096                 if (!empty($_GET["key"])) {
00097                         showAssignPasswordForm();
00098                 } else {
00099                         showAssistanceForm();
00100                 }
00101                 break;
00102 }
00103 
00104 // Logout current session
00105 $ilias->auth->logout();
00106 session_destroy();
00107 
00109 
00110 
00111 /* Shows the password assistance form.
00112  * This form is used to request a password assistance mail from ILIAS.
00113  *
00114  * This form contains the following fields: 
00115  * username 
00116  * email 
00117  *
00118  * When the user submits the form, then this script is invoked with the cmd
00119  * 'submitassist'.
00120  *
00121  * @param message  A message to display on the form.
00122  * @param username The user name to be shown in the form.
00123  * @param email    The e-mail to be shown in the form.
00124  */
00125 function showAssistanceForm($message="", $username="", $email="")
00126 {
00127         global $tpl, $ilias, $lng;
00128         
00129         // Create the form
00130         $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_assistance.html");
00131         $tpl->setVariable("FORMACTION","pwassist.php?cmd=submitassist&lang=".$lng->getLangKey());
00132         $tpl->setVariable("TARGET","target=\"_parent\"");
00133         $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00134         $tpl->setVariable("TXT_MESSAGE", str_replace("\\n","<br>",$message));
00135 
00136         $contact_address = $ilias->getSetting("admin_email");
00137         $tpl->setVariable
00138         (
00139                 "TXT_ENTER_USERNAME_AND_EMAIL", 
00140                 str_replace
00141                 (
00142                         "\\n","<br>",
00143                         sprintf
00144                                 (
00145                                 $lng->txt("pwassist_enter_username_and_email"),
00146                                 "<a href=\"mailto:".$contact_address."\">".$contact_address."</a>"
00147                                 )
00148                 )
00149         );
00150         $tpl->setVariable("TXT_USERNAME", $lng->txt("username"));
00151         $tpl->setVariable("TXT_EMAIL", $lng->txt("email"));
00152         $tpl->setVariable("USERNAME", $username);
00153         $tpl->setVariable("EMAIL", $email);
00154         $tpl->setVariable("TXT_SUBMIT", $lng->txt("submit"));
00155         $tpl->setVariable("BACK", $lng->txt("back"));
00156         $tpl->setVariable("LANG", $lng->getLangKey());
00157 
00158         $tpl->show();
00159 }
00160 
00161 
00162 /* Shows the password assistance form.
00163  * This form is used to request a password assistance mail from ILIAS.
00164  *
00165  * This form contains the following fields: 
00166  * username 
00167  * email 
00168  *
00169  * When the user submits the form, then this script is invoked with the cmd
00170  * 'submitassist'.
00171  *
00172  * @param message  A message to display on the form.
00173  * @param username The user name to be shown in the form.
00174  * @param email    The e-mail to be shown in the form.
00175  */
00176 function showUsernameAssistanceForm($message="", $username="", $email="")
00177 {
00178         global $tpl, $ilias, $lng;
00179         
00180         // Create the form
00181         $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_username_assistance.html");
00182         $tpl->setVariable("FORMACTION","pwassist.php?cmd=submitusernameassist&lang=".$lng->getLangKey());
00183         $tpl->setVariable("TARGET","target=\"_parent\"");
00184         $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00185         $tpl->setVariable("TXT_MESSAGE", str_replace("\\n","<br>",$message));
00186 
00187         $contact_address = $ilias->getSetting("admin_email");
00188         $tpl->setVariable
00189         (
00190                 "TXT_ENTER_USERNAME_AND_EMAIL", 
00191                 str_replace
00192                 (
00193                         "\\n","<br>",
00194                         sprintf
00195                                 (
00196                                 $lng->txt("pwassist_enter_email"),
00197                                 "<a href=\"mailto:".$contact_address."\">".$contact_address."</a>"
00198                                 )
00199                 )
00200         );
00201         $tpl->setVariable("TXT_USERNAME", $lng->txt("username"));
00202         $tpl->setVariable("TXT_EMAIL", $lng->txt("email"));
00203         $tpl->setVariable("USERNAME", $username);
00204         $tpl->setVariable("EMAIL", $email);
00205         $tpl->setVariable("TXT_SUBMIT", $lng->txt("submit"));
00206         $tpl->setVariable("BACK", $lng->txt("back"));
00207         $tpl->setVariable("LANG", $lng->getLangKey());
00208 
00209         $tpl->show();
00210 }
00211 
00224 function submitAssistanceForm()
00225 {
00226         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00227         
00228         require_once "classes/class.ilObjUser.php";
00229         require_once "classes/class.ilUtil.php";
00230         
00231         // Retrieve form data
00232         $username = $_POST["username"];
00233         $email = $_POST["email"];
00234         
00235         // Retrieve a user object with matching user name and email address.
00236         $userObj = null;
00237         $userid = ilObjUser::getUserIdByLogin($username);
00238         if ($userid != 0)
00239         {
00240                 $userObj = new ilObjUser($userid);
00241                 if (strcasecmp($userObj->getEmail(), $email) != 0) 
00242                 {
00243                         $userObj = null;
00244                 }
00245         }
00246         
00247         // No matching user object found?
00248         // Show the password assistance form again, and display an error message.
00249         if ($userObj == null) 
00250         {
00251                 showAssistanceForm
00252                 (
00253                         $lng->txt("pwassist_invalid_username_or_email"),
00254                         $username,
00255                         $email
00256                 );
00257         }
00258 
00259         // Matching user object found?
00260         // Check if the user is permitted to use the password assistance function,
00261         // and then send a password assistance mail to the email address.
00262         else
00263         {
00264                 // FIXME: Extend this if-statement to check whether the user
00265                 // has the permission to use the password assistance function.
00266                 // The anonymous user and users who are system administrators are
00267                 // not allowed to use this feature
00268                 if ($rbacreview->isAssigned($userObj->getID, ANONYMOUS_ROLE_ID)
00269                 || $rbacreview->isAssigned($userObj->getID, SYSTEM_ROLE_ID)
00270                 ) 
00271                 {
00272                         showAssistanceForm
00273                         (
00274                                 $lng->txt("pwassist_not_permitted"),
00275                                 $username,
00276                                 $email
00277                         );
00278                 }
00279                 else
00280                 {
00281                         sendPasswordAssistanceMail($userObj);
00282                         showMessageForm
00283                         (
00284                                 null,
00285                                 sprintf
00286                                 (
00287                                         $lng->txt("pwassist_mail_sent"),
00288                                         $email
00289                                 )
00290                         );
00291                 }
00292         }
00293 }
00294 
00307 function submitUsernameAssistanceForm()
00308 {
00309         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00310         
00311         require_once "classes/class.ilObjUser.php";
00312         require_once "classes/class.ilUtil.php";
00313         
00314         // Retrieve form data
00315         $email = $_POST["email"];
00316         
00317         // Retrieve a user object with matching user name and email address.
00318         $logins = ilObjUser::_getUserIdsByEmail($email);
00319         
00320         // No matching user object found?
00321         // Show the password assistance form again, and display an error message.
00322         if (count($logins)< 1)  
00323         {
00324                 showUsernameAssistanceForm
00325                 (
00326                         $lng->txt("pwassist_invalid_email"),
00327                         "",
00328                         $email
00329                 );
00330         }
00331 
00332         // Matching user object found?
00333         // Check if the user is permitted to use the password assistance function,
00334         // and then send a password assistance mail to the email address.
00335         else
00336         {
00337                 // FIXME: Extend this if-statement to check whether the user
00338                 // has the permission to use the password assistance function.
00339                 // The anonymous user and users who are system administrators are
00340                 // not allowed to use this feature
00341 /*              if ($rbacreview->isAssigned($userObj->getID, ANONYMOUS_ROLE_ID)
00342                 || $rbacreview->isAssigned($userObj->getID, SYSTEM_ROLE_ID)
00343                 ) 
00344                 {
00345                         showAssistanceForm
00346                         (
00347                                 $lng->txt("pwassist_not_permitted"),
00348                                 $username,
00349                                 $email
00350                         );
00351                 } 
00352                 else */
00353                 {
00354                         sendUsernameAssistanceMail($email, $logins);
00355                         showMessageForm
00356                         (
00357                                 null,
00358                                 sprintf
00359                                 (
00360                                         $lng->txt("pwassist_mail_sent"),
00361                                         $email
00362                                 )
00363                         );
00364                 }
00365         }
00366 }
00367 
00381 function sendPasswordAssistanceMail($userObj)
00382 {
00383         global $lng, $ilias;
00384 
00385         include_once "classes/class.ilMailbox.php";
00386         include_once "classes/class.ilMimeMail.php";
00387         require_once "include/inc.pwassist_session_handler.php";
00388         
00389 
00390         // Check if we need to create a new session
00391         $pwassist_session = db_pwassist_session_find($userObj->getId());
00392         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00393         {
00394                 // Create a new session id
00395                 $pwassist_session["pwassist_id"] = db_pwassist_create_id();
00396                 
00397                 db_pwassist_session_write(
00398                         $pwassist_session["pwassist_id"],
00399                         3600, 
00400                         $userObj->getId()
00401                 );
00402         }
00403         
00404         // Compose the mail
00405         $server_url='http://'.$_SERVER['HTTP_HOST'].
00406                 substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')).
00407                 '/';
00408         $pwassist_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
00409         ."?client_id=".$ilias->getClientId()
00410         ."&lang=".$lng->getLangKey()
00411         ."&key=".$pwassist_session["pwassist_id"];
00412         $contact_address=$ilias->getSetting("admin_email");
00413 
00414         $mm = new ilMimeMail();
00415         $mm->Subject($lng->txt("pwassist_mail_subject"));
00416         $mm->From($contact_address);
00417         $mm->To($userObj->getEmail());
00418         
00419         $mm->Body
00420         (
00421                 str_replace
00422                 (
00423                         array("\\n","\\t"),
00424                         array("\n","\t"),
00425                         sprintf
00426                         (
00427                         $lng->txt("pwassist_mail_body"),
00428                         $pwassist_url,
00429                         $server_url,
00430                         $_SERVER['REMOTE_ADDR'],
00431                         $userObj->getLogin(),
00432                         'mailto:'.$contact_address
00433                         )
00434                 )
00435         );
00436         
00437         $mm->Send();
00438 }
00439 
00440 
00454 function sendUsernameAssistanceMail($email, $logins)
00455 {
00456         global $lng, $ilias;
00457 
00458         include_once "classes/class.ilMailbox.php";
00459         include_once "classes/class.ilMimeMail.php";
00460         require_once "include/inc.pwassist_session_handler.php";
00461         
00462 
00463         // Compose the mail
00464         $server_url='http://'.$_SERVER['HTTP_HOST'].
00465                 substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')).
00466                 '/';
00467         $login_url=$server_url."pwassist.php"
00468                                 ."?client_id=".$ilias->getClientId()
00469                                 ."&lang=".$lng->getLangKey();
00470         
00471         $contact_address=$ilias->getSetting("admin_email");
00472 
00473         $mm = new ilMimeMail();
00474         $mm->Subject($lng->txt("pwassist_mail_subject"));
00475         $mm->From($contact_address);
00476         $mm->To($email);
00477         
00478         $mm->Body
00479         (
00480                 str_replace
00481                 (
00482                         array("\\n","\\t"),
00483                         array("\n","\t"),
00484                         sprintf
00485                         (
00486                                         $lng->txt("pwassist_username_mail_body"),
00487                                         join ($logins,",\n"), 
00488                                         $server_url, 
00489                                         $_SERVER['REMOTE_ADDR'], 
00490                                         $email,
00491                                         'mailto:'.$contact_address,
00492                                         $login_url                                                              
00493                         )
00494                 )
00495         );
00496         
00497         $mm->Send();
00498 }
00499 
00500 /* Assign password form.
00501  * This form is used to assign a password to a username.
00502  *
00503  * To use this form, the following data must be provided as HTTP GET parameter,
00504  * or in argument pwassist_id:
00505  * key
00506  *
00507  * The key is used to retrieve the password assistance session.
00508  * If the key is missing, or if the password assistance session has expired, the
00509  * password assistance form will be shown instead of this form.
00510  *
00511  * @param message  A message to display on the form.
00512  * @param username The user name to be shown in the form.
00513  * @param password The password1 to be shown in the form.
00514  * @param password The password2 to be shown in the form.
00515  * @param pwassist_id The session key for the password assistance use case.
00516  *                  If this parameter is omitted, the key is retrieved from
00517  *                  the form data.
00518  */
00519 function showAssignPasswordForm($message="", $username="", $password1="", $password2="", $pwassist_id="")
00520 {
00521         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00522         
00523         require_once "include/inc.pwassist_session_handler.php";
00524         require_once "classes/class.ilLanguage.php";
00525         
00526         // Retrieve form data
00527         if ($pwassist_id == "") 
00528         {
00529                 $pwassist_id = $_GET["key"];
00530         }
00531 
00532         // Retrieve the session, and check if it is valid
00533         $pwassist_session = db_pwassist_session_read($pwassist_id);
00534         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00535         {
00536                 showAssistanceForm($lng->txt("pwassist_session_expired"));
00537         }
00538         else
00539         {
00540                 $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_assignpassword.html");
00541                 $tpl->setVariable("FORMACTION","pwassist.php?cmd=submitassign&lang=".$lng->getLangKey());
00542                 $tpl->setVariable("TARGET","target=\"_parent\"");
00543                 $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00544                 $tpl->setVariable("TXT_MESSAGE",str_replace("\\n","<br>",$message));
00545                 $tpl->setVariable("TXT_ENTER_USERNAME_AND_NEW_PASSWORD", $lng->txt("pwassist_enter_username_and_new_password"));
00546                 $tpl->setVariable("TXT_USERNAME", $lng->txt("username"));
00547                 $tpl->setVariable("TXT_PASSWORD1", $lng->txt("password"));
00548                 $tpl->setVariable("TXT_PASSWORD2", $lng->txt("retype_password"));
00549                 $tpl->setVariable("USERNAME", $username);
00550                 $tpl->setVariable("PASSWORD1", $password1);
00551                 $tpl->setVariable("PASSWORD2", $password2);
00552                 $tpl->setVariable("TXT_SUBMIT", $lng->txt("submit"));
00553                 $tpl->setVariable("KEY", $pwassist_id);
00554                 $tpl->setVariable("BACK", $lng->txt("back"));
00555                 $tpl->setVariable("LANG", $lng->getLangKey());
00556         
00557                 $tpl->show();
00558         }
00559 }
00560 
00580 function submitAssignPasswordForm() {
00581         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00582         
00583         require_once "include/inc.pwassist_session_handler.php";
00584         
00585         // Retrieve form data
00586         $pwassist_id = $_POST["key"];
00587         $username = $_POST["username"];
00588         $password1 = $_POST["password1"];
00589         $password2 = $_POST["password2"];
00590 
00591         // Retrieve the session
00592         $pwassist_session = db_pwassist_session_read($pwassist_id);
00593         
00594         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00595         {
00596                 showAssistanceForm($lng->txt("pwassist_session_expired"));
00597         }
00598         else
00599         {
00600                 $is_successful = true;
00601                 $message = "";
00602                 
00603                 $userObj = new ilObjUser($pwassist_session["user_id"]);
00604 
00605                 // Validate the entries of the user
00606                 // ----------------------------------
00607                 // check if the user still exists
00608                 if ($userObj == null)
00609                 {
00610                         $message = $lng->txt("user_does_not_exist");
00611                         $is_successful = false;
00612                 }
00613                 
00614                 // check if the username entered by the user matches the
00615                 // one of the user object.
00616                 if ($is_successful && strcasecmp($userObj->getLogin(), $username) != 0)
00617                 {
00618                         $message = $lng->txt("pwassist_login_not_match");
00619                         $is_successful = false;
00620                 }
00621                 
00622                 // check if the user entered the password correctly into the
00623                 // two entry fields.
00624                 if ($is_successful && $password1 != $password2)
00625                 {
00626                         $message = $lng->txt("passwd_not_match");
00627                         $is_successful = false;
00628                 }
00629 
00630                 // validate the password
00631                 if ($is_successful && !ilUtil::isPassword($password1))
00632                 {
00633                         $message = $lng->txt("passwd_invalid");
00634                         $is_successful = false;
00635                 }
00636                 
00637                 // End of validation
00638                 // If the validation was successful, we change the password of the
00639                 // user.
00640                 // ------------------
00641                 if ($is_successful)
00642                 {
00643                         $is_successful = $userObj->resetPassword($password1,$password2);
00644                         if (! $is_successful) 
00645                         {
00646                                 $message = $lng->txt("passwd_invalid");
00647                         }
00648                 }
00649 
00650                 // If we are successful so far, we update the user object.
00651                 // ------------------
00652                 if ($is_successful) 
00653                 {
00654                         $is_successfull = $userObj->update();
00655                         if (! $is_successful) 
00656                         {
00657                                 $message = $lng->txt("update_error");
00658                         }
00659                 }
00660                 
00661                 // If we are successful, we destroy the password assistance
00662                 // session and redirect to the login page.
00663                 // Else we display the form again along with an error message.
00664                 // ------------------
00665                 if ($is_successful)
00666                 {
00667                         db_pwassist_session_destroy($pwassist_id);
00668                         showMessageForm
00669                         (
00670                                 null,
00671                                 sprintf
00672                                 (
00673                                         $lng->txt("pwassist_password_assigned"),
00674                                         $username
00675                                 )
00676                         );
00677                 }
00678                 else
00679                 {
00680                         showAssignPasswordForm
00681                         (
00682                                 $message,
00683                                 $username,
00684                                 $password1,
00685                                 $password2,
00686                                 $pwassist_id
00687                         );
00688                 }       
00689         }
00690 }
00691 
00692 /* Message form.
00693  * This form is used to show a message to the user.
00694  */
00695 function showMessageForm($message="", $text="")
00696 {
00697         global $tpl, $ilias, $lng;
00698         
00699 
00700         $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_message.html");
00701         $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00702         $tpl->setVariable("TXT_MESSAGE",str_replace("\\n","<br>",$message));
00703         $tpl->setVariable("TXT_TEXT",str_replace("\\n","<br>",$text));
00704         $tpl->setVariable("BACK", $lng->txt("back"));
00705         $tpl->setVariable("LANG", $lng->getLangKey());
00706 
00707         $tpl->show();
00708 }
00709 
00710 ?>

Generated on Fri Dec 13 2013 10:18:31 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1