Open Monograph Press  1.1
 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;
23 
25  var $_dispatcher = null;
26 
28  var $_requestVars = null;
29 
32 
34  var $_requestPath;
35 
38 
41 
44 
46  var $_baseUrl;
47 
50 
52  var $_isBot;
53 
56 
57 
62  function &getRouter() {
63  return $this->_router;
64  }
65 
70  function setRouter($router) {
71  $this->_router = $router;
72  }
73 
78  function setDispatcher($dispatcher) {
79  $this->_dispatcher = $dispatcher;
80  }
81 
86  function &getDispatcher() {
87  return $this->_dispatcher;
88  }
89 
90 
95  function redirectUrl($url) {
97 
98  if (HookRegistry::call('Request::redirect', array(&$url))) {
99  return;
100  }
101 
102  header("Location: $url");
103  exit();
104  }
105 
110  function redirectUrlJson($url) {
111  import('lib.pkp.classes.core.JSONMessage');
112  $json = new JSONMessage(true);
113  $json->setEvent('redirectRequested', $url);
114  return $json->getString();
115  }
116 
120  function redirectSSL() {
121  $_this =& PKPRequest::_checkThis();
123  $url = 'https://' . $_this->getServerHost() . $_this->getRequestPath();
124  $queryString = $_this->getQueryString();
125  if (!empty($queryString)) $url .= "?$queryString";
126  $_this->redirectUrl($url);
127  }
128 
132  function redirectNonSSL() {
133  $_this =& PKPRequest::_checkThis();
134 
135  $url = 'http://' . $_this->getServerHost() . $_this->getRequestPath();
136  $queryString = $_this->getQueryString();
137  if (!empty($queryString)) $url .= "?$queryString";
138  $_this->redirectUrl($url);
139  }
140 
145  function getIfModifiedSince() {
146  if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) return null;
147  return strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
148  }
149 
154  function getBaseUrl() {
155  $_this =& PKPRequest::_checkThis();
157  if (!isset($_this->_baseUrl)) {
158  $serverHost = $_this->getServerHost(null);
159  if ($serverHost !== null) {
160  // Auto-detection worked.
161  $_this->_baseUrl = $_this->getProtocol() . '://' . $_this->getServerHost() . $_this->getBasePath();
162  } else {
163  // Auto-detection didn't work (e.g. this is a command-line call); use configuration param
164  $_this->_baseUrl = Config::getVar('general', 'base_url');
165  }
166  HookRegistry::call('Request::getBaseUrl', array(&$_this->_baseUrl));
167  }
169  return $_this->_baseUrl;
170  }
171 
176  function getBasePath() {
177  $_this =& PKPRequest::_checkThis();
178 
179  if (!isset($_this->_basePath)) {
180  $path = preg_replace('#/[^/]*$#', '', $_SERVER['SCRIPT_NAME']);
182  // Encode charcters which need to be encoded in a URL.
183  // Simply using rawurlencode() doesn't work because it
184  // also encodes characters which are valid in a URL (i.e. @, $).
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);
189  }
190  $_this->_basePath = implode('/', $parts);
191 
192  if ($_this->_basePath == '/' || $_this->_basePath == '\\') {
193  $_this->_basePath = '';
194  }
195  HookRegistry::call('Request::getBasePath', array(&$_this->_basePath));
196  }
197 
198  return $_this->_basePath;
199  }
200 
207  function encodeBasePathFragment($fragment) {
208  if (!preg_match('/[A-Za-z0-9-._~!$&\'()*+,;=:@]/', $fragment)) {
209  return rawurlencode($fragment);
210  }
211  return $fragment;
212  }
213 
218  function getIndexUrl() {
219  static $indexUrl;
220 
221  $_this =& PKPRequest::_checkThis();
222  if (!isset($indexUrl)) {
223  $indexUrl = $_this->_delegateToRouter('getIndexUrl');
224 
225  // Call legacy hook
226  HookRegistry::call('Request::getIndexUrl', array(&$indexUrl));
227  }
228 
229  return $indexUrl;
230  }
231 
236  function getCompleteUrl() {
237  $_this =& PKPRequest::_checkThis();
238 
239  static $completeUrl;
240 
241  if (!isset($completeUrl)) {
242  $completeUrl = $_this->getRequestUrl();
243  $queryString = $_this->getQueryString();
244  if (!empty($queryString)) $completeUrl .= "?$queryString";
245  HookRegistry::call('Request::getCompleteUrl', array(&$completeUrl));
246  }
247 
248  return $completeUrl;
249  }
250 
255  function getRequestUrl() {
256  $_this =& PKPRequest::_checkThis();
257 
258  static $requestUrl;
259 
260  if (!isset($requestUrl)) {
261  $requestUrl = $_this->getProtocol() . '://' . $_this->getServerHost() . $_this->getRequestPath();
262  HookRegistry::call('Request::getRequestUrl', array(&$requestUrl));
263  }
264 
265  return $requestUrl;
266  }
267 
272  function getQueryString() {
274 
275  static $queryString;
276 
277  if (!isset($queryString)) {
278  $queryString = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'';
279  HookRegistry::call('Request::getQueryString', array(&$queryString));
280  }
281 
282  return $queryString;
283  }
284 
289  function getQueryArray() {
290  $_this =& PKPRequest::_checkThis();
292  $queryString = $_this->getQueryString();
293  $queryArray = array();
294 
295  if (isset($queryString)) {
296  parse_str($queryString, $queryArray);
297  }
298 
299  return $queryArray;
300  }
301 
306  function getRequestPath() {
307  $_this =& PKPRequest::_checkThis();
309  if (!isset($_this->_requestPath)) {
310  if ($_this->isRestfulUrlsEnabled()) {
311  $_this->_requestPath = $_this->getBasePath();
312  } else {
313  $_this->_requestPath = $_SERVER['SCRIPT_NAME'];
314  }
315 
316  if ($_this->isPathInfoEnabled()) {
317  $_this->_requestPath .= isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
318  }
319  HookRegistry::call('Request::getRequestPath', array(&$_this->_requestPath));
320  }
321  return $_this->_requestPath;
322  }
323 
329  function getServerHost($default = 'localhost') {
330  $_this =& PKPRequest::_checkThis();
331 
332  if (!isset($_this->_serverHost)) {
333  $_this->_serverHost = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST']
334  : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']
335  : (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME']
336  : $default));
337  HookRegistry::call('Request::getServerHost', array(&$_this->_serverHost));
338  }
339  return $_this->_serverHost;
340  }
341 
346  function getProtocol() {
347  $_this =& PKPRequest::_checkThis();
348 
349  if (!isset($_this->_protocol)) {
350  $_this->_protocol = (!isset($_SERVER['HTTPS']) || strtolower_codesafe($_SERVER['HTTPS']) != 'on') ? 'http' : 'https';
351  HookRegistry::call('Request::getProtocol', array(&$_this->_protocol));
352  }
353  return $_this->_protocol;
354  }
355 
360  function getRequestMethod() {
362 
363  $requestMethod = (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '');
364  return $requestMethod;
365  }
366 
371  function isPost() {
372  $_this =& PKPRequest::_checkThis();
373 
374  return ($_this->getRequestMethod() == 'POST');
375  }
376 
381  function isGet() {
383 
384  return ($_this->getRequestMethod() == 'GET');
385  }
386 
391  function getRemoteAddr() {
393 
394  static $ipaddr;
395  if (!isset($ipaddr)) {
396  if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
397  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
398  } else if (isset($_SERVER['REMOTE_ADDR']) &&
399  preg_match_all('/([0-9.a-fA-F:]+)/', $_SERVER['REMOTE_ADDR'], $matches)) {
400  } else if (preg_match_all('/([0-9.a-fA-F:]+)/', getenv('REMOTE_ADDR'), $matches)) {
401  } else {
402  $ipaddr = '';
403  }
404 
405  if (!isset($ipaddr)) {
406  // If multiple addresses are listed, take the last. (Supports ipv6.)
407  $ipaddr = $matches[0][count($matches[0])-1];
408  }
409  HookRegistry::call('Request::getRemoteAddr', array(&$ipaddr));
410  }
411  return $ipaddr;
412  }
413 
418  function getRemoteDomain() {
419  $_this =& PKPRequest::_checkThis();
420 
421  static $remoteDomain;
422  if (!isset($remoteDomain)) {
423  $remoteDomain = null;
424  $remoteDomain = @getHostByAddr($_this->getRemoteAddr());
425  HookRegistry::call('Request::getRemoteDomain', array(&$remoteDomain));
426  }
427  return $remoteDomain;
428  }
429 
434  function getUserAgent() {
436 
437  if (!isset($this->_userAgent)) {
438  if (isset($_SERVER['HTTP_USER_AGENT'])) {
439  $this->_userAgent = $_SERVER['HTTP_USER_AGENT'];
440  }
441  if (!isset($this->_userAgent) || empty($this->_userAgent)) {
442  $this->_userAgent = getenv('HTTP_USER_AGENT');
443  }
444  if (!isset($this->_userAgent) || $this->_userAgent == false) {
445  $this->_userAgent = '';
446  }
447  HookRegistry::call('Request::getUserAgent', array(&$this->_userAgent));
448  }
449  return $this->_userAgent;
450  }
451 
456  function isBot() {
457  $_this =& PKPRequest::_checkThis();
458 
459  if (!isset($this->_isBot)) {
460  $userAgent = $_this->getUserAgent();
461  $this->_isBot = Core::isUserAgentBot($userAgent);
462  }
463  return $this->_isBot;
464  }
465 
469  function isPathInfoEnabled() {
471 
472  if (!isset($_this->_isPathInfoEnabled)) {
473  $_this->_isPathInfoEnabled = Config::getVar('general', 'disable_path_info')?false:true;
474  }
475  return $_this->_isPathInfoEnabled;
476  }
477 
481  function isRestfulUrlsEnabled() {
482  $_this =& PKPRequest::_checkThis();
483 
484  if (!isset($_this->_isRestfulUrlsEnabled)) {
485  $_this->_isRestfulUrlsEnabled = Config::getVar('general', 'restful_urls')?true:false;
486  }
487  return $_this->_isRestfulUrlsEnabled;
488  }
489 
494  function &getSite() {
496 
497  $site =& Registry::get('site', true, null);
498  if ($site === null) {
499  $siteDao = DAORegistry::getDAO('SiteDAO');
500  $site = $siteDao->getSite();
501  // PHP bug? This is needed for some reason or extra queries results.
502  Registry::set('site', $site);
503  }
504 
505  return $site;
506  }
507 
512  function &getSession() {
514 
515  $session =& Registry::get('session', true, null);
516 
517  if ($session === null) {
518  $sessionManager = SessionManager::getManager();
519  $session = $sessionManager->getUserSession();
520  }
521 
522  return $session;
523  }
524 
529  function &getUser() {
531 
532  $user =& Registry::get('user', true, null);
533  if ($user === null) {
534  $sessionManager = SessionManager::getManager();
535  $session = $sessionManager->getUserSession();
536  $user = $session->getUser();
537  }
538 
539  return $user;
540  }
541 
546  function getUserVar($key) {
547  $_this = PKPRequest::_checkThis();
549  // Get all vars (already cleaned)
550  $vars = $_this->getUserVars();
551 
552  if (isset($vars[$key])) {
553  return $vars[$key];
554  } else {
555  return null;
556  }
557  }
558 
563  function &getUserVars() {
564  $_this =& PKPRequest::_checkThis();
566  if (!isset($_this->_requestVars)) {
567  $_this->_requestVars = array_merge($_GET, $_POST);
568  $_this->cleanUserVar($_this->_requestVars);
569  }
570 
571  return $_this->_requestVars;
572  }
573 
586  function getUserDateVar($prefix, $defaultDay = null, $defaultMonth = null, $defaultYear = null, $defaultHour = 0, $defaultMinute = 0, $defaultSecond = 0) {
587  $_this =& PKPRequest::_checkThis();
588 
589  $monthPart = $_this->getUserVar($prefix . 'Month');
590  $dayPart = $_this->getUserVar($prefix . 'Day');
591  $yearPart = $_this->getUserVar($prefix . 'Year');
592  $hourPart = $_this->getUserVar($prefix . 'Hour');
593  $minutePart = $_this->getUserVar($prefix . 'Minute');
594  $secondPart = $_this->getUserVar($prefix . 'Second');
595 
596  switch ($_this->getUserVar($prefix . 'Meridian')) {
597  case 'pm':
598  if (is_numeric($hourPart) && $hourPart != 12) $hourPart += 12;
599  break;
600  case 'am':
601  default:
602  // Do nothing.
603  break;
604  }
605 
606  if (empty($dayPart)) $dayPart = $defaultDay;
607  if (empty($monthPart)) $monthPart = $defaultMonth;
608  if (empty($yearPart)) $yearPart = $defaultYear;
609  if (empty($hourPart)) $hourPart = $defaultHour;
610  if (empty($minutePart)) $minutePart = $defaultMinute;
611  if (empty($secondPart)) $secondPart = $defaultSecond;
612 
613  if (empty($monthPart) || empty($dayPart) || empty($yearPart)) return null;
614  return mktime($hourPart, $minutePart, $secondPart, $monthPart, $dayPart, $yearPart);
615  }
616 
622  function cleanUserVar(&$var) {
623  $_this =& PKPRequest::_checkThis();
624 
625  if (isset($var) && is_array($var)) {
626  foreach ($var as $key => $value) {
627  $_this->cleanUserVar($var[$key]);
628  }
629  } else if (isset($var)) {
630  $var = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($var) : $var);
631 
632  } else {
633  return null;
634  }
635  }
636 
641  function getCookieVar($key) {
642  $_this =& PKPRequest::_checkThis();
643 
644  if (isset($_COOKIE[$key])) {
645  $value = $_COOKIE[$key];
646  $_this->cleanUserVar($value);
647  return $value;
648  } else {
649  return null;
650  }
651  }
652 
658  function setCookieVar($key, $value, $expire = 0) {
659  $_this =& PKPRequest::_checkThis();
660 
661  setcookie($key, $value, $expire, $_this->getBasePath());
662  $_COOKIE[$key] = $value;
663  }
664 
675  function redirect($context = null, $page = null, $op = null, $path = null, $params = null, $anchor = null) {
676  $_this = PKPRequest::_checkThis();
677  $dispatcher = $_this->getDispatcher();
678  $_this->redirectUrl($dispatcher->url($_this, ROUTE_PAGE, $context, $page, $op, $path, $params, $anchor));
679  }
680 
685  function &getContext() {
686  $_this =& PKPRequest::_checkThis();
687  return $_this->_delegateToRouter('getContext');
688  }
689 
694  function getRequestedContextPath($contextLevel = null) {
695  $_this =& PKPRequest::_checkThis();
696 
697  // Emulate the old behavior of getRequestedContextPath for
698  // backwards compatibility.
699  if (is_null($contextLevel)) {
700  return $_this->_delegateToRouter('getRequestedContextPaths');
701  } else {
702  return array($_this->_delegateToRouter('getRequestedContextPath', $contextLevel));
703  }
704  }
705 
710  function getRequestedPage() {
712  return $_this->_delegateToRouter('getRequestedPage');
713  }
714 
719  function getRequestedOp() {
720  $_this =& PKPRequest::_checkThis();
721  return $_this->_delegateToRouter('getRequestedOp');
722  }
723 
728  function getRequestedArgs() {
729  $_this =& PKPRequest::_checkThis();
730  return $_this->_delegateToRouter('getRequestedArgs');
731  }
732 
737  function url($context = null, $page = null, $op = null, $path = null,
738  $params = null, $anchor = null, $escape = false) {
739  $_this =& PKPRequest::_checkThis();
740  return $_this->_delegateToRouter('url', $context, $page, $op, $path,
741  $params, $anchor, $escape);
742  }
743 
758  function &_checkThis() {
759  if (isset($this) && is_a($this, 'PKPRequest')) {
760  return $this;
761  } else {
762  // This call is deprecated. We don't trigger a
763  // deprecation error, though, as there are so
764  // many instances of this error that it has a
765  // performance impact and renders the error
766  // log virtually useless when deprecation
767  // warnings are switched on.
768  // FIXME: Fix enough instances of this error so that
769  // we can put a deprecation warning in here.
770  $instance =& Registry::get('request');
771  assert(!is_null($instance));
772  return $instance;
773  }
774  }
775 
789  function &_delegateToRouter($method) {
790  // This call is deprecated. We don't trigger a
791  // deprecation error, though, as there are so
792  // many instances of this error that it has a
793  // performance impact and renders the error
794  // log virtually useless when deprecation
795  // warnings are switched on.
796  // FIXME: Fix enough instances of this error so that
797  // we can put a deprecation warning in here.
798  $_this = PKPRequest::_checkThis();
799  $router = $_this->getRouter();
800 
801  if (is_null($router)) {
802  assert(false);
803  $nullValue = null;
804  return $nullValue;
805  }
806 
807  // Construct the method call
808  $callable = array($router, $method);
809 
810  // Get additional parameters but replace
811  // the first parameter (currently the
812  // method to be called) with the request
813  // as all router methods required the request
814  // as their first parameter.
815  $parameters = func_get_args();
816  $parameters[0] =& $_this;
817 
818  $returner = call_user_func_array($callable, $parameters);
819  return $returner;
820  }
821 }
822 
823 ?>
static & getDAO($name, $dbconn=null)
static isUserAgentBot($userAgent, $botRegexpsFile=USER_AGENTS_FILE)
Definition: Core.inc.php:151
static set($key, &$value)
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.
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
getUserDateVar($prefix, $defaultDay=null, $defaultMonth=null, $defaultYear=null, $defaultHour=0, $defaultMinute=0, $defaultSecond=0)
static call($hookName, $args=null)
url($context=null, $page=null, $op=null, $path=null, $params=null, $anchor=null, $escape=false)
setCookieVar($key, $value, $expire=0)
& _delegateToRouter($method)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
redirectUrlJson($url)
static cleanVar($var)
Definition: Core.inc.php:54
setDispatcher($dispatcher)
Class providing operations associated with HTTP requests.
encodeBasePathFragment($fragment)
cleanUserVar(&$var)
setRouter($router)
getCookieVar($key)