Advertisement
Guest User

Untitled

a guest
Jan 5th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * ownCloud
  5.  *
  6.  * @author Dominik Schmidt
  7.  * @author Alessandro Lorenzi
  8.  * @copyright 2011 Dominik Schmidt dev@dominik-schmidt.de
  9.  * @copyright 2012 Alessandro Lorenzi alorenzi@fsugitalia.org
  10.  *
  11.  * This library is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13.  * License as published by the Free Software Foundation; either
  14.  * version 3 of the License, or any later version.
  15.  *
  16.  * This library is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20.  *
  21.  * You should have received a copy of the GNU Affero General Public
  22.  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
  23.  *
  24.  */
  25.  
  26. class OC_USER_LDAP extends OC_User_Backend {
  27.  
  28.     protected $ds;
  29.     protected $configured = false;
  30.  
  31.     // cached settings
  32.     protected $ldap_host;
  33.     protected $ldap_port;
  34.     protected $ldap_dn;
  35.     protected $ldap_password;
  36.     protected $ldap_base;
  37.     protected $ldap_filter;
  38.  
  39.     function __construct() {
  40.         $this->ldap_host = OC_Appconfig::getValue('user_ldap', 'ldap_host','');
  41.         $this->ldap_port = OC_Appconfig::getValue('user_ldap', 'ldap_port', OC_USER_BACKEND_LDAP_DEFAULT_PORT   );
  42.         $this->ldap_dn = OC_Appconfig::getValue('user_ldap', 'ldap_dn','');
  43.         $this->ldap_password = OC_Appconfig::getValue('user_ldap', 'ldap_password','');
  44.         $this->ldap_base = OC_Appconfig::getValue('user_ldap', 'ldap_base','');
  45.         $this->ldap_filter = OC_Appconfig::getValue('user_ldap', 'ldap_filter','');
  46.  
  47.         if( !empty($this->ldap_host)
  48.             && !empty($this->ldap_port)
  49.             && !empty($this->ldap_dn)
  50.             && !empty($this->ldap_password)
  51.             && !empty($this->ldap_base)
  52.             && !empty($this->ldap_filter)
  53.         )
  54.         {
  55.             $this->configured = true;
  56.         }
  57.     }
  58.  
  59.     function __destruct() {
  60.         // close the connection
  61.         if( $this->ds )
  62.             ldap_unbind($this->ds);
  63.     }
  64.  
  65.     private function getDs() {
  66.         if(!$this->ds) {
  67.             #$this->ds = ldap_connect( $this->ldap_host, $this->ldap_port );
  68.             #   if(ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3))
  69.             #    if(ldap_set_option($this->ds, LDAP_OPT_REFERRALS, 0))
  70.             #         ldap_start_tls($this->ds);
  71.             $this->ds=ldap_connect($this->ldap_host);
  72.             if (!$this->ds) echo "err";
  73.         }
  74.  
  75.         // login
  76.         if(!empty($this->ldap_dn)) {
  77.             ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  78.             $ldap_login = @ldap_bind( $this->ds, $this->ldap_dn, $this->ldap_password );
  79.             if(!$ldap_login)
  80.                 return false;
  81.         }
  82.         return $this->ds;
  83.     }
  84.  
  85.     private function getDn( $uid ) {
  86.         if(!$this->configured)
  87.             return false;
  88.  
  89.         // connect to server
  90.         $ds = $this->getDs();
  91.         if( !$ds )
  92.             return false;
  93.  
  94.         // get dn
  95.         $filter = str_replace("%uid", $uid, $this->ldap_filter);
  96.         $sr = ldap_search( $this->getDs(), $this->ldap_base, $filter );
  97.        
  98.         $entries = ldap_get_entries( $this->getDs(), $sr );
  99.         if( $entries["count"] == 0 )
  100.             return false;
  101.  
  102.         return $entries[0]["dn"];
  103.     }
  104.     public function checkPassword( $uid, $password ) {
  105.         if(!$this->configured){
  106.             return false;
  107.         }
  108.         $dn = $this->getDn( $uid );
  109.         if( !$dn )
  110.             return false;
  111.         if (!@ldap_bind( $this->getDs(), $dn, $password ))
  112.             return false;
  113.         return $uid;
  114.     }
  115.  
  116.     public function userExists( $uid ) {
  117.         if(!$this->configured){
  118.             return false;
  119.         }
  120.         $dn = $this->getDn($uid);
  121.         return !empty($dn);
  122.     }
  123.  
  124. }
  125.             ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement