Open Monograph Press  3.3.0
APIRouter.inc.php
1 <?php
2 
20 import('lib.pkp.classes.core.PKPRouter');
21 import('classes.core.Request');
22 import('classes.handler.Handler');
23 
24 class APIRouter extends PKPRouter {
25 
30  protected function getPathInfoParts() {
31  $pathInfoEnabled = Config::getVar('general', 'disable_path_info') ? false : true;
32  if ($pathInfoEnabled && isset($_SERVER['PATH_INFO'])) {
33  return explode('/', trim($_SERVER['PATH_INFO'], '/'));
34  }
35 
36  $request = $this->getApplication()->getRequest();
37  $queryString = $request->getQueryString();
38  $queryArray = array();
39  if (isset($queryString)) {
40  parse_str($queryString, $queryArray);
41  }
42 
43  if (in_array('endpoint', array_keys($queryArray)) && isset($queryArray['journal'])) {
44  $endpoint = $queryArray['endpoint'];
45  return explode('/', trim($endpoint, '/'));
46  }
47 
48  return null;
49  }
50 
56  function supports($request) {
57  $pathInfoParts = $this->getPathInfoParts();
58 
59  if (!is_null($pathInfoParts) && count($pathInfoParts)>=2 && $pathInfoParts[1] == 'api') {
60  // Context-specific API requests: [index.php]/{contextPath}/api
61  return true;
62  }
63 
64  return false;
65  }
66 
71  function getVersion() {
72  $pathInfoParts = $this->getPathInfoParts();
73  return Core::cleanFileVar(isset($pathInfoParts[2]) ? $pathInfoParts[2] : '');
74  }
75 
80  function getEntity() {
81  $pathInfoParts = $this->getPathInfoParts();
82  return Core::cleanFileVar(isset($pathInfoParts[3]) ? $pathInfoParts[3] : '');
83  }
84 
85  //
86  // Implement template methods from PKPRouter
87  //
91  function route($request) {
92  // Ensure slim library is available
93  require_once('lib/pkp/lib/vendor/autoload.php');
94 
95  $sourceFile = sprintf('api/%s/%s/index.php', $this->getVersion(), $this->getEntity());
96 
97  if (!file_exists($sourceFile)) {
98  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_API, LOCALE_COMPONENT_APP_API);
99  http_response_code('404');
100  header('Content-Type: application/json');
101  echo json_encode([
102  'error' => 'api.404.endpointNotFound',
103  'errorMessage' => __('api.404.endpointNotFound'),
104  ]);
105  exit;
106  }
107 
108  if (!defined('SESSION_DISABLE_INIT')) {
109  // Initialize session
111  }
112 
113  $handler = require ('./'.$sourceFile);
114  $this->setHandler($handler);
115  $handler->getApp()->run();
116  }
117 
124  function getRequestedOp($request) {
125  $handler = $this->getHandler();
126  $container = $handler->getApp()->getContainer();
127  $router = $container->get('router');
128  $slimRequest = $handler->getSlimRequest();
129  $routeInfo = $router->dispatch($slimRequest);
130  if (isset($routeInfo[1])) {
131  $route = $router->lookupRoute($routeInfo[1]);
132  $callable = $route->getCallable();
133  if (is_array($callable) && count($callable) == 2)
134  return $callable[1];
135  }
136  return '';
137  }
138 
142  function handleAuthorizationFailure($request, $authorizationMessage) {
143  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_API, LOCALE_COMPONENT_APP_API);
144  http_response_code('403');
145  header('Content-Type: application/json');
146  echo json_encode([
147  'error' => $authorizationMessage,
148  'errorMessage' => __($authorizationMessage),
149  ]);
150  exit;
151  }
152 
156  function url($request, $newContext = null, $endpoint = null, $op = null, $path = null,
157  $params = null, $anchor = null, $escape = false) {
158 
159  // APIHandlers do not understand $op, $path or $anchor. All routing is baked
160  // into the $endpoint string. It only accepts a string as the $newContext,
161  // since it relies on this when path info is disabled.
162  if (!is_null($op) || !is_null($path) || !is_null($anchor) || !is_scalar($newContext)) {
163  throw new Exception('APIRouter::url() should not be called with an op, path or anchor. If a new context is passed, the context path must be passed instead of the context object.');
164  }
165 
166  //
167  // Base URL and Context
168  //
169  $baseUrlAndContext = $this->_urlGetBaseAndContext($request, $this->_urlCanonicalizeNewContext($newContext));
170  $baseUrl = array_shift($baseUrlAndContext);
171  $context = $baseUrlAndContext;
172 
173  //
174  // Additional query parameters
175  //
176  $additionalParameters = $this->_urlGetAdditionalParameters($request, $params, $escape);
177 
178  //
179  // Assemble URL
180  //
181  if ($request->isPathInfoEnabled()) {
182  // If path info is enabled, everything but params goes into the path
183  $pathInfoArray = array_merge(
184  $context,
185  ['api', API_VERSION, $endpoint]
186  );
187  $queryParametersArray = $additionalParameters;
188  } else {
189  // If path info is disabled, the context and endpoint must be passed as
190  // query params, and the context must be concatenated into the endpoint
191  $pathInfoArray = array();
192  $queryParametersArray = array_merge(
193  $context,
194  [sprintf('endpoint=/%s/api/%s/%s', $newContext, API_VERSION, $endpoint)],
195  $additionalParameters
196  );
197  }
198 
199  return $this->_urlFromParts($baseUrl, $pathInfoArray, $queryParametersArray, $anchor, $escape);
200  }
201 }
APIRouter\handleAuthorizationFailure
handleAuthorizationFailure($request, $authorizationMessage)
Definition: APIRouter.inc.php:142
APIRouter\route
route($request)
Definition: APIRouter.inc.php:91
$op
$op
Definition: lib/pkp/pages/help/index.php:18
PKPRouter\setHandler
setHandler($handler)
Definition: PKPRouter.inc.php:153
SessionManager\getManager
static getManager()
Definition: SessionManager.inc.php:124
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
PKPRouter\getHandler
getHandler()
Definition: PKPRouter.inc.php:161
APIRouter\getRequestedOp
getRequestedOp($request)
Definition: APIRouter.inc.php:124
APIRouter
Map HTTP requests to a REST API using the Slim microframework.
Definition: APIRouter.inc.php:24
APIRouter\supports
supports($request)
Definition: APIRouter.inc.php:56
APIRouter\getEntity
getEntity()
Definition: APIRouter.inc.php:80
APIRouter\url
url($request, $newContext=null, $endpoint=null, $op=null, $path=null, $params=null, $anchor=null, $escape=false)
Definition: APIRouter.inc.php:156
PKPRouter\_urlCanonicalizeNewContext
_urlCanonicalizeNewContext($newContext)
Definition: PKPRouter.inc.php:452
PKPRouter\_urlGetBaseAndContext
_urlGetBaseAndContext($request, $newContext=array())
Definition: PKPRouter.inc.php:487
APIRouter\getVersion
getVersion()
Definition: APIRouter.inc.php:71
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
PKPRouter\getApplication
& getApplication()
Definition: PKPRouter.inc.php:114
PKPRouter
Basic router class that has functionality common to all routers.
Definition: PKPRouter.inc.php:57
PKPRouter\_urlFromParts
_urlFromParts($baseUrl, $pathInfoArray=array(), $queryParametersArray=array(), $anchor='', $escape=false)
Definition: PKPRouter.inc.php:582
APIRouter\getPathInfoParts
getPathInfoParts()
Definition: APIRouter.inc.php:30
PKPRouter\_urlGetAdditionalParameters
_urlGetAdditionalParameters($request, $params=null, $escape=true)
Definition: PKPRouter.inc.php:555
Core\cleanFileVar
static cleanFileVar($var)
Definition: Core.inc.php:54