-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.php
194 lines (182 loc) · 4.23 KB
/
schema.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
<?php
//***************************************
//***********FILE FUNCTIONS**************
//***************************************
function readNBTFile($file)
{
$or = error_reporting(0);
try
{
$nbt = new \Nbt\Service();
$nbt = $nbt -> loadFile($file);
}
catch(Exception $e)
{
$nbt = null;
}
error_reporting($or);
return $nbt;
}
function isSchemaFile($data)
{
if($data == null)
return false;
try
{
if($data->findChildByName("bounds") === false) return false;
if($data->findChildByName("primaryDoorFace") === false) return false;
if($data->findChildByName("primaryDoor") === false) return false;
if($data->findChildByName("storage") === false) return false;
if($data->findChildByName("doors") === false) return false;
if($data->findChildByName("name") === false) return false;
return true;
}
catch(Exception $e)
{
}
return false;
}
function getBounds($data)
{
if($data == null)
return null;
$boundData = $data->findChildByName("bounds")->getValue();
$bounds = array();
$bounds[0] = $boundData[0] + $boundData[2] + 1;
$bounds[1] = $boundData[4] + 1;
$bounds[2] = $boundData[1] + $boundData[3] + 1;
return $bounds;
}
function getSize($data)
{
if($data == null)
return 0;
$storeData = $data->findChildByName("storage")->getChildren();
return count($storeData);
}
//***************************************
//***********DATA FUNCTIONS**************
//***************************************
function isOwner($arr)
{
global $isLoggedIn;
if(!$isLoggedIn) return false;
global $userData;
return $arr["userID"] == $userData["id"];
}
function addVoteData($data)
{
global $mysql;
global $userData;
global $isLoggedIn;
if(!$isLoggedIn)
{
foreach($data as $key=>$value)
$data[$key]["canVote"] = false;
return $data;
}
$uid = $userData["id"];
foreach($data as $key=>$value)
{
if(isAdmin())
$data[$key]["canEdit"] = true;
if(isOwner($value))
{
$data[$key]["canVote"] = false;
$data[$key]["canEdit"] = true;
}
else
{
$s = 0;
$id = $value["id"];
$q = $mysql->fetch_array("SELECT score FROM schematicVotes WHERE userID='$uid' AND schemaID='$id'",MYSQLI_NUM,true);
if(count($q) > 0)
$s = $q[0];
$data[$key]["voteState"]=$s;
$data[$key]["canVote"] = true;
}
}
return $data;
}
function addSquashedUID($data)
{
if($data == null || !is_array($data)) return;
foreach($data as $key=>$value)
{
if(isSet($value["name"]))
$data[$key]["name"] = substr($value["name"],0,-7);
if(isSet($value["userID"]))
{
$id = $value["userID"];
$data[$key]["userIDHex"] = squashUserID($id);
}
if(isSet($value["bounds"]))
{
$d = array();
$cd = explode(" ",$value["bounds"]);
foreach($cd as $k2=>$v2)
{
$cdd = explode(":",$v2);
$d[$cdd[0]]= $cdd[1];
}
$data[$key]["bounds"] = $d;
}
if(isSet($value["fileName"]))
{
$date = date("F d Y H:i:s",filemtime($value["fileName"]));
$url = str_replace("/home/web/schema","http://foxpotato.com/schema",$value["fileName"]);
$data[$key]["fileName"] = $url;
$data[$key]["modified"] = $date;
}
}
return $data;
}
function addUserData($data)
{
foreach($data as $key=>$value)
{
if(isSet($value["userID"]))
{
$udata = getUserData($value["userID"]);
if($udata == null) continue;
$data[$key]["userPic"] = $udata["picture"];
$data[$key]["userLink"] = $udata["link"];
$data[$key]["userName"] = getUsername($udata);
}
}
return $data;
}
function formatDesc($data)
{
foreach($data as $key=>$value)
{
$desc = $value["description"];
$desc = str_replace("\\\\","\\",$desc);
$desc = str_replace(array("\\n","\\r"),"\n",$desc);
$desc = str_replace("\\'","'",$desc);
$data[$key]["description"] = $desc;
}
return $data;
}
function reindex($data)
{
$newData = array();
foreach($data as $key=>$value)
{
$newData[$value["id"]] = $value;
}
return $newData;
}
function getSchemaData($query)
{
global $mysql;
$q = "SELECT * FROM schematics $query";
$d = $mysql -> fetch_array($q, MYSQLI_ASSOC,false);
$d = addSquashedUID($d);
$d = addVoteData($d);
$d = addUserData($d);
$d = formatDesc($d);
$d = reindex($d);
return $d;
}
?>