10 defined(
'JPATH_PLATFORM') or die;
25 public $charSet =
'utf-8';
31 public $mimeType =
'text/html';
99 if ($input instanceof
JInput)
101 $this->input = $input;
106 $this->input =
new JInput;
112 $this->config = $config;
117 $this->config =
new JRegistry;
123 $this->client = $client;
128 $this->client =
new JApplicationWebClient;
132 $this->loadConfiguration($this->fetchConfigurationData());
135 $this->
set(
'execution.datetime', gmdate(
'Y-m-d H:i:s'));
136 $this->
set(
'execution.timestamp', time());
139 $this->response =
new stdClass;
140 $this->response->cachable =
false;
141 $this->response->headers = array();
142 $this->response->body = array();
145 $this->loadSystemUris();
159 public static function getInstance($name = null)
162 if (empty(self::$instance))
164 if (class_exists($name) && (is_subclass_of($name,
'JApplicationWeb')))
166 self::$instance =
new $name;
174 return self::$instance;
209 public function initialise($session = null, $document = null, $language = null, $dispatcher = null)
212 if ($session !==
false)
214 $this->loadSession($session);
218 if ($document !==
false)
220 $this->loadDocument($document);
224 if ($language !==
false)
226 $this->loadLanguage($language);
229 $this->loadDispatcher($dispatcher);
241 public function execute()
244 $this->triggerEvent(
'onBeforeExecute');
250 $this->triggerEvent(
'onAfterExecute');
253 if ($this->document instanceof
JDocument)
256 $this->triggerEvent(
'onBeforeRender');
262 $this->triggerEvent(
'onAfterRender');
266 if ($this->
get(
'gzip') && !ini_get(
'zlib.output_compression') && (ini_get(
'output_handler') !=
'ob_gzhandler'))
272 $this->triggerEvent(
'onBeforeRespond');
278 $this->triggerEvent(
'onAfterRespond');
291 protected function doExecute()
305 protected function render()
309 'template' => $this->
get(
'theme'),
310 'file' => $this->
get(
'themeFile',
'index.php'),
311 'params' => $this->
get(
'themeParams')
314 if ($this->
get(
'themes.base'))
316 $options[
'directory'] = $this->
get(
'themes.base');
321 $options[
'directory'] = defined(
'JPATH_THEMES') ? JPATH_THEMES : (defined(
'JPATH_BASE') ? JPATH_BASE : __DIR__) .
'/themes';
325 $this->document->parse($options);
328 $data = $this->document->render($this->
get(
'cache_enabled'), $options);
331 $this->setBody($data);
342 protected function compress()
348 'deflate' =>
'deflate'
352 $encodings = array_intersect($this->client->encodings, array_keys($supported));
355 if (empty($encodings))
361 if ($this->checkHeadersSent() || !$this->checkConnectionAlive())
367 foreach ($encodings as $encoding)
369 if (($supported[$encoding] ==
'gz') || ($supported[$encoding] ==
'deflate'))
373 if (!extension_loaded(
'zlib') || ini_get(
'zlib.output_compression'))
380 $data = $this->getBody();
381 $gzdata = gzencode($data, 4, ($supported[$encoding] ==
'gz') ? FORCE_GZIP : FORCE_DEFLATE);
385 if ($gzdata ===
false)
392 $this->setHeader(
'Content-Encoding', $encoding);
395 if ($this->
get(
'MetaVersion'))
397 $this->setHeader(
'X-Content-Encoded-By',
'Joomla');
401 $this->setBody($gzdata);
417 protected function respond()
420 $this->setHeader(
'Content-Type', $this->mimeType .
'; charset=' . $this->charSet);
423 if (!$this->response->cachable)
426 $this->setHeader(
'Expires',
'Mon, 1 Jan 2001 00:00:00 GMT',
true);
429 $this->setHeader(
'Last-Modified', gmdate(
'D, d M Y H:i:s') .
' GMT',
true);
430 $this->setHeader(
'Cache-Control',
'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
false);
433 $this->setHeader(
'Pragma',
'no-cache');
438 $this->setHeader(
'Expires', gmdate(
'D, d M Y H:i:s', time() + 900) .
' GMT');
441 if ($this->modifiedDate instanceof
JDate)
443 $this->setHeader(
'Last-Modified', $this->modifiedDate->format(
'D, d M Y H:i:s'));
447 $this->sendHeaders();
449 echo $this->getBody();
466 public function redirect($url, $moved =
false)
469 jimport(
'phputf8.utils.ascii');
472 if (preg_match(
'#^index\.php#', $url))
479 $url = preg_split(
"/[\r\n]/", $url);
487 if (!preg_match(
'#^[a-z]+\://#i', $url))
493 $prefix = $uri->toString(array(
'scheme',
'user',
'pass',
'host',
'port'));
498 $url = $prefix . $url;
503 $parts = explode(
'/', $uri->toString(array(
'path')));
505 $path = implode(
'/', $parts) .
'/';
506 $url = $prefix . $path . $url;
511 if ($this->checkHeadersSent())
513 echo
"<script>document.location.href='" . str_replace(
"'",
"'", $url) .
"';</script>\n";
520 $html =
'<html><head>';
521 $html .=
'<meta http-equiv="content-type" content="text/html; charset=' . $this->charSet .
'" />';
522 $html .=
'<script>document.location.href=\'' . str_replace(
"'",
"'", $url) .
'\';</script>
';
523 $html .= '</head><body></body></html>
';
529 // All other cases use the more efficient HTTP header for redirection.
530 $this->header($moved ? 'HTTP/1.1 301 Moved Permanently
' : 'HTTP/1.1 303 See other
');
531 $this->header('Location:
' . $url);
532 $this->header('Content-Type: text/html; charset=
' . $this->charSet);
536 // Close the application after the redirect.
549 public function loadConfiguration($data)
551 // Load the data into the configuration object.
554 $this->config->loadArray($data);
556 elseif (is_object($data))
558 $this->config->loadObject($data);
574 public function get($key, $default = null)
576 return $this->config->get($key, $default);
589 public function set($key, $value = null)
591 $previous = $this->config->get($key);
592 $this->config->set($key, $value);
607 public function allowCache($allow = null)
611 $this->response->cachable = (bool) $allow;
614 return $this->response->cachable;
630 public function setHeader($name, $value, $replace = false)
632 // Sanitize the input values.
633 $name = (string) $name;
634 $value = (string) $value;
636 // If the replace flag is set, unset all known headers with the given name.
639 foreach ($this->response->headers as $key => $header)
641 if ($name == $header['name
'])
643 unset($this->response->headers[$key]);
647 // Clean up the array as unsetting nested arrays leaves some junk.
648 $this->response->headers = array_values($this->response->headers);
651 // Add the header to the internal array.
652 $this->response->headers[] = array('name
' => $name, 'value
' => $value);
665 public function getHeaders()
667 return $this->response->headers;
677 public function clearHeaders()
679 $this->response->headers = array();
691 public function sendHeaders()
693 if (!$this->checkHeadersSent())
695 foreach ($this->response->headers as $header)
697 if ('status
' == strtolower($header['name
']))
699 // 'status
' headers indicate an HTTP status, and need to be handled slightly differently
700 $this->header(ucfirst(strtolower($header['name
'])) . ':
' . $header['value
'], null, (int) $header['value
']);
704 $this->header($header['name
'] . ':
' . $header['value
']);
721 public function setBody($content)
723 $this->response->body = array((string) $content);
737 public function prependBody($content)
739 array_unshift($this->response->body, (string) $content);
753 public function appendBody($content)
755 array_push($this->response->body, (string) $content);
769 public function getBody($asArray = false)
771 return $asArray ? $this->response->body : implode((array) $this->response->body);
781 public function getDocument()
783 return $this->document;
793 public function getLanguage()
795 return $this->language;
805 public function getSession()
807 return $this->session;
820 protected function checkConnectionAlive()
822 return (connection_status() === CONNECTION_NORMAL);
835 protected function checkHeadersSent()
837 return headers_sent();
847 protected function detectRequestUri()
849 // First we need to detect the URI scheme.
850 if (isset($_SERVER['HTTPS
']) && !empty($_SERVER['HTTPS
']) && (strtolower($_SERVER['HTTPS
']) != 'off
'))
866 if (!empty($_SERVER[
'PHP_SELF']) && !empty($_SERVER[
'REQUEST_URI']))
869 $uri = $scheme . $_SERVER[
'HTTP_HOST'] . $_SERVER[
'REQUEST_URI'];
875 $uri = $scheme . $_SERVER[
'HTTP_HOST'] . $_SERVER[
'SCRIPT_NAME'];
878 if (isset($_SERVER[
'QUERY_STRING']) && !empty($_SERVER[
'QUERY_STRING']))
880 $uri .=
'?' . $_SERVER[
'QUERY_STRING'];
901 protected function fetchConfigurationData($file =
'', $class =
'JConfig')
906 if (empty($file) && defined(
'JPATH_ROOT'))
908 $file = JPATH_ROOT .
'/configuration.php';
912 if (!file_exists($file))
922 if (class_exists($class))
924 $config =
new $class;
928 throw new RuntimeException(
'Configuration class does not exist.');
942 public function flushAssets()
944 $version =
new JVersion;
945 $version->refreshMediaVersion();
964 protected function header($string, $replace =
true, $code = null)
966 header($string, $replace, $code);
976 public function isSSLConnection()
978 return ((isset($_SERVER[
'HTTPS']) && ($_SERVER[
'HTTPS'] ==
'on')) || getenv(
'SSL_PROTOCOL_VERSION'));
994 public function loadDocument(
JDocument $document = null)
1036 if ($session !== null)
1038 $this->session = $session;
1044 $name = md5($this->
get(
'secret') . $this->
get(
'session_name', get_class($this)));
1047 $lifetime = (($this->
get(
'sess_lifetime')) ? $this->
get(
'sess_lifetime') * 60 : 900);
1050 $handler = $this->
get(
'sess_handler',
'none');
1055 'expire' => $lifetime,
1056 'force_ssl' => $this->
get(
'force_ssl')
1059 $this->registerEvent(
'onAfterSessionStart', array($this,
'afterSessionStart'));
1063 $session->initialise($this->input, $this->dispatcher);
1065 if ($session->getState() ==
'expired')
1067 $session->restart();
1075 $this->session = $session;
1087 public function afterSessionStart()
1091 if ($session->isNew())
1093 $session->set(
'registry',
new JRegistry(
'session'));
1094 $session->set(
'user',
new JUser);
1108 protected function loadSystemUris($requestUri = null)
1112 if (!empty($requestUri))
1114 $this->
set(
'uri.request', $requestUri);
1118 $this->
set(
'uri.request', $this->detectRequestUri());
1123 $siteUri = trim($this->
get(
'site_uri'));
1136 if (strpos(php_sapi_name(),
'cgi') !==
false && !ini_get(
'cgi.fix_pathinfo') && !empty($_SERVER[
'REQUEST_URI']))
1139 $uri->setPath(rtrim(dirname($_SERVER[
'PHP_SELF']),
'/\\'));
1144 $uri->setPath(rtrim(dirname($_SERVER[
'SCRIPT_NAME']),
'/\\'));
1148 $uri->setQuery(null);
1149 $uri->setFragment(null);
1153 $host = $uri->toString(array(
'scheme',
'user',
'pass',
'host',
'port'));
1154 $path = rtrim($uri->toString(array(
'path')),
'/\\');
1157 if (strpos($path,
'index.php') !==
false)
1160 $path = substr_replace($path,
'', strpos($path,
'index.php'), 9);
1161 $path = rtrim($path,
'/\\');
1165 $this->
set(
'uri.base.full', $host . $path .
'/');
1166 $this->
set(
'uri.base.host', $host);
1167 $this->
set(
'uri.base.path', $path .
'/');
1170 $this->
set(
'uri.route', substr_replace($this->
get(
'uri.request'),
'', 0, strlen($this->
get(
'uri.base.full'))));
1173 $mediaURI = trim($this->
get(
'media_uri'));
1177 if (strpos($mediaURI,
'://') !==
false)
1179 $this->
set(
'uri.media.full', $mediaURI);
1180 $this->
set(
'uri.media.path', $mediaURI);
1185 $mediaURI = trim($mediaURI,
'/\\');
1186 $mediaURI = !empty($mediaURI) ?
'/' . $mediaURI .
'/' :
'/';
1187 $this->
set(
'uri.media.full', $this->
get(
'uri.base.host') . $mediaURI);
1188 $this->
set(
'uri.media.path', $mediaURI);
1194 $this->
set(
'uri.media.full', $this->
get(
'uri.base.full') .
'media/');
1195 $this->
set(
'uri.media.path', $this->
get(
'uri.base.path') .
'media/');