-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
156 lines (142 loc) · 4.28 KB
/
index.php
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
<?php
/*
* Steps:
* 0.9 check password
* 1a check if database exists
* 1b if not: create database/tables
* 1c handle ajax
* 2a check db for last scan
* 2b if last db check >= 1hour, then do filesystem/music dir scan
* 3a load last played playlist (but do not play yet)
* (3b load list of all playlists)
* 4 present HTML5-Audio-Player
*
* TODO: handle each and every mysql query: log mysql errors to a file
*
* MySQL query to get the size of all tables:
* SELECT `table_name` AS 'tbl', round(((data_length + index_length) / 1024 / 1024), 2) AS 'size in MB' FROM information_schema.TABLES WHERE table_schema='juph';
*
* MySQL query to get the number of tagified and untagified files in `filecache`:
* (SELECT 'tagitfied' AS 'tagified', COUNT(`id`) AS 'anzahl' FROM `filecache` WHERE `tagified`='Y') UNION (SELECT 'untagified' AS 'tagified', COUNT(`id`) AS 'anzahl' FROM `filecache` WHERE `tagified`='N');
*/
$CONFIG_FILE = 'config.php';
$CONFIG_VAR = NULL;
$do_setup = true;
$cur_time = time();
$SESSION_ID = '';
require_once('include/commons.php');
if( file_exists($CONFIG_FILE))
{
require_once($CONFIG_FILE);
if(isset($CONFIG_VAR['setup_complete']) && $CONFIG_VAR['setup_complete'])
{
$do_setup = false;
}
}
if( $do_setup)
{
if(isset($_GET['ajax']))
{
server_error("Ajax unavailable", true);
} else
{
include('include/setup.php');
}
exit(0);
}
/* check if necessary configuration variables are available */
if(!isset($CONFIG_VAR['DB_ADDR'])
|| !isset($CONFIG_VAR['DB_PORT'])
|| !isset($CONFIG_VAR['DB_DB'])
|| !isset($CONFIG_VAR['DB_USER'])
|| !isset($CONFIG_VAR['DB_PWD'])
|| !isset($CONFIG_VAR['ACCESS_PWD'])
|| !isset($CONFIG_VAR['MUSIC_DIR_ROOT']))
{
server_error('Could not load essential configuration variables, setup correct?');
//TODO: also provide a link to juph setup
exit(0);
}
//step 0.9 check password
require_once('include/check_access.php');
/* try to connect to database */
$dbcon = new mysqli($CONFIG_VAR['DB_ADDR'], $CONFIG_VAR['DB_USER'], $CONFIG_VAR['DB_PWD'], $CONFIG_VAR['DB_DB'], (int) $CONFIG_VAR['DB_PORT']);
if($dbcon->connect_errno)
{
server_error('could not connect to database');
exit(0);
}
//maybe TODO for later: bother with incomplete scans (e.g. `completed`='N')
$result_scan_check = @$dbcon->query('SELECT `time`,`completed` FROM `scans` WHERE `completed`=\'Y\' ORDER BY `time` DESC LIMIT 1');
$need_scan = true;
$need_create_table = false;
//step 1a, check for db
if(FALSE === $result_scan_check)
{
if(1146 == $dbcon->errno)
{
$need_create_table = true;
} else
{
server_error('Error while looking up database table `scans`');
$dbcon->close();
exit(0);
}
}
//step 1b, table creation
if($need_create_table)
{
require_once('include/create-tables.php');
}
//step 1c, handle ajax
$AJAX_PAGE_LIMIT = 10;
if(isset($_GET['ajax']))
{
require_once('include/ajax.php');
}
if(isset($_GET['put_file_info']))
{
require_once('include/put_file_info.php');
}
if(isset($_GET['file_edit_form']))
{
require_once('include/file_edit_form.php');
}
//step 2a, lookup scans
//TODO: why do we do this lookup twice? - reduce this to just one query
$need_scan_music_dir = True;
if(!(FALSE === $result_scan_check) && 0 < $result_scan_check->num_rows)
{
$last_scan_row = $result_scan_check->fetch_assoc();
$last_scan_time = (int) $last_scan_row['time'];
$last_scan_completed = $last_scan_row['completed'];
if($last_scan_time >= ($cur_time - 86400 * 7) && 'Y' == $last_scan_completed)
{
$need_scan_music_dir = False;
}
}
//step 2b, do scan
//DONE: tested this section with detailed output of found files
if($need_scan_music_dir)
{
scan_music_dir($CONFIG_VAR['MUSIC_DIR_ROOT'], $dbcon);
}
//step 4, present audio player
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>juph audio player</title>
<script type="text/javascript">
window.juph = new Object();
window.juph.sessionId = <?php echo "\"" . js_escape($SESSION_ID) . "\";"; ?>;
window.juph.userAgent = <?php echo "\"" . js_escape($_SERVER['HTTP_USER_AGENT']) . "\""; ?>;
</script>
<script type="text/javascript" src="js/main.js"></script>
<link rel="stylesheet" href="css/main.css" media="screen">
</head>
<body>
<?php require_once('include/main_body.php'); ?>
</body>
</html>