1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 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 /* $Id$ */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "Zend/zend_exceptions.h"
27 #include "php_curl.h"
28 #if HAVE_CURL
29
30 PHP_CURL_API zend_class_entry *curl_CURLFile_class;
31
curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)32 static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)
33 {
34 char *fname = NULL, *mime = NULL, *postname = NULL;
35 int fname_len, mime_len, postname_len;
36 zval *cf = return_value;
37
38 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|ss", &fname, &fname_len, &mime, &mime_len, &postname, &postname_len) == FAILURE) {
39 return;
40 }
41
42 if (fname) {
43 zend_update_property_string(curl_CURLFile_class, cf, "name", sizeof("name")-1, fname TSRMLS_CC);
44 }
45
46 if (mime) {
47 zend_update_property_string(curl_CURLFile_class, cf, "mime", sizeof("mime")-1, mime TSRMLS_CC);
48 }
49
50 if (postname) {
51 zend_update_property_string(curl_CURLFile_class, cf, "postname", sizeof("postname")-1, postname TSRMLS_CC);
52 }
53 }
54
55 /* {{{ proto void CURLFile::__construct(string $name, [string $mimetype [, string $postfilename]])
56 Create the CURLFile object */
ZEND_METHOD(CURLFile,__construct)57 ZEND_METHOD(CURLFile, __construct)
58 {
59 return_value = getThis();
60 curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
61 }
62 /* }}} */
63
64 /* {{{ proto CURLFile curl_file_create(string $name, [string $mimetype [, string $postfilename]])
65 Create the CURLFile object */
PHP_FUNCTION(curl_file_create)66 PHP_FUNCTION(curl_file_create)
67 {
68 object_init_ex( return_value, curl_CURLFile_class );
69 curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
70 }
71 /* }}} */
72
curlfile_get_property(char * name,INTERNAL_FUNCTION_PARAMETERS)73 static void curlfile_get_property(char *name, INTERNAL_FUNCTION_PARAMETERS)
74 {
75 zval *res;
76 if (zend_parse_parameters_none() == FAILURE) {
77 return;
78 }
79 res = zend_read_property(curl_CURLFile_class, getThis(), name, strlen(name), 1 TSRMLS_CC);
80 *return_value = *res;
81 zval_copy_ctor(return_value);
82 INIT_PZVAL(return_value);
83 }
84
curlfile_set_property(char * name,INTERNAL_FUNCTION_PARAMETERS)85 static void curlfile_set_property(char *name, INTERNAL_FUNCTION_PARAMETERS)
86 {
87 char *arg = NULL;
88 int arg_len;
89
90 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
91 return;
92 }
93 zend_update_property_string(curl_CURLFile_class, getThis(), name, strlen(name), arg TSRMLS_CC);
94 }
95
96 /* {{{ proto string CURLFile::getFilename()
97 Get file name */
ZEND_METHOD(CURLFile,getFilename)98 ZEND_METHOD(CURLFile, getFilename)
99 {
100 curlfile_get_property("name", INTERNAL_FUNCTION_PARAM_PASSTHRU);
101 }
102 /* }}} */
103
104 /* {{{ proto string CURLFile::getMimeType()
105 Get MIME type */
ZEND_METHOD(CURLFile,getMimeType)106 ZEND_METHOD(CURLFile, getMimeType)
107 {
108 curlfile_get_property("mime", INTERNAL_FUNCTION_PARAM_PASSTHRU);
109 }
110 /* }}} */
111
112 /* {{{ proto string CURLFile::getPostFilename()
113 Get file name for POST */
ZEND_METHOD(CURLFile,getPostFilename)114 ZEND_METHOD(CURLFile, getPostFilename)
115 {
116 curlfile_get_property("postname", INTERNAL_FUNCTION_PARAM_PASSTHRU);
117 }
118 /* }}} */
119
120 /* {{{ proto void CURLFile::setMimeType(string $mime)
121 Set MIME type */
ZEND_METHOD(CURLFile,setMimeType)122 ZEND_METHOD(CURLFile, setMimeType)
123 {
124 curlfile_set_property("mime", INTERNAL_FUNCTION_PARAM_PASSTHRU);
125 }
126 /* }}} */
127
128 /* {{{ proto void CURLFile::setPostFilename(string $name)
129 Set file name for POST */
ZEND_METHOD(CURLFile,setPostFilename)130 ZEND_METHOD(CURLFile, setPostFilename)
131 {
132 curlfile_set_property("postname", INTERNAL_FUNCTION_PARAM_PASSTHRU);
133 }
134 /* }}} */
135
136 /* {{{ proto void CURLFile::__wakeup()
137 Unserialization handler */
ZEND_METHOD(CURLFile,__wakeup)138 ZEND_METHOD(CURLFile, __wakeup)
139 {
140 zval *_this = getThis();
141
142 zend_unset_property(curl_CURLFile_class, _this, "name", sizeof("name")-1 TSRMLS_CC);
143 zend_update_property_string(curl_CURLFile_class, _this, "name", sizeof("name")-1, "" TSRMLS_CC);
144 zend_throw_exception(NULL, "Unserialization of CURLFile instances is not allowed", 0 TSRMLS_CC);
145 }
146 /* }}} */
147
148 ZEND_BEGIN_ARG_INFO_EX(arginfo_curlfile_create, 0, 0, 1)
149 ZEND_ARG_INFO(0, filename)
150 ZEND_ARG_INFO(0, mimetype)
151 ZEND_ARG_INFO(0, postname)
152 ZEND_END_ARG_INFO()
153
154 ZEND_BEGIN_ARG_INFO(arginfo_curlfile_name, 0)
155 ZEND_ARG_INFO(0, name)
156 ZEND_END_ARG_INFO()
157
158
159 static const zend_function_entry curlfile_funcs[] = {
160 PHP_ME(CURLFile, __construct, arginfo_curlfile_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
161 PHP_ME(CURLFile, getFilename, NULL, ZEND_ACC_PUBLIC)
162 PHP_ME(CURLFile, getMimeType, NULL, ZEND_ACC_PUBLIC)
163 PHP_ME(CURLFile, setMimeType, arginfo_curlfile_name, ZEND_ACC_PUBLIC)
164 PHP_ME(CURLFile, getPostFilename, NULL, ZEND_ACC_PUBLIC)
165 PHP_ME(CURLFile, setPostFilename, arginfo_curlfile_name, ZEND_ACC_PUBLIC)
166 PHP_ME(CURLFile, __wakeup, NULL, ZEND_ACC_PUBLIC)
167 PHP_FE_END
168 };
169
curlfile_register_class(TSRMLS_D)170 void curlfile_register_class(TSRMLS_D)
171 {
172 zend_class_entry ce;
173 INIT_CLASS_ENTRY( ce, "CURLFile", curlfile_funcs );
174 curl_CURLFile_class = zend_register_internal_class(&ce TSRMLS_CC);
175 zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
176 zend_declare_property_string(curl_CURLFile_class, "mime", sizeof("mime")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
177 zend_declare_property_string(curl_CURLFile_class, "postname", sizeof("postname")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
178 }
179
180 #endif
181