1
0
Fork 0
ArkiwiAPI/arav_up_api/index.php
2016-09-06 15:11:59 +02:00

125 Zeilen
3,5 KiB
PHP

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Max-Age: 86400');
// includes
$includePath = array();
$includePath[] = '../arav_up_inclu';
$includePath[] = get_include_path();
$includePath = implode(PATH_SEPARATOR,$includePath);
set_include_path($includePath);
// carica configurazione
require '../arav_up_confs.php';
require_once('auth.php');
require_once('report.php');
// presenta auth
if (!isset($_SERVER['PHP_AUTH_USER'])) {
prompt_auth();
// report(1,"PHP_AUTH_USER not set, exiting"); //Lo vogliamo davvero loggare?
exit;
}
$authstate = do_local_auth($_SERVER['PHP_AUTH_USER'], hash("sha256",$_SERVER['PHP_AUTH_PW']));
// report(1,"userid : ".$authstate["userid"]);
// report(1,"dir : ".$authstate["dir"]);
if ($authstate["esito"] != "AUTH_OK") {
// // es: is_inside_dir($object, $authstate["dir"]);
prompt_auth();
// report(1,"esito not AUTH_OK, exiting");
exit;
}
else {
// registra in sessione i dati dell'utente //FIXME lo facciamo qui o dentro la funzione do_local_auth?
$_SESSION['authstate'] = $authstate;
}
// -----------------
// I am not afraid of errors:
error_reporting(E_ALL);
// Parse the request, and run:
$request = parseRequest();
$request->run();
// Print the headers and the body:
$request->writeHeader();
$request->writeBody();
// This method parses the incoming request:
function parseRequest() {
$request = array('method' => 'GET', 'path' => null, 'extra' => Array());
$request['method'] = $_SERVER['REQUEST_METHOD'];
//Parsing query string:
parse_str($_SERVER['QUERY_STRING'], $request['queryString']);
//Processing the path:
if(($pos = strpos($_SERVER['REQUEST_URI'], '?')) !== false)
$path = substr($_SERVER['REQUEST_URI'], 0, $pos);
else
$path = $_SERVER['REQUEST_URI'];
if(strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) {
$dirname = dirname($_SERVER['SCRIPT_NAME']);
$path = substr($path, strlen($dirname) + 1);
}
//Remove empty values from split $path array
$split = array_filter(explode('/', $path));
$request['path'] = isset($split[0]) ? $split[0] : null;
$request['extra'] = array_slice($split, 1);
$requestHandler = null;
// Looking for the best function for this request:
switch ($request['path']) {
case 'upload':
require_once './requests/requestPostUpload.php';
$requestHandler = new arkiwiRequestPostUpload($request);
break;
case 'listmetadata':
require_once './requests/requestGetListMetadata.php';
$requestHandler = new arkiwiRequestGetListMetadata($request);
break;
case 'modifymetadata':
require_once './requests/requestPostModifyMetadata.php';
$requestHandler = new arkiwiRequestPostModifyMetadata($request);
break;
case 'createdirectory':
require_once './requests/requestPostCreateDirectory.php';
$requestHandler = new arkiwiRequestPostCreateDirectory($request);
break;
/* case 'removemetadata':
require_once './requests/requestPostRemoveMetadata.php';
$requestHandler = new arkiwiRequestPostRemoveMetadata($request);
break;*/
default:
require_once './requests/requestError.php';
$requestHandler = new arkiwiRequestError($request);
break;
}
return $requestHandler;
}