/** * Set up logger * * @return $this */ protected function setupLogger() { if ($this->config->hasSection('logging')) { $loggingConfig = $this->config->getSection('logging'); try { Logger::create($loggingConfig); } catch (ConfigurationError $e) { Logger::getInstance()->registerConfigError($e->getMessage()); try { Logger::getInstance()->setLevel($loggingConfig->get('level', Logger::ERROR)); } catch (ConfigurationError $e) { Logger::getInstance()->registerConfigError($e->getMessage()); } } } return $this; }
/** * Perform a LDAP search and return the result * * @param LdapQuery $query * @param array $attributes An array of the required attributes * @param int $attrsonly Should be set to 1 if only attribute types are wanted * @param int $sizelimit Enables you to limit the count of entries fetched * @param int $timelimit Sets the number of seconds how long is spend on the search * @param int $deref * * @return resource|bool A search result identifier or false on error * * @throws LogicException If the LDAP query search scope is unsupported */ public function ldapSearch(LdapQuery $query, array $attributes = null, $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER) { $queryString = (string) $query; $baseDn = $query->getBase() ?: $this->getDn(); $scope = $query->getScope(); if (Logger::getInstance()->getLevel() === Logger::DEBUG) { // We're checking the level by ourself to avoid rendering the ldapsearch commandline for nothing $starttlsParam = $this->encryption === static::STARTTLS ? ' -ZZ' : ''; $ldapUrl = ($this->encryption === static::LDAPS ? 'ldaps://' : 'ldap://') . $this->hostname . ($this->port ? ':' . $this->port : ''); if ($this->bound) { $bindParams = ' -D "' . $this->bindDn . '"' . ($this->bindPw ? ' -W' : ''); } if ($deref === LDAP_DEREF_NEVER) { $derefName = 'never'; } elseif ($deref === LDAP_DEREF_ALWAYS) { $derefName = 'always'; } elseif ($deref === LDAP_DEREF_SEARCHING) { $derefName = 'search'; } else { // $deref === LDAP_DEREF_FINDING $derefName = 'find'; } Logger::debug("Issueing LDAP search. Use '%s' to reproduce.", sprintf('ldapsearch -P 3%s -H "%s"%s -b "%s" -s "%s" -z %u -l %u -a "%s"%s%s%s', $starttlsParam, $ldapUrl, $bindParams, $baseDn, $scope, $sizelimit, $timelimit, $derefName, $attrsonly ? ' -A' : '', $queryString ? ' "' . $queryString . '"' : '', $attributes ? ' "' . join('" "', $attributes) . '"' : '')); } switch ($scope) { case LdapQuery::SCOPE_SUB: $function = 'ldap_search'; break; case LdapQuery::SCOPE_ONE: $function = 'ldap_list'; break; case LdapQuery::SCOPE_BASE: $function = 'ldap_read'; break; default: throw new LogicException('LDAP scope %s not supported by ldapSearch', $scope); } return @$function($this->getConnection(), $baseDn, $queryString, $attributes, $attrsonly, $sizelimit, $timelimit, $deref); }
/** * Send the request * * @return mixed * * @throws Exception */ public function send() { $defaults = array('host' => 'localhost', 'path' => '/'); $url = array_merge($defaults, parse_url($this->uri)); if (isset($url['port'])) { $url['host'] .= sprintf(':%u', $url['port']); } if (isset($url['query'])) { $url['path'] .= sprintf('?%s', $url['query']); } $headers = array("{$this->method} {$url['path']} HTTP/1.1", "Host: {$url['host']}", "Content-Type: {$this->contentType}", 'Accept: application/json', 'Expect:'); $ch = curl_init(); $options = array(CURLOPT_URL => $this->uri, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_PROXY => '', CURLOPT_CUSTOMREQUEST => $this->method, CURLOPT_RETURNTRANSFER => true); // Record cURL command line for debugging $curlCmd = array('curl', '-s', '-X', $this->method, '-H', escapeshellarg('Accept: application/json')); if ($this->strictSsl) { $options[CURLOPT_SSL_VERIFYHOST] = 2; $options[CURLOPT_SSL_VERIFYPEER] = true; } else { $options[CURLOPT_SSL_VERIFYHOST] = false; $options[CURLOPT_SSL_VERIFYPEER] = false; $curlCmd[] = '-k'; } if ($this->hasBasicAuth) { $options[CURLOPT_USERPWD] = sprintf('%s:%s', $this->username, $this->password); $curlCmd[] = sprintf('-u %s:%s', escapeshellarg($this->username), escapeshellarg($this->password)); } if (!empty($this->payload)) { $payload = $this->serializePayload($this->payload, $this->contentType); $options[CURLOPT_POSTFIELDS] = $payload; $curlCmd[] = sprintf('-d %s', escapeshellarg($payload)); } $options[CURLOPT_HTTPHEADER] = $headers; $stream = null; if (Logger::getInstance()->getLevel() === Logger::DEBUG) { $stream = fopen('php://temp', 'w'); $options[CURLOPT_VERBOSE] = true; $options[CURLOPT_STDERR] = $stream; } curl_setopt_array($ch, $options); Logger::debug('Executing %s %s', implode(' ', $curlCmd), escapeshellarg($this->uri)); $result = curl_exec($ch); if ($result === false) { throw new Exception(curl_error($ch)); } curl_close($ch); if (is_resource($stream)) { rewind($stream); Logger::debug(stream_get_contents($stream)); fclose($stream); } $response = @json_decode($result, true); if ($response === null) { if (version_compare(PHP_VERSION, '5.5.0', '>=')) { throw new Exception(json_last_error_msg()); } else { switch (json_last_error()) { case JSON_ERROR_DEPTH: $msg = 'The maximum stack depth has been exceeded'; break; case JSON_ERROR_CTRL_CHAR: $msg = 'Control character error, possibly incorrectly encoded'; break; case JSON_ERROR_STATE_MISMATCH: $msg = 'Invalid or malformed JSON'; break; case JSON_ERROR_SYNTAX: $msg = 'Syntax error'; break; case JSON_ERROR_UTF8: $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $msg = 'An error occured when parsing a JSON string'; } throw new Exception($msg); } } return $response; }