68 $this->_router = $router;
76 $this->_dispatcher = $dispatcher;
97 header(
"Location: $url");
107 import(
'lib.pkp.classes.core.JSONMessage');
109 $json->setEvent(
'redirectRequested', $url);
119 $url =
'https://' . $this->
getServerHost() . $_SERVER[
'REQUEST_URI'];
120 $queryString = $_SERVER[
'QUERY_STRING'];
121 if (!empty($queryString)) $url .=
"?$queryString";
131 $url =
'http://' . $this->
getServerHost() . $_SERVER[
'REQUEST_URI'];
132 $queryString = $_SERVER[
'QUERY_STRING'];
133 if (!empty($queryString)) $url .=
"?$queryString";
142 if (!isset($_SERVER[
'HTTP_IF_MODIFIED_SINCE']))
return null;
143 return strtotime($_SERVER[
'HTTP_IF_MODIFIED_SINCE']);
151 function getBaseUrl($allowProtocolRelative =
false) {
153 if ($serverHost !==
false) {
155 if ($allowProtocolRelative) {
173 if (!isset($this->_basePath)) {
180 $path = preg_replace(
'#/[^/]*$#',
'', $_SERVER[
'SCRIPT_NAME'].(substr($_SERVER[
'SCRIPT_NAME'], -1) ==
'/' || preg_match(
'#.php$#i', $_SERVER[
'SCRIPT_NAME']) ?
'' :
'/'));
185 $parts = explode(
'/', $path);
186 foreach ($parts as $i => $part) {
187 $pieces = array_map(array($this,
'encodeBasePathFragment'), str_split($part));
188 $parts[$i] = implode(
'', $pieces);
190 $this->_basePath = implode(
'/', $parts);
192 if ($this->_basePath ==
'/' || $this->_basePath ==
'\\') {
193 $this->_basePath =
'';
208 if (!preg_match(
'/[A-Za-z0-9-._~!$&\'()*+,;=:@]/', $fragment)) {
209 return rawurlencode($fragment);
221 if (!isset($indexUrl)) {
238 if (!isset($completeUrl)) {
241 if (!empty($queryString)) $completeUrl .=
"?$queryString";
255 if (!isset($requestUrl)) {
270 if (!isset($queryString)) {
271 $queryString = isset($_SERVER[
'QUERY_STRING'])?$_SERVER[
'QUERY_STRING']:
'';
286 $queryArray = array();
288 if (isset($queryString)) {
289 parse_str($queryString, $queryArray);
294 if (isset($queryArray[$varName])) unset($queryArray[$varName]);
305 if (!isset($this->_requestPath)) {
309 $this->_requestPath = isset($_SERVER[
'SCRIPT_NAME'])?$_SERVER[
'SCRIPT_NAME']:
'';
313 $this->_requestPath .= isset($_SERVER[
'PATH_INFO']) ? $_SERVER[
'PATH_INFO'] :
'';
326 function getServerHost($default =
null, $includePort =
true) {
327 if ($default ===
null) $default =
'localhost';
329 if (!isset($this->_serverHost)) {
330 $this->_serverHost = isset($_SERVER[
'HTTP_X_FORWARDED_HOST']) ? $_SERVER[
'HTTP_X_FORWARDED_HOST']
331 : (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST']
332 : (isset($_SERVER[
'SERVER_NAME']) ? $_SERVER[
'SERVER_NAME']
335 $this->_serverHost = strtok($this->_serverHost,
',');
336 HookRegistry::call(
'Request::getServerHost', array(&$this->_serverHost, &$default, &$includePort));
340 return preg_replace(
"/:\d*$/",
'', $this->_serverHost);
350 if (!isset($this->_protocol)) {
351 $this->_protocol = (!isset($_SERVER[
'HTTPS']) ||
strtolower_codesafe($_SERVER[
'HTTPS']) !=
'on') ?
'http' :
'https';
362 return (isset($_SERVER[
'REQUEST_METHOD']) ? $_SERVER[
'REQUEST_METHOD'] :
'');
387 return $this->
getUserVar(
'csrfToken') == $session->getCSRFToken();
396 if (is_null($ipaddr)) {
397 if (isset($_SERVER[
'HTTP_X_FORWARDED_FOR']) &&
399 preg_match_all(
'/([0-9.a-fA-F:]+)/', $_SERVER[
'HTTP_X_FORWARDED_FOR'], $matches)) {
400 }
else if (isset($_SERVER[
'REMOTE_ADDR']) &&
401 preg_match_all(
'/([0-9.a-fA-F:]+)/', $_SERVER[
'REMOTE_ADDR'], $matches)) {
402 }
else if (preg_match_all(
'/([0-9.a-fA-F:]+)/', getenv(
'REMOTE_ADDR'), $matches)) {
407 if (!isset($ipaddr)) {
409 $ipaddr = $matches[0][count($matches[0])-1];
421 static $remoteDomain;
422 if (!isset($remoteDomain)) {
423 $remoteDomain =
null;
427 return $remoteDomain;
435 if (!isset($this->_userAgent)) {
436 if (isset($_SERVER[
'HTTP_USER_AGENT'])) {
437 $this->_userAgent = $_SERVER[
'HTTP_USER_AGENT'];
439 if (!isset($this->_userAgent) || empty($this->_userAgent)) {
440 $this->_userAgent = getenv(
'HTTP_USER_AGENT');
442 if (!isset($this->_userAgent) || $this->_userAgent ==
false) {
443 $this->_userAgent =
'';
455 if (!isset($this->_isBot)) {
466 if (!isset($this->_isPathInfoEnabled)) {
467 $this->_isPathInfoEnabled =
Config::getVar(
'general',
'disable_path_info')?
false:
true;
476 if (!isset($this->_isRestfulUrlsEnabled)) {
477 $this->_isRestfulUrlsEnabled =
Config::getVar(
'general',
'restful_urls')?
true:
false;
488 if ($site ===
null) {
490 $site = $siteDao->getSite();
505 if ($session ===
null) {
507 $session = $sessionManager->getUserSession();
521 if (!is_null($handler = $router->getHandler()) && !is_null($token = $handler->getApiToken())) {
522 if ($user ===
null) {
524 $user = $userDao->getBySetting(
'apiKey', $token);
526 if (is_null($user) || !$user->getData(
'apiKeyEnabled')) {
532 if ($user ===
null) {
534 $session = $sessionManager->getUserSession();
535 $user = $session->getUser();
548 if (is_a($router,
'APIRouter') && (!is_null($handler = $router->getHandler()))) {
549 $handler = $router->getHandler();
550 $value = $handler->getParameter($key);
551 if (!is_null($value)) {
559 if (isset($vars[$key])) {
571 if (!isset($this->_requestVars)) {
572 $this->_requestVars = array_map(
function($s) {
573 return is_string($s)?trim($s):$s;
574 }, array_merge($_GET, $_POST));
592 function getUserDateVar($prefix, $defaultDay =
null, $defaultMonth =
null, $defaultYear =
null, $defaultHour = 0, $defaultMinute = 0, $defaultSecond = 0) {
593 $monthPart = $this->
getUserVar($prefix .
'Month');
594 $dayPart = $this->
getUserVar($prefix .
'Day');
595 $yearPart = $this->
getUserVar($prefix .
'Year');
596 $hourPart = $this->
getUserVar($prefix .
'Hour');
597 $minutePart = $this->
getUserVar($prefix .
'Minute');
598 $secondPart = $this->
getUserVar($prefix .
'Second');
600 switch ($this->
getUserVar($prefix .
'Meridian')) {
602 if (is_numeric($hourPart) && $hourPart != 12) $hourPart += 12;
610 if (empty($dayPart)) $dayPart = $defaultDay;
611 if (empty($monthPart)) $monthPart = $defaultMonth;
612 if (empty($yearPart)) $yearPart = $defaultYear;
613 if (empty($hourPart)) $hourPart = $defaultHour;
614 if (empty($minutePart)) $minutePart = $defaultMinute;
615 if (empty($secondPart)) $secondPart = $defaultSecond;
617 if (empty($monthPart) || empty($dayPart) || empty($yearPart))
return null;
618 return mktime($hourPart, $minutePart, $secondPart, $monthPart, $dayPart, $yearPart);
626 if (isset($_COOKIE[$key])) {
627 return $_COOKIE[$key];
641 if (!$basePath) $basePath =
'/';
643 setcookie($key, $value, $expire, $basePath);
644 $_COOKIE[$key] = $value;
657 function redirect($context =
null, $page =
null,
$op =
null, $path =
null, $params =
null, $anchor =
null) {
659 $this->
redirectUrl($dispatcher->url($this, ROUTE_PAGE, $context, $page,
$op, $path, $params, $anchor));
678 if (is_null($contextLevel)) {
681 return array($this->
_delegateToRouter(
'getRequestedContextPath', $contextLevel));
713 function url($context =
null, $page =
null,
$op =
null, $path =
null,
714 $params =
null, $anchor =
null, $escape =
false) {
716 $params, $anchor, $escape);
743 if (is_null($router)) {
750 $callable = array($router, $method);
757 $parameters = func_get_args();
758 $parameters[0] =& $this;
760 $returner = call_user_func_array($callable, $parameters);