/
play.php
175 lines (144 loc) · 4.95 KB
/
play.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
<?
$p = new Playlist;
require dirname(__FILE__) . '/main.php';
Config::set('DEBUG', 'OFF');
$p->fetch();
$p->parse();
if (empty($p->links))
exit('No audio files found');
if (count($p->links) > 3)
$p->store();
//debug($p);
$p->headers();
$p->output();
class Playlist {
function __construct(){
if (!$url = $_GET['url'])
exit('url parameter required');
if (parse_url($url, PHP_URL_SCHEME) != 'http')
exit('valid url parameter required');
$this->url = preg_replace('/\#*$/', '', $url);
$this->format = $_GET['format'] ? str_replace('.', '', $_GET['format']) : 'm3u';
}
function fetch(){
$this->api = new API;
$this->api->cache = TRUE;
$http = array('header' => 'User-Agent: Playr (http://playr.hubmed.org)', 'timeout' => 60);
$this->api->get_data($this->url, array(), 'html-dom', $http);
$this->base = preg_match('/\/$/', $this->url) ? $this->url : dirname($this->url);
$nodes = $this->api->xpath->query("head/base/@href");
if ($nodes->length)
$this->base = $nodes->item(0)->nodeValue;
$this->title = $url;
$nodes = $this->api->xpath->query("head/title");
if ($nodes->length)
$this->title = trim($nodes->item(0)->nodeValue);
}
function headers(){
//header('Content-type: text/plain;charset=utf-8'); return;
switch($this->format){
case 'm3u':
default:
header('Content-type: audio/mpegurl; charset=UTF-8');
header('Content-Disposition: inline; filename=playlist.m3u');
break;
case 'rss':
header('Content-type: text/xml; charset=UTF-8');
header('Content-Disposition: inline; filename=playlist-rss.xml');
break;
case 'xspf':
header('Content-type: application/xspf+xml; charset=UTF-8');
header('Content-Disposition: inline; filename=playlist.xspf');
break;
}
}
function parse(){
$this->links = array();
$nodes = $this->api->xpath->query("//a/@href");
if (!$nodes->length)
$nodes = $this->api->xpath->query("//enclosure/@url");
if ($nodes->length)
foreach ($nodes as $node)
if ($link = $this->check_link($node->value))
$this->links[$link] = array(
'url' => absolute_url($link, $this->base),
'title' => empty($node->ownerElement->textContent) ? basename($link) : trim($node->ownerElement->textContent),
);
}
function output(){
switch($this->format){
case 'm3u':
default:
printf("#EXTM3U\n#EXTINF - info - -1,%s\n", str_replace("\n", '', $this->title));
foreach ($this->links as $link)
printf("#EXTINF:-1,%s\n%s\n", str_replace("\n", '', $link['title']), $link['url']);
break;
case 'rss':
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss xmlns:itunes="http://www.itunes.com/DTDs/Podcast-1.0.dtd" version="2.0">
<channel>
<title><? h($title); ?></title>
<link><? h($url); ?></link>
<description><? h($url); ?></description>
<? foreach ($this->links as $link): ?>
<item>
<title><? h($link['title']); ?></title>
<guid><? h($link['url']); ?></guid>
<enclosure url="<? h($link['url']); ?>" length="1" type="audio/mpeg"/>
</item>
<? endforeach; ?>
</channel>
</rss>
<?
break;
case 'xspf':
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<? foreach ($this->links as $link): ?>
<track>
<location><? h($link['url']); ?></location>
<title><? h($link['title']); ?></title>
<annotation><? h($link['title']); ?></annotation>
<info><? h($url); ?></info>
</track>
<? endforeach; ?>
</trackList>
</playlist>
<?
break;
}
}
function check_link($link){
$link = preg_replace('/^\/insta\.m3u\?url\=/', '', $link);
$patterns = array(
'/(\.|\%2E)mp[\dg](\W.*)?$/',
'/\.m\da$/',
'/\.mov?$/',
'/mp3\.php\?/',
'/freetrack.php\?id\=.+$/',
'/index.php\?fileid\=.+$/',
'/download.cfm\?mp3id\=.+$/',
'/modules.php\?name\=Downloads\&d_op\=getit\&lid\=.+$/',
);
foreach ($patterns as $pattern)
if (preg_match($pattern, $link))
return $link;
}
function store(){
$db = new DB;
$count = count($this->links);
$result = $db->query("SELECT * FROM `playlists` WHERE `url` = '%s'", $this->url);
if (mysql_num_rows($result)){
$item = mysql_fetch_object($result);
if ($count != $item->files)
$item->changes++;
$db->query("UPDATE playlists SET title = '%s', files = %d, changes = %d WHERE url = '%s'", $this->title, $count, $item->changes, $this->url);
}
else{
$db->query("INSERT INTO playlists (url, title, files) VALUES ('%s', '%s', %d)", $this->url, $this->title, $count);
}
}
}