diff --git a/SolrPhpClient/Apache/Solr/HttpTransport/Abstract.php b/SolrPhpClient/Apache/Solr/HttpTransport/Abstract.php index cf9f76d..d16266c 100644 --- a/SolrPhpClient/Apache/Solr/HttpTransport/Abstract.php +++ b/SolrPhpClient/Apache/Solr/HttpTransport/Abstract.php @@ -48,7 +48,7 @@ abstract class Apache_Solr_HttpTransport_Abstract implements Apache_Solr_HttpTra * @var float */ private $_defaultTimeout = false; - + /** * Get the current default timeout setting (initially the default_socket_timeout ini setting) * in seconds diff --git a/SolrPhpClient/Apache/Solr/HttpTransport/Curl.php b/SolrPhpClient/Apache/Solr/HttpTransport/Curl.php index 7cb7743..67b0962 100644 --- a/SolrPhpClient/Apache/Solr/HttpTransport/Curl.php +++ b/SolrPhpClient/Apache/Solr/HttpTransport/Curl.php @@ -90,7 +90,33 @@ function __destruct() // close our curl session curl_close($this->_curl); } + + public function setAuthenticationCredentials($username, $password) + { + // add the options to our curl handle + curl_setopt_array($this->_curl, array( + CURLOPT_USERPWD => $username . ":" . $password, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC + )); + } + public function setProxy($proxy, $port, $username = '', $password = '') + { + // add the options to our curl handle + curl_setopt_array($this->_curl, array( + CURLOPT_PROXY => $proxy, + CURLOPT_PROXYPORT => $port + )); + + if ($username != '' && $password != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXYAUTH => CURLAUTH_BASIC, + CURLOPT_PROXYUSERPWD => "$username:$password" + )); + } + } + public function performGetRequest($url, $timeout = false) { // check the timeout value diff --git a/SolrPhpClient/Apache/Solr/HttpTransport/CurlNoReuse.php b/SolrPhpClient/Apache/Solr/HttpTransport/CurlNoReuse.php index 1454958..0c1ee5d 100644 --- a/SolrPhpClient/Apache/Solr/HttpTransport/CurlNoReuse.php +++ b/SolrPhpClient/Apache/Solr/HttpTransport/CurlNoReuse.php @@ -55,7 +55,27 @@ class Apache_Solr_HttpTransport_CurlNoReuse extends Apache_Solr_HttpTransport_Ab * SVN ID meta data for this class */ const SVN_ID = '$Id:$'; + + private $_authString = false; + private $_proxy = ''; + private $_proxyPort = ''; + private $_proxyUsername = ''; + private $_proxyPassword = ''; + + public function setAuthenticationCredentials($username, $password) + { + // this is how curl wants it for the CURLOPT_USERPWD + $this->_authString = $username . ":" . $password; + } + public function setProxy($proxy, $port, $username = '', $password = '') + { + $this->_proxy = $proxy; + $this->_proxyPort = $port; + $this->_proxyUsername = $username; + $this->_proxyPassword = $password; + } + public function performGetRequest($url, $timeout = false) { // check the timeout value @@ -84,6 +104,32 @@ public function performGetRequest($url, $timeout = false) // set the timeout CURLOPT_TIMEOUT => $timeout )); + + // set auth if appropriate + if ($this->_authString !== false) + { + curl_setopt_array($curl, array( + CURLOPT_USERPWD => $this->_authString, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC + )); + } + + // set proxy + if ($this->_proxy != '' && $this->_proxyPort != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXY => $this->_proxy, + CURLOPT_PROXYPORT => $this->_proxyPort + )); + + if ($this->_proxyUsername != '' && $this->_proxyPassword != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXYAUTH => CURLAUTH_BASIC, + CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword" + )); + } + } // make the request $responseBody = curl_exec($curl); @@ -130,6 +176,32 @@ public function performHeadRequest($url, $timeout = false) CURLOPT_TIMEOUT => $timeout )); + // set auth if appropriate + if ($this->_authString !== false) + { + curl_setopt_array($curl, array( + CURLOPT_USERPWD => $this->_authString, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC + )); + } + + // set proxy + if ($this->_proxy != '' && $this->_proxyPort != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXY => $this->_proxy, + CURLOPT_PROXYPORT => $this->_proxyPort + )); + + if ($this->_proxyUsername != '' && $this->_proxyPassword != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXYAUTH => CURLAUTH_BASIC, + CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword" + )); + } + } + // make the request $responseBody = curl_exec($curl); @@ -181,6 +253,32 @@ public function performPostRequest($url, $postData, $contentType, $timeout = fal CURLOPT_TIMEOUT => $timeout )); + // set auth if appropriate + if ($this->_authString !== false) + { + curl_setopt_array($curl, array( + CURLOPT_USERPWD => $this->_authString, + CURLOPT_HTTPAUTH => CURLAUTH_BASIC + )); + } + + // set proxy + if ($this->_proxy != '' && $this->_proxyPort != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXY => $this->_proxy, + CURLOPT_PROXYPORT => $this->_proxyPort + )); + + if ($this->_proxyUsername != '' && $this->_proxyPassword != '') + { + curl_setopt_array($this->_curl, array( + CURLOPT_PROXYAUTH => CURLAUTH_BASIC, + CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword" + )); + } + } + // make the request $responseBody = curl_exec($curl); diff --git a/SolrPhpClient/Apache/Solr/HttpTransport/FileGetContents.php b/SolrPhpClient/Apache/Solr/HttpTransport/FileGetContents.php index 5e01775..f3386f6 100644 --- a/SolrPhpClient/Apache/Solr/HttpTransport/FileGetContents.php +++ b/SolrPhpClient/Apache/Solr/HttpTransport/FileGetContents.php @@ -61,6 +61,15 @@ class Apache_Solr_HttpTransport_FileGetContents extends Apache_Solr_HttpTranspor */ private $_getContext, $_headContext, $_postContext; + /** + * For POST operations, we're already using the Header context value for + * specifying the content type too, so we have to keep our computed + * authorization header around + * + * @var string + */ + private $_authHeader = ""; + /** * Initializes our reuseable get and post stream contexts */ @@ -70,7 +79,40 @@ public function __construct() $this->_headContext = stream_context_create(); $this->_postContext = stream_context_create(); } + + public function setAuthenticationCredentials($username, $password) + { + // compute the Authorization header + $this->_authHeader = "Authorization: Basic " . base64_encode($username . ":" . $password); + + // set it now for get and head contexts + stream_context_set_option($this->_getContext, 'http', 'header', $this->_authHeader); + stream_context_set_option($this->_headContext, 'http', 'header', $this->_authHeader); + + // for post, it'll be set each time, so add an \r\n so it can be concatenated + // with the Content-Type + $this->_authHeader .= "\r\n"; + } + public function setProxy($proxy, $port, $username = '', $password = '') + { + $proxy = "tcp://$proxy:$port"; + + // set it now for get and head contexts + stream_context_set_option($this->_getContext, 'http', 'proxy', $proxy); + stream_context_set_option($this->_headContext, 'http', 'proxy', $proxy); + + if ($username != '' && $password != '') + { + $authProxy = base64_encode("$username:$password"); + $proxyAutorization = "Proxy-Authorization: Basic $authProxy"; + + // set it now for get and head contexts + stream_context_set_option($this->_getContext, 'http', 'header', $proxyAutorization); + stream_context_set_option($this->_headContext, 'http', 'header', $proxyAutorization); + } + } + public function performGetRequest($url, $timeout = false) { // set the timeout if specified @@ -87,7 +129,7 @@ public function performGetRequest($url, $timeout = false) // use the default timeout pulled from default_socket_timeout otherwise stream_context_set_option($this->_getContext, 'http', 'timeout', $this->getDefaultTimeout()); } - + // $http_response_headers will be updated by the call to file_get_contents later // see http://us.php.net/manual/en/wrappers.http.php for documentation // Unfortunately, it will still create a notice in analyzers if we don't set it here @@ -136,8 +178,8 @@ public function performPostRequest($url, $rawPost, $contentType, $timeout = fals // set HTTP method 'method' => 'POST', - // Add our posted content type - 'header' => "Content-Type: $contentType", + // Add our posted content type (and auth header - see setAuthentication) + 'header' => "{$this->_authHeader}Content-Type: {$contentType}", // the posted content 'content' => $rawPost, diff --git a/SolrPhpClient/Apache/Solr/HttpTransport/Interface.php b/SolrPhpClient/Apache/Solr/HttpTransport/Interface.php index 090fc27..5586d7c 100644 --- a/SolrPhpClient/Apache/Solr/HttpTransport/Interface.php +++ b/SolrPhpClient/Apache/Solr/HttpTransport/Interface.php @@ -59,7 +59,29 @@ public function getDefaultTimeout(); * @param float $timeout */ public function setDefaultTimeout($timeout); - + + /** + * Set authentication credentials to pass along with the requests. + * + * These will be used to perform HTTP Basic authentication. + * + * @param string $username + * @param string $password + */ + public function setAuthenticationCredentials($username, $password); + + /** + * Set proxy. + * + * These will be used to perform HTTP connection througt proxy. + * + * @param string $proxy + * @param string $port + * @param string $username + * @param string $password + */ + public function setProxy($proxy, $port, $username = '', $password = ''); + /** * Perform a GET HTTP operation with an optional timeout and return the response * contents, use getLastResponseHeaders to retrieve HTTP headers diff --git a/SolrPhpClient/Apache/Solr/Service.php b/SolrPhpClient/Apache/Solr/Service.php index ea9ae37..5ce06a1 100644 --- a/SolrPhpClient/Apache/Solr/Service.php +++ b/SolrPhpClient/Apache/Solr/Service.php @@ -1,6 +1,6 @@ _extractUrl = $this->_constructUrl(self::EXTRACT_SERVLET); $this->_pingUrl = $this->_constructUrl(self::PING_SERVLET); $this->_searchUrl = $this->_constructUrl(self::SEARCH_SERVLET); + $this->_systemUrl = $this->_constructUrl(self::SYSTEM_SERVLET, array('wt' => self::SOLR_WRITER)); $this->_threadsUrl = $this->_constructUrl(self::THREADS_SERVLET, array('wt' => self::SOLR_WRITER )); $this->_updateUrl = $this->_constructUrl(self::UPDATE_SERVLET, array('wt' => self::SOLR_WRITER )); @@ -458,7 +460,14 @@ public function setPath($path) { $path = trim($path, '/'); - $this->_path = '/' . $path . '/'; + if (strlen($path) > 0) + { + $this->_path = '/' . $path . '/'; + } + else + { + $this->_path = '/'; + } if ($this->_urlsInited) { @@ -559,7 +568,30 @@ public function setDefaultTimeout($timeout) { $this->getHttpTransport()->setDefaultTimeout($timeout); } + + /** + * Convenience method to set authentication credentials on the current HTTP transport implementation + * + * @param string $username + * @param string $password + */ + public function setAuthenticationCredentials($username, $password) + { + $this->getHttpTransport()->setAuthenticationCredentials($username, $password); + } + /** + * Convenience method to set proxy + * + * @param string $username + * @param string $password + */ + public function setProxy($proxy, $port, $username = '', $password = '') + { + $this->getHttpTransport()->setProxy($proxy, $port, $username, $password); + } + + /** * Set how NamedLists should be formatted in the response data. This mainly effects * the facet counts format. @@ -641,6 +673,18 @@ public function ping($timeout = 2) return false; } } + + /** + * Call the /admin/system servlet and retrieve system information about Solr + * + * @return Apache_Solr_Response + * + * @throws Apache_Solr_HttpTransportException If an error occurs during the service call + */ + public function system() + { + return $this->_sendRawGet($this->_systemUrl); + } /** * Call the /admin/threads servlet and retrieve information about all threads in the @@ -751,8 +795,8 @@ protected function _documentToXmlFragment(Apache_Solr_Document $document) foreach ($document as $key => $value) { - $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $fieldBoost = $document->getFieldBoost($key); + $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); if (is_array($value)) { diff --git a/advanced-search-by-my-solr-server-options-page.php b/advanced-search-by-my-solr-server-options-page.php old mode 100755 new mode 100644 index f1ce9d5..408e3d9 --- a/advanced-search-by-my-solr-server-options-page.php +++ b/advanced-search-by-my-solr-server-options-page.php @@ -85,6 +85,11 @@ function mss_checkConnectOption($optionType, $connectType) { $mss_passwd = decrypt($options['mss_passwd']); $mss_url = $options['mss_url']; +$mss_proxy = $options['mss_solr_proxy']; +$mss_proxyport = $options['mss_solr_proxyport']; +$mss_proxyusername = $options['mss_solr_proxyusername']; +$mss_proxypassword = decrypt($options['mss_solr_proxypassword']); + $account_plan=''; $account_status=''; $account_expire=''; @@ -95,7 +100,7 @@ function mss_checkConnectOption($optionType, $connectType) { $connected = false; if ($mss_id!='' && $mss_passwd!='') { - $account_info_json = getMssAccountInfo($url_mysolrserver, $url_extraparam, $mss_id, $mss_passwd); + $account_info_json = getMssAccountInfo($url_mysolrserver, $url_extraparam, $mss_id, $mss_passwd, $mss_proxy, $mss_proxyport, $mss_proxyusername, $mss_proxypassword); $account_info = json_decode ($account_info_json, true); //print_r($account_info); if ($account_info['status']=='ok') { @@ -111,6 +116,38 @@ function mss_checkConnectOption($optionType, $connectType) { ?>