-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
75 lines (61 loc) · 2.05 KB
/
index.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
<?php
/**
* This script handles all requests made to the server and
* sends them to one router according to the request URI
*/
header("Content-Type: application/json; charset=utf-8");
include("routes/SociosRouter.php");
include("routes/LoginRouter.php");
session_start();
$base_url = getCurrentUri();
$routes = array();
$uriRoutes = explode('/', $base_url);
$request = $_SERVER['REQUEST_METHOD'];
foreach($uriRoutes as $route)
{
if(trim($route) != '')
array_push($routes, $route);
}
if($routes[0]!="ws"){
sendErrorMessage();
return;
}
//Sends each requests accordingly to its router
switch($routes[1]){
case "socio":
if(isset($_SESSION["id"]) || $request == "POST"){
SociosRouter::enrutar($request,$routes);
}else{
sendSessionMessage();
}
break;
case "login":
LoginRouter::enrutar($request,$routes);
break;
default:
sendErrorMessage();
break;
}
function sendErrorMessage(){
echo json_encode(array("success" => false, "m" => "Petición incorrecta"));
}
function sendSessionMessage(){
echo json_encode(array("success" => false, "m" => "No se ha inciado sesión"));
}
/**
* Returns the adress of the current request who called the script.
*
* This is the core method, since the .htaccess files redirects all requests to
* this script, this one parses the requests, removes any whitespaces and query symbols
*
*
* @return string $uri Containing a string representation of adress. E.g /user/1
*/
function getCurrentUri()
{
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
$uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
$uri = '/' . trim($uri, '/');
return $uri;
}