/
GlobalUtils.php
370 lines (340 loc) · 13.4 KB
/
GlobalUtils.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* Bunch of utility functions I don't want to define somewhere else.
*/
/**
* @param $link - URL whose content shall be retrieved
* @return mixed - Site-Content
* Function to get contents of some URL, for example API-Calls.
*/
function cURL($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* @param $json - Riot-API-JSON
* @param array $retArr - If the array shall already contain some default values, specify one here.
* @param bool $onlySprites - Unused
* @return ArrayObject
*/
function parseChampJSON($json, $retArr = array(), $onlySprites = false)
{
$arr = json_decode($json, true);
foreach ($arr["data"] as $value) {
if($onlySprites){
$retArr[$value["key"]] = "http://ddragon.leagueoflegends.com/cdn/5.16.1/img/champion/" . $value["key"].".png";
continue;
}
$retArr[$value["key"]] = new ArrayObject(array("img" => "http://ddragon.leagueoflegends.com/cdn/5.16.1/img/champion/" . $value["key"].".png", "name" => $value["name"], "key" => $value["key"]), ArrayObject::ARRAY_AS_PROPS);
}
$retObj = new ArrayObject($retArr);
$retObj->setFlags(ArrayObject::ARRAY_AS_PROPS);
return $retObj;
}
/**
* UNUSED
* @param $json - Riot-API-JSON
* @param array $retArr - If the array shall already contain some default values, specify one here.
* @param bool $onlySprites - Unused
* @return ArrayObject
*/
function parseSpellJSON($json, $onlySprites = false)
{
$retArr = array();
$arr = json_decode($json, true);
foreach ($arr["data"] as $value) {
if ($onlySprites) {
$retArr[$value["key"]] = "http://ddragon.leagueoflegends.com/cdn/5.16.1/img/spell/" . $value["key"].".png";
continue;
}
$retArr[$value["key"]] = new ArrayObject(array("img" => "http://ddragon.leagueoflegends.com/cdn/5.16.1/img/spell/" . $value["key"].".png", "name" => $value["name"], "key" => $value["key"]), ArrayObject::ARRAY_AS_PROPS);
}
$retObj = new ArrayObject($retArr);
$retObj->setFlags(ArrayObject::ARRAY_AS_PROPS);
return $retObj;
}
/**
* Retrieve all Items and store them in own Database (Reason: Riot-API is so slow, unfortunately...)
* @param $db - Database
*/
function saveItems($db){
global $apiKey;
$arr = json_decode(cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/item?itemListData=gold&api_key=".$apiKey), true);
foreach($arr["data"] as $val){
$stmt = $db->mysqli->prepare("INSERT INTO `Items` (`ID`, `Gold`, `Image`, `Name`, `Description`) VALUES(?, ?, ?, ?, ?);");
$url = "http://lkimg.zamimg.com/images/v2/items/icons/size32x32/" . $val["id"] . ".png";
$stmt->bind_param("iisss", $val["id"], $val["gold"]["total"], $url, $val["name"], $val["description"]);
$stmt->execute();
$stmt->close();
}
}
/**
* Retrieve even more specific Item-Data and store them. See above, "saveItems"
* @param $db - Database
*/
function saveItemData($db){
global $apiKey;
$arr = json_decode(cURL("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/item?itemListData=from,into,tags&api_key=" . $apiKey), true);
foreach($arr["data"] as $val){
if(! empty($val["tags"])){
prettyPrint($val["tags"]);
foreach($val["tags"] as $tag) {
$stmt = $db->mysqli->prepare("INSERT INTO `ItemTags` (`ItemID`, `Tag`) VALUES (?, ?)");
$stmt->bind_param("is", $val["id"], $tag);
$stmt->execute();
$stmt->close();
}
}
if(! empty($val["from"])){
foreach($val["from"] as $from){
$stmt = $db->mysqli->prepare("INSERT INTO `ItemBuildsFrom` (`ItemID`, `BuildsFromID`) VALUES (?, ?)");
$stmt->bind_param("is", $val["id"], $from);
$stmt->execute();
$stmt->close();
}
}
if (!empty($val["into"])) {
foreach ($val["into"] as $into) {
$stmt = $db->mysqli->prepare("INSERT INTO `ItemBuildsInto` (`ItemID`, `BuildsIntoID`) VALUES (?, ?)");
$stmt->bind_param("is", $val["id"], $into);
$stmt->execute();
$stmt->close();
}
}
}
}
/**
* Is an integer in the range of two others?
* @param $integer
* @param $min
* @param $max
* @return bool
*/
function intInRange($integer, $min, $max){
return $integer >= $min && $integer <= $max;
}
/**
* Simplified call in order to let the Templating System transform ArrayObjects to JSON.
* @param $arrobj - PHP ArrayObject
* @return string - JSON
*/
function arrobjtojson($arrobj){
$arr = $arrobj->getArrayCopy();
return json_encode($arr);
}
/**
* Also just a simple call wrapper for the templating system.
* @param $string
* @return mixed
*/
function escapeQuotes($string){
return str_replace("\"", """, $string);
}
/**
* Sorts an array of items first by cost and then alphabetically
* @param $items
* @return ArrayObject
*/
function sortItems($items){
$sort = array();
$arr = $items->getArrayCopy();
foreach ($arr as $k => $v) {
$sort['Gold'][$k] = $v['Gold'];
$sort['Name'][$k] = $v['Name'];
}
array_multisort($sort['Gold'], SORT_ASC, $sort['Name'], SORT_ASC, $arr);
return recursiveArrayObject($arr, ArrayObject::ARRAY_AS_PROPS);
}
/**
* Array-Debugging made easy
* @param $a
*/
function prettyPrint($a)
{
echo '<pre>' . print_r($a, 1) . '</pre>';
}
/**
* Recursively searches for Arrays in an array, in order to convert them to PHP ArrayObjects
* @param $array
* @param int $flags
* @return ArrayObject
*/
function recursiveArrayObject($array, $flags = 0){
foreach($array as $k => $val){
if(is_array($val)) $array[$k] = recursiveArrayObject($val, $flags);
}
return new ArrayObject($array, $flags);
}
/**
* ItemSet-MySQL-Queries give back an array of rows containing some data multiple times. This function "collapses" those arrays, to avoid repetition of data and allow easier "data-extraction".
* @param $mysqlRes
*/
function collapseResultArray($mysqlRes, $buildsInto = true, $buildsFrom = true, $tags = false)
{
$returnArray = array();
foreach ($mysqlRes as $row) {
if (!isset($returnArray[$row['ItemSetID']])) {
$returnArray[$row['ItemSetID']] = array();
$returnArray[$row["ItemSetID"]]["Blocks"] = array();
}
$returnArray[$row['ItemSetID']]["ID"] = $row['ItemSetID'];
$returnArray[$row['ItemSetID']]["OwnerID"] = $row["OwnerID"];
$returnArray[$row['ItemSetID']]["Title"] = $row["Title"];
$returnArray[$row['ItemSetID']]["Type"] = $row["Type"];
$returnArray[$row['ItemSetID']]["Map"] = $row["Map"];
$returnArray[$row['ItemSetID']]["Mode"] = $row["Mode"];
$returnArray[$row['ItemSetID']]["Sortrank"] = $row["Sortrank"];
$returnArray[$row['ItemSetID']]["Champion"] = $row["Champion"];
$returnArray[$row['ItemSetID']]["Rating"] = $row["Rating"];
if (!isset($returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]])) {
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]] = array();
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"] = array();
}
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Name"] = $row["Name"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["recMath"] = $row["recMath"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["minSumLvl"] = $row["minSumLvl"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["maxSumLvl"] = $row["maxSumLvl"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["showIfSumSpell"] = $row["showIfSumSpell"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["hideIfSumSpell"] = $row["hideIfSumSpell"];
if (!isset($returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]])) {
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]] = array();
if ($buildsInto) $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsInto"] = array();
if ($buildsFrom) $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsFrom"] = array();
if ($tags) $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Tags"] = array();
}
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["ItemID"] = $row["ItemID"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["ItemCount"] = $row["ItemCount"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Gold"] = str_pad($row["Gold"], 4, " ", STR_PAD_LEFT);
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Name"] = $row["ItemName"];
//$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Image"] = $row["Image"];
$returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Description"] = $row["Description"];
if ($buildsFrom && !empty($row["BuildsFromID"]) && !in_array($row["BuildsFromID"], $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsFrom"]))
array_push($returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsFrom"], $row["BuildsFromID"]);
if ($buildsInto && !empty($row["BuildsIntoID"]) && !in_array($row["BuildsIntoID"], $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsInto"]))
array_push($returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["BuildsInto"], $row["BuildsIntoID"]);
if ($tags && !empty($row["Tag"]) && !in_array($row["Tag"], $returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Tags"]))
array_push($returnArray[$row["ItemSetID"]]["Blocks"][$row["Name"]]["Items"][$row["ItemID"]]["Tags"], $row["Tag"]);
}
return $returnArray;
}
/**
* Creates an ItemSet and adjusts it depending on the user.
* @param $database
* @param $userAuth
* @param $val
* @return ItemSet
*/
function newItemSet($database, $userAuth, $val){
$set = ItemSet::newSet($val);
$set->setEditable($set->ownerID == $userAuth->id || $userAuth->userGroup == UserAuthentification::$ADMIN);
$res = $database->query("SELECT `Rating` FROM `Votes` WHERE `UserID`=" . $userAuth->id . " AND `ItemsetID` = " . $set->ID . ";");
$set->canRate($userAuth->userGroup != UserAuthentification::$GUEST && $set->ownerID != $userAuth->id && empty($res));
$set->canCopy($userAuth->id != UserAuthentification::$GUEST);
return $set;
}
/**
* Riot API Item-Descriptions contain unclosed tags - Fix it. Simple but works.
* @param $html
* @return mixed
*/
function closetag($html)
{
$html_new = $html;
preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result1);
preg_match_all("#</([a-z]+)>#iU", $html, $result2);
$results_start = $result1[1];
$results_end = $result2[1];
foreach ($results_start AS $startag) {
if (!in_array($startag, $results_end) && $startag != "br") {
$html_new = str_replace('<' . $startag . '>', '', $html_new);
}
}
foreach ($results_end AS $endtag) {
if (!in_array($endtag, $results_start) && $endtag != "br") {
$html_new = str_replace('</' . $endtag . '>', '', $html_new);
}
}
return $html_new;
}
/**
* For the smaller purpose. (No blocks, no itemsets: Only Items.)
* @param $mysqlRes
* @param bool $buildsInto
* @param bool $buildsFrom
* @param bool $tags
* @return array
*/
function collapseResultArray2($mysqlRes, $buildsInto = true, $buildsFrom = true, $tags = false)
{
$returnArray = array();
foreach ($mysqlRes as $row) {
if (!isset($returnArray[$row["ItemID"]])) {
$returnArray[$row["ItemID"]] = array();
if ($buildsInto) $returnArray[$row["ItemID"]]["BuildsInto"] = array();
if ($buildsFrom) $returnArray[$row["ItemID"]]["BuildsFrom"] = array();
if ($tags) $returnArray[$row["ItemID"]]["Tags"] = array();
}
$returnArray[$row["ItemID"]]["ItemID"] = $row["ItemID"];
$returnArray[$row["ItemID"]]["Gold"] = str_pad($row["Gold"], 4, " ", STR_PAD_LEFT);
$returnArray[$row["ItemID"]]["Name"] = $row["ItemName"];
$returnArray[$row["ItemID"]]["Description"] = $row["Description"];
if ($buildsFrom && !empty($row["BuildsFromID"]) && !in_array($row["BuildsFromID"], $returnArray[$row["ItemID"]]["BuildsFrom"]))
array_push($returnArray[$row["ItemID"]]["BuildsFrom"], $row["BuildsFromID"]);
if ($buildsInto && !empty($row["BuildsIntoID"]) && !in_array($row["BuildsIntoID"], $returnArray[$row["ItemID"]]["BuildsInto"]))
array_push($returnArray[$row["ItemID"]]["BuildsInto"], $row["BuildsIntoID"]);
if ($tags && !empty($row["Tag"]) && !in_array($row["Tag"], $returnArray[$row["ItemID"]]["Tags"]))
array_push($returnArray[$row["ItemID"]]["Tags"], $row["Tag"]);
}
return $returnArray;
}
/**
* Debug used by templating
* @param $i
* @return mixed
*/
function pp($i){
return $i+1;
}
/**
* Debug used by templating
* @param $i
* @return mixed
*/
function mm($i){
return $i--;
}
/**
* Delete an ItemSet with ID $delete.
* @param $database
* @param $userAuth
* @param $delete
*/
function delSet($database, $userAuth, $delete){
$stmt0 = $database->mysqli->prepare("SELECT i.Sortrank FROM `ItemSets` i JOIN `Users` u ON u.ID=? WHERE i.ID=? AND (i.OwnerID=? OR u.Group = 2);");
$stmt0->bind_param("iii", $userAuth->id, $delete, $userAuth->id);
$stmt0->execute();
$stmt0->store_result();
if ($stmt0->num_rows < 1) die();
$stmt1 = $database->mysqli->prepare("DELETE FROM `ItemSets` WHERE `ID`=?;");
$stmt1->bind_param("i", $delete);
$stmt1->execute();
$stmt1->close();
$stmt2 = $database->mysqli->prepare("DELETE FROM `ItemSetMap` WHERE `ItemSetID`=?");
$stmt2->bind_param("i", $delete);
$stmt2->execute();
$stmt2->close();
}
//Include classes
include_once "DBWrapper.php";
include_once "UserAuthentification.php";
include_once "Template.php";
include_once "ItemSet.php";
include "credentials.php";
?>