PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */ { size_t algo_len = strlen(algo); char *lower = zend_str_tolower_dup(algo, algo_len); zend_hash_str_add_ptr(&php_hash_hashtable, lower, algo_len, (void *) ops); efree(lower); }
/** * Finds the correct scope to execute the function */ int phalcon_find_scope(zend_class_entry *ce, char *method_name, int method_len, int lower, ulong hash TSRMLS_DC){ char *lcname; if (lower) { lcname = method_name; } else { lcname = zend_str_tolower_dup(method_name, method_len); } if (!hash) { hash = zend_inline_hash_func(lcname, method_len + 1); } while (ce) { if (phalcon_hash_quick_exists(&ce->function_table, lcname, method_len + 1, hash)) { EG(scope) = ce; if (!lower) { efree(lcname); } return SUCCESS; } ce = ce->parent; } if (!lower) { efree(lcname); } return FAILURE; }
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */ { char *lower = zend_str_tolower_dup(algo, algo_len); php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len); efree(lower); return ops; }
static void convert_browscap_pattern(zval *pattern, int persistent) /* {{{ */ { int i, j=0; char *t; zend_string *res; char *lc_pattern; res = zend_string_safe_alloc(Z_STRLEN_P(pattern), 2, 4, persistent); t = res->val; lc_pattern = zend_str_tolower_dup(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern)); t[j++] = '\xA7'; /* section sign */ t[j++] = '^'; for (i=0; i<Z_STRLEN_P(pattern); i++, j++) { switch (lc_pattern[i]) { case '?': t[j] = '.'; break; case '*': t[j++] = '.'; t[j] = '*'; break; case '.': t[j++] = '\\'; t[j] = '.'; break; case '\\': t[j++] = '\\'; t[j] = '\\'; break; case '(': t[j++] = '\\'; t[j] = '('; break; case ')': t[j++] = '\\'; t[j] = ')'; break; case '\xA7': t[j++] = '\\'; t[j] = '\xA7'; break; default: t[j] = lc_pattern[i]; break; } } t[j++] = '$'; t[j++] = '\xA7'; t[j]=0; res->len = j; Z_STR_P(pattern) = res; efree(lc_pattern); }
int zephir_method_exists(const zval *object, const zval *method_name) { if (Z_TYPE_P(method_name) != IS_STRING) { zend_error(E_WARNING, "method_exists expected a string"); return 0; } char *lcname = zend_str_tolower_dup(Z_STRVAL_P(method_name), Z_STRLEN_P(method_name)); int res = zephir_method_exists_ex(object, lcname, Z_STRLEN_P(method_name)); efree(lcname); return res; }
// time critical bool PHPQt::methodExists(const zend_class_entry* ce_ptr, const char* methodname) { if(ce_ptr == NULL) pError() << "no class entry, could not check for message " << methodname; char* lcname = zend_str_tolower_dup(methodname, strlen(methodname)); if(zend_hash_exists(const_cast<HashTable*>(&ce_ptr->function_table), lcname, strlen(methodname)+1)){ return true; } efree(lcname); return false; }
/** * Finds the correct scope to execute the function */ int phalcon_find_scope(zend_class_entry *ce, char *method_name, int method_len TSRMLS_DC){ char *lcname = zend_str_tolower_dup(method_name, method_len); while (ce) { if (zend_hash_exists(&ce->function_table, lcname, method_len+1)) { EG(scope) = ce; efree(lcname); return SUCCESS; } ce = ce->parent; } if (lcname) { efree(lcname); } return FAILURE; }
/** * Find out the function scope on parent classes */ int phalcon_find_parent_scope(zend_class_entry *ce, char *active_class, int active_class_len, char *method_name, int method_len TSRMLS_DC){ char *lcname = zend_str_tolower_dup(method_name, method_len); while (ce) { if (ce->name_length == active_class_len) { if (!zend_binary_strcasecmp(ce->name, ce->name_length, active_class, active_class_len)) { if (zend_hash_exists(&ce->function_table, lcname, method_len+1)) { EG(scope) = ce; efree(lcname); return SUCCESS; } } } ce = ce->parent; } if (lcname) { efree(lcname); } return FAILURE; }
static int do_cli(int argc, char **argv) /* {{{ */ { int c; zend_file_handle file_handle; int behavior = PHP_MODE_STANDARD; char *reflection_what = NULL; volatile int request_started = 0; volatile int exit_status = 0; char *php_optarg = NULL, *orig_optarg = NULL; int php_optind = 1, orig_optind = 1; char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL; char *arg_free=NULL, **arg_excp=&arg_free; char *script_file=NULL, *translated_path = NULL; int interactive=0; int lineno = 0; const char *param_error=NULL; int hide_argv = 0; zend_try { CG(in_compilation) = 0; /* not initialized but needed for several options */ while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) { switch (c) { case 'i': /* php info & quit */ if (php_request_startup()==FAILURE) { goto err; } request_started = 1; php_print_info(0xFFFFFFFF); php_output_end_all(); exit_status = (c == '?' && argc > 1 && !strchr(argv[1], c)); goto out; case 'v': /* show php version & quit */ php_printf("PHP %s (%s) (built: %s %s) ( %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__, #if ZTS "ZTS " #else "NTS " #endif #if ZEND_DEBUG "DEBUG " #endif #ifdef HAVE_GCOV "GCOV " #endif , get_zend_version() ); sapi_deactivate(); goto out; case 'm': /* list compiled in modules */ if (php_request_startup()==FAILURE) { goto err; } request_started = 1; php_printf("[PHP Modules]\n"); print_modules(); php_printf("\n[Zend Modules]\n"); print_extensions(); php_printf("\n"); php_output_end_all(); exit_status=0; goto out; default: break; } } /* Set some CLI defaults */ SG(options) |= SAPI_OPTION_NO_CHDIR; php_optind = orig_optind; php_optarg = orig_optarg; while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) { switch (c) { case 'a': /* interactive mode */ if (!interactive) { if (behavior != PHP_MODE_STANDARD) { param_error = param_mode_conflict; break; } interactive=1; } break; case 'C': /* don't chdir to the script directory */ /* This is default so NOP */ break; case 'F': if (behavior == PHP_MODE_PROCESS_STDIN) { if (exec_run || script_file) { param_error = "You can use -R or -F only once.\n"; break; } } else if (behavior != PHP_MODE_STANDARD) { param_error = param_mode_conflict; break; } behavior=PHP_MODE_PROCESS_STDIN; script_file = php_optarg; break; case 'f': /* parse file */ if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { param_error = param_mode_conflict; break; } else if (script_file) { param_error = "You can use -f only once.\n"; break; } script_file = php_optarg; break; case 'l': /* syntax check mode */ if (behavior != PHP_MODE_STANDARD) { break; } behavior=PHP_MODE_LINT; break; case 'q': /* do not generate HTTP headers */ /* This is default so NOP */ break; case 'r': /* run code from command line */ if (behavior == PHP_MODE_CLI_DIRECT) { if (exec_direct || script_file) { param_error = "You can use -r only once.\n"; break; } } else if (behavior != PHP_MODE_STANDARD || interactive) { param_error = param_mode_conflict; break; } behavior=PHP_MODE_CLI_DIRECT; exec_direct=php_optarg; break; case 'R': if (behavior == PHP_MODE_PROCESS_STDIN) { if (exec_run || script_file) { param_error = "You can use -R or -F only once.\n"; break; } } else if (behavior != PHP_MODE_STANDARD) { param_error = param_mode_conflict; break; } behavior=PHP_MODE_PROCESS_STDIN; exec_run=php_optarg; break; case 'B': if (behavior == PHP_MODE_PROCESS_STDIN) { if (exec_begin) { param_error = "You can use -B only once.\n"; break; } } else if (behavior != PHP_MODE_STANDARD || interactive) { param_error = param_mode_conflict; break; } behavior=PHP_MODE_PROCESS_STDIN; exec_begin=php_optarg; break; case 'E': if (behavior == PHP_MODE_PROCESS_STDIN) { if (exec_end) { param_error = "You can use -E only once.\n"; break; } } else if (behavior != PHP_MODE_STANDARD || interactive) { param_error = param_mode_conflict; break; } behavior=PHP_MODE_PROCESS_STDIN; exec_end=php_optarg; break; case 's': /* generate highlighted HTML from source */ if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { param_error = "Source highlighting only works for files.\n"; break; } behavior=PHP_MODE_HIGHLIGHT; break; case 'w': if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) { param_error = "Source stripping only works for files.\n"; break; } behavior=PHP_MODE_STRIP; break; case 'z': /* load extension file */ zend_load_extension(php_optarg); break; case 'H': hide_argv = 1; break; case 10: behavior=PHP_MODE_REFLECTION_FUNCTION; reflection_what = php_optarg; break; case 11: behavior=PHP_MODE_REFLECTION_CLASS; reflection_what = php_optarg; break; case 12: behavior=PHP_MODE_REFLECTION_EXTENSION; reflection_what = php_optarg; break; case 13: behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION; reflection_what = php_optarg; break; case 14: behavior=PHP_MODE_REFLECTION_EXT_INFO; reflection_what = php_optarg; break; case 15: behavior = PHP_MODE_SHOW_INI_CONFIG; break; default: break; } } if (param_error) { PUTS(param_error); exit_status=1; goto err; } if (interactive) { #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE) printf("Interactive shell\n\n"); #else printf("Interactive mode enabled\n\n"); #endif fflush(stdout); } /* only set script_file if not set already and not in direct mode and not at end of parameter list */ if (argc > php_optind && !script_file && behavior!=PHP_MODE_CLI_DIRECT && behavior!=PHP_MODE_PROCESS_STDIN && strcmp(argv[php_optind-1],"--")) { script_file=argv[php_optind]; php_optind++; } if (script_file) { if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) { goto err; } else { char real_path[MAXPATHLEN]; if (VCWD_REALPATH(script_file, real_path)) { translated_path = strdup(real_path); } script_filename = script_file; } } else { /* We could handle PHP_MODE_PROCESS_STDIN in a different manner */ /* here but this would make things only more complicated. And it */ /* is consitent with the way -R works where the stdin file handle*/ /* is also accessible. */ file_handle.filename = "-"; file_handle.handle.fp = stdin; } file_handle.type = ZEND_HANDLE_FP; file_handle.opened_path = NULL; file_handle.free_filename = 0; php_self = (char*)file_handle.filename; /* before registering argv to module exchange the *new* argv[0] */ /* we can achieve this without allocating more memory */ SG(request_info).argc=argc-php_optind+1; arg_excp = argv+php_optind-1; arg_free = argv[php_optind-1]; SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename; argv[php_optind-1] = (char*)file_handle.filename; SG(request_info).argv=argv+php_optind-1; if (php_request_startup()==FAILURE) { *arg_excp = arg_free; fclose(file_handle.handle.fp); PUTS("Could not startup.\n"); goto err; } request_started = 1; CG(start_lineno) = lineno; *arg_excp = arg_free; /* reconstuct argv */ if (hide_argv) { int i; for (i = 1; i < argc; i++) { memset(argv[i], 0, strlen(argv[i])); } } zend_is_auto_global_str(ZEND_STRL("_SERVER")); PG(during_request_startup) = 0; switch (behavior) { case PHP_MODE_STANDARD: if (strcmp(file_handle.filename, "-")) { cli_register_file_handles(); } if (interactive && cli_shell_callbacks.cli_shell_run) { exit_status = cli_shell_callbacks.cli_shell_run(); } else { php_execute_script(&file_handle); exit_status = EG(exit_status); } break; case PHP_MODE_LINT: exit_status = php_lint_script(&file_handle); if (exit_status==SUCCESS) { zend_printf("No syntax errors detected in %s\n", file_handle.filename); } else { zend_printf("Errors parsing %s\n", file_handle.filename); } break; case PHP_MODE_STRIP: if (open_file_for_scanning(&file_handle)==SUCCESS) { zend_strip(); } goto out; break; case PHP_MODE_HIGHLIGHT: { zend_syntax_highlighter_ini syntax_highlighter_ini; if (open_file_for_scanning(&file_handle)==SUCCESS) { php_get_highlight_struct(&syntax_highlighter_ini); zend_highlight(&syntax_highlighter_ini); } goto out; } break; case PHP_MODE_CLI_DIRECT: cli_register_file_handles(); if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1) == FAILURE) { exit_status=254; } break; case PHP_MODE_PROCESS_STDIN: { char *input; size_t len, index = 0; zval argn, argi; cli_register_file_handles(); if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) { exit_status=254; } while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) { len = strlen(input); while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) { input[len] = '\0'; } ZVAL_STRINGL(&argn, input, len + 1); zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn); ZVAL_LONG(&argi, ++index); zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi); if (exec_run) { if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) { exit_status=254; } } else { if (script_file) { if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) { exit_status = 1; } else { CG(start_lineno) = lineno; php_execute_script(&file_handle); exit_status = EG(exit_status); } } } efree(input); } if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1) == FAILURE) { exit_status=254; } break; } case PHP_MODE_REFLECTION_FUNCTION: case PHP_MODE_REFLECTION_CLASS: case PHP_MODE_REFLECTION_EXTENSION: case PHP_MODE_REFLECTION_ZEND_EXTENSION: { zend_class_entry *pce = NULL; zval arg, ref; zend_execute_data execute_data; switch (behavior) { default: break; case PHP_MODE_REFLECTION_FUNCTION: if (strstr(reflection_what, "::")) { pce = reflection_method_ptr; } else { pce = reflection_function_ptr; } break; case PHP_MODE_REFLECTION_CLASS: pce = reflection_class_ptr; break; case PHP_MODE_REFLECTION_EXTENSION: pce = reflection_extension_ptr; break; case PHP_MODE_REFLECTION_ZEND_EXTENSION: pce = reflection_zend_extension_ptr; break; } ZVAL_STRING(&arg, reflection_what); object_init_ex(&ref, pce); memset(&execute_data, 0, sizeof(zend_execute_data)); EG(current_execute_data) = &execute_data; zend_call_method_with_1_params(&ref, pce, &pce->constructor, "__construct", NULL, &arg); if (EG(exception)) { zval tmp, *msg, rv; ZVAL_OBJ(&tmp, EG(exception)); msg = zend_read_property(zend_ce_exception, &tmp, "message", sizeof("message")-1, 0, &rv); zend_printf("Exception: %s\n", Z_STRVAL_P(msg)); zval_ptr_dtor(&tmp); EG(exception) = NULL; } else { zend_call_method_with_1_params(NULL, reflection_ptr, NULL, "export", NULL, &ref); } zval_ptr_dtor(&ref); zval_ptr_dtor(&arg); break; } case PHP_MODE_REFLECTION_EXT_INFO: { int len = (int)strlen(reflection_what); char *lcname = zend_str_tolower_dup(reflection_what, len); zend_module_entry *module; if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) { if (!strcmp(reflection_what, "main")) { display_ini_entries(NULL); } else { zend_printf("Extension '%s' not present.\n", reflection_what); exit_status = 1; } } else { php_info_print_module(module); } efree(lcname); break; } case PHP_MODE_SHOW_INI_CONFIG: { zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH); zend_printf("Loaded Configuration File: %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)"); zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path ? php_ini_scanned_path : "(none)"); zend_printf("Additional .ini files parsed: %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)"); break; } } } zend_end_try(); out: if (request_started) { php_request_shutdown((void *) 0); } if (translated_path) { free(translated_path); } if (exit_status == 0) { exit_status = EG(exit_status); } return exit_status; err: sapi_deactivate(); zend_ini_deactivate(); exit_status = 1; goto out; }
PHP_METHOD(jsonrpc_server, register) { zval *closure=NULL; zval *val; zval *name; zval *callbacks; char *lcname; char *name_str, *tmp; int name_len, tmp_len; zval *classname; zend_class_entry **pce; zend_class_entry *ce; zend_function *fptr; zval *object = getThis(); if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "zO", &name, &closure, zend_ce_closure) == SUCCESS) { fptr = (zend_function*)zend_get_closure_method_def(closure TSRMLS_CC); Z_ADDREF_P(closure); } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &name, &name_str, &name_len) == SUCCESS) { if ((tmp = strstr(name_str, "::")) == NULL) { char *nsname; lcname = zend_str_tolower_dup(name_str, name_len); // Ignore leading "\" nsname = lcname; if (lcname[0] == '\\') { nsname = &lcname[1]; name_len--; } if (zend_hash_find(EG(function_table), nsname, name_len + 1, (void **)&fptr) == FAILURE) { efree(lcname); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function %s() does not exist", name_str); return; } efree(lcname); }else { tmp_len = tmp - name_str; MAKE_STD_ZVAL(classname); ZVAL_STRINGL(classname, name_str, tmp_len, 1); name_len = name_len - (tmp_len + 2); name_str = tmp + 2; //php_printf("classname: %s, method: %s\n", Z_STRVAL_P(classname), name_str); if (zend_lookup_class(Z_STRVAL_P(classname), Z_STRLEN_P(classname), &pce TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s does exist", Z_STRVAL_P(classname)); //zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, // "Class %s does not exist", Z_STRVAL_P(classname)); zval_dtor(classname); return; } ce = *pce; lcname = zend_str_tolower_dup(name_str, name_len); if (zend_hash_find(&ce->function_table, lcname, name_len + 1, (void **) &fptr) == FAILURE) { efree(lcname); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Method %s::%s() does not exist", ce->name, name_str); //zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, // "Method %s::%s() does not exist", ce->name, name_str); return; } efree(lcname); } }else { return ; } callbacks = zend_read_property( php_jsonrpc_server_entry, object, "callbacks", sizeof("callbacks")-1, 0 TSRMLS_CC ); MAKE_STD_ZVAL(val); #if PHP_VERSION_ID < 50399 zend_create_closure(val, fptr TSRMLS_CC); #else zend_create_closure(val, fptr, NULL, NULL TSRMLS_CC); #endif add_assoc_zval(callbacks, Z_STRVAL_P(name), val); zend_update_property(php_jsonrpc_server_entry, object, "callbacks", sizeof("callbacks")-1, callbacks TSRMLS_CC); RETURN_ZVAL(object,1,0); }
static int http_request_on_header_value(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len); if (strncmp(header_name, "cookie", 6) == 0) { zval *cookie; MAKE_STD_ZVAL(cookie); array_init(cookie); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("cookie"), cookie TSRMLS_CC); struct { char *k; int klen; char *v; int vlen; } kv = { 0 }; char *_c = (char *) at; int n = 1; kv.k = _c; while (_c < at + length) { if (*_c == '=') { kv.v = _c + 1; kv.klen = n; n = 0; } else if (*_c == ';') { kv.vlen = n; add_assoc_stringl_ex(cookie, kv.k, kv.klen, kv.v, kv.vlen, 1); kv.k = _c + 2; n = 0; } else { n++; } _c++; } kv.vlen = n; add_assoc_stringl_ex(cookie, kv.k, kv.klen, kv.v, kv.vlen, 1); } else { zval *header = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("header"), 1 TSRMLS_CC); add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1); } if (client->current_header_name_allocated) { efree(client->current_header_name); client->current_header_name_allocated = 0; } efree(header_name); return 0; }
static void dic_optimizer_get_handler(INTERNAL_FUNCTION_PARAMETERS) { char *id, should_free = 1; zend_ulong id_hash; int id_len, i; long oninvalid, exception_on_invalid_reference_const; zval **found_service, **alias, **method_in_map = NULL, *this_services, *this_aliases, *this_methodMap, *this_loading; zend_function *method = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &id, &id_len, &oninvalid) == FAILURE) { return; } if (strcasecmp(id, "service_container") == 0) { RETURN_THIS; } this_services = zend_read_property(Z_OBJCE_P(getThis()), getThis(), SYMFONY_DIC_TOKEN_SERVICES_KEY, sizeof(SYMFONY_DIC_TOKEN_SERVICES_KEY) - 1, 0 TSRMLS_CC); this_aliases = zend_read_property(Z_OBJCE_P(getThis()), getThis(), SYMFONY_DIC_TOKEN_ALIASES_KEY, sizeof(SYMFONY_DIC_TOKEN_ALIASES_KEY) - 1, 0 TSRMLS_CC); this_loading = zend_read_property(Z_OBJCE_P(getThis()), getThis(), SYMFONY_DIC_TOKEN_LOADING_KEY, sizeof(SYMFONY_DIC_TOKEN_LOADING_KEY) - 1, 0 TSRMLS_CC); this_methodMap = zend_read_property(Z_OBJCE_P(getThis()), getThis(), SYMFONY_DIC_TOKEN_METHODMAP_KEY, sizeof(SYMFONY_DIC_TOKEN_METHODMAP_KEY) - 1, 0 TSRMLS_CC); if (!SYMFONY_DIC_G(cache_done)) { zval invalidBehavior; zend_class_entry **exception_ce; zend_get_constant_ex("self::EXCEPTION_ON_INVALID_REFERENCE", sizeof("self::EXCEPTION_ON_INVALID_REFERENCE") - 1, &invalidBehavior, Z_OBJCE_P(getThis()), 0 TSRMLS_CC); SYMFONY_DIC_G(invalid_behavior) = Z_LVAL(invalidBehavior); if (FAILURE == zend_lookup_class(SYMFONY_DIC_TOKEN_SERVICE_CIRCULAR_REFERENCE_EXCEPTION_KEY_UP, sizeof(SYMFONY_DIC_TOKEN_SERVICE_CIRCULAR_REFERENCE_EXCEPTION_KEY_UP) - 1, &exception_ce TSRMLS_CC)) { zend_error_noreturn(E_ERROR, "Class %s not found", SYMFONY_DIC_TOKEN_SERVICE_CIRCULAR_REFERENCE_EXCEPTION_KEY_UP); } SYMFONY_DIC_G(ServiceCircularReferenceException) = *exception_ce; if (FAILURE == zend_lookup_class(SYMFONY_DIC_TOKEN_SERVICE_NOT_FOUND_EXCEPTION_KEY_UP, sizeof(SYMFONY_DIC_TOKEN_SERVICE_NOT_FOUND_EXCEPTION_KEY_UP) - 1, &exception_ce TSRMLS_CC)) { zend_error_noreturn(E_ERROR, "Class %s not found", SYMFONY_DIC_TOKEN_SERVICE_NOT_FOUND_EXCEPTION_KEY_UP); } SYMFONY_DIC_G(ServiceNotFoundException) = *exception_ce; if (FAILURE == zend_lookup_class(SYMFONY_DIC_TOKEN_INACTIVE_SCOPE_EXCEPTION_KEY_UP, sizeof(SYMFONY_DIC_TOKEN_INACTIVE_SCOPE_EXCEPTION_KEY_UP) - 1, &exception_ce TSRMLS_CC)) { zend_error_noreturn(E_ERROR, "Class %s not found", SYMFONY_DIC_TOKEN_INACTIVE_SCOPE_EXCEPTION_KEY_UP); } SYMFONY_DIC_G(InactiveScopeException) = *exception_ce; SYMFONY_DIC_G(cache_done) = 1; } exception_on_invalid_reference_const = SYMFONY_DIC_G(invalid_behavior); if (ZEND_NUM_ARGS() <= 1) { oninvalid = exception_on_invalid_reference_const; } if (IS_INTERNED(id)) { id_hash = INTERNED_HASH(id); } else { id_hash = zend_inline_hash_func(id, id_len + 1); } id = estrndup(id, id_len); /* zend_str_tolower will change it otherwise */ for (i = 0; i <= 1; i++, zend_str_tolower(id, id_len), id_hash = zend_inline_hash_func(id, id_len + 1)) { if (zend_hash_quick_find(Z_ARRVAL_P(this_aliases), id, id_len + 1, id_hash, (void **)&alias) == SUCCESS) { should_free = 0; efree(id); id = Z_STRVAL_PP(alias); id_len = Z_STRLEN_PP(alias); id_hash = zend_inline_hash_func(id, id_len + 1); } if (zend_hash_quick_find(Z_ARRVAL_P(this_services), id, id_len + 1, id_hash, (void **)&found_service) == SUCCESS) { RETVAL_ZVAL_FAST(*found_service); goto free_and_return; } } if (zend_hash_quick_exists(Z_ARRVAL_P(this_loading), id, id_len + 1, id_hash)) { zval *ServiceCircularReferenceException; ALLOC_INIT_ZVAL(ServiceCircularReferenceException); /* ctor_args */ object_init_ex(ServiceCircularReferenceException, SYMFONY_DIC_G(ServiceCircularReferenceException)); zend_throw_exception_object(ServiceCircularReferenceException TSRMLS_CC); goto free_and_return; } zend_hash_quick_find(Z_ARRVAL_P(this_methodMap), id, id_len + 1, id_hash, (void **)&method_in_map); if (!method_in_map) { char *new_id; for (i=0; i < id_len; i++) { if (id[i] == '_') { memmove(&id[i], &id[i + 1], --id_len); } } php_strtr(id, id_len, ".\\", "__", 2); id_len = spprintf(&new_id, 0, "get%sservice", id); efree(id); id = new_id; id_hash = zend_inline_hash_func(id, id_len + 1); zend_hash_quick_find(&Z_OBJCE_P(getThis())->function_table, id, id_len + 1, id_hash, (void **)&method); if (!method) { if (oninvalid == exception_on_invalid_reference_const) { zval *ServiceNotFoundException; ALLOC_INIT_ZVAL(ServiceNotFoundException); object_init_ex(ServiceNotFoundException, SYMFONY_DIC_G(ServiceNotFoundException)); zend_throw_exception_object(ServiceNotFoundException TSRMLS_CC); /* ctor_args */ } goto free_and_return; } } else { char *method_name_lc; method_name_lc = zend_str_tolower_dup(Z_STRVAL_PP(method_in_map), Z_STRLEN_PP(method_in_map)); zend_hash_find(&Z_OBJCE_P(getThis())->function_table, method_name_lc, Z_STRLEN_PP(method_in_map) + 1, (void **)&method); efree(method_name_lc); } zend_fcall_info fci = {0}; zend_fcall_info_cache fcic = {0}; zval *loading, *result; ALLOC_INIT_ZVAL(loading); ZVAL_BOOL(loading, 1); zend_hash_quick_add(Z_ARRVAL_P(this_loading), id, id_len + 1, id_hash, &loading, sizeof(zval *), NULL); fcic.called_scope = Z_OBJCE_P(getThis()); fcic.calling_scope = Z_OBJCE_P(getThis()); fcic.function_handler = method; fcic.initialized = 1; fcic.object_ptr = getThis(); fci.retval_ptr_ptr = &result; fci.size = sizeof(zend_fcall_info); zend_call_function(&fci, &fcic TSRMLS_CC); zend_hash_quick_del(Z_ARRVAL_P(this_loading), id, id_len + 1, id_hash); if (!EG(exception)) { RETVAL_ZVAL_FAST(result); } else { zend_hash_quick_del(Z_ARRVAL_P(this_services), id, id_len, id_hash); if (instanceof_function(Z_OBJCE_P(EG(exception)), SYMFONY_DIC_G(InactiveScopeException) TSRMLS_CC) && oninvalid == exception_on_invalid_reference_const) { EG(exception) = NULL; } } zval_ptr_dtor(&result); free_and_return: if (should_free) { efree(id); } return; }
static inline void yaf_dispatcher_fix_default(yaf_dispatcher_t *dispatcher, yaf_request_t *request) /* {{{ */ { zval *module, *controller, *action; module = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1, NULL); action = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), 1, NULL); controller = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), 1, NULL); if (Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module)) { zval *default_module = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1, NULL); zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), default_module); } else { char *p = zend_str_tolower_dup(Z_STRVAL_P(module), Z_STRLEN_P(module)); *p = toupper(*p); zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), p, Z_STRLEN_P(module)); efree(p); } if (Z_TYPE_P(controller) != IS_STRING || !Z_STRLEN_P(controller)) { zval *default_controller = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), 1, NULL); zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), default_controller); } else { char *q, *p = zend_str_tolower_dup(Z_STRVAL_P(controller), Z_STRLEN_P(controller)); /** * Upper controller name * eg: Index_sub -> Index_Sub */ q = p; *q = toupper(*q); while (*q != '\0') { if (*q == '_' || *q == '\\') { if (*(q+1) != '\0') { *(q+1) = toupper(*(q+1)); q++; } } q++; } zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), p, Z_STRLEN_P(controller)); efree(p); } if (Z_TYPE_P(action) != IS_STRING || !Z_STRLEN_P(action)) { zval *default_action = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), 1, NULL); zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), default_action); } else { char *p = zend_str_tolower_dup(Z_STRVAL_P(action), Z_STRLEN_P(action)); zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), p, Z_STRLEN_P(action)); efree(p); } }
static int http_request_on_header_value(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len); if (memcmp(header_name, ZEND_STRL("cookie")) == 0) { zval *cookie; MAKE_STD_ZVAL(cookie); array_init(cookie); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("cookie"), cookie TSRMLS_CC); struct { char *k; int klen; char *v; int vlen; } kv = { 0 }; char keybuf[SW_HTTP_COOKIE_KEYLEN]; char *_c = (char *) at; int n = 1; kv.k = _c; while (_c < at + length) { if (*_c == '=') { kv.v = _c + 1; kv.klen = n; n = 0; } else if (*_c == ';') { kv.vlen = n; if (kv.klen >= SW_HTTP_COOKIE_KEYLEN) { kv.klen = SW_HTTP_COOKIE_KEYLEN - 1; } memcpy(keybuf, kv.k, kv.klen - 1); keybuf[kv.klen - 1] = 0; add_assoc_stringl_ex(cookie, keybuf, kv.klen, kv.v, kv.vlen, 1); kv.k = _c + 2; n = 0; } else { n++; } _c++; } kv.vlen = n; if (kv.klen >= SW_HTTP_COOKIE_KEYLEN) { kv.klen = SW_HTTP_COOKIE_KEYLEN - 1; } memcpy(keybuf, kv.k, kv.klen - 1); keybuf[kv.klen - 1] = 0; add_assoc_stringl_ex(cookie, keybuf, kv.klen , kv.v, kv.vlen, 1); // ZEND_SET_SYMBOL(&EG(symbol_table), "_COOKIE", cookie); mergeGlobal(cookie, client->zrequest, HTTP_GLOBAL_COOKIE); } else if (memcmp(header_name, ZEND_STRL("upgrade")) == 0 && memcmp(at, ZEND_STRL("websocket")) == 0) { SwooleG.lock.lock(&SwooleG.lock); swConnection *conn = swServer_connection_get(SwooleG.serv, client->fd); if(conn->websocket_status == 0) { conn->websocket_status = WEBSOCKET_STATUS_CONNECTION; } SwooleG.lock.unlock(&SwooleG.lock); zval *header = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("header"), 1 TSRMLS_CC); add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1); } else if (parser->method == PHP_HTTP_POST && memcmp(header_name, ZEND_STRL("content-type")) == 0 && memcmp(at, ZEND_STRL("application/x-www-form-urlencoded")) == 0) { client->request.post_form_urlencoded = 1; zval *header = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("header"), 1 TSRMLS_CC); add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1); } else { zval *header = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("header"), 1 TSRMLS_CC); add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1); } if (client->current_header_name_allocated) { efree(client->current_header_name); client->current_header_name_allocated = 0; } efree(header_name); return 0; }
static inline int phpdbg_call_register(phpdbg_param_t *stack) /* {{{ */ { phpdbg_param_t *name = NULL; if (stack->type == STACK_PARAM) { char *lc_name; name = stack->next; if (!name || name->type != STR_PARAM) { return FAILURE; } lc_name = zend_str_tolower_dup(name->str, name->len); if (zend_hash_str_exists(&PHPDBG_G(registered), lc_name, name->len)) { zval fretval; zend_fcall_info fci; memset(&fci, 0, sizeof(zend_fcall_info)); ZVAL_STRINGL(&fci.function_name, lc_name, name->len); fci.size = sizeof(zend_fcall_info); fci.function_table = &PHPDBG_G(registered); fci.symbol_table = zend_rebuild_symbol_table(); fci.object = NULL; fci.retval = &fretval; fci.no_separation = 1; if (name->next) { zval params; phpdbg_param_t *next = name->next; array_init(¶ms); while (next) { char *buffered = NULL; switch (next->type) { case OP_PARAM: case COND_PARAM: case STR_PARAM: add_next_index_stringl(¶ms, next->str, next->len); break; case NUMERIC_PARAM: add_next_index_long(¶ms, next->num); break; case METHOD_PARAM: spprintf(&buffered, 0, "%s::%s", next->method.class, next->method.name); add_next_index_string(¶ms, buffered); break; case NUMERIC_METHOD_PARAM: spprintf(&buffered, 0, "%s::%s#%ld", next->method.class, next->method.name, next->num); add_next_index_string(¶ms, buffered); break; case NUMERIC_FUNCTION_PARAM: spprintf(&buffered, 0, "%s#%ld", next->str, next->num); add_next_index_string(¶ms, buffered); break; case FILE_PARAM: spprintf(&buffered, 0, "%s:%ld", next->file.name, next->file.line); add_next_index_string(¶ms, buffered); break; case NUMERIC_FILE_PARAM: spprintf(&buffered, 0, "%s:#%ld", next->file.name, next->file.line); add_next_index_string(¶ms, buffered); break; default: { /* not yet */ } } next = next->next; } zend_fcall_info_args(&fci, ¶ms); } else {
static int zephir_is_callable_check_class(const char *name, int name_len, zend_fcall_info_cache *fcc, int *strict_class, char **error) /* {{{ */ { int ret = 0; zend_class_entry *pce; char *lcname = zend_str_tolower_dup(name, name_len); *strict_class = 0; if (name_len == sizeof("self") - 1 && !memcmp(lcname, "self", sizeof("self") - 1)) { if (!EG(scope)) { if (error) *error = estrdup("cannot access self:: when no class scope is active"); } else { fcc->called_scope = EG(current_execute_data)->called_scope; if (!fcc->object) { fcc->object = Z_OBJ(EG(current_execute_data)->This); } ret = 1; } } else if (name_len == sizeof("parent") - 1 && !memcmp(lcname, "parent", sizeof("parent") - 1)) { if (!EG(scope)) { if (error) *error = estrdup("cannot access parent:: when no class scope is active"); } else if (!EG(scope)->parent) { if (error) *error = estrdup("cannot access parent:: when current class scope has no parent"); } else { fcc->called_scope = EG(current_execute_data)->called_scope; if (!fcc->object) { fcc->object = Z_OBJ(EG(current_execute_data)->This); } *strict_class = 1; ret = 1; } } else if (name_len == sizeof("static") - 1 && !memcmp(lcname, "static", sizeof("static") - 1)) { if (!EG(current_execute_data)->called_scope) { if (error) *error = estrdup("cannot access static:: when no class scope is active"); } else { fcc->called_scope = EG(current_execute_data)->called_scope; if (!fcc->object) { fcc->object = Z_OBJ(EG(current_execute_data)->This); } *strict_class = 1; ret = 1; } } else { zend_string *class_name; class_name = zend_string_init(name, name_len, 0); if ((pce = zend_lookup_class_ex(class_name, NULL, 1)) != NULL) { zend_class_entry *scope = EG(current_execute_data) ? EG(current_execute_data)->func->common.scope : NULL; fcc->calling_scope = pce; if (scope && !fcc->object && EG(current_execute_data) && Z_OBJ(EG(current_execute_data)->This) && instanceof_function(Z_OBJCE(EG(current_execute_data)->This), scope TSRMLS_CC) && instanceof_function(scope, fcc->calling_scope TSRMLS_CC)) { fcc->object = Z_OBJ(EG(current_execute_data)->This); fcc->called_scope = fcc->object->ce; } else { fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope; } *strict_class = 1; ret = 1; } else { if (error) zephir_spprintf(error, 0, "class '%.*s' not found", name_len, name); } zend_string_free(class_name); } efree(lcname); return ret; }
/* }}} */ /** {{{ zval * yaf_controller_render(yaf_controller_t *instance, char *action_name, int len, zval *var_array TSRMLS_DC) */ zval * yaf_controller_render(yaf_controller_t *instance, char *action_name, int len, zval *var_array TSRMLS_DC) { char *path, *view_ext, *self_name, *tmp; zval *name, *param, *ret = NULL; int path_len; yaf_view_t *view; zend_class_entry *view_ce; view = zend_read_property(yaf_controller_ce, instance, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_VIEW), 0 TSRMLS_CC); name = zend_read_property(yaf_controller_ce, instance, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), 0 TSRMLS_CC); view_ext = YAF_G(view_ext); self_name = zend_str_tolower_dup(Z_STRVAL_P(name), Z_STRLEN_P(name)); tmp = self_name; while (*tmp != '\0') { if (*tmp == '_') { *tmp = DEFAULT_SLASH; } tmp++; } action_name = estrndup(action_name, len); tmp = action_name; while (*tmp != '\0') { if (*tmp == '_') { *tmp = DEFAULT_SLASH;
ZEND_METHOD(afk_app, run){/*{{{*/ zval **uri; char *c=NULL, *a=NULL; zval *arr = PG(http_globals)[TRACK_VARS_GET]; //从http_globals中寻找get参数,确认Controller和action的值。 if(arr && Z_TYPE_P(arr) == IS_ARRAY){ if(zend_hash_find(HASH_OF(arr), HTTP_CONTRONLLER_PARAM, strlen(HTTP_CONTRONLLER_PARAM)+1, (void **)&uri) == SUCCESS){ c = Z_STRVAL_PP(uri); }else{ c = "index"; } if(zend_hash_find(HASH_OF(arr), HTTP_ACTION_PARAM, strlen(HTTP_ACTION_PARAM)+1, (void **)&uri) == SUCCESS){ a = Z_STRVAL_PP(uri); }else{ a = "index"; } } //寻找对应的Controller和action方法所在的文件。 char *controller_path; spprintf(&controller_path, 0, "%s/controller/%s.php", APP_DIR, c); FILE *fp; //php_printf("%s\n", controller_path); //文件存在则引入该文件。不存在则报错。 if( (fp = fopen(controller_path, "r")) != NULL){ fclose(fp); int dummy = 1; zend_file_handle file_handle; zend_op_array *op_array; file_handle.filename = controller_path; file_handle.free_filename = 0; file_handle.type = ZEND_HANDLE_FILENAME; file_handle.opened_path = NULL; file_handle.handle.fp = NULL; op_array = zend_compile_file(&file_handle, ZEND_INCLUDE TSRMLS_CC); if (op_array && file_handle.handle.stream.handle) { int dummy = 1; if (!file_handle.opened_path) { file_handle.opened_path = controller_path; } php_printf("opened_path: %s\n", file_handle.opened_path); zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL); } zend_destroy_file_handle(&file_handle TSRMLS_CC); if(op_array){ php_printf("execute op_array \n"); //保存旧的环境变量 zval ** __old_return_value_pp = EG(return_value_ptr_ptr); zend_op ** __old_opline_ptr = EG(opline_ptr); zend_op_array * __old_op_array = EG(active_op_array); zend_function_state * __old_func_state = EG(function_state_ptr); //执行op_array zval *result = NULL; EG(return_value_ptr_ptr) = &result; EG(active_op_array) = op_array; zend_execute(op_array TSRMLS_CC); destroy_op_array(op_array TSRMLS_CC); efree(op_array); //恢复旧的环境变量 EG(return_value_ptr_ptr) = __old_return_value_pp; EG(opline_ptr) = __old_opline_ptr; EG(active_op_array) = __old_op_array; EG(function_state_ptr) = __old_func_state; } }else{ char *error; spprintf(&error, 0, "cann't find file %s", controller_path); zend_error(1, error); } //dispatcher 调用分发的请求。 /** *在EG(class_table)查找相应的类,然后调用它的方法。 */ zend_class_entry **class = NULL; char *class_name = emalloc(strlen(c)+strlen("Controller")+1); class_name = strcpy(class_name, c); class_name = strcat(class_name, zend_str_tolower_dup("Controller", strlen("Controller")+1)); //Notice: class name need tolower. if(zend_hash_find(EG(class_table), class_name, strlen(class_name)+1, (void *)&class) != SUCCESS){ char *error; spprintf(&error, 0, "cann't find the controller class: %s ", class_name); php_printf("%s", class_name); efree(class_name); efree(class); zend_error(1, error); } efree(class_name); zval *obj, *function_name, *retval; MAKE_STD_ZVAL(obj); MAKE_STD_ZVAL(function_name); MAKE_STD_ZVAL(retval); object_init_ex(obj, *class); //php_var_dump(&obj, 1 TSRMLS_CC); ZVAL_STRINGL(function_name, "indexaction", strlen("indexaction"), 1); call_user_function(&((*class)->function_table), &obj, function_name, retval, 0, NULL TSRMLS_CC); zval_ptr_dtor(&obj); zval_ptr_dtor(&function_name); zval_ptr_dtor(&retval); //efree(class); RETURN_BOOL(1); }/*}}}*/