-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanageScreenshots.php
246 lines (170 loc) · 7.35 KB
/
manageScreenshots.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
<?php
require_once("/home/uesp/www/esomap/UespMemcachedSession.php");
require_once("viewBuildData.class.php");
require_once("viewCharData.class.php");
class CEsoCharManageScreenshots
{
public $inputParams = array();
public $inputCharacterId = -1;
public $inputBuildId = -1;
public $inputScreenshotId = -1;
public $inputCaption = "";
public $inputAction = "";
public $wikiUserId = 0;
public $wikiUserName = "";
public $errorMsg = "";
public $outputData = array();
public $viewCharData = null;
public $viewBuildData = null;
public $canEditScreenshots = false;
public $canEditScreenshotsCharId = 0;
public $db = null;
public function __construct()
{
UespMemcachedSession::install();
session_name('uesp_net_wiki5_session');
session_start();
$this->viewBuildData = new EsoBuildDataViewer(true, true);
$this->viewCharData = new EsoCharDataViewer(true, true);
}
public function ReportError ($errorMsg)
{
if ($this->outputData['error'] == null) $this->outputData['error'] = array();
$this->outputData['error'][] = $errorMsg;
$this->outputData['result'] = -1;
error_log("ManageEsoCharScreenshots: $errorMsg");
//header("X-PHP-Response-Code: " . 400, true, 400);
return false;
}
public function ReportSuccess ($msg)
{
$this->outputData['result'] = 1;
$this->outputData['msg'] = $msg;
return false;
}
public function WriteHeaders()
{
ob_start("ob_gzhandler");
header("Expires: 0");
header("Pragma: no-cache");
header("Cache-Control: no-cache, no-store, must-revalidate");
//header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN'] . "");
$origin = $_SERVER['HTTP_ORIGIN'];
if (substr($origin, -8) == "uesp.net")
{
header("Access-Control-Allow-Origin: $origin");
}
header("Access-Control-Allow-Credentials: true");
header("content-type: application/json");
}
public function ParseInputParams()
{
$this->inputParams = $_REQUEST;
if (array_key_exists("charid", $this->inputParams)) $this->inputCharacterId = intval($this->inputParams['charid']);
if (array_key_exists("buildid", $this->inputParams)) $this->inputBuildId = intval($this->inputParams['buildid']);
if (array_key_exists("screenshotid", $this->inputParams)) $this->inputScreenshotId = intval($this->inputParams['screenshotid']);
if (array_key_exists("caption", $this->inputParams)) $this->inputCaption = $this->inputParams['caption'];
if (array_key_exists("action", $this->inputParams)) $this->inputAction = $this->inputParams['action'];
}
public function HasEditPermission()
{
global $_SESSION;
//$wikiUser = $_SESSION['wsUserName'];
$this->wikiUserId = UespMemcachedSession::readKey('wsUserID');
$this->wikiUserName = UespMemcachedSession::readKey('wsUserName');
error_log("wsUserName: {$this->wikiUserName}");
//$this->canEditScreenshots = $_SESSION['UESP_ESO_canEditScreenshots'];
$this->canEditScreenshots = UespMemcachedSession::readKey('UESP_ESO_canEditScreenshots');
if ($this->canEditScreenshots === null) $this->canEditScreenshots = 0;
error_log("canEditScreenshots: {$this->canEditScreenshots}");
//$this->canEditScreenshotsCharId = $_SESSION['UESP_ESO_canEditScreenshotsCharId'];
$this->canEditScreenshotsCharId = UespMemcachedSession::readKey('UESP_ESO_canEditScreenshotsCharId');
if ($this->canEditScreenshotsCharId === null) $this->canEditScreenshotsCharId = 0;
error_log("canEditScreenshotsCharId: {$this->canEditScreenshotsCharId}, {$this->inputBuildId}, {$this->inputCharacterId}");
if (!$this->canEditScreenshots) return false;
if ($this->inputBuildId > 0) return $this->canEditScreenshotsCharId == $this->inputBuildId;
if ($this->inputCharacterId > 0) return $this->canEditScreenshotsCharId == $this->inputCharacterId;
return false;
}
public function LoadBuildData()
{
$this->viewBuildData->characterId = $this->inputBuildId;
if (!$this->viewBuildData->initDatabaseWrite()) return $this->ReportError("Failed to initialize database!");
if (!$this->viewBuildData->loadSingleCharacter()) return $this->ReportError("Failed to load build data!");
$this->db = $this->viewBuildData->db;
$screenshotId = $this->inputScreenshotId;
$query = "SELECT * FROM screenshots WHERE id='$screenshotId';";
$result = $this->db->query($query);
if ($result === false) return $this->ReportError("Database error loading screenshot data!");
if ($result->num_rows <= 0) return $this->ReportError("Screenshot data not found!");
$row = $result->fetch_assoc();
if ($row['characterId'] != $this->inputBuildId) return $this->ReportError("Screenshot data does not belond to build!");
return true;
}
public function LoadCharData()
{
$this->viewCharData->characterId = $this->inputCharacterId;
if (!$this->viewCharData->initDatabaseWrite()) return $this->ReportError("Failed to initialize database!");
if (!$this->viewCharData->loadSingleCharacter()) return $this->ReportError("Failed to load character data!");
$this->db = $this->viewCharData->db;
$screenshotId = $this->inputScreenshotId;
$query = "SELECT * FROM screenshots WHERE id='$screenshotId';";
$result = $this->db->query($query);
if ($result === false) return $this->ReportError("Database error loading screenshot data!");
if ($result->num_rows <= 0) return $this->ReportError("Screenshot data not found!");
$row = $result->fetch_assoc();
if ($row['characterId'] != $this->inputCharacterId) return $this->ReportError("Screenshot data does not belong to character!");
return true;
}
public function LoadData()
{
if ($this->inputBuildId > 0)
return $this->LoadBuildData();
elseif ($this->inputCharacterId > 0)
return $this->LoadCharData();
return $this->ReportError("No character or build ID specified!");
}
public function DoEditCaption()
{
if (!$this->LoadData()) return false;
if ($this->inputScreenshotId <= 0) return $this->ReportError("Missing screenshot ID input!");
$caption = $this->db->real_escape_string($this->inputCaption);
$screenshotId = $this->inputScreenshotId;
$query = "UPDATE screenshots SET caption='$caption' WHERE id='$screenshotId';";
$result = $this->db->query($query);
if ($result === false) return $this->ReportError("Failed to save screenshot caption!");
$this->ReportSuccess("Saved caption!");
return true;
}
public function DoDeleteScreenshot()
{
if (!$this->LoadData()) return false;
$screenshotId = $this->inputScreenshotId;
$query = "DELETE FROM screenshots WHERE id='$screenshotId';";
$result = $this->db->query($query);
if ($result === false) return $this->ReportError("Failed to delete screenshot!");
$this->ReportSuccess("Deleted screenshot!");
return true;
}
public function OutputJson()
{
print( json_encode($this->outputData) );
}
public function DoRequest()
{
$this->WriteHeaders();
$this->ParseInputParams();
if (!$this->HasEditPermission())
$this->ReportError("Manage screenshots permission denied! Action = {$this->inputAction}, BuildId = {$this->inputCharacterId}, WikiUser = {$this->wikiUserName}");
elseif ($this->inputAction == "editcaption")
$this->DoEditCaption();
elseif ($this->inputAction == "delete")
$this->DoDeleteScreenshot();
else
$this->ReportError("Unknown action provided!");
$this->OutputJson();
return true;
}
};
$g_ManageScreenshots = new CEsoCharManageScreenshots();
$g_ManageScreenshots->DoRequest();