mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 12:33:26 +00:00
V 3.5 RC 1
This commit is contained in:
433
lib/classes/module/api/OAuthv2.php
Normal file
433
lib/classes/module/api/OAuthv2.php
Normal file
@@ -0,0 +1,433 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2010 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @package OAuth2 Connector
|
||||
*
|
||||
* @see http://oauth.net/2/
|
||||
* @uses http://code.google.com/p/oauth2-php/
|
||||
*
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
require_once dirname(__FILE__) . "/../../../../lib/bootstrap.php";
|
||||
require_once dirname(__FILE__) . "/../../../../lib/classes/API/OAuth2/Autoloader.class.php";
|
||||
bootstrap::register_autoloads();
|
||||
API_OAuth2_Autoloader::register();
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
return call_user_func(function()
|
||||
{
|
||||
$app = new Silex\Application();
|
||||
|
||||
$app->register(new Silex\Provider\ValidatorServiceProvider(), array(
|
||||
'validator.class_path' => __DIR__ . '/../../../../lib/vendor/symfony/src',
|
||||
));
|
||||
|
||||
|
||||
$app['appbox'] = function()
|
||||
{
|
||||
return appbox::get_instance();
|
||||
};
|
||||
|
||||
$app['supertwig'] = $app->share(function()
|
||||
{
|
||||
$twig = new supertwig();
|
||||
$twig->addFilter(array('prettyDate' => 'phraseadate::getPrettyString'));
|
||||
|
||||
return $twig;
|
||||
});
|
||||
|
||||
$app['oauth'] = function($app)
|
||||
{
|
||||
return new API_OAuth2_Adapter($app['appbox']);
|
||||
};
|
||||
|
||||
|
||||
$app['user'] = function($app)
|
||||
{
|
||||
if ($app['appbox']->get_session()->is_authenticated())
|
||||
{
|
||||
$user = user_adapter::getInstance(
|
||||
$app['appbox']->get_session()->get_usr_id()
|
||||
, $app['appbox']
|
||||
);
|
||||
|
||||
return $user;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Protected Closure
|
||||
* @var Closure
|
||||
* @return Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
$app['response'] = $app->protect(function ($template, $variable) use ($app)
|
||||
{
|
||||
$response = new Response(
|
||||
$app['supertwig']->render($template, $variable)
|
||||
, 200
|
||||
, array('Content-Type' => 'text/html')
|
||||
);
|
||||
$response->setCharset('UTF-8');
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
|
||||
/* * *******************************************************************
|
||||
* AUTHENTIFICATION API
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* AUTHORIZE ENDPOINT
|
||||
*
|
||||
* Authorization endpoint - used to obtain authorization from the
|
||||
* resource owner via user-agent redirection.
|
||||
*/
|
||||
$authorize_func = function() use ($app)
|
||||
{
|
||||
$request = $app['request'];
|
||||
$oauth2_adapter = $app['oauth'];
|
||||
$twig = $app['supertwig'];
|
||||
$session = $app['appbox']->get_session();
|
||||
|
||||
//Check for auth params, send error or redirect if not valid
|
||||
$params = $oauth2_adapter->getAuthorizationRequestParameters($request);
|
||||
|
||||
$authenticated = $session->is_authenticated();
|
||||
$app_authorized = false;
|
||||
$errorMessage = false;
|
||||
|
||||
$client = API_OAuth2_Application::load_from_client_id($app['appbox'], $params['client_id']);
|
||||
|
||||
$oauth2_adapter->setClient($client);
|
||||
|
||||
$action_accept = $request->get("action_accept", null);
|
||||
$action_login = $request->get("action_login", null);
|
||||
|
||||
|
||||
$template = "api/auth/end_user_authorization.twig";
|
||||
$custom_template = $app['appbox']->get_registry()->get('GV_RootPath') . 'config/templates/web/api/auth/end_user_authorization/' . $client->get_id() . '.twig';
|
||||
if (file_exists($custom_template))
|
||||
{
|
||||
$template = 'api/auth/end_user_authorization/' . $client->get_id() . '.twig';
|
||||
}
|
||||
|
||||
if (!$authenticated)
|
||||
{
|
||||
if ($action_login !== null)
|
||||
{
|
||||
try
|
||||
{
|
||||
$login = $request->get("login");
|
||||
$password = $request->get("password");
|
||||
$auth = new Session_Authentication_Native($app['appbox'], $login, $password);
|
||||
$session->authenticate($auth);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$params = array(
|
||||
"auth" => $oauth2_adapter
|
||||
, "session" => $session
|
||||
, "errorMessage" => true
|
||||
, "user" => $app['user']
|
||||
);
|
||||
$html = $twig->render($template, $params);
|
||||
|
||||
return new Response($html, 200, array("content-type" => "text/html"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$params = array(
|
||||
"auth" => $oauth2_adapter
|
||||
, "session" => $session
|
||||
, "errorMessage" => $errorMessage
|
||||
, "user" => $app['user']
|
||||
);
|
||||
$html = $twig->render($template, $params);
|
||||
|
||||
return new Response($html, 200, array("content-type" => "text/html"));
|
||||
}
|
||||
}
|
||||
|
||||
//check if current client is alreadu authorized by current user
|
||||
$user_auth_clients = API_OAuth2_Application::load_authorized_app_by_user($app['appbox'], $app['user']);
|
||||
|
||||
foreach ($user_auth_clients as $auth_client)
|
||||
{
|
||||
if ($client->get_client_id() == $auth_client->get_client_id())
|
||||
$app_authorized = true;
|
||||
}
|
||||
|
||||
$account = $oauth2_adapter->updateAccount($session->get_usr_id());
|
||||
$params['account_id'] = $account->get_id();
|
||||
|
||||
if (!$app_authorized && $action_accept === null)
|
||||
{
|
||||
$params = array(
|
||||
"auth" => $oauth2_adapter
|
||||
, "session" => $session
|
||||
, "errorMessage" => $errorMessage
|
||||
, "user" => $app['user']
|
||||
);
|
||||
|
||||
$html = $twig->render($template, $params);
|
||||
|
||||
return new Response($html, 200, array("content-type" => "text/html"));
|
||||
}
|
||||
elseif (!$app_authorized && $action_accept !== null)
|
||||
{
|
||||
$app_authorized = !!$action_accept;
|
||||
$account->set_revoked(!$app_authorized);
|
||||
}
|
||||
|
||||
//if native app show template
|
||||
if ($oauth2_adapter->isNativeApp($params['redirect_uri']))
|
||||
{
|
||||
$params = $oauth2_adapter->finishNativeClientAuthorization($app_authorized, $params);
|
||||
$html = $twig->render("api/auth/native_app_access_token.twig", $params);
|
||||
|
||||
return new Response($html, 200, array("content-type" => "text/html"));
|
||||
}
|
||||
else
|
||||
{
|
||||
$oauth2_adapter->finishClientAuthorization($app_authorized, $params);
|
||||
}
|
||||
};
|
||||
|
||||
$route = '/authorize';
|
||||
$app->get($route, $authorize_func);
|
||||
$app->post($route, $authorize_func);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* TOKEN ENDPOINT
|
||||
* Token endpoint - used to exchange an authorization grant for an access token.
|
||||
*/
|
||||
$route = '/token';
|
||||
$app->post($route, function() use ($app)
|
||||
{
|
||||
$app['oauth']->grantAccessToken();
|
||||
ob_flush();
|
||||
flush();
|
||||
|
||||
return;
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* MANAGEMENT APPS
|
||||
*
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* list of all authorized apps by logged user
|
||||
*/
|
||||
$route = '/applications';
|
||||
$app->get($route, function() use ($app)
|
||||
{
|
||||
$apps = API_OAuth2_Application::load_app_by_user($app['appbox'], $app['user']);
|
||||
|
||||
return $app['response']('api/auth/applications.twig', array("apps" => $apps, 'user' => $app['user']));
|
||||
});
|
||||
|
||||
/**
|
||||
* list of apps created by user
|
||||
*/
|
||||
$route = "/applications/dev";
|
||||
$app->get($route, function() use ($app)
|
||||
{
|
||||
$rs = API_OAuth2_Application::load_dev_app_by_user($app['appbox'], $app['user']);
|
||||
|
||||
return $app['response']('api/auth/application_dev.twig', array("apps" => $rs));
|
||||
});
|
||||
|
||||
/**
|
||||
* display a new app form
|
||||
*/
|
||||
$route = "/applications/dev/new";
|
||||
$app->get($route, function() use ($app)
|
||||
{
|
||||
$var = array("violations" => null);
|
||||
|
||||
return $app['response']('api/auth/application_dev_new.twig', $var);
|
||||
});
|
||||
|
||||
|
||||
|
||||
$route = "/applications/dev/create";
|
||||
$app->post($route, function() use ($app)
|
||||
{
|
||||
$submit = false;
|
||||
$post = new API_OAuth2_Form_DevApp($app['request']);
|
||||
$violations = $app['validator']->validate($post);
|
||||
|
||||
if ($violations->count() == 0)
|
||||
$submit = true;
|
||||
|
||||
$request = $app['request'];
|
||||
|
||||
if ($submit)
|
||||
{
|
||||
$application = API_OAuth2_Application::create($app['appbox'], $app['user'], $request->get('name'));
|
||||
$application->set_description($request->get('description'))
|
||||
->set_redirect_uri($request->get('callback'))
|
||||
->set_type($request->get('type'))
|
||||
->set_website($request->get('website'));
|
||||
|
||||
return $app->redirect("/api/oauthv2/applications/dev/" . $application->get_id() . "/show");
|
||||
}
|
||||
|
||||
$var = array(
|
||||
"violations" => $violations,
|
||||
"form" => $post
|
||||
);
|
||||
|
||||
return $app['response']('api/auth/application_dev_new.twig', $var);
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* show details of app identified by its id
|
||||
*/
|
||||
$route = "/applications/dev/{id}/show";
|
||||
$app->get($route, function($id) use ($app)
|
||||
{
|
||||
$client = new API_OAuth2_Application($app['appbox'], $id);
|
||||
$token = $client->get_user_account($app['user'])->get_token()->get_value();
|
||||
$var = array("app" => $client, "user" => $app['user'], "token" => $token);
|
||||
|
||||
return $app['response']('api/auth/application_dev_show.twig', $var);
|
||||
});
|
||||
|
||||
/**
|
||||
* revoke access from a user to the app
|
||||
* identified by account id
|
||||
*/
|
||||
$route = "/applications/revoke_access/";
|
||||
$app->post($route, function() use ($app)
|
||||
{
|
||||
$result = array("ok" => false);
|
||||
try
|
||||
{
|
||||
$account = new API_OAuth2_Account($app['appbox'], $app['request']->get('account_id'));
|
||||
$account->set_revoked((bool) $app['request']->get('revoke'));
|
||||
$result['ok'] = true;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return new Response(json_encode($result), 200, array("content-type" => "application/json"));
|
||||
});
|
||||
|
||||
$route = "/applications/{id}/generate_access_token/";
|
||||
$app->post($route, function($id) use ($app)
|
||||
{
|
||||
$result = array("ok" => false);
|
||||
try
|
||||
{
|
||||
$client = new API_OAuth2_Application($app['appbox'], $id);
|
||||
$account = $client->get_user_account($app['user']);
|
||||
|
||||
$token = $account->get_token();
|
||||
|
||||
if ($token instanceof API_OAuth2_Token)
|
||||
$token->renew();
|
||||
else
|
||||
$token = API_OAuth2_Token::create($app['appbox'], $account);
|
||||
|
||||
$result = array(
|
||||
"ok" => true
|
||||
, 'token' => $token->get_value()
|
||||
);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return new response(json_encode($result), 200, array("content-type" => "application/json"));
|
||||
});
|
||||
|
||||
$route = "/applications/oauth_callback";
|
||||
$app->post($route, function() use ($app)
|
||||
{
|
||||
$app_id = $app['request']->request->get("app_id");
|
||||
$app_callback = $app["request"]->request->get("callback");
|
||||
$result = array("success" => false);
|
||||
try
|
||||
{
|
||||
$client = new API_OAuth2_Application($app['appbox'], $app_id);
|
||||
$client->set_redirect_uri($app_callback);
|
||||
$result['success'] = true;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return new Response(json_encode($result), 200, array("content-type" => "application/json"));
|
||||
});
|
||||
|
||||
$route = "/applications/{id}";
|
||||
$app->delete($route, function($id) use ($app)
|
||||
{
|
||||
$result = array("success" => false);
|
||||
try
|
||||
{
|
||||
$client = new API_OAuth2_Application($app['appbox'], $id);
|
||||
$client->delete();
|
||||
$result['success'] = true;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return new Response(json_encode($result), 200, array("content-type" => "application/json"));
|
||||
});
|
||||
/**
|
||||
* *******************************************************************
|
||||
*
|
||||
* Route Errors
|
||||
*
|
||||
*/
|
||||
$app->error(function (Exception $e) use ($app)
|
||||
{
|
||||
if ($e instanceof NotFoundHttpException || $e instanceof Exception_NotFound)
|
||||
{
|
||||
return new Response('The requested page could not be found.', 404);
|
||||
}
|
||||
|
||||
$code = $e instanceof HttpExceptionInterface ? $e->getStatusCode() : 500;
|
||||
|
||||
return new Response('We are sorry, but something went terribly wrong.<br />' . $e->getMessage(), $code);
|
||||
});
|
||||
|
||||
|
||||
return $app;
|
||||
});
|
748
lib/classes/module/api/V1.php
Normal file
748
lib/classes/module/api/V1.php
Normal file
@@ -0,0 +1,748 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2010 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package APIv1
|
||||
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
|
||||
* @link www.phraseanet.com
|
||||
*/
|
||||
/**
|
||||
* Application Routage for API v1
|
||||
*/
|
||||
require_once dirname(__FILE__) . "/../../../../lib/classes/API/OAuth2/Autoloader.class.php";
|
||||
require_once dirname(__FILE__) . "/../../../../lib/bootstrap.php";
|
||||
|
||||
bootstrap::register_autoloads();
|
||||
API_OAuth2_Autoloader::register();
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception;
|
||||
|
||||
return call_user_func(function(){
|
||||
|
||||
|
||||
$app = new Silex\Application();
|
||||
|
||||
$app["appbox"] = appbox::get_instance();
|
||||
|
||||
/**
|
||||
* Associated user to related token
|
||||
* @var User_Adapter
|
||||
*/
|
||||
$app['p4user'] = null;
|
||||
|
||||
/**
|
||||
* @var API_OAuth2_Token
|
||||
*/
|
||||
$app['token'] = null;
|
||||
|
||||
/**
|
||||
* Protected Closure
|
||||
* @var Closure
|
||||
* @return Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
$app['response'] = $app->protect(function ($result)
|
||||
{
|
||||
$response = new Response(
|
||||
$result->format()
|
||||
, $result->get_http_code()
|
||||
, array('Content-Type' => $result->get_content_type())
|
||||
);
|
||||
$response->setCharset('UTF-8');
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
/**
|
||||
* Api Service
|
||||
* @var Closure
|
||||
*/
|
||||
$app['api'] = function () use ($app)
|
||||
{
|
||||
return new API_V1_adapter(false, $app["appbox"]);
|
||||
};
|
||||
|
||||
|
||||
|
||||
$parseRoute = function ($route, Response $response)
|
||||
{
|
||||
$ressource = $general = $aspect = $action = null;
|
||||
$exploded_route = explode('/', p4string::delFirstSlash((p4string::delEndSlash($route))));
|
||||
if (sizeof($exploded_route) > 0 && $response->isOk())
|
||||
{
|
||||
$ressource = $exploded_route[0];
|
||||
|
||||
if (sizeof($exploded_route) == 2 && (int) $exploded_route[1] == 0)
|
||||
{
|
||||
$general = $exploded_route[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
switch ($ressource)
|
||||
{
|
||||
case API_V1_Log::DATABOXES_RESSOURCE :
|
||||
if ((int) $exploded_route[1] > 0 && sizeof($exploded_route) == 3)
|
||||
$aspect = $exploded_route[2];
|
||||
break;
|
||||
case API_V1_Log::RECORDS_RESSOURCE :
|
||||
if ((int) $exploded_route[1] > 0 && sizeof($exploded_route) == 4)
|
||||
{
|
||||
if (!isset($exploded_route[3]))
|
||||
$aspect = "record";
|
||||
elseif (preg_match("/^set/", $exploded_route[3]))
|
||||
$action = $exploded_route[3];
|
||||
else
|
||||
$aspect = $exploded_route[3];
|
||||
}
|
||||
break;
|
||||
case API_V1_Log::BASKETS_RESSOURCE :
|
||||
if ((int) $exploded_route[1] > 0 && sizeof($exploded_route) == 3)
|
||||
{
|
||||
if (preg_match("/^set/", $exploded_route[2]) || preg_match("/^delete/", $exploded_route[2]))
|
||||
$action = $exploded_route[2];
|
||||
else
|
||||
$aspect = $exploded_route[2];
|
||||
}
|
||||
break;
|
||||
case API_V1_Log::FEEDS_RESSOURCE :
|
||||
if ((int) $exploded_route[1] > 0 && sizeof($exploded_route) == 3)
|
||||
$aspect = $exploded_route[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array('ressource' => $ressource, 'general' => $general, 'aspect' => $aspect, 'action' => $action);
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* oAuth verification process
|
||||
*/
|
||||
$app->before(function($request) use ($app)
|
||||
{
|
||||
$session = $app["appbox"]->get_session();
|
||||
$oauth2_adapter = new API_OAuth2_Adapter($app["appbox"]);
|
||||
$oauth2_adapter->verifyAccessToken();
|
||||
|
||||
$app['p4user'] = User_Adapter::getInstance($oauth2_adapter->get_usr_id(), $app["appbox"]);
|
||||
$app['token'] = API_OAuth2_Token::load_by_oauth_token($app["appbox"], $oauth2_adapter->getToken());
|
||||
|
||||
if ($session->is_authenticated())
|
||||
return;
|
||||
if ($oauth2_adapter->has_ses_id())
|
||||
{
|
||||
try
|
||||
{
|
||||
$session->restore($app['p4user'], $oauth2_adapter->get_ses_id());
|
||||
|
||||
return;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
$auth = new Session_Authentication_None($app['p4user']);
|
||||
$session->authenticate($auth);
|
||||
$oauth2_adapter->remember_this_ses_id($session->get_ses_id());
|
||||
|
||||
return;
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* oAUth log process
|
||||
*/
|
||||
$app->after(function (Request $request, Response $response) use ($app, $parseRoute)
|
||||
{
|
||||
$account = $app['token']->get_account();
|
||||
$pathInfo = $request->getPathInfo();
|
||||
$route = $parseRoute($pathInfo, $response);
|
||||
$log = API_V1_Log::create(
|
||||
$app["appbox"],
|
||||
$account,
|
||||
$request->getMethod() . " " . $pathInfo,
|
||||
$response->getStatusCode(),
|
||||
$response->headers->get('content-type'),
|
||||
$route['ressource'],
|
||||
$route['general'],
|
||||
$route['aspect'],
|
||||
$route['action']);
|
||||
});
|
||||
|
||||
/**
|
||||
* Method Not Allowed Closure
|
||||
*/
|
||||
$bad_request_exception = function()
|
||||
{
|
||||
throw new API_V1_exception_badrequest();
|
||||
};
|
||||
|
||||
/**
|
||||
* *******************************************************************
|
||||
* Route : /databoxes/list/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
*
|
||||
*/
|
||||
$route = '/databoxes/list/';
|
||||
$app->get(
|
||||
$route, function() use ($app)
|
||||
{
|
||||
return $app['response']($app['api']->get_databoxes($app['request']));
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* *******************************************************************
|
||||
*
|
||||
* Route /databoxes/DATABOX_ID/collections/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters ;
|
||||
* DATABOX_ID : required INT
|
||||
*/
|
||||
$route = '/databoxes/{databox_id}/collections/';
|
||||
$app->get(
|
||||
$route, function($databox_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_databox_collections($app['request'], $databox_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+');
|
||||
|
||||
$app->get('/databoxes/{any_id}/collections/', $bad_request_exception);
|
||||
|
||||
|
||||
/**
|
||||
* *******************************************************************
|
||||
* Route /databoxes/DATABOX_ID/status/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters ;
|
||||
* DATABOX_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/databoxes/{databox_id}/status/';
|
||||
$app->get(
|
||||
$route, function($databox_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_databox_status($app['request'], $databox_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+');
|
||||
|
||||
$app->get('/databoxes/{any_id}/status/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route /databoxes/DATABOX_ID/metadatas/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters ;
|
||||
* DATABOX_ID : required INT
|
||||
*/
|
||||
$route = '/databoxes/{databox_id}/metadatas/';
|
||||
$app->get(
|
||||
$route, function($databox_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_databox_metadatas($app['request'], $databox_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+');
|
||||
|
||||
$app->get('/databoxes/{any_id}/metadatas/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route /databoxes/DATABOX_ID/termsOfUse/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters ;
|
||||
* DATABOX_ID : required INT
|
||||
*/
|
||||
$route = '/databoxes/{databox_id}/termsOfUse/';
|
||||
$app->get(
|
||||
$route, function($databox_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_databox_terms($app['request'], $databox_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+');
|
||||
|
||||
$app->get('/databoxes/{any_id}/termsOfUse/', $bad_request_exception);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Route : /records/search/FORMAT/
|
||||
*
|
||||
* Method : GET or POST
|
||||
*
|
||||
* Parameters :
|
||||
* bases[] : array
|
||||
* status[] : array
|
||||
* fields[] : array
|
||||
* record_type : boolean
|
||||
* media_type : string
|
||||
*
|
||||
* Response :
|
||||
* Array of record objects
|
||||
*
|
||||
*/
|
||||
$route = '/records/search/';
|
||||
$app->post(
|
||||
$route, function() use ($app)
|
||||
{
|
||||
$result = $app['api']->search_records($app['request']);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/metadatas/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/metadatas/';
|
||||
$app->get(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_record_metadatas($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->get('/records/{any_id}/{anyother_id}/metadatas/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/status/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/status/';
|
||||
$app->get(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_record_status($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->get('/records/{any_id}/{anyother_id}/status/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/related/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/related/';
|
||||
$app->get(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_record_related($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->get('/records/{any_id}/{anyother_id}/related/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/embed/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/embed/';
|
||||
$app->get(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_record_embed($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->get('/records/{any_id}/{anyother_id}/embed/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/setmetadatas/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/setmetadatas/';
|
||||
$app->post(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->set_record_metadatas($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->post('/records/{any_id}/{anyother_id}/setmetadatas/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/setstatus/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/setstatus/';
|
||||
$app->post(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->set_record_status($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
|
||||
$app->post('/records/{any_id}/{anyother_id}/setstatus/', $bad_request_exception);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /records/DATABOX_ID/RECORD_ID/setcollection/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
* DATABOX_ID : required INT
|
||||
* RECORD_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/records/{databox_id}/{record_id}/setcollection/';
|
||||
$app->post(
|
||||
$route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->set_record_collection($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
$app->post('/records/{wrong_databox_id}/{wrong_record_id}/setcollection/', $bad_request_exception);
|
||||
|
||||
|
||||
$route = '/records/{databox_id}/{record_id}/';
|
||||
$app->get($route, function($databox_id, $record_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_record($app['request'], $databox_id, $record_id);
|
||||
|
||||
return $app['response']($result);
|
||||
})->assert('databox_id', '\d+')->assert('record_id', '\d+');
|
||||
$app->get('/records/{any_id}/{anyother_id}/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /baskets/list/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/list/';
|
||||
$app->get(
|
||||
$route, function() use ($app)
|
||||
{
|
||||
$result = $app['api']->search_baskets($app['request']);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /baskets/add/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/add/';
|
||||
$app->post(
|
||||
$route, function() use ($app)
|
||||
{
|
||||
$result = $app['api']->create_basket($app['request']);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Route : /baskets/BASKET_ID/content/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* BASKET_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/{basket_id}/content/';
|
||||
$app->get(
|
||||
$route, function($basket_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_basket($app['request'], $basket_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('basket_id', '\d+');
|
||||
$app->get('/baskets/{wrong_basket_id}/content/', $bad_request_exception);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /baskets/BASKET_ID/settitle/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* BASKET_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/{basket_id}/setname/';
|
||||
$app->post(
|
||||
$route, function($basket_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->set_basket_title($app['request'], $basket_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('basket_id', '\d+');
|
||||
$app->post('/baskets/{wrong_basket_id}/setname/', $bad_request_exception);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /baskets/BASKET_ID/setdescription/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
* BASKET_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/{basket_id}/setdescription/';
|
||||
$app->post(
|
||||
$route, function($basket_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->set_basket_description($app['request'], $basket_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('basket_id', '\d+');
|
||||
$app->post('/baskets/{wrong_basket_id}/setdescription/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* Route : /baskets/BASKET_ID/delete/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
* BASKET_ID : required INT
|
||||
*
|
||||
*/
|
||||
$route = '/baskets/{basket_id}/delete/';
|
||||
$app->post(
|
||||
$route, function($basket_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->delete_basket($app['request'], $basket_id);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('basket_id', '\d+');
|
||||
$app->post('/baskets/{wrong_basket_id}/delete/', $bad_request_exception);
|
||||
|
||||
|
||||
/**
|
||||
* Route : /feeds/list/FORMAT/
|
||||
*
|
||||
* Method : POST
|
||||
*
|
||||
* Parameters :
|
||||
*
|
||||
*/
|
||||
// public function search_publications(\Symfony\Component\HttpFoundation\Request $app['request']);
|
||||
|
||||
|
||||
$route = '/feeds/list/';
|
||||
$app->get(
|
||||
$route, function() use ($app)
|
||||
{
|
||||
$result = $app['api']->search_publications($app['request'], $app['p4user']);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Route : /feeds/PUBLICATION_ID/content/FORMAT/
|
||||
*
|
||||
* Method : GET
|
||||
*
|
||||
* Parameters :
|
||||
* PUBLICATION_ID : required INT
|
||||
*
|
||||
*/
|
||||
// public function get_publication(\Symfony\Component\HttpFoundation\Request $app['request'], $publication_id);
|
||||
|
||||
$route = '/feeds/{feed_id}/content/';
|
||||
$app->get(
|
||||
$route, function($feed_id) use ($app)
|
||||
{
|
||||
$result = $app['api']->get_publication($app['request'], $feed_id, $app['p4user']);
|
||||
|
||||
return $app['response']($result);
|
||||
}
|
||||
)->assert('feed_id', '\d+');
|
||||
$app->get('/feeds/{wrong_feed_id}/content/', $bad_request_exception);
|
||||
|
||||
/**
|
||||
* *******************************************************************
|
||||
*
|
||||
* Route Errors
|
||||
*
|
||||
*/
|
||||
$app->error(function (\Exception $e) use ($app)
|
||||
{
|
||||
|
||||
if ($e instanceof API_V1_exception_methodnotallowed)
|
||||
$code = API_V1_result::ERROR_METHODNOTALLOWED;
|
||||
elseif ($e instanceof Exception\MethodNotAllowedHttpException)
|
||||
$code = API_V1_result::ERROR_METHODNOTALLOWED;
|
||||
elseif ($e instanceof API_V1_exception_badrequest)
|
||||
$code = API_V1_result::ERROR_BAD_REQUEST;
|
||||
elseif ($e instanceof API_V1_exception_forbidden)
|
||||
$code = API_V1_result::ERROR_FORBIDDEN;
|
||||
elseif ($e instanceof API_V1_exception_unauthorized)
|
||||
$code = API_V1_result::ERROR_UNAUTHORIZED;
|
||||
elseif ($e instanceof API_V1_exception_internalservererror)
|
||||
$code = API_V1_result::ERROR_INTERNALSERVERERROR;
|
||||
// elseif ($e instanceof API_V1_exception_notfound)
|
||||
// $code = API_V1_result::ERROR_NOTFOUND;
|
||||
elseif ($e instanceof Exception_NotFound)
|
||||
$code = API_V1_result::ERROR_NOTFOUND;
|
||||
elseif ($e instanceof Exception\NotFoundHttpException)
|
||||
$code = API_V1_result::ERROR_NOTFOUND;
|
||||
else
|
||||
$code = API_V1_result::ERROR_INTERNALSERVERERROR;
|
||||
|
||||
$result = $app['api']->get_error_message($app['request'], $code);
|
||||
|
||||
return $app['response']($result);
|
||||
});
|
||||
//// public function get_version();
|
||||
////
|
||||
////
|
||||
//// /**
|
||||
//// * Route : /records/DATABOX_ID/RECORD_ID/addtobasket/FORMAT/
|
||||
//// *
|
||||
//// * Method : POST
|
||||
//// *
|
||||
//// * Parameters :
|
||||
//// * DATABOX_ID : required INT
|
||||
//// * RECORD_ID : required INT
|
||||
//// *
|
||||
//// */
|
||||
//// public function add_record_tobasket(\Symfony\Component\HttpFoundation\Request $app['request'], $databox_id, $record_id);
|
||||
////
|
||||
////
|
||||
//// /**
|
||||
//// * Route : /feeds/PUBLICATION_ID/remove/FORMAT/
|
||||
//// *
|
||||
//// * Method : GET
|
||||
//// *
|
||||
//// * Parameters :
|
||||
//// * PUBLICATION_ID : required INT
|
||||
//// *
|
||||
//// */
|
||||
//// public function remove_publications(\Symfony\Component\HttpFoundation\Request $app['request'], $publication_id);
|
||||
////
|
||||
////
|
||||
//// /**
|
||||
//// * Route : /users/search/FORMAT/
|
||||
//// *
|
||||
//// * Method : POST-GET
|
||||
//// *
|
||||
//// * Parameters :
|
||||
//// *
|
||||
//// */
|
||||
//// public function search_users(\Symfony\Component\HttpFoundation\Request $app['request']);
|
||||
////
|
||||
//// /**
|
||||
//// * Route : /users/USER_ID/access/FORMAT/
|
||||
//// *
|
||||
//// * Method : GET
|
||||
//// *
|
||||
//// * Parameters :
|
||||
//// * USER_ID : required INT
|
||||
//// *
|
||||
//// */
|
||||
//// public function get_user_acces(\Symfony\Component\HttpFoundation\Request $app['request'], $usr_id);
|
||||
////
|
||||
//// /**
|
||||
//// * Route : /users/add/FORMAT/
|
||||
//// *
|
||||
//// * Method : POST
|
||||
//// *
|
||||
//// * Parameters :
|
||||
//// *
|
||||
//// */
|
||||
//// public function add_user(\Symfony\Component\HttpFoundation\Request $app['request']);
|
||||
return $app;
|
||||
|
||||
});
|
Reference in New Issue
Block a user