PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/administrator/components/com_config/models/application.php

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