Skip to content

Commit

Permalink
Branch 1650. Added web server > "hide_files" option to settings.json
Browse files Browse the repository at this point in the history
(Issue 17).
  • Loading branch information
cztomczak committed Feb 13, 2015
1 parent 44e5577 commit 1afd0c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
11 changes: 8 additions & 3 deletions phpdesktop-chrome/mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -2773,15 +2773,20 @@ static int scan_directory(struct mg_connection *conn, const char *dir,
de.conn = conn;

while ((dp = readdir(dirp)) != NULL) {
// PHP Desktop fix (Issue 17):
// In handle_request() a full path is passed to must_hide_file. But
// when scanning directory only directory name was passed. This causes
// problems with hide_files_patterns. Fix it by providing full directory
// path to must_hide_file().
mg_snprintf(conn, path, sizeof(path), "%s%c%s", dir, '/', dp->d_name);

// Do not show current dir and hidden files
if (!strcmp(dp->d_name, ".") ||
!strcmp(dp->d_name, "..") ||
must_hide_file(conn, dp->d_name)) {
must_hide_file(conn, path)) {
continue;
}

mg_snprintf(conn, path, sizeof(path), "%s%c%s", dir, '/', dp->d_name);

// If we don't memset stat structure to zero, mtime will have
// garbage and strftime() will segfault later on in
// print_dir_entry(). memset is required only if mg_stat()
Expand Down
3 changes: 2 additions & 1 deletion phpdesktop-chrome/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"cgi_interpreter": "../php/php-cgi.exe",
"cgi_extensions": ["php"],
"cgi_temp_dir": "",
"404_handler": "/pretty-urls.php"
"404_handler": "/pretty-urls.php",
"hide_files": []
},
"chrome": {
"log_file": "debug.log",
Expand Down
22 changes: 18 additions & 4 deletions phpdesktop-chrome/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool StartWebServer() {

// 404_handler
std::string _404_handler = (*appSettings)["web_server"]["404_handler"];

// Ip address and port. If port was set to 0, then real port
// will be known only after the webserver was started.
std::string ipAddress = (*appSettings)["web_server"]["listen_on"][0];
Expand Down Expand Up @@ -117,6 +117,19 @@ bool StartWebServer() {
cgiPattern = "**.php$";
LOG_INFO << "CGI pattern: " << cgiPattern;

// Hide files patterns.
const json_value hide_files = (*appSettings)["web_server"]["hide_files"];
std::string hide_files_patterns = "";
for (int i = 0; i < 100; i++) {
const char* pattern = hide_files[i];
if (strlen(pattern)) {
if (hide_files_patterns.length())
hide_files_patterns.append("|");
hide_files_patterns.append("**/").append(pattern).append("$");
}
}
LOG_INFO << "Hide files patterns: " << hide_files_patterns;

// Temp directory.
std::string cgi_temp_dir = (*appSettings)["web_server"]["cgi_temp_dir"];
cgi_temp_dir = GetAbsolutePath(cgi_temp_dir);
Expand All @@ -126,7 +139,7 @@ bool StartWebServer() {
<< cgi_temp_dir;
}
cgi_temp_dir.assign(GetAnsiTempDirectory());
}
}

// CGI environment variables.
std::string cgiEnvironment = "";
Expand Down Expand Up @@ -154,6 +167,7 @@ bool StartWebServer() {
"cgi_pattern", cgiPattern.c_str(),
"cgi_environment", cgiEnvironment.c_str(),
"404_handler", _404_handler.c_str(),
"hide_files_patterns", hide_files_patterns.c_str(),
NULL
};

Expand All @@ -167,7 +181,7 @@ bool StartWebServer() {
g_mongooseContext = mg_start(&callbacks, NULL, options);
if (g_mongooseContext == NULL)
return false;

// When port was set to 0 then a random free port was assigned.
g_webServerPort = mg_get_listening_port(g_mongooseContext);
g_webServerIpAddress = ipAddress;
Expand Down Expand Up @@ -210,4 +224,4 @@ std::string GetWwwDirectory() {
}
std::string GetCgiInterpreter() {
return g_cgiInterpreter;
}
}

0 comments on commit 1afd0c8

Please sign in to comment.