From 9b8b31b62ac6c583a2d9f89286d599e135acfb52 Mon Sep 17 00:00:00 2001 From: Drew Paroski Date: Fri, 17 May 2013 23:26:16 -0700 Subject: [PATCH] Fix bug with $_SERVER['PHP_SELF'] Currently we have bug where $_SERVER['PHP_SELF'] is "" a script is invoked at the command line without using "-f" or "--file". This diff fixes the problem by making more places use the adjusted version of argc/argv built using prepare_args(). This also allows us to get rid of a old hack we've had for a long time for supporting running scripts without using "-f" or "--file" at the command line. Github issue 730: https://github.com/facebook/hiphop-php/issues/730 --- hphp/runtime/base/builtin_functions.cpp | 20 +---------------- hphp/runtime/base/program_functions.cpp | 29 ++++++++++++++++--------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/hphp/runtime/base/builtin_functions.cpp b/hphp/runtime/base/builtin_functions.cpp index 1131f8882..db51dadb4 100644 --- a/hphp/runtime/base/builtin_functions.cpp +++ b/hphp/runtime/base/builtin_functions.cpp @@ -1036,25 +1036,7 @@ Variant invoke_file(CStrRef s, bool once, const char *currentDir) { if (invoke_file_impl(r, s, once, currentDir)) { return r; } - if (!s.empty()) { - return throw_missing_file(s.c_str()); - } - // The gross hack which follows is here so that "hhvm foo.php" works - // the same as "hhvm -f foo.php". - // TODO Task #2171414: Find a less hacky way to accomplish this; we probably - // should be handling this elsewhere at a higher level rather than within - // the bowels of the invoke/include machinery - SystemGlobals* g = (SystemGlobals*)get_global_variables(); - Variant& v_argc = g->GV(argc); - Variant& v_argv = g->GV(argv); - if (!more(v_argc, int64_t(0))) { - return true; - } - String s2 = toString(v_argv.rvalAt(int64_t(0), AccessFlags::Error)); - if (invoke_file_impl(r, s2, once, "")) { - return r; - } - return throw_missing_file(s2.c_str()); + return throw_missing_file(s.c_str()); } bool invoke_file_impl(Variant &res, CStrRef path, bool once, diff --git a/hphp/runtime/base/program_functions.cpp b/hphp/runtime/base/program_functions.cpp index daac9395d..c0a00c035 100644 --- a/hphp/runtime/base/program_functions.cpp +++ b/hphp/runtime/base/program_functions.cpp @@ -491,13 +491,17 @@ void execute_command_line_begin(int argc, char **argv, int xhprof) { now = time(nullptr); now_double = (double)now; } + String file = empty_string; + if (argc > 0) { + file = NEW(StringData)(argv[0], AttachLiteral); + } server.set(s_REQUEST_START_TIME, now); server.set(s_REQUEST_TIME, now); server.set(s_REQUEST_TIME_FLOAT, now_double); server.set(s_DOCUMENT_ROOT, empty_string); - server.set(s_SCRIPT_FILENAME, argv[0]); - server.set(s_SCRIPT_NAME, argv[0]); - server.set(s_PHP_SELF, argv[0]); + server.set(s_SCRIPT_FILENAME, file); + server.set(s_SCRIPT_NAME, file); + server.set(s_PHP_SELF, file); server.set(s_argv, g->GV(argv)); server.set(s_argc, g->GV(argc)); server.set(s_PWD, g_context->getCwd()); @@ -1031,13 +1035,18 @@ static int execute_program_impl(int argc, char **argv) { if (!po.file.empty()) { Repo::setCliFile(po.file); - } else if (new_argc >= 1) { + } else if (new_argc > 0) { Repo::setCliFile(new_argv[0]); } int ret = 0; hphp_process_init(); + string file; + if (new_argc > 0) { + file = new_argv[0]; + } + if (po.mode == "debug") { StackTraceNoHeap::AddExtraLogging("IsDebugger", "True"); RuntimeOption::EnableDebugger = true; @@ -1049,7 +1058,6 @@ static int execute_program_impl(int argc, char **argv) { } Eval::Debugger::RegisterSandbox(proxy->getDummyInfo()); Eval::Debugger::RegisterThread(); - string file = po.file; StringVecPtr client_args; bool restart = false; ret = 0; @@ -1081,10 +1089,10 @@ static int execute_program_impl(int argc, char **argv) { for (int i = 0; i < po.count; i++) { execute_command_line_begin(new_argc, new_argv, po.xhprofFlags); ret = 1; - if (hphp_invoke_simple(po.file)) { + if (hphp_invoke_simple(file)) { ret = ExitException::ExitCode; } - execute_command_line_end(po.xhprofFlags, true, new_argv[0]); + execute_command_line_end(po.xhprofFlags, true, file.c_str()); } } @@ -1296,9 +1304,10 @@ ExecutionContext *hphp_context_init() { bool hphp_invoke_simple(const std::string &filename, bool warmupOnly /* = false */) { - bool error; string errorMsg; - return hphp_invoke(g_context.getNoCheck(), filename, false, null_array, uninit_null(), - "", "", error, errorMsg, true, warmupOnly); + bool error; + string errorMsg; + return hphp_invoke(g_context.getNoCheck(), filename, false, null_array, + uninit_null(), "", "", error, errorMsg, true, warmupOnly); } bool hphp_invoke(ExecutionContext *context, const std::string &cmd,