PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_config/model/application.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 475 lines | 265 code | 82 blank | 128 comment | 41 complexity | 90d357a6dfc94320327b59a95693c628 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_config
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. /**
  12. * Model for the global configuration
  13. *
  14. * @since 3.2
  15. */
  16. class ConfigModelApplication extends ConfigModelForm
  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. *
  24. * @return mixed A JForm object on success, false on failure
  25. *
  26. * @since 1.6
  27. */
  28. public function getForm($data = array(), $loadData = true)
  29. {
  30. // Get the form.
  31. $form = $this->loadForm('com_config.application', 'application', array('control' => 'jform', 'load_data' => $loadData));
  32. if (empty($form))
  33. {
  34. return false;
  35. }
  36. return $form;
  37. }
  38. /**
  39. * Method to get the configuration data.
  40. *
  41. * This method will load the global configuration data straight from
  42. * JConfig. If configuration data has been saved in the session, that
  43. * data will be merged into the original data, overwriting it.
  44. *
  45. * @return array An array containg all global config data.
  46. *
  47. * @since 1.6
  48. */
  49. public function getData()
  50. {
  51. // Get the config data.
  52. $config = new JConfig;
  53. $data = JArrayHelper::fromObject($config);
  54. // Prime the asset_id for the rules.
  55. $data['asset_id'] = 1;
  56. // Get the text filter data
  57. $params = JComponentHelper::getParams('com_config');
  58. $data['filters'] = JArrayHelper::fromObject($params->get('filters'));
  59. // If no filter data found, get from com_content (update of 1.6/1.7 site)
  60. if (empty($data['filters']))
  61. {
  62. $contentParams = JComponentHelper::getParams('com_content');
  63. $data['filters'] = JArrayHelper::fromObject($contentParams->get('filters'));
  64. }
  65. // Check for data in the session.
  66. $temp = JFactory::getApplication()->getUserState('com_config.config.global.data');
  67. // Merge in the session data.
  68. if (!empty($temp))
  69. {
  70. $data = array_merge($data, $temp);
  71. }
  72. return $data;
  73. }
  74. /**
  75. * Method to save the configuration data.
  76. *
  77. * @param array $data An array containing all global config data.
  78. *
  79. * @return boolean True on success, false on failure.
  80. *
  81. * @since 1.6
  82. */
  83. public function save($data)
  84. {
  85. $app = JFactory::getApplication();
  86. // Check that we aren't setting wrong database configuration
  87. $options = array(
  88. 'driver' => $data['dbtype'],
  89. 'host' => $data['host'],
  90. 'user' => $data['user'],
  91. 'password' => JFactory::getConfig()->get('password'),
  92. 'database' => $data['db'],
  93. 'prefix' => $data['dbprefix']
  94. );
  95. try
  96. {
  97. $dbc = JDatabaseDriver::getInstance($options)->getVersion();
  98. }
  99. catch (Exception $e)
  100. {
  101. $app->enqueueMessage(JText::_('JLIB_DATABASE_ERROR_DATABASE_CONNECT'), 'error');
  102. return false;
  103. }
  104. // Save the rules
  105. if (isset($data['rules']))
  106. {
  107. $rules = new JAccessRules($data['rules']);
  108. // Check that we aren't removing our Super User permission
  109. // Need to get groups from database, since they might have changed
  110. $myGroups = JAccess::getGroupsByUser(JFactory::getUser()->get('id'));
  111. $myRules = $rules->getData();
  112. $hasSuperAdmin = $myRules['core.admin']->allow($myGroups);
  113. if (!$hasSuperAdmin)
  114. {
  115. $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_REMOVING_SUPER_ADMIN'), 'error');
  116. return false;
  117. }
  118. $asset = JTable::getInstance('asset');
  119. if ($asset->loadByName('root.1'))
  120. {
  121. $asset->rules = (string) $rules;
  122. if (!$asset->check() || !$asset->store())
  123. {
  124. $app->enqueueMessage(JText::_('SOME_ERROR_CODE'), 'error');
  125. return;
  126. }
  127. }
  128. else
  129. {
  130. $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_ROOT_ASSET_NOT_FOUND'), 'error');
  131. return false;
  132. }
  133. unset($data['rules']);
  134. }
  135. // Save the text filters
  136. if (isset($data['filters']))
  137. {
  138. $registry = new Registry;
  139. $registry->loadArray(array('filters' => $data['filters']));
  140. $extension = JTable::getInstance('extension');
  141. // Get extension_id
  142. $extension_id = $extension->find(array('name' => 'com_config'));
  143. if ($extension->load((int) $extension_id))
  144. {
  145. $extension->params = (string) $registry;
  146. if (!$extension->check() || !$extension->store())
  147. {
  148. $app->enqueueMessage(JText::_('SOME_ERROR_CODE'), 'error');
  149. return;
  150. }
  151. }
  152. else
  153. {
  154. $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIG_EXTENSION_NOT_FOUND'), 'error');
  155. return false;
  156. }
  157. unset($data['filters']);
  158. }
  159. // Get the previous configuration.
  160. $prev = new JConfig;
  161. $prev = JArrayHelper::fromObject($prev);
  162. // Merge the new data in. We do this to preserve values that were not in the form.
  163. $data = array_merge($prev, $data);
  164. /*
  165. * Perform miscellaneous options based on configuration settings/changes.
  166. */
  167. // Escape the offline message if present.
  168. if (isset($data['offline_message']))
  169. {
  170. $data['offline_message'] = JFilterOutput::ampReplace($data['offline_message']);
  171. }
  172. // Purge the database session table if we are changing to the database handler.
  173. if ($prev['session_handler'] != 'database' && $data['session_handler'] == 'database')
  174. {
  175. $table = JTable::getInstance('session');
  176. $table->purge(-1);
  177. }
  178. if (empty($data['cache_handler']))
  179. {
  180. $data['caching'] = 0;
  181. }
  182. $path = JPATH_SITE . '/cache';
  183. // Give a warning if the cache-folder can not be opened
  184. if ($data['caching'] > 0 && $data['cache_handler'] == 'file' && @opendir($path) == false)
  185. {
  186. JLog::add(JText::sprintf('COM_CONFIG_ERROR_CACHE_PATH_NOTWRITABLE', $path), JLog::WARNING, 'jerror');
  187. $data['caching'] = 0;
  188. }
  189. // Clean the cache if disabled but previously enabled.
  190. if (!$data['caching'] && $prev['caching'])
  191. {
  192. $cache = JFactory::getCache();
  193. $cache->clean();
  194. }
  195. // Create the new configuration object.
  196. $config = new Registry('config');
  197. $config->loadArray($data);
  198. // Overwrite the old FTP credentials with the new ones.
  199. $temp = JFactory::getConfig();
  200. $temp->set('ftp_enable', $data['ftp_enable']);
  201. $temp->set('ftp_host', $data['ftp_host']);
  202. $temp->set('ftp_port', $data['ftp_port']);
  203. $temp->set('ftp_user', $data['ftp_user']);
  204. $temp->set('ftp_pass', $data['ftp_pass']);
  205. $temp->set('ftp_root', $data['ftp_root']);
  206. // Clear cache of com_config component.
  207. $this->cleanCache('_system', 0);
  208. $this->cleanCache('_system', 1);
  209. // Write the configuration file.
  210. return $this->writeConfigFile($config);
  211. }
  212. /**
  213. * Method to unset the root_user value from configuration data.
  214. *
  215. * This method will load the global configuration data straight from
  216. * JConfig and remove the root_user value for security, then save the configuration.
  217. *
  218. * @return boolean True on success, false on failure.
  219. *
  220. * @since 1.6
  221. */
  222. public function removeroot()
  223. {
  224. // Get the previous configuration.
  225. $prev = new JConfig;
  226. $prev = JArrayHelper::fromObject($prev);
  227. // Create the new configuration object, and unset the root_user property
  228. $config = new Registry('config');
  229. unset($prev['root_user']);
  230. $config->loadArray($prev);
  231. // Write the configuration file.
  232. return $this->writeConfigFile($config);
  233. }
  234. /**
  235. * Method to write the configuration to a file.
  236. *
  237. * @param Registry $config A Registry object containing all global config data.
  238. *
  239. * @return boolean True on success, false on failure.
  240. *
  241. * @since 2.5.4
  242. * @throws RuntimeException
  243. */
  244. private function writeConfigFile(Registry $config)
  245. {
  246. jimport('joomla.filesystem.path');
  247. jimport('joomla.filesystem.file');
  248. // Set the configuration file path.
  249. $file = JPATH_CONFIGURATION . '/configuration.php';
  250. // Get the new FTP credentials.
  251. $ftp = JClientHelper::getCredentials('ftp', true);
  252. $app = JFactory::getApplication();
  253. // Attempt to make the file writeable if using FTP.
  254. if (!$ftp['enabled'] && JPath::isOwner($file) && !JPath::setPermissions($file, '0644'))
  255. {
  256. $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
  257. }
  258. // Attempt to write the configuration file as a PHP class named JConfig.
  259. $configuration = $config->toString('PHP', array('class' => 'JConfig', 'closingtag' => false));
  260. if (!JFile::write($file, $configuration))
  261. {
  262. throw new RuntimeException(JText::_('COM_CONFIG_ERROR_WRITE_FAILED'));
  263. }
  264. // Attempt to make the file unwriteable if using FTP.
  265. if (!$ftp['enabled'] && JPath::isOwner($file) && !JPath::setPermissions($file, '0444'))
  266. {
  267. $app->enqueueMessage(JText::_('COM_CONFIG_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
  268. }
  269. return true;
  270. }
  271. /**
  272. * Method to store the permission values in the asset table.
  273. *
  274. * This method will get an array with permission key value pairs and transform it
  275. * into json and update the asset table in the database.
  276. *
  277. * @param string $permission Need an array with Permissions (component, rule, value and title)
  278. *
  279. * @return boolean True on success, false on failure.
  280. *
  281. * @since 3.5
  282. */
  283. public function storePermissions($permission)
  284. {
  285. try
  286. {
  287. // Load the current settings for this component
  288. $query = $this->db->getQuery(true)
  289. ->select($this->db->quoteName(array('name', 'rules')))
  290. ->from($this->db->quoteName('#__assets'))
  291. ->where($this->db->quoteName('name') . ' = ' . $this->db->quote($permission['component']));
  292. $this->db->setQuery($query);
  293. // Load the results as a list of stdClass objects (see later for more options on retrieving data).
  294. $results = $this->db->loadAssocList();
  295. if (empty($results))
  296. {
  297. $data = array();
  298. $data[$permission['action']] = array();
  299. $data[$permission['action']] = array($permission['rule'] => $permission['value']);
  300. $rules = new JAccessRules($data);
  301. $asset = JTable::getInstance('asset');
  302. $asset->rules = (string) $rules;
  303. $asset->name = (string) $permission['component'];
  304. $asset->title = (string) $permission['title'];
  305. if (!$asset->check() || !$asset->store())
  306. {
  307. JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_CODE'), 'error');
  308. return false;
  309. }
  310. return true;
  311. }
  312. else
  313. {
  314. // Decode the rule settings
  315. $temp = json_decode($results[0]['rules'], true);
  316. // Check if a new value is to be set
  317. if (isset($permission['value']))
  318. {
  319. // Check if we already have an action entry
  320. if (!isset($temp[$permission['action']]))
  321. {
  322. $temp[$permission['action']] = array();
  323. }
  324. // Check if we already have a rule entry
  325. if (!isset($temp[$permission['action']][$permission['rule']]))
  326. {
  327. $temp[$permission['action']][$permission['rule']] = array();
  328. }
  329. // Set the new permission
  330. $temp[$permission['action']][$permission['rule']] = intval($permission['value']);
  331. // Check if we have an inherited setting
  332. if (strlen($permission['value']) == 0)
  333. {
  334. unset($temp[$permission['action']][$permission['rule']]);
  335. }
  336. }
  337. else
  338. {
  339. // There is no value so remove the action as it's not needed
  340. unset($temp[$permission['action']]);
  341. }
  342. // Store the new permissions
  343. $temp = json_encode($temp);
  344. $query = $this->db->getQuery(true)
  345. ->update($this->db->quoteName('#__assets'))
  346. ->set('rules = ' . $this->db->quote($temp))
  347. ->where($this->db->quoteName('name') . ' = ' . $this->db->quote($permission['component']));
  348. $this->db->setQuery($query);
  349. $result = $this->db->execute();
  350. return (bool) $result;
  351. }
  352. }
  353. catch (Exception $e)
  354. {
  355. return $e->getMessage();
  356. }
  357. }
  358. /**
  359. * Method to send a test mail which is called via an AJAX request
  360. *
  361. * @return bool
  362. *
  363. * @since 3.5
  364. * @throws Exception
  365. */
  366. public function sendTestMail()
  367. {
  368. // Set the new values to test with the current settings
  369. $app = JFactory::getApplication();
  370. $input = $app->input;
  371. $app->set('smtpauth', $input->get('smtpauth'));
  372. $app->set('smtpuser', $input->get('smtpuser', '', 'STRING'));
  373. $app->set('smtppass', $input->get('smtppass', '', 'RAW'));
  374. $app->set('smtphost', $input->get('smtphost'));
  375. $app->set('smtpsecure', $input->get('smtpsecure'));
  376. $app->set('smtpport', $input->get('smtpport'));
  377. $app->set('mailfrom', $input->get('mailfrom', '', 'STRING'));
  378. $app->set('fromname', $input->get('fromname', '', 'STRING'));
  379. $app->set('mailer', $input->get('mailer'));
  380. $app->set('mailonline', $input->get('mailonline'));
  381. // Prepare email and send try to send it
  382. $mailSubject = JText::sprintf('COM_CONFIG_SENDMAIL_SUBJECT', $app->get('sitename'));
  383. $mailBody = JText::sprintf('COM_CONFIG_SENDMAIL_BODY', JText::_('COM_CONFIG_SENDMAIL_METHOD_' . strtoupper($app->get('mailer'))));
  384. if (JFactory::getMailer()->sendMail($app->get('mailfrom'), $app->get('fromname'), $app->get('mailfrom'), $mailSubject, $mailBody) === true)
  385. {
  386. $methodName = JText::_('COM_CONFIG_SENDMAIL_METHOD_' . strtoupper($app->get('mailer')));
  387. $app->enqueueMessage(JText::sprintf('COM_CONFIG_SENDMAIL_SUCCESS', $app->get('mailfrom'), $methodName), 'success');
  388. return true;
  389. }
  390. $app->enqueueMessage(JText::_('COM_CONFIG_SENDMAIL_ERROR'), 'error');
  391. return false;
  392. }
  393. }