forked from jaeksoft/opensearchserver-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_abstract.class.php
More file actions
286 lines (249 loc) · 9.5 KB
/
oss_abstract.class.php
File metadata and controls
286 lines (249 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
* This file is part of OpenSearchServer PHP Client.
*
* Copyright (C) 2008-2013 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* OpenSearchServer PHP Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer PHP Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenSearchServer PHP Client. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class OssAbstract {
protected $enginePath;
protected $index;
protected $login;
protected $apiKey;
protected $lastQueryString;
public function init($enginePath, $index = NULL, $login = NULL, $apiKey = NULL) {
if (!function_exists('OssApi_Dummy_Function')) {
function OssApi_Dummy_Function() {
}
}
$this->lastQueryString = null;
$this->enginePath = $enginePath;
$this->index = $index;
$this->credential($login, $apiKey);
}
/**
* Assign credential information for the next queries
* @param $login string
* @param $apiKey string
* If $login is empty, credential is removed
*/
public function credential($login, $apiKey) {
// Remove credentials
if (empty($login)) {
$this->login = NULL;
$this->apiKey = NULL;
return;
}
// Else parse and affect new credentials
if (empty($login) || empty($apiKey)) {
if (class_exists('OssException')) {
throw new UnexpectedValueException('You must provide a login and an api key to use credential.');
}
trigger_error(__CLASS__ . '::' . __METHOD__ . ': You must provide a login and an api key to use credential.', E_USER_ERROR);
return FALSE;
}
$this->login = $login;
$this->apiKey = $apiKey;
}
/**
* Return the url to use with curl
* param string $apiCall The Web API to call. Refer to the OSS Wiki documentation of [Web API]
* param string[] $options Additional query parameters
* return string
* Optionals query parameters are provided as a named list:
* array(
* "arg1" => "value1",
* "arg2" => "value2"
* )
*/
protected function getQueryURL($apiCall, $options = NULL) {
$path = $this->enginePath . '/' . $apiCall;
$chunks = array();
if (!empty($this->index)) {
$chunks[] = 'use=' . urlencode($this->index);
}
// If credential provided, include them in the query url
if (!empty($this->login)) {
$chunks[] = "login=" . urlencode($this->login);
$chunks[] = "key=" . urlencode($this->apiKey);
}
// Prepare additionnal parameters
if (is_array($options)) {
foreach ($options as $argName => $argValue) {
$chunks[] = $argName . "=" . urlencode($argValue);
}
} else if ($options != null) {
$chunks[] = $options;
}
$path .= (strpos($path, '?') !== FALSE ? '&' : '?') . implode('&', $chunks);
return $path;
}
/**
* @return string The parsed engine path
*/
public function getEnginePath() {
return $this->enginePath;
}
/**
* @return string The parsed index (NULL if not specified)
*/
public function getIndex() {
return $this->index;
}
/**
* Post data to an URL
* @param string $url
* @param string $data Optional. If provided will use a POST method. Only accept
* data as POST encoded string or raw XML string.
* @param int $timeout Optional. Number of seconds before the query fail
* @return FALSE|string
*
* Will fail if more than 16 HTTP redirection
*/
protected function queryServer($url, $data = NULL, $connexionTimeout = OssApi::DEFAULT_CONNEXION_TIMEOUT, $timeout = OssApi::DEFAULT_QUERY_TIMEOUT, $method = 'GET') {
$this->lastQueryString = $url;
// Use CURL to post the data
$rcurl = curl_init($url);
if($method === 'GET') {
curl_setopt($rcurl, CURLOPT_HTTP_VERSION, '1.0');
}
curl_setopt($rcurl, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($rcurl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($rcurl, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($rcurl, CURLOPT_MAXREDIRS, 16);
curl_setopt($rcurl, CURLOPT_VERBOSE, FALSE);
if ($method === 'PUT') {
curl_setopt($rcurl, CURLOPT_PUT, TRUE);
}
if (is_integer($connexionTimeout) && $connexionTimeout >= 0) {
curl_setopt($rcurl, CURLOPT_CONNECTTIMEOUT, $connexionTimeout);
}
if (is_integer($timeout) && $timeout >= 0) {
curl_setopt($rcurl, CURLOPT_TIMEOUT, $timeout);
}
// Send provided string as POST data. Must be encoded to meet POST specification
if ($data !== NULL) {
curl_setopt($rcurl, CURLOPT_POST, TRUE);
curl_setopt($rcurl, CURLOPT_POSTFIELDS, (string)$data);
curl_setopt($rcurl, CURLOPT_HTTPHEADER, array("Content-type: text/xml; charset=utf-8"));
}
set_error_handler('OssApi_Dummy_Function', E_ALL);
$content = curl_exec($rcurl);
restore_error_handler();
if ($content === FALSE) {
if (class_exists('OssException')) {
throw new RuntimeException('CURL failed to execute on URL "' . $url . '"');
}
trigger_error('CURL failed to execute on URL "' . $url . '"', E_USER_WARNING);
return FALSE;
}
$aResponse = curl_getinfo($rcurl);
// Must check return code
if ($aResponse['http_code'] >= 400) {
if (class_exists('OssException')) {
throw new TomcatException($aResponse['http_code'], $content);
}
trigger_error('HTTP ERROR ' . $aResponse['http_code'] . ': "' . trim(strip_tags($content)) . '"', E_USER_WARNING);
return FALSE;
}
// FIXME Possible problem to identify Locked Index message. Must set a lock on an index to check this
if ($this->isOSSError($content)) {
if (class_exists('OssException')) {
throw new OssException($content);
}
trigger_error('OSS Returned an error: "' . trim(strip_tags($content)) . '"', E_USER_WARNING);
return FALSE;
}
return $content;
}
public function getLastQueryString() {
return $this->lastQueryString;
}
protected function queryServerTXT($path, $params = null, $data = null, $connexionTimeout = OssApi::DEFAULT_CONNEXION_TIMEOUT, $timeout = OssApi::DEFAULT_QUERY_TIMEOUT) {
return $this->queryServer($this->getQueryURL($path, $params), $data, $connexionTimeout, $timeout);
}
protected function queryServerREST($path, $params = null, $data = null, $connexionTimeout = OssApi::DEFAULT_CONNEXION_TIMEOUT, $timeout = OssApi::DEFAULT_QUERY_TIMEOUT, $method = 'GET') {
return $this->queryServer($this->getQueryURL($path, $params), $data, $connexionTimeout, $timeout, $method);
}
/**
* Post data to an URL and retrieve an XML
* @param string $url
* @param string $data Optional. If provided will use a POST method. Only accept
* data as POST encoded string or raw XML string.
* @param int $timeout Optional. Number of seconds before the query fail
* @return SimpleXMLElement
* Use queryServer to retrieve an XML and check its validity
*/
protected function queryServerXML($path, $params, $data = NULL, $connexionTimeout = OssApi::DEFAULT_CONNEXION_TIMEOUT, $timeout = OssApi::DEFAULT_QUERY_TIMEOUT) {
$result = $this->queryServerTXT($path, $params, $data, $connexionTimeout, $timeout);
if ($result === FALSE) {
return FALSE;
}
// Check if we have a valid XML string from the engine
$lastErrorLevel = error_reporting(0);
$xmlResult = simplexml_load_string(OssApi::cleanUTF8($result));
error_reporting($lastErrorLevel);
if (!$xmlResult instanceof SimpleXMLElement) {
if (class_exists('OssException')) {
throw new RuntimeException("The search engine didn't return a valid XML");
}
trigger_error("The search engine didn't return a valid XML", E_USER_WARNING);
return FALSE;
}
return $xmlResult;
}
/**
* Check if the answer is an error returned by OSS
* @param $xml string, DOMDocument or SimpleXMLElement
* @return boolean True if error success
*/
protected function isOSSError($xml) {
// Cast $xml param to be a SimpleXMLElement
// If we don't find the word 'Error' in the xml string, exit immediatly
if ($xml instanceof SimpleXMLElement) {
if (strpos((string)$xml, 'Error') === FALSE) {
return FALSE;
}
$xmlDoc = $xml;
}
elseif ($xml instanceof DOMDocument) {
$xmlDoc = simplexml_import_dom($xml);
if (strpos((string)$xmlDoc, 'Error') === FALSE) {
return FALSE;
}
}
else {
if (strpos((string)$xml, 'Error') === FALSE) {
return FALSE;
}
$previous_error_level = error_reporting(0);
$xmlDoc = simplexml_load_string($xml);
error_reporting($previous_error_level);
}
if (!$xmlDoc instanceof SimpleXMLElement) {
return FALSE;
}
// Make sure the Error we found was a Status Error
foreach ($xmlDoc->entry as $entry) {
if ($entry['key'] == 'Status' && $entry == 'Error') {
return TRUE;
}
}
return FALSE;
}
}
?>