Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
web.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Application
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Base class for a Joomla! Web application.
14  *
15  * @package Joomla.Platform
16  * @subpackage Application
17  * @since 11.4
18  */
20 {
21  /**
22  * @var string Character encoding string.
23  * @since 11.3
24  */
25  public $charSet = 'utf-8';
26 
27  /**
28  * @var string Response mime type.
29  * @since 11.3
30  */
31  public $mimeType = 'text/html';
32 
33  /**
34  * @var JDate The body modified date for response headers.
35  * @since 11.3
36  */
37  public $modifiedDate;
38 
39  /**
40  * @var JApplicationWebClient The application client object.
41  * @since 11.3
42  */
43  public $client;
44 
45  /**
46  * @var JRegistry The application configuration object.
47  * @since 11.3
48  */
49  protected $config;
50 
51  /**
52  * @var JDocument The application document object.
53  * @since 11.3
54  */
55  protected $document;
56 
57  /**
58  * @var JLanguage The application language object.
59  * @since 11.3
60  */
61  protected $language;
62 
63  /**
64  * @var JSession The application session object.
65  * @since 11.3
66  */
67  protected $session;
68 
69  /**
70  * @var object The application response object.
71  * @since 11.3
72  */
73  protected $response;
74 
75  /**
76  * @var JApplicationWeb The application instance.
77  * @since 11.3
78  */
79  protected static $instance;
80 
81  /**
82  * Class constructor.
83  *
84  * @param mixed $input An optional argument to provide dependency injection for the application's
85  * input object. If the argument is a JInput object that object will become
86  * the application's input object, otherwise a default input object is created.
87  * @param mixed $config An optional argument to provide dependency injection for the application's
88  * config object. If the argument is a JRegistry object that object will become
89  * the application's config object, otherwise a default config object is created.
90  * @param mixed $client An optional argument to provide dependency injection for the application's
91  * client object. If the argument is a JApplicationWebClient object that object will become
92  * the application's client object, otherwise a default client object is created.
93  *
94  * @since 11.3
95  */
96  public function __construct(JInput $input = null, JRegistry $config = null, JApplicationWebClient $client = null)
97  {
98  // If a input object is given use it.
99  if ($input instanceof JInput)
100  {
101  $this->input = $input;
102  }
103  // Create the input based on the application logic.
104  else
105  {
106  $this->input = new JInput;
107  }
108 
109  // If a config object is given use it.
110  if ($config instanceof JRegistry)
111  {
112  $this->config = $config;
113  }
114  // Instantiate a new configuration object.
115  else
116  {
117  $this->config = new JRegistry;
118  }
119 
120  // If a client object is given use it.
121  if ($client instanceof JApplicationWebClient)
122  {
123  $this->client = $client;
124  }
125  // Instantiate a new web client object.
126  else
127  {
128  $this->client = new JApplicationWebClient;
129  }
130 
131  // Load the configuration object.
132  $this->loadConfiguration($this->fetchConfigurationData());
133 
134  // Set the execution datetime and timestamp;
135  $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
136  $this->set('execution.timestamp', time());
137 
138  // Setup the response object.
139  $this->response = new stdClass;
140  $this->response->cachable = false;
141  $this->response->headers = array();
142  $this->response->body = array();
143 
144  // Set the system URIs.
145  $this->loadSystemUris();
146  }
147 
148  /**
149  * Returns a reference to the global JApplicationWeb object, only creating it if it doesn't already exist.
150  *
151  * This method must be invoked as: $web = JApplicationWeb::getInstance();
152  *
153  * @param string $name The name (optional) of the JApplicationWeb class to instantiate.
154  *
155  * @return JApplicationWeb
156  *
157  * @since 11.3
158  */
159  public static function getInstance($name = null)
160  {
161  // Only create the object if it doesn't exist.
162  if (empty(self::$instance))
163  {
164  if (class_exists($name) && (is_subclass_of($name, 'JApplicationWeb')))
165  {
166  self::$instance = new $name;
167  }
168  else
169  {
170  self::$instance = new JApplicationWeb;
171  }
172  }
173 
174  return self::$instance;
175  }
176 
177  /**
178  * Initialise the application.
179  *
180  * @param mixed $session An optional argument to provide dependency injection for the application's
181  * session object. If the argument is a JSession object that object will become
182  * the application's session object, if it is false then there will be no session
183  * object, and if it is null then the default session object will be created based
184  * on the application's loadSession() method.
185  * @param mixed $document An optional argument to provide dependency injection for the application's
186  * document object. If the argument is a JDocument object that object will become
187  * the application's document object, if it is false then there will be no document
188  * object, and if it is null then the default document object will be created based
189  * on the application's loadDocument() method.
190  * @param mixed $language An optional argument to provide dependency injection for the application's
191  * language object. If the argument is a JLanguage object that object will become
192  * the application's language object, if it is false then there will be no language
193  * object, and if it is null then the default language object will be created based
194  * on the application's loadLanguage() method.
195  * @param mixed $dispatcher An optional argument to provide dependency injection for the application's
196  * event dispatcher. If the argument is a JEventDispatcher object that object will become
197  * the application's event dispatcher, if it is null then the default event dispatcher
198  * will be created based on the application's loadDispatcher() method.
199  *
200  * @return JApplicationWeb Instance of $this to allow chaining.
201  *
202  * @deprecated 13.1 (Platform) & 4.0 (CMS)
203  * @see JApplicationWeb::loadSession()
204  * @see JApplicationWeb::loadDocument()
205  * @see JApplicationWeb::loadLanguage()
206  * @see JApplicationBase::loadDispatcher()
207  * @since 11.3
208  */
209  public function initialise($session = null, $document = null, $language = null, $dispatcher = null)
210  {
211  // Create the session based on the application logic.
212  if ($session !== false)
213  {
214  $this->loadSession($session);
215  }
216 
217  // Create the document based on the application logic.
218  if ($document !== false)
219  {
220  $this->loadDocument($document);
221  }
222 
223  // Create the language based on the application logic.
224  if ($language !== false)
225  {
226  $this->loadLanguage($language);
227  }
228 
229  $this->loadDispatcher($dispatcher);
230 
231  return $this;
232  }
233 
234  /**
235  * Execute the application.
236  *
237  * @return void
238  *
239  * @since 11.3
240  */
241  public function execute()
242  {
243  // Trigger the onBeforeExecute event.
244  $this->triggerEvent('onBeforeExecute');
245 
246  // Perform application routines.
247  $this->doExecute();
248 
249  // Trigger the onAfterExecute event.
250  $this->triggerEvent('onAfterExecute');
251 
252  // If we have an application document object, render it.
253  if ($this->document instanceof JDocument)
254  {
255  // Trigger the onBeforeRender event.
256  $this->triggerEvent('onBeforeRender');
257 
258  // Render the application output.
259  $this->render();
260 
261  // Trigger the onAfterRender event.
262  $this->triggerEvent('onAfterRender');
263  }
264 
265  // If gzip compression is enabled in configuration and the server is compliant, compress the output.
266  if ($this->get('gzip') && !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler'))
267  {
268  $this->compress();
269  }
270 
271  // Trigger the onBeforeRespond event.
272  $this->triggerEvent('onBeforeRespond');
273 
274  // Send the application response.
275  $this->respond();
276 
277  // Trigger the onAfterRespond event.
278  $this->triggerEvent('onAfterRespond');
279  }
280 
281  /**
282  * Method to run the Web application routines. Most likely you will want to instantiate a controller
283  * and execute it, or perform some sort of action that populates a JDocument object so that output
284  * can be rendered to the client.
285  *
286  * @return void
287  *
288  * @codeCoverageIgnore
289  * @since 11.3
290  */
291  protected function doExecute()
292  {
293  // Your application routines go here.
294  }
295 
296  /**
297  * Rendering is the process of pushing the document buffers into the template
298  * placeholders, retrieving data from the document and pushing it into
299  * the application response buffer.
300  *
301  * @return void
302  *
303  * @since 11.3
304  */
305  protected function render()
306  {
307  // Setup the document options.
308  $options = array(
309  'template' => $this->get('theme'),
310  'file' => $this->get('themeFile', 'index.php'),
311  'params' => $this->get('themeParams')
312  );
313 
314  if ($this->get('themes.base'))
315  {
316  $options['directory'] = $this->get('themes.base');
317  }
318  // Fall back to constants.
319  else
320  {
321  $options['directory'] = defined('JPATH_THEMES') ? JPATH_THEMES : (defined('JPATH_BASE') ? JPATH_BASE : __DIR__) . '/themes';
322  }
323 
324  // Parse the document.
325  $this->document->parse($options);
326 
327  // Render the document.
328  $data = $this->document->render($this->get('cache_enabled'), $options);
329 
330  // Set the application output data.
331  $this->setBody($data);
332  }
333 
334  /**
335  * Checks the accept encoding of the browser and compresses the data before
336  * sending it to the client if possible.
337  *
338  * @return void
339  *
340  * @since 11.3
341  */
342  protected function compress()
343  {
344  // Supported compression encodings.
345  $supported = array(
346  'x-gzip' => 'gz',
347  'gzip' => 'gz',
348  'deflate' => 'deflate'
349  );
350 
351  // Get the supported encoding.
352  $encodings = array_intersect($this->client->encodings, array_keys($supported));
353 
354  // If no supported encoding is detected do nothing and return.
355  if (empty($encodings))
356  {
357  return;
358  }
359 
360  // Verify that headers have not yet been sent, and that our connection is still alive.
361  if ($this->checkHeadersSent() || !$this->checkConnectionAlive())
362  {
363  return;
364  }
365 
366  // Iterate through the encodings and attempt to compress the data using any found supported encodings.
367  foreach ($encodings as $encoding)
368  {
369  if (($supported[$encoding] == 'gz') || ($supported[$encoding] == 'deflate'))
370  {
371  // Verify that the server supports gzip compression before we attempt to gzip encode the data.
372  // @codeCoverageIgnoreStart
373  if (!extension_loaded('zlib') || ini_get('zlib.output_compression'))
374  {
375  continue;
376  }
377  // @codeCoverageIgnoreEnd
378 
379  // Attempt to gzip encode the data with an optimal level 4.
380  $data = $this->getBody();
381  $gzdata = gzencode($data, 4, ($supported[$encoding] == 'gz') ? FORCE_GZIP : FORCE_DEFLATE);
382 
383  // If there was a problem encoding the data just try the next encoding scheme.
384  // @codeCoverageIgnoreStart
385  if ($gzdata === false)
386  {
387  continue;
388  }
389  // @codeCoverageIgnoreEnd
390 
391  // Set the encoding headers.
392  $this->setHeader('Content-Encoding', $encoding);
393 
394  // Header will be removed at 4.0
395  if ($this->get('MetaVersion'))
396  {
397  $this->setHeader('X-Content-Encoded-By', 'Joomla');
398  }
399 
400  // Replace the output with the encoded data.
401  $this->setBody($gzdata);
402 
403  // Compression complete, let's break out of the loop.
404  break;
405  }
406  }
407  }
408 
409  /**
410  * Method to send the application response to the client. All headers will be sent prior to the main
411  * application output data.
412  *
413  * @return void
414  *
415  * @since 11.3
416  */
417  protected function respond()
418  {
419  // Send the content-type header.
420  $this->setHeader('Content-Type', $this->mimeType . '; charset=' . $this->charSet);
421 
422  // If the response is set to uncachable, we need to set some appropriate headers so browsers don't cache the response.
423  if (!$this->response->cachable)
424  {
425  // Expires in the past.
426  $this->setHeader('Expires', 'Mon, 1 Jan 2001 00:00:00 GMT', true);
427 
428  // Always modified.
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);
431 
432  // HTTP 1.0
433  $this->setHeader('Pragma', 'no-cache');
434  }
435  else
436  {
437  // Expires.
438  $this->setHeader('Expires', gmdate('D, d M Y H:i:s', time() + 900) . ' GMT');
439 
440  // Last modified.
441  if ($this->modifiedDate instanceof JDate)
442  {
443  $this->setHeader('Last-Modified', $this->modifiedDate->format('D, d M Y H:i:s'));
444  }
445  }
446 
447  $this->sendHeaders();
448 
449  echo $this->getBody();
450  }
451 
452  /**
453  * Redirect to another URL.
454  *
455  * If the headers have not been sent the redirect will be accomplished using a "301 Moved Permanently"
456  * or "303 See Other" code in the header pointing to the new location. If the headers have already been
457  * sent this will be accomplished using a JavaScript statement.
458  *
459  * @param string $url The URL to redirect to. Can only be http/https URL
460  * @param boolean $moved True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
461  *
462  * @return void
463  *
464  * @since 11.3
465  */
466  public function redirect($url, $moved = false)
467  {
468  // Import library dependencies.
469  jimport('phputf8.utils.ascii');
470 
471  // Check for relative internal links.
472  if (preg_match('#^index\.php#', $url))
473  {
474  // We changed this from "$this->get('uri.base.full') . $url" due to the inability to run the system tests with the original code
475  $url = JUri::base() . $url;
476  }
477 
478  // Perform a basic sanity check to make sure we don't have any CRLF garbage.
479  $url = preg_split("/[\r\n]/", $url);
480  $url = $url[0];
481 
482  /*
483  * Here we need to check and see if the URL is relative or absolute. Essentially, do we need to
484  * prepend the URL with our base URL for a proper redirect. The rudimentary way we are looking
485  * at this is to simply check whether or not the URL string has a valid scheme or not.
486  */
487  if (!preg_match('#^[a-z]+\://#i', $url))
488  {
489  // Get a JUri instance for the requested URI.
490  $uri = JUri::getInstance($this->get('uri.request'));
491 
492  // Get a base URL to prepend from the requested URI.
493  $prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
494 
495  // We just need the prefix since we have a path relative to the root.
496  if ($url[0] == '/')
497  {
498  $url = $prefix . $url;
499  }
500  // It's relative to where we are now, so lets add that.
501  else
502  {
503  $parts = explode('/', $uri->toString(array('path')));
504  array_pop($parts);
505  $path = implode('/', $parts) . '/';
506  $url = $prefix . $path . $url;
507  }
508  }
509 
510  // If the headers have already been sent we need to send the redirect statement via JavaScript.
511  if ($this->checkHeadersSent())
512  {
513  echo "<script>document.location.href='" . str_replace("'", "&apos;", $url) . "';</script>\n";
514  }
515  else
516  {
517  // We have to use a JavaScript redirect here because MSIE doesn't play nice with utf-8 URLs.
518  if (($this->client->engine == JApplicationWebClient::TRIDENT) && !utf8_is_ascii($url))
519  {
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("'", "&apos;", $url) . '\';</script>';
523  $html .= '</head><body></body></html>';
524 
525  echo $html;
526  }
527  else
528  {
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);
533  }
534  }
535 
536  // Close the application after the redirect.
537  $this->close();
538  }
539 
540  /**
541  * Load an object or array into the application configuration object.
542  *
543  * @param mixed $data Either an array or object to be loaded into the configuration object.
544  *
545  * @return JApplicationWeb Instance of $this to allow chaining.
546  *
547  * @since 11.3
548  */
549  public function loadConfiguration($data)
550  {
551  // Load the data into the configuration object.
552  if (is_array($data))
553  {
554  $this->config->loadArray($data);
555  }
556  elseif (is_object($data))
557  {
558  $this->config->loadObject($data);
559  }
560 
561  return $this;
562  }
563 
564  /**
565  * Returns a property of the object or the default value if the property is not set.
566  *
567  * @param string $key The name of the property.
568  * @param mixed $default The default value (optional) if none is set.
569  *
570  * @return mixed The value of the configuration.
571  *
572  * @since 11.3
573  */
574  public function get($key, $default = null)
575  {
576  return $this->config->get($key, $default);
577  }
578 
579  /**
580  * Modifies a property of the object, creating it if it does not already exist.
581  *
582  * @param string $key The name of the property.
583  * @param mixed $value The value of the property to set (optional).
584  *
585  * @return mixed Previous value of the property
586  *
587  * @since 11.3
588  */
589  public function set($key, $value = null)
590  {
591  $previous = $this->config->get($key);
592  $this->config->set($key, $value);
593 
594  return $previous;
595  }
596 
597  /**
598  * Set/get cachable state for the response. If $allow is set, sets the cachable state of the
599  * response. Always returns the current state.
600  *
601  * @param boolean $allow True to allow browser caching.
602  *
603  * @return boolean
604  *
605  * @since 11.3
606  */
607  public function allowCache($allow = null)
608  {
609  if ($allow !== null)
610  {
611  $this->response->cachable = (bool) $allow;
612  }
613 
614  return $this->response->cachable;
615  }
616 
617  /**
618  * Method to set a response header. If the replace flag is set then all headers
619  * with the given name will be replaced by the new one. The headers are stored
620  * in an internal array to be sent when the site is sent to the browser.
621  *
622  * @param string $name The name of the header to set.
623  * @param string $value The value of the header to set.
624  * @param boolean $replace True to replace any headers with the same name.
625  *
626  * @return JApplicationWeb Instance of $this to allow chaining.
627  *
628  * @since 11.3
629  */
630  public function setHeader($name, $value, $replace = false)
631  {
632  // Sanitize the input values.
633  $name = (string) $name;
634  $value = (string) $value;
635 
636  // If the replace flag is set, unset all known headers with the given name.
637  if ($replace)
638  {
639  foreach ($this->response->headers as $key => $header)
640  {
641  if ($name == $header['name'])
642  {
643  unset($this->response->headers[$key]);
644  }
645  }
646 
647  // Clean up the array as unsetting nested arrays leaves some junk.
648  $this->response->headers = array_values($this->response->headers);
649  }
650 
651  // Add the header to the internal array.
652  $this->response->headers[] = array('name' => $name, 'value' => $value);
653 
654  return $this;
655  }
656 
657  /**
658  * Method to get the array of response headers to be sent when the response is sent
659  * to the client.
660  *
661  * @return array
662  *
663  * @since 11.3
664  */
665  public function getHeaders()
666  {
667  return $this->response->headers;
668  }
669 
670  /**
671  * Method to clear any set response headers.
672  *
673  * @return JApplicationWeb Instance of $this to allow chaining.
674  *
675  * @since 11.3
676  */
677  public function clearHeaders()
678  {
679  $this->response->headers = array();
680 
681  return $this;
682  }
683 
684  /**
685  * Send the response headers.
686  *
687  * @return JApplicationWeb Instance of $this to allow chaining.
688  *
689  * @since 11.3
690  */
691  public function sendHeaders()
692  {
693  if (!$this->checkHeadersSent())
694  {
695  foreach ($this->response->headers as $header)
696  {
697  if ('status' == strtolower($header['name']))
698  {
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']);
701  }
702  else
703  {
704  $this->header($header['name'] . ': ' . $header['value']);
705  }
706  }
707  }
708 
709  return $this;
710  }
711 
712  /**
713  * Set body content. If body content already defined, this will replace it.
714  *
715  * @param string $content The content to set as the response body.
716  *
717  * @return JApplicationWeb Instance of $this to allow chaining.
718  *
719  * @since 11.3
720  */
721  public function setBody($content)
722  {
723  $this->response->body = array((string) $content);
724 
725  return $this;
726  }
727 
728  /**
729  * Prepend content to the body content
730  *
731  * @param string $content The content to prepend to the response body.
732  *
733  * @return JApplicationWeb Instance of $this to allow chaining.
734  *
735  * @since 11.3
736  */
737  public function prependBody($content)
738  {
739  array_unshift($this->response->body, (string) $content);
740 
741  return $this;
742  }
743 
744  /**
745  * Append content to the body content
746  *
747  * @param string $content The content to append to the response body.
748  *
749  * @return JApplicationWeb Instance of $this to allow chaining.
750  *
751  * @since 11.3
752  */
753  public function appendBody($content)
754  {
755  array_push($this->response->body, (string) $content);
756 
757  return $this;
758  }
759 
760  /**
761  * Return the body content
762  *
763  * @param boolean $asArray True to return the body as an array of strings.
764  *
765  * @return mixed The response body either as an array or concatenated string.
766  *
767  * @since 11.3
768  */
769  public function getBody($asArray = false)
770  {
771  return $asArray ? $this->response->body : implode((array) $this->response->body);
772  }
773 
774  /**
775  * Method to get the application document object.
776  *
777  * @return JDocument The document object
778  *
779  * @since 11.3
780  */
781  public function getDocument()
782  {
783  return $this->document;
784  }
785 
786  /**
787  * Method to get the application language object.
788  *
789  * @return JLanguage The language object
790  *
791  * @since 11.3
792  */
793  public function getLanguage()
794  {
795  return $this->language;
796  }
797 
798  /**
799  * Method to get the application session object.
800  *
801  * @return JSession The session object
802  *
803  * @since 11.3
804  */
805  public function getSession()
806  {
807  return $this->session;
808  }
809 
810  /**
811  * Method to check the current client connnection status to ensure that it is alive. We are
812  * wrapping this to isolate the connection_status() function from our code base for testing reasons.
813  *
814  * @return boolean True if the connection is valid and normal.
815  *
816  * @codeCoverageIgnore
817  * @see connection_status()
818  * @since 11.3
819  */
820  protected function checkConnectionAlive()
821  {
822  return (connection_status() === CONNECTION_NORMAL);
823  }
824 
825  /**
826  * Method to check to see if headers have already been sent. We are wrapping this to isolate the
827  * headers_sent() function from our code base for testing reasons.
828  *
829  * @return boolean True if the headers have already been sent.
830  *
831  * @codeCoverageIgnore
832  * @see headers_sent()
833  * @since 11.3
834  */
835  protected function checkHeadersSent()
836  {
837  return headers_sent();
838  }
839 
840  /**
841  * Method to detect the requested URI from server environment variables.
842  *
843  * @return string The requested URI
844  *
845  * @since 11.3
846  */
847  protected function detectRequestUri()
848  {
849  // First we need to detect the URI scheme.
850  if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off'))
851  {
852  $scheme = 'https://';
853  }
854  else
855  {
856  $scheme = 'http://';
857  }
858 
859  /*
860  * There are some differences in the way that Apache and IIS populate server environment variables. To
861  * properly detect the requested URI we need to adjust our algorithm based on whether or not we are getting
862  * information from Apache or IIS.
863  */
864 
865  // If PHP_SELF and REQUEST_URI are both populated then we will assume "Apache Mode".
866  if (!empty($_SERVER['PHP_SELF']) && !empty($_SERVER['REQUEST_URI']))
867  {
868  // The URI is built from the HTTP_HOST and REQUEST_URI environment variables in an Apache environment.
869  $uri = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
870  }
871  // If not in "Apache Mode" we will assume that we are in an IIS environment and proceed.
872  else
873  {
874  // IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
875  $uri = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
876 
877  // If the QUERY_STRING variable exists append it to the URI string.
878  if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']))
879  {
880  $uri .= '?' . $_SERVER['QUERY_STRING'];
881  }
882  }
883 
884  return trim($uri);
885  }
886 
887  /**
888  * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
889  * will extend this method in child classes to provide configuration data from whatever data source is relevant
890  * for your specific application.
891  *
892  * @param string $file The path and filename of the configuration file. If not provided, configuration.php
893  * in JPATH_BASE will be used.
894  * @param string $class The class name to instantiate.
895  *
896  * @return mixed Either an array or object to be loaded into the configuration object.
897  *
898  * @since 11.3
899  * @throws RuntimeException
900  */
901  protected function fetchConfigurationData($file = '', $class = 'JConfig')
902  {
903  // Instantiate variables.
904  $config = array();
905 
906  if (empty($file) && defined('JPATH_ROOT'))
907  {
908  $file = JPATH_ROOT . '/configuration.php';
909 
910  // Applications can choose not to have any configuration data
911  // by not implementing this method and not having a config file.
912  if (!file_exists($file))
913  {
914  $file = '';
915  }
916  }
917 
918  if (!empty($file))
919  {
920  JLoader::register($class, $file);
921 
922  if (class_exists($class))
923  {
924  $config = new $class;
925  }
926  else
927  {
928  throw new RuntimeException('Configuration class does not exist.');
929  }
930  }
931 
932  return $config;
933  }
934 
935  /**
936  * Flush the media version to refresh versionable assets
937  *
938  * @return void
939  *
940  * @since 3.2
941  */
942  public function flushAssets()
943  {
944  $version = new JVersion;
945  $version->refreshMediaVersion();
946  }
947 
948  /**
949  * Method to send a header to the client. We are wrapping this to isolate the header() function
950  * from our code base for testing reasons.
951  *
952  * @param string $string The header string.
953  * @param boolean $replace The optional replace parameter indicates whether the header should
954  * replace a previous similar header, or add a second header of the same type.
955  * @param integer $code Forces the HTTP response code to the specified value. Note that
956  * this parameter only has an effect if the string is not empty.
957  *
958  * @return void
959  *
960  * @codeCoverageIgnore
961  * @see header()
962  * @since 11.3
963  */
964  protected function header($string, $replace = true, $code = null)
965  {
966  header($string, $replace, $code);
967  }
968 
969  /**
970  * Determine if we are using a secure (SSL) connection.
971  *
972  * @return boolean True if using SSL, false if not.
973  *
974  * @since 12.2
975  */
976  public function isSSLConnection()
977  {
978  return ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) || getenv('SSL_PROTOCOL_VERSION'));
979  }
980 
981  /**
982  * Allows the application to load a custom or default document.
983  *
984  * The logic and options for creating this object are adequately generic for default cases
985  * but for many applications it will make sense to override this method and create a document,
986  * if required, based on more specific needs.
987  *
988  * @param JDocument $document An optional document object. If omitted, the factory document is created.
989  *
990  * @return JApplicationWeb This method is chainable.
991  *
992  * @since 11.3
993  */
994  public function loadDocument(JDocument $document = null)
995  {
996  $this->document = ($document === null) ? JFactory::getDocument() : $document;
997 
998  return $this;
999  }
1000 
1001  /**
1002  * Allows the application to load a custom or default language.
1003  *
1004  * The logic and options for creating this object are adequately generic for default cases
1005  * but for many applications it will make sense to override this method and create a language,
1006  * if required, based on more specific needs.
1007  *
1008  * @param JLanguage $language An optional language object. If omitted, the factory language is created.
1009  *
1010  * @return JApplicationWeb This method is chainable.
1011  *
1012  * @since 11.3
1013  */
1014  public function loadLanguage(JLanguage $language = null)
1015  {
1016  $this->language = ($language === null) ? JFactory::getLanguage() : $language;
1017 
1018  return $this;
1019  }
1020 
1021  /**
1022  * Allows the application to load a custom or default session.
1023  *
1024  * The logic and options for creating this object are adequately generic for default cases
1025  * but for many applications it will make sense to override this method and create a session,
1026  * if required, based on more specific needs.
1027  *
1028  * @param JSession $session An optional session object. If omitted, the session is created.
1029  *
1030  * @return JApplicationWeb This method is chainable.
1031  *
1032  * @since 11.3
1033  */
1034  public function loadSession(JSession $session = null)
1035  {
1036  if ($session !== null)
1037  {
1038  $this->session = $session;
1039 
1040  return $this;
1041  }
1042 
1043  // Generate a session name.
1044  $name = md5($this->get('secret') . $this->get('session_name', get_class($this)));
1045 
1046  // Calculate the session lifetime.
1047  $lifetime = (($this->get('sess_lifetime')) ? $this->get('sess_lifetime') * 60 : 900);
1048 
1049  // Get the session handler from the configuration.
1050  $handler = $this->get('sess_handler', 'none');
1051 
1052  // Initialize the options for JSession.
1053  $options = array(
1054  'name' => $name,
1055  'expire' => $lifetime,
1056  'force_ssl' => $this->get('force_ssl')
1057  );
1058 
1059  $this->registerEvent('onAfterSessionStart', array($this, 'afterSessionStart'));
1060 
1061  // Instantiate the session object.
1062  $session = JSession::getInstance($handler, $options);
1063  $session->initialise($this->input, $this->dispatcher);
1064 
1065  if ($session->getState() == 'expired')
1066  {
1067  $session->restart();
1068  }
1069  else
1070  {
1071  $session->start();
1072  }
1073 
1074  // Set the session object.
1075  $this->session = $session;
1076 
1077  return $this;
1078  }
1079 
1080  /**
1081  * After the session has been started we need to populate it with some default values.
1082  *
1083  * @return void
1084  *
1085  * @since 12.2
1086  */
1087  public function afterSessionStart()
1088  {
1089  $session = JFactory::getSession();
1090 
1091  if ($session->isNew())
1092  {
1093  $session->set('registry', new JRegistry('session'));
1094  $session->set('user', new JUser);
1095  }
1096  }
1097 
1098  /**
1099  * Method to load the system URI strings for the application.
1100  *
1101  * @param string $requestUri An optional request URI to use instead of detecting one from the
1102  * server environment variables.
1103  *
1104  * @return void
1105  *
1106  * @since 11.3
1107  */
1108  protected function loadSystemUris($requestUri = null)
1109  {
1110  // Set the request URI.
1111  // @codeCoverageIgnoreStart
1112  if (!empty($requestUri))
1113  {
1114  $this->set('uri.request', $requestUri);
1115  }
1116  else
1117  {
1118  $this->set('uri.request', $this->detectRequestUri());
1119  }
1120  // @codeCoverageIgnoreEnd
1121 
1122  // Check to see if an explicit base URI has been set.
1123  $siteUri = trim($this->get('site_uri'));
1124 
1125  if ($siteUri != '')
1126  {
1127  $uri = JUri::getInstance($siteUri);
1128  }
1129  // No explicit base URI was set so we need to detect it.
1130  else
1131  {
1132  // Start with the requested URI.
1133  $uri = JUri::getInstance($this->get('uri.request'));
1134 
1135  // If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF.
1136  if (strpos(php_sapi_name(), 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI']))
1137  {
1138  // We aren't expecting PATH_INFO within PHP_SELF so this should work.
1139  $uri->setPath(rtrim(dirname($_SERVER['PHP_SELF']), '/\\'));
1140  }
1141  // Pretty much everything else should be handled with SCRIPT_NAME.
1142  else
1143  {
1144  $uri->setPath(rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\'));
1145  }
1146 
1147  // Clear the unused parts of the requested URI.
1148  $uri->setQuery(null);
1149  $uri->setFragment(null);
1150  }
1151 
1152  // Get the host and path from the URI.
1153  $host = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
1154  $path = rtrim($uri->toString(array('path')), '/\\');
1155 
1156  // Check if the path includes "index.php".
1157  if (strpos($path, 'index.php') !== false)
1158  {
1159  // Remove the index.php portion of the path.
1160  $path = substr_replace($path, '', strpos($path, 'index.php'), 9);
1161  $path = rtrim($path, '/\\');
1162  }
1163 
1164  // Set the base URI both as just a path and as the full URI.
1165  $this->set('uri.base.full', $host . $path . '/');
1166  $this->set('uri.base.host', $host);
1167  $this->set('uri.base.path', $path . '/');
1168 
1169  // Set the extended (non-base) part of the request URI as the route.
1170  $this->set('uri.route', substr_replace($this->get('uri.request'), '', 0, strlen($this->get('uri.base.full'))));
1171 
1172  // Get an explicitly set media URI is present.
1173  $mediaURI = trim($this->get('media_uri'));
1174 
1175  if ($mediaURI)
1176  {
1177  if (strpos($mediaURI, '://') !== false)
1178  {
1179  $this->set('uri.media.full', $mediaURI);
1180  $this->set('uri.media.path', $mediaURI);
1181  }
1182  else
1183  {
1184  // Normalise slashes.
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);
1189  }
1190  }
1191  // No explicit media URI was set, build it dynamically from the base uri.
1192  else
1193  {
1194  $this->set('uri.media.full', $this->get('uri.base.full') . 'media/');
1195  $this->set('uri.media.path', $this->get('uri.base.path') . 'media/');
1196  }
1197  }
1198 }