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    | Author: Omar Shaban <omars@php.net>                                  |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php_solr.h"
18 
19 extern zend_class_entry *solr_ce_SolrExtractRequest;
20 extern zend_object_handlers solr_extract_request_object_handlers;
21 extern zend_class_entry *solr_ce_SolrIllegalArgumentException;
22 extern zend_class_entry *solr_ce_SolrIllegalOperationException;
23 extern HashTable *ustreams;
24 
25 #define SOLR_EXTRACT_OBJ_CTOR() { \
26     do { \
27         object_init_ex(return_value, solr_ce_SolrExtractRequest); \
28         stream_entry = Z_USTREAM_P(return_value); \
29         stream_entry->params = Z_REFVAL_P(params); \
30         Z_ADDREF_P(stream_entry->params); \
31     } while(0); \
32 }
33 
solr_init_ustream(zval * obj)34 PHP_SOLR_API int solr_init_ustream(zval *obj)
35 {
36     zend_ulong index = SOLR_UNIQUE_USTREAM_INDEX();
37     solr_ustream_t *ustream_entry = NULL;
38 
39     ustream_entry = pemalloc(sizeof(solr_ustream_t), 0);
40 
41     if (zend_hash_index_update_ptr(SOLR_GLOBAL(ustreams), index, ustream_entry) == NULL) {
42         return FAILURE;
43     }
44 
45     return SUCCESS;
46 }
47 
48 /* {{{ solr_extract_obj_handlers */
solr_extract_free_object_handler(zend_object * obj)49 static void solr_extract_free_object_handler(zend_object *obj)
50 {
51     solr_ustream_t *intern = solr_get_ustream_object(obj);
52 
53     solr_string_free(&(intern->content_info->filename));
54     solr_string_free(&(intern->content_info->stream_info.binary_content));
55     solr_string_free(&(intern->content_info->stream_info.mime_type));
56 
57     Z_DELREF_P(intern->params);
58 
59     efree(intern->content_info);
60     zend_object_std_dtor(obj);
61     efree(intern);
62 }
63 
solr_extract_create_object_handler(zend_class_entry * ce)64 zend_object *solr_extract_create_object_handler(zend_class_entry *ce)
65 {
66     solr_ustream_t *intern = ecalloc(1, sizeof(solr_ustream_t)+zend_object_properties_size(ce));
67     memset(intern, 0, sizeof(solr_ustream_t));
68 
69     zend_object_std_init(&intern->std, ce);
70     object_properties_init(&intern->std, ce);
71 
72     intern->content_info = emalloc(sizeof(solr_cuv_t));
73 
74     solr_string_init(&(intern->content_info->stream_info.mime_type));
75     solr_string_init(&(intern->content_info->stream_info.binary_content));
76 
77     solr_extract_request_object_handlers.free_obj = solr_extract_free_object_handler;
78 
79     intern->std.handlers = &solr_extract_request_object_handlers;
80 
81     return &intern->std;
82 }
83 /* }}} */
84 
PHP_METHOD(SolrExtractRequest,__construct)85 PHP_METHOD(SolrExtractRequest, __construct)
86 {
87 }
88 
PHP_METHOD(SolrExtractRequest,__destruct)89 PHP_METHOD(SolrExtractRequest, __destruct)
90 {
91 }
92 
93 /* {{{ proto SolrExtractRequest::createFromFile(string filename, SolrModifiableParams params)
94    Create request from file */
PHP_METHOD(SolrExtractRequest,createFromFile)95 PHP_METHOD(SolrExtractRequest, createFromFile)
96 {
97     char *filename;
98     COMPAT_ARG_SIZE_T filename_length = 0;
99     solr_ustream_t *stream_entry = NULL;
100     zval *params = NULL;
101     zend_error_handling error_handling;
102 
103     zend_replace_error_handling(EH_THROW, solr_ce_SolrIllegalArgumentException, &error_handling);
104 
105     if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &filename, &filename_length, &params) == FAILURE) {
106         zend_restore_error_handling(&error_handling);
107         return;
108     }
109 
110     SOLR_EXTRACT_OBJ_CTOR();
111 
112     stream_entry->content_type = SOLR_EXTRACT_CONTENT_FILE;
113 
114     solr_string_set_ex(&(stream_entry->content_info->filename), filename, filename_length);
115 }
116 
117 /* {{{ proto SolrExtractRequest::createFromStream(string content, string contentType, SolrModifiableParams params)
118    Create request from binary stream */
PHP_METHOD(SolrExtractRequest,createFromStream)119 PHP_METHOD(SolrExtractRequest, createFromStream)
120 {
121     char *content, *content_type;
122     COMPAT_ARG_SIZE_T content_length = 0, content_type_length = 0;
123     solr_ustream_t *stream_entry = NULL;
124     zval *params = NULL;
125     zend_error_handling error_handling;
126 
127     zend_replace_error_handling(EH_THROW, solr_ce_SolrIllegalArgumentException, &error_handling);
128     if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssz", &content, &content_length, &content_type, &content_type_length, &params) == FAILURE) {
129         zend_restore_error_handling(&error_handling);
130         return;
131     }
132 
133     SOLR_EXTRACT_OBJ_CTOR();
134 
135     stream_entry->content_type = SOLR_EXTRACT_CONTENT_STREAM;
136     solr_string_set_ex(&(stream_entry->content_info->stream_info.mime_type), content_type, content_type_length);
137     solr_string_set_ex(&(stream_entry->content_info->stream_info.binary_content), content, content_length);
138 }
139 /* }}} */
140 
141 /* {{{ proto SolrExtractRequest::__clone(void)
142    Should not be called directly. Cloning is not supported. */
PHP_METHOD(SolrExtractRequest,__clone)143 PHP_METHOD(SolrExtractRequest, __clone)
144 {
145     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_4001, SOLR_FILE_LINE_FUNC, "Cloning of SolrExtractRequest objects is currently not supported");
146 }
147 /* }}} */
148 
149 /* {{{ proto SolrExtractRequest::__sleep(void)
150    Should not be called directly. Serialization is not supported. */
PHP_METHOD(SolrExtractRequest,__sleep)151 PHP_METHOD(SolrExtractRequest, __sleep)
152 {
153     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_4001, SOLR_FILE_LINE_FUNC, "SolrExtractRequest objects cannot be serialized or unserialized");
154 }
155 /* }}} */
156 
157 /* {{{ proto SolrExtractRequest::__wakeup(void)
158    Should not be called directly. Deserialization is not supported. */
PHP_METHOD(SolrExtractRequest,__wakeup)159 PHP_METHOD(SolrExtractRequest, __wakeup)
160 {
161     solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_4001, SOLR_FILE_LINE_FUNC, "SolrExtractRequest objects cannot be serialized or unserialized");
162 }
163 /* }}} */
164