~zodiacsohma/armagetronad/vertrex_website

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
<?php
/*

*************************************************************************

Armagetron Advanced - Alpha Project Website
Copyright (C) 2012-2013  Vijay Kakani (zodiacsohma1@gmail.com)

***************************************************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

***************************************************************************

*/

//  check whether ?url= is set and not empty
if (isset($_REQUEST['url']) && ($_REQUEST['url'] != ""))
{
    $url = $_REQUEST['url'];
    $con = curl_init($url);
    if (!is_null($con))
    {
        //  set curl so output will be stord in variable
        curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
        //  execute the loading of website and 
        $content = trim(curl_exec($con));
        
        //  fetch the response code we get for trying to connect to that file
        $response = curl_getinfo($con, CURLINFO_HTTP_CODE);
        //  check if response code is good == 200
        if ($response == 200)
        {
            //  if good then,
            //  let's parse through the content to make sure it's a map file
            
            //  if content is blank, skip the next code
            if (trim($content) == "") continue;
            
            //  now verify the map file
            if (validate_map('', $content))
            {
                //  close the connection to resource
                curl_close($con);
                
                //  send out the map code
                respond($content, 200, 'text/xml');
            }
        }
    }
    
    //  close the connection to resource
    curl_close($con);
    
    respond('MAP_URL_FAILED', 404);
}
elseif (isset($_REQUEST['resource']) && (isset($_REQUEST['file'])))
{
    $resource = trim($_REQUEST['resource']);
    $map_file = trim($_REQUEST['file']);
    if (($resource != "") && ($map_file != ""))
    {
        $con = curl_init($resource.$map_file);
        if (!is_null($con))
        {
            //  set curl so output will be stord in variable
            curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
            //  execute the loading of website and 
            $content = trim(curl_exec($con));
            
            //  fetch the response code we get for trying to connect to that file
            $response = curl_getinfo($con, CURLINFO_HTTP_CODE);
            //  check if response code is good == 200
            if ($response == 200)
            {
                //  if good then,
                //  let's parse through the content to make sure it's a map file
                
                //  if content is blank, skip the next code
                if (trim($content) == "") continue;
                
                //  now verify the map file
                if (validate_map($map_file, $content))
                {
                    //  close the connection to resource
                    curl_close($con);
                    
                    //  send out the map code
                    respond($content, 200, 'text/xml');
                }
            }
        }
        
        //  close the connection to resource
        curl_close($con);
    }
    
    respond('MAP_URL_FAILED', 404);
}
else respond('MAP_URL_ERROR', 404);

//  respond with messsage and the status of return with the correct content_type
function respond($message, $status, $content_type = 'text/plain')
{    
    header('Status: ' . $status, true, $status);
    header('Content-Type: '.$content_type);

    if($status == 401)
        header('WWW-Authenticate: AA-Hash-md5 realm=' . $_SERVER['SERVER_NAME']);

    die($message . "\n");
}

function endsWith($str, $ending)
{
    if (strlen($ending) > strlen($str))
        return false;

    if (strlen($ending) == 0)
        return false;
    
    $isThis = strtolower(substr($str, strlen($str) - strlen($ending)));
    if ($isThis == strtolower($ending))
        return true;
    
    return false;
}

function validate_map($map_file, $content)
{
    //  map verification code
    $xml = new DOMDocument;
    $xml->loadXML($content);
    
    //  New code (using DTD provided in map file)
    //  Checks the DTD file (if it exists in our server) and then returns true or false for validation
    //  Question: How to get DTD that does not exist in our website?
    
    if ($xml->validate())
    {
        $resources = $xml->getElementsByTagName("Resource");
        if (!is_null($resources))
        {
            foreach($resources as $resource)
            {
                $author         = $resource->getAttribute("author");
                $version        = $resource->getAttribute("version");
                $type           = $resource->getAttribute("type");
                $name           = $resource->getAttribute("name");
                $category       = $resource->getAttribute("category");
    
                if ($author != "" && $version != "" && $type != "" && $name != "")
                {
                    if ($map_file != "")
                    {
                        $mapName = $author.(($category != "")?("/".$category):"")."/".$name."-".$version.".".$type.".xml";
                        if ($map_file != $mapName)
                            return true;
                    }
                }
                
                break;
            }
        }
        else return false;
        
        return true;
    }
    else
        return false;
    
    
    //  Old code (manual validation)
    //  The below code manually checks through things and ensures they all follow
    /*
    $resources = $xml->getElementsByTagName("Resource");
    if (!is_null($resources))
    {
        foreach($resources as $resource)
        {
            $author         = $resource->getAttribute("author");
            $version        = $resource->getAttribute("version");
            $type           = $resource->getAttribute("type");
            $name           = $resource->getAttribute("name");
            $category       = $resource->getAttribute("category");

            if ($author != "" && $version != "" && $type != "" && $name != "")
            {
                $mapName = $name."-".$version.".".$type.".xml";
                if (($mapName != $map_file) && ($map_file != "")) return false;
            }
            
            break;
        }
    }
    else
    {
        //respond('MAP_FILE_NO_RESOURCE_HEADER', 404);
        return false;
    }
    
    $walls = $xml->getElementsByTagName('Wall');
    if (!is_null($walls))
    {
        foreach($walls as $wall)        // load each wall
        {
            $points = $wall->getElementsByTagName('Point');         //get their points
            if (!is_null($points))
            {
                $wPointer = '';
                foreach($points as $point)
                {
                    if (!$point->hasAttribute("x") && !$point->hasAttribute("y"))
                    {
                        //respond('MAP_FILE_WALL_XY_POINTS_MISSING', 404);
                        return false;
                    }
                    elseif (!$point->hasAttribute("x"))
                    {
                        //respond('MAP_FILE_WALL_X_POINT_MISSING', 404);
                        return false;
                    }
                    elseif (!$point->hasAttribute("y"))
                    {
                        //respond('MAP_FILE_WALL_Y_POINT_MISSING', 404);
                        return false;
                    }
                };
            }
            else
            {
                //respond('MAP_FILE_WALL_NO_POINTS', 404);
                return false;
            }
        }
    }
    else
    {
        //respond('MAP_FILE_NO_WALLS', 404);
        return false;
    }
    
    //      let's get data from the zones
    $zones = $dom->getElementsByTagName('Zone');
    if (!is_null($zones))
    {
        foreach($zones as $zone)
        {    
            //      fetch what kind of zone it is
            if (!$zone->hasAttribute('effect'))
                return false;
    
            $shapes = $zone->getElementsByTagName('ShapeCircle');
            if (!is_null($shapes))
            {
                foreach($shapes as $shape)
                {
                    if (!$shape->hasAttribute('radius'))
                        return false;
                    
                    $points = $shape->getElementsByTagName('Point');
                    foreach($points as $point)
                    {
                        if (!$point->hasAttribute('x'))
                            return false;
                        
                        if (!$point->hasAttribute('y'))
                            return false;
                        
                        break;
                    }
                }
            }
            else return false;
        }
    }

    $spawns = $xml->getElementsByTagName('Spawn');
    if (!is_null($spawns))    // if spawns do exist on the map
    {
        foreach($spawns as $spawn)      // load each spawn
        {                                    
            // this will remain constant throughout. Leave the rest of this code as it is!
            if (!$spawn->hasAttribute('x') && !$spawn->hasAttribute('y'))
            {
                //respond('MAP_FILE_SPAWN_XY_POINTS_MISSING', 404);
                return false;
            }
            elseif (!$spawn->hasAttribute('xdir') && !$spawn->hasAttribute('ydir'))
            {                
                //respond('MAP_FILE_SPAWN_DIRECTIONS_MISSING', 404);
                if (!$spawn->hasAttribute('angle'))
                    return false;
            }
            elseif (!$spawn->hasAttribute('x'))
            {
                //respond('MAP_FILE_SPAWN_X_POINT_MISSING', 404);
                return false;
            }
            elseif (!$spawn->hasAttribute('y'))
            {
                //respond('MAP_FILE_SPAWN_Y_POINT_MISSING', 404);
                return false;
            }
            elseif (!$spawn->hasAttribute('xdir'))
            {
                //respond('MAP_FILE_SPAWN_X_DIRECTION_MISSING', 404);
                return false;
            }
            elseif (!$spawn->hasAttribute('ydir'))
            {
                //respond('MAP_FILE_SPAWN_Y_DIRECTION_MISSING', 404);
                return false;
            }
        }
    }
    else
    {
        //respond('MAP_FILE_NO_SPAWNS', 404);
        return false;
    }
    
    return true;
    */
}
?>