[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/ldap/Adapter/ExtLdap/ -> Connection.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Ldap\Adapter\ExtLdap;
  13  
  14  use LDAP\Connection as LDAPConnection;
  15  use Symfony\Component\Ldap\Adapter\AbstractConnection;
  16  use Symfony\Component\Ldap\Exception\AlreadyExistsException;
  17  use Symfony\Component\Ldap\Exception\ConnectionException;
  18  use Symfony\Component\Ldap\Exception\ConnectionTimeoutException;
  19  use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
  20  use Symfony\Component\Ldap\Exception\LdapException;
  21  use Symfony\Component\OptionsResolver\Options;
  22  use Symfony\Component\OptionsResolver\OptionsResolver;
  23  
  24  /**
  25   * @author Charles Sarrazin <[email protected]>
  26   */
  27  class Connection extends AbstractConnection
  28  {
  29      private const LDAP_INVALID_CREDENTIALS = 0x31;
  30      private const LDAP_TIMEOUT = 0x55;
  31      private const LDAP_ALREADY_EXISTS = 0x44;
  32  
  33      /** @var bool */
  34      private $bound = false;
  35  
  36      /** @var resource|LDAPConnection */
  37      private $connection;
  38  
  39      /**
  40       * @return array
  41       */
  42      public function __sleep()
  43      {
  44          throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  45      }
  46  
  47      public function __wakeup()
  48      {
  49          throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  50      }
  51  
  52      public function __destruct()
  53      {
  54          $this->disconnect();
  55      }
  56  
  57      /**
  58       * {@inheritdoc}
  59       */
  60      public function isBound()
  61      {
  62          return $this->bound;
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       *
  68       * @param string $password WARNING: When the LDAP server allows unauthenticated binds, a blank $password will always be valid
  69       */
  70      public function bind(string $dn = null, string $password = null)
  71      {
  72          if (!$this->connection) {
  73              $this->connect();
  74          }
  75  
  76          if (false === @ldap_bind($this->connection, $dn, $password)) {
  77              $error = ldap_error($this->connection);
  78              switch (ldap_errno($this->connection)) {
  79                  case self::LDAP_INVALID_CREDENTIALS:
  80                      throw new InvalidCredentialsException($error);
  81                  case self::LDAP_TIMEOUT:
  82                      throw new ConnectionTimeoutException($error);
  83                  case self::LDAP_ALREADY_EXISTS:
  84                      throw new AlreadyExistsException($error);
  85              }
  86              throw new ConnectionException($error);
  87          }
  88  
  89          $this->bound = true;
  90      }
  91  
  92      /**
  93       * @return resource|LDAPConnection
  94       *
  95       * @internal
  96       */
  97      public function getResource()
  98      {
  99          return $this->connection;
 100      }
 101  
 102      public function setOption(string $name, $value)
 103      {
 104          if (!@ldap_set_option($this->connection, ConnectionOptions::getOption($name), $value)) {
 105              throw new LdapException(sprintf('Could not set value "%s" for option "%s".', $value, $name));
 106          }
 107      }
 108  
 109      public function getOption(string $name)
 110      {
 111          if (!@ldap_get_option($this->connection, ConnectionOptions::getOption($name), $ret)) {
 112              throw new LdapException(sprintf('Could not retrieve value for option "%s".', $name));
 113          }
 114  
 115          return $ret;
 116      }
 117  
 118      protected function configureOptions(OptionsResolver $resolver)
 119      {
 120          parent::configureOptions($resolver);
 121  
 122          $resolver->setDefault('debug', false);
 123          $resolver->setAllowedTypes('debug', 'bool');
 124          $resolver->setDefault('referrals', false);
 125          $resolver->setAllowedTypes('referrals', 'bool');
 126          $resolver->setDefault('options', function (OptionsResolver $options, Options $parent) {
 127              $options->setDefined(array_map('strtolower', array_keys((new \ReflectionClass(ConnectionOptions::class))->getConstants())));
 128  
 129              if (true === $parent['debug']) {
 130                  $options->setDefault('debug_level', 7);
 131              }
 132  
 133              if (!isset($parent['network_timeout'])) {
 134                  $options->setDefault('network_timeout', ini_get('default_socket_timeout'));
 135              }
 136  
 137              $options->setDefaults([
 138                  'protocol_version' => $parent['version'],
 139                  'referrals' => $parent['referrals'],
 140              ]);
 141          });
 142      }
 143  
 144      private function connect()
 145      {
 146          if ($this->connection) {
 147              return;
 148          }
 149  
 150          $this->connection = ldap_connect($this->config['connection_string']);
 151  
 152          foreach ($this->config['options'] as $name => $value) {
 153              $this->setOption($name, $value);
 154          }
 155  
 156          if (false === $this->connection) {
 157              throw new LdapException('Could not connect to Ldap server: '.ldap_error($this->connection));
 158          }
 159  
 160          if ('tls' === $this->config['encryption'] && false === @ldap_start_tls($this->connection)) {
 161              throw new LdapException('Could not initiate TLS connection: '.ldap_error($this->connection));
 162          }
 163      }
 164  
 165      private function disconnect()
 166      {
 167          if ($this->connection) {
 168              ldap_unbind($this->connection);
 169          }
 170  
 171          $this->connection = null;
 172          $this->bound = false;
 173      }
 174  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer