/**
  * Add an LDAP object.
  *
  * For this work you might need that LDAP connection is bind:ed with a user with
  * enough permissions to change attributes and that the LDAP connection is using
  * SSL/TLS. It depends on the server setup.
  *
  * @param string $dn Location to add the entry at.
  * @param array $attributes An associative array of attributes.
  * @throws \Zend\Ldap\Exception\LdapException
  */
 public function add($dn, array $attributes)
 {
     $this->ldap->add($dn, $attributes);
 }
示例#2
0
 public function __construct($config = [])
 {
     if (empty($config)) {
         // Get arguments
         $argManager = new ArgumentManager();
         $argManager->parse();
     }
     $config = $argManager->asArray();
     $this->server = $config['server'];
     $this->port = $config['port'];
     $this->username = $config['username'];
     $this->password = $config['password'];
     $this->accountDomainName = $config['domain'];
     $this->baseDn = $config['basedn'];
     // optionals
     $this->filter = isset($config['filter']) && $config['filter'] ? $config['filter'] : '(objectClass=*)';
     $this->attributes = isset($config['attributes']) && $config['attributes'] ? $config['attributes'] : [];
     $this->keyAttribute = strtolower(isset($config['keyattr']) && $config['keyattr'] ? $config['keyattr'] : 'sAMAccountName');
     $this->isSSL = isset($config['ssl']) && $config['ssl'] ? true : null;
     $this->output = isset($config['output']) && $config['output'] ? $config['output'] : self::STDIO;
     // check if output needs to be verified
     if ($this->output != self::STDIO) {
         // Get base directory
         $dir = dirname($this->output);
         if (!file_exists($dir) && !is_dir($this->output)) {
             mkdir($this->output, 0777, true);
         }
         if (file_exists($this->output) && is_file($this->output)) {
             unlink($this->output);
         }
     }
     // Create LDAP connection
     $ldap = new \Zend\Ldap\Ldap(['accountDomainName' => $this->accountDomainName, 'baseDn' => $this->baseDn]);
     $ldap->connect($this->server, $this->port, $this->isSSL);
     $ldap->bind($this->username . '@' . $this->accountDomainName, $this->password);
     // Apply filter
     $result = $ldap->search($this->filter);
     // Prepare output container
     $output = [];
     foreach ($result as $index => $entry) {
         $newrow = [];
         foreach ($entry as $key => $value) {
             if ($this->attributeIncluded($key)) {
                 if (is_array($value) && sizeof($value) == 1) {
                     $value = $value[0];
                 }
                 $newrow[$key] = $value;
             }
         }
         // Push attribute
         if (isset($newrow[$this->keyAttribute])) {
             $output[$newrow[$this->keyAttribute]] = $newrow;
         } else {
             $output[] = $newrow;
         }
     }
     $ymlOutput = \Spyc::YAMLDump($output, true);
     $this->_queryoutput = $ymlOutput;
     if ($this->output != self::STDIO) {
         $writeSuccess = file_put_contents($this->output, $this->_queryoutput);
         echo ($writeSuccess !== false ? PHP_EOL . sprintf("Output has been successfully exported to \"%s\"", realpath($this->output)) : sprintf("Unable to write output to file \"%s\"")) . PHP_EOL . PHP_EOL;
     }
 }