Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
PKPRequest.inc.php
1 <?php
2 
17 class PKPRequest {
18  //
19  // Internal state - please do not reference directly
20  //
22  var $_router = null;
24  var $_dispatcher = null;
26  var $_requestVars = null;
28  var $_basePath;
30  var $_requestPath;
32  var $_isRestfulUrlsEnabled;
34  var $_isPathInfoEnabled;
36  var $_serverHost;
38  var $_baseUrl;
40  var $_protocol;
41 
42 
47  function &getRouter() {
48  return $this->_router;
49  }
50 
55  function setRouter(&$router) {
56  $this->_router =& $router;
57  }
58 
63  function setDispatcher(&$dispatcher) {
64  $this->_dispatcher =& $dispatcher;
65  }
66 
71  function &getDispatcher() {
72  return $this->_dispatcher;
73  }
74 
75 
80  function redirectUrl($url) {
82 
83  if (HookRegistry::call('Request::redirect', array(&$url))) {
84  return;
85  }
86 
87  header("Location: $url");
88  exit();
89  }
90 
95  function redirectUrlJson($url) {
96  import('lib.pkp.classes.core.JSONMessage');
97  $json = new JSONMessage(true);
98  $json->setEvent('redirectRequested', $url);
99  return $json->getString();
100  }
101 
105  function redirectSSL() {
106  $_this =& PKPRequest::_checkThis();
107 
108  $url = 'https://' . $_this->getServerHost() . $_this->getRequestPath();
109  $queryString = $_this->getQueryString();
110  if (!empty($queryString)) $url .= "?$queryString";
111  $_this->redirectUrl($url);
112  }
113 
117  function redirectNonSSL() {
118  $_this =& PKPRequest::_checkThis();
119 
120  $url = 'http://' . $_this->getServerHost() . $_this->getRequestPath();
121  $queryString = $_this->getQueryString();
122  if (!empty($queryString)) $url .= "?$queryString";
123  $_this->redirectUrl($url);
124  }
125 
130  function getIfModifiedSince() {
131  if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) return null;
132  return strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
133  }
134 
139  function getBaseUrl() {
140  $_this =& PKPRequest::_checkThis();
141 
142  if (!isset($_this->_baseUrl)) {
143  $serverHost = $_this->getServerHost(null);
144  if ($serverHost !== null) {
145  // Auto-detection worked.
146  $_this->_baseUrl = $_this->getProtocol() . '://' . $_this->getServerHost() . $_this->getBasePath();
147  } else {
148  // Auto-detection didn't work (e.g. this is a command-line call); use configuration param
149  $_this->_baseUrl = Config::getVar('general', 'base_url');
150  }
151  HookRegistry::call('Request::getBaseUrl', array(&$_this->_baseUrl));
152  }
153 
154  return $_this->_baseUrl;
155  }
156 
161  function getBasePath() {
162  $_this =& PKPRequest::_checkThis();
163 
164  if (!isset($_this->_basePath)) {
165  $path = preg_replace('#/[^/]*$#', '', $_SERVER['SCRIPT_NAME']);
166 
167  // Encode charcters which need to be encoded in a URL.
168  // Simply using rawurlencode() doesn't work because it
169  // also encodes characters which are valid in a URL (i.e. @, $).
170  $parts = explode('/', $path);
171  foreach ($parts as $i => $part) {
172  $pieces = array_map(array($this, 'encodeBasePathFragment'), str_split($part));
173  $parts[$i] = implode('', $pieces);
174  }
175  $_this->_basePath = implode('/', $parts);
176 
177  if ($_this->_basePath == '/' || $_this->_basePath == '\\') {
178  $_this->_basePath = '';
179  }
180  HookRegistry::call('Request::getBasePath', array(&$_this->_basePath));
181  }
182 
183  return $_this->_basePath;
184  }
185 
192  function encodeBasePathFragment($fragment) {
193  if (!preg_match('/[A-Za-z0-9-._~!$&\'()*+,;=:@]/', $fragment)) {
194  return rawurlencode($fragment);
195  }
196  return $fragment;
197  }
198 
203  function getIndexUrl() {
204  static $indexUrl;
205 
206  $_this =& PKPRequest::_checkThis();
207  if (!isset($indexUrl)) {
208  $indexUrl = $_this->_delegateToRouter('getIndexUrl');
209 
210  // Call legacy hook
211  HookRegistry::call('Request::getIndexUrl', array(&$indexUrl));
212  }
213 
214  return $indexUrl;
215  }
216 
221  function getCompleteUrl() {
222  $_this =& PKPRequest::_checkThis();
223 
224  static $completeUrl;
225 
226  if (!isset($completeUrl)) {
227  $completeUrl = $_this->getRequestUrl();
228  $queryString = $_this->getQueryString();
229  if (!empty($queryString)) $completeUrl .= "?$queryString";
230  HookRegistry::call('Request::getCompleteUrl', array(&$completeUrl));
231  }
232 
233  return $completeUrl;
234  }
235 
240  function getRequestUrl() {
241  $_this =& PKPRequest::_checkThis();
242 
243  static $requestUrl;
244 
245  if (!isset($requestUrl)) {
246  $requestUrl = $_this->getProtocol() . '://' . $_this->getServerHost() . $_this->getRequestPath();
247  HookRegistry::call('Request::getRequestUrl', array(&$requestUrl));
248  }
249 
250  return $requestUrl;
251  }
252 
257  function getQueryString() {
259 
260  static $queryString;
261 
262  if (!isset($queryString)) {
263  $queryString = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'';
264  HookRegistry::call('Request::getQueryString', array(&$queryString));
265  }
266 
267  return $queryString;
268  }
269 
276  function getQueryArray() {
277  $_this =& PKPRequest::_checkThis();
278 
279  $queryString = $_this->getQueryString();
280  $queryArray = array();
281 
282  if (isset($queryString)) {
283  parse_str($queryString, $queryArray);
284  }
285 
286  // Filter out disable_path_info reserved parameters
287  foreach (array_merge(Application::getContextList(), array('path', 'page', 'op')) as $varName) {
288  if (isset($queryArray[$varName])) unset($queryArray[$varName]);
289  }
290 
291  return $queryArray;
292  }
293 
298  function getRequestPath() {
299  $_this =& PKPRequest::_checkThis();
300 
301  if (!isset($_this->_requestPath)) {
302  if ($_this->isRestfulUrlsEnabled()) {
303  $_this->_requestPath = $_this->getBasePath();
304  } else {
305  $_this->_requestPath = $_SERVER['SCRIPT_NAME'];
306  }
307 
308  if ($_this->isPathInfoEnabled()) {
309  $_this->_requestPath .= isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
310  }
311  HookRegistry::call('Request::getRequestPath', array(&$_this->_requestPath));
312  }
313  return $_this->_requestPath;
314  }
315 
321  function getServerHost($default = 'localhost') {
322  $_this =& PKPRequest::_checkThis();
323 
324  if (!isset($_this->_serverHost)) {
325  $_this->_serverHost = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST']
326  : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']
327  : (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME']
328  : $default));
329  HookRegistry::call('Request::getServerHost', array(&$_this->_serverHost));
330  }
331  return $_this->_serverHost;
332  }
333 
338  function getProtocol() {
339  $_this =& PKPRequest::_checkThis();
340 
341  if (!isset($_this->_protocol)) {
342  $_this->_protocol = (!isset($_SERVER['HTTPS']) || strtolower_codesafe($_SERVER['HTTPS']) != 'on') ? 'http' : 'https';
343  HookRegistry::call('Request::getProtocol', array(&$_this->_protocol));
344  }
345  return $_this->_protocol;
346  }
347 
352  function getRequestMethod() {
354 
355  $requestMethod = (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '');
356  return $requestMethod;
357  }
358 
363  function isPost() {
364  $_this =& PKPRequest::_checkThis();
365 
366  return ($_this->getRequestMethod() == 'POST');
367  }
368 
373  function isGet() {
374  $_this =& PKPRequest::_checkThis();
375 
376  return ($_this->getRequestMethod() == 'GET');
377  }
378 
383  function getRemoteAddr() {
385 
386  static $ipaddr;
387  if (!isset($ipaddr)) {
388  if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
389  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
390  } else if (isset($_SERVER['REMOTE_ADDR']) &&
391  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['REMOTE_ADDR'], $matches)) {
392  } else if (preg_match_all('/([0-9.a-fA-F:]+)/', getenv('REMOTE_ADDR'), $matches)) {
393  } else {
394  $ipaddr = '';
395  }
396 
397  if (!isset($ipaddr)) {
398  // If multiple addresses are listed, take the last. (Supports ipv6.)
399  $ipaddr = $matches[0][count($matches[0])-1];
400  }
401  HookRegistry::call('Request::getRemoteAddr', array(&$ipaddr));
402  }
403  return $ipaddr;
404  }
405 
410  function getRemoteDomain() {
411  $_this =& PKPRequest::_checkThis();
412 
413  static $remoteDomain;
414  if (!isset($remoteDomain)) {
415  $remoteDomain = null;
416  $remoteDomain = @getHostByAddr($_this->getRemoteAddr());
417  HookRegistry::call('Request::getRemoteDomain', array(&$remoteDomain));
418  }
419  return $remoteDomain;
420  }
421 
426  function getUserAgent() {
428 
429  static $userAgent;
430  if (!isset($userAgent)) {
431  if (isset($_SERVER['HTTP_USER_AGENT'])) {
432  $userAgent = $_SERVER['HTTP_USER_AGENT'];
433  }
434  if (!isset($userAgent) || empty($userAgent)) {
435  $userAgent = getenv('HTTP_USER_AGENT');
436  }
437  if (!isset($userAgent) || $userAgent == false) {
438  $userAgent = '';
439  }
440  HookRegistry::call('Request::getUserAgent', array(&$userAgent));
441  }
442  return $userAgent;
443  }
444 
449  function isBot() {
450  $_this =& PKPRequest::_checkThis();
451 
452  static $isBot;
453  if (!isset($isBot)) {
454  $userAgent = $_this->getUserAgent();
455  $isBot = Core::isUserAgentBot($userAgent);
456  }
457  return $isBot;
458  }
459 
463  function isPathInfoEnabled() {
464  $_this =& PKPRequest::_checkThis();
465 
466  if (!isset($_this->_isPathInfoEnabled)) {
467  $_this->_isPathInfoEnabled = Config::getVar('general', 'disable_path_info')?false:true;
468  }
469  return $_this->_isPathInfoEnabled;
470  }
471 
475  function isRestfulUrlsEnabled() {
476  $_this =& PKPRequest::_checkThis();
477 
478  if (!isset($_this->_isRestfulUrlsEnabled)) {
479  $_this->_isRestfulUrlsEnabled = Config::getVar('general', 'restful_urls')?true:false;
480  }
481  return $_this->_isRestfulUrlsEnabled;
482  }
483 
488  function &getSite() {
490 
491  $site =& Registry::get('site', true, null);
492  if ($site === null) {
493  $siteDao =& DAORegistry::getDAO('SiteDAO');
494  $site =& $siteDao->getSite();
495  // PHP bug? This is needed for some reason or extra queries results.
496  Registry::set('site', $site);
497  }
498 
499  return $site;
500  }
501 
506  function &getSession() {
508 
509  $session =& Registry::get('session', true, null);
510 
511  if ($session === null) {
512  $sessionManager =& SessionManager::getManager();
513  $session = $sessionManager->getUserSession();
514  }
515 
516  return $session;
517  }
518 
523  function &getUser() {
525 
526  $user =& Registry::get('user', true, null);
527  if ($user === null) {
528  $sessionManager =& SessionManager::getManager();
529  $session =& $sessionManager->getUserSession();
530  $user =& $session->getUser();
531  }
532 
533  return $user;
534  }
535 
540  function getUserVar($key) {
541  $_this =& PKPRequest::_checkThis();
542 
543  // Get all vars (already cleaned)
544  $vars =& $_this->getUserVars();
545 
546  if (isset($vars[$key])) {
547  return $vars[$key];
548  } else {
549  return null;
550  }
551  }
552 
557  function &getUserVars() {
558  $_this =& PKPRequest::_checkThis();
559 
560  if (!isset($_this->_requestVars)) {
561  $_this->_requestVars = array_merge($_GET, $_POST);
562  $_this->cleanUserVar($_this->_requestVars);
563  }
564 
565  return $_this->_requestVars;
566  }
567 
580  function getUserDateVar($prefix, $defaultDay = null, $defaultMonth = null, $defaultYear = null, $defaultHour = 0, $defaultMinute = 0, $defaultSecond = 0) {
581  $_this =& PKPRequest::_checkThis();
582 
583  $monthPart = $_this->getUserVar($prefix . 'Month');
584  $dayPart = $_this->getUserVar($prefix . 'Day');
585  $yearPart = $_this->getUserVar($prefix . 'Year');
586  $hourPart = $_this->getUserVar($prefix . 'Hour');
587  $minutePart = $_this->getUserVar($prefix . 'Minute');
588  $secondPart = $_this->getUserVar($prefix . 'Second');
589 
590  switch ($_this->getUserVar($prefix . 'Meridian')) {
591  case 'pm':
592  if (is_numeric($hourPart) && $hourPart != 12) $hourPart += 12;
593  break;
594  case 'am':
595  default:
596  // Do nothing.
597  break;
598  }
599 
600  if (empty($dayPart)) $dayPart = $defaultDay;
601  if (empty($monthPart)) $monthPart = $defaultMonth;
602  if (empty($yearPart)) $yearPart = $defaultYear;
603  if (empty($hourPart)) $hourPart = $defaultHour;
604  if (empty($minutePart)) $minutePart = $defaultMinute;
605  if (empty($secondPart)) $secondPart = $defaultSecond;
606 
607  if (empty($monthPart) || empty($dayPart) || empty($yearPart)) return null;
608  return mktime($hourPart, $minutePart, $secondPart, $monthPart, $dayPart, $yearPart);
609  }
610 
616  function cleanUserVar(&$var) {
617  $_this =& PKPRequest::_checkThis();
618 
619  if (isset($var) && is_array($var)) {
620  foreach ($var as $key => $value) {
621  $_this->cleanUserVar($var[$key]);
622  }
623  } else if (isset($var)) {
624  $var = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($var) : $var);
625 
626  } else {
627  return null;
628  }
629  }
630 
635  function getCookieVar($key) {
636  $_this =& PKPRequest::_checkThis();
637 
638  if (isset($_COOKIE[$key])) {
639  $value = $_COOKIE[$key];
640  $_this->cleanUserVar($value);
641  return $value;
642  } else {
643  return null;
644  }
645  }
646 
652  function setCookieVar($key, $value) {
653  $_this =& PKPRequest::_checkThis();
654 
655  setcookie($key, $value, 0, $_this->getBasePath());
656  $_COOKIE[$key] = $value;
657  }
658 
669  function redirect($context = null, $page = null, $op = null, $path = null, $params = null, $anchor = null) {
670  $_this =& PKPRequest::_checkThis();
671  $dispatcher =& $_this->getDispatcher();
672  $_this->redirectUrl($dispatcher->url($_this, ROUTE_PAGE, $context, $page, $op, $path, $params, $anchor));
673  }
674 
679  function &getContext() {
680  $_this =& PKPRequest::_checkThis();
681  return $_this->_delegateToRouter('getContext');
682  }
683 
688  function getRequestedContextPath($contextLevel = null) {
689  $_this =& PKPRequest::_checkThis();
690 
691  // Emulate the old behavior of getRequestedContextPath for
692  // backwards compatibility.
693  if (is_null($contextLevel)) {
694  return $_this->_delegateToRouter('getRequestedContextPaths');
695  } else {
696  return array($_this->_delegateToRouter('getRequestedContextPath', $contextLevel));
697  }
698  }
699 
704  function getRequestedPage() {
705  $_this =& PKPRequest::_checkThis();
706  return $_this->_delegateToRouter('getRequestedPage');
707  }
708 
713  function getRequestedOp() {
714  $_this =& PKPRequest::_checkThis();
715  return $_this->_delegateToRouter('getRequestedOp');
716  }
717 
722  function getRequestedArgs() {
723  $_this =& PKPRequest::_checkThis();
724  return $_this->_delegateToRouter('getRequestedArgs');
725  }
726 
731  function url($context = null, $page = null, $op = null, $path = null,
732  $params = null, $anchor = null, $escape = false) {
733  $_this =& PKPRequest::_checkThis();
734  return $_this->_delegateToRouter('url', $context, $page, $op, $path,
735  $params, $anchor, $escape);
736  }
737 
752  function &_checkThis() {
753  if (isset($this) && is_a($this, 'PKPRequest')) {
754  return $this;
755  } else {
756  // This call is deprecated. We don't trigger a
757  // deprecation error, though, as there are so
758  // many instances of this error that it has a
759  // performance impact and renders the error
760  // log virtually useless when deprecation
761  // warnings are switched on.
762  // FIXME: Fix enough instances of this error so that
763  // we can put a deprecation warning in here.
764  $instance =& Registry::get('request');
765  assert(!is_null($instance));
766  return $instance;
767  }
768  }
769 
783  function &_delegateToRouter($method) {
784  // This call is deprecated. We don't trigger a
785  // deprecation error, though, as there are so
786  // many instances of this error that it has a
787  // performance impact and renders the error
788  // log virtually useless when deprecation
789  // warnings are switched on.
790  // FIXME: Fix enough instances of this error so that
791  // we can put a deprecation warning in here.
792  $_this =& PKPRequest::_checkThis();
793  $router =& $_this->getRouter();
794 
795  if (is_null($router)) {
796  assert(false);
797  $nullValue = null;
798  return $nullValue;
799  }
800 
801  // Construct the method call
802  $callable = array($router, $method);
803 
804  // Get additional parameters but replace
805  // the first parameter (currently the
806  // method to be called) with the request
807  // as all router methods required the request
808  // as their first parameter.
809  $parameters = func_get_args();
810  $parameters[0] =& $_this;
811 
812  $returner = call_user_func_array($callable, $parameters);
813  return $returner;
814  }
815 }
816 
817 ?>
setDispatcher(&$dispatcher)
redirectUrl($url)
getRequestedContextPath($contextLevel=null)
redirect($context=null, $page=null, $op=null, $path=null, $params=null, $anchor=null)
getServerHost($default= 'localhost')
Class to represent a JSON (Javascript Object Notation) message.
getUserDateVar($prefix, $defaultDay=null, $defaultMonth=null, $defaultYear=null, $defaultHour=0, $defaultMinute=0, $defaultSecond=0)
isUserAgentBot($userAgent, $botRegexpsFile=USER_AGENTS_FILE)
Definition: Core.inc.php:138
set($key, &$value)
getVar($section, $key, $default=null)
Definition: Config.inc.php:34
setRouter(&$router)
url($context=null, $page=null, $op=null, $path=null, $params=null, $anchor=null, $escape=false)
& getDAO($name, $dbconn=null)
cleanVar($var)
Definition: Core.inc.php:49
& _delegateToRouter($method)
call($hookName, $args=null)
redirectUrlJson($url)
& get($key, $createIfEmpty=false, $createWithDefault=null)
Class providing operations associated with HTTP requests.
encodeBasePathFragment($fragment)
cleanUserVar(&$var)
setCookieVar($key, $value)
getCookieVar($key)