We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

install.php 13.3 KB
Newer Older
mattl's avatar
mattl committed
1 2
<?php

3
/* GNUkebox -- a free software server for recording your listening habits
mattl's avatar
mattl committed
4

5
   Copyright (C) 2009 Free Software Foundation, Inc
mattl's avatar
mattl committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

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

   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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

clint's avatar
clint committed
22 23
require_once('adodb/adodb-exceptions.inc.php');
require_once('adodb/adodb.inc.php');
24
require_once('version.php');
25
require_once('utils/get_absolute_url.php');
26

27 28
if (file_exists('config.php')) {
	die('A configuration file already exists. Please delete <i>config.php</i> if you wish to reinstall.');
29 30 31 32 33 34
}

if (isset($_POST['install'])) {

	//Get the database connection string
	$dbms = $_POST['dbms'];
35
	if ($dbms == 'sqlite') {
Reiner Herrmann's avatar
Reiner Herrmann committed
36
		$filename = urlencode($_POST['filename']);
37
		$connect_string = 'sqlite://' . $filename;
38
	} else {
39
		$connect_string = $dbms . '://' . $_POST['username'] . ':' . $_POST['password'] . '@' . $_POST['hostname'] . ':' . $_POST['port'] . '/' . $_POST['dbname'];
40
	}
41 42

	$adodb_connect_string = str_replace('pgsql:', 'postgres:', $connect_string);
43

clint's avatar
clint committed
44 45
	try {
		$adodb =& NewADOConnection($adodb_connect_string);
46
	} catch (Exception $e) {
clint's avatar
clint committed
47 48
		var_dump($e);
		adodb_backtrace($e->gettrace());
clint's avatar
clint committed
49
		die("Database connection failure\n");
50 51 52
	}

	//Create tables
53

54
	$stage_one_queries = array(
55
		'CREATE TABLE Places(
jurgbohn's avatar
jurgbohn committed
56
		location_uri VARCHAR(255) unique,
57 58
		latitude FLOAT,
		longitude FLOAT,
59
		country CHAR(2))',
60

61
		'CREATE TABLE Countries (
tobyink's avatar
tobyink committed
62 63
		country varchar(2) PRIMARY KEY,
		country_name varchar(200),
64
		wikipedia_en varchar(120));',
tobyink's avatar
tobyink committed
65

66
		'CREATE TABLE Users (
clint's avatar
clint committed
67
		uniqueid SERIAL PRIMARY KEY,
68
		username VARCHAR(64) unique,
69 70 71
		password VARCHAR(32) NOT NULL,
		email VARCHAR(255),
		fullname VARCHAR(255),
72 73 74
		bio TEXT,
		homepage VARCHAR(255),
		location VARCHAR(255),
75
		userlevel INTEGER DEFAULT 0,
clint's avatar
clint committed
76
		anticommercial INTEGER DEFAULT 0,
77 78
		webid_uri VARCHAR(255),
		avatar_uri VARCHAR(255),
79
		openid_uri VARCHAR(100),
80
		active INTEGER DEFAULT 0,
mattl's avatar
mattl committed
81
		public_export INTEGER DEFAULT 0,
tobyink's avatar
tobyink committed
82 83
		location_uri VARCHAR(255) REFERENCES Places(location_uri),
		laconica_profile VARCHAR(255),
84
		created INTEGER DEFAULT 0,
85
		modified INTEGER DEFAULT 0,
86
		journal_rss VARCHAR(255))',
87

88
		'CREATE TABLE Groups (
89 90
		id SERIAL PRIMARY KEY,
		groupname VARCHAR(64),
clint's avatar
clint committed
91
		owner INTEGER REFERENCES Users(uniqueid),
tobyink's avatar
tobyink committed
92 93 94
		fullname VARCHAR(255),
		bio TEXT,
		homepage VARCHAR(255),
elleo's avatar
elleo committed
95
		created INTEGER NOT NULL,
tobyink's avatar
tobyink committed
96
		modified INTEGER,
tobyink's avatar
tobyink committed
97
		avatar_uri VARCHAR(255),
98
		grouptype INTEGER)',
tobyink's avatar
tobyink committed
99

100
		'CREATE TABLE Group_Members (
101
		grp INTEGER REFERENCES Groups(id),
clint's avatar
clint committed
102
		member INTEGER REFERENCES Users(uniqueid),
103
		joined INTEGER NOT NULL,
104
		PRIMARY KEY (grp, member))',
tobyink's avatar
tobyink committed
105

106
		# TODO: REMOVE
107
		'CREATE TABLE AccountActivation(
jurgbohn's avatar
jurgbohn committed
108
		username VARCHAR(64),
109
		authcode VARCHAR(32),
110
		expires INTEGER)',
jurgbohn's avatar
jurgbohn committed
111

112
		'CREATE TABLE Auth (
113
		token VARCHAR(32) PRIMARY KEY,
elleo's avatar
elleo committed
114
		sk VARCHAR(32),
clint's avatar
clint committed
115
		expires INTEGER,
116
		username VARCHAR(64) REFERENCES Users(username))',
117

118
		'CREATE TABLE Artist(
119
		id SERIAL PRIMARY KEY,
120
		name VARCHAR(255) unique,
121
		mbid VARCHAR(36),
122
		imbid INTEGER,
clint's avatar
clint committed
123 124
		streamable INTEGER,
		bio_published INTEGER,
125 126 127 128
		bio_content TEXT,
		bio_summary TEXT,
		image_small VARCHAR(255),
		image_medium VARCHAR(255),
jurgbohn's avatar
jurgbohn committed
129
		image_large VARCHAR(255),
tobyink's avatar
tobyink committed
130
		homepage VARCHAR(255),
131
		hashtag VARCHAR(255),
132
		origin VARCHAR(255) REFERENCES Places(location_uri),
133
		flattr_uid VARCHAR(255))',
134

135
		'CREATE TABLE Album(
136
		id SERIAL PRIMARY KEY,
137
		name VARCHAR(255),
138 139
		artist_name VARCHAR(255) REFERENCES Artist(name),
		mbid VARCHAR(36),
140 141
		image VARCHAR(255),
		artwork_license VARCHAR(255),
clint's avatar
clint committed
142
		releasedate INTEGER,
143
		albumurl VARCHAR(255),
144
		downloadurl VARCHAR(255))',
145

146
		'CREATE TABLE Similar_Artist(
147 148
		name_a VARCHAR(255) REFERENCES Artist(name),
		name_b VARCHAR(255) REFERENCES Artist(name),
149
		PRIMARY KEY(name_a, name_b))'
150
	);
151

152
	$stage_two_queries_mysql = array(
153
		'CREATE TABLE Track(
154 155 156 157 158 159 160 161 162 163
		id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
		name VARCHAR(255),
		artist_name VARCHAR(255) REFERENCES Artist(name),
		album_name VARCHAR(255),
		mbid VARCHAR(36),
		duration INTEGER,
		streamable INTEGER DEFAULT 0,
		license VARCHAR(255),
		downloadurl VARCHAR(255),
		streamurl VARCHAR(255),
164
		otherid VARCHAR(16))'
165 166 167
	);

	$stage_two_queries_other = array(
168 169
		'CREATE SEQUENCE track_id_seq;',
		'CREATE TABLE Track(
170
		id INTEGER NOT NULL DEFAULT nextval(\'track_id_seq\'::regclass) PRIMARY KEY,
171 172 173 174 175 176 177 178 179
		name VARCHAR(255),
		artist_name VARCHAR(255) REFERENCES Artist(name),
		album_name VARCHAR(255),
		mbid VARCHAR(36),
		duration INTEGER,
		streamable INTEGER DEFAULT 0,
		license VARCHAR(255),
		downloadurl VARCHAR(255),
		streamurl VARCHAR(255),
180
		otherid VARCHAR(16))'
181 182 183
	);

	$stage_three_queries = array(
184
		'CREATE TABLE Scrobbles(
185
		userid INTEGER REFERENCES Users(uniqueid),
186 187
		track VARCHAR(255),
		album VARCHAR(255),
elleo's avatar
elleo committed
188
		artist VARCHAR(255) REFERENCES Artist(name),
clint's avatar
clint committed
189
		time INTEGER,
190
		mbid VARCHAR(36),
clint's avatar
clint committed
191 192
		source VARCHAR(6),
		rating CHAR(1),
clint's avatar
clint committed
193
		length INTEGER,
194
		stid INTEGER)',
elleo's avatar
elleo committed
195

196
		'CREATE TABLE Scrobble_Sessions(
197
		userid INTEGER REFERENCES Users(uniqueid),
clint's avatar
clint committed
198
		sessionid VARCHAR(32) PRIMARY KEY,
clint's avatar
clint committed
199
		client CHAR(3),
200
		expires INTEGER)',
elleo's avatar
elleo committed
201

202
		'CREATE TABLE Now_Playing(
203
		sessionid VARCHAR(32) PRIMARY KEY REFERENCES Scrobble_Sessions(sessionid) ON DELETE CASCADE,
204
		track VARCHAR(255),
205
		artist VARCHAR(255),
206
		album VARCHAR(255),
207
		mbid VARCHAR(36),
208
		expires INTEGER)',
elleo's avatar
elleo committed
209

210
		# TODO: Delete
211
		'CREATE TABLE Invitation_Request(
212
		email VARCHAR(255) PRIMARY KEY,
213
		time INTEGER)',
214

215
		'CREATE TABLE Invitations(
elleo's avatar
elleo committed
216 217 218
		inviter VARCHAR(64) REFERENCES Users(username),
		invitee VARCHAR(64) REFERENCES Users(username),
		code VARCHAR(32),
219
		PRIMARY KEY(inviter, invitee, code))',
elleo's avatar
elleo committed
220

221
		'CREATE TABLE ClientCodes(
222 223 224
		code CHAR(3),
		name VARCHAR(32),
		url VARCHAR(256),
clint's avatar
clint committed
225
		free CHAR(1),
226
		PRIMARY KEY(code))',
227

228
		'CREATE TABLE Tags(
229 230
		tag VARCHAR(64),
		artist VARCHAR(255) REFERENCES Artist(name),
231
		album VARCHAR(255),
232 233
		track VARCHAR(255),
		userid INTEGER REFERENCES Users(uniqueid),
234
		UNIQUE(tag, artist, album, track, userid))',
clint's avatar
clint committed
235

236
		'CREATE TABLE Manages(
237 238
		userid INTEGER REFERENCES Users(uniqueid),
		artist VARCHAR(255) REFERENCES Artist(name),
239
		authorised INTEGER)',
240

241
		'CREATE TABLE Error(
242 243 244
		id SERIAL PRIMARY KEY,
		msg TEXT,
		data TEXT,
245
		time INTEGER)',
246

247 248 249 250 251 252
		'CREATE TABLE Recovery_Request(
			username VARCHAR(64),
			email VARCHAR(255),
			code VARCHAR(32),
			expires INTEGER,
			PRIMARY KEY(username))',
clint's avatar
clint committed
253

254
		'CREATE TABLE Radio_Sessions(
clint's avatar
clint committed
255 256 257 258
			username VARCHAR(64),
			session VARCHAR(32),
			url VARCHAR(255),
			expires INTEGER NOT NULL DEFAULT 0,
259
			PRIMARY KEY(session))',
260 261

		//Table for delete profile requests
262
		'CREATE TABLE Delete_Request (
263 264 265
		code VARCHAR(300),
		expires INTEGER,
		username VARCHAR(64) REFERENCES Users(username),
266
		PRIMARY KEY(code))',
267

268
		'CREATE TABLE Scrobble_Track(
269 270 271 272 273
		id SERIAL PRIMARY KEY,
		artist VARCHAR(255) NOT NULL,
		album VARCHAR(255),
		name VARCHAR(255) NOT NULL,
		mbid VARCHAR(36),
274
		track INTEGER NOT NULL)',
275

276
		'CREATE VIEW Free_Scrobbles AS
277 278 279 280
		SELECT s.userid, s.track, s.artist, s.time, s.mbid, s.album, s.source, s.rating, s.length
		FROM Scrobbles s
		JOIN Scrobble_Track st ON s.stid = st.id
		JOIN Track t ON st.track = t.id
281
		WHERE t.streamable = 1',
282

283
		'CREATE TABLE Banned_Tracks (
284 285
		userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
		track varchar(255),
286 287
		artist varchar(255),
		time INTEGER,
288
		UNIQUE(userid, track, artist))',
289

290
		'CREATE TABLE Loved_Tracks (
291 292
		userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
		track varchar(255),
293 294
		artist varchar(255),
		time varchar(255),
295
		UNIQUE(userid, track, artist))',
296

297 298
		'CREATE TABLE Service_Connections (
		userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
299 300
		webservice_url VARCHAR(255),
		remote_key VARCHAR(255),
301
		remote_username VARCHAR(255),
302
		forward INTEGER DEFAULT 1)',
303

304
		'CREATE TABLE User_Relationships (
305 306
		uid1 INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
		uid2 INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
307
		established INTEGER NOT NULL,
308 309 310
		PRIMARY KEY (uid1, uid2))',

		'CREATE TABLE Relationship_Flags (
311
		flag VARCHAR(12),
312
		PRIMARY KEY (flag))',
313

314
		'CREATE TABLE User_Relationship_Flags (
315 316
		uid1 INTEGER,
		uid2 INTEGER,
317
		flag VARCHAR(12) REFERENCES Relationship_Flags(flag),
318
		PRIMARY KEY (uid1, uid2, flag),
319 320
		FOREIGN KEY (uid1, uid2) REFERENCES User_Relationships (uid1, uid2))',

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		'INSERT INTO Relationship_Flags VALUES (\'contact\')',
		'INSERT INTO Relationship_Flags VALUES (\'acquaintance\')',
		'INSERT INTO Relationship_Flags VALUES (\'friend\')',
		'INSERT INTO Relationship_Flags VALUES (\'met\')',
		'INSERT INTO Relationship_Flags VALUES (\'co-worker\')',
		'INSERT INTO Relationship_Flags VALUES (\'colleague\')',
		'INSERT INTO Relationship_Flags VALUES (\'co-resident\')',
		'INSERT INTO Relationship_Flags VALUES (\'neighbor\')',
		'INSERT INTO Relationship_Flags VALUES (\'child\')',
		'INSERT INTO Relationship_Flags VALUES (\'parent\')',
		'INSERT INTO Relationship_Flags VALUES (\'sibling\')',
		'INSERT INTO Relationship_Flags VALUES (\'spouse\')',
		'INSERT INTO Relationship_Flags VALUES (\'kin\')',
		'INSERT INTO Relationship_Flags VALUES (\'muse\')',
		'INSERT INTO Relationship_Flags VALUES (\'crush\')',
		'INSERT INTO Relationship_Flags VALUES (\'date\')',
		'INSERT INTO Relationship_Flags VALUES (\'sweetheart\')'
338 339
	);

340
	foreach ($stage_one_queries as $query) {
341 342
		try {
			$adodb->Execute($query);
343 344
		} catch (Exception $e) {
			die('Database Error: ' . $adodb->ErrorMsg());
345 346 347
		}
	}

348 349
	if (strtolower(substr($dbms, 0, 5)) == 'mysql') {
		foreach ($stage_two_queries_mysql as $query) {
350 351
			try {
				$adodb->Execute($query);
352 353
			} catch (Exception $e) {
				die('Database Error: ' . $adodb->ErrorMsg());
354 355 356
			}
		}
	} else {
357
		foreach ($stage_two_queries_other as $query) {
358 359
			try {
				$adodb->Execute($query);
360 361
			} catch (Exception $e) {
				die('Database Error: ' . $adodb->ErrorMsg());
362
			}
363 364 365
		}
	}

366
	foreach ($stage_three_queries as $query) {
367 368
		try {
			$adodb->Execute($query);
369 370
		} catch (Exception $e) {
			die('Database Error: ' . $adodb->ErrorMsg());
371 372
		}
	}
373

374 375 376 377 378 379 380 381 382
	$adodb->Execute("CREATE INDEX album_artistname_idx ON Album(artist_name)");
	$adodb->Execute("CREATE INDEX scrobbles_artist_idx ON Scrobbles(artist)");
	$adodb->Execute("CREATE INDEX scrobbles_time_idx ON Scrobbles(time)");
	$adodb->Execute("CREATE INDEX track_artist_idx ON Track(lower(artist_name))");
	$adodb->Execute("CREATE INDEX track_name_idx ON Track(lower(name))");
	$adodb->Execute("CREATE INDEX track_streamable_idx on Track(streamable);");
	$adodb->Execute("CREATE INDEX scrobbles_artist_idx on Scrobbles(lower(artist))");
	$adodb->Execute("CREATE INDEX scrobbles_track_idx on Scrobbles(lower(track))");
	$adodb->Execute("CREATE UNIQE INDEX groups_groupname_idx ON Groups(lower(groupname))");
clint's avatar
clint committed
383

clint's avatar
clint committed
384
	$adodb->Close();
385

386
	$submissions_server = $_POST['submissions'];
387
	$install_path = dirname(__FILE__) . '/';
388

389
	//Write out the configuration
390
	$config = "<?php\n \$config_version = " . $version .";\n \$connect_string = '" . $connect_string . "';\n \$submissions_server = '" . $submissions_server . "';\n \$install_path = '" . $install_path . "';\n \$adodb_connect_string = '" . $adodb_connect_string . "'; ";
391

392
	$conf_file = fopen('config.php', 'w');
393 394 395
	$result = fwrite($conf_file, $config);
	fclose($conf_file);

396 397 398
	if (!$result) {
		$print_config = str_replace('<', '&lt;', $config);
		die('Unable to write to file \'<i>config.php</i>\'. Please create this file and copy the following in to it: <br /><pre>' . $print_config . '</pre>');
399
	}
400

401
	die('Configuration completed successfully!');
402 403 404 405 406
}

?>
<html>
	<head>
mattl's avatar
mattl committed
407
		<title>GNUkebox Installer</title>
408 409 410 411 412 413 414 415 416 417 418 419 420 421
		<script type='text/javascript'>
			function showSqlite() {
				document.getElementById("sqlite").style.visibility = "visible";
				document.getElementById("networkdbms").style.visibility = "hidden";
			}

			function showNetworkDBMS() {
				document.getElementById("sqlite").style.visibility = "hidden";
				document.getElementById("networkdbms").style.visibility = "visible";
			}
		</script>
	</head>

	<body onload="showSqlite()">
mattl's avatar
mattl committed
422
		<h1>GNUkebox Installer</h1>
423
		<form method="post">
424
			<h2>Database</h2>
425
			Database Management System: <br />
426
			<input type="radio" name="dbms" value="sqlite" onclick='showSqlite()' checked>SQLite (use an absolute path)</input><br />
427 428 429 430 431 432 433 434 435 436 437 438 439 440
			<input type="radio" name="dbms" value="mysql" onclick='showNetworkDBMS()'>MySQL</input><br />
			<input type="radio" name="dbms" value="pgsql" onclick='showNetworkDBMS()'>PostgreSQL</input><br />
			<br />
			<div id="sqlite">
				Filename: <input type="text" name="filename" /><br />
			</div>
			<div id="networkdbms">
				Hostname: <input type="text" name="hostname" /><br />
				Port: <input type="text" name="port" /><br />
				Database: <input type="text" name="dbname" /><br />
				Username: <input type="text" name="username" /><br />
				Password: <input type="password" name="password" /><br />
			</div>
			<br />
441
			<h2>Servers</h2>
442
			Submissions Server URL: <input type="text" name="submissions" value="<?php echo getAbsoluteURL(); ?>" /><br />
443
			<br />
444 445
			<input type="submit" value="Install" name="install" />
		</form>
446 447
		<br />
		<div align="center"><a href="http://docs.jurg.no/gnufm_install.txt">Help</a></div>
448 449 450 451
	</body>
</html>