Source for file lds.auth.php

Documentation is available at lds.auth.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage auth_driver
  5. @author     Nicolas JEUDY
  6. @contributor Laurent Jouanneau
  7. @copyright  2006 Nicolas JEUDY
  8. @copyright  2007 Laurent Jouanneau
  9. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  10. */
  11.  
  12.  
  13. /**
  14.  * object which represent a user
  15.  *
  16.  * @package    jelix
  17.  * @subpackage auth_driver
  18.  */
  19. class jAuthUserLDS extends jAuthUser {
  20. }
  21.  
  22.  
  23.  
  24.  
  25. /**
  26.  * authentification driver, which communicate with a LDS server
  27.  * LDS = Linbox Directory Server
  28.  * @package    jelix
  29.  * @subpackage auth_driver
  30.  * @link http://lds.linbox.org/
  31.  * @see jAuth
  32.  * @since 1.0b1
  33.  */
  34. class ldsAuthDriver implements jIAuthDriver {
  35.  
  36.     protected $_params;
  37.  
  38.     function __construct($params){
  39.         $this->_params = $params;
  40.     }
  41.  
  42.  
  43.     public function saveNewUser($user){
  44.         $login $user->login;
  45.         $pass $user->password;
  46.         $firstname "Jelix User";
  47.         $name "Jelix User";
  48.         // NULL homedir = /tmp ...
  49.         $homedir "/tmp";
  50.         $param array($login$pass$firstname$name$homedir);
  51.         $ret $this->xmlCall("base.createUser",$param);
  52.         return true;
  53.     }
  54.  
  55.     public function removeUser($login){
  56.         //fichier=1 -> remove user files (homeDirectory etc)
  57.         $fichier=0;
  58.         $param=array ($login,$fichier);
  59.         //ldap account can be modified from an other apps, so group can be use in an other app
  60.         $this->xmlCall("base.delUserFromAllGroups"$login);
  61.         $this->xmlCall("base.delUser",$param);
  62.         return true;
  63.     }
  64.  
  65.     public function updateUser($user){
  66.         return true;
  67.     }
  68.  
  69.     public function getUser($login){
  70.         $login '*'.$login.'*';
  71.         $paramsArr $this->xmlCall('base.getUsersLdap',$login);
  72.         $user new jAuthUserLDS();
  73.         $user->login $paramsArr['uid'][0];
  74.         $user->password $paramsArr['userPassword'][0];
  75.         return $user;
  76.     }
  77.  
  78.     public function createUserObject($login,$password){
  79.         $user new jAuthUserLDS();
  80.         $user->login $login;
  81.         $user->password $password;
  82.         return $user;
  83.     }
  84.  
  85.     public function getUserList($pattern){
  86.         $users $this->xmlCall('base.getUsersLdap',$pattern '*');
  87.         $userslist array();
  88.         foreach ($users as $userldap{
  89.             $user new jAuthUserLDS();
  90.             $user->login $userldap['uid'];
  91.             $userslist[$user;
  92.         }
  93.         return $userslist;
  94.     }
  95.  
  96.     public function changePassword($login$newpassword){
  97.         $param[]=$login;
  98.         $param[]=$newpassword;
  99.         return $this->xmlCall("base.changeUserPasswd",$param);
  100.     }
  101.  
  102.     public function verifyPassword($login$password){
  103.         if (trim($password== '')
  104.             return false;
  105.         $param[]=$login;
  106.         $param[]=$password;
  107.         $ret$this->xmlCall("base.ldapAuth",$param);
  108.         if $ret == '1'{
  109.             $user new jAuthUserLDS();
  110.             $user->login $login;
  111.             $user->password $password;
  112.         }
  113.         return ($user?$user:false);
  114.     }
  115.  
  116.     /**
  117.     * function wich decode UTF-8 Entity with ref
  118.     * &#03; for example
  119.     * need because XMLRPC server doest not like this sort
  120.     * of encoding
  121.     */
  122.     protected function decodeEntities($text{
  123.         $text html_entity_decode($text,ENT_QUOTES,"ISO-8859-1")/* NOTE: UTF-8 does not work! */
  124.         $textpreg_replace('/&#(\d+);/me',"chr(\\1)",$text)/* decimal notation */
  125.         $textpreg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text);  /* hex notation */
  126.         return $text;
  127.     }
  128.  
  129.     /**
  130.     * call an xmlrpc call for a method
  131.     * via the xmlrpc server in python (lmc-agent)
  132.     * @param $method name of the method
  133.     * @param $params array with param
  134.     */
  135.     protected function xmlCall($method,$params{
  136.  
  137.         $output_options array"output_type" => "xml""verbosity" => "pretty""escaping" => array("markup""non-ascii""non-print")"version" => "xmlrpc""encoding" => "UTF-8" );
  138.  
  139.        //$output_options = array( "output_type" => "xml", "verbosity" => "pretty", "escaping" => array("markup", "non-ascii", "non-print"), "version" => "xmlrpc", "encoding" => "iso-8859-1" );
  140.  
  141.         if ($params==null{
  142.             $request xmlrpc_encode_request($method,null,$output_options);
  143.         }else {
  144.             $request xmlrpc_encode_request($method,$params,$output_options);
  145.             $request $this->decodeEntities($request,ENT_QUOTES,"UTF-8");
  146.         }
  147.  
  148.  
  149.         $host$this->_params['host'].":".$this->_params['port'];
  150.         $url "/";
  151.  
  152.         $httpQuery "POST "$url ." HTTP/1.0\r\n";
  153.         $httpQuery .= "User-Agent: xmlrpc\r\n";
  154.         $httpQuery .= "Host: "$host ."\r\n";
  155.         $httpQuery .= "Content-Type: text/xml\r\n";
  156.         $httpQuery .= "Content-Length: "strlen($request."\r\n";
  157.         $httpQuery .= "Authorization: Basic ".base64_encode($this->_params['login']).":".base64_encode($this->_params['password'])."\r\n\r\n";
  158.         $httpQuery .= $request;
  159.         $sock=null;
  160.  
  161.         // if crypted connexion
  162.         if ($this->_params['scheme']=="https"{
  163.             $prot="ssl://";
  164.         }
  165.         $sock @fsockopen($prot.$this->_params['host'],$this->_params['port']$errNo$errString);
  166.  
  167.         if !$sock {
  168.             jLog::log('Erreur de connexion XMLRPC');
  169.             jLog::dump($prot.$this->_params['host']);
  170.             jLog::dump($this->_params['port']);
  171.             jLOG::dump($httpQuery);
  172.             jLOG::dump(strlen($httpQuery));
  173.             jLOG::dump($errNo);
  174.             jLOG::dump($errString);
  175.             throw new jException('jelix~auth.error.lds.unreachable.server');
  176.         }
  177.  
  178.         if !fwrite($sock$httpQuerystrlen($httpQuery)) ) {
  179.             throw new jException('jelix~auth.error.lds.request.not.send');
  180.         }
  181.  
  182.         fflush($sock);
  183.         // We get the response from the server
  184.         while !feof($sock) ) {
  185.             $xmlResponse .= fgets($sock);
  186.         }
  187.         // Closing the connection
  188.         fclose($sock);
  189.         $xmlResponse substr($xmlResponsestrpos($xmlResponse"\r\n\r\n"+4);
  190.         /*
  191.         To decode the XML into PHP, we use the (finaly a short function)
  192.         xmlrpc_decode function. And that should've done the trick.
  193.         We now have what ever the server function made in our $xmlResponse
  194.         variable.
  195.  
  196.         Test if the XMLRPC result is a boolean value set to False.
  197.         If it is the case, xmlrpc_decode will return an empty string.
  198.         So we need to test this special case. */
  199.  
  200.         $booleanFalse "<?xml version='1.0'?>\n<methodResponse>\n<params>\n<param>\n<value><boolean>0</boolean></value>\n</param>\n</params>\n</methodResponse>\n";
  201.         if ($xmlResponse == $booleanFalse)
  202.             $xmlResponse "0";
  203.         else {
  204.             $xmlResponseTmp xmlrpc_decode($xmlResponse,"UTF-8");
  205.  
  206.             //if we cannot decode in UTF-8
  207.             if (!$xmlResponseTmp{
  208.                     //conversion in UTF-8
  209.                     $xmlResponse iconv("ISO-8859-1","UTF-8",$xmlResponse);
  210.                     $xmlResponse xmlrpc_decode($xmlResponse,"UTF-8");
  211.             else {
  212.                     $xmlResponse=$xmlResponseTmp;
  213.             }
  214.         }
  215.         return $xmlResponse;
  216.     }
  217. }

Documentation generated on Thu, 22 Mar 2012 22:17:44 +0100 by phpDocumentor 1.4.3