wxwork_finance_sdk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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: |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "php_wxwork_finance_sdk.h"
  26. /* If you declare any globals in php_wxwork_finance_sdk.h uncomment this:
  27. ZEND_DECLARE_MODULE_GLOBALS(wxwork_finance_sdk)
  28. */
  29. /* True global resources - no need for thread safety here */
  30. static int le_wxwork_finance_sdk;
  31. const static char *VERSION = "1.0";
  32. static zend_class_entry *wxwork_finance_sdk_ce;
  33. static zend_class_entry *wxwork_finance_sdk_exception_ce;
  34. static WeWorkFinanceSdk_t *sdk;
  35. /**
  36. * options = [
  37. 'proxy_host' => 'http://www.baidu.com',
  38. 'proxy_password' => 'helloworld'
  39. ]
  40. */
  41. PHP_METHOD(WxworkFinanceSdk, __construct)
  42. {
  43. char *corp_id, *secret;
  44. size_t corp_id_len, secret_len;
  45. zval *option_zval = NULL;
  46. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a", &corp_id, &corp_id_len, &secret, &secret_len, &option_zval) == FAILURE) {
  47. zend_error(E_ERROR, "param error");
  48. return;
  49. }
  50. if (corp_id_len == 0 || secret_len == 0) {
  51. zend_throw_exception(spl_ce_InvalidArgumentException, "corpId and secret cannot be bull", 0);
  52. return;
  53. }
  54. int ret = Init(sdk, corp_id, secret);
  55. if (ret != 0) {
  56. zend_throw_exception(wxwork_finance_sdk_exception_ce, "Call WeWorkFinanceSdk_t Init error", ret);
  57. return;
  58. }
  59. zval *this = getThis();
  60. zend_class_entry *ce = Z_OBJCE_P(this);
  61. zend_update_property_string(ce, this, "_corpId", sizeof("_corpId") - 1, corp_id);
  62. zend_update_property_string(ce, this, "_secret", sizeof("_secret") - 1, secret);
  63. if (option_zval) {
  64. zval *proxy_host_zval = zend_hash_find(Z_ARR_P(option_zval), zend_string_init("proxy_host", sizeof("proxy_host") - 1, 0));
  65. if (proxy_host_zval != NULL) {
  66. zval *proxy_password_zval = zend_hash_find(Z_ARR_P(option_zval), zend_string_init("proxy_password", sizeof("proxy_password") - 1, 0));
  67. zend_update_property_string(ce, this, "proxy_host", sizeof("proxy_host") - 1, Z_STRVAL_P(proxy_host_zval));
  68. if (proxy_password_zval != NULL) {
  69. zend_update_property_string(ce, this, "proxy_password", sizeof("proxy_password") - 1, Z_STRVAL_P(proxy_password_zval));
  70. zval_ptr_dtor(proxy_password_zval);
  71. }
  72. zval_ptr_dtor(proxy_host_zval);
  73. }
  74. zval *timeout_zval = zend_hash_find(Z_ARR_P(option_zval), zend_string_init("timeout", sizeof("timeout") - 1, 0));
  75. if (timeout_zval != NULL) {
  76. zend_update_property_long(ce, this, "_timeout", sizeof("_timeout") - 1, zval_get_long(timeout_zval));
  77. zval_ptr_dtor(timeout_zval);
  78. }
  79. }
  80. }
  81. /**
  82. {{{ proto public WxworkFinanceSdk::getChatData(int $seq, int $limit)
  83. */
  84. PHP_METHOD(WxworkFinanceSdk, getChatData)
  85. {
  86. zend_long seq, limit = 0;
  87. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &seq, &limit) == FAILURE) {
  88. return;
  89. }
  90. Slice_t *chat_data = NewSlice();
  91. if (NULL == chat_data) {
  92. zend_error(E_ERROR, "There is not enough memory!");
  93. return;
  94. }
  95. zval *this = getThis();
  96. zend_class_entry *ce = Z_OBJCE_P(this);
  97. zval *proxy_host_zval = zend_read_property(ce, this, "_proxy_host", sizeof("_proxy_host") - 1, 0, NULL);
  98. zval *proxy_password_zval = zend_read_property(ce, this, "_proxy_password", sizeof("_proxy_password") - 1, 0, NULL);
  99. zval *timeout_zval = zend_read_property(ce, this, "_timeout", sizeof("_timeout") - 1, 0, NULL);
  100. int ret = GetChatData(sdk, (int)seq, (int)limit, Z_STRVAL_P(proxy_host_zval), Z_STRVAL_P(proxy_password_zval), zval_get_long(timeout_zval), chat_data);
  101. if (0 != ret) {
  102. zend_throw_exception(wxwork_finance_sdk_exception_ce, "Call WeWorkFinanceSdk_t GetChatData error", ret);
  103. return;
  104. }
  105. zend_string *s = zend_string_init(GetContentFromSlice(chat_data), GetSliceLen(chat_data), 0);
  106. RETURN_STR(s);
  107. zend_string_release(s);
  108. zval_ptr_dtor(proxy_host_zval);
  109. zval_ptr_dtor(proxy_password_zval);
  110. zval_ptr_dtor(timeout_zval);
  111. FreeSlice(chat_data);
  112. }
  113. /* }}} */
  114. /**
  115. {{{ proto WxworkFinanceSdk->decryptData(string $encryptRandomKey, string $encryptData)
  116. */
  117. PHP_METHOD(WxworkFinanceSdk, decryptData)
  118. {
  119. zend_string *encrypt_random_key, *encrypt_data;
  120. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &encrypt_random_key, &encrypt_data) == FAILURE) {
  121. return;
  122. }
  123. Slice_t *msg = NewSlice();
  124. int ret = DecryptData(ZSTR_VAL(encrypt_random_key), ZSTR_VAL(encrypt_data), msg);
  125. if (ret != 0) {
  126. zend_throw_exception(spl_ce_InvalidArgumentException, "DecryptData data error", ret);
  127. return;
  128. }
  129. zend_string *return_msg = zend_string_init(GetContentFromSlice(msg), GetSliceLen(msg), 0);
  130. RETURN_STR(return_msg);
  131. FreeSlice(msg);
  132. zend_string_release(return_msg);
  133. }
  134. /**
  135. {{{ proto WxworkFinanceSdk->getMediaData(string $filedId, string $index='')
  136. */
  137. PHP_METHOD(WxworkFinanceSdk, downloadMedia)
  138. {
  139. zend_string *sdk_filedid, *file_saveto;
  140. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &sdk_filedid, &file_saveto) == FAILURE) {
  141. return;
  142. }
  143. zval *this = getThis();
  144. zend_class_entry *ce = Z_OBJCE_P(this);
  145. zval *proxy_host_zval = zend_read_property(ce, this, "_proxy_host", sizeof("_proxy_host") - 1, 0, NULL);
  146. zval *proxy_password_zval = zend_read_property(ce, this, "_proxy_password", sizeof("_proxy_password") - 1, 0, NULL);
  147. zval *timeout_zval = zend_read_property(ce, this, "_timeout", sizeof("_timeout") - 1, 0, NULL);
  148. FILE *fp = fopen(ZSTR_VAL(file_saveto), "wb");
  149. if (NULL == fp) {
  150. zend_throw_exception(wxwork_finance_sdk_exception_ce, "Open", 0);
  151. return;
  152. }
  153. MediaData_t *media_data = NewMediaData();
  154. if (NULL == media_data) {
  155. zend_error(E_ERROR, "There is not enough memory!");
  156. return;
  157. }
  158. do {
  159. int ret = GetMediaData(sdk, GetOutIndexBuf(media_data), ZSTR_VAL(sdk_filedid), Z_STRVAL_P(proxy_host_zval), Z_STRVAL_P(proxy_password_zval), zval_get_long(timeout_zval), media_data);
  160. if (0 != ret) {
  161. FreeMediaData(media_data);
  162. zend_throw_exception(wxwork_finance_sdk_exception_ce, "GetMediaData error", ret);
  163. return;
  164. }
  165. fwrite(GetData(media_data), GetDataLen(media_data), 1, fp);
  166. }while(IsMediaDataFinish(media_data) != 1);
  167. fclose(fp);
  168. FreeMediaData(media_data);
  169. RETURN_TRUE;
  170. }
  171. /* }}} */
  172. static const zend_function_entry wxwork_finance_sdk_class_methods[] = {
  173. PHP_ME(WxworkFinanceSdk, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  174. PHP_ME(WxworkFinanceSdk, getChatData, NULL, ZEND_ACC_PUBLIC)
  175. PHP_ME(WxworkFinanceSdk, decryptData, NULL, ZEND_ACC_PUBLIC)
  176. PHP_ME(WxworkFinanceSdk, downloadMedia, NULL, ZEND_ACC_PUBLIC)
  177. PHP_FE_END
  178. };
  179. static const zend_function_entry wxwork_finance_sdk_exception_methods[] = {
  180. PHP_FE_END
  181. };
  182. /* {{{ PHP_INI
  183. */
  184. /* Remove comments and fill if you need to have entries in php.ini
  185. PHP_INI_BEGIN()
  186. STD_PHP_INI_ENTRY("wxwork_finance_sdk.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_wxwork_finance_sdk_globals, wxwork_finance_sdk_globals)
  187. STD_PHP_INI_ENTRY("wxwork_finance_sdk.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_wxwork_finance_sdk_globals, wxwork_finance_sdk_globals)
  188. PHP_INI_END()
  189. */
  190. /* }}} */
  191. /* Remove the following function when you have successfully modified config.m4
  192. so that your module can be compiled into PHP, it exists only for testing
  193. purposes. */
  194. /* Every user-visible function in PHP should document itself in the source */
  195. /* {{{ proto string confirm_wxwork_finance_sdk_compiled(string arg)
  196. Return a string to confirm that the module is compiled in */
  197. PHP_FUNCTION(confirm_wxwork_finance_sdk_compiled)
  198. {
  199. char *arg = NULL;
  200. size_t arg_len, len;
  201. zend_string *strg;
  202. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
  203. return;
  204. }
  205. strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "wxwork_finance_sdk", arg);
  206. RETURN_STR(strg);
  207. }
  208. /* }}} */
  209. /* The previous line is meant for vim and emacs, so it can correctly fold and
  210. unfold functions in source code. See the corresponding marks just before
  211. function definition, where the functions purpose is also documented. Please
  212. follow this convention for the convenience of others editing your code.
  213. */
  214. /* {{{ php_wxwork_finance_sdk_init_globals
  215. */
  216. /* Uncomment this function if you have INI entries
  217. static void php_wxwork_finance_sdk_init_globals(zend_wxwork_finance_sdk_globals *wxwork_finance_sdk_globals)
  218. {
  219. wxwork_finance_sdk_globals->global_value = 0;
  220. wxwork_finance_sdk_globals->global_string = NULL;
  221. }
  222. */
  223. /* }}} */
  224. /* {{{ PHP_MINIT_FUNCTION
  225. */
  226. PHP_MINIT_FUNCTION(wxwork_finance_sdk)
  227. {
  228. /* If you have INI entries, uncomment these lines
  229. REGISTER_INI_ENTRIES();
  230. */
  231. // define WxworkFinanceSdkException
  232. zend_class_entry wxwork_finance_sdk_exception_def;
  233. INIT_CLASS_ENTRY(wxwork_finance_sdk_exception_def, "WxworkFinanceSdkException", wxwork_finance_sdk_exception_methods);
  234. wxwork_finance_sdk_exception_ce = zend_register_internal_class_ex(&wxwork_finance_sdk_exception_def, zend_ce_exception);
  235. zend_class_entry wxwork_finance_sdk_def;
  236. INIT_CLASS_ENTRY(wxwork_finance_sdk_def, "WxworkFinanceSdk", wxwork_finance_sdk_class_methods);
  237. wxwork_finance_sdk_ce = zend_register_internal_class(&wxwork_finance_sdk_def);
  238. zend_declare_property_string(wxwork_finance_sdk_ce, "_corpId", sizeof("_corpId") - 1, "", ZEND_ACC_PRIVATE);
  239. zend_declare_property_string(wxwork_finance_sdk_ce, "_secret", sizeof("_secret") - 1, "", ZEND_ACC_PRIVATE);
  240. // http proxy
  241. zend_declare_property_string(wxwork_finance_sdk_ce, "_proxy_host", sizeof("_proxy_host") - 1, "", ZEND_ACC_PRIVATE);
  242. zend_declare_property_string(wxwork_finance_sdk_ce, "_proxy_password", sizeof("_proxy_password") - 1, "", ZEND_ACC_PRIVATE);
  243. // request timeout
  244. zend_declare_property_long(wxwork_finance_sdk_ce, "_timeout", sizeof("_timeout") - 1, 10, ZEND_ACC_PRIVATE);
  245. zend_declare_class_constant_string(wxwork_finance_sdk_ce, "VERSION", sizeof("VERSION") - 1, VERSION);
  246. return SUCCESS;
  247. }
  248. /* }}} */
  249. /* {{{ PHP_MSHUTDOWN_FUNCTION
  250. */
  251. PHP_MSHUTDOWN_FUNCTION(wxwork_finance_sdk)
  252. {
  253. /* uncomment this line if you have INI entries
  254. UNREGISTER_INI_ENTRIES();
  255. */
  256. return SUCCESS;
  257. }
  258. /* }}} */
  259. /* Remove if there's nothing to do at request start */
  260. /* {{{ PHP_RINIT_FUNCTION
  261. */
  262. PHP_RINIT_FUNCTION(wxwork_finance_sdk)
  263. {
  264. #if defined(COMPILE_DL_WXWORK_FINANCE_SDK) && defined(ZTS)
  265. ZEND_TSRMLS_CACHE_UPDATE();
  266. #endif
  267. sdk = NewSdk();
  268. return SUCCESS;
  269. }
  270. /* }}} */
  271. /* Remove if there's nothing to do at request end */
  272. /* {{{ PHP_RSHUTDOWN_FUNCTION
  273. */
  274. PHP_RSHUTDOWN_FUNCTION(wxwork_finance_sdk)
  275. {
  276. DestroySdk(sdk);
  277. return SUCCESS;
  278. }
  279. /* }}} */
  280. /* {{{ PHP_MINFO_FUNCTION
  281. */
  282. PHP_MINFO_FUNCTION(wxwork_finance_sdk)
  283. {
  284. php_info_print_table_start();
  285. php_info_print_table_header(2, "wxwork_finance_sdk support", "enabled");
  286. php_info_print_table_end();
  287. /* Remove comments if you have entries in php.ini
  288. DISPLAY_INI_ENTRIES();
  289. */
  290. }
  291. /* }}} */
  292. /* {{{ wxwork_finance_sdk_functions[]
  293. *
  294. * Every user visible function must have an entry in wxwork_finance_sdk_functions[].
  295. */
  296. const zend_function_entry wxwork_finance_sdk_functions[] = {
  297. PHP_FE(confirm_wxwork_finance_sdk_compiled, NULL) /* For testing, remove later. */
  298. PHP_FE_END /* Must be the last line in wxwork_finance_sdk_functions[] */
  299. };
  300. /* }}} */
  301. /* {{{ wxwork_finance_sdk_module_entry
  302. */
  303. zend_module_entry wxwork_finance_sdk_module_entry = {
  304. STANDARD_MODULE_HEADER,
  305. "wxwork_finance_sdk",
  306. wxwork_finance_sdk_functions,
  307. PHP_MINIT(wxwork_finance_sdk),
  308. PHP_MSHUTDOWN(wxwork_finance_sdk),
  309. PHP_RINIT(wxwork_finance_sdk), /* Replace with NULL if there's nothing to do at request start */
  310. PHP_RSHUTDOWN(wxwork_finance_sdk), /* Replace with NULL if there's nothing to do at request end */
  311. PHP_MINFO(wxwork_finance_sdk),
  312. PHP_WXWORK_FINANCE_SDK_VERSION,
  313. STANDARD_MODULE_PROPERTIES
  314. };
  315. /* }}} */
  316. #ifdef COMPILE_DL_WXWORK_FINANCE_SDK
  317. #ifdef ZTS
  318. ZEND_TSRMLS_CACHE_DEFINE()
  319. #endif
  320. ZEND_GET_MODULE(wxwork_finance_sdk)
  321. #endif
  322. /*
  323. * Local variables:
  324. * tab-width: 4
  325. * c-basic-offset: 4
  326. * End:
  327. * vim600: noet sw=4 ts=4 fdm=marker
  328. * vim<600: noet sw=4 ts=4
  329. */