-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.php
More file actions
190 lines (132 loc) · 3.93 KB
/
editor.php
File metadata and controls
190 lines (132 loc) · 3.93 KB
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
<?php
//error_reporting(E_ALL);
session_start();
$adventure = true;
$user_registry = array ( "username" => "password");
// User must login.
if ( !authenticated ( )) {
require ( 'views/login.inc' );
return;
}
$method = $_SERVER['REQUEST_METHOD'];
$username = $_SESSION['username'];
$list_messages = array();
if ( $method == "GET" ) {
doList();
return;
} elseif ( $method == "POST" ) {
if ( !array_key_exists ( 'action', $_POST )) {
die ( 'Missing required POST parameter: action.' );
}
$action = $_POST['action'];
if ( $action == "login" ) {
doList();
} elseif ( $action == "create" ) {
doCreate();
} elseif ( $action == "edit" ) {
doEdit();
} elseif ( $action == "delete" ) {
doDelete();
} elseif ( $action == "update" ) {
doUpdate();
} elseif ( $action == "cancel" ) {
doList();
} else {
die ( 'Unknown action: $action' );
}
}
function doList() {
global $adventure, $username, $list_messages;
$games = array ();
$userdir = glob ("games/$username");
if ( $userdir ) {
foreach ( glob ("games/$username/*.json") as $file ) {
$games[] = basename ( $file, '.json' );
}
}
require ( 'views/list.inc' );
}
function doCreate() {
global $username, $list_messages;
if ( !array_key_exists ( 'gamename', $_POST )) {
die ( 'Missing required POST parameter: gamename.' );
}
// User cannot specify directory component; use only basename
$gamename = basename ( $_POST['gamename'] );
// User files go into a user directory
$userdir = "games/$username";
if ( !is_dir ( $userdir )) {
mkdir ( $userdir, 0777, true ) or die ( "Cannot create directory $userdir" );
}
$f = $userdir . '/' . $gamename . '.json';
if ( file_exists ( $f )) {
$list_messages['create'] = "Game '$gamename' already exists.";
doList();
} else {
$fh = fopen($f, "w");
fclose ( $fh );
// $template = 'template.json';
// copy ( $template, $f ) or die("Failed to copy $template to $f.");
doEdit();
}
}
function doEdit() {
global $username, $adventure;
if ( !array_key_exists ( 'gamename', $_POST )) {
die ( 'Missing required POST parameter: gamename.' );
}
// User cannot specify directory component; use only basename
$gamename = basename ( $_POST['gamename'] );
$f = "games/$username/$gamename.json";
(is_file ( $f )) or die ( "Cannot find file $f" );
$game_definition = file_get_contents ( $f );
require ( 'views/edit.inc' );
}
function doDelete() {
global $username;
if ( !array_key_exists ( 'gamename', $_POST )) {
die ( 'Missing required POST parameter: gamename.' );
}
// User cannot specify directory component; use only basename
$gamename = basename ( $_POST['gamename'] );
$f = "games/$username/$gamename.json";
unlink ( $f ) or die ( 'Failed to delete $f' );
doList();
}
function doUpdate() {
global $username;
if ( !array_key_exists ( 'gamename', $_POST )) {
die ( 'Missing required POST parameter: gamename.' );
}
// User cannot specify directory component; use only basename
$gamename = basename ( $_POST['gamename'] );
$f = "games/$username/$gamename.json";
array_key_exists ( 'content', $_POST ) or
die ( 'Missing required POST parameter: gamename.' );
$fh = fopen($f, "w");
$content = $_POST['content'];
fwrite ( $fh, stripslashes ( $content ));
fclose ( $fh );
doList();
}
function authenticated ( ) {
global $user_registry;
if ( array_key_exists ( 'username', $_SESSION)) {
return true;
}
if ( !array_key_exists ( 'username', $_POST )
|| !array_key_exists ( 'password', $_POST )) {
return false;
}
$login_username = $_POST['username'];
$login_password = $_POST['password'];
if ( !array_key_exists ( $login_username, $user_registry )) {
return false;
}
$isauthn = ( $user_registry[$login_username] == $login_password );
if ( $isauthn ) {
$_SESSION['username'] = $login_username;
}
return $isauthn;
}
?>