[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/includes/ -> application.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // no direct access
   8  defined('_JEXEC') or die;
   9  
  10  jimport('joomla.application.component.helper');
  11  
  12  /**
  13   * Joomla! Application class
  14   *
  15   * Provide many supporting API functions
  16   *
  17   * @package        Joomla.Site
  18   * @subpackage    Application
  19   */
  20  final class JSite extends JApplication
  21  {
  22      /**
  23       * Currently active template
  24       * @var object
  25       */
  26      private $template = null;
  27  
  28      /**
  29       * Option to filter by language
  30       */
  31      private $_language_filter = false;
  32  
  33      /**
  34       * Option to detect language by the browser
  35       */
  36      private $_detect_browser = false;
  37  
  38      /**
  39       * Class constructor
  40       *
  41       * @param    array An optional associative array of configuration settings.
  42       * Recognized key values include 'clientId' (this list is not meant to be comprehensive).
  43       */
  44  	public function __construct($config = array())
  45      {
  46          $config['clientId'] = 0;
  47          parent::__construct($config);
  48      }
  49  
  50      /**
  51       * Initialise the application.
  52       *
  53       * @param    array
  54       */
  55  	public function initialise($options = array())
  56      {
  57          $config = JFactory::getConfig();
  58  
  59          // if a language was specified it has priority
  60          // otherwise use user or default language settings
  61          JPluginHelper::importPlugin('system', 'languagefilter');
  62  
  63          if (empty($options['language'])) {
  64              $lang = JRequest::getString('language', null);
  65              if ($lang && JLanguage::exists($lang)) {
  66                  $options['language'] = $lang;
  67              }
  68          }
  69  
  70          if ($this->_language_filter && empty($options['language'])) {
  71              // Detect cookie language
  72              jimport('joomla.utilities.utility');
  73              $lang = JRequest::getString(self::getHash('language'), null , 'cookie');
  74              // Make sure that the user's language exists
  75              if ($lang && JLanguage::exists($lang)) {
  76                  $options['language'] = $lang;
  77              }
  78          }
  79  
  80          if (empty($options['language'])) {
  81              // Detect user language
  82              $lang = JFactory::getUser()->getParam('language');
  83              // Make sure that the user's language exists
  84              if ($lang && JLanguage::exists($lang)) {
  85                  $options['language'] = $lang;
  86              }
  87          }
  88  
  89          if ($this->_detect_browser && empty($options['language'])) {
  90              // Detect browser language
  91              $lang = JLanguageHelper::detectLanguage();
  92              // Make sure that the user's language exists
  93              if ($lang && JLanguage::exists($lang)) {
  94                  $options['language'] = $lang;
  95              }
  96          }
  97  
  98          if (empty($options['language'])) {
  99              // Detect default language
 100              $params =  JComponentHelper::getParams('com_languages');
 101              $client    = JApplicationHelper::getClientInfo($this->getClientId());
 102              $options['language'] = $params->get($client->name, $config->get('language', 'en-GB'));
 103          }
 104  
 105          // One last check to make sure we have something
 106          if (!JLanguage::exists($options['language'])) {
 107              $lang = $config->get('language', 'en-GB');
 108              if (JLanguage::exists($lang)) {
 109                  $options['language'] = $lang;
 110              }
 111              else {
 112                  $options['language'] = 'en-GB'; // as a last ditch fail to english
 113              }
 114          }
 115  
 116          // Execute the parent initialise method.
 117          parent::initialise($options);
 118  
 119          // Load Library language
 120          $lang = JFactory::getLanguage();
 121  
 122          // Try the lib_joomla file in the current language (without allowing the loading of the file in the default language)
 123          $lang->load('lib_joomla', JPATH_SITE, null, false, false)
 124          || $lang->load('lib_joomla', JPATH_ADMINISTRATOR, null, false, false)
 125          // Fallback to the lib_joomla file in the default language
 126          || $lang->load('lib_joomla', JPATH_SITE, null, true)
 127          || $lang->load('lib_joomla', JPATH_ADMINISTRATOR, null, true);
 128      }
 129  
 130      /**
 131       * Route the application.
 132       *
 133       */
 134  	public function route()
 135      {
 136          parent::route();
 137  
 138          $Itemid = JRequest::getInt('Itemid');
 139          $this->authorise($Itemid);
 140      }
 141  
 142      /**
 143       * Dispatch the application
 144       *
 145       * @param    string
 146       */
 147  	public function dispatch($component = null)
 148      {
 149          try
 150          {
 151              // Get the component if not set.
 152              if (!$component) {
 153                  $component = JRequest::getCmd('option');
 154              }
 155  
 156              $document    = JFactory::getDocument();
 157              $user        = JFactory::getUser();
 158              $router        = $this->getRouter();
 159              $params        = $this->getParams();
 160  
 161              switch($document->getType())
 162              {
 163                  case 'html':
 164                      // Get language
 165                      $lang_code = JFactory::getLanguage()->getTag();
 166                      $languages = JLanguageHelper::getLanguages('lang_code');
 167  
 168                      // Set metadata
 169                      if (isset($languages[$lang_code]) && $languages[$lang_code]->metakey) {
 170                          $document->setMetaData('keywords', $languages[$lang_code]->metakey);
 171                      } else {
 172                          $document->setMetaData('keywords', $this->getCfg('MetaKeys'));
 173                      }
 174                      $document->setMetaData('rights', $this->getCfg('MetaRights'));
 175                      if ($router->getMode() == JROUTER_MODE_SEF) {
 176                          $document->setBase(htmlspecialchars(JURI::current()));
 177                      }
 178                      break;
 179  
 180                  case 'feed':
 181                      $document->setBase(htmlspecialchars(JURI::current()));
 182                      break;
 183              }
 184  
 185              $document->setTitle($params->get('page_title'));
 186              $document->setDescription($params->get('page_description'));
 187  
 188              // Add version number or not based on global configuration
 189              if ($this->getCfg('MetaVersion', 0))
 190              {
 191                  $document->setGenerator('Joomla! - Open Source Content Management  - Version ' . JVERSION);
 192              }
 193              else
 194              {
 195                  $document->setGenerator('Joomla! - Open Source Content Management');
 196              }
 197  
 198              $contents = JComponentHelper::renderComponent($component);
 199              $document->setBuffer($contents, 'component');
 200  
 201              // Trigger the onAfterDispatch event.
 202              JPluginHelper::importPlugin('system');
 203              $this->triggerEvent('onAfterDispatch');
 204          }
 205          // Mop up any uncaught exceptions.
 206          catch (Exception $e)
 207          {
 208              $code = $e->getCode();
 209              JError::raiseError($code ? $code : 500, $e->getMessage());
 210          }
 211      }
 212  
 213      /**
 214       * Display the application.
 215       */
 216  	public function render()
 217      {
 218          $document    = JFactory::getDocument();
 219          $user        = JFactory::getUser();
 220  
 221          // get the format to render
 222          $format = $document->getType();
 223  
 224          switch ($format)
 225          {
 226              case 'feed':
 227                  $params = array();
 228                  break;
 229  
 230              case 'html':
 231              default:
 232                  $template    = $this->getTemplate(true);
 233                  $file        = JRequest::getCmd('tmpl', 'index');
 234  
 235                  if (!$this->getCfg('offline') && ($file == 'offline')) {
 236                      $file = 'index';
 237                  }
 238  
 239                  if ($this->getCfg('offline') && !$user->authorise('core.login.offline')) {
 240                      $uri        = JFactory::getURI();
 241                      $return        = (string)$uri;
 242                      $this->setUserState('users.login.form.data', array( 'return' => $return ) );
 243                      $file = 'offline';
 244                      JResponse::setHeader('Status', '503 Service Temporarily Unavailable', 'true');
 245                  }
 246                  if (!is_dir(JPATH_THEMES . '/' . $template->template) && !$this->getCfg('offline')) {
 247                      $file = 'component';
 248                  }
 249                  $params = array(
 250                      'template'    => $template->template,
 251                      'file'        => $file.'.php',
 252                      'directory'    => JPATH_THEMES,
 253                      'params'    => $template->params
 254                  );
 255                  break;
 256          }
 257  
 258          // Parse the document.
 259          $document = JFactory::getDocument();
 260          $document->parse($params);
 261  
 262          // Trigger the onBeforeRender event.
 263          JPluginHelper::importPlugin('system');
 264          $this->triggerEvent('onBeforeRender');
 265  
 266          $caching = false;
 267          if ($this->getCfg('caching') && $this->getCfg('caching', 2) == 2 && !$user->get('id')) {
 268              $caching = true;
 269          }
 270  
 271          // Render the document.
 272          JResponse::setBody($document->render($caching, $params));
 273  
 274          // Trigger the onAfterRender event.
 275          $this->triggerEvent('onAfterRender');
 276      }
 277  
 278      /**
 279       * Login authentication function
 280       *
 281       * @param    array    Array('username' => string, 'password' => string)
 282       * @param    array    Array('remember' => boolean)
 283       *
 284       * @see JApplication::login
 285       */
 286  	public function login($credentials, $options = array())
 287      {
 288           // Set the application login entry point
 289          if (!array_key_exists('entry_url', $options)) {
 290              $options['entry_url'] = JURI::base().'index.php?option=com_users&task=user.login';
 291          }
 292  
 293          // Set the access control action to check.
 294          $options['action'] = 'core.login.site';
 295  
 296          return parent::login($credentials, $options);
 297      }
 298  
 299      /**
 300       * @deprecated 1.6    Use the authorise method instead.
 301       */
 302  	public function authorize($itemid)
 303      {
 304          JLog::add('JSite::authorize() is deprecated. Use JSite::authorise() instead.', JLog::WARNING, 'deprecated');
 305          return $this->authorise($itemid);
 306      }
 307  
 308      /**
 309       * Check if the user can access the application
 310       */
 311  	public function authorise($itemid)
 312      {
 313          $menus    = $this->getMenu();
 314          $user    = JFactory::getUser();
 315  
 316          if (!$menus->authorise($itemid))
 317          {
 318              if ($user->get('id') == 0)
 319              {
 320                  // Redirect to login
 321                  $uri        = JFactory::getURI();
 322                  $return        = (string)$uri;
 323  
 324                  $this->setUserState('users.login.form.data', array( 'return' => $return ) );
 325  
 326                  $url    = 'index.php?option=com_users&view=login';
 327                  $url    = JRoute::_($url, false);
 328  
 329                  $this->redirect($url, JText::_('JGLOBAL_YOU_MUST_LOGIN_FIRST'));
 330              }
 331              else {
 332                  JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
 333              }
 334          }
 335      }
 336  
 337      /**
 338       * Get the appliaction parameters
 339       *
 340       * @param    string    The component option
 341       * @return    object    The parameters object
 342       * @since    1.5
 343       */
 344  	public function getParams($option = null)
 345      {
 346          static $params = array();
 347  
 348          $hash = '__default';
 349          if (!empty($option)) {
 350              $hash = $option;
 351          }
 352          if (!isset($params[$hash]))
 353          {
 354              // Get component parameters
 355              if (!$option) {
 356                  $option = JRequest::getCmd('option');
 357              }
 358              // Get new instance of component global parameters
 359              $params[$hash] = clone JComponentHelper::getParams($option);
 360  
 361              // Get menu parameters
 362              $menus    = $this->getMenu();
 363              $menu    = $menus->getActive();
 364  
 365              // Get language
 366              $lang_code = JFactory::getLanguage()->getTag();
 367              $languages = JLanguageHelper::getLanguages('lang_code');
 368  
 369              $title = $this->getCfg('sitename');
 370              if (isset($languages[$lang_code]) && $languages[$lang_code]->metadesc) {
 371                  $description = $languages[$lang_code]->metadesc;
 372              } else {
 373                  $description = $this->getCfg('MetaDesc');
 374              }
 375              $rights = $this->getCfg('MetaRights');
 376              $robots = $this->getCfg('robots');
 377              // Lets cascade the parameters if we have menu item parameters
 378              if (is_object($menu)) {
 379                  $temp = new JRegistry;
 380                  $temp->loadString($menu->params);
 381                  $params[$hash]->merge($temp);
 382                  $title = $menu->title;
 383              } else {
 384                  // get com_menu global settings
 385                  $temp = clone JComponentHelper::getParams('com_menus');
 386                  $params[$hash]->merge($temp);
 387                  // if supplied, use page title
 388                  $title = $temp->get('page_title', $title);
 389              }
 390  
 391              $params[$hash]->def('page_title', $title);
 392              $params[$hash]->def('page_description', $description);
 393              $params[$hash]->def('page_rights', $rights);
 394              $params[$hash]->def('robots', $robots);
 395          }
 396  
 397          return $params[$hash];
 398      }
 399  
 400      /**
 401       * Get the application parameters
 402       *
 403       * @param    string    The component option
 404       *
 405       * @return    object    The parameters object
 406       * @since    1.5
 407       */
 408  	public function getPageParameters($option = null)
 409      {
 410          return $this->getParams($option);
 411      }
 412  
 413      /**
 414       * Get the template
 415       *
 416       * @return string The template name
 417       * @since 1.0
 418       */
 419  	public function getTemplate($params = false)
 420      {
 421          if(is_object($this->template))
 422          {
 423              if ($params) {
 424                  return $this->template;
 425              }
 426              return $this->template->template;
 427          }
 428          // Get the id of the active menu item
 429          $menu = $this->getMenu();
 430          $item = $menu->getActive();
 431          if (!$item) {
 432              $item = $menu->getItem(JRequest::getInt('Itemid'));
 433          }
 434  
 435          $id = 0;
 436          if (is_object($item)) { // valid item retrieved
 437              $id = $item->template_style_id;
 438          }
 439          $condition = '';
 440  
 441          $tid = JRequest::getVar('templateStyle', 0);
 442          if (is_numeric($tid) && (int) $tid > 0) {
 443              $id = (int) $tid;
 444          }
 445  
 446  
 447          $cache = JFactory::getCache('com_templates', '');
 448          if ($this->_language_filter) {
 449              $tag = JFactory::getLanguage()->getTag();
 450          }
 451          else {
 452              $tag ='';
 453          }
 454          if (!$templates = $cache->get('templates0'.$tag)) {
 455              // Load styles
 456              $db = JFactory::getDbo();
 457              $query = $db->getQuery(true);
 458              $query->select('id, home, template, s.params');
 459              $query->from('#__template_styles as s');
 460              $query->where('s.client_id = 0');
 461              $query->where('e.enabled = 1');
 462              $query->leftJoin('#__extensions as e ON e.element=s.template AND e.type='.$db->quote('template').' AND e.client_id=s.client_id');
 463  
 464              $db->setQuery($query);
 465              $templates = $db->loadObjectList('id');
 466              foreach($templates as &$template) {
 467                  $registry = new JRegistry;
 468                  $registry->loadString($template->params);
 469                  $template->params = $registry;
 470  
 471                  // Create home element
 472                  //sqlsrv change
 473                  if ($template->home == 1 && !isset($templates[0]) || $this->_language_filter && $template->home == $tag) {
 474                      $templates[0] = clone $template;
 475                  }
 476              }
 477              $cache->store($templates, 'templates0'.$tag);
 478          }
 479  
 480          if (isset($templates[$id])) {
 481              $template = $templates[$id];
 482          }
 483          else {
 484              $template = $templates[0];
 485          }
 486  
 487          // Allows for overriding the active template from the request
 488          $template->template = JRequest::getCmd('template', $template->template);
 489          $template->template = JFilterInput::getInstance()->clean($template->template, 'cmd'); // need to filter the default value as well
 490  
 491          // Fallback template
 492          if (!file_exists(JPATH_THEMES . '/' . $template->template . '/index.php')) {
 493              JError::raiseWarning(0, JText::_('JERROR_ALERTNOTEMPLATE'));
 494              $template->template = 'beez_20';
 495              if (!file_exists(JPATH_THEMES . '/beez_20/index.php')) {
 496                  $template->template = '';
 497              }
 498          }
 499  
 500          // Cache the result
 501          $this->template = $template;
 502          if ($params) {
 503              return $template;
 504          }
 505          return $template->template;
 506      }
 507  
 508      /**
 509       * Overrides the default template that would be used
 510       *
 511       * @param string    The template name
 512       * @param mixed        The template style parameters
 513       */
 514  	public function setTemplate($template, $styleParams=null)
 515       {
 516           if (is_dir(JPATH_THEMES . '/' . $template)) {
 517               $this->template = new stdClass();
 518               $this->template->template = $template;
 519              if ($styleParams instanceof JRegistry) {
 520                  $this->template->params = $styleParams;
 521              }
 522              else {
 523                  $this->template->params = new JRegistry($styleParams);
 524              }
 525           }
 526       }
 527  
 528      /**
 529       * Return a reference to the JPathway object.
 530       *
 531       * @param    string    $name        The name of the application/client.
 532       * @param    array    $options    An optional associative array of configuration settings.
 533       *
 534       * @return    object    JMenu.
 535       * @since    1.5
 536       */
 537  	public function getMenu($name = null, $options = array())
 538      {
 539          $options    = array();
 540          $menu        = parent::getMenu('site', $options);
 541          return $menu;
 542      }
 543  
 544      /**
 545       * Return a reference to the JPathway object.
 546       *
 547       * @param    string    $name        The name of the application.
 548       * @param    array    $options    An optional associative array of configuration settings.
 549       *
 550       * @return    object JPathway.
 551       * @since    1.5
 552       */
 553  	public function getPathway($name = null, $options = array())
 554      {
 555          $options = array();
 556          $pathway = parent::getPathway('site', $options);
 557          return $pathway;
 558      }
 559  
 560      /**
 561       * Return a reference to the JRouter object.
 562       *
 563       * @param    string    $name        The name of the application.
 564       * @param    array    $options    An optional associative array of configuration settings.
 565       *
 566       * @return    JRouter
 567       * @since    1.5
 568       */
 569  	static public function getRouter($name = null, array $options = array())
 570      {
 571          $config = JFactory::getConfig();
 572          $options['mode'] = $config->get('sef');
 573          $router = parent::getRouter('site', $options);
 574          return $router;
 575      }
 576  
 577      /**
 578       * Return the current state of the language filter.
 579       *
 580       * @return    boolean
 581       * @since    1.6
 582       */
 583  	public function getLanguageFilter()
 584      {
 585          return $this->_language_filter;
 586      }
 587  
 588      /**
 589       * Set the current state of the language filter.
 590       *
 591       * @return    boolean    The old state
 592       * @since    1.6
 593       */
 594  	public function setLanguageFilter($state=false)
 595      {
 596          $old = $this->_language_filter;
 597          $this->_language_filter=$state;
 598          return $old;
 599      }
 600      /**
 601       * Return the current state of the detect browser option.
 602       *
 603       * @return    boolean
 604       * @since    1.6
 605       */
 606  	public function getDetectBrowser()
 607      {
 608          return $this->_detect_browser;
 609      }
 610  
 611      /**
 612       * Set the current state of the detect browser option.
 613       *
 614       * @return    boolean    The old state
 615       * @since    1.6
 616       */
 617  	public function setDetectBrowser($state=false)
 618      {
 619          $old = $this->_detect_browser;
 620          $this->_detect_browser=$state;
 621          return $old;
 622      }
 623  
 624      /**
 625       * Redirect to another URL.
 626       *
 627       * Optionally enqueues a message in the system message queue (which will be displayed
 628       * the next time a page is loaded) using the enqueueMessage method. If the headers have
 629       * not been sent the redirect will be accomplished using a "301 Moved Permanently"
 630       * code in the header pointing to the new location. If the headers have already been
 631       * sent this will be accomplished using a JavaScript statement.
 632       *
 633       * @param    string    The URL to redirect to. Can only be http/https URL
 634       * @param    string    An optional message to display on redirect.
 635       * @param    string  An optional message type.
 636       * @param    boolean    True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
 637       * @param    boolean    True if the enqueued messages are passed to the redirection, false else.
 638       * @return    none; calls exit().
 639       * @since    1.5
 640       * @see        JApplication::enqueueMessage()
 641       */
 642  	public function redirect($url, $msg='', $msgType='message', $moved = false, $persistMsg = true)
 643      {
 644          if (!$persistMsg) {
 645              $this->_messageQueue = array();
 646          }
 647          parent::redirect($url, $msg, $msgType, $moved);
 648      }
 649  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1