<?php
session_start();

if (!isset($PathPrefix)) {
	$PathPrefix='../../';
}

require_once($PathPrefix . 'config.php');
$_SESSION['DatabaseName'] = $DefaultDatabase;
require_once($PathPrefix . 'includes/ConnectDB_' . $DBType . '.inc');

require '../includes/lms_functions.php';

// define HTTP routes
$getrouter = array(
    "/contact/:id" => "DisplayContact",
    "/:page/:id/:key" => "CaptureRedirect"
);

$postrouter = array(
    "/contact/:id" => "GetContact"
);

$method = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['REQUEST_URI'];
$requestpath = substr($request, strpos($request, basename(__DIR__)) + strlen(basename(__DIR__)) + 1);
$valid_request = false;

switch($method)
{
    case "GET":
        foreach($getrouter as $url => $function)
        {
            $route = explode(":", $url, 2);
            $path = ltrim($route[0], "/");
            $params = "";
            $params_count = 0;

            if(!empty($route[1]))
            {
                $params = $route[1];
                $params_count = 1 + substr_count($params, ":");
            }

            // get the request params count
            $request_params_count = 0;
            $buffer = substr($requestpath, strlen($path));
            if(strlen($buffer) > 0)
            {
                $request_params_count = 1 + substr_count($buffer, "/");
            }

            // validate path and params count
            if( (empty($path) || strpos($requestpath, $path) !== false) && $request_params_count == $params_count)
            {
                if(function_exists($function))
                {
                    if($request_params_count > 0)
                    {
                        $args = getIncomingParams($params, str_replace($path, '', $requestpath));
                        // print_r($args);
                    }
                    $valid_request = true;
                }

                break;
            }

        }
    break;

    case "POST":
        foreach($postrouter as $url => $function)
        {
            $route = explode(":", $url, 2);
            $path = ltrim($route[0], "/");
            $params = "";
            $params_count = 0;

            if(!empty($route[1]))
            {
                $params = $route[1];
                $params_count = 1 + substr_count($params, ":");
            }

            // get the request params count
            $request_params_count = 0;
            $buffer = substr($requestpath, strlen($path));
            if(strlen($buffer) > 0)
            {
                $request_params_count = 1 + substr_count($buffer, "/");
            }

            // validate path and params count
            if(strpos($requestpath, $path) !== false && $request_params_count == $params_count)
            {
                if(function_exists($function))
                {
                    if($request_params_count > 0)
                    {
                        $args = getIncomingParams($params, str_replace($path, '', $requestpath));
                        $args = array_merge($args, getPostParams());
                    }
                    $valid_request = true;
                }

                break;
            }
        }
    break;
    default:
}

if($valid_request)
{
    switch($function)
    {
        case "CaptureRedirect":
            if(!empty($args['page']) && !empty($args['id']) && !empty($args['key']))
            {
                $function($args['page'], $args['id'], $args['key']);
            }
        break;

        case "DisplayContact":
            if(!empty($args['id']))
            {
                $function($args['id']);
            }
        break;

        case "GetContact":
            if(!empty($args['id']) && !empty($args['vcard']))
            {
                $function($args['id']);
            }
        break;

        default:
            header('HTTP/1.1 404 Not Found');
            exit();
    }
}
else
{
    header('HTTP/1.1 404 Not Found');
    exit();
}

function getIncomingParams($params, $elements)
{
    $parameters = array();
    $param = explode("/:", $params);
    $element = explode("/", $elements);

    if(count($param) == count($element))
    {
        while(count($param))
        {
            $parameters[array_shift($param)] = array_shift($element)                                                                                                                                                              ;
        }
    }

    return $parameters;
}

function getPostParams()
{
    $parameters = array();
    foreach ($_POST as $key => $value){
        $parameters[$key] = $value;
    }

    return $parameters;
}
?>