/* ecore extension for PHP */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "ext/standard/info.h" #include "php_ecore.h" #include #include #include #include "json/php_json.h" #include #include "zend_smart_str.h" int auth_result = 0; /* For compatibility with older PHP versions */ #ifndef ZEND_PARSE_PARAMETERS_NONE #define ZEND_PARSE_PARAMETERS_NONE() \ ZEND_PARSE_PARAMETERS_START(0, 0) \ ZEND_PARSE_PARAMETERS_END() #endif ZEND_DECLARE_MODULE_GLOBALS(ecore) static PHP_GINIT_FUNCTION(ecore) { #if defined(COMPILE_DL_BCMATH) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif ecore_globals->scale = 1; } PHP_MINIT_FUNCTION(ecore) { #if defined(ZTS) && defined(COMPILE_DL_TEST) ZEND_TSRMLS_CACHE_UPDATE(); #endif REGISTER_LONG_CONSTANT("TEST_SCALE_FACTOR", 2, CONST_CS | CONST_PERSISTENT); return SUCCESS; } PHP_FUNCTION(ecore_pass_check) { char *password, *salt, *signed_passwrod; unsigned char signed_str[16]; size_t p_len = sizeof(password); size_t s_len = sizeof(salt); size_t sp_len = sizeof(signed_passwrod); char temp[8] = {0}; unsigned char decrypt[64] = {0}; ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_STRING(password, p_len) Z_PARAM_STRING(salt, s_len) ZEND_PARSE_PARAMETERS_END(); strcat(password, salt); int a_len = strlen(password); MD5(password, a_len, signed_str); for (int i = 0; i <= 15; i++) { sprintf(temp, "%02x", signed_str[i]); strcat(decrypt, temp); } if (signed_passwrod && strcmp(decrypt, signed_passwrod) == 0) { RETURN_TRUE; } RETURN_FALSE; } PHP_FUNCTION(ecore_pass_create) { char *password, *salt; unsigned char signed_str[16]; size_t p_len = sizeof(password); size_t s_len = sizeof(salt); char temp[8] = {0}; unsigned char decrypt[64] = {0}; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STRING(password, p_len) Z_PARAM_STRING(salt, s_len) ZEND_PARSE_PARAMETERS_END(); strcat(password, salt); int a_len = strlen(password); MD5(password, a_len, signed_str); for (int i = 0; i <= 15; i++) { sprintf(temp, "%02x", signed_str[i]); strcat(decrypt, temp); } RETURN_STRING(decrypt); } static long write_data(void *buffer, size_t size, size_t nmemb, void *stream) { FILE *fptr = (FILE *)stream; fwrite(buffer, size, nmemb, fptr); return size * nmemb; } static int post_parse(void *buffer, size_t size, size_t nmemb, void *stream) { char value[BUFSIZ] = {0}; memcpy(value, (char *)buffer, size * nmemb); // printf("--------%s %s\n", value, strstr(value, "success")); if (strstr(value, "true")) { auth_result = 1; } else { auth_result = 0; } return size * nmemb; } static int org_auth() { char data[BUFSIZ] = ""; char *file = "../runtime/auth"; FILE *fp = NULL; fp = fopen(file, "r"); if (fp != NULL) { char buff[BUFSIZ]; int i = 0; while (fscanf(fp, "%s", buff) != EOF) { // printf("buff: %s\n", buff); i++; if (i == 1) { strcat(data, "orgKey="); strcat(data, buff); } else if (i == 2) { strcat(data, "&orgHost="); strcat(data, buff); } } fclose(fp); // printf("data: %s\n", data); } char *url = "http://140.249.182.90:8081/api/v1/org/auth"; char *logfile = "king_curl_post.log"; CURL *curl = NULL; CURLcode res; struct curl_slist *http_header = NULL; curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl, CURLOPT_POST, 1); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // curl_easy_setopt(curl, CURLOPT_HEADER, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, post_parse); // curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); res = curl_easy_perform(curl); if (res != CURLE_OK) { char *buf = NULL; if (asprintf(&buf, "curl easy perform error res = %d", res) < 0) { return FAILURE; } if (buf != NULL) { free(buf); buf = NULL; } // RETVAL_STRING(buf); return FAILURE; } curl_easy_cleanup(curl); return SUCCESS; } PHP_FUNCTION(json_return) { char *status = "0"; char *info = ""; zval *data; size_t s_len = sizeof(status); ZEND_PARSE_PARAMETERS_START(0, 3) Z_PARAM_OPTIONAL Z_PARAM_STRING(status, s_len) Z_PARAM_STRING(info, s_len) // Z_PARAM_ARRAY(data) Z_PARAM_ARRAY_OR_OBJECT(data) ZEND_PARSE_PARAMETERS_END(); org_auth(); // printf("--auth_result------%d \n", auth_result); if (auth_result == 0) { php_printf("{\"status\":%d,\"info\":\"%s\",\"data\":[]}", 0, "非法访问"); zend_bailout(); return; } if (ZEND_NUM_ARGS() == 3) { smart_str buf = {0}; php_json_encode(&buf, data, 0); smart_str_0(&buf); if (buf.s) { php_printf("{\"status\":%d,\"info\":\"%s\",\"data\":%s}", atoi(status), info, buf.s->val); } else { php_printf("{\"status\":%d,\"info\":\"%s\",\"data\":%s}", atoi(status), info, zend_empty_string->val); } smart_str_free(&buf); } else { php_printf("{\"status\":%d,\"info\":\"%s\",\"data\":[]}", atoi(status), info); } zend_bailout(); return; } /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(ecore) { #if defined(ZTS) && defined(COMPILE_DL_ECORE) ZEND_TSRMLS_CACHE_UPDATE(); #endif return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(ecore) { php_info_print_table_start(); php_info_print_table_header(2, "ecore support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO(arginfo_ecore_pass_check, 0) ZEND_ARG_INFO(0, password) ZEND_ARG_INFO(0, salt) ZEND_ARG_INFO(0, signed_password) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_ecore_pass_create, 0) ZEND_ARG_INFO(0, password) ZEND_ARG_INFO(0, salt) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_json_return, 0) ZEND_ARG_INFO(0, status) ZEND_ARG_INFO(0, info) ZEND_ARG_INFO(0, data) ZEND_END_ARG_INFO() /* }}} */ /* {{{ ecore_functions[] */ static const zend_function_entry ecore_functions[] = { PHP_FE(ecore_pass_check, arginfo_ecore_pass_check) PHP_FE(ecore_pass_create, arginfo_ecore_pass_create) PHP_FE(json_return, arginfo_json_return) PHP_FE_END}; /* }}} */ /* {{{ ecore_module_entry */ zend_module_entry ecore_module_entry = { STANDARD_MODULE_HEADER, "ecore", /* Extension name */ ecore_functions, /* zend_function_entry */ PHP_MINIT(ecore), /* PHP_MINIT - Module initialization */ NULL, /* PHP_MSHUTDOWN - Module shutdown */ PHP_RINIT(ecore), /* PHP_RINIT - Request initialization */ NULL, /* PHP_RSHUTDOWN - Request shutdown */ PHP_MINFO(ecore), /* PHP_MINFO - Module info */ PHP_ECORE_VERSION, /* Version */ PHP_MODULE_GLOBALS(ecore), PHP_GINIT(ecore), NULL, NULL, STANDARD_MODULE_PROPERTIES_EX}; /* }}} */ #ifdef COMPILE_DL_ECORE #ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() #endif ZEND_GET_MODULE(ecore) #endif