00001 <?php
00002
00017
00018
00019
00020
00021 define('INDEX_SCRIPTNAME', 'index.php');
00022
00023 class Request {
00024
00030 function redirectUrl($url) {
00031 if (HookRegistry::call('Request::redirect', array(&$url))) {
00032 return;
00033 }
00034
00035 header("Location: $url");
00036 exit();
00037 }
00038
00048 function redirect($journalPath = null, $page = null, $op = null, $path = null, $params = null, $anchor = null) {
00049 Request::redirectUrl(Request::url($journalPath, $page, $op, $path, $params, $anchor));
00050 }
00051
00055 function redirectSSL() {
00056 $url = 'https://' . Request::getServerHost() . Request::getRequestPath();
00057 $queryString = Request::getQueryString();
00058 if (!empty($queryString)) $url .= "?$queryString";
00059 Request::redirectUrl($url);
00060 }
00061
00065 function redirectNonSSL() {
00066 $url = 'http://' . Request::getServerHost() . Request::getRequestPath();
00067 $queryString = Request::getQueryString();
00068 if (!empty($queryString)) $url .= "?$queryString";
00069 Request::redirectUrl($url);
00070 }
00071
00076 function getBaseUrl() {
00077 static $baseUrl;
00078
00079 if (!isset($baseUrl)) {
00080 $serverHost = Request::getServerHost(null);
00081 if ($serverHost !== null) {
00082
00083 $baseUrl = Request::getProtocol() . '://' . Request::getServerHost() . Request::getBasePath();
00084 } else {
00085
00086 $baseUrl = Config::getVar('general', 'base_url');
00087 }
00088 HookRegistry::call('Request::getBaseUrl', array(&$baseUrl));
00089 }
00090
00091 return $baseUrl;
00092 }
00093
00098 function getBasePath() {
00099 static $basePath;
00100
00101 if (!isset($basePath)) {
00102 $basePath = dirname($_SERVER['SCRIPT_NAME']);
00103 if ($basePath == '/' || $basePath == '\\') {
00104 $basePath = '';
00105 }
00106 HookRegistry::call('Request::getBasePath', array(&$basePath));
00107 }
00108
00109 return $basePath;
00110 }
00111
00116 function getIndexUrl() {
00117 static $indexUrl;
00118
00119 if (!isset($indexUrl)) {
00120 $indexUrl = Request::getBaseUrl() . '/' . INDEX_SCRIPTNAME;
00121 HookRegistry::call('Request::getIndexUrl', array(&$indexUrl));
00122 }
00123
00124 return $indexUrl;
00125 }
00126
00131 function getCompleteUrl() {
00132 static $completeUrl;
00133
00134 if (!isset($completeUrl)) {
00135 $completeUrl = Request::getRequestUrl();
00136 $queryString = Request::getQueryString();
00137 if (!empty($queryString)) $completeUrl .= "?$queryString";
00138 HookRegistry::call('Request::getCompleteUrl', array(&$completeUrl));
00139 }
00140
00141 return $completeUrl;
00142 }
00143
00148 function getRequestUrl() {
00149 static $requestUrl;
00150
00151 if (!isset($requestUrl)) {
00152 $requestUrl = Request::getProtocol() . '://' . Request::getServerHost() . Request::getRequestPath();
00153 HookRegistry::call('Request::getRequestUrl', array(&$requestUrl));
00154 }
00155
00156 return $requestUrl;
00157 }
00158
00163 function getQueryString() {
00164 static $queryString;
00165
00166 if (!isset($queryString)) {
00167 $queryString = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'';
00168 HookRegistry::call('Request::getQueryString', array(&$queryString));
00169 }
00170
00171 return $queryString;
00172 }
00173
00178 function getRequestPath() {
00179 static $requestPath;
00180 if (!isset($requestPath)) {
00181 $requestPath = $_SERVER['SCRIPT_NAME'];
00182 if (Request::isPathInfoEnabled()) {
00183 $requestPath .= isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
00184 }
00185 HookRegistry::call('Request::getRequestPath', array(&$requestPath));
00186 }
00187 return $requestPath;
00188 }
00189
00194 function getServerHost($default = 'localhost') {
00195 static $serverHost;
00196 if (!isset($serverHost)) {
00197 $serverHost = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST']
00198 : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']
00199 : (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME']
00200 : $default));
00201 HookRegistry::call('Request::getServerHost', array(&$serverHost));
00202 }
00203 return $serverHost;
00204 }
00205
00210 function getProtocol() {
00211 static $protocol;
00212 if (!isset($protocol)) {
00213 $protocol = (!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') ? 'http' : 'https';
00214 HookRegistry::call('Request::getProtocol', array(&$protocol));
00215 }
00216 return $protocol;
00217 }
00218
00223 function getRequestMethod() {
00224 return $_SERVER['REQUEST_METHOD'];
00225 }
00226
00231 function isPost() {
00232 return (Request::getRequestMethod() == 'POST');
00233 }
00234
00239 function isGet() {
00240 return (Request::getRequestMethod() == 'GET');
00241 }
00242
00247 function getRemoteAddr() {
00248 static $ipaddr;
00249 if (!isset($ipaddr)) {
00250 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
00251 $ipaddr = $_SERVER['HTTP_X_FORWARDED_FOR'];
00252 } else if (isset($_SERVER['REMOTE_ADDR'])) {
00253 $ipaddr = $_SERVER['REMOTE_ADDR'];
00254 }
00255 if (!isset($ipaddr) || empty($ipaddr)) {
00256 $ipaddr = getenv('REMOTE_ADDR');
00257 }
00258 if (!isset($ipaddr) || $ipaddr == false) {
00259 $ipaddr = '';
00260 }
00261
00262
00263 if (preg_match('/^([0-9.a-fA-F:]+)/', $ipaddr, $matches)) {
00264 $ipaddr = $matches[1];
00265 }
00266 HookRegistry::call('Request::getRemoteAddr', array(&$ipaddr));
00267 }
00268 return $ipaddr;
00269 }
00270
00275 function getRemoteDomain() {
00276 static $remoteDomain;
00277 if (!isset($remoteDomain)) {
00278 $remoteDomain = null;
00279 $remoteDomain = @getHostByAddr(Request::getRemoteAddr());
00280 HookRegistry::call('Request::getRemoteDomain', array(&$remoteDomain));
00281 }
00282 return $remoteDomain;
00283 }
00284
00289 function getUserAgent() {
00290 static $userAgent;
00291 if (!isset($userAgent)) {
00292 if (isset($_SERVER['HTTP_USER_AGENT'])) {
00293 $userAgent = $_SERVER['HTTP_USER_AGENT'];
00294 }
00295 if (!isset($userAgent) || empty($userAgent)) {
00296 $userAgent = getenv('HTTP_USER_AGENT');
00297 }
00298 if (!isset($userAgent) || $userAgent == false) {
00299 $userAgent = '';
00300 }
00301 HookRegistry::call('Request::getUserAgent', array(&$userAgent));
00302 }
00303 return $userAgent;
00304 }
00305
00310 function isBot() {
00311 static $isBot;
00312 if (!isset($isBot)) {
00313 $userAgent = Request::getUserAgent();
00314 $isBot = false;
00315 $userAgentsFile = Config::getVar('general', 'registry_dir') . DIRECTORY_SEPARATOR . 'botAgents.txt';
00316 $regexps = array_filter(file($userAgentsFile), create_function('&$a', 'return ($a = trim($a)) && !empty($a) && $a[0] != \'#\';'));
00317 foreach ($regexps as $regexp) {
00318 if (String::regexp_match($regexp, $userAgent)) {
00319 $isBot = true;
00320 return $isBot;
00321 }
00322 }
00323 }
00324 return $isBot;
00325 }
00326
00330 function isPathInfoEnabled() {
00331 static $isPathInfoEnabled;
00332 if (!isset($isPathInfoEnabled)) {
00333 $isPathInfoEnabled = Config::getVar('general', 'disable_path_info')?false:true;
00334 }
00335 return $isPathInfoEnabled;
00336 }
00337
00342 function getRequestedJournalPath() {
00343 static $journal;
00344
00345 if (!isset($journal)) {
00346 if (Request::isPathInfoEnabled()) {
00347 $journal = '';
00348 if (isset($_SERVER['PATH_INFO'])) {
00349 $vars = explode('/', $_SERVER['PATH_INFO']);
00350 if (count($vars) >= 2) {
00351 $journal = Core::cleanFileVar($vars[1]);
00352 }
00353 }
00354 } else {
00355 $journal = Request::getUserVar('journal');
00356 }
00357
00358 $journal = empty($journal) ? 'index' : $journal;
00359 HookRegistry::call('Request::getRequestedJournalPath', array(&$journal));
00360 }
00361
00362 return $journal;
00363 }
00364
00369 function &getSite() {
00370 static $site;
00371
00372 if (!isset($site)) {
00373 $siteDao = &DAORegistry::getDAO('SiteDAO');
00374 $site = $siteDao->getSite();
00375 }
00376
00377 return $site;
00378 }
00379
00384 function &getSession() {
00385 static $session;
00386
00387 if (!isset($session)) {
00388 $sessionManager = &SessionManager::getManager();
00389 $session = $sessionManager->getUserSession();
00390 }
00391
00392 return $session;
00393 }
00394
00399 function &getUser() {
00400 static $user;
00401
00402 if (!isset($user)) {
00403 $sessionManager = &SessionManager::getManager();
00404 $session = &$sessionManager->getUserSession();
00405 $user = $session->getUser();
00406 }
00407
00408 return $user;
00409 }
00410
00415 function &getJournal() {
00416 static $journal;
00417
00418 if (!isset($journal)) {
00419 $path = Request::getRequestedJournalPath();
00420 if ($path != 'index') {
00421 $journalDao = &DAORegistry::getDAO('JournalDAO');
00422 $journal = $journalDao->getJournalByPath(Request::getRequestedJournalPath());
00423 }
00424 }
00425
00426 return $journal;
00427 }
00428
00433 function getRequestedPage() {
00434 static $page;
00435
00436 if (!isset($page)) {
00437 if (Request::isPathInfoEnabled()) {
00438 $page = '';
00439 if (isset($_SERVER['PATH_INFO'])) {
00440 $vars = explode('/', $_SERVER['PATH_INFO']);
00441 if (count($vars) >= 3) {
00442 $page = Core::cleanFileVar($vars[2]);
00443 }
00444 }
00445 } else {
00446 $page = Request::getUserVar('page');
00447 }
00448 }
00449
00450 return $page;
00451 }
00452
00457 function getRequestedOp() {
00458 static $op;
00459
00460 if (!isset($op)) {
00461 if (Request::isPathInfoEnabled()) {
00462 $op = '';
00463 if (isset($_SERVER['PATH_INFO'])) {
00464 $vars = explode('/', $_SERVER['PATH_INFO']);
00465 if (count($vars) >= 4) {
00466 $op = Core::cleanFileVar($vars[3]);
00467 }
00468 }
00469 } else {
00470 return Request::getUserVar('op');
00471 }
00472 $op = empty($op) ? 'index' : $op;
00473 }
00474
00475 return $op;
00476 }
00477
00482 function getRequestedArgs() {
00483 if (Request::isPathInfoEnabled()) {
00484 $args = array();
00485 if (isset($_SERVER['PATH_INFO'])) {
00486 $vars = explode('/', $_SERVER['PATH_INFO']);
00487 if (count($vars) > 3) {
00488 $args = array_slice($vars, 4);
00489 for ($i=0, $count=count($args); $i<$count; $i++) {
00490 $args[$i] = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($args[$i]) : $args[$i]);
00491 }
00492 }
00493 }
00494 } else {
00495 $args = Request::getUserVar('path');
00496 if (empty($args)) $args = array();
00497 elseif (!is_array($args)) $args = array($args);
00498 }
00499 return $args;
00500 }
00501
00506 function getUserVar($key) {
00507 static $vars;
00508
00509 if (!isset($vars)) {
00510 $vars = array_merge($_GET, $_POST);
00511 }
00512
00513 if (isset($vars[$key])) {
00514
00515 Request::cleanUserVar($vars[$key]);
00516 return $vars[$key];
00517 } else {
00518 return null;
00519 }
00520 }
00521
00534 function getUserDateVar($prefix, $defaultDay = null, $defaultMonth = null, $defaultYear = null, $defaultHour = 0, $defaultMinute = 0, $defaultSecond = 0) {
00535 $monthPart = Request::getUserVar($prefix . 'Month');
00536 $dayPart = Request::getUserVar($prefix . 'Day');
00537 $yearPart = Request::getUserVar($prefix . 'Year');
00538 $hourPart = Request::getUserVar($prefix . 'Hour');
00539 $minutePart = Request::getUserVar($prefix . 'Minute');
00540 $secondPart = Request::getUserVar($prefix . 'Second');
00541
00542 switch (Request::getUserVar($prefix . 'Meridian')) {
00543 case 'pm':
00544 if (is_numeric($hourPart) && $hourPart != 12) $hourPart += 12;
00545 break;
00546 case 'am':
00547 default:
00548
00549 break;
00550 }
00551
00552 if (empty($dayPart)) $dayPart = $defaultDay;
00553 if (empty($monthPart)) $monthPart = $defaultMonth;
00554 if (empty($yearPart)) $yearPart = $defaultYear;
00555 if (empty($hourPart)) $hourPart = $defaultHour;
00556 if (empty($minutePart)) $minutePart = $defaultMinute;
00557 if (empty($secondPart)) $secondPart = $defaultSecond;
00558
00559 if (empty($monthPart) || empty($dayPart) || empty($yearPart)) return null;
00560 return mktime($hourPart, $minutePart, $secondPart, $monthPart, $dayPart, $yearPart);
00561 }
00562
00568 function cleanUserVar(&$var, $stripHtml = false) {
00569 if (isset($var) && is_array($var)) {
00570 foreach ($var as $key => $value) {
00571 Request::cleanUserVar($var[$key], $stripHtml);
00572 }
00573 } else if (isset($var)) {
00574 $var = Core::cleanVar(get_magic_quotes_gpc() ? stripslashes($var) : $var);
00575
00576 } else {
00577 return null;
00578 }
00579 }
00580
00585 function getCookieVar($key) {
00586 if (isset($_COOKIE[$key])) {
00587 $value = $_COOKIE[$key];
00588 Request::cleanUserVar($value);
00589 return $value;
00590 } else {
00591 return null;
00592 }
00593 }
00594
00600 function setCookieVar($key, $value) {
00601 setcookie($key, $value, 0, Request::getBasePath());
00602 $_COOKIE[$key] = $value;
00603 }
00604
00615 function url($journalPath = null, $page = null, $op = null, $path = null, $params = null, $anchor = null, $escape = false) {
00616 $pathInfoDisabled = !Request::isPathInfoEnabled();
00617
00618 $amp = $escape?'&':'&';
00619 $prefix = $pathInfoDisabled?$amp:'?';
00620
00621
00622 $defaultPage = Request::getRequestedPage();
00623 $defaultOp = Request::getRequestedOp();
00624
00625
00626
00627 if ($journalPath) {
00628 $journalPath = rawurlencode($journalPath);
00629 $defaultPage = null;
00630 $defaultOp = null;
00631 } else {
00632 $journal =& Request::getJournal();
00633 if ($journal) $journalPath = $journal->getPath();
00634 else $journalPath = 'index';
00635 }
00636
00637
00638 $overriddenBaseUrl = Config::getVar('general', "base_url[$journalPath]");
00639
00640
00641 if ($page) {
00642 $page = rawurlencode($page);
00643 $defaultOp = null;
00644 } else {
00645 $page = $defaultPage;
00646 }
00647
00648
00649 if ($op) $op = rawurlencode($op);
00650 else $op = $defaultOp;
00651
00652
00653 $additionalParams = '';
00654 if (!empty($params)) foreach ($params as $key => $value) {
00655 if (is_array($value)) foreach($value as $element) {
00656 $additionalParams .= $prefix . $key . '%5B%5D=' . rawurlencode($element);
00657 $prefix = $amp;
00658 } else {
00659 $additionalParams .= $prefix . $key . '=' . rawurlencode($value);
00660 $prefix = $amp;
00661 }
00662 }
00663
00664
00665 if (!empty($anchor)) $anchor = '#' . rawurlencode($anchor);
00666 else $anchor = '';
00667
00668 if (!empty($path)) {
00669 if (is_array($path)) $path = array_map('rawurlencode', $path);
00670 else $path = array(rawurlencode($path));
00671 if (!$page) $page = 'index';
00672 if (!$op) $op = 'index';
00673 }
00674
00675 $pathString = '';
00676 if ($pathInfoDisabled) {
00677 $joiner = $amp . 'path%5B%5D=';
00678 if (!empty($path)) $pathString = $joiner . implode($joiner, $path);
00679 if (empty($overriddenBaseUrl)) $baseParams = "?journal=$journalPath";
00680 else $baseParams = '';
00681
00682 if (!empty($page) || !empty($overriddenBaseUrl)) {
00683 $baseParams .= empty($baseParams)?'?':$amp . "page=$page";
00684 if (!empty($op)) {
00685 $baseParams .= $amp . "op=$op";
00686 }
00687 }
00688 } else {
00689 if (!empty($path)) $pathString = '/' . implode('/', $path);
00690 if (empty($overriddenBaseUrl)) $baseParams = "/$journalPath";
00691 else $baseParams = '';
00692
00693 if (!empty($page)) {
00694 $baseParams .= "/$page";
00695 if (!empty($op)) {
00696 $baseParams .= "/$op";
00697 }
00698 }
00699 }
00700
00701 return ((empty($overriddenBaseUrl)?Request::getIndexUrl():$overriddenBaseUrl) . $baseParams . $pathString . $additionalParams . $anchor);
00702 }
00703
00704 function isCacheable() {
00705 if (defined('SESSION_DISABLE_INIT')) return false;
00706 if (!Config::getVar('general', 'installed')) return false;
00707 if (!empty($_POST) || Validation::isLoggedIn()) return false;
00708 if (!Config::getVar('cache', 'web_cache')) return false;
00709 if (!Request::isPathInfoEnabled()) {
00710 $ok = array('journal', 'page', 'op', 'path');
00711 if (!empty($_GET) && count(array_diff(array_keys($_GET), $ok)) != 0) {
00712 return false;
00713 }
00714 } else {
00715 if (!empty($_GET)) return false;
00716 }
00717
00718 if (in_array(Request::getRequestedPage(), array(
00719 'about', 'announcement', 'help', 'index', 'information', 'rt', 'issue', ''
00720 ))) return true;
00721
00722 return false;
00723 }
00724
00725 function getCacheFilename() {
00726 static $cacheFilename;
00727 if (!isset($cacheFilename)) {
00728 if (Request::isPathInfoEnabled()) {
00729 $id = isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'index';
00730 $id .= '-' . Locale::getLocale();
00731 } else {
00732 $id = Request::getUserVar('journal') . '-' . Request::getUserVar('page') . '-' . Request::getUserVar('op') . '-' . Request::getUserVar('path') . '-' . Locale::getLocale();
00733 }
00734 $path = dirname(dirname(dirname(__FILE__)));
00735 $cacheFilename = $path . '/cache/wc-' . md5($id) . '.html';
00736 }
00737 return $cacheFilename;
00738 }
00739
00740 function cacheContent($contents) {
00741 $filename = Request::getCacheFilename();
00742 $fp = fopen($filename, 'w');
00743 if ($fp) {
00744 fwrite($fp, mktime() . ':' . $contents);
00745 fclose($fp);
00746 }
00747 return $contents;
00748 }
00749
00750 function displayCached() {
00751 $filename = Request::getCacheFilename();
00752 if (!file_exists($filename)) return false;
00753
00754 $fp = fopen($filename, 'r');
00755 $data = fread($fp, filesize($filename));
00756 fclose($fp);
00757
00758 $i = strpos($data, ':');
00759 $time = substr($data, 0, $i);
00760 $contents = substr($data, $i+1);
00761
00762 if (mktime() > $time + Config::getVar('cache', 'web_cache_hours') * 60 * 60) return false;
00763
00764 header('Content-Type: text/html; charset=' . Config::getVar('i18n', 'client_charset'));
00765
00766 echo $contents;
00767 return true;
00768 }
00769 }
00770
00771 ?>