xref: /PHP-7.3/ext/curl/curl_file.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Stanislav Malyshev <stas@php.net>                            |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "Zend/zend_exceptions.h"
25 #include "php_curl.h"
26 #if HAVE_CURL
27 
28 PHP_CURL_API zend_class_entry *curl_CURLFile_class;
29 
curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)30 static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)
31 {
32 	zend_string *fname, *mime = NULL, *postname = NULL;
33 	zval *cf = return_value;
34 
35 	ZEND_PARSE_PARAMETERS_START(1,3)
36 		Z_PARAM_PATH_STR(fname)
37 		Z_PARAM_OPTIONAL
38 		Z_PARAM_STR(mime)
39 		Z_PARAM_STR(postname)
40 	ZEND_PARSE_PARAMETERS_END();
41 
42 	zend_update_property_string(curl_CURLFile_class, cf, "name", sizeof("name")-1, ZSTR_VAL(fname));
43 
44 	if (mime) {
45 		zend_update_property_string(curl_CURLFile_class, cf, "mime", sizeof("mime")-1, ZSTR_VAL(mime));
46 	}
47 
48 	if (postname) {
49 		zend_update_property_string(curl_CURLFile_class, cf, "postname", sizeof("postname")-1, ZSTR_VAL(postname));
50 	}
51 }
52 
53 /* {{{ proto CURLFile::__construct(string $name, [string $mimetype [, string $postfilename]])
54    Create the CURLFile object */
ZEND_METHOD(CURLFile,__construct)55 ZEND_METHOD(CURLFile, __construct)
56 {
57 	return_value = getThis();
58 	curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
59 }
60 /* }}} */
61 
62 /* {{{ proto CURLFile curl_file_create(string $name, [string $mimetype [, string $postfilename]])
63    Create the CURLFile object */
PHP_FUNCTION(curl_file_create)64 PHP_FUNCTION(curl_file_create)
65 {
66     object_init_ex( return_value, curl_CURLFile_class );
67     curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
68 }
69 /* }}} */
70 
curlfile_get_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)71 static void curlfile_get_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
72 {
73 	zval *res, rv;
74 
75 	if (zend_parse_parameters_none() == FAILURE) {
76 		return;
77 	}
78 	res = zend_read_property(curl_CURLFile_class, getThis(), name, name_len, 1, &rv);
79 	ZVAL_COPY_DEREF(return_value, res);
80 }
81 
curlfile_set_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)82 static void curlfile_set_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
83 {
84 	zend_string *arg;
85 
86 	ZEND_PARSE_PARAMETERS_START(1,1)
87 		Z_PARAM_STR(arg)
88 	ZEND_PARSE_PARAMETERS_END();
89 
90 	zend_update_property_string(curl_CURLFile_class, getThis(), name, name_len, ZSTR_VAL(arg));
91 }
92 
93 /* {{{ proto string CURLFile::getFilename()
94    Get file name */
ZEND_METHOD(CURLFile,getFilename)95 ZEND_METHOD(CURLFile, getFilename)
96 {
97 	curlfile_get_property("name", sizeof("name")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
98 }
99 /* }}} */
100 
101 /* {{{ proto string CURLFile::getMimeType()
102    Get MIME type */
ZEND_METHOD(CURLFile,getMimeType)103 ZEND_METHOD(CURLFile, getMimeType)
104 {
105 	curlfile_get_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
106 }
107 /* }}} */
108 
109 /* {{{ proto string CURLFile::getPostFilename()
110    Get file name for POST */
ZEND_METHOD(CURLFile,getPostFilename)111 ZEND_METHOD(CURLFile, getPostFilename)
112 {
113 	curlfile_get_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
114 }
115 /* }}} */
116 
117 /* {{{ proto void CURLFile::setMimeType(string $mime)
118    Set MIME type */
ZEND_METHOD(CURLFile,setMimeType)119 ZEND_METHOD(CURLFile, setMimeType)
120 {
121 	curlfile_set_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
122 }
123 /* }}} */
124 
125 /* {{{ proto void CURLFile::setPostFilename(string $name)
126    Set file name for POST */
ZEND_METHOD(CURLFile,setPostFilename)127 ZEND_METHOD(CURLFile, setPostFilename)
128 {
129 	curlfile_set_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
130 }
131 /* }}} */
132 
133 /* {{{ proto CURLFile::__wakeup()
134    Unserialization handler */
ZEND_METHOD(CURLFile,__wakeup)135 ZEND_METHOD(CURLFile, __wakeup)
136 {
137 	zend_unset_property(curl_CURLFile_class, getThis(), "name", sizeof("name")-1);
138 	zend_update_property_string(curl_CURLFile_class, getThis(), "name", sizeof("name")-1, "");
139 	zend_throw_exception(NULL, "Unserialization of CURLFile instances is not allowed", 0);
140 }
141 /* }}} */
142 
143 ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1)
144 	ZEND_ARG_INFO(0, filename)
145 	ZEND_ARG_INFO(0, mimetype)
146 	ZEND_ARG_INFO(0, postname)
147 ZEND_END_ARG_INFO()
148 
149 ZEND_BEGIN_ARG_INFO(arginfo_curlfile_name, 0)
150 	ZEND_ARG_INFO(0, name)
151 ZEND_END_ARG_INFO()
152 
153 
154 static const zend_function_entry curlfile_funcs[] = {
155 	PHP_ME(CURLFile,			__construct,        arginfo_curlfile_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
156 	PHP_ME(CURLFile,			getFilename,        NULL, ZEND_ACC_PUBLIC)
157 	PHP_ME(CURLFile,			getMimeType,        NULL, ZEND_ACC_PUBLIC)
158 	PHP_ME(CURLFile,			setMimeType,        arginfo_curlfile_name, ZEND_ACC_PUBLIC)
159 	PHP_ME(CURLFile,			getPostFilename,    NULL, ZEND_ACC_PUBLIC)
160 	PHP_ME(CURLFile,			setPostFilename,    arginfo_curlfile_name, ZEND_ACC_PUBLIC)
161 	PHP_ME(CURLFile,            __wakeup,           NULL, ZEND_ACC_PUBLIC)
162 	PHP_FE_END
163 };
164 
curlfile_register_class(void)165 void curlfile_register_class(void)
166 {
167 	zend_class_entry ce;
168 	INIT_CLASS_ENTRY( ce, "CURLFile", curlfile_funcs );
169 	curl_CURLFile_class = zend_register_internal_class(&ce);
170 	zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
171 	zend_declare_property_string(curl_CURLFile_class, "mime", sizeof("mime")-1, "", ZEND_ACC_PUBLIC);
172 	zend_declare_property_string(curl_CURLFile_class, "postname", sizeof("postname")-1, "", ZEND_ACC_PUBLIC);
173 }
174 
175 #endif
176