[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Installation 5 * @subpackage Model 6 * 7 * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\CMS\Installation\Model; 12 13 use Joomla\CMS\Factory; 14 use Joomla\CMS\Form\Form; 15 use Joomla\CMS\Installation\Helper\DatabaseHelper; 16 use Joomla\CMS\Language\LanguageHelper; 17 use Joomla\CMS\Language\Text; 18 use Joomla\Utilities\ArrayHelper; 19 20 // phpcs:disable PSR1.Files.SideEffects 21 \defined('_JEXEC') or die; 22 // phpcs:enable PSR1.Files.SideEffects 23 24 /** 25 * Setup model for the Joomla Core Installer. 26 * 27 * @since 3.1 28 */ 29 class SetupModel extends BaseInstallationModel 30 { 31 /** 32 * Get the current setup options from the session. 33 * 34 * @return array An array of options from the session. 35 * 36 * @since 3.1 37 */ 38 public function getOptions() 39 { 40 if (!empty(Factory::getSession()->get('setup.options', array()))) { 41 return Factory::getSession()->get('setup.options', array()); 42 } 43 } 44 45 /** 46 * Store the current setup options in the session. 47 * 48 * @param array $options The installation options. 49 * 50 * @return array An array of options from the session. 51 * 52 * @since 3.1 53 */ 54 public function storeOptions($options) 55 { 56 // Get the current setup options from the session. 57 $old = (array) $this->getOptions(); 58 59 // Ensure that we have language 60 if (!isset($options['language']) || empty($options['language'])) { 61 $options['language'] = Factory::getLanguage()->getTag(); 62 } 63 64 // Store passwords as a separate key that is not used in the forms 65 foreach (array('admin_password', 'db_pass') as $passwordField) { 66 if (isset($options[$passwordField])) { 67 $plainTextKey = $passwordField . '_plain'; 68 69 $options[$plainTextKey] = $options[$passwordField]; 70 71 unset($options[$passwordField]); 72 } 73 } 74 75 // Get the session 76 $session = Factory::getSession(); 77 $options['helpurl'] = $session->get('setup.helpurl', null); 78 79 // Merge the new setup options into the current ones and store in the session. 80 $options = array_merge($old, (array) $options); 81 $session->set('setup.options', $options); 82 83 return $options; 84 } 85 86 /** 87 * Method to get the form. 88 * 89 * @param string|null $view The view being processed. 90 * 91 * @return Form|boolean JForm object on success, false on failure. 92 * 93 * @since 3.1 94 */ 95 public function getForm($view = null) 96 { 97 if (!$view) { 98 $view = Factory::getApplication()->input->getWord('view', 'setup'); 99 } 100 101 // Get the form. 102 Form::addFormPath(JPATH_COMPONENT . '/forms'); 103 104 try { 105 $form = Form::getInstance('jform', $view, array('control' => 'jform')); 106 } catch (\Exception $e) { 107 Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); 108 109 return false; 110 } 111 112 // Check the session for previously entered form data. 113 $data = (array) $this->getOptions(); 114 115 // Bind the form data if present. 116 if (!empty($data)) { 117 $form->bind($data); 118 } 119 120 return $form; 121 } 122 123 /** 124 * Method to check the form data. 125 * 126 * @param string $page The view being checked. 127 * 128 * @return array|boolean Array with the validated form data or boolean false on a validation failure. 129 * 130 * @since 3.1 131 */ 132 public function checkForm($page = 'setup') 133 { 134 // Get the posted values from the request and validate them. 135 $data = Factory::getApplication()->input->post->get('jform', array(), 'array'); 136 $return = $this->validate($data, $page); 137 138 // Attempt to save the data before validation. 139 $form = $this->getForm(); 140 $data = $form->filter($data); 141 142 $this->storeOptions($data); 143 144 // Check for validation errors. 145 if ($return === false) { 146 return false; 147 } 148 149 // Store the options in the session. 150 return $this->storeOptions($return); 151 } 152 153 /** 154 * Generate a panel of language choices for the user to select their language. 155 * 156 * @return array 157 * 158 * @since 3.1 159 */ 160 public function getLanguages() 161 { 162 // Detect the native language. 163 $native = LanguageHelper::detectLanguage(); 164 165 if (empty($native)) { 166 $native = 'en-GB'; 167 } 168 169 // Get a forced language if it exists. 170 $forced = Factory::getApplication()->getLocalise(); 171 172 if (!empty($forced['language'])) { 173 $native = $forced['language']; 174 } 175 176 // Get the list of available languages. 177 $list = LanguageHelper::createLanguageList($native); 178 179 if (!$list || $list instanceof \Exception) { 180 $list = array(); 181 } 182 183 return $list; 184 } 185 186 /** 187 * Method to validate the form data. 188 * 189 * @param array $data The form data. 190 * @param string|null $view The view. 191 * 192 * @return array|boolean Array of filtered data if valid, false otherwise. 193 * 194 * @since 3.1 195 */ 196 public function validate($data, $view = null) 197 { 198 // Get the form. 199 $form = $this->getForm($view); 200 201 // Check for an error. 202 if ($form === false) { 203 return false; 204 } 205 206 // Filter and validate the form data. 207 $data = $form->filter($data); 208 $return = $form->validate($data); 209 210 // Check for an error. 211 if ($return instanceof \Exception) { 212 Factory::getApplication()->enqueueMessage($return->getMessage(), 'warning'); 213 214 return false; 215 } 216 217 // Check the validation results. 218 if ($return === false) { 219 // Get the validation messages from the form. 220 $messages = array_reverse($form->getErrors()); 221 222 foreach ($messages as $message) { 223 if ($message instanceof \Exception) { 224 Factory::getApplication()->enqueueMessage($message->getMessage(), 'warning'); 225 } else { 226 Factory::getApplication()->enqueueMessage($message, 'warning'); 227 } 228 } 229 230 return false; 231 } 232 233 return $data; 234 } 235 236 /** 237 * Method to validate the db connection properties. 238 * 239 * @return boolean 240 * 241 * @since 4.0.0 242 * @throws \Exception 243 */ 244 public function validateDbConnection() 245 { 246 $options = $this->getOptions(); 247 248 // Get the options as an object for easier handling. 249 $options = ArrayHelper::toObject($options); 250 251 // Load the backend language files so that the DB error messages work. 252 $lang = Factory::getLanguage(); 253 $currentLang = $lang->getTag(); 254 255 // Load the selected language 256 if (LanguageHelper::exists($currentLang, JPATH_ADMINISTRATOR)) { 257 $lang->load('joomla', JPATH_ADMINISTRATOR, $currentLang, true); 258 } else { 259 // Pre-load en-GB in case the chosen language files do not exist. 260 $lang->load('joomla', JPATH_ADMINISTRATOR, 'en-GB', true); 261 } 262 263 // Validate and clean up connection parameters 264 $paramsCheck = DatabaseHelper::validateConnectionParameters($options); 265 266 if ($paramsCheck) { 267 // Validation error: Enqueue the error message 268 Factory::getApplication()->enqueueMessage($paramsCheck, 'error'); 269 270 return false; 271 } 272 273 // Security check for remote db hosts 274 if (!DatabaseHelper::checkRemoteDbHost($options)) { 275 // Messages have been enqueued in the called function. 276 return false; 277 } 278 279 // Get a database object. 280 try { 281 $db = DatabaseHelper::getDbo( 282 $options->db_type, 283 $options->db_host, 284 $options->db_user, 285 $options->db_pass_plain, 286 $options->db_name, 287 $options->db_prefix, 288 false, 289 DatabaseHelper::getEncryptionSettings($options) 290 ); 291 292 $db->connect(); 293 } catch (\RuntimeException $e) { 294 if ( 295 $options->db_type === 'mysql' && strpos($e->getMessage(), '[1049] Unknown database') === 42 296 || $options->db_type === 'pgsql' && strpos($e->getMessage(), 'database "' . $options->db_name . '" does not exist') 297 ) { 298 // Database doesn't exist: Skip the below checks, they will be done later at database creation 299 return true; 300 } 301 302 Factory::getApplication()->enqueueMessage(Text::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'error'); 303 304 return false; 305 } 306 307 // Check database server parameters 308 $dbServerCheck = DatabaseHelper::checkDbServerParameters($db, $options); 309 310 if ($dbServerCheck) { 311 // Some server parameter is not ok: Enqueue the error message 312 Factory::getApplication()->enqueueMessage($dbServerCheck, 'error'); 313 314 return false; 315 } 316 317 return true; 318 } 319 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |