-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
418 lines (368 loc) · 12.6 KB
/
utils.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
include "theme.php";
$phpMpVersion="0.14.4";
function cleanSort( $sort_array, $display_fields )
{
$sort_count = sizeof( $sort_array );
//php7 throws exception on line 16
//$new_sort_array = "";
$new_sort_array = array();
for( $i=0; $i<$sort_count; $i++ )
{
if( in_array( $sort_array[$i], $display_fields ))
{
$new_sort_array[] = $sort_array[$i];
}
}
$new_sort_count = sizeof( $new_sort_array );
$new_sort = $new_sort_array[0];
for( $j=1; $j<$new_sort_count; $j++ )
{
$new_sort .= ','.$new_sort_array[$j];
}
return( array( $new_sort_array, $new_sort ));
}
/***********************************************************************************************#
# #
# crop: deletes all but the currently playing song from the current playlist #
# #
#***********************************************************************************************#
# #
# $fp => connection #
# $current => value of the current song (starts at '0') #
# $playlistlength => value of the number of songs in the current playlst (starts at '1') #
# #
#***********************************************************************************************/
function crop( $fp, $current, $playlistlength )
{
$playlistlength -= 1;
$str = "command_list_begin\n";
for( $playlistlength; $playlistlength >= "0"; $playlistlength-- )
{
if( strcmp( $current, $playlistlength ))
{
$str .= "delete $playlistlength\n";
}
}
fputs( $fp, $str . "command_list_end\n" );
initialConnect( $fp );
return;
}
function initialConnect( $fp )
{
while( ! feof( $fp ))
{
$got = fgets( $fp, "1024" );
if( strncmp( "OK", $got, strlen( "OK" )) == "0" )
{
return preg_replace( "/^OK MPD /", "", $got );
}
if( strncmp( "ACK", $got, strlen( "ACK" )) == "0")
{
return $got;
}
}
}
function doCommand( $fp, $arg, $arg2, $command, $overwrite, $status )
{
// Don't let updates collide
if( isset ( $status["updating_db"] ) && strcmp( $command,"update" ) == "0" )
{
return 0;
}
// Lets cleanup the $arg first
$arg = rawurldecode( $arg );
// This is to facilitate for overwriting playlists, it should probably detect to see if
// it exists first, but who cares, right?
if( strncmp( "save", $command, strlen( "save" )) == "0" && $overwrite === true )
{
fputs( $fp, "rm \"$arg\"\n" );
fgets( $fp, "1024");
}
// Cannot use empty() here because when the argument == 0 it returns false.
if ( strlen( $arg2 ) > "0" )
{
$command.= " \"$arg\" \"$arg2\"";
}
else if( strlen( $arg ) > "0" )
{
$command.= " \"$arg\"";
}
// By the time you read this the 'kill' command will hopefully be a non-default compile time
// option. $arg is also used here, so to make sure someone doesn't kill your MPD leave it.
if( strncmp( "kill", $command, strlen( "kill" )) || ( ! empty( $command ) && strncmp( "kill", $arg, strlen( "kill" ))))
{
fputs( $fp, "$command\n" );
}
while( ! feof( $fp ))
{
$got = fgets( $fp, "1024" );
if( strncmp( "OK", $got, strlen( "OK" )) == "0" )
{
break;
}
str_replace( "\n", "\n<br>", $got );
// Otherwise it will output how many times it's updated
if ( strncmp( "update", $command, strlen( "update" )))
{
echo "<a target=main><b><br>Error! $got<br></b></a><br>\n";
}
if ( strncmp( "ACK", $got, strlen( "ACK" )) == "0" )
{
break;
}
}
}
function displayDirectory( $dir, $dir_url, $sort, $title, $mfcount, $mtcount, $pcount, $dcount, $has_password, $commands, $color, $server, $servers, $fp, $passarg, $ordered, $feature, $arg, $search_bar, $javascript )
{
echo "<!-- Begin displayDirectory -->";
// The next line needs a cellspacing value of 2 since the other tables have 2 tables, and this one only has one
echo "<table summary=\"Directory\" cellspacing=1 bgcolor=\"{$color["title"]}\">";
echo "<tr><td>";
echo "<b>$title</b> ";
if( $dcount > "0" )
{
if( $mfcount > "0" && $mtcount > "0" )
{
echo "<small>(<a title=\"Jump to the tagged music menu\" href=\"#Tagged Music\">Tagged</a>)</small> ";
echo "<small>(<a title=\"Jump to the untagged music menu\" href=\"#Untagged Music\">Untagged</a>)</small> ";
}
else if( $mfcount > "0" )
{
echo "<small>(<a title=\"Jump to the music menu\" href=\"#Untagged Music\">Music</a>)</small> ";
}
else if( $mtcount > "0" )
{
echo "<small>(<a title=\"Jump to the music menu\" href=\"#Tagged Music\">Music</a>)</small> ";
}
}
else if( $mfcount > "0" && $mtcount > "0" )
{
echo "<small>(<a title=\"Jump to the untagged music menu\" href=\"#Untagged Music\">Untagged</a>)</small> ";
}
if ( $pcount > "0" )
{
echo "<small>(<a title=\"Jump to the saved playlists menu\" href=\"#playlists\">Saved Playlists</a>)</small> ";
}
echo "</td><td align=right><small>";
$feature_bar = "";
// If we have all commands available and we don't have an active password don't show the feature bar.
if( $commands["all"] === false || strlen( $passarg ) > "0" )
{
if( $has_password === true )
{
$feature_bar .= "<a title=\"Logout of MPD Server\" target=_top href=\"index.php?server=$server&dir=$dir_url&sort=$sort&logout=1\">Logout</a>";
}
else
{
$feature_bar .= "<a title=\"Login to MPD Server\" target=main href=\"index.php?body=main&feature=login&server=$server&dir=$dir_url&sort=$sort\">Login</a>";
}
}
if( $commands["outputs"] === true )
{
if( ! empty( $feature_bar ))
{
$feature_bar .= " | ";
}
$feature_bar .= "<a title=\"View the Sound Outputs\" href=\"index.php?body=main&server=$server&dir=$dir_url&sort=$sort&feature=outputs&dir=$dir_url\">Outputs</a>";
}
if( $commands["search"] === true )
{
if( ! empty( $feature_bar ))
{
$feature_bar .= " | ";
}
$feature_bar .= "<a title=\"Search the MPD Database\" href=\"index.php?body=main&server=$server&dir=$dir_url&sort=$sort&feature=search\">Search</a>";
}
if( sizeof( $servers ) > 1 )
{
if( ! empty( $feature_bar ))
{
$feature_bar .= " | ";
}
$feature_bar .= "<a title=\"Change the MPD Server\" href=\"index.php?body=main&server=$server&sort=$sort&dir=$dir_url&feature=server\">Servers</a>";
}
if( $commands["stats"] === true )
{
if( ! empty( $feature_bar ))
{
$feature_bar .= " | ";
}
$feature_bar .= "<a title=\"View MPD/phpMp Statistics\" target=main href=\"index.php?body=main&server=$server&dir=$dir_url&sort=$sort&feature=stats\">Stats</a>";
}
if( $commands["load"] === true )
{
if( ! empty( $feature_bar ))
{
$feature_bar .= " | ";
}
$feature_bar .= "<a title=\"Add a Stream or Playlist of Streams to the Active Playlist\" ";
$feature_bar .= "href=\"index.php?body=main&server=$server&dir=$dir_url&sort=$sort&feature=stream\">Stream</a>";
}
if( ! empty( $feature_bar ))
{
echo $feature_bar;
}
echo " </small></td></tr><tr><td colspan=2>";
echo "<table style=\"background-color: {$color["body"][0]}\"\">";
echo "<tr bgcolor=\"{$color["body"][0]}\">";
if( $search_bar === true && empty($feature)) {
echo "<td>";
} else {
echo "<td colspan=2>";
}
$dirs = explode( "/", $dir );
echo "<a title=\"Back to the Root Music Directory\" href=\"index.php?body=main&server=$server&sort=$sort&ordered=$ordered\">Music</a>";
$build_dir = "";
for( $i=0; $i < ( count( $dirs ) - 1 ); $i++ )
{
if ($i > "0" && $i < ( count( $dirs ) - 1 ))
{
$build_dir.="/";
}
$dirs[$i] = stripslashes( $dirs[$i] );
echo " / ";
// A dirty fucking hack because for some reason the '&' doesn't get properly encoded.
// To see this bug in action goto a directory with '&' in the name, then go to a child directory, then click on the
// directory with the '&' in the name again.
$build_dir .= str_replace('&','%26',$dirs[$i]);
// This is a workaround to prevent letters that shouldn't be in the title from getting there.
$nice = str_replace(array("\"","\'"), '', $dirs[$i]);
echo "<a title=\"Jump to '".$nice."'\" href=\"index.php?body=main&server=$server&sort=$sort&ordered=$ordered&dir=$build_dir\">$dirs[$i]</a>";
}
if ( $i > "0" )
{
$build_dir.="/";
}
if ( ! empty( $dir ))
{
$dirs[$i] = stripslashes( $dirs[$i] );
$build_dir.=$dirs[$i];
$build_dir = rawurlencode( $build_dir );
echo " / ";
// This is a workaround to prevent letters that shouldn't be in the title from getting there.
$nice = str_replace(array("\"","\'"), '', $dirs[$i]);
echo "<a title=\"Refresh the Current Directory '".$nice."'\" href=\"index.php?body=main&server=$server&sort=$sort&ordered=$ordered&dir=$build_dir\">$dirs[$i]</a>";
}
// We don't allow update during search or find because
// the search will not be re-submitted without some javascript magic.
// It would probably be best to compensate for the stream browsers too.
$status = getStatusInfo( $fp );
if( isset( $status["updating_db"] ))
{
echo " <small>(db updating...)</small>";
}
else if( strcmp( $title, "Current Directory" ) == "0" && $commands["update"] === true &&
!( strcmp( $feature, "search" ) == "0" || strcmp( $feature, "find" ) == "0" ))
{
$dirs[$i] = stripslashes( $dirs[$i] );
$build_dir = rawurlencode( $build_dir );
echo " <small>(<a href=\"index.php?body=main&feature=$feature&server=$server&dir=$build_dir&sort=$sort&ordered=$ordered&command=update&arg=$build_dir/\"";
echo "target=\"main\" title=\"Update the Current Directory\">db update</a>)</small>";
}
if( $search_bar === true && empty($feature)) {
echo "<form action=index.php? method=get>";
echo "<td align=right>";
echo "<input type=hidden value=\"main\" name=body>";
echo "<input type=hidden value=\"search\" name=feature>";
echo "<input type=hidden value=\"any\" name=search>";
echo "<input type=hidden value=\"$dir\" name=dir>";
echo "<input type=hidden value=\"$server\" name=server>";
echo "<input type=hidden value=\"$sort\" name=sort>";
echo " <input name=arg size=25 id=\"f\" autocomplete=on>";
echo "<input type=submit value=Go name=foo>";
if(strcmp($javascript,"yes") == 0) {
echo "<script type=\"text/javascript\">";
echo "document.getElementById('f').focus();";
echo "</script>";
}
echo "</td></form>";
}
echo "</td></tr></table></td></tr></table>";
echo "<!-- End displayDirectory -->";
}
function mbFirstChar( $str )
{
$i = 1;
if( isset( $str[0] ))
{
$ret = $str[0];
}
while ($i < strlen( $str ) && ord( $str[$i] ) >= 128 && ord( $str[$i] ) < 192)
{
$ret .= $str[$i];
$i++;
}
if( isset( $ret ))
{
return $ret;
}
}
function readFileOverHTTP( $fp, $stream )
{
$stream = rawurldecode( $stream );
$pointer = fopen( $stream, "r" ) or die ( "<H3><b>If you want phpMp to download your stream, you have to change 'allow_url_open' to On in your php.ini</b></H3>");
$contents = fread( $pointer, "1024" );
if( preg_match( "/.pls$/", $stream ))
{
preg_match_all( "/File[0-9]*=(.*?)\n/", $contents, $out );
$streams = $out[1];
}
else
{
if( strcmp( $contents, " " ))
{
$streams = explode( " ", $contents );
}
else
{
$streams = $contents;
}
}
return($streams);
}
function postStream( $fp, $filetype )
{
$add = array();
$i = 0;
while ( ! feof( $fp ))
{
$url = fgets( $fp, 4096 );
if( strcmp( $filetype, "pls" ) && preg_match( "/File[0-9]*=/", $url ))
{
$url = preg_replace( "/^File[0-9]*=/", "", $url );
}
$url = preg_replace( "/\n$/", "", $url );
if ( preg_match( "/^[a-z]*:\/\//", $url ))
{
$add[$i] = $url;
$i++;
}
}
return $add;
}
function hostname($url) {
$i = parse_url($url);
return "{$i["scheme"]}://{$i["host"]}";
}
/* This function was taken from php.net examples*/
function x_array_merge($arr1,$arr2) {
for($i=0;$i<count($arr1);$i++) {
$arr[$i]=($arr1[$i] == '')?$arr2[$i]:$arr1[$i];
}
return $arr;
}
/* This is where the file downloading is done */
/* This code was taken from php.net examples */
function get_links($url) {
if( !($body = @file_get_contents($url)) ) return FALSE;
//Pattern building across multiple lines to avoid page distortion.
$pattern = "/((@import\s+[\"'`]([\w:?=@&\/#._;-]+)[\"'`];)|";
$pattern .= "(:\s*url\s*\([\s\"'`]*([\w:?=@&\/#._;-]+)";
$pattern .= "([\s\"'`]*\))|<[^>]*\s+(src|href|url)\=[\s\"'`]*";
$pattern .= "([\w:?=@&\/#._;-]+)[\s\"'`]*[^>]*>))/i";
//End pattern building.
preg_match_all ($pattern, $body, $matches);
return (is_array($matches)) ? $matches:FALSE;
}
?>