Source for file application.php

Documentation is available at application.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_config
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Model for the global configuration
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_config
  17.  * @since       3.2
  18.  */
  19. {
  20.     /**
  21.      * Method to get a form object.
  22.      *
  23.      * @param   array    $data      Data for the form.
  24.      * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  25.      *
  26.      * @return  mixed  A JForm object on success, false on failure
  27.      *
  28.      * @since    1.6
  29.      */
  30.     public function getForm($data array()$loadData true)
  31.     {
  32.         // Get the form.
  33.         $form $this->loadForm('com_config.application''application'array('control' => 'jform''load_data' => $loadData));
  34.  
  35.         if (empty($form))
  36.         {
  37.             return false;
  38.         }
  39.  
  40.         return $form;
  41.     }
  42.  
  43.     /**
  44.      * Method to get the configuration data.
  45.      *
  46.      * This method will load the global configuration data straight from
  47.      * JConfig. If configuration data has been saved in the session, that
  48.      * data will be merged into the original data, overwriting it.
  49.      *
  50.      * @return    array  An array containg all global config data.
  51.      *
  52.      * @since    1.6
  53.      */
  54.     public function getData()
  55.     {
  56.         // Get the config data.
  57.         $config    new JConfig;
  58.         $data    JArrayHelper::fromObject($config);
  59.  
  60.         // Prime the asset_id for the rules.
  61.         $data['asset_id'1;
  62.  
  63.         // Get the text filter data
  64.         $params JComponentHelper::getParams('com_config');
  65.         $data['filters'JArrayHelper::fromObject($params->get('filters'));
  66.  
  67.         // If no filter data found, get from com_content (update of 1.6/1.7 site)
  68.         if (empty($data['filters']))
  69.         {
  70.             $contentParams JComponentHelper::getParams('com_content');
  71.             $data['filters'JArrayHelper::fromObject($contentParams->get('filters'));
  72.         }
  73.  
  74.         // Check for data in the session.
  75.         $temp JFactory::getApplication()->getUserState('com_config.config.global.data');
  76.  
  77.         // Merge in the session data.
  78.         if (!empty($temp))
  79.         {
  80.             $data array_merge($data$temp);
  81.         }
  82.  
  83.         return $data;
  84.     }
  85.  
  86.     /**
  87.      * Method to save the configuration data.
  88.      *
  89.      * @param   array  $data  An array containing all global config data.
  90.      *
  91.      * @return    boolean  True on success, false on failure.
  92.      *
  93.      * @since    1.6
  94.      */
  95.     public function save($data)
  96.     {
  97.         $app JFactory::getApplication();
  98.  
  99.         // Save the rules
  100.         if (isset($data['rules']))
  101.         {
  102.             $rules    new JAccessRules($data['rules']);
  103.  
  104.             // Check that we aren't removing our Super User permission
  105.             // Need to get groups from database, since they might have changed
  106.             $myGroups JAccess::getGroupsByUser(JFactory::getUser()->get('id'));
  107.             $myRules $rules->getData();
  108.             $hasSuperAdmin $myRules['core.admin']->allow($myGroups);
  109.  
  110.             if (!$hasSuperAdmin)
  111.             {
  112.                 $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_REMOVING_SUPER_ADMIN')'error');
  113.  
  114.                 return false;
  115.             }
  116.  
  117.             $asset JTable::getInstance('asset');
  118.  
  119.             if ($asset->loadByName('root.1'))
  120.             {
  121.                 $asset->rules = (string) $rules;
  122.  
  123.                 if (!$asset->check(|| !$asset->store())
  124.                 {
  125.                     $app->enqueueMessage(JText::_('SOME_ERROR_CODE')'error');
  126.  
  127.                     return;
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_ROOT_ASSET_NOT_FOUND')'error');
  133.  
  134.                 return false;
  135.             }
  136.  
  137.             unset($data['rules']);
  138.         }
  139.  
  140.         // Save the text filters
  141.         if (isset($data['filters']))
  142.         {
  143.             $registry new JRegistry;
  144.             $registry->loadArray(array('filters' => $data['filters']));
  145.  
  146.             $extension JTable::getInstance('extension');
  147.  
  148.             // Get extension_id
  149.             $extension_id $extension->find(array('name' => 'com_config'));
  150.  
  151.             if ($extension->load((int) $extension_id))
  152.             {
  153.                 $extension->params = (string) $registry;
  154.  
  155.                 if (!$extension->check(|| !$extension->store())
  156.                 {
  157.                     $app->enqueueMessage(JText::_('SOME_ERROR_CODE')'error');
  158.  
  159.                     return;
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIG_EXTENSION_NOT_FOUND')'error');
  165.  
  166.                 return false;
  167.             }
  168.  
  169.             unset($data['filters']);
  170.         }
  171.  
  172.         // Get the previous configuration.
  173.         $prev new JConfig;
  174.         $prev JArrayHelper::fromObject($prev);
  175.  
  176.         // Merge the new data in. We do this to preserve values that were not in the form.
  177.         $data array_merge($prev$data);
  178.  
  179.         /*
  180.          * Perform miscellaneous options based on configuration settings/changes.
  181.          */
  182.  
  183.         // Escape the offline message if present.
  184.         if (isset($data['offline_message']))
  185.         {
  186.             $data['offline_message'JFilterOutput::ampReplace($data['offline_message']);
  187.         }
  188.  
  189.         // Purge the database session table if we are changing to the database handler.
  190.         if ($prev['session_handler'!= 'database' && $data['session_handler'== 'database')
  191.         {
  192.             $table JTable::getInstance('session');
  193.             $table->purge(-1);
  194.         }
  195.  
  196.         if (empty($data['cache_handler']))
  197.         {
  198.             $data['caching'0;
  199.         }
  200.  
  201.         // Clean the cache if disabled but previously enabled.
  202.         if (!$data['caching'&& $prev['caching'])
  203.         {
  204.             $cache JFactory::getCache();
  205.             $cache->clean();
  206.         }
  207.  
  208.         // Create the new configuration object.
  209.         $config new JRegistry('config');
  210.         $config->loadArray($data);
  211.  
  212.         // Overwrite the old FTP credentials with the new ones.
  213.         $temp JFactory::getConfig();
  214.         $temp->set('ftp_enable'$data['ftp_enable']);
  215.         $temp->set('ftp_host'$data['ftp_host']);
  216.         $temp->set('ftp_port'$data['ftp_port']);
  217.         $temp->set('ftp_user'$data['ftp_user']);
  218.         $temp->set('ftp_pass'$data['ftp_pass']);
  219.         $temp->set('ftp_root'$data['ftp_root']);
  220.  
  221.         // Clear cache of com_config component.
  222.         $this->cleanCache('_system');
  223.  
  224.         // Write the configuration file.
  225.         return $this->writeConfigFile($config);
  226.     }
  227.  
  228.     /**
  229.      * Method to unset the root_user value from configuration data.
  230.      *
  231.      * This method will load the global configuration data straight from
  232.      * JConfig and remove the root_user value for security, then save the configuration.
  233.      *
  234.      * @return    boolean  True on success, false on failure.
  235.      *
  236.      * @since    1.6
  237.      */
  238.     public function removeroot()
  239.     {
  240.         // Get the previous configuration.
  241.         $prev new JConfig;
  242.         $prev JArrayHelper::fromObject($prev);
  243.  
  244.         // Create the new configuration object, and unset the root_user property
  245.         $config new JRegistry('config');
  246.         unset($prev['root_user']);
  247.         $config->loadArray($prev);
  248.  
  249.         // Write the configuration file.
  250.         return $this->writeConfigFile($config);
  251.     }
  252.  
  253.     /**
  254.      * Method to write the configuration to a file.
  255.      *
  256.      * @param   JRegistry  $config  A JRegistry object containing all global config data.
  257.      *
  258.      * @return    boolean  True on success, false on failure.
  259.      *
  260.      * @since    2.5.4
  261.      * @throws  RuntimeException
  262.      */
  263.     private function writeConfigFile(JRegistry $config)
  264.     {
  265.         jimport('joomla.filesystem.path');
  266.         jimport('joomla.filesystem.file');
  267.  
  268.         // Set the configuration file path.
  269.         $file JPATH_CONFIGURATION '/configuration.php';
  270.  
  271.         // Get the new FTP credentials.
  272.         $ftp JClientHelper::getCredentials('ftp'true);
  273.  
  274.         $app JFactory::getApplication();
  275.  
  276.         // Attempt to make the file writeable if using FTP.
  277.         if (!$ftp['enabled'&& JPath::isOwner($file&& !JPath::setPermissions($file'0644'))
  278.         {
  279.             $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTWRITABLE')'notice');
  280.         }
  281.  
  282.         // Attempt to write the configuration file as a PHP class named JConfig.
  283.         $configuration $config->toString('PHP'array('class' => 'JConfig''closingtag' => false));
  284.  
  285.         if (!JFile::write($file$configuration))
  286.         {
  287.             throw new RuntimeException(JText::_('COM_CONFIG_ERROR_WRITE_FAILED'));
  288.         }
  289.  
  290.         // Attempt to make the file unwriteable if using FTP.
  291.         if (!$ftp['enabled'&& JPath::isOwner($file&& !JPath::setPermissions($file'0444'))
  292.         {
  293.             $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE')'notice');
  294.         }
  295.  
  296.         return true;
  297.     }
  298. }

Documentation generated on Tue, 19 Nov 2013 14:53:48 +0100 by phpDocumentor 1.4.3