xref: /PHP-7.4/ext/curl/curl_file.c (revision 0cf7de1c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 "Zend/zend_interfaces.h"
26 #include "php_curl.h"
27 #if HAVE_CURL
28 
29 PHP_CURL_API zend_class_entry *curl_CURLFile_class;
30 
curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)31 static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)
32 {
33 	zend_string *fname, *mime = NULL, *postname = NULL;
34 	zval *cf = return_value;
35 
36 	ZEND_PARSE_PARAMETERS_START(1,3)
37 		Z_PARAM_PATH_STR(fname)
38 		Z_PARAM_OPTIONAL
39 		Z_PARAM_STR(mime)
40 		Z_PARAM_STR(postname)
41 	ZEND_PARSE_PARAMETERS_END();
42 
43 	zend_update_property_string(curl_CURLFile_class, cf, "name", sizeof("name")-1, ZSTR_VAL(fname));
44 
45 	if (mime) {
46 		zend_update_property_string(curl_CURLFile_class, cf, "mime", sizeof("mime")-1, ZSTR_VAL(mime));
47 	}
48 
49 	if (postname) {
50 		zend_update_property_string(curl_CURLFile_class, cf, "postname", sizeof("postname")-1, ZSTR_VAL(postname));
51 	}
52 }
53 
54 /* {{{ proto CURLFile::__construct(string $name, [string $mimetype [, string $postfilename]])
55    Create the CURLFile object */
ZEND_METHOD(CURLFile,__construct)56 ZEND_METHOD(CURLFile, __construct)
57 {
58 	return_value = ZEND_THIS;
59 	curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
60 }
61 /* }}} */
62 
63 /* {{{ proto CURLFile curl_file_create(string $name, [string $mimetype [, string $postfilename]])
64    Create the CURLFile object */
PHP_FUNCTION(curl_file_create)65 PHP_FUNCTION(curl_file_create)
66 {
67     object_init_ex( return_value, curl_CURLFile_class );
68     curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
69 }
70 /* }}} */
71 
curlfile_get_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)72 static void curlfile_get_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
73 {
74 	zval *res, rv;
75 
76 	if (zend_parse_parameters_none() == FAILURE) {
77 		return;
78 	}
79 	res = zend_read_property(curl_CURLFile_class, ZEND_THIS, name, name_len, 1, &rv);
80 	ZVAL_COPY_DEREF(return_value, res);
81 }
82 
curlfile_set_property(char * name,size_t name_len,INTERNAL_FUNCTION_PARAMETERS)83 static void curlfile_set_property(char *name, size_t name_len, INTERNAL_FUNCTION_PARAMETERS)
84 {
85 	zend_string *arg;
86 
87 	ZEND_PARSE_PARAMETERS_START(1,1)
88 		Z_PARAM_STR(arg)
89 	ZEND_PARSE_PARAMETERS_END();
90 
91 	zend_update_property_string(curl_CURLFile_class, ZEND_THIS, name, name_len, ZSTR_VAL(arg));
92 }
93 
94 /* {{{ proto string CURLFile::getFilename()
95    Get file name */
ZEND_METHOD(CURLFile,getFilename)96 ZEND_METHOD(CURLFile, getFilename)
97 {
98 	curlfile_get_property("name", sizeof("name")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
99 }
100 /* }}} */
101 
102 /* {{{ proto string CURLFile::getMimeType()
103    Get MIME type */
ZEND_METHOD(CURLFile,getMimeType)104 ZEND_METHOD(CURLFile, getMimeType)
105 {
106 	curlfile_get_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
107 }
108 /* }}} */
109 
110 /* {{{ proto string CURLFile::getPostFilename()
111    Get file name for POST */
ZEND_METHOD(CURLFile,getPostFilename)112 ZEND_METHOD(CURLFile, getPostFilename)
113 {
114 	curlfile_get_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
115 }
116 /* }}} */
117 
118 /* {{{ proto void CURLFile::setMimeType(string $mime)
119    Set MIME type */
ZEND_METHOD(CURLFile,setMimeType)120 ZEND_METHOD(CURLFile, setMimeType)
121 {
122 	curlfile_set_property("mime", sizeof("mime")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
123 }
124 /* }}} */
125 
126 /* {{{ proto void CURLFile::setPostFilename(string $name)
127    Set file name for POST */
ZEND_METHOD(CURLFile,setPostFilename)128 ZEND_METHOD(CURLFile, setPostFilename)
129 {
130 	curlfile_set_property("postname", sizeof("postname")-1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
131 }
132 /* }}} */
133 
134 ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1)
135 	ZEND_ARG_INFO(0, filename)
136 	ZEND_ARG_INFO(0, mimetype)
137 	ZEND_ARG_INFO(0, postname)
138 ZEND_END_ARG_INFO()
139 
140 ZEND_BEGIN_ARG_INFO(arginfo_curlfile_name, 0)
141 	ZEND_ARG_INFO(0, name)
142 ZEND_END_ARG_INFO()
143 
144 
145 static const zend_function_entry curlfile_funcs[] = {
146 	PHP_ME(CURLFile,			__construct,        arginfo_curlfile_create, ZEND_ACC_PUBLIC)
147 	PHP_ME(CURLFile,			getFilename,        NULL, ZEND_ACC_PUBLIC)
148 	PHP_ME(CURLFile,			getMimeType,        NULL, ZEND_ACC_PUBLIC)
149 	PHP_ME(CURLFile,			setMimeType,        arginfo_curlfile_name, ZEND_ACC_PUBLIC)
150 	PHP_ME(CURLFile,			getPostFilename,    NULL, ZEND_ACC_PUBLIC)
151 	PHP_ME(CURLFile,			setPostFilename,    arginfo_curlfile_name, ZEND_ACC_PUBLIC)
152 	PHP_FE_END
153 };
154 
curlfile_register_class(void)155 void curlfile_register_class(void)
156 {
157 	zend_class_entry ce;
158 	INIT_CLASS_ENTRY( ce, "CURLFile", curlfile_funcs );
159 	curl_CURLFile_class = zend_register_internal_class(&ce);
160 	curl_CURLFile_class->serialize = zend_class_serialize_deny;
161 	curl_CURLFile_class->unserialize = zend_class_unserialize_deny;
162 	zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);
163 	zend_declare_property_string(curl_CURLFile_class, "mime", sizeof("mime")-1, "", ZEND_ACC_PUBLIC);
164 	zend_declare_property_string(curl_CURLFile_class, "postname", sizeof("postname")-1, "", ZEND_ACC_PUBLIC);
165 }
166 
167 #endif
168