Open Journal Systems  2.4.3
 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 = parse_url(dirname($_SERVER['SCRIPT_NAME']), PHP_URL_PATH);
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 
274  function getQueryArray() {
275  $_this =& PKPRequest::_checkThis();
276 
277  $queryString = $_this->getQueryString();
278  $queryArray = array();
279 
280  if (isset($queryString)) {
281  parse_str($queryString, $queryArray);
282  }
283 
284  return $queryArray;
285  }
286 
291  function getRequestPath() {
292  $_this =& PKPRequest::_checkThis();
293 
294  if (!isset($_this->_requestPath)) {
295  if ($_this->isRestfulUrlsEnabled()) {
296  $_this->_requestPath = $_this->getBasePath();
297  } else {
298  $_this->_requestPath = $_SERVER['SCRIPT_NAME'];
299  }
300 
301  if ($_this->isPathInfoEnabled()) {
302  $_this->_requestPath .= isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
303  }
304  HookRegistry::call('Request::getRequestPath', array(&$_this->_requestPath));
305  }
306  return $_this->_requestPath;
307  }
308 
314  function getServerHost($default = 'localhost') {
315  $_this =& PKPRequest::_checkThis();
316 
317  if (!isset($_this->_serverHost)) {
318  $_this->_serverHost = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST']
319  : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']
320  : (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME']
321  : $default));
322  HookRegistry::call('Request::getServerHost', array(&$_this->_serverHost));
323  }
324  return $_this->_serverHost;
325  }
326 
331  function getProtocol() {
332  $_this =& PKPRequest::_checkThis();
333 
334  if (!isset($_this->_protocol)) {
335  $_this->_protocol = (!isset($_SERVER['HTTPS']) || strtolower_codesafe($_SERVER['HTTPS']) != 'on') ? 'http' : 'https';
336  HookRegistry::call('Request::getProtocol', array(&$_this->_protocol));
337  }
338  return $_this->_protocol;
339  }
340 
345  function getRequestMethod() {
347 
348  $requestMethod = (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '');
349  return $requestMethod;
350  }
351 
356  function isPost() {
357  $_this =& PKPRequest::_checkThis();
358 
359  return ($_this->getRequestMethod() == 'POST');
360  }
361 
366  function isGet() {
367  $_this =& PKPRequest::_checkThis();
368 
369  return ($_this->getRequestMethod() == 'GET');
370  }
371 
376  function getRemoteAddr() {
378 
379  static $ipaddr;
380  if (!isset($ipaddr)) {
381  if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
382  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
383  } else if (isset($_SERVER['REMOTE_ADDR']) &&
384  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['REMOTE_ADDR'], $matches)) {
385  } else if (preg_match_all('/([0-9.a-fA-F:]+)/', getenv('REMOTE_ADDR'), $matches)) {
386  } else {
387  $ipaddr = '';
388  }
389 
390  if (!isset($ipaddr)) {
391  // If multiple addresses are listed, take the last. (Supports ipv6.)
392  $ipaddr = $matches[0][count($matches[0])-1];
393  }
394  HookRegistry::call('Request::getRemoteAddr', array(&$ipaddr));
395  }
396  return $ipaddr;
397  }
398 
403  function getRemoteDomain() {
404  $_this =& PKPRequest::_checkThis();
405 
406  static $remoteDomain;
407  if (!isset($remoteDomain)) {
408  $remoteDomain = null;
409  $remoteDomain = @getHostByAddr($_this->getRemoteAddr());
410  HookRegistry::call('Request::getRemoteDomain', array(&$remoteDomain));
411  }
412  return $remoteDomain;
413  }
414 
419  function getUserAgent() {
421 
422  static $userAgent;
423  if (!isset($userAgent)) {
424  if (isset($_SERVER['HTTP_USER_AGENT'])) {
425  $userAgent = $_SERVER['HTTP_USER_AGENT'];
426  }
427  if (!isset($userAgent) || empty($userAgent)) {
428  $userAgent = getenv('HTTP_USER_AGENT');
429  }
430  if (!isset($userAgent) || $userAgent == false) {
431  $userAgent = '';
432  }
433  HookRegistry::call('Request::getUserAgent', array(&$userAgent));
434  }
435  return $userAgent;
436  }
437 
442  function isBot() {
443  $_this =& PKPRequest::_checkThis();
444 
445  static $isBot;
446  if (!isset($isBot)) {
447  $userAgent = $_this->getUserAgent();
448  $isBot = Core::isUserAgentBot($userAgent);
449  }
450  return $isBot;
451  }
452 
456  function isPathInfoEnabled() {
457  $_this =& PKPRequest::_checkThis();
458 
459  if (!isset($_this->_isPathInfoEnabled)) {
460  $_this->_isPathInfoEnabled = Config::getVar('general', 'disable_path_info')?false:true;
461  }
462  return $_this->_isPathInfoEnabled;
463  }
464 
468  function isRestfulUrlsEnabled() {
469  $_this =& PKPRequest::_checkThis();
470 
471  if (!isset($_this->_isRestfulUrlsEnabled)) {
472  $_this->_isRestfulUrlsEnabled = Config::getVar('general', 'restful_urls')?true:false;
473  }
474  return $_this->_isRestfulUrlsEnabled;
475  }
476 
481  function &getSite() {
483 
484  $site =& Registry::get('site', true, null);
485  if ($site === null) {
486  $siteDao =& DAORegistry::getDAO('SiteDAO');
487  $site =& $siteDao->getSite();
488  // PHP bug? This is needed for some reason or extra queries results.
489  Registry::set('site', $site);
490  }
491 
492  return $site;
493  }
494 
499  function &getSession() {
501 
502  $session =& Registry::get('session', true, null);
503 
504  if ($session === null) {
505  $sessionManager =& SessionManager::getManager();
506  $session = $sessionManager->getUserSession();
507  }
508 
509  return $session;
510  }
511 
516  function &getUser() {
518 
519  $user =& Registry::get('user', true, null);
520  if ($user === null) {
521  $sessionManager =& SessionManager::getManager();
522  $session =& $sessionManager->getUserSession();
523  $user =& $session->getUser();
524  }
525 
526  return $user;
527  }
528 
533  function getUserVar($key) {
534  $_this =& PKPRequest::_checkThis();
535 
536  // Get all vars (already cleaned)
537  $vars =& $_this->getUserVars();
538 
539  if (isset($vars[$key])) {
540  return $vars[$key];
541  } else {
542  return null;
543  }
544  }
545 
550  function &getUserVars() {
551  $_this =& PKPRequest::_checkThis();
552 
553  if (!isset($_this->_requestVars)) {
554  $_this->_requestVars = array_merge($_GET, $_POST);
555  $_this->cleanUserVar($_this->_requestVars);
556  }
557 
558  return $_this->_requestVars;
559  }
560 
573  function getUserDateVar($prefix, $defaultDay = null, $defaultMonth = null, $defaultYear = null, $defaultHour = 0, $defaultMinute = 0, $defaultSecond = 0) {
574  $_this =& PKPRequest::_checkThis();
575 
576  $monthPart = $_this->getUserVar($prefix . 'Month');
577  $dayPart = $_this->getUserVar($prefix . 'Day');
578  $yearPart = $_this->getUserVar($prefix . 'Year');
579  $hourPart = $_this->getUserVar($prefix . 'Hour');
580  $minutePart = $_this->getUserVar($prefix . 'Minute');
581  $secondPart = $_this->getUserVar($prefix . 'Second');
582 
583  switch ($_this->getUserVar($prefix . 'Meridian')) {
584  case 'pm':
585  if (is_numeric($hourPart) && $hourPart != 12) $hourPart += 12;
586  break;
587  case 'am':
588  default:
589  // Do nothing.
590  break;
591  }
592 
593  if (empty($dayPart)) $dayPart = $defaultDay;
594  if (empty($monthPart)) $monthPart = $defaultMonth;
595  if (empty($yearPart)) $yearPart = $defaultYear;
596  if (empty($hourPart)) $hourPart = $defaultHour;
597  if (empty($minutePart)) $minutePart = $defaultMinute;
598  if (empty($secondPart)) $secondPart = $defaultSecond;
599 
600  if (empty($monthPart) || empty($dayPart) || empty($yearPart)) return null;
601  return mktime($hourPart, $minutePart, $secondPart, $monthPart, $dayPart, $yearPart);
602  }
603 
609  function cleanUserVar(&$var) {
610  $_this =& PKPRequest::_checkThis();
611 
612  if (isset($var) && is_array($var)) {
613  foreach ($var as $key => $value) {
614  $_this->cleanUserVar($var[$key]);
615  }
616  } else if (isset($var)) {
617  $var = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($var) : $var);
618 
619  } else {
620  return null;
621  }
622  }
623 
628  function getCookieVar($key) {
629  $_this =& PKPRequest::_checkThis();
630 
631  if (isset($_COOKIE[$key])) {
632  $value = $_COOKIE[$key];
633  $_this->cleanUserVar($value);
634  return $value;
635  } else {
636  return null;
637  }
638  }
639 
645  function setCookieVar($key, $value) {
646  $_this =& PKPRequest::_checkThis();
647 
648  setcookie($key, $value, 0, $_this->getBasePath());
649  $_COOKIE[$key] = $value;
650  }
651 
662  function redirect($context = null, $page = null, $op = null, $path = null, $params = null, $anchor = null) {
663  $_this =& PKPRequest::_checkThis();
664  $dispatcher =& $_this->getDispatcher();
665  $_this->redirectUrl($dispatcher->url($_this, ROUTE_PAGE, $context, $page, $op, $path, $params, $anchor));
666  }
667 
672  function &getContext() {
673  $_this =& PKPRequest::_checkThis();
674  return $_this->_delegateToRouter('getContext');
675  }
676 
681  function getRequestedContextPath($contextLevel = null) {
682  $_this =& PKPRequest::_checkThis();
683 
684  // Emulate the old behavior of getRequestedContextPath for
685  // backwards compatibility.
686  if (is_null($contextLevel)) {
687  return $_this->_delegateToRouter('getRequestedContextPaths');
688  } else {
689  return array($_this->_delegateToRouter('getRequestedContextPath', $contextLevel));
690  }
691  }
692 
697  function getRequestedPage() {
698  $_this =& PKPRequest::_checkThis();
699  return $_this->_delegateToRouter('getRequestedPage');
700  }
701 
706  function getRequestedOp() {
707  $_this =& PKPRequest::_checkThis();
708  return $_this->_delegateToRouter('getRequestedOp');
709  }
710 
715  function getRequestedArgs() {
716  $_this =& PKPRequest::_checkThis();
717  return $_this->_delegateToRouter('getRequestedArgs');
718  }
719 
724  function url($context = null, $page = null, $op = null, $path = null,
725  $params = null, $anchor = null, $escape = false) {
726  $_this =& PKPRequest::_checkThis();
727  return $_this->_delegateToRouter('url', $context, $page, $op, $path,
728  $params, $anchor, $escape);
729  }
730 
745  function &_checkThis() {
746  if (isset($this) && is_a($this, 'PKPRequest')) {
747  return $this;
748  } else {
749  // This call is deprecated. We don't trigger a
750  // deprecation error, though, as there are so
751  // many instances of this error that it has a
752  // performance impact and renders the error
753  // log virtually useless when deprecation
754  // warnings are switched on.
755  // FIXME: Fix enough instances of this error so that
756  // we can put a deprecation warning in here.
757  $instance =& Registry::get('request');
758  assert(!is_null($instance));
759  return $instance;
760  }
761  }
762 
776  function &_delegateToRouter($method) {
777  // This call is deprecated. We don't trigger a
778  // deprecation error, though, as there are so
779  // many instances of this error that it has a
780  // performance impact and renders the error
781  // log virtually useless when deprecation
782  // warnings are switched on.
783  // FIXME: Fix enough instances of this error so that
784  // we can put a deprecation warning in here.
785  $_this =& PKPRequest::_checkThis();
786  $router =& $_this->getRouter();
787 
788  if (is_null($router)) {
789  assert(false);
790  $nullValue = null;
791  return $nullValue;
792  }
793 
794  // Construct the method call
795  $callable = array($router, $method);
796 
797  // Get additional parameters but replace
798  // the first parameter (currently the
799  // method to be called) with the request
800  // as all router methods required the request
801  // as their first parameter.
802  $parameters = func_get_args();
803  $parameters[0] =& $_this;
804 
805  $returner = call_user_func_array($callable, $parameters);
806  return $returner;
807  }
808 }
809 
810 ?>
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)