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 #ifndef SOLR_TYPES_H
20 #define SOLR_TYPES_H
21 
22 #include <main/php.h>
23 #include <ext/standard/url.h>
24 
25 #include <curl/curl.h>
26 #include <curl/curlver.h>
27 #include <curl/easy.h>
28 
29 /* Declaring workarounds for SSL private key constants */
30 #if LIBCURL_VERSION_NUM >= 0x071700
31 /* Use CURLOPT_KEYPASSWD as is */
32 #elif LIBCURL_VERSION_NUM >= 0x070903
33 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
34 #else
35 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
36 #endif
37 
38 /* Declaring workarounds for ACCEPT ENCODING constants */
39 #if LIBCURL_VERSION_NUM >= 0x071506
40 /* Use CURLOPT_ACCEPT_ENCODING as is */
41 #else
42 #define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
43 #endif
44 
45 #include <Zend/zend_hash.h>
46 #include <Zend/zend.h>
47 #include <Zend/zend_API.h>
48 
49 #include <libxml/parser.h>
50 #include <libxml/parserInternals.h>
51 #include <libxml/tree.h>
52 
53 typedef unsigned char solr_bool;
54 
55 #include "solr_string.h"
56 
57 /* {{{ types for the http client */
58 typedef enum {
59 
60 	SOLR_REQUEST_BEGIN   = 0,
61 	SOLR_REQUEST_SEARCH  = 1,    /** 1 **/
62 	SOLR_REQUEST_UPDATE  = 2,    /** 2 **/
63 	SOLR_REQUEST_THREADS = 3,    /** 3 **/
64 	SOLR_REQUEST_PING    = 4,    /** 4 **/
65 	SOLR_REQUEST_TERMS   = 5,    /** 5 **/
66 	SOLR_REQUEST_SYSTEM  = 6,    /** 6 **/
67 	SOLR_REQUEST_GET     = 7,
68 	SOLR_REQUEST_EXTRACT = 8,
69 	SOLR_REQUEST_END
70 
71 } solr_request_type_t;
72 
73 typedef enum {
74 
75 	SOLR_SERVLET_TYPE_BEGIN   = 0,
76 	SOLR_SERVLET_TYPE_SEARCH  = 1,
77 	SOLR_SERVLET_TYPE_UPDATE  = 2,
78 	SOLR_SERVLET_TYPE_THREADS = 4,
79 	SOLR_SERVLET_TYPE_PING    = 8,
80 	SOLR_SERVLET_TYPE_TERMS   = 16,
81 	SOLR_SERVLET_TYPE_SYSTEM  = 32,
82 	SOLR_SERVLET_TYPE_GET     = 64,
83 	SOLR_SERVLET_TYPE_EXTRACT = 128,
84 	SOLR_SERVLET_TYPE_END
85 
86 } solr_servlet_type_t;
87 
88 typedef enum
89 {
90 	SOLR_PARAM_TYPE_BEGIN 		= 0,
91 	SOLR_PARAM_TYPE_NORMAL 		= 1,
92 	SOLR_PARAM_TYPE_SIMPLE_LIST = 2,
93 	SOLR_PARAM_TYPE_ARG_LIST 	= 4,
94 	SOLR_PARAM_TYPE_END
95 
96 } solr_param_type_t;
97 
98 typedef enum
99 {
100 	SOLR_SORT_DIR_ASC  = 0,
101 	SOLR_SORT_DIR_DESC = 1
102 
103 } solr_sort_dir_t;
104 
105 typedef enum  {
106     SOLR_JSON_ERROR_NONE = 0,
107     SOLR_JSON_ERROR_DEPTH,
108     SOLR_JSON_ERROR_STATE_MISMATCH,
109     SOLR_JSON_ERROR_CTRL_CHAR,
110     SOLR_JSON_ERROR_SYNTAX,
111     SOLR_JSON_ERROR_UTF8,
112     SOLR_JSON_ERROR_SERIALIZATION
113 } solr_json_error_codes_t;
114 
115 /* document field value modifier */
116 typedef enum {
117     SOLR_FIELD_VALUE_MOD_NONE           = 0,
118     SOLR_FIELD_VALUE_MOD_ADD            = 1,
119     SOLR_FIELD_VALUE_MOD_SET            = 2,
120     SOLR_FIELD_VALUE_MOD_INC            = 3,
121     SOLR_FIELD_VALUE_MOD_REMOVE         = 4,
122     SOLR_FIELD_VALUE_MOD_REMOVEREGEX    = 5,
123 } solr_fvm_t;
124 
125 /* solr input document optimistic concurrency */
126 typedef enum {
127     SOLR_VERSION_ASSERT_NONE = 0,
128     SOLR_VERSION_ASSERT_EXISTS = 1,
129     SOLR_VERSION_ASSERT_NOT_EXISTS = -1
130 } solr_input_document_version_t;
131 
132 typedef struct curl_slist solr_http_header_list_t;
133 
134 typedef struct {
135 
136 	solr_string_t buffer;	/* Stores the HTTP request headers */
137 
138 } solr_curl_send_headers_t;
139 
140 typedef struct _solr_curl_send_data {
141 
142 	solr_string_t buffer; /* Stores the HTTP request data */
143 
144 } solr_curl_send_data_t;
145 
146 typedef struct {
147 
148 	solr_string_t buffer; /* Stores the HTTP response headers */
149 
150 	long int response_code; /* Stores the HTTP response code */
151 
152 } solr_curl_response_headers_t;
153 
154 typedef struct {
155 
156 	solr_string_t buffer;	/* Stores the HTTP response data */
157 
158 } solr_curl_response_data_t;
159 
160 typedef struct {
161 
162 	char str[CURL_ERROR_SIZE + 1]; /* Stores the error message */
163 
164 	uint32_t  number;	/* Stores the error number */
165 
166 } solr_curl_error_t;
167 
168 typedef struct {
169 
170 	solr_curl_send_headers_t request_header;
171 
172 	solr_curl_send_data_t request_body;
173 
174 	solr_curl_send_data_t request_body_debug;
175 
176 	solr_curl_response_headers_t response_header;
177 
178 	solr_curl_response_data_t response_body;
179 
180 	solr_curl_error_t err;
181 
182 	solr_string_t debug_data_buffer; /* Stores all the debug data */
183 
184 	size_t handle_status; /* Whether or not the handle is still usable */
185 
186 	CURLcode result_code;
187 
188 	CURL *curl_handle;
189 
190 } solr_curl_t;
191 
192 typedef struct {
193 
194 	long int host_port;						/* HTTP port for Solr server */
195 
196 	long int proxy_port;					/* Proxy port for Solr server */
197 
198 	long int timeout;						/* maximum time in seconds allowed for the http data transfer operation */
199 
200 	long int secure;						/* Whether to connect to Solr Server using SSL/HTTPS */
201 
202 	long int ssl_verify_peer;				/* Determines whether libcurl verifies the peer certificate */
203 
204 	long int ssl_verify_host;				/* Determines how libcurl verifies the server certificate */
205 
206 	solr_string_t hostname; 				/* IP address or domain name */
207 
208 	solr_string_t path; 					/* Path to solr */
209 
210 	solr_string_t http_auth_credentials; 	/* HTTP Auth username:password */
211 
212 	solr_string_t proxy_hostname;			/* IP address of domain name for the proxy server */
213 
214 	solr_string_t proxy_auth_credentials; 	/* Proxy Auth username:password */
215 
216 	solr_string_t ssl_cert;					/* File name to a PEM-formatted private key + private certificate (concatenated in that order) */
217 
218 	solr_string_t ssl_key;					/* File name to a PEM-formatted private key */
219 
220 	solr_string_t ssl_keypassword;			/* The pass phrase/password for the private ssl_key */
221 
222 	solr_string_t ssl_cainfo;				/* Name of file holding one or more certificates to verify peer with */
223 
224 	solr_string_t ssl_capath;				/* Name of directory holding multiple CA certificates to verify peer with */
225 
226 	solr_string_t qs_delimiter;				/* The query string delimiter */
227 
228 	solr_string_t response_writer;			/* The response writer type (wt parameter) */
229 
230 	solr_string_t update_url;				/* URL used for updates */
231 
232 	solr_string_t extract_url;              /* URL used for file indexing using extract */
233 
234 	solr_string_t search_url;				/* URL used for queries */
235 
236 	solr_string_t thread_url;				/* URL used for thread monitoring */
237 
238 	solr_string_t ping_url;					/* URL for sending ping requests */
239 
240 	solr_string_t terms_url;				/* URL for sending terms requests */
241 
242 	solr_string_t system_url;			    /* URL for sending system requests */
243 
244 	solr_string_t get_url;                  /* URL for sending realtime get requests */
245 
246 	solr_string_t update_servlet;			/* The update servlet */
247 
248 	solr_string_t extract_servlet;          /* The update servlet appended with extract Req. handler */
249 
250 	solr_string_t search_servlet;			/* The search servlet */
251 
252 	solr_string_t thread_servlet;			/* The thread servlet */
253 
254 	solr_string_t ping_servlet;				/* The ping servlet */
255 
256 	solr_string_t terms_servlet;			/* The terms servlet */
257 
258 	solr_string_t system_servlet;			/* The system info servlet */
259 
260 	solr_string_t get_servlet;              /* The realtime get servlet */
261 
262 } solr_client_options_t;
263 
264 typedef struct {
265 
266 	zend_ulong client_index;     	   	/* Unique for the SolrClient instance. Used as index in HashTables */
267 
268 	uint32_t request_count; 			/* The number of requests made from this client */
269 
270 	solr_client_options_t options;	/* The connection options for this client */
271 
272 	solr_curl_t handle;           	/* The Solr handle for this client */
273 
274 } solr_client_t;
275 
276 /* }}} */
277 
278 /* Types for documents and collections */
279 
280 /* {{{ Types for documents and collections */
281 
282 /* {{{ Types for SolrDocument fields */
283 typedef struct _solr_field_value_t
284 {
285 	solr_char_t *field_value;         /* The value for this field */
286 
287 	solr_fvm_t modifier;    /* field value modifier for partial updates */
288 
289 	struct _solr_field_value_t *next; /* The next entry in the queue */
290 
291 } solr_field_value_t;
292 
293 /**
294  * List of Fields in a Solr Document
295  *
296  * @field_boost stores the boost value
297  * @count stores how many fields there are in the list
298  * @field_name stores the name of the field
299  * @head the first entry in the list (if any)
300  * @last the last entry in the list (if any)
301  */
302 typedef struct {
303 
304 	double field_boost;         /* The boost value for this field */
305 
306 	uint32_t count;                 /* The number of values in this field */
307 
308 	uint32_t modified;              /* marks field as getting modified */
309 
310 	solr_char_t *field_name;   	/* The name of the field */
311 
312 	solr_field_value_t *head;  	/* The first value in the queue */
313 
314 	solr_field_value_t *last;  	/* The last value in the queue */
315 
316 } solr_field_list_t;
317 /* }}} */
318 
319 /* {{{ Types for Documents and Document Collections */
320 typedef struct {
321 
322 	zend_ulong document_index;   /* Unique for the SolrInputDocument instance. Used as index in HashTables */
323 
324 	uint32_t field_count;  		/* Number of fields in this document */
325 
326 	double document_boost;	/* The boost value for this document */
327 
328 	HashTable *fields;    	/* HashTable for storing all the solr_field_list_t fields */
329 
330 	HashTable *children;    /* Hashtable for storing child documents(zval *) */
331 
332 } solr_document_t;
333 
334 /* }}} */
335 
336 /* {{{ Types for Solr Query Parameters */
337 
338 typedef struct
339 {
340 	solr_string_t value;	/* The value for this parameter */
341 
342 	solr_string_t arg;		/* The argument for this value*/
343 
344 	solr_char_t *delimiter_override; /* overrides the arg_list delimiter for this particular argument */
345 
346 	solr_bool delimiter_overriden; /* is delimiter overriden */
347 
348 } solr_arg_list_value_t;
349 
350 typedef union
351 {
352 	solr_string_t	normal;			/* Stores normal parameters */
353 
354 	solr_string_t	simple_list;	/* Stores delimited list parameters */
355 
356 	solr_arg_list_value_t arg_list;		/* Stores parameters that have arguments */
357 
358 } solr_pvc_t;
359 
360 typedef struct _solr_param_value_t
361 {
362 	solr_pvc_t contents;				/* A union for the contents of this value */
363 
364 	struct _solr_param_value_t *prev;	/* The previous value in the list */
365 
366 	struct _solr_param_value_t *next;	/* The next value in the list */
367 
368 } solr_param_value_t;
369 
370 /* Checks if two parameter values are equal */
371 typedef int (*solr_param_value_equal_func_t)(const solr_param_value_t *a, const solr_param_value_t *b);
372 
373 /* Used to retrieve all the values for this parameter as a single solr string */
374 typedef void (*solr_param_fetch_func_t)(void *solr_param, solr_string_t *buffer);
375 
376 /* Frees the parameter value and its contents. */
377 typedef void (*solr_param_value_free_func_t)(solr_param_value_t *param_value);
378 
379 typedef struct {
380 
381 	solr_param_type_t type;							/* What type of query parameter this is */
382 
383 	uint32_t count;               		 				/* The number of values for this parameter */
384 
385 	solr_char_t *param_name;    					/* The name of the parameter */
386 
387 	size_t param_name_length;    					/* The length of name of the parameter */
388 
389 	solr_bool allow_multiple;						/* Whether this parameter should store more than one value */
390 
391 	solr_param_value_t *head;   					/* The first value in the list */
392 
393 	solr_param_value_t *last;   					/* The last value in the list */
394 
395 	solr_param_fetch_func_t fetch_func; 			/* Retrieves all the value contents as a single solr_string_t */
396 
397 	solr_param_value_free_func_t value_free_func;	/* Frees the value and its contents */
398 
399 	solr_param_value_equal_func_t value_equal_func;	/* Checks if two values are equal */
400 
401 	solr_char_t delimiter;							/* Default is "," Used to separate list parameter values */
402 
403 	solr_char_t arg_separator;						/* Used only be arg_lists to separate arguments from values */
404 
405 } solr_param_t;
406 
407 /* Used to retrieve parameter values just for display purposes */
408 typedef void (*solr_param_display_func_t)(solr_param_t *solr_param, zval *param_value_array);
409 
410 /* Used to retrieve parameter values just for the toString() method */
411 /* The url_encode parameter will be ignored for now */
412 typedef void (*solr_param_tostring_func_t)(solr_param_t *solr_param, solr_string_t *buffer, zend_bool url_encode);
413 
414 typedef struct {
415 
416 	zend_ulong params_index;	/* The index for this object in the HashTable */
417 
418 	uint32_t  params_count;	/* The number of parameters for the query object */
419 
420 	HashTable *params;	/* The HashTable for storing query parameters */
421 
422 } solr_params_t;
423 
424 /* }}} */
425 
426 /* {{{ solr function/localparams type */
427 typedef struct {
428 
429     zend_ulong function_index; /* The index for this object in the HashTable */
430 
431     solr_char_t *name;
432     size_t name_length;
433 
434     solr_char_t *argument;
435     size_t argument_length;
436 
437     HashTable *params;  /* The HashTable<solr_string_t> for storing function key-val parameters */
438 
439 } solr_function_t;
440 /* }}} */
441 
442 /* {{{ content stream/extract types */
443 
444 #define SOLR_EXTRACT_CONTENT_FILE 0
445 #define SOLR_EXTRACT_CONTENT_STREAM 1
446 
447 typedef struct {
448     solr_string_t binary_content;   /* actual contents */
449     solr_string_t mime_type;     /* mime type */
450 } solr_cuv_binary_t;
451 
452 typedef union {
453     solr_string_t filename;
454     solr_cuv_binary_t stream_info;
455 } solr_cuv_t;
456 
457 typedef struct {
458     solr_cuv_t *content_info;    /* stores the content data whether filename or stream_info */
459     int content_type;            /* stores the content type from the constants above */
460     zval *params;
461     zend_object std;
462 } solr_ustream_t;
463 /* }}} */
464 
465 /* }}} */
466 
467 /* {{{ Extension Global : This should be the last data type declared. More members may be added later. */
468 ZEND_BEGIN_MODULE_GLOBALS(solr)
469 
470 	uint32_t request_count;      /* The number of times PHP_RINIT has been called */
471 
472 	uint32_t document_count;     /* The number of active SolrDocuments in this request */
473 
474 	uint32_t client_count;	     /* The number of active SolrClients in this request */
475 
476 	uint32_t functions_count;    /* The number of active Functions in this request */
477 
478 	HashTable *documents;	 /* HashTable for storing solr_document_t documents */
479 
480 	HashTable *clients;      /* HashTable for storing solr_client_t clients */
481 
482 	HashTable *params;		 /* HashTable for storing solr_params_t parameter containers */
483 
484 	HashTable *functions;    /* HashTable for storing solr_function_t */
485 
486 	HashTable *ustreams;     /* HashTable for storing solr_ustream_t */
487 
488 ZEND_END_MODULE_GLOBALS(solr)
489 /* }}} */
490 
491 typedef struct {
492     int code;
493     solr_char_t *message;
494 } solr_exception_t;
495 
496 #endif /* SOLR_TYPES_H */
497 
498 /*
499  * Local variables:
500  * tab-width: 4
501  * c-basic-offset: 4
502  * End:
503  * vim600: fdm=marker
504  * vim: noet sw=4 ts=4
505  */
506