ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

createdirectoryhandler.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/networking/sdl_net/handlers/createdirectoryhandler.h"
00024 #include "backends/fs/fs-factory.h"
00025 #include "backends/networking/sdl_net/handlerutils.h"
00026 #include "backends/networking/sdl_net/localwebserver.h"
00027 #include "common/translation.h"
00028 #include <common/callback.h>
00029 
00030 namespace Networking {
00031 
00032 CreateDirectoryHandler::CreateDirectoryHandler() {}
00033 
00034 CreateDirectoryHandler::~CreateDirectoryHandler() {}
00035 
00036 void CreateDirectoryHandler::handleError(Client &client, Common::String message) const {
00037     if (client.queryParameter("answer_json") == "true")
00038         setJsonResponseHandler(client, "error", message);
00039     else
00040         HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
00041 }
00042 
00043 void CreateDirectoryHandler::setJsonResponseHandler(Client &client, Common::String type, Common::String message) const {
00044     Common::JSONObject response;
00045     response.setVal("type", new Common::JSONValue(type));
00046     response.setVal("message", new Common::JSONValue(message));
00047 
00048     Common::JSONValue json = response;
00049     LocalWebserver::setClientGetHandler(client, json.stringify(true));
00050 }
00051 
00053 
00054 void CreateDirectoryHandler::handle(Client &client) {
00055     Common::String path = client.queryParameter("path");
00056     Common::String name = client.queryParameter("directory_name");
00057 
00058     // check that <path> is not an absolute root
00059     if (path == "" || path == "/") {
00060         handleError(client, _("Can't create directory here!"));
00061         return;
00062     }
00063 
00064     // check that <path> contains no '../'
00065     if (HandlerUtils::hasForbiddenCombinations(path)) {
00066         handleError(client, _("Invalid path!"));
00067         return;
00068     }
00069 
00070     // transform virtual path to actual file system one
00071     Common::String prefixToRemove = "", prefixToAdd = "";
00072     if (!transformPath(path, prefixToRemove, prefixToAdd) || path.empty()) {
00073         handleError(client, _("Invalid path!"));
00074         return;
00075     }
00076 
00077     // check that <path> exists, is directory and isn't forbidden
00078     AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(path);
00079     if (!HandlerUtils::permittedPath(node->getPath())) {
00080         handleError(client, _("Invalid path!"));
00081         return;
00082     }
00083     if (!node->exists()) {
00084         handleError(client, _("Parent directory doesn't exists!"));
00085         return;
00086     }
00087     if (!node->isDirectory()) {
00088         handleError(client, _("Can't create a directory within a file!"));
00089         return;
00090     }
00091 
00092     // check that <directory_name> doesn't exist or is directory
00093     if (path.lastChar() != '/' && path.lastChar() != '\\')
00094         path += '/';
00095     node = g_system->getFilesystemFactory()->makeFileNodePath(path + name);
00096     if (node->exists()) {
00097         if (!node->isDirectory()) {
00098             handleError(client, _("There is a file with that name in the parent directory!"));
00099             return;
00100         }
00101     } else {
00102         // create the <directory_name> in <path>
00103         if (!node->create(true)) {
00104             handleError(client, _("Failed to create the directory!"));
00105             return;
00106         }
00107     }
00108 
00109     // if json requested, respond with it
00110     if (client.queryParameter("answer_json") == "true") {
00111         setJsonResponseHandler(client, "success", _("Directory created successfully!"));
00112         return;
00113     }
00114 
00115     // set redirect on success
00116     HandlerUtils::setMessageHandler(
00117         client,
00118         Common::String::format(
00119             "%s<br/><a href=\"files?path=%s\">%s</a>",
00120             _("Directory created successfully!"),
00121             client.queryParameter("path").c_str(),
00122             _("Back to parent directory")
00123         ),
00124         (client.queryParameter("ajax") == "true" ? "/filesAJAX?path=" : "/files?path=") +
00125         LocalWebserver::urlEncodeQueryParameterValue(client.queryParameter("path"))
00126     );
00127 }
00128 
00129 } // End of namespace Networking


Generated on Sat Mar 23 2019 05:01:25 for ResidualVM by doxygen 1.7.1
curved edge   curved edge