2
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3
* http://www.gnu.org/licenses/lgpl-3.0.html
10
#include <sdk_precomp.h>
12
#include <macrosmanager.h>
13
#include <configmanager.h>
14
#include <annoyingdialog.h>
18
#include <wx/string.h>
19
#include <wx/filedlg.h>
22
#include <wx/filename.h>
25
#include "scriptsecuritywarningdlg.h"
26
#include "sc_base_types.h"
28
namespace ScriptBindings
33
bool SecurityAllows(const wxString& operation, const wxString& descr)
35
if (Manager::Get()->GetScriptingManager()->IsCurrentlyRunningScriptTrusted())
38
if (Manager::Get()->GetConfigManager(_T("security"))->ReadBool(operation, false))
41
ScriptSecurityWarningDlg dlg(Manager::Get()->GetAppWindow(), operation, descr);
42
if (dlg.ShowModal() != wxID_OK)
45
ScriptSecurityResponse response = dlg.GetResponse();
52
Manager::Get()->GetConfigManager(_T("security"))->Write(operation, true);
55
case ssrTrust: // purposely fall through
56
case ssrTrustPermanently:
57
Manager::Get()->GetScriptingManager()->TrustCurrentlyRunningScript(response == ssrTrustPermanently);
71
void SetCwd(const wxString& dir)
73
wxSetWorkingDirectory(dir);
76
bool CreateDirRecursively(const wxString& full_path, int perms)
78
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(full_path));
79
NormalizePath(fname, wxEmptyString);
80
if (!SecurityAllows(_T("CreateDir"), fname.GetFullPath()))
82
return ::CreateDirRecursively(fname.GetFullPath(), perms);
85
wxString ChooseDir(const wxString& message, const wxString& initialPath, bool showCreateDirButton)
87
return ChooseDirectory(0, message, Manager::Get()->GetMacrosManager()->ReplaceMacros(initialPath), wxEmptyString, false, showCreateDirButton);
90
bool RemoveDir(const wxString& src)
92
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
93
NormalizePath(fname, wxEmptyString);
94
if (!SecurityAllows(_T("RemoveDir"), fname.GetFullPath()))
96
return wxRmdir(fname.GetFullPath());
99
bool DirectoryExists(const wxString& dir)
101
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(dir));
102
NormalizePath(fname, wxEmptyString);
103
return wxDirExists(fname.GetFullPath());
106
bool CopyFile(const wxString& src, const wxString& dst, bool overwrite)
108
wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
109
wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
110
NormalizePath(fname1, wxEmptyString);
111
NormalizePath(fname2, wxEmptyString);
112
if (!SecurityAllows(_T("CopyFile"), wxString::Format(_T("%s -> %s"), src.c_str(), dst.c_str())))
114
if (!wxFileExists(fname1.GetFullPath())) return false;
115
return wxCopyFile(fname1.GetFullPath(),
116
fname2.GetFullPath(),
120
bool RenameFile(const wxString& src, const wxString& dst)
122
wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
123
wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
124
NormalizePath(fname1, wxEmptyString);
125
NormalizePath(fname2, wxEmptyString);
126
if (!SecurityAllows(_T("RenameFile"), wxString::Format(_T("%s -> %s"),
127
fname1.GetFullPath().c_str(), fname2.GetFullPath().c_str())))
129
if (!wxFileExists(fname1.GetFullPath())) return false;
130
return wxRenameFile(fname1.GetFullPath(),
131
fname2.GetFullPath());
134
bool RemoveFile(const wxString& src)
136
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
137
NormalizePath(fname, wxEmptyString);
138
if (!SecurityAllows(_T("RemoveFile"), fname.GetFullPath()))
140
if (!wxFileExists(fname.GetFullPath())) return false;
141
return wxRemoveFile(fname.GetFullPath());
144
bool FileExists(const wxString& file)
146
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(file));
147
NormalizePath(fname, wxEmptyString);
148
return wxFileExists(fname.GetFullPath());
151
wxString ChooseFile(const wxString& title, const wxString& defaultFile, const wxString& filter)
156
Manager::Get()->GetMacrosManager()->ReplaceMacros(defaultFile),
158
wxOPEN | compatibility::wxHideReadonly);
160
if (dlg.ShowModal() == wxID_OK)
161
return dlg.GetPath();
162
return wxEmptyString;
165
wxString ReadFileContents(const wxString& filename)
167
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
168
NormalizePath(fname, wxEmptyString);
169
wxFile f(fname.GetFullPath());
170
return cbReadFileContents(f);
173
bool WriteFileContents(const wxString& filename, const wxString& contents)
175
wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
176
NormalizePath(fname, wxEmptyString);
177
if (!SecurityAllows(_T("CreateFile"), fname.GetFullPath()))
179
wxFile f(fname.GetFullPath(), wxFile::write);
180
return cbWrite(f, contents);
183
int Execute(const wxString& command)
185
if (!SecurityAllows(_T("Execute"), command))
187
wxArrayString output;
188
return wxExecute(command, output, wxEXEC_NODISABLE);
191
wxString ExecuteAndGetOutput(const wxString& command)
193
if (!SecurityAllows(_T("Execute"), command))
194
return wxEmptyString;
195
wxArrayString output;
196
wxExecute(command, output, wxEXEC_NODISABLE);
197
return GetStringFromArray(output, _T("\n"));
201
} // namespace ScriptBindings
203
namespace ScriptBindings
205
struct IONamespace {};
209
SqPlus::SQClassDef<IONamespace>("IO").
211
#ifndef NO_INSECURE_SCRIPTS
212
staticFunc(&IOLib::CreateDirRecursively, "CreateDirectory").
213
staticFunc(&IOLib::RemoveDir, "RemoveDirectory").
214
staticFunc(&IOLib::CopyFile, "CopyFile").
215
staticFunc(&IOLib::RenameFile, "RenameFile").
216
staticFunc(&IOLib::RemoveFile, "RemoveFile").
217
staticFunc(&IOLib::WriteFileContents, "WriteFileContents").
218
staticFunc(&IOLib::Execute, "Execute").
219
staticFunc(&IOLib::ExecuteAndGetOutput, "ExecuteAndGetOutput").
220
#endif // NO_INSECURE_SCRIPTS
222
staticFunc(&IOLib::GetCwd, "GetCwd").
223
staticFunc(&IOLib::SetCwd, "SetCwd").
225
staticFunc(&IOLib::DirectoryExists, "DirectoryExists").
226
staticFunc(&IOLib::ChooseDir, "SelectDirectory").
227
staticFunc(&IOLib::FileExists, "FileExists").
228
staticFunc(&IOLib::ChooseFile, "SelectFile").
229
staticFunc(&IOLib::ReadFileContents, "ReadFileContents");
231
#ifndef NO_INSECURE_SCRIPTS
232
SqPlus::BindConstant(true, "allowInsecureScripts");
234
SqPlus::BindConstant(false, "allowInsecureScripts");
235
#endif // NO_INSECURE_SCRIPTS
237
} // namespace ScriptBindings