1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors:                                                             |
14    |          Israel Ekpo <iekpo@php.net>                                 |
15    |          Omar Shaban <omars@php.net>                                 |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php_solr.h"
20 
21 /* {{{ Macro for extracting property values using string constants */
22 #define solr_read_response_object_property(objptr, name, silent, rv) zend_read_property(Z_OBJCE_P(objptr), OBJ_FOR_PROP(objptr), name, sizeof(name)-1, silent, rv)
23 /* }}} */
24 
25 /* {{{ proto int SolrResponse::getHttpStatus(void)
26    Returns the value of the http_status property. */
PHP_METHOD(SolrResponse,getHttpStatus)27 PHP_METHOD(SolrResponse, getHttpStatus)
28 {
29 	zend_bool silent = 1;
30 	zval rv;
31 	zval *objptr = getThis();
32 	zval *http_status = NULL;
33 
34 	http_status = solr_read_response_object_property(objptr, "http_status", silent, &rv);
35 
36 	RETURN_LONG(Z_LVAL_P(http_status));
37 }
38 /* }}} */
39 
40 /* {{{ proto string SolrResponse::getHttpStatusMessage(void)
41    Returns the http_status_message property. */
PHP_METHOD(SolrResponse,getHttpStatusMessage)42 PHP_METHOD(SolrResponse, getHttpStatusMessage)
43 {
44 	zend_bool silent = 1;
45 	zval rv;
46 	zval *objptr = getThis();
47 	zval *http_status_message = NULL;
48 
49 	http_status_message = solr_read_response_object_property(objptr, "http_status_message", silent, &rv);
50 
51 	RETURN_STRINGL(Z_STRVAL_P(http_status_message), Z_STRLEN_P(http_status_message));
52 }
53 /* }}} */
54 
55 /* {{{ proto bool SolrResponse::success(void)
56    Returns whether the request was successful or not. */
PHP_METHOD(SolrResponse,success)57 PHP_METHOD(SolrResponse, success)
58 {
59 	zend_bool silent = 1;
60 	zval rv;
61 	zval *objptr = getThis();
62 	zval *success = NULL;
63 
64 	success = solr_read_response_object_property(objptr, "success", silent, &rv);
65 	RETURN_ZVAL(success, 0, 0);
66 }
67 /* }}} */
68 
69 /* {{{ proto string SolrResponse::getRequestUrl(void)
70    Returns the URL used for the request. */
PHP_METHOD(SolrResponse,getRequestUrl)71 PHP_METHOD(SolrResponse, getRequestUrl)
72 {
73 	zend_bool silent = 1;
74 	zval rv;
75 	zval *objptr = getThis();
76 	zval *prop = NULL;
77 
78 	prop = solr_read_response_object_property(objptr, "http_request_url", silent, &rv);
79 
80 	RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
81 }
82 /* }}} */
83 
84 /* {{{ proto string SolrResponse::getRawRequestHeaders(void)
85    Returns the raw http request headers sent to the server. */
PHP_METHOD(SolrResponse,getRawRequestHeaders)86 PHP_METHOD(SolrResponse, getRawRequestHeaders)
87 {
88 	zend_bool silent = 1;
89 	zval rv;
90 	zval *objptr = getThis();
91 	zval *prop = NULL;
92 
93 	prop = solr_read_response_object_property(objptr, "http_raw_request_headers", silent, &rv);
94 
95 	RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
96 }
97 /* }}} */
98 
99 /* {{{ proto string SolrResponse::getRawRequest(void)
100    Returns the raw http request sent to the server. */
PHP_METHOD(SolrResponse,getRawRequest)101 PHP_METHOD(SolrResponse, getRawRequest)
102 {
103 	zend_bool silent = 1;
104 	zval rv;
105 	zval *objptr = getThis();
106 	zval *prop = NULL;
107 
108 	prop = solr_read_response_object_property(objptr, "http_raw_request", silent, &rv);
109 
110 	RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
111 }
112 /* }}} */
113 
114 /* {{{ proto string SolrResponse::getRawResponseHeaders(void)
115    Returns the raw http response headers from the server. */
PHP_METHOD(SolrResponse,getRawResponseHeaders)116 PHP_METHOD(SolrResponse, getRawResponseHeaders)
117 {
118 	zend_bool silent = 1;
119 	zval rv;
120 	zval *objptr = getThis();
121 	zval *prop = NULL;
122 
123 	prop = solr_read_response_object_property(objptr, "http_raw_response_headers", silent, &rv);
124 
125 	RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
126 }
127 /* }}} */
128 
129 /* {{{ proto string SolrResponse::getRawResponse(void)
130    Returns the raw http response from the server. */
PHP_METHOD(SolrResponse,getRawResponse)131 PHP_METHOD(SolrResponse, getRawResponse)
132 {
133 	zend_bool silent = 1;
134 	zval rv;
135 	zval *objptr = getThis();
136 	zval *prop = NULL;
137 
138 	prop = solr_read_response_object_property(objptr, "http_raw_response", silent, &rv);
139 
140 	if (Z_STRLEN_P(prop))
141 	{
142 		RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
143 	}
144 
145 	RETURN_NULL();
146 }
147 /* }}} */
148 
149 /* {{{ proto string SolrResponse::getDigestedResponse(void)
150    Returns the serialized object string derived from the XML response. */
PHP_METHOD(SolrResponse,getDigestedResponse)151 PHP_METHOD(SolrResponse, getDigestedResponse)
152 {
153 	zend_bool silent = 0;
154 	zval rv;
155 	zval *objptr = getThis();
156 	zval *prop = NULL;
157 
158 	prop = solr_read_response_object_property(objptr, "http_digested_response", silent, &rv);
159 
160 	if (Z_STRLEN_P(prop))
161 	{
162 		RETURN_STRINGL(Z_STRVAL_P(prop), Z_STRLEN_P(prop));
163 	}
164 
165 	RETURN_NULL();
166 }
167 /* }}} */
168 
169 /* {{{ proto string SolrReponse::setParseMode([bool parse_mode])
170    Sets the parsing mode. This determines whether documents will be parsed as SolrObjects or SolrDocuments. */
PHP_METHOD(SolrResponse,setParseMode)171 PHP_METHOD(SolrResponse, setParseMode)
172 {
173 	long int parse_mode = 0L;
174 	zval *objptr = getThis();
175 
176 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &parse_mode) == FAILURE) {
177 
178 		RETURN_FALSE;
179 	}
180 
181 	parse_mode = ((parse_mode < 0L) ? 0L : ((parse_mode > 1L) ? 1L : parse_mode));
182 
183 	zend_update_property_long(Z_OBJCE_P(objptr), OBJ_FOR_PROP(objptr), "parser_mode", sizeof("parser_mode")-1, parse_mode);
184 
185 	RETURN_TRUE;
186 }
187 /* }}} */
188 
189 /* {{{ proto SolrObject SolrResponse::getResponse(void)
190    Returns the response object from the server. */
PHP_METHOD(SolrResponse,getResponse)191 PHP_METHOD(SolrResponse, getResponse)
192 {
193     solr_response_get_response_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
194 }
195 /* }}} */
196 
197 /* {{{ proto array SolrResponse::getArrayResponse(void)
198    Returns the response array from the server. */
PHP_METHOD(SolrResponse,getArrayResponse)199 PHP_METHOD(SolrResponse, getArrayResponse)
200 {
201     solr_response_get_response_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
202 }
203 /* }}} */
204 
205 /**
206  * Digest solr server raw response
207  * and return SolrObject or array
208  */
solr_response_get_response_impl(INTERNAL_FUNCTION_PARAMETERS,int return_array)209 PHP_SOLR_API void solr_response_get_response_impl(
210         INTERNAL_FUNCTION_PARAMETERS,
211         int return_array)
212 {
213     zend_bool silent = 0;
214     zval *objptr = getThis();
215     zval rv;
216     zval *response_writer = solr_read_response_object_property(objptr, "response_writer", silent, &rv);
217     zval *raw_response = solr_read_response_object_property(objptr, "http_raw_response", silent, &rv);
218     zval *success = solr_read_response_object_property(objptr, "success", silent, &rv);
219     zval *parser_mode = solr_read_response_object_property(objptr, "parser_mode", silent, &rv);
220 
221     if (Z_TYPE_P(success) == IS_TRUE && Z_STRLEN_P(raw_response))
222     {
223         solr_string_t buffer;
224         php_unserialize_data_t var_hash;
225         const unsigned char *raw_resp;
226         size_t raw_res_length;
227         const unsigned char *str_end;
228         int successful = 1;
229 
230         memset(&buffer, 0, sizeof(solr_string_t));
231 
232         if (Z_STRLEN_P(response_writer))
233         {
234             if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_XML_RESPONSE_WRITER))
235             {
236                 /* SOLR_XML_RESPONSE_WRITER */
237 
238                 /* Convert from XML serialization to PHP serialization format */
239                 solr_encode_generic_xml_response(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response), Z_LVAL_P(parser_mode));
240                 if(return_array)
241                 {
242                     solr_sobject_to_sarray(&buffer);
243                 }
244             } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_PHP_NATIVE_RESPONSE_WRITER) || 0 == strcmp(Z_STRVAL_P(response_writer), SOLR_PHP_SERIALIZED_RESPONSE_WRITER)) {
245 
246                 /* SOLR_PHP_NATIVE_RESPONSE_WRITER */
247 
248                 /* Response string is already in Native PHP serialization format */
249                 solr_string_set(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response));
250 
251                 if(!return_array)
252                 {
253                     solr_sarray_to_sobject(&buffer);
254                 }
255 
256             } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_JSON_RESPONSE_WRITER)) {
257 
258                 int json_translation_result = solr_json_to_php_native(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response));
259 
260                 /* SOLR_JSON_RESPONSE_WRITER */
261 
262                 /* Convert from JSON serialization to PHP serialization format */
263                 if (json_translation_result > 0)
264                 {
265                     solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000, SOLR_FILE_LINE_FUNC, solr_get_json_error_msg(json_translation_result));
266 
267                     php_error_docref(NULL, E_WARNING, "Error in JSON->PHP conversion. JSON Error Code %d", json_translation_result);
268                 }
269 
270                 if(!return_array)
271                 {
272                     solr_sarray_to_sobject(&buffer);
273                 }
274             }
275         }
276 
277         if (buffer.len)
278         {
279             zend_update_property_stringl(Z_OBJCE_P(objptr), OBJ_FOR_PROP(objptr), "http_digested_response", sizeof("http_digested_response")-1, buffer.str, buffer.len);
280         }
281 
282         memset(&var_hash, 0, sizeof(php_unserialize_data_t));
283 
284         PHP_VAR_UNSERIALIZE_INIT(var_hash);
285 
286         raw_resp = (unsigned char *) buffer.str;
287         raw_res_length = buffer.len;
288         str_end = (unsigned char *) (raw_resp + raw_res_length);
289 
290         if (!php_var_unserialize(return_value, &raw_resp, str_end, &var_hash))
291         {
292             successful = 0;
293 
294             solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);
295 
296             php_error_docref(NULL, E_WARNING, "Error unserializing raw response.");
297         }
298 
299         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
300 
301         solr_string_free(&buffer);
302 
303         if (successful && !return_array)
304         {
305             /* Overriding the default object handlers */
306             Z_OBJ_HT_P(return_value) = &solr_object_handlers;
307         }
308 
309         return ;
310     }
311 
312     RETURN_NULL();
313 }
314 
315 /* {{{ proto string SolrPingResponse::getResponse(void)
316   Ping responses are always empty. Returns null. */
PHP_METHOD(SolrPingResponse,getResponse)317 PHP_METHOD(SolrPingResponse, getResponse)
318 {
319 	/* Ping responses are not serialized */
320 	RETURN_NULL();
321 }
322 /* }}} */
323 
324 /* {{{ proto SolrPingResponse::__construct(void)
325    Constructor */
PHP_METHOD(SolrPingResponse,__construct)326 PHP_METHOD(SolrPingResponse, __construct)
327 {
328 
329 }
330 /* }}} */
331 
332 /* {{{ proto SolrPingResponse::__destruct(void)
333    Destructor */
PHP_METHOD(SolrPingResponse,__destruct)334 PHP_METHOD(SolrPingResponse, __destruct)
335 {
336 
337 }
338 /* }}} */
339 
340 /* {{{ proto SolrQueryResponse::__construct(void)
341    Constructor */
PHP_METHOD(SolrQueryResponse,__construct)342 PHP_METHOD(SolrQueryResponse, __construct)
343 {
344 
345 }
346 /* }}} */
347 
348 /* {{{ proto SolrQueryResponse::__destruct(void)
349    Destructor */
PHP_METHOD(SolrQueryResponse,__destruct)350 PHP_METHOD(SolrQueryResponse, __destruct)
351 {
352 
353 }
354 /* }}} */
355 
356 /* {{{ proto SolrUpdateResponse::__construct(void)
357    Constructor */
PHP_METHOD(SolrUpdateResponse,__construct)358 PHP_METHOD(SolrUpdateResponse, __construct)
359 {
360 
361 }
362 /* }}} */
363 
364 /* {{{ proto SolrUpdateResponse::__destruct(void)
365    Destructor */
PHP_METHOD(SolrUpdateResponse,__destruct)366 PHP_METHOD(SolrUpdateResponse, __destruct)
367 {
368 
369 }
370 /* }}} */
371 
372 /* {{{ proto SolrGenericResponse::__construct(void)
373    Constructor */
PHP_METHOD(SolrGenericResponse,__construct)374 PHP_METHOD(SolrGenericResponse, __construct)
375 {
376 
377 }
378 /* }}} */
379 
380 /* {{{ proto SolrGenericResponse::__destruct(void)
381    Destructor */
PHP_METHOD(SolrGenericResponse,__destruct)382 PHP_METHOD(SolrGenericResponse, __destruct)
383 {
384 
385 }
386 /* }}} */
387 
388 /*
389  * Local variables:
390  * tab-width: 4
391  * c-basic-offset: 4
392  * indent-tabs-mode: t
393  * End:
394  * vim600: fdm=marker
395  * vim: noet sw=4 ts=4
396  */
397