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

Services/SOAPAuth/classes/class.ilSOAPAuth.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 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 */
00023 
00024 
00025 include_once("Auth/Auth.php");
00026 include_once("./webservice/soap/lib/nusoap.php");
00027 
00034 class ilSOAPAuth extends Auth
00035 {
00036         var             $valid  = array();
00037         
00042         function ilSOAPAuth($a_params)
00043         {
00044                 parent::Auth("");
00045                 
00046                 $this->server_hostname = $a_params["server_hostname"];
00047                 $this->server_port = (int) $a_params["server_port"];
00048                 $this->server_uri = $a_params["server_uri"];
00049                 $this->namespace = $a_params["namespace"];
00050                 $this->use_dotnet = $a_params["use_dotnet"];
00051                 if ($a_params["https"])
00052                 {
00053                         $this->https = true;
00054                         $uri = "https://";
00055                 }
00056                 else
00057                 {
00058                         $this->https = false;
00059                         $uri = "http://";
00060                 }
00061                 
00062                 $uri.= $this->server_hostname;
00063                 
00064                 if ($this->server_port > 0)
00065                 {
00066                         $uri.= ":".$this->server_port;
00067                 }
00068 
00069                 if ($this->server_uri != "")
00070                 {
00071                         $uri.= "/".$this->server_uri;
00072                 }
00073                 
00074                 $this->uri = $uri;
00075 
00076                 $this->soap_client = new soap_client($this->uri);
00077 //echo "<br>== Get SOAP client ==";
00078 //echo "<br>SOAP client with URI: ".$this->uri."<br>";
00079                 if ($err = $this->soap_client->getError()) 
00080                 {
00081                         die("SOAP Authentication Initialisation Error: ".$err);
00082                 }
00083         }
00084         
00088         static function testConnection($a_ext_uid, $a_soap_pw, $a_new_user)
00089         {
00090                 global $ilSetting;
00091                 
00092                 $settings = $ilSetting->getAll();
00093                 
00094                 $server_hostname = $settings["soap_auth_server"];
00095                 $server_port = (int) $settings["soap_auth_port"];
00096                 $server_uri = $settings["soap_auth_uri"];
00097                 $namespace = $settings["soap_auth_namespace"];
00098                 $use_dotnet = $settings["soap_auth_use_dotnet"];
00099                 if ($settings["soap_auth_use_https"])
00100                 {
00101                         $uri = "https://";
00102                 }
00103                 else
00104                 {
00105                         $uri = "http://";
00106                 }
00107                 
00108                 $uri.= $server_hostname;
00109                 
00110                 if ($server_port > 0)
00111                 {
00112                         $uri.= ":".$server_port;
00113                 }
00114 
00115                 if ($server_uri != "")
00116                 {
00117                         $uri.= "/".$server_uri;
00118                 }
00119 
00120                 $soap_client = new soap_client($uri);
00121                 if ($err = $soap_client->getError()) 
00122                 {
00123                         return "SOAP Authentication Initialisation Error: ".$err;
00124                 }
00125                 
00126                 $soapAction = "";
00127                 $nspref = "";
00128                 if ($use_dotnet)
00129                 {
00130                         $soapAction = $namespace."/isValidSession";
00131                         $nspref = "ns1:";
00132                 }
00133                 
00134                 $valid = $soap_client->call('isValidSession',
00135                         array($nspref.'ext_uid' => $a_ext_uid,
00136                                 $nspref.'soap_pw' => $a_soap_pw,
00137                                 $nspref.'new_user' => $a_new_user),
00138                         $namespace,
00139                         $soapAction);
00140                         
00141                 return
00142                         "<br>== Request ==".
00143                         '<br><pre>' . htmlspecialchars(str_replace("\" ", "\"\n ", str_replace(">", ">\n", $soap_client->request)), ENT_QUOTES) . '</pre><br>'.
00144                         "<br>== Response ==".
00145                         "<br>Valid: -".$valid["valid"]."-".
00146                         '<br><pre>' . htmlspecialchars(str_replace("\" ", "\"\n ", str_replace(">", ">\n", $soap_client->response)), ENT_QUOTES) . '</pre>';
00147         }
00148         
00154         function validateSOAPUser($a_ext_uid, $a_soap_pw)
00155         {
00156                 // check whether external user exists in ILIAS database
00157                 $local_user = ilObjUser::_checkExternalAuthAccount("soap", $a_ext_uid);
00158                 
00159                 if ($local_user == "")
00160                 {
00161                         $new_user = true;
00162                 }
00163                 else
00164                 {
00165                         $new_user = false;
00166                 }
00167                 
00168                 $soapAction = "";
00169                 $nspref = "";
00170                 if ($this->use_dotnet)
00171                 {
00172                         $soapAction = $this->namespace."/isValidSession";
00173                         $nspref = "ns1:";
00174                 }
00175                 
00176                 $valid = $this->soap_client->call('isValidSession',
00177                         array($nspref.'ext_uid' => $a_ext_uid,
00178                                 $nspref.'soap_pw' => $a_soap_pw,
00179                                 $nspref.'new_user' => $new_user),
00180                         $this->namespace,
00181                         $soapAction);
00182 
00183 //echo "<br>== Request ==";
00184 //echo '<br><pre>' . htmlspecialchars($this->soap_client->request, ENT_QUOTES) . '</pre><br>';
00185 //echo "<br>== Response ==";
00186 //echo "<br>Valid: -".$valid["valid"]."-";
00187 //echo '<br><pre>' . htmlspecialchars($this->soap_client->response, ENT_QUOTES) . '</pre>';
00188 
00189                 // to do check SOAP error!?
00190                 $valid["local_user"] = $local_user;
00191                 
00192                 $this->valid = $valid;
00193                 
00194                 return $valid;
00195         }
00196         
00200         function getValidationData()
00201         {
00202                 return $this->valid;
00203         }
00204         
00211         function login()
00212         {
00213                 global $ilias, $rbacadmin, $lng, $ilSetting;
00214 
00215                 if (empty($_GET["ext_uid"]) || empty($_GET["soap_pw"]))
00216                 {
00217                         $this->status = AUTH_WRONG_LOGIN;
00218                         return;
00219                 }
00220 
00221                 $validation_data = $this->validateSoapUser($_GET["ext_uid"], $_GET["soap_pw"]);
00222                 
00223                 if (!$validation_data["valid"])
00224                 {
00225                         $this->status = AUTH_WRONG_LOGIN;
00226                         return;
00227                 }
00228                 
00229                 $local_user = $validation_data["local_user"];
00230                 
00231                 if ($local_user != "")
00232                 {
00233                         // to do: handle update of user
00234                         $this->setAuth($local_user);
00235                 }
00236                 else
00237                 {
00238                         if (!$ilSetting->get("soap_auth_create_users"))
00239                         {
00240                                 $this->status = AUTH_SOAP_NO_ILIAS_USER;
00241                                 $this->logout();
00242                                 return;
00243                         }
00244 //echo "1";
00245                         // try to map external user via e-mail to ILIAS user
00246                         if ($validation_data["email"] != "")
00247                         {
00248 //echo "2";
00249 //var_dump ($_POST);
00250                                 $email_user = ilObjUser::_getLocalAccountsForEmail($validation_data["email"]);
00251 
00252                                 // check, if password has been provided in user mapping screen
00253                                 // (see ilStartUpGUI::showUserMappingSelection)
00254                                 if ($_POST["LoginMappedUser"] != "")
00255                                 { 
00256                                         if (count($email_user) > 0)
00257                                         {
00258                                                 if (ilObjUser::_checkPassword($_POST["usr_id"], $_POST["password"]))
00259                                                 {
00260                                                         // password is correct -> map user
00261                                                         //$this->setAuth($local_user); (use login not id)
00262                                                         ilObjUser::_writeExternalAccount($_POST["usr_id"], $_GET["ext_uid"]);
00263                                                         ilObjUser::_writeAuthMode($_POST["usr_id"], "soap");
00264                                                         $_GET["cmd"] = $_POST["cmd"] = $_GET["auth_stat"]= "";
00265                                                         $local_user = ilObjUser::_lookupLogin($_POST["usr_id"]);
00266                                                         $this->status = "";
00267                                                         $this->setAuth($local_user);
00268                                                         return;
00269                                                 }
00270                                                 else
00271                                                 {
00272 //echo "6"; exit;
00273                                                         $this->status = AUTH_SOAP_NO_ILIAS_USER_BUT_EMAIL;
00274                                                         $this->sub_status = AUTH_WRONG_LOGIN;
00275                                                         $this->logout();
00276                                                         return;
00277                                                 }
00278                                         }
00279                                 }
00280                                 
00281                                 if (count($email_user) > 0 && $_POST["CreateUser"] == "")
00282                                 {                                       
00283                                         $_GET["email"] = $validation_data["email"]; 
00284                                         $this->status = AUTH_SOAP_NO_ILIAS_USER_BUT_EMAIL;
00285                                         $this->logout();
00286                                         return;
00287                                 }
00288                         }
00289 
00290                         $userObj = new ilObjUser();
00291                         
00292                         $local_user = ilAuthUtils::_generateLogin($_GET["ext_uid"]);
00293                         
00294                         $newUser["firstname"] = $validation_data["firstname"];
00295                         $newUser["lastname"] = $validation_data["lastname"];
00296                         $newUser["email"] = $validation_data["email"];
00297                         
00298                         $newUser["login"] = $local_user;
00299                         
00300                         // to do: set valid password and send mail
00301                         $newUser["passwd"] = ""; 
00302                         $newUser["passwd_type"] = IL_PASSWD_MD5;
00303                         
00304                         // generate password, if local authentication is allowed
00305                         // and account mail is activated
00306                         $pw = "";
00307 
00308                         if ($ilSetting->get("soap_auth_allow_local") &&
00309                                 $ilSetting->get("soap_auth_account_mail"))
00310                         {
00311                                 $pw = ilUtil::generatePasswords(1);
00312                                 $pw = $pw[0];
00313                                 $newUser["passwd"] = md5($pw); 
00314                                 $newUser["passwd_type"] = IL_PASSWD_MD5;
00315                         }
00316 
00317                         //$newUser["gender"] = "m";
00318                         $newUser["auth_mode"] = "soap";
00319                         $newUser["ext_account"] = $_GET["ext_uid"];
00320                         $newUser["profile_incomplete"] = 1;
00321                         
00322                         // system data
00323                         $userObj->assignData($newUser);
00324                         $userObj->setTitle($userObj->getFullname());
00325                         $userObj->setDescription($userObj->getEmail());
00326                 
00327                         // set user language to system language
00328                         $userObj->setLanguage($lng->lang_default);
00329                         
00330                         // Time limit
00331                         $userObj->setTimeLimitOwner(7);
00332                         $userObj->setTimeLimitUnlimited(1);
00333                         $userObj->setTimeLimitFrom(time());
00334                         $userObj->setTimeLimitUntil(time());
00335                                                         
00336                         // Create user in DB
00337                         $userObj->setOwner(6);
00338                         $userObj->create();
00339                         $userObj->setActive(1, 6);
00340                         
00341                         $userObj->updateOwner();
00342                         
00343                         //insert user data in table user_data
00344                         $userObj->saveAsNew(false);
00345                         
00346                         // setup user preferences
00347                         $userObj->writePrefs();
00348                         
00349                         // to do: test this
00350                         $rbacadmin->assignUser($ilSetting->get('soap_auth_user_default_role'), $userObj->getId(),true);
00351 
00352                         // send account mail
00353                         if ($ilSetting->get("soap_auth_account_mail"))
00354                         {
00355                                 include_once('./Services/User/classes/class.ilObjUserFolder.php');
00356                                 $amail = ilObjUserFolder::_lookupNewAccountMail($ilSetting->get("language"));
00357                                 if (trim($amail["body"]) != "" && trim($amail["subject"]) != "")
00358                                 {
00359                                         include_once("Services/Mail/classes/class.ilAccountMail.php");
00360                                         $acc_mail = new ilAccountMail();
00361 
00362                                         if ($pw != "")
00363                                         {
00364                                                 $acc_mail->setUserPassword($pw);
00365                                         }
00366                                         $acc_mail->setUser($userObj);
00367                                         $acc_mail->send();
00368                                 }
00369                         }
00370 
00371                         unset($userObj);
00372 
00373                         $this->setAuth($local_user);
00374 
00375                 }
00376         }
00377         
00386 /*
00387         function setAuth($username)
00388         {
00389                 $session = &Auth::_importGlobalVariable('session');
00390                 
00391                 if (!isset($session[$this->_sessionName]) && !isset($_SESSION)) {
00392                         session_register($this->_sessionName);
00393                 }
00394                 
00395                 if (!isset($session[$this->_sessionName]) || !is_array($session[$this->_sessionName])) {
00396                         $session[$this->_sessionName] = array();
00397                 }
00398                 
00399                 if(!isset($session[$this->_sessionName]['data'])){
00400                         $session[$this->_sessionName]['data']       = array();
00401                 }
00402                         $session[$this->_sessionName]['registered'] = true;
00403                         $session[$this->_sessionName]['username']   = $username;
00404                         $session[$this->_sessionName]['timestamp']  = time();
00405                         $session[$this->_sessionName]['idle']       = time();
00406         }
00407 */
00408         
00419         function logout()
00420         {
00421                 parent::logout();
00422         }
00423         
00430 /*
00431         function getUsername()
00432         {
00433                 $session = &$this->_importGlobalVariable('session');
00434                 if (!isset($session[$this->_sessionName]['username'])) {
00435                         return '';
00436                 }
00437                 return $session[$this->_sessionName]['username'];
00438         }
00439 */
00440         
00447 /*
00448         function getStatus()
00449         {
00450                 
00451                 return $status;
00452         }
00453 */
00454         
00462 /*
00463         function &_importGlobalVariable($variable)
00464         {
00465                 $var = null;
00466                 
00467                 switch (strtolower($variable)) {
00468                 
00469                         case 'server' :
00470                                 if (isset($_SERVER)) {
00471                                         $var = &$_SERVER;
00472                                 } else {
00473                                         $var = &$GLOBALS['HTTP_SERVER_VARS'];
00474                                 }
00475                                 break;
00476                         
00477                         case 'session' :
00478                                 if (isset($_SESSION)) {
00479                                         $var = &$_SESSION;
00480                                 } else {
00481                                         $var = &$GLOBALS['HTTP_SESSION_VARS'];
00482                                 }
00483                                 break;
00484                         
00485                         case 'post' :
00486                                 if (isset($_POST)) {
00487                                         $var = &$_POST;
00488                                 } else {
00489                                         $var = &$GLOBALS['HTTP_POST_VARS'];
00490                                 }
00491                                 break;
00492                         
00493                         case 'cookie' :
00494                                 if (isset($_COOKIE)) {
00495                                         $var = &$_COOKIE;
00496                                 } else {
00497                                         $var = &$GLOBALS['HTTP_COOKIE_VARS'];
00498                                 }
00499                                 break;
00500                         
00501                         case 'get' :
00502                                 if (isset($_GET)) {
00503                                         $var = &$_GET;
00504                                 } else {
00505                                         $var = &$GLOBALS['HTTP_GET_VARS'];
00506                                 }
00507                                 break;
00508                         
00509                         default:
00510                                 break;
00511                 
00512                 }
00513 
00514                 return $var;
00515         }
00516 */      
00517 } // END class.ilCASAuth
00518 ?>

Generated on Fri Dec 13 2013 17:57:01 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1