10 defined(
'JPATH_PLATFORM') or die;
35 protected $_clientId = null;
44 protected $_messageQueue = array();
53 protected $_name = null;
71 public $requestTime = null;
80 public $startTime = null;
94 protected static $instances = array();
102 protected $useStrongEncryption =
false;
113 public function __construct($config = array())
116 $this->_name = $this->getName();
119 if (isset($config[
'clientId']))
121 $this->_clientId = $config[
'clientId'];
125 if (!isset($config[
'session']))
127 $config[
'session'] =
true;
131 $this->input =
new JInput;
135 $this->loadDispatcher();
138 if (!isset($config[
'session_name']))
140 $config[
'session_name'] = $this->_name;
144 if (!isset($config[
'config_file']))
146 $config[
'config_file'] =
'configuration.php';
150 if (file_exists(JPATH_CONFIGURATION .
'/' . $config[
'config_file']))
152 $this->_createConfiguration(JPATH_CONFIGURATION .
'/' . $config[
'config_file']);
156 if ($config[
'session'] !==
false)
158 $this->_createSession(self::getHash($config[
'session_name']));
161 $this->requestTime = gmdate(
'Y-m-d H:i');
181 public static function getInstance($client, $config = array(), $prefix =
'J')
183 return JApplicationCms::getInstance($client);
196 public function initialise($options = array())
202 if (isset($options[
'language']))
204 $config->set(
'language', $options[
'language']);
209 $editor = $user->getParam(
'editor', $this->getCfg(
'editor'));
211 if (!JPluginHelper::isEnabled(
'editors', $editor))
213 $editor = $this->getCfg(
'editor');
215 if (!JPluginHelper::isEnabled(
'editors', $editor))
221 $config->set(
'editor', $editor);
225 if (JPluginHelper::isEnabled(
'user',
'joomla'))
227 $userPlugin = JPluginHelper::getPlugin(
'user',
'joomla');
229 $userPluginParams->
loadString($userPlugin->params);
230 $useStrongEncryption = $userPluginParams->get(
'strong_passwords', 0);
232 $config->set(
'useStrongEncryption', $useStrongEncryption);
236 JPluginHelper::importPlugin(
'system');
237 $this->triggerEvent(
'onAfterInitialise');
253 public function route()
258 $router = $this->getRouter();
259 $result = $router->parse($uri);
261 foreach ($result as $key => $value)
263 $this->input->def($key, $value);
267 JPluginHelper::importPlugin(
'system');
268 $this->triggerEvent(
'onAfterRoute');
285 public function dispatch($component = null)
289 $contents = JComponentHelper::renderComponent($component);
290 $document->setBuffer($contents,
'component');
293 JPluginHelper::importPlugin(
'system');
294 $this->triggerEvent(
'onAfterDispatch');
309 public function render()
311 $template = $this->getTemplate(
true);
313 $params = array(
'template' => $template->template,
'file' =>
'index.php',
'directory' => JPATH_THEMES,
'params' => $template->params);
317 $document->parse($params);
320 JPluginHelper::importPlugin(
'system');
321 $this->triggerEvent(
'onBeforeRender');
324 $caching = ($this->getCfg(
'caching') >= 2) ?
true :
false;
328 $this->triggerEvent(
'onAfterRender');
352 public function redirect($url, $msg =
'', $msgType =
'message', $moved =
false)
355 if (preg_match(
'#^index2?\.php#', $url))
361 $url = preg_split(
"/[\r\n]/", $url);
369 if (!preg_match(
'#^http#i', $url))
372 $prefix = $uri->toString(array(
'scheme',
'user',
'pass',
'host',
'port'));
377 $url = $prefix . $url;
382 $parts = explode(
'/', $uri->toString(array(
'path')));
384 $path = implode(
'/', $parts) .
'/';
385 $url = $prefix . $path . $url;
392 $this->enqueueMessage($msg, $msgType);
396 if (count($this->_messageQueue))
399 $session->set(
'application.queue', $this->_messageQueue);
406 echo
"<script>document.location.href='" . str_replace(
"'",
"'", $url) .
"';</script>\n";
412 jimport(
'phputf8.utils.ascii');
417 echo
'<html><head><meta http-equiv="content-type" content="text/html; charset=' . $document->getCharset() .
'" />'
418 .
'<script>document.location.href=\'' . str_replace(
"'",
"'", $url) .
'\';</script></head></html>
';
422 // All other browsers, use the more efficient HTTP header method
423 header($moved ? 'HTTP/1.1 301 Moved Permanently
' : 'HTTP/1.1 303 See other
');
424 header('Location:
' . $url);
425 header('Content-Type: text/html; charset=
' . $document->getCharset());
443 public function enqueueMessage($msg, $type = 'message
')
445 // For empty queue, if messages exists in the session, enqueue them first.
446 if (!count($this->_messageQueue))
448 $session = JFactory::getSession();
449 $sessionQueue = $session->get('application.queue
');
451 if (count($sessionQueue))
453 $this->_messageQueue = $sessionQueue;
454 $session->set('application.queue
', null);
458 // Enqueue the message.
459 $this->_messageQueue[] = array('message
' => $msg, 'type
' => strtolower($type));
470 public function getMessageQueue()
472 // For empty queue, if messages exists in the session, enqueue them.
473 if (!count($this->_messageQueue))
475 $session = JFactory::getSession();
476 $sessionQueue = $session->get('application.queue
');
478 if (count($sessionQueue))
480 $this->_messageQueue = $sessionQueue;
481 $session->set('application.queue
', null);
485 return $this->_messageQueue;
501 public function getCfg($varname, $default = null)
503 $config = JFactory::getConfig();
505 return $config->get('
' . $varname, $default);
519 public function getName()
521 $name = $this->_name;
527 if (!preg_match('/J(.*)/i
', get_class($this), $r))
529 JLog::add(JText::_('JLIB_APPLICATION_ERROR_APPLICATION_GET_NAME
'), JLog::WARNING, 'jerror
');
532 $name = strtolower($r[1]);
549 public function getUserState($key, $default = null)
551 $session = JFactory::getSession();
552 $registry = $session->get('registry
');
554 if (!is_null($registry))
556 return $registry->get($key, $default);
573 public function setUserState($key, $value)
575 $session = JFactory::getSession();
576 $registry = $session->get('registry
');
578 if (!is_null($registry))
580 return $registry->set($key, $value);
599 public function getUserStateFromRequest($key, $request, $default = null, $type = 'none
')
601 $cur_state = $this->getUserState($key, $default);
602 $new_state = $this->input->get($request, null, $type);
604 // Save the new value only if it was set in this request.
605 if ($new_state !== null)
607 $this->setUserState($key, $new_state);
611 $new_state = $cur_state;
637 public function login($credentials, $options = array())
639 // Get the global JAuthentication object.
640 jimport('joomla.user.authentication
');
642 $authenticate = JAuthentication::getInstance();
643 $response = $authenticate->authenticate($credentials, $options);
645 if ($response->status === JAuthentication::STATUS_SUCCESS)
647 // Validate that the user should be able to login (different to being authenticated).
648 // This permits authentication plugins blocking the user
649 $authorisations = $authenticate->authorise($response, $options);
651 foreach ($authorisations as $authorisation)
653 $denied_states = array(JAuthentication::STATUS_EXPIRED, JAuthentication::STATUS_DENIED);
655 if (in_array($authorisation->status, $denied_states))
657 // Trigger onUserAuthorisationFailure Event.
658 $this->triggerEvent('onUserAuthorisationFailure
', array((array) $authorisation));
660 // If silent is set, just return false.
661 if (isset($options['silent
']) && $options['silent
'])
667 switch ($authorisation->status)
669 case JAuthentication::STATUS_EXPIRED:
670 return JError::raiseWarning('102002
', JText::_('JLIB_LOGIN_EXPIRED
'));
673 case JAuthentication::STATUS_DENIED:
674 return JError::raiseWarning('102003
', JText::_('JLIB_LOGIN_DENIED
'));
678 return JError::raiseWarning('102004
', JText::_('JLIB_LOGIN_AUTHORISATION
'));
684 // Import the user plugin group.
685 JPluginHelper::importPlugin('user
');
687 // OK, the credentials are authenticated and user is authorised. Let's fire the onLogin
event.
688 $results = $this->triggerEvent(
'onUserLogin', array((array) $response, $options));
699 if ($response->type ==
'Cookie')
701 $user->set(
'cookieLogin',
true);
704 if (in_array(
false, $results,
true) ==
false)
706 $options[
'user'] = $user;
707 $options[
'responseType'] = $response->type;
709 if (isset($response->length) && isset($response->secure) && isset($response->lifetime))
711 $options[
'length'] = $response->length;
712 $options[
'secure'] = $response->secure;
713 $options[
'lifetime'] = $response->lifetime;
717 $this->triggerEvent(
'onUserAfterLogin', array($options));
724 $this->triggerEvent(
'onUserLoginFailure', array((array) $response));
727 if (isset($options[
'silent']) && $options[
'silent'])
759 public function logout($userid = null, $options = array())
765 $parameters[
'username'] = $user->get(
'username');
766 $parameters[
'id'] = $user->get(
'id');
769 if (!isset($options[
'clientid']))
771 $options[
'clientid'] = $this->getClientId();
775 JPluginHelper::importPlugin(
'user');
778 $results = $this->triggerEvent(
'onUserLogout', array($parameters, $options));
780 if (!in_array(
false, $results,
true))
782 $options[
'username'] = $user->get(
'username');
783 $results = $this->triggerEvent(
'onUserAfterLogout', array($options));
789 $this->triggerEvent(
'onUserLogoutFailure', array($parameters));
804 public function getTemplate($params =
false)
806 $template =
new stdClass;
808 $template->template =
'system';
816 return $template->template;
830 static public function getRouter($name = null, array $options = array())
835 $name = $app->getName();
840 $router = JRouter::getInstance($name, $options);
862 static public function stringURLSafe($string)
864 return JApplicationHelper::stringURLSafe($string);
878 public function getPathway($name = null, $options = array())
882 $name = $this->_name;
887 $pathway = JPathway::getInstance($name, $options);
908 public function getMenu($name = null, $options = array())
912 $name = $this->_name;
917 $menu = JMenu::getInstance($name, $options);
937 public static function getHash($seed)
939 return JApplicationHelper::getHash($seed);
952 protected function _createConfiguration($file)
957 $config =
new JConfig;
963 $registry->loadObject($config);
983 protected function _createSession($name)
986 $options[
'name'] = $name;
988 switch ($this->_clientId)
991 if ($this->getCfg(
'force_ssl') == 2)
993 $options[
'force_ssl'] =
true;
998 if ($this->getCfg(
'force_ssl') >= 1)
1000 $options[
'force_ssl'] =
true;
1005 $this->registerEvent(
'onAfterSessionStart', array($this,
'afterSessionStart'));
1008 $session->initialise($this->input, $this->dispatcher);
1022 $query = $db->getQuery(
true)
1023 ->delete($db->quoteName(
'#__session'))
1024 ->where($db->quoteName(
'time') .
' < ' . $db->quote((
int) ($time - $session->getExpire())));
1026 $db->setQuery($query);
1031 $handler = $this->getCfg(
'session_handler');
1033 if (($handler !=
'database' && ($time % 2 || $session->isNew()))
1034 || ($handler ==
'database' && $session->isNew()))
1036 $this->checkSession();
1053 public function checkSession()
1059 $query = $db->getQuery(
true)
1060 ->select($db->quoteName(
'session_id'))
1061 ->from($db->quoteName(
'#__session'))
1062 ->where($db->quoteName(
'session_id') .
' = ' . $db->quote($session->getId()));
1064 $db->setQuery($query, 0, 1);
1065 $exists = $db->loadResult();
1072 if ($session->isNew())
1074 $query->insert($db->quoteName(
'#__session'))
1075 ->columns($db->quoteName(
'session_id') .
', ' . $db->quoteName(
'client_id') .
', ' . $db->quoteName(
'time'))
1076 ->values($db->quote($session->getId()) .
', ' . (
int) $this->getClientId() .
', ' . $db->quote((
int) time()));
1077 $db->setQuery($query);
1081 $query->insert($db->quoteName(
'#__session'))
1083 $db->quoteName(
'session_id') .
', ' . $db->quoteName(
'client_id') .
', ' . $db->quoteName(
'guest') .
', ' .
1084 $db->quoteName(
'time') .
', ' . $db->quoteName(
'userid') .
', ' . $db->quoteName(
'username')
1087 $db->quote($session->getId()) .
', ' . (
int) $this->getClientId() .
', ' . (int) $user->get(
'guest') .
', ' .
1088 $db->quote((
int) $session->get(
'session.timer.start')) .
', ' . (
int) $user->get(
'id') .
', ' . $db->quote($user->get(
'username'))
1091 $db->setQuery($query);
1099 catch (RuntimeException $e)
1101 jexit($e->getMessage());
1114 public function afterSessionStart()
1118 if ($session->isNew())
1120 $session->set(
'registry',
new JRegistry(
'session'));
1121 $session->set(
'user',
new JUser);
1133 public function getClientId()
1135 return $this->_clientId;
1146 public function isAdmin()
1148 return ($this->_clientId == 1);
1159 public function isSite()
1161 return ($this->_clientId == 0);
1172 public static function isWinOS()
1174 JLog::add(
'JApplication::isWinOS() is deprecated. Use the IS_WIN constant instead.',
JLog::WARNING,
'deprecated');
1187 public function isSSLConnection()
1189 return ((isset($_SERVER[
'HTTPS']) && ($_SERVER[
'HTTPS'] ==
'on')) || getenv(
'SSL_PROTOCOL_VERSION'));
1200 public function __toString()
1202 $compress = $this->getCfg(
'gzip',
false);