| Conditions | 31 |
| Paths | 1721 |
| Total Lines | 367 |
| Code Lines | 170 |
| Lines | 12 |
| Ratio | 3.27 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | function form() |
||
| 26 | { |
||
| 27 | $app = \Slim\Slim::getInstance(); |
||
| 28 | $env = $app->environment(); |
||
| 29 | $final_global_template_vars = $app->config('final_global_template_vars'); |
||
| 30 | |||
| 31 | // Redirect to the installer if database variables aren't present, and if we aren't already there. |
||
| 32 | if ( |
||
| 33 | isset($final_global_template_vars["db_connection"]["name"]) && |
||
| 34 | isset($final_global_template_vars["db_connection"]["host"]) && |
||
| 35 | isset($final_global_template_vars["db_connection"]["user"]) && |
||
| 36 | isset($final_global_template_vars["db_connection"]["password"]) && |
||
| 37 | $_SERVER["REQUEST_URI"] == "/webapp_installer/" |
||
| 38 | ) { |
||
| 39 | header("Location: ".$final_global_template_vars["login_url"]."/"); |
||
| 40 | exit; |
||
| 41 | } |
||
| 42 | |||
| 43 | require_once $_SERVER["PATH_TO_VENDOR"] . "wixel/gump/gump.class.php"; |
||
| 44 | $gump = new GUMP(); |
||
| 45 | |||
| 46 | $data = $posted_data = $app->request()->post() ? $app->request()->post() : false; |
||
| 47 | |||
| 48 | // GUMP validation rules |
||
| 49 | $rules = array( |
||
| 50 | "user_account_email" => "required" |
||
| 51 | ,"user_account_password" => "required" |
||
| 52 | ,"first_name" => "required" |
||
| 53 | ,"last_name" => "required" |
||
| 54 | ,"application_name" => "required" |
||
| 55 | ,"session_key" => "required" |
||
| 56 | ,"cname" => "required" |
||
| 57 | // ,"http_mode" => "required" |
||
| 58 | ,"database_host" => "required" |
||
| 59 | ,"database_name" => "required" |
||
| 60 | ,"database_username" => "required" |
||
| 61 | ,"database_password" => "required" |
||
| 62 | ); |
||
| 63 | |||
| 64 | // Validation using GUMP |
||
| 65 | View Code Duplication | if ($posted_data) { |
|
| 66 | $validated = array(); |
||
| 67 | $errors = array(); |
||
| 68 | $validated = $gump->validate($posted_data, $rules); |
||
| 69 | if ($validated !== true) { |
||
| 70 | $errors = \phpskeleton\models\utility::gump_parse_errors($validated); |
||
| 71 | } |
||
| 72 | if ($errors) { |
||
| 73 | $env = $app->environment(); |
||
| 74 | $env["default_validation_errors"] = $errors; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | $default_validation_errors = isset($env["default_validation_errors"]) ? $env["default_validation_errors"] : false; |
||
| 79 | |||
| 80 | // If there are no errors, begin the second round of checks |
||
| 81 | if (!$default_validation_errors && $posted_data) { |
||
| 82 | // Check to see if the database user exists |
||
| 83 | $link = @mysqli_connect($posted_data['database_host'], $posted_data['database_username'], $posted_data['database_password']); |
||
| 84 | if (!$link) { |
||
| 85 | // die('Could not connect to the database. Please check your parameters.'); |
||
| 86 | $app->flash('message', 'Could not connect to the database. Please check your parameters.'); |
||
| 87 | $app->redirect($final_global_template_vars["path_to_this_module"]); |
||
| 88 | } |
||
| 89 | // Next, check to see if the database exists by making $posted_data['database_name'] the current db |
||
| 90 | $db_selected = mysqli_select_db($link, $posted_data['database_name']); |
||
| 91 | if (!$db_selected) { |
||
| 92 | // die('Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?'); |
||
| 93 | $app->flash('message', 'Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?'); |
||
| 94 | $app->redirect($final_global_template_vars["path_to_this_module"]); |
||
| 95 | } |
||
| 96 | |||
| 97 | // If there are no MYSQL errors, overwrite the default_global_settings.php file |
||
| 98 | $file_name = "default_global_settings.php"; |
||
| 99 | $original_file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$file_name); |
||
| 100 | $parsed = str_replace('#~site_name~#', '"site_name" => "'.$posted_data['application_name'].'",', $original_file); |
||
| 101 | $parsed = str_replace('#~session_key~#', ',"session_key" => "'.$posted_data['session_key'].'"', $parsed); |
||
| 102 | $parsed = str_replace('#~name~#', '"name" => ($_SERVER["IS_DEV"] == "true") ? "'.$posted_data['database_name'].'" : "'.$posted_data['database_name'].'"', $parsed); |
||
| 103 | $parsed = str_replace('#~host~#', ',"host" => "'.$posted_data['database_host'].'"', $parsed); |
||
| 104 | $parsed = str_replace('#~user~#', ',"user" => "'.$posted_data['database_username'].'"', $parsed); |
||
| 105 | $parsed = str_replace('#~password~#', ',"password" => "'.$posted_data['database_password'].'",', $parsed); |
||
| 106 | $parsed = str_replace('#~admin_emails~#', ',"admin_emails" => "'.$posted_data['user_account_email'].'",', $parsed); |
||
| 107 | unlink($_SERVER['DOCUMENT_ROOT'].'/'.$file_name); |
||
| 108 | $file_handle = fopen($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 'w') or die("can't open file"); |
||
| 109 | fwrite($file_handle, $parsed); |
||
| 110 | fclose($file_handle); |
||
| 111 | chmod($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 0664); |
||
| 112 | |||
| 113 | // Overwrite the .htaccess file |
||
| 114 | $file_name = ".htaccess"; |
||
| 115 | $original_file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$file_name); |
||
| 116 | $parsed = str_replace('"^([^\.]*)\.com$"', $posted_data['cname'], $original_file); |
||
| 117 | unlink($_SERVER['DOCUMENT_ROOT'].'/'.$file_name); |
||
| 118 | $file_handle = fopen($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 'w') or die("can't open file"); |
||
| 119 | fwrite($file_handle, $parsed); |
||
| 120 | fclose($file_handle); |
||
| 121 | chmod($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 0664); |
||
| 122 | |||
| 123 | // Build the database tables |
||
| 124 | $db_vars = array( |
||
| 125 | "name" => $posted_data['database_name'] |
||
| 126 | ,"host" => $posted_data['database_host'] |
||
| 127 | ,"user" => $posted_data['database_username'] |
||
| 128 | ,"password" => $posted_data['database_password'] |
||
| 129 | ); |
||
| 130 | |||
| 131 | $db_conn = new \PHPSkeleton\models\db($db_vars); |
||
| 132 | $db = $db_conn->get_resource(); |
||
| 133 | |||
| 134 | require_once $final_global_template_vars["default_module_list"]["authenticate"]["absolute_path_to_this_module"] . "/models/authenticate.class.php"; |
||
| 135 | $authenticate = new \PHPSkeleton\Authenticate($db, $final_global_template_vars["session_key"]); |
||
| 136 | |||
| 137 | $statement = $db->prepare("CREATE TABLE `user_account` ( |
||
| 138 | `user_account_id` int(10) NOT NULL AUTO_INCREMENT, |
||
| 139 | `user_account_email` varchar(255) NOT NULL, |
||
| 140 | `user_account_password` varchar(255) NOT NULL, |
||
| 141 | `first_name` varchar(255) NOT NULL, |
||
| 142 | `last_name` varchar(255) NOT NULL, |
||
| 143 | `acceptable_use_policy` int(1) DEFAULT NULL, |
||
| 144 | `active` int(1) NOT NULL DEFAULT '0', |
||
| 145 | `emailed_hash` varchar(255) DEFAULT NULL, |
||
| 146 | `created_date` datetime DEFAULT NULL, |
||
| 147 | `modified_date` datetime DEFAULT NULL, |
||
| 148 | PRIMARY KEY (`user_account_id`) |
||
| 149 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores user accounts'"); |
||
| 150 | $statement->execute(); |
||
| 151 | $error = $db->errorInfo(); |
||
| 152 | if ($error[0] != "00000") { |
||
| 153 | var_dump($db->errorInfo()); |
||
| 154 | die('CREATE TABLE `user_account` failed.'); |
||
| 155 | } |
||
| 156 | |||
| 157 | // INSERT this user into the user_account table |
||
| 158 | $statement = $db->prepare("INSERT INTO user_account |
||
| 159 | (user_account_email, user_account_password, first_name, last_name, acceptable_use_policy, created_date, active) |
||
| 160 | VALUES ( :user_account_email, :user_account_password, :first_name, :last_name, 1, NOW(), 1 )"); |
||
| 161 | $statement->bindValue(":user_account_email", $posted_data['user_account_email'], PDO::PARAM_STR); |
||
| 162 | $statement->bindValue(":user_account_password", $authenticate->generate_hashed_password($posted_data['user_account_password']), PDO::PARAM_STR); |
||
| 163 | $statement->bindValue(":first_name", $posted_data['first_name'], PDO::PARAM_STR); |
||
| 164 | $statement->bindValue(":last_name", $posted_data['last_name'], PDO::PARAM_STR); |
||
| 165 | $statement->execute(); |
||
| 166 | $error = $db->errorInfo(); |
||
| 167 | if ($error[0] != "00000") { |
||
| 168 | var_dump($db->errorInfo()); |
||
| 169 | die('The INSERT INTO user_account failed.'); |
||
| 170 | } |
||
| 171 | $last_inserted_user_account_id = $db->lastInsertId(); |
||
| 172 | |||
| 173 | $statement = $db->prepare("CREATE TABLE `user_account_addresses` ( |
||
| 174 | `user_account_addresses_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 175 | `user_account_id` int(11) NOT NULL, |
||
| 176 | `address_label` varchar(100) NOT NULL DEFAULT '', |
||
| 177 | `address_1` varchar(50) DEFAULT NULL, |
||
| 178 | `address_2` varchar(50) DEFAULT NULL, |
||
| 179 | `city` varchar(50) NOT NULL DEFAULT '', |
||
| 180 | `state` char(2) NOT NULL DEFAULT '', |
||
| 181 | `zip` varchar(10) NOT NULL, |
||
| 182 | `date_created` datetime NOT NULL, |
||
| 183 | `created_by_user_account_id` int(11) NOT NULL, |
||
| 184 | `last_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, |
||
| 185 | `last_modified_user_account_id` int(11) NOT NULL, |
||
| 186 | `primary` tinyint(1) NOT NULL DEFAULT '0', |
||
| 187 | `active` tinyint(1) NOT NULL DEFAULT '1', |
||
| 188 | PRIMARY KEY (`user_account_addresses_id`), |
||
| 189 | KEY `created_by_user_account_id` (`created_by_user_account_id`), |
||
| 190 | KEY `last_modified_user_account_id` (`last_modified_user_account_id`) |
||
| 191 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores user account addresses'"); |
||
| 192 | $statement->execute(); |
||
| 193 | $error = $db->errorInfo(); |
||
| 194 | if ($error[0] != "00000") { |
||
| 195 | var_dump($db->errorInfo()); |
||
| 196 | die('CREATE TABLE `user_account_addresses` failed.'); |
||
| 197 | } |
||
| 198 | |||
| 199 | $statement = $db->prepare("CREATE TABLE `group` ( |
||
| 200 | `group_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 201 | `name` varchar(100) NOT NULL DEFAULT '', |
||
| 202 | `abbreviation` varchar(10) NOT NULL DEFAULT '', |
||
| 203 | `description` mediumtext NOT NULL, |
||
| 204 | `address_1` varchar(50) DEFAULT NULL, |
||
| 205 | `address_2` varchar(50) DEFAULT NULL, |
||
| 206 | `city` varchar(50) NOT NULL DEFAULT '', |
||
| 207 | `state` char(2) NOT NULL DEFAULT '', |
||
| 208 | `zip` varchar(10) NOT NULL, |
||
| 209 | `date_created` datetime NOT NULL, |
||
| 210 | `created_by_user_account_id` int(11) NOT NULL, |
||
| 211 | `last_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, |
||
| 212 | `last_modified_user_account_id` int(11) NOT NULL, |
||
| 213 | `active` tinyint(1) NOT NULL DEFAULT '1', |
||
| 214 | PRIMARY KEY (`group_id`), |
||
| 215 | KEY `created_by_user_account_id` (`created_by_user_account_id`), |
||
| 216 | KEY `last_modified_user_account_id` (`last_modified_user_account_id`) |
||
| 217 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores groups for user accounts'"); |
||
| 218 | $statement->execute(); |
||
| 219 | $error = $db->errorInfo(); |
||
| 220 | if ($error[0] != "00000") { |
||
| 221 | var_dump($db->errorInfo()); |
||
| 222 | die('CREATE TABLE `group` failed.'); |
||
| 223 | } |
||
| 224 | |||
| 225 | $statement = $db->prepare("INSERT INTO `group` ( |
||
| 226 | `group_id` |
||
| 227 | ,`name` |
||
| 228 | ,`abbreviation` |
||
| 229 | ,`description` |
||
| 230 | ,`address_1` |
||
| 231 | ,`address_2` |
||
| 232 | ,`city` |
||
| 233 | ,`state` |
||
| 234 | ,`zip` |
||
| 235 | ,`date_created` |
||
| 236 | ,`created_by_user_account_id` |
||
| 237 | ,`last_modified` |
||
| 238 | ,`last_modified_user_account_id` |
||
| 239 | ,`active` |
||
| 240 | ) |
||
| 241 | VALUES (1 |
||
| 242 | ,'Global Group' |
||
| 243 | ,'GLOBAL' |
||
| 244 | ,'Global Web App Group' |
||
| 245 | ,'ADDRESS PLACEHOLDER' |
||
| 246 | ,'' |
||
| 247 | ,'CITY PLACEHOLDER' |
||
| 248 | ,'STATE PLACEHOLDER' |
||
| 249 | ,'12345' |
||
| 250 | ,NOW() |
||
| 251 | ,:user_account_id |
||
| 252 | ,NOW() |
||
| 253 | ,:user_account_id |
||
| 254 | ,1) |
||
| 255 | "); |
||
| 256 | $statement->bindValue(":user_account_id", $last_inserted_user_account_id, PDO::PARAM_INT); |
||
| 257 | $statement->execute(); |
||
| 258 | $error = $db->errorInfo(); |
||
| 259 | if ($error[0] != "00000") { |
||
| 260 | var_dump($db->errorInfo()); |
||
| 261 | die('The INSERT INTO `group` failed.'); |
||
| 262 | } |
||
| 263 | |||
| 264 | $statement = $db->prepare("CREATE TABLE `group_closure_table` ( |
||
| 265 | `ancestor` int(10) NOT NULL DEFAULT '0', |
||
| 266 | `descendant` int(10) NOT NULL DEFAULT '0', |
||
| 267 | `pathlength` int(10) NOT NULL DEFAULT '0', |
||
| 268 | PRIMARY KEY (`ancestor`,`descendant`) |
||
| 269 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table was from the guidance of Mr. Bill Karwin'"); |
||
| 270 | $statement->execute(); |
||
| 271 | $error = $db->errorInfo(); |
||
| 272 | if ($error[0] != "00000") { |
||
| 273 | var_dump($db->errorInfo()); |
||
| 274 | die('The CREATE TABLE `group_closure_table` failed.'); |
||
| 275 | } |
||
| 276 | |||
| 277 | $statement = $db->prepare("INSERT INTO `group_closure_table` ( |
||
| 278 | `ancestor` |
||
| 279 | ,`descendant` |
||
| 280 | ,`pathlength` |
||
| 281 | ) |
||
| 282 | VALUES (1,1,0) |
||
| 283 | "); |
||
| 284 | $statement->execute(); |
||
| 285 | $error = $db->errorInfo(); |
||
| 286 | if ($error[0] != "00000") { |
||
| 287 | var_dump($db->errorInfo()); |
||
| 288 | die('The INSERT INTO `group_closure_table` failed.'); |
||
| 289 | } |
||
| 290 | |||
| 291 | $statement = $db->prepare("CREATE TABLE `user_account_groups` ( |
||
| 292 | `role_id` int(10) NOT NULL DEFAULT '0', |
||
| 293 | `user_account_id` int(10) NOT NULL DEFAULT '0', |
||
| 294 | `group_id` int(10) NOT NULL DEFAULT '0', |
||
| 295 | `user_account_groups_id` int(10) NOT NULL AUTO_INCREMENT, |
||
| 296 | PRIMARY KEY (`user_account_groups_id`), |
||
| 297 | KEY `role_id` (`role_id`), |
||
| 298 | KEY `user_account_id` (`user_account_id`), |
||
| 299 | KEY `group_id` (`group_id`) |
||
| 300 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores user account groups'"); |
||
| 301 | $statement->execute(); |
||
| 302 | $error = $db->errorInfo(); |
||
| 303 | if ($error[0] != "00000") { |
||
| 304 | var_dump($db->errorInfo()); |
||
| 305 | die('CREATE TABLE `user_account_groups` failed.'); |
||
| 306 | } |
||
| 307 | |||
| 308 | $statement = $db->prepare("CREATE TABLE `user_account_proxy` ( |
||
| 309 | `user_account_groups_id` int(10) NOT NULL DEFAULT '0', |
||
| 310 | `proxy_user_account_id` int(10) NOT NULL DEFAULT '0', |
||
| 311 | PRIMARY KEY (`user_account_groups_id`,`proxy_user_account_id`) |
||
| 312 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores user account proxy users'"); |
||
| 313 | $statement->execute(); |
||
| 314 | $error = $db->errorInfo(); |
||
| 315 | if ($error[0] != "00000") { |
||
| 316 | var_dump($db->errorInfo()); |
||
| 317 | die('CREATE TABLE `user_account_proxy` failed.'); |
||
| 318 | } |
||
| 319 | |||
| 320 | $statement = $db->prepare("CREATE TABLE `user_account_roles` ( |
||
| 321 | `role_id` int(10) NOT NULL AUTO_INCREMENT, |
||
| 322 | `label` varchar(50) DEFAULT NULL, |
||
| 323 | PRIMARY KEY (`role_id`) |
||
| 324 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table stores user account roles'"); |
||
| 325 | $statement->execute(); |
||
| 326 | $error = $db->errorInfo(); |
||
| 327 | if ($error[0] != "00000") { |
||
| 328 | var_dump($db->errorInfo()); |
||
| 329 | die('CREATE TABLE `user_account_roles` failed.'); |
||
| 330 | } |
||
| 331 | |||
| 332 | $statement = $db->prepare("INSERT INTO `user_account_roles` (`role_id`,`label`) |
||
| 333 | VALUES |
||
| 334 | (1, 'Administrator'), |
||
| 335 | (2, 'Author'), |
||
| 336 | (3, 'Proxy'), |
||
| 337 | (4, 'Editor'), |
||
| 338 | (5, 'Manager'), |
||
| 339 | (6, 'Universal Administrator') |
||
| 340 | "); |
||
| 341 | $statement->execute(); |
||
| 342 | $error = $db->errorInfo(); |
||
| 343 | if ($error[0] != "00000") { |
||
| 344 | var_dump($db->errorInfo()); |
||
| 345 | die('The INSERT INTO `user_account_roles` failed.'); |
||
| 346 | } |
||
| 347 | |||
| 348 | // INSERT this user into the user_account_groups table with "Universal Administrator" privileges |
||
| 349 | $statement = $db->prepare("INSERT INTO user_account_groups |
||
| 350 | (role_id, user_account_id, group_id) |
||
| 351 | VALUES ( 6, :user_account_id, 1 ), ( 1, :user_account_id, 1 )"); |
||
| 352 | $statement->bindValue(":user_account_id", $last_inserted_user_account_id, PDO::PARAM_INT); |
||
| 353 | $statement->execute(); |
||
| 354 | $error = $db->errorInfo(); |
||
| 355 | if ($error[0] != "00000") { |
||
| 356 | var_dump($db->errorInfo()); |
||
| 357 | die('The INSERT INTO user_account_groups failed.'); |
||
| 358 | } |
||
| 359 | |||
| 360 | $statement = $db->prepare("CREATE TABLE `login_attempt` ( |
||
| 361 | `login_attempt_id` int(11) NOT NULL AUTO_INCREMENT, |
||
| 362 | `user_account_email` varchar(255) NOT NULL, |
||
| 363 | `ip_address` varchar(255) NOT NULL DEFAULT '0', |
||
| 364 | `result` varchar(255) DEFAULT NULL, |
||
| 365 | `page` varchar(255) DEFAULT NULL, |
||
| 366 | `created_date` datetime DEFAULT NULL, |
||
| 367 | PRIMARY KEY (`login_attempt_id`) |
||
| 368 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This table is used to log login attempts'"); |
||
| 369 | $statement->execute(); |
||
| 370 | $error = $db->errorInfo(); |
||
| 371 | if ($error[0] != "00000") { |
||
| 372 | var_dump($db->errorInfo()); |
||
| 373 | die('The CREATE TABLE `login_attempt` failed.'); |
||
| 374 | } |
||
| 375 | |||
| 376 | // Don't return the user account password and the CSRF key value. |
||
| 377 | unset($data['user_account_password']); |
||
| 378 | unset($data['csrf_key']); |
||
| 379 | |||
| 380 | $data['success_message'] = 'installed'; |
||
| 381 | } |
||
| 382 | |||
| 383 | if (!$posted_data) { |
||
| 384 | $data['cname'] = $_SERVER['SERVER_NAME']; |
||
| 385 | $data['database_host'] = 'localhost'; |
||
| 386 | } |
||
| 387 | |||
| 388 | $app->render('form.php', array( |
||
| 389 | "page_title" => "Web Application Installer", "hide_page_header" => true, "path_to_this_module" => $final_global_template_vars["path_to_this_module"], "errors" => $default_validation_errors, "data" => $data |
||
| 390 | )); |
||
| 391 | } |
||
| 392 |