xref: /PHP-8.2/ext/curl/curl_file.c (revision 0d013cd0)
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    | https://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: Stanislav Malyshev <stas@php.net>                            |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "Zend/zend_exceptions.h"
23 #include "curl_private.h"
24 #include "curl_file_arginfo.h"
25 
26 PHP_CURL_API zend_class_entry *curl_CURLFile_class;
27 PHP_CURL_API zend_class_entry *curl_CURLStringFile_class;
28 
curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)29 static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)
30 {
31 	zend_string *fname, *mime = NULL, *postname = NULL;
32 	zval *cf = return_value;
33 
34 	ZEND_PARSE_PARAMETERS_START(1,3)
35 		Z_PARAM_PATH_STR(fname)
36 		Z_PARAM_OPTIONAL
37 		Z_PARAM_STR_OR_NULL(mime)
38 		Z_PARAM_STR_OR_NULL(postname)
39 	ZEND_PARSE_PARAMETERS_END();
40 
41 	zend_update_property_str(curl_CURLFile_class, Z_OBJ_P(cf), "name", sizeof("name")-1, fname);
42 
43 	if (mime) {
44 		zend_update_property_str(curl_CURLFile_class, Z_OBJ_P(cf), "mime", sizeof("mime")-1, mime);
45 	}
46 
47 	if (postname) {
48 		zend_update_property_str(curl_CURLFile_class, Z_OBJ_P(cf), "postname", sizeof("postname")-1, postname);
49 	}
50 }
51 
52 /* {{{ Create the CURLFile object */
ZEND_METHOD(CURLFile,__construct)53 ZEND_METHOD(CURLFile, __construct)
54 {
55 	return_value = ZEND_THIS;
56 	curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
57 }
58 /* }}} */
59 
60 /* {{{ Create the CURLFile object */
PHP_FUNCTION(curl_file_create)61 PHP_FUNCTION(curl_file_create)
62 {
63 	object_init_ex( return_value, curl_CURLFile_class );
64 	curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
65 }
66 /* }}} */
67 
curlfile_get_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)68 static void curlfile_get_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
69 {
70 	zval *res, rv;
71 
72 	ZEND_PARSE_PARAMETERS_NONE();
73 	res = zend_read_property(curl_CURLFile_class, Z_OBJ_P(ZEND_THIS), name, name_len, 1, &rv);
74 	RETURN_COPY_DEREF(res);
75 }
76 
curlfile_set_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)77 static void curlfile_set_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
78 {
79 	zend_string *arg;
80 
81 	ZEND_PARSE_PARAMETERS_START(1,1)
82 		Z_PARAM_STR(arg)
83 	ZEND_PARSE_PARAMETERS_END();
84 
85 	zend_update_property_str(curl_CURLFile_class, Z_OBJ_P(ZEND_THIS), name, name_len, arg);
86 }
87 
88 /* {{{ Get file name */
ZEND_METHOD(CURLFile,getFilename)89 ZEND_METHOD(CURLFile, getFilename)
90 {
91 	curlfile_get_property("name", sizeof("name")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
92 }
93 /* }}} */
94 
95 /* {{{ Get MIME type */
ZEND_METHOD(CURLFile,getMimeType)96 ZEND_METHOD(CURLFile, getMimeType)
97 {
98 	curlfile_get_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
99 }
100 /* }}} */
101 
102 /* {{{ Get file name for POST */
ZEND_METHOD(CURLFile,getPostFilename)103 ZEND_METHOD(CURLFile, getPostFilename)
104 {
105 	curlfile_get_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
106 }
107 /* }}} */
108 
109 /* {{{ Set MIME type */
ZEND_METHOD(CURLFile,setMimeType)110 ZEND_METHOD(CURLFile, setMimeType)
111 {
112 	curlfile_set_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
113 }
114 /* }}} */
115 
116 /* {{{ Set file name for POST */
ZEND_METHOD(CURLFile,setPostFilename)117 ZEND_METHOD(CURLFile, setPostFilename)
118 {
119 	curlfile_set_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
120 }
121 /* }}} */
122 
ZEND_METHOD(CURLStringFile,__construct)123 ZEND_METHOD(CURLStringFile, __construct)
124 {
125 	zend_string *data, *postname, *mime = NULL;
126 	zval *object;
127 
128 	object = ZEND_THIS;
129 
130 	ZEND_PARSE_PARAMETERS_START(2,3)
131 		Z_PARAM_STR(data)
132 		Z_PARAM_STR(postname)
133 		Z_PARAM_OPTIONAL
134 		Z_PARAM_STR(mime)
135 	ZEND_PARSE_PARAMETERS_END();
136 
137 	zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "data", sizeof("data") - 1, data);
138 	zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "postname", sizeof("postname")-1, postname);
139 	if (mime) {
140 		zend_update_property_str(curl_CURLStringFile_class, Z_OBJ_P(object), "mime", sizeof("mime")-1, mime);
141 	} else {
142 		zend_update_property_string(curl_CURLStringFile_class, Z_OBJ_P(object), "mime", sizeof("mime")-1, "application/octet-stream");
143 	}
144 }
145 
curlfile_register_class(void)146 void curlfile_register_class(void)
147 {
148 	curl_CURLFile_class = register_class_CURLFile();
149 
150 	curl_CURLStringFile_class = register_class_CURLStringFile();
151 }
152