1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Application
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 JLog::add('JApplication is deprecated.', JLog::WARNING, 'deprecated');
15
16 /**
17 * Base class for a Joomla! application.
18 *
19 * Acts as a Factory class for application specific objects and provides many
20 * supporting API functions. Derived clases should supply the route(), dispatch()
21 * and render() functions.
22 *
23 * @since 1.5
24 * @deprecated 3.2 Use JApplicationCms instead unless specified otherwise
25 */
26 class JApplication extends JApplicationBase
27 {
28 /**
29 * The client identifier.
30 *
31 * @var integer
32 * @since 1.5
33 * @deprecated 3.2
34 */
35 protected $_clientId = null;
36
37 /**
38 * The application message queue.
39 *
40 * @var array
41 * @since 1.5
42 * @deprecated 3.2
43 */
44 protected $_messageQueue = array();
45
46 /**
47 * The name of the application.
48 *
49 * @var array
50 * @since 1.5
51 * @deprecated 3.2
52 */
53 protected $_name = null;
54
55 /**
56 * The scope of the application.
57 *
58 * @var string
59 * @since 1.5
60 * @deprecated 3.2
61 */
62 public $scope = null;
63
64 /**
65 * The time the request was made.
66 *
67 * @var string
68 * @since 1.5
69 * @deprecated 3.2
70 */
71 public $requestTime = null;
72
73 /**
74 * The time the request was made as Unix timestamp.
75 *
76 * @var integer
77 * @since 1.6
78 * @deprecated 3.2
79 */
80 public $startTime = null;
81
82 /**
83 * The application client object.
84 *
85 * @var JApplicationWebClient
86 * @since 3.0
87 * @deprecated 3.2
88 */
89 public $client;
90
91 /**
92 * JApplication instances container.
93 *
94 * @var JApplication[]
95 * @since 2.5
96 * @deprecated 3.2
97 */
98 protected static $instances = array();
99
100 /**
101 * Class constructor.
102 *
103 * @param array $config A configuration array including optional elements such as session
104 * session_name, clientId and others. This is not exhaustive.
105 *
106 * @since 1.5
107 * @deprecated 3.2
108 */
109 public function __construct($config = array())
110 {
111 // Set the view name.
112 $this->_name = $this->getName();
113
114 // Only set the clientId if available.
115 if (isset($config['clientId']))
116 {
117 $this->_clientId = $config['clientId'];
118 }
119
120 // Enable sessions by default.
121 if (!isset($config['session']))
122 {
123 $config['session'] = true;
124 }
125
126 // Create the input object
127 $this->input = new JInput;
128
129 $this->client = new JApplicationWebClient;
130
131 $this->loadDispatcher();
132
133 // Set the session default name.
134 if (!isset($config['session_name']))
135 {
136 $config['session_name'] = $this->_name;
137 }
138
139 // Set the default configuration file.
140 if (!isset($config['config_file']))
141 {
142 $config['config_file'] = 'configuration.php';
143 }
144
145 // Create the configuration object.
146 if (file_exists(JPATH_CONFIGURATION . '/' . $config['config_file']))
147 {
148 $this->_createConfiguration(JPATH_CONFIGURATION . '/' . $config['config_file']);
149 }
150
151 // Create the session if a session name is passed.
152 if ($config['session'] !== false)
153 {
154 $this->_createSession(JApplicationHelper::getHash($config['session_name']));
155 }
156
157 $this->requestTime = gmdate('Y-m-d H:i');
158
159 // Used by task system to ensure that the system doesn't go over time.
160 $this->startTime = JProfiler::getmicrotime();
161 }
162
163 /**
164 * Returns the global JApplicationCms object, only creating it if it
165 * doesn't already exist.
166 *
167 * @param mixed $client A client identifier or name.
168 * @param array $config An optional associative array of configuration settings.
169 * @param string $prefix A prefix for class names
170 *
171 * @return JApplicationCms A JApplicationCms object.
172 *
173 * @since 1.5
174 * @deprecated 3.2 Use JApplicationCms::getInstance() instead
175 * @note As of 3.2, this proxies to JApplicationCms::getInstance()
176 */
177 public static function getInstance($client, $config = array(), $prefix = 'J')
178 {
179 return JApplicationCms::getInstance($client);
180 }
181
182 /**
183 * Initialise the application.
184 *
185 * @param array $options An optional associative array of configuration settings.
186 *
187 * @return void
188 *
189 * @since 1.5
190 * @deprecated 3.2
191 */
192 public function initialise($options = array())
193 {
194 // Set the language in the class.
195 $config = JFactory::getConfig();
196
197 // Check that we were given a language in the array (since by default may be blank).
198 if (isset($options['language']))
199 {
200 $config->set('language', $options['language']);
201 }
202
203 // Set user specific editor.
204 $user = JFactory::getUser();
205 $editor = $user->getParam('editor', $this->get('editor'));
206
207 if (!JPluginHelper::isEnabled('editors', $editor))
208 {
209 $editor = $this->get('editor');
210
211 if (!JPluginHelper::isEnabled('editors', $editor))
212 {
213 $editor = 'none';
214 }
215 }
216
217 $config->set('editor', $editor);
218
219 // Trigger the onAfterInitialise event.
220 JPluginHelper::importPlugin('system');
221 $this->triggerEvent('onAfterInitialise');
222 }
223
224 /**
225 * Route the application.
226 *
227 * Routing is the process of examining the request environment to determine which
228 * component should receive the request. The component optional parameters
229 * are then set in the request object to be processed when the application is being
230 * dispatched.
231 *
232 * @return void
233 *
234 * @since 1.5
235 * @deprecated 3.2
236 */
237 public function route()
238 {
239 // Get the full request URI.
240 $uri = clone JUri::getInstance();
241
242 $router = $this->getRouter();
243 $result = $router->parse($uri);
244
245 foreach ($result as $key => $value)
246 {
247 $this->input->def($key, $value);
248 }
249
250 // Trigger the onAfterRoute event.
251 JPluginHelper::importPlugin('system');
252 $this->triggerEvent('onAfterRoute');
253 }
254
255 /**
256 * Dispatch the application.
257 *
258 * Dispatching is the process of pulling the option from the request object and
259 * mapping them to a component. If the component does not exist, it handles
260 * determining a default component to dispatch.
261 *
262 * @param string $component The component to dispatch.
263 *
264 * @return void
265 *
266 * @since 1.5
267 * @deprecated 3.2
268 */
269 public function dispatch($component = null)
270 {
271 $document = JFactory::getDocument();
272
273 $contents = JComponentHelper::renderComponent($component);
274 $document->setBuffer($contents, 'component');
275
276 // Trigger the onAfterDispatch event.
277 JPluginHelper::importPlugin('system');
278 $this->triggerEvent('onAfterDispatch');
279 }
280
281 /**
282 * Render the application.
283 *
284 * Rendering is the process of pushing the document buffers into the template
285 * placeholders, retrieving data from the document and pushing it into
286 * the JResponse buffer.
287 *
288 * @return void
289 *
290 * @since 1.5
291 * @deprecated 3.2
292 */
293 public function render()
294 {
295 $template = $this->getTemplate(true);
296
297 $params = array('template' => $template->template, 'file' => 'index.php', 'directory' => JPATH_THEMES, 'params' => $template->params);
298
299 // Parse the document.
300 $document = JFactory::getDocument();
301 $document->parse($params);
302
303 // Trigger the onBeforeRender event.
304 JPluginHelper::importPlugin('system');
305 $this->triggerEvent('onBeforeRender');
306
307 // Render the document.
308 $caching = ($this->get('caching') >= 2);
309 JResponse::setBody($document->render($caching, $params));
310
311 // Trigger the onAfterRender event.
312 $this->triggerEvent('onAfterRender');
313 }
314
315 /**
316 * Redirect to another URL.
317 *
318 * Optionally enqueues a message in the system message queue (which will be displayed
319 * the next time a page is loaded) using the enqueueMessage method. If the headers have
320 * not been sent the redirect will be accomplished using a "301 Moved Permanently"
321 * code in the header pointing to the new location. If the headers have already been
322 * sent this will be accomplished using a JavaScript statement.
323 *
324 * @param string $url The URL to redirect to. Can only be http/https URL
325 * @param string $msg An optional message to display on redirect.
326 * @param string $msgType An optional message type. Defaults to message.
327 * @param boolean $moved True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
328 *
329 * @return void Calls exit().
330 *
331 * @since 1.5
332 * @deprecated 3.2
333 *
334 * @see JApplication::enqueueMessage()
335 */
336 public function redirect($url, $msg = '', $msgType = 'message', $moved = false)
337 {
338 // Check for relative internal links.
339 if (preg_match('#^index2?\.php#', $url))
340 {
341 $url = JUri::base() . $url;
342 }
343
344 // Strip out any line breaks.
345 $url = preg_split("/[\r\n]/", $url);
346 $url = $url[0];
347
348 /*
349 * If we don't start with a http we need to fix this before we proceed.
350 * We could validly start with something else (e.g. ftp), though this would
351 * be unlikely and isn't supported by this API.
352 */
353 if (!preg_match('#^http#i', $url))
354 {
355 $uri = JUri::getInstance();
356 $prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
357
358 if ($url[0] == '/')
359 {
360 // We just need the prefix since we have a path relative to the root.
361 $url = $prefix . $url;
362 }
363 else
364 {
365 // It's relative to where we are now, so lets add that.
366 $parts = explode('/', $uri->toString(array('path')));
367 array_pop($parts);
368 $path = implode('/', $parts) . '/';
369 $url = $prefix . $path . $url;
370 }
371 }
372
373 // If the message exists, enqueue it.
374 if (trim($msg))
375 {
376 $this->enqueueMessage($msg, $msgType);
377 }
378
379 // Persist messages if they exist.
380 if (count($this->_messageQueue))
381 {
382 $session = JFactory::getSession();
383 $session->set('application.queue', $this->_messageQueue);
384 }
385
386 // If the headers have been sent, then we cannot send an additional location header
387 // so we will output a javascript redirect statement.
388 if (headers_sent())
389 {
390 echo "<script>document.location.href='" . str_replace("'", ''', $url) . "';</script>\n";
391 }
392 else
393 {
394 $document = JFactory::getDocument();
395
396 jimport('phputf8.utils.ascii');
397
398 if (($this->client->engine == JApplicationWebClient::TRIDENT) && !utf8_is_ascii($url))
399 {
400 // MSIE type browser and/or server cause issues when URL contains utf8 character,so use a javascript redirect method
401 echo '<html><head><meta http-equiv="content-type" content="text/html; charset=' . $document->getCharset() . '" />'
402 . '<script>document.location.href=\'' . str_replace("'", ''', $url) . '\';</script></head></html>';
403 }
404 else
405 {
406 // All other browsers, use the more efficient HTTP header method
407 header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
408 header('Location: ' . $url);
409 header('Content-Type: text/html; charset=' . $document->getCharset());
410 }
411 }
412
413 $this->close();
414 }
415
416 /**
417 * Enqueue a system message.
418 *
419 * @param string $msg The message to enqueue.
420 * @param string $type The message type. Default is message.
421 *
422 * @return void
423 *
424 * @since 1.5
425 * @deprecated 3.2
426 */
427 public function enqueueMessage($msg, $type = 'message')
428 {
429 // For empty queue, if messages exists in the session, enqueue them first.
430 if (!count($this->_messageQueue))
431 {
432 $session = JFactory::getSession();
433 $sessionQueue = $session->get('application.queue');
434
435 if (count($sessionQueue))
436 {
437 $this->_messageQueue = $sessionQueue;
438 $session->set('application.queue', null);
439 }
440 }
441
442 // Enqueue the message.
443 $this->_messageQueue[] = array('message' => $msg, 'type' => strtolower($type));
444 }
445
446 /**
447 * Get the system message queue.
448 *
449 * @return array The system message queue.
450 *
451 * @since 1.5
452 * @deprecated 3.2
453 */
454 public function getMessageQueue()
455 {
456 // For empty queue, if messages exists in the session, enqueue them.
457 if (!count($this->_messageQueue))
458 {
459 $session = JFactory::getSession();
460 $sessionQueue = $session->get('application.queue');
461
462 if (count($sessionQueue))
463 {
464 $this->_messageQueue = $sessionQueue;
465 $session->set('application.queue', null);
466 }
467 }
468
469 return $this->_messageQueue;
470 }
471
472 /**
473 * Gets a configuration value.
474 *
475 * An example is in application/japplication-getcfg.php Getting a configuration
476 *
477 * @param string $varname The name of the value to get.
478 * @param string $default Default value to return
479 *
480 * @return mixed The user state.
481 *
482 * @since 1.5
483 * @deprecated 3.2
484 */
485 public function getCfg($varname, $default = null)
486 {
487 $config = JFactory::getConfig();
488
489 return $config->get('' . $varname, $default);
490 }
491
492 /**
493 * Method to get the application name.
494 *
495 * The dispatcher name is by default parsed using the classname, or it can be set
496 * by passing a $config['name'] in the class constructor.
497 *
498 * @return string The name of the dispatcher.
499 *
500 * @since 1.5
501 * @deprecated 3.2
502 */
503 public function getName()
504 {
505 $name = $this->_name;
506
507 if (empty($name))
508 {
509 $r = null;
510
511 if (!preg_match('/J(.*)/i', get_class($this), $r))
512 {
513 JLog::add(JText::_('JLIB_APPLICATION_ERROR_APPLICATION_GET_NAME'), JLog::WARNING, 'jerror');
514 }
515
516 $name = strtolower($r[1]);
517 }
518
519 return $name;
520 }
521
522 /**
523 * Gets a user state.
524 *
525 * @param string $key The path of the state.
526 * @param mixed $default Optional default value, returned if the internal value is null.
527 *
528 * @return mixed The user state or null.
529 *
530 * @since 1.5
531 * @deprecated 3.2
532 */
533 public function getUserState($key, $default = null)
534 {
535 $session = JFactory::getSession();
536 $registry = $session->get('registry');
537
538 if (!is_null($registry))
539 {
540 return $registry->get($key, $default);
541 }
542
543 return $default;
544 }
545
546 /**
547 * Sets the value of a user state variable.
548 *
549 * @param string $key The path of the state.
550 * @param string $value The value of the variable.
551 *
552 * @return mixed The previous state, if one existed.
553 *
554 * @since 1.5
555 * @deprecated 3.2
556 */
557 public function setUserState($key, $value)
558 {
559 $session = JFactory::getSession();
560 $registry = $session->get('registry');
561
562 if (!is_null($registry))
563 {
564 return $registry->set($key, $value);
565 }
566
567 return;
568 }
569
570 /**
571 * Gets the value of a user state variable.
572 *
573 * @param string $key The key of the user state variable.
574 * @param string $request The name of the variable passed in a request.
575 * @param string $default The default value for the variable if not found. Optional.
576 * @param string $type Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
577 *
578 * @return The request user state.
579 *
580 * @since 1.5
581 * @deprecated 3.2
582 */
583 public function getUserStateFromRequest($key, $request, $default = null, $type = 'none')
584 {
585 $cur_state = $this->getUserState($key, $default);
586 $new_state = $this->input->get($request, null, $type);
587
588 // Save the new value only if it was set in this request.
589 if ($new_state !== null)
590 {
591 $this->setUserState($key, $new_state);
592 }
593 else
594 {
595 $new_state = $cur_state;
596 }
597
598 return $new_state;
599 }
600
601 /**
602 * Login authentication function.
603 *
604 * Username and encoded password are passed the onUserLogin event which
605 * is responsible for the user validation. A successful validation updates
606 * the current session record with the user's details.
607 *
608 * Username and encoded password are sent as credentials (along with other
609 * possibilities) to each observer (authentication plugin) for user
610 * validation. Successful validation will update the current session with
611 * the user details.
612 *
613 * @param array $credentials Array('username' => string, 'password' => string)
614 * @param array $options Array('remember' => boolean)
615 *
616 * @return boolean|JException True on success, false if failed or silent handling is configured, or a JException object on authentication error.
617 *
618 * @since 1.5
619 * @deprecated 3.2
620 */
621 public function login($credentials, $options = array())
622 {
623 JPluginHelper::importPlugin('user');
624
625 // Get the global JAuthentication object.
626 $authenticate = JAuthentication::getInstance();
627 $response = $authenticate->authenticate($credentials, $options);
628
629 if ($response->status === JAuthentication::STATUS_SUCCESS)
630 {
631 // Validate that the user should be able to login (different to being authenticated).
632 // This permits authentication plugins blocking the user
633 $authorisations = $authenticate->authorise($response, $options);
634
635 foreach ($authorisations as $authorisation)
636 {
637 $denied_states = array(JAuthentication::STATUS_EXPIRED, JAuthentication::STATUS_DENIED);
638
639 if (in_array($authorisation->status, $denied_states))
640 {
641 // Trigger onUserAuthorisationFailure Event.
642 $this->triggerEvent('onUserAuthorisationFailure', array((array) $authorisation));
643
644 // If silent is set, just return false.
645 if (isset($options['silent']) && $options['silent'])
646 {
647 return false;
648 }
649
650 // Return the error.
651 switch ($authorisation->status)
652 {
653 case JAuthentication::STATUS_EXPIRED:
654 return JError::raiseWarning('102002', JText::_('JLIB_LOGIN_EXPIRED'));
655 break;
656
657 case JAuthentication::STATUS_DENIED:
658 return JError::raiseWarning('102003', JText::_('JLIB_LOGIN_DENIED'));
659 break;
660
661 default:
662 return JError::raiseWarning('102004', JText::_('JLIB_LOGIN_AUTHORISATION'));
663 break;
664 }
665 }
666 }
667
668 // Import the user plugin group.
669 JPluginHelper::importPlugin('user');
670
671 // OK, the credentials are authenticated and user is authorised. Let's fire the onLogin event.
672 $results = $this->triggerEvent('onUserLogin', array((array) $response, $options));
673
674 /*
675 * If any of the user plugins did not successfully complete the login routine
676 * then the whole method fails.
677 *
678 * Any errors raised should be done in the plugin as this provides the ability
679 * to provide much more information about why the routine may have failed.
680 */
681 $user = JFactory::getUser();
682
683 if ($response->type == 'Cookie')
684 {
685 $user->set('cookieLogin', true);
686 }
687
688 if (in_array(false, $results, true) == false)
689 {
690 $options['user'] = $user;
691 $options['responseType'] = $response->type;
692
693 if (isset($response->length) && isset($response->secure) && isset($response->lifetime))
694 {
695 $options['length'] = $response->length;
696 $options['secure'] = $response->secure;
697 $options['lifetime'] = $response->lifetime;
698 }
699
700 // The user is successfully logged in. Run the after login events
701 $this->triggerEvent('onUserAfterLogin', array($options));
702 }
703
704 return true;
705 }
706
707 // Trigger onUserLoginFailure Event.
708 $this->triggerEvent('onUserLoginFailure', array((array) $response));
709
710 // If silent is set, just return false.
711 if (isset($options['silent']) && $options['silent'])
712 {
713 return false;
714 }
715
716 // If status is success, any error will have been raised by the user plugin
717 if ($response->status !== JAuthentication::STATUS_SUCCESS)
718 {
719 JLog::add($response->error_message, JLog::WARNING, 'jerror');
720 }
721
722 return false;
723 }
724
725 /**
726 * Logout authentication function.
727 *
728 * Passed the current user information to the onUserLogout event and reverts the current
729 * session record back to 'anonymous' parameters.
730 * If any of the authentication plugins did not successfully complete
731 * the logout routine then the whole method fails. Any errors raised
732 * should be done in the plugin as this provides the ability to give
733 * much more information about why the routine may have failed.
734 *
735 * @param integer $userid The user to load - Can be an integer or string - If string, it is converted to ID automatically
736 * @param array $options Array('clientid' => array of client id's)
737 *
738 * @return boolean True on success
739 *
740 * @since 1.5
741 * @deprecated 3.2
742 */
743 public function logout($userid = null, $options = array())
744 {
745 // Get a user object from the JApplication.
746 $user = JFactory::getUser($userid);
747
748 // Build the credentials array.
749 $parameters['username'] = $user->get('username');
750 $parameters['id'] = $user->get('id');
751
752 // Set clientid in the options array if it hasn't been set already.
753 if (!isset($options['clientid']))
754 {
755 $options['clientid'] = $this->getClientId();
756 }
757
758 // Import the user plugin group.
759 JPluginHelper::importPlugin('user');
760
761 // OK, the credentials are built. Lets fire the onLogout event.
762 $results = $this->triggerEvent('onUserLogout', array($parameters, $options));
763
764 if (!in_array(false, $results, true))
765 {
766 $options['username'] = $user->get('username');
767 $this->triggerEvent('onUserAfterLogout', array($options));
768
769 return true;
770 }
771
772 // Trigger onUserLoginFailure Event.
773 $this->triggerEvent('onUserLogoutFailure', array($parameters));
774
775 return false;
776 }
777
778 /**
779 * Gets the name of the current template.
780 *
781 * @param boolean $params An optional associative array of configuration settings
782 *
783 * @return mixed System is the fallback.
784 *
785 * @since 1.5
786 * @deprecated 3.2
787 */
788 public function getTemplate($params = false)
789 {
790 $template = new stdClass;
791
792 $template->template = 'system';
793 $template->params = new Registry;
794
795 if ($params)
796 {
797 return $template;
798 }
799
800 return $template->template;
801 }
802
803 /**
804 * Returns the application JRouter object.
805 *
806 * @param string $name The name of the application.
807 * @param array $options An optional associative array of configuration settings.
808 *
809 * @return JRouter|null A JRouter object
810 *
811 * @since 1.5
812 * @deprecated 3.2
813 */
814 public static function getRouter($name = null, array $options = array())
815 {
816 if (!isset($name))
817 {
818 $app = JFactory::getApplication();
819 $name = $app->getName();
820 }
821
822 try
823 {
824 $router = JRouter::getInstance($name, $options);
825 }
826 catch (Exception $e)
827 {
828 return;
829 }
830
831 return $router;
832 }
833
834 /**
835 * This method transliterates a string into a URL
836 * safe string or returns a URL safe UTF-8 string
837 * based on the global configuration
838 *
839 * @param string $string String to process
840 *
841 * @return string Processed string
842 *
843 * @since 1.6
844 * @deprecated 3.2 Use JApplicationHelper::stringURLSafe instead
845 */
846 public static function stringURLSafe($string)
847 {
848 return JApplicationHelper::stringURLSafe($string);
849 }
850
851 /**
852 * Returns the application JPathway object.
853 *
854 * @param string $name The name of the application.
855 * @param array $options An optional associative array of configuration settings.
856 *
857 * @return JPathway|null A JPathway object
858 *
859 * @since 1.5
860 * @deprecated 3.2
861 */
862 public function getPathway($name = null, $options = array())
863 {
864 if (!isset($name))
865 {
866 $name = $this->_name;
867 }
868
869 try
870 {
871 $pathway = JPathway::getInstance($name, $options);
872 }
873 catch (Exception $e)
874 {
875 return;
876 }
877
878 return $pathway;
879 }
880
881 /**
882 * Returns the application JPathway object.
883 *
884 * @param string $name The name of the application/client.
885 * @param array $options An optional associative array of configuration settings.
886 *
887 * @return JMenu|null JMenu object.
888 *
889 * @since 1.5
890 * @deprecated 3.2
891 */
892 public function getMenu($name = null, $options = array())
893 {
894 if (!isset($name))
895 {
896 $name = $this->_name;
897 }
898
899 try
900 {
901 $menu = JMenu::getInstance($name, $options);
902 }
903 catch (Exception $e)
904 {
905 return;
906 }
907
908 return $menu;
909 }
910
911 /**
912 * Provides a secure hash based on a seed
913 *
914 * @param string $seed Seed string.
915 *
916 * @return string A secure hash
917 *
918 * @since 1.6
919 * @deprecated 3.2 Use JApplicationHelper::getHash instead
920 */
921 public static function getHash($seed)
922 {
923 return JApplicationHelper::getHash($seed);
924 }
925
926 /**
927 * Create the configuration registry.
928 *
929 * @param string $file The path to the configuration file
930 *
931 * @return JConfig A JConfig object
932 *
933 * @since 1.5
934 * @deprecated 3.2
935 */
936 protected function _createConfiguration($file)
937 {
938 JLoader::register('JConfig', $file);
939
940 // Create the JConfig object.
941 $config = new JConfig;
942
943 // Get the global configuration object.
944 $registry = JFactory::getConfig();
945
946 // Load the configuration values into the registry.
947 $registry->loadObject($config);
948
949 return $config;
950 }
951
952 /**
953 * Create the user session.
954 *
955 * Old sessions are flushed based on the configuration value for the cookie
956 * lifetime. If an existing session, then the last access time is updated.
957 * If a new session, a session id is generated and a record is created in
958 * the #__sessions table.
959 *
960 * @param string $name The sessions name.
961 *
962 * @return JSession JSession on success. May call exit() on database error.
963 *
964 * @since 1.5
965 * @deprecated 3.2
966 */
967 protected function _createSession($name)
968 {
969 $options = array();
970 $options['name'] = $name;
971
972 switch ($this->_clientId)
973 {
974 case 0:
975 if ($this->get('force_ssl') == 2)
976 {
977 $options['force_ssl'] = true;
978 }
979 break;
980
981 case 1:
982 if ($this->get('force_ssl') >= 1)
983 {
984 $options['force_ssl'] = true;
985 }
986 break;
987 }
988
989 $this->registerEvent('onAfterSessionStart', array($this, 'afterSessionStart'));
990
991 $session = JFactory::getSession($options);
992 $session->initialise($this->input, $this->dispatcher);
993 $session->start();
994
995 // TODO: At some point we need to get away from having session data always in the db.
996
997 $db = JFactory::getDbo();
998
999 // Remove expired sessions from the database.
1000 $time = time();
1001
1002 if ($time % 2)
1003 {
1004 // The modulus introduces a little entropy, making the flushing less accurate
1005 // but fires the query less than half the time.
1006 $query = $db->getQuery(true)
1007 ->delete($db->quoteName('#__session'))
1008 ->where($db->quoteName('time') . ' < ' . $db->quote((int) ($time - $session->getExpire())));
1009
1010 $db->setQuery($query);
1011 $db->execute();
1012 }
1013
1014 // Check to see the the session already exists.
1015 $handler = $this->get('session_handler');
1016
1017 if (($handler != 'database' && ($time % 2 || $session->isNew()))
1018 || ($handler == 'database' && $session->isNew()))
1019 {
1020 $this->checkSession();
1021 }
1022
1023 return $session;
1024 }
1025
1026 /**
1027 * Checks the user session.
1028 *
1029 * If the session record doesn't exist, initialise it.
1030 * If session is new, create session variables
1031 *
1032 * @return void
1033 *
1034 * @since 1.6
1035 * @deprecated 3.2
1036 */
1037 public function checkSession()
1038 {
1039 $db = JFactory::getDbo();
1040 $session = JFactory::getSession();
1041 $user = JFactory::getUser();
1042
1043 $query = $db->getQuery(true)
1044 ->select($db->quoteName('session_id'))
1045 ->from($db->quoteName('#__session'))
1046 ->where($db->quoteName('session_id') . ' = ' . $db->quote($session->getId()));
1047
1048 $db->setQuery($query, 0, 1);
1049 $exists = $db->loadResult();
1050
1051 // If the session record doesn't exist initialise it.
1052 if (!$exists)
1053 {
1054 $query->clear();
1055
1056 if ($session->isNew())
1057 {
1058 $query->insert($db->quoteName('#__session'))
1059 ->columns($db->quoteName('session_id') . ', ' . $db->quoteName('client_id') . ', ' . $db->quoteName('time'))
1060 ->values($db->quote($session->getId()) . ', ' . (int) $this->getClientId() . ', ' . $db->quote((int) time()));
1061 $db->setQuery($query);
1062 }
1063 else
1064 {
1065 $query->insert($db->quoteName('#__session'))
1066 ->columns(
1067 $db->quoteName('session_id') . ', ' . $db->quoteName('client_id') . ', ' . $db->quoteName('guest') . ', ' .
1068 $db->quoteName('time') . ', ' . $db->quoteName('userid') . ', ' . $db->quoteName('username')
1069 )
1070 ->values(
1071 $db->quote($session->getId()) . ', ' . (int) $this->getClientId() . ', ' . (int) $user->get('guest') . ', ' .
1072 $db->quote((int) $session->get('session.timer.start')) . ', ' . (int) $user->get('id') . ', ' . $db->quote($user->get('username'))
1073 );
1074
1075 $db->setQuery($query);
1076 }
1077
1078 // If the insert failed, exit the application.
1079 try
1080 {
1081 $db->execute();
1082 }
1083 catch (RuntimeException $e)
1084 {
1085 jexit($e->getMessage());
1086 }
1087 }
1088 }
1089
1090 /**
1091 * After the session has been started we need to populate it with some default values.
1092 *
1093 * @return void
1094 *
1095 * @since 3.0
1096 * @deprecated 3.2
1097 */
1098 public function afterSessionStart()
1099 {
1100 $session = JFactory::getSession();
1101
1102 if ($session->isNew())
1103 {
1104 $session->set('registry', new Registry);
1105 $session->set('user', new JUser);
1106 }
1107 }
1108
1109 /**
1110 * Gets the client id of the current running application.
1111 *
1112 * @return integer A client identifier.
1113 *
1114 * @since 1.5
1115 * @deprecated 3.2
1116 */
1117 public function getClientId()
1118 {
1119 return $this->_clientId;
1120 }
1121
1122 /**
1123 * Is admin interface?
1124 *
1125 * @return boolean True if this application is administrator.
1126 *
1127 * @since 1.0.2
1128 * @deprecated 3.2
1129 */
1130 public function isAdmin()
1131 {
1132 return $this->isClient('administrator');
1133 }
1134
1135 /**
1136 * Is site interface?
1137 *
1138 * @return boolean True if this application is site.
1139 *
1140 * @since 1.5
1141 * @deprecated 3.2
1142 */
1143 public function isSite()
1144 {
1145 return $this->isClient('site');
1146 }
1147
1148 /**
1149 * Check the client interface by name.
1150 *
1151 * @param string $identifier String identifier for the application interface
1152 *
1153 * @return boolean True if this application is of the given type client interface.
1154 *
1155 * @since 3.7.0
1156 */
1157 public function isClient($identifier)
1158 {
1159 return $this->getName() == $identifier;
1160 }
1161
1162 /**
1163 * Method to determine if the host OS is Windows
1164 *
1165 * @return boolean True if Windows OS
1166 *
1167 * @since 1.6
1168 * @deprecated 4.0 Use the IS_WIN constant instead.
1169 */
1170 public static function isWinOs()
1171 {
1172 JLog::add('JApplication::isWinOS() is deprecated. Use the IS_WIN constant instead.', JLog::WARNING, 'deprecated');
1173
1174 return IS_WIN;
1175 }
1176
1177 /**
1178 * Determine if we are using a secure (SSL) connection.
1179 *
1180 * @return boolean True if using SSL, false if not.
1181 *
1182 * @since 3.0
1183 * @deprecated 3.2
1184 */
1185 public function isSSLConnection()
1186 {
1187 return (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) || getenv('SSL_PROTOCOL_VERSION');
1188 }
1189
1190 /**
1191 * Returns the response as a string.
1192 *
1193 * @return string The response
1194 *
1195 * @since 1.6
1196 * @deprecated 3.2
1197 */
1198 public function __toString()
1199 {
1200 $compress = $this->get('gzip', false);
1201
1202 return JResponse::toString($compress);
1203 }
1204 }
1205