summaryrefslogtreecommitdiff
path: root/installer/index.php
blob: 5ea418bc031cad8b9c09293f09e20503e0365f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php

$BASE = dirname(dirname(__FILE__));
set_include_path(get_include_path()
                 .PATH_SEPARATOR. "$BASE/src/lib"
                 .PATH_SEPARATOR. "$BASE/src/ext"
                 );
$uri = $_SERVER['REQUEST_URI'];

require_once('include.php');

$conf_file = "$BASE/conf.php";
if (!mm_isSqlConfigured($conf_file)) {
	require_once('Template.class.php');
	$t = new Template($BASE);
	
	$t->header('Message Manager: Installer');

	$t->paragraph("First we need to set up the SQL configuration, ".
	              "then we will set up the first user.");
	
	$t->openTag('form', array('method'=>'post','action'=>$uri));
	$t->tag('input', array('type'=>'hidden', 'name'=>'try', 'value'=>'t'));
	$try = isset($_POST['try']);
	
	$mysql = false;
	if ($try) {
		@$mysql = mysql_connect($_POST['db_host'],
		                        $_POST['db_user'],
		                        $_POST['db_password']);
	}
	
	////////////////////////////////////////////////////////////////////////
	
	$t->openFieldset("MySQL Authentication", $mysql);
	$t->inputText('db_host', 'Hostname', '',
	              getParam('db_host','localhost'), $mysql);
	$t->inputText('db_user', 'Username', '',
	              getParam('db_user'), $mysql);
	$t->inputPassword('db_password', 'Password', '',
	                  getParam('db_password'), $mysql);
	if ($try && !$mysql) {
		$t->inputP("Could not authenticate: ".mysql_error(), true);
	}
	$t->closeFieldset();
	
	////////////////////////////////////////////////////////////////////////
	
	$charset = false;
	if ($mysql) {
		$charset = mysql_set_charset($_POST['db_charset'], $mysql);
		if (!$charset) {
			$charset_error = mysql_error($mysql);
		}
	}
	
	$db = false;
	$db_message = '';
	if ($charset) {
		$t->setRet(true);
		$db = mm_mysql_db($mysql, $_POST['db_name'], $db_message);
		$t->setRet(false);
	}
	
	$db_prefix = $_POST['db_prefix'];
	
	$table = false;
	if ($db) {
		$table_exists = mm_mysql_table_exists($mysql,$db_prefix.'auth');
		if (!$table_exists) {
			$query =
				'CREATE TABLE '.$db_prefix."auth (\n".
				"  uid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n".
				"  name VARCHAR(255),\n".
				"  hash CHAR(60)\n".
				"  status INT\n"
				");";
			$table = mysql_query($query);
			if (!$table) {
				$table_error = mysql_error($mysql);
			}
		} else {
			$table = true;
		}
	}
	
	////////////////////////////////////////////////////////////////////////
	
	$t->openFieldset("MySQL Settings", $table);
	
	$t->inputText('db_charset', 'Charset',
	              "I've heard that you may need to change this if ".
	              "you use an old version of MySQL. 'utf8' is ".
	              "generally a good option, though.",
	              getParam('db_charset','utf8'), $table);
	if ($mysql) {
		$str = $_POST['db_charset'];
		if ($charset) {
			$t->inputP("Set charset to <q>$str</q>.");
		} else {
			$t->inputP("Could not set charset to ".
			           "<q>$str</q>: ".$charset_error, true);
		}
	}
	
	$t->inputText('db_name', 'Database name', '',
	              getParam('db_name', 'messagemanager'), $table);
	echo $db_message;
	
	$t->inputText('db_prefix', 'Table prefix',
	              'Just use simple characters, like [A-Za-z0-9_], '.
	              'and keep it short.',
	              getParam('db_prefix','mm_'), $table);
	
	if ($db) {
		$db_name = '<q>'.$db_prefix.'auth</q>';
		if ($table) {
			if ($table_exists) {
				$msg="Table $db_name already exists.";
			} else {
				$msg="Created table $db_name.";
			}
		} else {
			$msg="Could not create table $db_name: ".$table_error;
		}
		$t->inputP($msg, !$table);
	}
	
	$t->closeFieldset();
	
	////////////////////////////////////////////////////////////////////////
	
	$fh = false;
	if ($table) {
		$fh = fopen('conf.php', 'w');
		if ($fh === FALSE) {
			$msg="Could not open file <q>conf.php</q> for writing.";
			$template->paragraph($msg, true);
		} else { 
			fwrite($fh, '<?php global $db_config;'."\n");
			fwrite($fh, configStr('host'));
			fwrite($fh, configStr('user'));
			fwrite($fh, configStr('password'));
			fwrite($fh, "\n");
			fwrite($fh, configStr('charset'));
			fwrite($fh, configStr('name'));
			fwrite($fh, configStr('prefix'));
			fclose($fh);
		}
	}
	if ($fh) {
		$t->closeTag('form');
		$t->openTag('form', array('action'=>$uri));
		$t->tag('input', array('type'=>'submit',
		                       'value'=>'Cool beans, go to step 2!'));
	} else {
		$t->tag('input', array('type'=>'submit', 'value'=>'Submit'));
	}
	$t->closeTag('form');
	$t->footer();
	////////////////////////////////////////////////////////////////////////
} else {
	require_once('MessageManager.class.php');
	$m = new MessageManager($conf_file);
	$t = $m->template();
	
	$t->header('Message Manager: Installer');
	
	$user_count = $m->countUsers();
	
	if ($user_count<1) {
		$t->openTag('form', array('method'=>'post', 'action'=>$uri));
		$t->tag('input', array('type'=>'hidden',
		                       'id'=>'try',
		                       'name'=>'try',
		                       'value'=>'t'));
		$try = isset($_POST['try']);
		
		$pw = false;
		if ($try) {
			$pw = ( $_POST['mm_password'] ===
			        $_POST['mm_password_verify'] );
		}
		
		$admin = false;
		if ($pw) {
			$user = $_POST['mm_user'];
			$password = $_POST['mm_password'];
			
			$uid = $m->addUser($user, $password);
			$admin = $m->setStatus($uid, 2);
			if (!$admin) {
				$admin_error = mysql_error($mysql);
			}
		}
		
		////////////////////////////////////////////////////////////////
		
		$t->openFieldset("First Account (administrator)",$admin);
		$t->inputText('mm_user', 'Username',
		              "Must be <= 255 characters.",
		              getParam('mm_user','root'), $admin);
		$t->inputNewPassword('mm_password', 'Password',
		                     ($pw?getParam('mm_password'):''),
		                     $admin);
		if ($try && !$pw) {
			$msg="Passwords don't match.";
			$template->inputP($msg, true);
		}
		if ($pw) {
			$user = "<q>".$_POST['mm_user'].'</q>';
			if ($admin) {
				$msg="Created user $user.";
			} else {
				$msg="Could not create user $user: ".
					$admin_error;
			}
			$t->inputP($msg, !$admin);
		}
		$t->closeFieldset();
		
		////////////////////////////////////////////////////////////////
		
		if (!$admin) {
			$t->tag('input', array('type'=>'submit',
			                       'value'=>'Submit'));
		}
		$t->closeTag('form');
	} else {
		$t->paragraph("File conf.php already exists, and there ".
		                     "is at least one user. Return to the ".
		                     "<a href='index.php'>main page</a>.");
	}
	$t->footer();
}