curl_file.c 4.5 KB

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