initial commit - version 2.0 + README added
[mirrors/JukeBox.git] / index.php
CommitLineData
ca88871f
H
1<?php require_once('./http_auth.php'); /*Delete this line to disable password protection*/ ?>
2<?php /*
3Harvie's JuKe!Box
4///////////////////////////////////////////////////////////////////////
5Version info:
6 0.2 - Few new functions (search playlist, random,...)
7 0.1.1 - Few little fixups, written help.html in Czech language ;D
8 0.1 - All functions works - TODO: bugfix & replace ugly code
9///////////////////////////////////////////////////////////////////////
10*/
11
12//Config-basic
13$title = 'Harvie\'s&nbsp;JuKe!Box'; //Title of jukebox
14$music_dir = './music'; //Local path to directory with music
15$music_dir_url = 'http://192.168.2.163/pub/m3gen/music'; //URL path to same directory
16//$music_dir_url = 'http://music.harvie.cz/music';
17//$music_dir_url = 'http://softz.harvie.cz/jukebox/demo/music';
18$cache_passwd = 'reload'; //You need this passwd to refresh search cache
19$sort = 3; //Sort? 0 = none, 1 = playlists, 2 = 1+listings; 3 = 2+search-EXPERIMENTAL! (sorting could eat lot of memory)
20$access_limit = 20; //How many files could be accessed without using cache (while searching)
21
22//Playlist settings
23$playlist_name = 'playlist.m3u'; //Name of downloaded pl
24$m3u_exts = 'ogg|mp[0-9]|wma|wmv|wav'; //Allow only these files
25$default_random_count = 30; //How many random songs by defaul?
26
27//External files
28$indexlist = array('index.html', 'index.txt'); //Search for this file in each directory
29$bonus_dir = './jukebox-bonus'; //Misc. files directory
30$search_cache = $bonus_dir.'/cache.db'; //Database for searching music (php +rw) - .htaccess: Deny from all!!!
31$flash_player = $bonus_dir.'/musicplayer.swf'; //path to musicplayer
32$css_file = $bonus_dir.'/jukebox.css'; //CSS
33$header_file = $bonus_dir.'/header.html'; //header file
34$footer_file = $bonus_dir.'/footer.html'; //footer file
35
36//Security
37error_reporting(0);
38
39//Init
40srand(time());
41$current_dir = ereg_replace('/+', '/', '/'.$_GET['dir'].'/');
42$dir = $music_dir.$current_dir;
43$url = $music_dir_url.$current_dir;
44$parent_dir = dirname($current_dir);
45
46//FCs
47function serve_download($filename) {
48 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
49 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
50 header('Pragma: no-cache');
51
52 //header('Content-Type: application/force-download');
53 header('Content-Type: audio/x-mpegurl');
54 header("Content-Disposition: attachment; filename={$filename}");
55 header('Content-Transfer-Encoding: binary');
56
57 header('X-PHP-Application: Harvie\'s JuKe!Box');
58}
59
60function generate_m3u($dir, $prefix='', $recursive=0) {
61 $dir = $dir . '/';
62 $dd = opendir($dir);
63 while(($item = readdir($dd)) != false) {
64 if($item == '.' || $item == '..') continue;
65 if( is_file($dir.$item) && eregi(('\.('.$GLOBALS['m3u_exts'].')$'), $item) ) {
66 if($GLOBALS['sort'] > 0) {
67 $temp[] = $item;
68 } else {
69 echo($prefix.'/'.str_replace('%2F', '/', (rawurlencode($dir.$item)))."\r\n");
70 }
71 }
72 if($recursive && is_dir($dir.$item)) {
73 generate_m3u($dir.$item, $prefix);
74 }
75 }
76 if($GLOBALS['sort'] > 0) {
77 @sort($temp);
78 foreach($temp as $item)
79 echo($prefix.'/'.str_replace('%2F', '/', (rawurlencode($dir.$item)))."\r\n");
80 }
81}
82
83function write_search_cache($dir, $outfp) {
84 $dir = $dir . '/';
85 $dd = opendir($dir);
86 while($item = readdir($dd)) {
87 if($item == '.' || $item == '..') continue;
88 if( is_file($dir.$item) && eregi(('\.('.$GLOBALS['m3u_exts'].')$'), $item) ) {
89 fwrite($outfp, $dir.$item."\n");
90 }
91 if(is_dir($dir.$item)) {
92 write_search_cache($dir.$item, $outfp);
93 }
94 }
95}
96
97function generate_search_cache($dir, $outfile) {
98 echo("Generating search cache. Please wait...<br />\n"); flush();
99 @chmod($outfile, 0755); //At least i tryed ;D
100 if(!($outfp = fopen($outfile, 'w')))
101 die("Cannot write cache to $outfile<br />You probably haven't set the permissions properly!<br />\n");
102 write_search_cache($dir, $outfp);
103 fclose($outfp);
104 $osize = filesize($outfile); clearstatcache();
105 if($GLOBALS['sort'] > 2) {
106 echo("Sorting search cache. Please wait...<br />\n"); flush();
107
108 $items = file($outfile); @sort($items);
109 $total = ' ('.sizeof($items).' files)';
110 file_put_contents($outfile, @implode('', $items));
111 unset($items);
112 if(abs(filesize($outfile)-$osize) > 2)
113 die('ERROR! Please disable sorting of search cache ($sort < 3)<br />'."\nSorted only ".
114 filesize($outfile).' of '.$osize.' bytes!!!\n');
115 }
116 echo('Total: '.filesize($outfile).' of '.$osize.' bytes'.$total.' <a href="?">DONE!</a>'.'<br /><META http-equiv="refresh" content="2;URL=?">'."\n");
117}
118
119function render_file_line($dir, $item, $dir_url, $index, $filesize, $parent = false) {
120 $parclass=($index%2?"even":"odd"); $parcolor=($index%2?"lightblue":"white");
121 $temp=str_replace('&', '%26', dirname($dir_url)).'/'.str_replace('%2F', '/', (rawurlencode($dir.$item)));
122 if(is_numeric($filesize)) $filesize = round($filesize/(1024*1024), 2);
123 echo("<tr class=\"$parclass\" bgcolor=\"$parcolor\">".'<td>'.$index.'</td><td>');
124 echo('<a href="?download&song='.rawurlencode($temp).'">P</a>');
125 if($parent) {
126 echo('/<a href="?dir='.
127 substr(str_replace(array('&','%2F'), array('%26','/'), (rawurlencode(dirname($dir.$item)))), strlen($GLOBALS['music_dir'])).
128 '">D</a>');
129 }
130 if(is_file($GLOBALS['flash_player']) && eregi(('\.('.$GLOBALS['m3u_exts'].')$'), $item)) {
131 /*echo('/<object type="application/x-shockwave-flash" width=17 height=17 data="'.
132 $GLOBALS['flash_player'].'?song_url='.rawurlencode($temp).'"></object>');*/
133 echo('/<a href="'.$GLOBALS['flash_player'].'?autoplay=true&song_url='.rawurlencode($temp).'" target="playframe">F</a>/'.
134 '<a href="about:blank" target="playframe">S</a>');
135 }
136 echo('&nbsp;</td><td><a href="'.$temp.'">'.str_replace('_', ' ', $item).'</a></td><td>'.$filesize."&nbsp;MiB&nbsp;</td></tr>\n");
137}
138
139function unxss($string) {
140 return str_replace(
141 array('&', '"', '\'', '<', '>'),
142 array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;'),
143 $string);
144}
145
146//GET
147if(isset($_GET['download'])) serve_download($playlist_name);
148if(isset($_GET['song'])) die($_GET['song']."\r\n");
149
150if($_POST['cache-refresh'] == $cache_passwd) {
151 generate_search_cache($music_dir, $search_cache);
152 die("\n");
153}
154
155if(isset($_GET['playlist'])) {
156 if(!isset($_GET['search'])) {
157 generate_m3u($dir, dirname($music_dir_url), isset($_GET['recursive']));
158 } else {
159 if(!($searchfp = fopen($search_cache, 'r')))
160 die("Cannot read cache from $outfile<br />Refresh cache or set permissions properly!<br />\n");
161 while(!feof($searchfp)) {
162 $line = trim(fgets($searchfp));
163 if(@eregi(str_replace(' ', '(.*)', $_GET['search']), $line))
164 echo(dirname($music_dir_url).'/'.str_replace('%2F', '/', (rawurlencode($line)))."\r\n");
165 }
166 }
167 die("\n");
168}
169
170if(isset($_GET['random'])) {
171 $flen = 0;
172 if(!($searchfp = fopen($search_cache, 'r')))
173 die("Cannot read cache from $outfile<br />Refresh cache or set permissions properly!<br />\n");
174 while(!feof($searchfp)) { fgets($searchfp); $flen++; }
175 for($i=0; $i<$_GET['random']; $i++) {
176 rewind($searchfp);
177 for($j=0; $j<rand(0, $flen-1); $j++) fgets($searchfp);
178 echo(dirname($music_dir_url).'/'.str_replace('%2F', '/', (rawurlencode(trim(fgets($searchfp)))))."\r\n");
179 }
180 die("\n");
181}
182
183?>
184<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
185<link rel="stylesheet" type="text/css" href="<?=$css_file?>">
186
187<div align="right" style="position: absolute; top: 0; right: 0;">
188 <iframe src="about:blank" name="playframe" width="0" height="0" style="border: none;"></iframe>
189 &lt; <a href="javascript: history.go(-1)">BACK</a> | <a href="?">HOME (<?=$music_dir?>)</a> | <a href="?help">ABOUT/HELP</a> | <a href="?logout">LOGOUT</a>
190</div>
191
192<?php
193if(isset($_GET['help'])) {
194 ?><h1>About/Help</h1><?php
195 readfile($bonus_dir.'/help.html');
196 die();
197}
198
199if(!isset($_GET['search'])) {
200 echo('<title>'.$title.': '.$dir.'</title>');
201 echo('<a href="?" style="color: black;"><h1>'.$title.'</h1></a><h2>Index of: '.$dir.'</h2>');
202} else {
203 echo('<title>'.$title.': '.unxss($_GET['search']).'</title>');
204 echo('<a href="?" style="color: black;"><h1>'.$title.'</h1></a><h2>Searching for: '.unxss($_GET['search']).'</h2>');
205}
206
207?>
208
209<div align="right">
210 <form action="?" method="GET" align="right" style="display: inline;">
211 <input type="hidden" name="download" value="" />
212 <input type="text" name="random" value="<?=$default_random_count?>" />
213 <input type="submit" value="random" title="Generate random music playlist..." />
214 </form>
215 <form action="?" method="GET" align="right" style="display: inline;">
216 <input type="text" name="search"
217 title="Search in music/google/lyrics/mp3/youtube; Hint: You can use regular expressions in search query..."
218 value="<?=unxss($_GET['search'])?>"
219 />
220 <input type="submit" value="search" title="Search in this JuKe!Box..." />
221 </form>
222</div><br /><?php
223
224if(isset($_GET['search'])) {
225
226?><div align="right">
227 <form action="http://google.com/search" method="GET" align="right" style="display: inline;">
228 <input type="text" name="q" value="<?=unxss($_GET['search'])?>" />
229 <input type="submit" value="google" title="Search on Google..." />
230 </form>
231 <form action="http://www.elyricsworld.com/search.php?phrase=marley" method="GET" align="right" style="display: inline;">
232 <input type="text" name="phrase" value="<?=unxss($_GET['search'])?>" />
233 <input type="submit" value="lyrics" title="Search for lyrics on internet..." />
234 </form>
235 <form action="http://jyxo.cz/s" method="GET" align="right" style="display: inline;">
236 <input type="hidden" name="d" value="mm" />
237 <input type="text" name="q" value="<?=unxss($_GET['search'])?>" />
238 <input type="submit" value="jyxo multimedia" title="Search media on internet..." />
239 </form>
240 <form action="http://youtube.com/results" method="GET" align="right" style="display: inline;">
241 <input type="text" name="search_query" value="<?=unxss($_GET['search'])?>" />
242 <input type="submit" value="youtube" title="Search on YOUTube..." />
243 </form>
244</div><br />
245<div align="right">
246 <form action="?" method="POST" align="right">
247 <input type="password" name="cache-refresh" value="" title="Password for refreshing - good for avoiding DoS Attacks!!!" />
248 <input type="submit" value="refresh cache" title="You should refresh cache each time when you add new music or upgrade to newer version of JuKe!Box !!!" />
249 </form>
250</div><?php
251echo('Search DB size: '.(filesize($search_cache)/1024)." kB<br />\n");
252
253if(!($searchfp = fopen($search_cache, 'r')))
254 die("Cannot read cache from $outfile<br />Refresh cache or set permissions properly!<br />\n");
255
256$i = 0;
257echo('<table border="1" width="100%">');
258echo('<tr><td>S</td><td><a href="?download&playlist&search='.unxss($_GET['search']).'">P</a></td><td colspan="100%">Search: '.unxss($_GET['search']).'</td></tr>');
259while(!feof($searchfp)) {
260 $line = trim(fgets($searchfp));
261 $parclass=($i%2?"even":"odd"); $parcolor=($i%2?"lightblue":"white");
262 if(@eregi(str_replace(' ', '(.*)', $_GET['search']), $line)) {
263 $i++;
264 echo("<tr class=\"$parclass\" bgcolor=\"$parcolor\">");
265 $filesize = 0; if($i <= $access_limit) $filesize = filesize($line); else $filesize = 'n/a';
266 render_file_line('', $line, $music_dir_url, $i, $filesize, true);
267 echo("</tr>\n");
268 }
269}
270echo("</table>Total: $i results...<br />");
271die();
272
273}
274@readfile($header_file);
275foreach($indexlist as $index) @readfile($dir.$index);
276?>
277<br />
278<table border="1" width="100%">
279<tr><td>&gt;</td>
280<td><b><a href="?download&playlist&dir=<?=str_replace('%2F', '/', rawurlencode($current_dir))?>">P</a>/<a
281href="?download&recursive&playlist&dir=<?=str_replace('%2F', '/', rawurlencode($current_dir))?>">R</a></b></td>
282<td colspan="100%"><?=$dir?></td></tr>
283<tr><td>^</td><td>&nbsp;</td><td colspan="100%">[DIR] <a href="?dir=<?=rawurlencode($parent_dir)?>">.. (<?=$parent_dir?>)</a></td></tr>
284<?php
285
286$i = 0;
287$dd = opendir($dir);
288for($s=2;$s;$s--) { while(($item = readdir($dd)) != false) {
289 if($item == '.' || $item == '..') continue;
290 if(($s==2 && is_file($dir.$item)) || ($s!=2 && is_dir($dir.$item))) continue;
291 $i++;
292 $parclass=($i%2?"even":"odd"); $parcolor=($i%2?"lightblue":"white");
293 if(is_file($dir.$item)) {
294 if($sort > 1) {
295 $i--;
296 $items[] = $item;
297 } else {
298 render_file_line($dir, $item, $music_dir_url, $i, filesize($dir.$item));
299 }
300 }
301 if(is_dir($dir.$item)) {
302 $temp=str_replace('%2F', '/', rawurlencode($current_dir)).rawurlencode($item);
303 echo("<tr class=\"$parclass\" bgcolor=\"$parcolor\">".
304 '<td>'.$i.'</td><td><a href="?download&playlist&dir='.$temp.'">P</a>/'.
305 '<a href="?download&recursive&playlist&dir='.$temp.'">R</a></td>'.
306 '<td colspan="100%">[DIR] <a href="?dir='.$temp.'">'.str_replace('_', ' ', $item)."</a></td></tr>\n");
307 }
308} rewinddir($dd); }
309if($sort > 1) {
310 @sort($items);
311 foreach($items as $item) {
312 $i++;
313 render_file_line($dir, $item, $music_dir_url, $i, filesize($dir.$item));
314 }
315}
316
317?></table>
318
319<?php
320$quotes = array(
321 'This is NOT advertisments. This is just good text to think about... Remove it if you want!',
322 'Downloading without sharing and other forms of leeching equals STEALING! ;P',
323 'Do NOT support Microsoft!!! Use Linux! ;D',
324 'Don\'t steal! Steal and share!!! ;P',
325 'Linux is not matter of price, it\'s matter of freedom!',
326 'This software brought to you by <a href="http://blog.Harvie.cz">Harvie</a> free of charge! Of course...',
327 'Don\'t be looser, use GNU/Linux! ;P',
328 'Make love and not war!',
329 'Take your chance! Prove yourself!',
330 'This software is free of charge. If you wan\'t to donate, please send some money to children in Africa/etc...'
331);
332
333echo('<i>'.$quotes[rand(0,sizeof($quotes)-1)]."</i>\n");
334@readfile($footer_file);
This page took 0.452351 seconds and 4 git commands to generate.