• 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_CURRENT != 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         default :
00085                 if (!empty($_GET["key"])) {
00086                         showAssignPasswordForm();
00087                 } else {
00088                         showAssistanceForm();
00089                 }
00090                 break;
00091 }
00092 
00093 // Logout current session
00094 $ilias->auth->logout();
00095 session_destroy();
00096 
00098 
00099 
00100 /* Shows the password assistance form.
00101  * This form is used to request a password assistance mail from ILIAS.
00102  *
00103  * This form contains the following fields: 
00104  * username 
00105  * email 
00106  *
00107  * When the user submits the form, then this script is invoked with the cmd
00108  * 'submitassist'.
00109  *
00110  * @param message  A message to display on the form.
00111  * @param username The user name to be shown in the form.
00112  * @param email    The e-mail to be shown in the form.
00113  */
00114 function showAssistanceForm($message="", $username="", $email="")
00115 {
00116         global $tpl, $ilias, $lng;
00117         
00118         // Create the form
00119         $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_assistance.html");
00120         $tpl->setVariable("FORMACTION","pwassist.php?cmd=submitassist&lang=".$lng->getLangKey());
00121         $tpl->setVariable("TARGET","target=\"_parent\"");
00122         $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00123         $tpl->setVariable("TXT_MESSAGE", str_replace("\\n","<br>",$message));
00124 
00125         $contact_address = $ilias->getSetting("admin_email");
00126         $tpl->setVariable
00127         (
00128                 "TXT_ENTER_USERNAME_AND_EMAIL", 
00129                 str_replace
00130                 (
00131                         "\\n","<br>",
00132                         sprintf
00133                                 (
00134                                 $lng->txt("pwassist_enter_username_and_email"),
00135                                 "<a href=\"mailto:".$contact_address."\">".$contact_address."</a>"
00136                                 )
00137                 )
00138         );
00139         $tpl->setVariable("TXT_USERNAME", $lng->txt("username"));
00140         $tpl->setVariable("TXT_EMAIL", $lng->txt("email"));
00141         $tpl->setVariable("USERNAME", $username);
00142         $tpl->setVariable("EMAIL", $email);
00143         $tpl->setVariable("TXT_SUBMIT", $lng->txt("submit"));
00144         $tpl->setVariable("BACK", $lng->txt("back"));
00145         $tpl->setVariable("LANG", $lng->getLangKey());
00146 
00147         $tpl->show();
00148 }
00149 
00162 function submitAssistanceForm()
00163 {
00164         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00165         
00166         require_once "classes/class.ilObjUser.php";
00167         require_once "classes/class.ilUtil.php";
00168         
00169         // Retrieve form data
00170         $username = $_POST["username"];
00171         $email = $_POST["email"];
00172         
00173         // Retrieve a user object with matching user name and email address.
00174         $userObj = null;
00175         $userid = ilObjUser::getUserIdByLogin($username);
00176         if ($userid != 0)
00177         {
00178                 $userObj = new ilObjUser($userid);
00179                 if (strcasecmp($userObj->getEmail(), $email) != 0) 
00180                 {
00181                         $userObj = null;
00182                 }
00183         }
00184         
00185         // No matching user object found?
00186         // Show the password assistance form again, and display an error message.
00187         if ($userObj == null) 
00188         {
00189                 showAssistanceForm
00190                 (
00191                         $lng->txt("pwassist_invalid_username_or_email"),
00192                         $username,
00193                         $email
00194                 );
00195         }
00196 
00197         // Matching user object found?
00198         // Check if the user is permitted to use the password assistance function,
00199         // and then send a password assistance mail to the email address.
00200         else
00201         {
00202                 // FIXME: Extend this if-statement to check whether the user
00203                 // has the permission to use the password assistance function.
00204                 // The anonymous user and users who are system administrators are
00205                 // not allowed to use this feature
00206                 if ($rbacreview->isAssigned($userObj->getID, ANONYMOUS_ROLE_ID)
00207                 || $rbacreview->isAssigned($userObj->getID, SYSTEM_ROLE_ID)
00208                 ) 
00209                 {
00210                         showAssistanceForm
00211                         (
00212                                 $lng->txt("pwassist_not_permitted"),
00213                                 $username,
00214                                 $email
00215                         );
00216                 }
00217                 else
00218                 {
00219                         sendPasswordAssistanceMail($userObj);
00220                         showMessageForm
00221                         (
00222                                 null,
00223                                 sprintf
00224                                 (
00225                                         $lng->txt("pwassist_mail_sent"),
00226                                         $email
00227                                 )
00228                         );
00229                 }
00230         }
00231 }
00232 
00246 function sendPasswordAssistanceMail($userObj)
00247 {
00248         global $lng, $ilias;
00249 
00250         include_once "classes/class.ilMailbox.php";
00251         include_once "classes/class.ilMimeMail.php";
00252         require_once "include/inc.pwassist_session_handler.php";
00253         
00254 
00255         // Check if we need to create a new session
00256         $pwassist_session = db_pwassist_session_find($userObj->getId());
00257         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00258         {
00259                 // Create a new session id
00260                 $pwassist_session["pwassist_id"] = db_pwassist_create_id();
00261                 
00262                 db_pwassist_session_write(
00263                         $pwassist_session["pwassist_id"],
00264                         3600, 
00265                         $userObj->getId()
00266                 );
00267         }
00268         
00269         // Compose the mail
00270         $server_url='http://'.$_SERVER['HTTP_HOST'].
00271                 substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')).
00272                 '/';
00273         $pwassist_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
00274         ."?client_id=".$ilias->getClientId()
00275         ."&lang=".$lng->getLangKey()
00276         ."&key=".$pwassist_session["pwassist_id"];
00277         $contact_address=$ilias->getSetting("admin_email");
00278 
00279         $mm = new ilMimeMail();
00280         $mm->Subject($lng->txt("pwassist_mail_subject"));
00281         $mm->From($contact_address);
00282         $mm->To($userObj->getEmail());
00283         
00284         $mm->Body
00285         (
00286                 str_replace
00287                 (
00288                         array("\\n","\\t"),
00289                         array("\n","\t"),
00290                         sprintf
00291                         (
00292                         $lng->txt("pwassist_mail_body"),
00293                         $pwassist_url,
00294                         $server_url,
00295                         $_SERVER['REMOTE_ADDR'],
00296                         $userObj->getLogin(),
00297                         'mailto:'.$contact_address
00298                         )
00299                 )
00300         );
00301         
00302         $mm->Send();
00303 }
00304 
00305 /* Assign password form.
00306  * This form is used to assign a password to a username.
00307  *
00308  * To use this form, the following data must be provided as HTTP GET parameter,
00309  * or in argument pwassist_id:
00310  * key
00311  *
00312  * The key is used to retrieve the password assistance session.
00313  * If the key is missing, or if the password assistance session has expired, the
00314  * password assistance form will be shown instead of this form.
00315  *
00316  * @param message  A message to display on the form.
00317  * @param username The user name to be shown in the form.
00318  * @param password The password1 to be shown in the form.
00319  * @param password The password2 to be shown in the form.
00320  * @param pwassist_id The session key for the password assistance use case.
00321  *                  If this parameter is omitted, the key is retrieved from
00322  *                  the form data.
00323  */
00324 function showAssignPasswordForm($message="", $username="", $password1="", $password2="", $pwassist_id="")
00325 {
00326         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00327         
00328         require_once "include/inc.pwassist_session_handler.php";
00329         require_once "classes/class.ilLanguage.php";
00330         
00331         // Retrieve form data
00332         if ($pwassist_id == "") 
00333         {
00334                 $pwassist_id = $_GET["key"];
00335         }
00336 
00337         // Retrieve the session, and check if it is valid
00338         $pwassist_session = db_pwassist_session_read($pwassist_id);
00339         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00340         {
00341                 showAssistanceForm($lng->txt("pwassist_session_expired"));
00342         }
00343         else
00344         {
00345                 $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_assignpassword.html");
00346                 $tpl->setVariable("FORMACTION","pwassist.php?cmd=submitassign&lang=".$lng->getLangKey());
00347                 $tpl->setVariable("TARGET","target=\"_parent\"");
00348                 $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00349                 $tpl->setVariable("TXT_MESSAGE",str_replace("\\n","<br>",$message));
00350                 $tpl->setVariable("TXT_ENTER_USERNAME_AND_NEW_PASSWORD", $lng->txt("pwassist_enter_username_and_new_password"));
00351                 $tpl->setVariable("TXT_USERNAME", $lng->txt("username"));
00352                 $tpl->setVariable("TXT_PASSWORD1", $lng->txt("password"));
00353                 $tpl->setVariable("TXT_PASSWORD2", $lng->txt("retype_password"));
00354                 $tpl->setVariable("USERNAME", $username);
00355                 $tpl->setVariable("PASSWORD1", $password1);
00356                 $tpl->setVariable("PASSWORD2", $password2);
00357                 $tpl->setVariable("TXT_SUBMIT", $lng->txt("submit"));
00358                 $tpl->setVariable("KEY", $pwassist_id);
00359                 $tpl->setVariable("BACK", $lng->txt("back"));
00360                 $tpl->setVariable("LANG", $lng->getLangKey());
00361         
00362                 $tpl->show();
00363         }
00364 }
00365 
00385 function submitAssignPasswordForm() {
00386         global $tpl, $ilias, $lng, $rbacadmin, $rbacreview;
00387         
00388         require_once "include/inc.pwassist_session_handler.php";
00389         
00390         // Retrieve form data
00391         $pwassist_id = $_POST["key"];
00392         $username = $_POST["username"];
00393         $password1 = $_POST["password1"];
00394         $password2 = $_POST["password2"];
00395 
00396         // Retrieve the session
00397         $pwassist_session = db_pwassist_session_read($pwassist_id);
00398         
00399         if (count($pwassist_session) == 0 || $pwassist_session["expires"] < time())
00400         {
00401                 showAssistanceForm($lng->txt("pwassist_session_expired"));
00402         }
00403         else
00404         {
00405                 $is_successful = true;
00406                 $message = "";
00407                 
00408                 $userObj = new ilObjUser($pwassist_session["user_id"]);
00409 
00410                 // Validate the entries of the user
00411                 // ----------------------------------
00412                 // check if the user still exists
00413                 if ($userObj == null)
00414                 {
00415                         $message = $lng->txt("user_does_not_exist");
00416                         $is_successful = false;
00417                 }
00418                 
00419                 // check if the username entered by the user matches the
00420                 // one of the user object.
00421                 if ($is_successful && strcasecmp($userObj->getLogin(), $username) != 0)
00422                 {
00423                         $message = $lng->txt("pwassist_login_not_match");
00424                         $is_successful = false;
00425                 }
00426                 
00427                 // check if the user entered the password correctly into the
00428                 // two entry fields.
00429                 if ($is_successful && $password1 != $password2)
00430                 {
00431                         $message = $lng->txt("passwd_not_match");
00432                         $is_successful = false;
00433                 }
00434 
00435                 // validate the password
00436                 if ($is_successful && !ilUtil::isPassword($password1))
00437                 {
00438                         $message = $lng->txt("passwd_invalid");
00439                         $is_successful = false;
00440                 }
00441                 
00442                 // End of validation
00443                 // If the validation was successful, we change the password of the
00444                 // user.
00445                 // ------------------
00446                 if ($is_successful)
00447                 {
00448                         $is_successful = $userObj->resetPassword($password1,$password2);
00449                         if (! $is_successful) 
00450                         {
00451                                 $message = $lng->txt("passwd_invalid");
00452                         }
00453                 }
00454 
00455                 // If we are successful so far, we update the user object.
00456                 // ------------------
00457                 if ($is_successful) 
00458                 {
00459                         $is_successfull = $userObj->update();
00460                         if (! $is_successful) 
00461                         {
00462                                 $message = $lng->txt("update_error");
00463                         }
00464                 }
00465                 
00466                 // If we are successful, we destroy the password assistance
00467                 // session and redirect to the login page.
00468                 // Else we display the form again along with an error message.
00469                 // ------------------
00470                 if ($is_successful)
00471                 {
00472                         db_pwassist_session_destroy($pwassist_id);
00473                         showMessageForm
00474                         (
00475                                 null,
00476                                 sprintf
00477                                 (
00478                                         $lng->txt("pwassist_password_assigned"),
00479                                         $username
00480                                 )
00481                         );
00482                 }
00483                 else
00484                 {
00485                         showAssignPasswordForm
00486                         (
00487                                 $message,
00488                                 $username,
00489                                 $password1,
00490                                 $password2,
00491                                 $pwassist_id
00492                         );
00493                 }       
00494         }
00495 }
00496 
00497 /* Message form.
00498  * This form is used to show a message to the user.
00499  */
00500 function showMessageForm($message="", $text="")
00501 {
00502         global $tpl, $ilias, $lng;
00503         
00504 
00505         $tpl->addBlockFile("CONTENT", "content", "tpl.pwassist_message.html");
00506         $tpl->setVariable("TXT_PAGEHEADLINE", $lng->txt("password_assistance"));
00507         $tpl->setVariable("TXT_MESSAGE",str_replace("\\n","<br>",$message));
00508         $tpl->setVariable("TXT_TEXT",str_replace("\\n","<br>",$text));
00509         $tpl->setVariable("BACK", $lng->txt("back"));
00510         $tpl->setVariable("LANG", $lng->getLangKey());
00511 
00512         $tpl->show();
00513 }
00514 
00515 ?>

Generated on Fri Dec 13 2013 09:06:37 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1