qa-install.php 11.3 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-install.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: User interface for installing, upgrading and fixing the database


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-db-install.php';
Scott Vivian committed
33

Gideon Greenspan committed
34 35 36
	qa_report_process_stage('init_install');


Scott Vivian committed
37
//	Output start of HTML early, so we can see a nicely-formatted list of database queries when upgrading
Gideon Greenspan committed
38 39

?>
Gideon Greenspan committed
40 41 42 43
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<style type="text/css">
Gideon Greenspan committed
44 45 46
			body,input {font-size:16px; font-family:Verdana, Arial, Helvetica, sans-serif;}
			body {text-align:center; width:640px; margin:64px auto;}
			table {margin: 16px auto;}
Gideon Greenspan committed
47 48 49
		</style>
	</head>
	<body>
Gideon Greenspan committed
50 51 52 53 54 55 56 57 58 59 60 61 62
<?php


//	Define database failure handler for install process, if not defined already (file could be included more than once)

	if (!function_exists('qa_install_db_fail_handler')) {

		function qa_install_db_fail_handler($type, $errno=null, $error=null, $query=null)
	/*
		Handler function for database failures during the installation process
	*/
		{
			global $pass_failure_from_install;
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66 67 68
			$pass_failure_type=$type;
			$pass_failure_errno=$errno;
			$pass_failure_error=$error;
			$pass_failure_query=$query;
			$pass_failure_from_install=true;
Scott Vivian committed
69

Gideon Greenspan committed
70
			require QA_INCLUDE_DIR.'qa-install.php';
Scott Vivian committed
71

Gideon Greenspan committed
72 73
			qa_exit('error');
		}
Scott Vivian committed
74

Gideon Greenspan committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	}


	$success='';
	$errorhtml='';
	$suggest='';
	$buttons=array();
	$fields=array();
	$fielderrors=array();
	$hidden=array();

	if (isset($pass_failure_type)) { // this page was requested due to query failure, via the fail handler
		switch ($pass_failure_type) {
			case 'connect':
				$errorhtml.='Could not establish database connection. Please check the username, password and hostname in the config file, and if necessary set up the appropriate MySQL user and privileges.';
				break;
Scott Vivian committed
91

Gideon Greenspan committed
92 93 94
			case 'select':
				$errorhtml.='Could not switch to the Question2Answer database. Please check the database name in the config file, and if necessary create the database in MySQL and grant appropriate user privileges.';
				break;
Scott Vivian committed
95

Gideon Greenspan committed
96 97
			case 'query':
				global $pass_failure_from_install;
Scott Vivian committed
98

Gideon Greenspan committed
99
				if (@$pass_failure_from_install)
Gideon Greenspan committed
100
					$errorhtml.="Question2Answer was unable to perform the installation query below. Please check the user in the config file has CREATE and ALTER permissions:\n\n".qa_html($pass_failure_query."\n\nError ".$pass_failure_errno.": ".$pass_failure_error."\n\n");
Gideon Greenspan committed
101
				else
Gideon Greenspan committed
102
					$errorhtml.="A Question2Answer database query failed when generating this page.\n\nA full description of the failure is available in the web server's error log file.";
Gideon Greenspan committed
103 104 105 106 107
				break;
		}

	} else { // this page was requested by user GET/POST, so handle any incoming clicks on buttons
		qa_db_connect('qa_install_db_fail_handler');
Scott Vivian committed
108

Gideon Greenspan committed
109 110
		if (qa_clicked('create')) {
			qa_db_install_tables();
Scott Vivian committed
111

Gideon Greenspan committed
112 113 114 115
			if (QA_FINAL_EXTERNAL_USERS) {
				if (defined('QA_FINAL_WORDPRESS_INTEGRATE_PATH')) {
					require_once QA_INCLUDE_DIR.'qa-db-admin.php';
					require_once QA_INCLUDE_DIR.'qa-app-format.php';
Scott Vivian committed
116

Gideon Greenspan committed
117 118
					qa_db_page_move(qa_db_page_create(get_option('blogname'), QA_PAGE_FLAGS_EXTERNAL, get_option('home'), null, null, null), 'O', 1);
						// create link back to WordPress home page
Scott Vivian committed
119

Gideon Greenspan committed
120 121 122 123
					$success.='Your Question2Answer database has been created and integrated with your WordPress site.';

				} else
					$success.='Your Question2Answer database has been created for external user identity management. Please read the online documentation to complete integration.';
Scott Vivian committed
124

Gideon Greenspan committed
125 126 127
			} else
				$success.='Your Question2Answer database has been created.';
		}
Scott Vivian committed
128

Gideon Greenspan committed
129 130 131 132
		if (qa_clicked('nonuser')) {
			qa_db_install_tables();
			$success.='The additional Question2Answer database tables have been created.';
		}
Scott Vivian committed
133

Gideon Greenspan committed
134 135 136 137 138 139 140 141 142
		if (qa_clicked('upgrade')) {
			qa_db_upgrade_tables();
			$success.='Your Question2Answer database has been updated.';
		}

		if (qa_clicked('repair')) {
			qa_db_install_tables();
			$success.='The Question2Answer database tables have been repaired.';
		}
Scott Vivian committed
143

Gideon Greenspan committed
144 145 146
		if (qa_clicked('module')) {
			$moduletype=qa_post_text('moduletype');
			$modulename=qa_post_text('modulename');
Scott Vivian committed
147

Gideon Greenspan committed
148
			$module=qa_load_module($moduletype, $modulename);
Scott Vivian committed
149

Gideon Greenspan committed
150
			$queries=$module->init_queries(qa_db_list_tables_lc());
Scott Vivian committed
151

Gideon Greenspan committed
152 153 154
			if (!empty($queries)) {
				if (!is_array($queries))
					$queries=array($queries);
Scott Vivian committed
155

Gideon Greenspan committed
156 157 158 159 160 161 162 163 164 165
				foreach ($queries as $query)
					qa_db_upgrade_query($query);
			}

			$success.='The '.$modulename.' '.$moduletype.' module has completed database initialization.';
		}

		if (qa_clicked('super')) {
			require_once QA_INCLUDE_DIR.'qa-db-users.php';
			require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
Scott Vivian committed
166

Gideon Greenspan committed
167 168 169
			$inemail=qa_post_text('email');
			$inpassword=qa_post_text('password');
			$inhandle=qa_post_text('handle');
Scott Vivian committed
170

Gideon Greenspan committed
171 172 173 174
			$fielderrors=array_merge(
				qa_handle_email_filter($inhandle, $inemail),
				qa_password_validate($inpassword)
			);
Scott Vivian committed
175

Gideon Greenspan committed
176 177
			if (empty($fielderrors)) {
				require_once QA_INCLUDE_DIR.'qa-app-users.php';
Scott Vivian committed
178

Gideon Greenspan committed
179 180
				$userid=qa_create_new_user($inemail, $inpassword, $inhandle, QA_USER_LEVEL_SUPER);
				qa_set_logged_in_user($userid, $inhandle);
Scott Vivian committed
181

Gideon Greenspan committed
182
				qa_set_option('feedback_email', $inemail);
Scott Vivian committed
183

Gideon Greenspan committed
184 185 186 187
				$success.="Congratulations - Your Question2Answer site is ready to go!\n\nYou are logged in as the super administrator and can start changing settings.\n\nThank you for installing Question2Answer.";
			}
		}
	}
Scott Vivian committed
188

Gideon Greenspan committed
189 190
	if (is_resource(qa_db_connection(false)) && !@$pass_failure_from_install) {
		$check=qa_db_check_tables(); // see where the database is at
Scott Vivian committed
191

Gideon Greenspan committed
192 193 194 195
		switch ($check) {
			case 'none':
				if (@$pass_failure_errno==1146) // don't show error if we're in installation process
					$errorhtml='';
Scott Vivian committed
196

Gideon Greenspan committed
197 198 199 200
				$errorhtml.='Welcome to Question2Answer. It\'s time to set up your database!';

				if (QA_FINAL_EXTERNAL_USERS) {
					if (defined('QA_FINAL_WORDPRESS_INTEGRATE_PATH'))
Gideon Greenspan committed
201
						$errorhtml.="\n\nWhen you click below, your Question2Answer site will be set up to integrate with the users of your WordPress site <a href=\"".qa_html(get_option('home'))."\" target=\"_blank\">".qa_html(get_option('blogname'))."</a>. Please consult the online documentation for more information.";
Gideon Greenspan committed
202 203
					else
						$errorhtml.="\n\nWhen you click below, your Question2Answer site will be set up to integrate with your existing user database and management. Users will be referenced with database column type ".qa_html(qa_get_mysql_user_column_type()).". Please consult the online documentation for more information.";
Scott Vivian committed
204

Gideon Greenspan committed
205 206 207 208 209 210
					$buttons=array('create' => 'Create Database');
				} else {
					$errorhtml.="\n\nWhen you click below, your Question2Answer database will be set up to manage user identities and logins internally.\n\nIf you want to offer a single sign-on for an existing user base or website, please consult the online documentation before proceeding.";
					$buttons=array('create' => 'Create Database including User Management');
				}
				break;
Scott Vivian committed
211

Gideon Greenspan committed
212 213 214
			case 'old-version':
				if (!@$pass_failure_from_install)
					$errorhtml=''; // don't show error if we need to upgrade
Scott Vivian committed
215

Gideon Greenspan committed
216 217 218
				$errorhtml.='Your Question2Answer database needs to be upgraded for this version of the software.'; // don't show error before this
				$buttons=array('upgrade' => 'Upgrade Database');
				break;
Scott Vivian committed
219

Gideon Greenspan committed
220
			case 'non-users-missing':
Gideon Greenspan committed
221
				$errorhtml='This Question2Answer site is sharing its users with another Q2A site, but it needs some additional database tables for its own content. Please click below to create them.';
Gideon Greenspan committed
222 223
				$buttons=array('nonuser' => 'Create Tables');
				break;
Scott Vivian committed
224

Gideon Greenspan committed
225 226 227 228
			case 'table-missing':
				$errorhtml.='One or more tables are missing from your Question2Answer database.';
				$buttons=array('repair' => 'Repair Database');
				break;
Scott Vivian committed
229

Gideon Greenspan committed
230 231 232 233
			case 'column-missing':
				$errorhtml.='One or more Question2Answer database tables are missing a column.';
				$buttons=array('repair' => 'Repair Database');
				break;
Scott Vivian committed
234

Gideon Greenspan committed
235 236
			default:
				require_once QA_INCLUDE_DIR.'qa-db-admin.php';
Scott Vivian committed
237

Gideon Greenspan committed
238 239 240 241
				if ( (!QA_FINAL_EXTERNAL_USERS) && (qa_db_count_users()==0) ) {
					$errorhtml.="There are currently no users in the Question2Answer database.\n\nPlease enter your details below to create the super administrator:";
					$fields=array('handle' => 'Username:', 'password' => 'Password:', 'email' => 'Email address:');
					$buttons=array('super' => 'Create Super Administrator');
Scott Vivian committed
242

Gideon Greenspan committed
243 244
				} else {
					$tables=qa_db_list_tables_lc();
Scott Vivian committed
245

Gideon Greenspan committed
246
					$moduletypes=qa_list_module_types();
Scott Vivian committed
247

Gideon Greenspan committed
248 249
					foreach ($moduletypes as $moduletype) {
						$modules=qa_load_modules_with($moduletype, 'init_queries');
Scott Vivian committed
250

Gideon Greenspan committed
251 252 253
						foreach ($modules as $modulename => $module) {
							$queries=$module->init_queries($tables);
							if (!empty($queries)) { // also allows single query to be returned
Gideon Greenspan committed
254
								$errorhtml=strtr(qa_lang_html('admin/module_x_database_init'), array(
Gideon Greenspan committed
255 256 257 258 259
									'^1' => qa_html($modulename),
									'^2' => qa_html($moduletype),
									'^3' => '',
									'^4' => '',
								));
Scott Vivian committed
260

Gideon Greenspan committed
261
								$buttons=array('module' => 'Initialize Database');
Scott Vivian committed
262

Gideon Greenspan committed
263 264 265 266 267 268 269 270 271 272
								$hidden['moduletype']=$moduletype;
								$hidden['modulename']=$modulename;
								break;
							}
						}
					}
				}
				break;
		}
	}
Scott Vivian committed
273

Gideon Greenspan committed
274 275 276
	if (empty($errorhtml)) {
		if (empty($success))
			$success='Your Question2Answer database has been checked with no problems.';
Scott Vivian committed
277

Gideon Greenspan committed
278
		$suggest='<a href="'.qa_path_html('admin', null, null, QA_URL_FORMAT_SAFEST).'">Go to admin center</a>';
Gideon Greenspan committed
279 280 281 282
	}

?>

Gideon Greenspan committed
283
		<form method="post" action="<?php echo qa_path_html('install', null, null, QA_URL_FORMAT_SAFEST)?>">
Gideon Greenspan committed
284 285 286 287

<?php

	if (strlen($success))
Gideon Greenspan committed
288
		echo '<p><font color="#006600">'.nl2br(qa_html($success)).'</font></p>'; // green
Scott Vivian committed
289

Gideon Greenspan committed
290
	if (strlen($errorhtml))
Gideon Greenspan committed
291
		echo '<p><font color="#990000">'.nl2br($errorhtml).'</font></p>'; // red
Scott Vivian committed
292

Gideon Greenspan committed
293
	if (strlen($suggest))
Gideon Greenspan committed
294
		echo '<p>'.$suggest.'</p>';
Gideon Greenspan committed
295 296 297 298 299


//	Very simple general form display logic (we don't use theme since it depends on tons of DB options)

	if (count($fields)) {
Gideon Greenspan committed
300
		echo '<table>';
Scott Vivian committed
301

Gideon Greenspan committed
302
		foreach ($fields as $name => $prompt) {
Gideon Greenspan committed
303
			echo '<tr><td>'.qa_html($prompt).'</td><td><input type="text" size="24" name="'.qa_html($name).'" value="'.qa_html(@${'in'.$name}).'"></td>';
Gideon Greenspan committed
304
			if (isset($fielderrors[$name]))
Gideon Greenspan committed
305 306
				echo '<td><font color="#990000"><small>'.qa_html($fielderrors[$name]).'</small></font></td>';
			echo '</tr>';
Gideon Greenspan committed
307
		}
Scott Vivian committed
308

Gideon Greenspan committed
309
		echo '</table>';
Gideon Greenspan committed
310
	}
Scott Vivian committed
311

Gideon Greenspan committed
312
	foreach ($buttons as $name => $value)
Gideon Greenspan committed
313
		echo '<input type="submit" name="'.qa_html($name).'" value="'.qa_html($value).'">';
Scott Vivian committed
314

Gideon Greenspan committed
315
	foreach ($hidden as $name => $value)
Gideon Greenspan committed
316
		echo '<input type="hidden" name="'.qa_html($name).'" value="'.qa_html($value).'">';
Gideon Greenspan committed
317 318 319 320

	qa_db_disconnect();
?>

Gideon Greenspan committed
321 322 323
		</form>
	</body>
</html>