forked from Sleepcap/vDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.php
343 lines (297 loc) · 13.6 KB
/
files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
// For security reasons fileupload and all variables are ussually discarded in header.php. Save this in constants.
$uploadname=(isset($_FILES['upload']['name'])) ? $_FILES['upload']['name'] : '' ; // The uploaded filename
$uploadtmp =(isset($_FILES['upload']['tmp_name']))? $_FILES['upload']['tmp_name'] : '' ; // the tmp-filename from PHP
define('UP',$uploadname);
define('UPTMP',$uploadtmp);
require_once('header.php');
// Put the values back...
$uploadtmp = UPTMP;
// The Variant-ID for the map
$variantID = isset($_REQUEST['variantID']) ? $_REQUEST['variantID'] : '0';
if (!(isset(Config::$variants[$variantID]))) $variantID = 0;
// What to do
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '' ;
// Users can only access these 3 directories.
$basedir = isset($_REQUEST['basedir']) ? $_REQUEST['basedir'] : '/';
switch($basedir) {
case '/classes/' : $basedir = '/classes/'; break;
case '/resources/' : $basedir = '/resources/'; break;
case '/interactiveMap/' : $basedir = '/interactiveMap/';break;
default : $basedir = '/';
}
// The filename (only letters, numbers and "." or "-" allowed...
$file = isset($_REQUEST['file']) ? $_REQUEST['file'] : UP;
$file = preg_replace('/[^A-z0-9 \(\)\-\.]/i', '', basename($file));
// Filled with the new content after editing
$updatedfile = isset($_REQUEST['updatedfile'])? $_REQUEST['updatedfile']: '' ;
// a message to diaplay.
$msg = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : '' ;
switch($msg) {
case '1' : $msg = 'Failed to save '.$basedir.$file.' !'; break;
case '2' : $msg = 'File '.$basedir.$file.' saved.'; break;
case '3' : $msg = 'File '.$basedir.$file.' uploaded.'; break;
case '4' : $msg = 'File '.$basedir.$file.' deleted.'; break;
case '5' : $msg = 'File '.$basedir.$file.' verified.'; break;
case '6' : $msg = 'Editing canceled. File '.$basedir.$file.' not saved!'; break;
default : $msg = '';
}
// You can't edit or upload a modified install.php. It causes all sort of strange problems.
if (($action == 'upload' || $action == 'filesave') && $file=='install.php')
{
$msg = 'Editing or uploading of a modified install.php is for experts only.<br>Please contakt an admin if you leally need to do this.';
$action = ''; $file='';
}
// You can't edit or upload a modified install.php. It causes all sort of strange problems.
if (($action == 'upload' || $action == 'filesave') && $file=='variant.php' && !file_exists("variants/".Config::$variants[$variantID]."/install.php"))
{
$msg = "You can't edit or upload variants.php while in edit-mode.";
$action = ''; $file='';
}
/*
* Now for the different possible actions:
*/
/*
* View a file
*/
if ($action == 'view' && $variantID != 0) {
$filename = "variants/" . Config::$variants[$variantID] . $basedir.$file;
if (file_exists($filename))
{
header("Content-type: text/plain; charset=utf-8");
header("Content-disposition: inline; filename=".$file);
readfile($filename);
}
exit;
}
/*
* Download the sourcecode in a big zip-file
*/
if ($action == 'download' && $variantID != '0') {
$variant = libVariant::loadFromVariantID($variantID);
$version= (isset($variant->version)?'_V'.$variant->version:'');
$code = (isset($variant->codeVersion)?'_C'.$variant->codeVersion:'');
$filename=$variant->name.str_replace('.','_',$version).str_replace('.','_',$code) . '.zip';
chdir('variants');
if (!file_exists($filename)) {
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE)
exit("cannot open <$filename>\n");
$zip->addEmptyDir($variant->name);
$zip->addEmptyDir($variant->name.'/cache');
$zip->addEmptyDir($variant->name.'/classes');
$zip->addEmptyDir($variant->name.'/resources');
if(file_exists($variant->name.'/interactiveMap')) $zip->addEmptyDir($variant->name.'/interactiveMap');
foreach (glob($variant->name. '/classes/*') as $file) $zip->addFile($file);
foreach (glob($variant->name. '/resources/*') as $file) $zip->addFile($file);
if(file_exists($variant->name.'/interactiveMap')) foreach (glob($variant->name. '/interactiveMap/*') as $file) $zip->addFile($file);
$zip->addFile($variant->name. '/variant.php');
$zip->addFile($variant->name. '/install.php');
if (file_exists($variant->name. '/rules.html'))
$zip->addFile($variant->name. '/rules.html');
$zip->close();
}
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=".basename($filename));
readfile($filename);
exit;
}
/*
* Show the variant-files with some edit/upload/delete possibilities for variant developers
*/
libHTML::starthtml();
print '<div class="content">';
print '<li class="formlisttitle">Variant: ';
print '<form style="display: inline" method="get" name="set_map">';
print '<select name="variantID" onchange="this.form.submit();">';
if ($variantID == 0)
print '<option value="0" selected>Choose a variant...</option>';
asort(Config::$variants);
foreach ( Config::$variants as $id=>$name )
{
if (isset(Config::$devs))
{
if (array_key_exists($User->username, Config::$devs))
{
foreach (Config::$devs[$User->username] as $variantName)
{
if ($name == $variantName)
print '<option value="'.$id.'"'.($id == $variantID ? ' selected':'').'>'.$name.'</option>';
}
}
elseif ($User->id == 5)
{
foreach ( Config::$devs as $dev=>$variants )
{
foreach ($variants as $variantName)
{
if ($name == $variantName)
print '<option value="'.$id.'"'.($id == $variantID ? ' selected':'').'>'.$dev.': '.$name.'</option>';
}
}
}
}
else
{
print '<option value="'.$id.'"'.($id == $variantID ? ' selected':'').'>'.$name.'</option>';
}
}
print '</select></form>'.($variantID == 0 ? '</li>' : '');
if ($variantID != 0)
{
$variantbase = "variants/" . Config::$variants[$variantID];
$edit = false;
if ($User->id == 5) $edit=true;
if (isset(Config::$devs))
if (array_key_exists($User->username, Config::$devs))
if (in_array(Config::$variants[$variantID], Config::$devs[$User->username]))
$edit = true;
print '<form style="display: inline" action="'.$_SERVER['SCRIPT_NAME'].'" method="POST">
<input type="hidden" name="variantID" value="'.$variantID.'" />
<input type="hidden" name="action" value="download" />
<input type="submit" value="Download as zip" /></form>
</li>';
if ($edit)
{
if ($msg != '')
print '<li class="formlisttitle">'.$msg.'</li>';
if (($action == 'edit') && (file_exists ($variantbase.$basedir.$file)))
{
print '
<li class="formlisttitle">Edit: '.$basedir.$file.':
<form style="display: inline" action="'. $_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=6&file='.$file.'&basedir='.$basedir.'" method="post">
<input type="submit" value="Cancel">
</form>
<form style="display: inline" action="'. $_SERVER['SCRIPT_NAME'] .'" method="post">
<input type="submit" value="Save Changes"></li>
<input type="hidden" name="action" value="filesave" />
<input type="hidden" name="variantID" value="' . $variantID . '" />
<input type="hidden" name="basedir" value="'.$basedir.'"/>
<input type="hidden" name="file" value="'.$file.'"/>
<textarea rows="20" style="border: 1px solid #666666; font-family: courier;" name="updatedfile">';
//Open the file chosen in the select box in the previous form into the text area
$file2open = fopen($variantbase.$basedir.$file, "r");
$current_data = @fread($file2open, filesize($variantbase.$basedir.$file));
fclose($file2open);
// Recplace a "</textarea>" tag so it does not break the layout
$current_data = str_ireplace("</textarea>", "<END-TA-DO-NOT-EDIT>", $current_data);
echo $current_data;
print '</textarea></form></div>';
libHTML::footer();
exit;
}
// Delete the global css if a CSS file got edited or upload
if (($action == 'upload' || $action == 'filesave') && !stripos($file, '.css') === false)
foreach (glob(libCache::Dirname('css').'/variants-*.css') as $cssfilename)
unlink($cssfilename);
if (($action == 'filesave') && (file_exists ($variantbase.$basedir.$file)))
{
rename($variantbase.$basedir.$file, $variantbase."/cache/".date("ymd-His")."-edit-".$file);
if (stripos($file, '(wait for verify)') === false)
if (!stripos($file, 'php') === false)
$file .= ' (wait for verify)';
$file2ed = fopen($variantbase.$basedir.$file, "w+");
// Recplace a "</textarea>" tag so it dows not break the layout
$updatedfile = str_ireplace("<END-TA-DO-NOT-EDIT>", "</textarea>", $updatedfile);
//Remove any slashes that may be added do to " ' " s. Thats a single tick, btw.
$updatedfile = stripslashes($updatedfile);
$ok = fwrite($file2ed,$updatedfile);
fclose($file2ed);
if (!$ok)
echo '<script type="text/javascript">top.location.replace("'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=1&file='.$file.'&basedir='.$basedir.'");</script>';
else
echo '<script type="text/javascript">top.location.replace("'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=2&file='.$file.'&basedir='.$basedir.'");</script>';
exit;
}
if (($action == 'delete') && (file_exists ($variantbase.$basedir.$file)))
{
rename($variantbase.$basedir.$file, $variantbase."/cache/".date("ymd-His")."-del-".$file);
echo '<script type="text/javascript">top.location.replace("'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=4&file='.$file.'&basedir='.$basedir.'");</script>';
exit;
}
if ($action == 'upload') {
if (!($file == 'install.php' && file_exists($variantbase.'/cache/install-backup.php')))
{
if ($file != '')
{
if (file_exists ($variantbase.$basedir.$file))
rename($variantbase.$basedir.$file, $variantbase."/cache/".date("ymd-His")."-upl-".$file);
if (file_exists ($variantbase.$basedir.$file.' (wait for verify)'))
rename($variantbase.$basedir.$file.' (wait for verify)', $variantbase."/cache/".date("ymd-His")."-upl-".$file);
if (!stripos($file, 'php') === false)
$file .= ' (wait for verify)';
if($basedir == '/interactiveMap/' && !file_exists($variantbase.$basedir)) mkdir($variantbase.'/interactiveMap', 0755);
rename ($uploadtmp, $variantbase.$basedir.$file);
chmod($variantbase.$basedir.$file, 0644);
}
}
echo '<script type="text/javascript">top.location.replace("'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=3&file='.$file.'&basedir='.$basedir.'");</script>';
exit;
}
if ($action == 'verify' && ($User->id == 5)) {
if (file_exists ($variantbase.$basedir.$file))
{
$newfile = substr($file, 0, -18);
rename($variantbase.$basedir.$file, $variantbase.$basedir.$newfile);
}
echo '<script type="text/javascript">top.location.replace("'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&msg=5&file='.$newfile.'&basedir='.$basedir.'");</script>';
exit;
}
print '
<li class="formlisttitle">
<form enctype="multipart/form-data"
action="'. $_SERVER['SCRIPT_NAME'] .'"
method="POST">
<input type="hidden" name="variantID" value="' . $variantID . '" />
<input type="hidden" name="action" value="upload" />
Upload file: <input type="file" name="upload" /> - directory:
<select name="basedir">
<option value="/" selected> / </option>
<option value="/classes/"> /classes/ </option>
<option value="/resources/"> /resources/ </option>
'./*(file_exists($variantbase.'/interactiveMap')?*/'<option value="/interactiveMap/"> /interactiveMap/</option>'/*:'')*/.
'</select>
<input type="submit" value="Upload File" />
</form>';
}
// print the variant-files in a nice grid
print '<li class="formlisttitle">Variant-Files:';
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
foreach (array("/","/classes/", "/resources/","/interactiveMap/") as $dirname) {
if(!file_exists($variantbase.$dirname)) continue;
print("<TH>" . $dirname . "</TH><TR>\n");
$files = array();
$dir = opendir($variantbase . $dirname);
while (false !== ($file = readdir($dir)))
if (!is_dir($variantbase . $dirname . $file))
$files[] = $file;
closedir($dir);
sort($files);
foreach ($files as $file) {
// Call the php and html files with a wrapper to display the content...
if (substr($file, -3) == 'php' || substr($file, -4) == 'html' || substr($file, -4) == 'htm')
print('<TD><a href="'.$_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&action=view&file='.$file.'&basedir='.$dirname.'">'.$dirname.$file.'</a></td>');
else
print("<TD><a href=\"$variantbase$dirname$file\">$dirname$file</a></td>");
if ($edit == 'on')
{
// Add a delete and edit button if we have a developer: (But on on install.php, and not on variant.php if we are still in edit-mode.
if ($file != "install.php" && !($file == "variant.php" && !file_exists($variantbase."/install.php")))
{
print('<td><a href="' . $_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&action=delete&file='.$file.'&basedir='.$dirname.'">Delete</a></td>');
print('<td><a href="' . $_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&action=edit&file='.$file.'&basedir='.$dirname.'">Edit</a></td>');
}
}
// Superuser can verify files:
if (($User->id == 5) && substr($file, -7) == "verify)")
print('<td><a href="' . $_SERVER['SCRIPT_NAME'].'?variantID='.$variantID.'&action=verify&file='.$file.'&basedir='.$dirname.'">Verify</a></td>');
print("</TR>\n");
}
}
print("</TABLE>\n");
}
print '</div>';
libHTML::footer();
?>