forked from amsoell/rrrrradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
200 lines (168 loc) · 7.68 KB
/
controller.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
<?php
include("configuration.php");
include("classes/Db.class.php");
include("classes/Rdio.class.php");
include("classes/Track.class.php");
include("classes/Collection.class.php");
include("classes/Queue.class.php");
include("include/functions.php");
$c = new Config();
$db = new Db();
$rdio = new Rdio(RDIO_CONSKEY, RDIO_CONSSEC);
$q = new Queue();
session_start();
authenticate();
switch (strtolower($_REQUEST['r'])) {
case "mark":
$track = new Track($_REQUEST['key']);
$track->mark($_REQUEST['val'], isset($_REQUEST['userKey'])?$_REQUEST['userKey']:null);
break;
case "ignore":
if (isset($_REQUEST['off'])) {
$q->removeMuter($_SESSION['user']->key);
} else {
$q->addMuter($_SESSION['user']->key);
}
// break intentionally omitted
case "queue":
// add a requested track to the queue
if (isset($_REQUEST['key'])) {
$track = new Track($_REQUEST['key']);
if ($q->isComingUp($_REQUEST['key'])) {
$response = "Track is already in upcoming queue";
} elseif ($rdio->loggedIn() || isset($_REQUEST['oauth_token']) || (($c->requests_open===true) && in_array($_SERVER['REMOTE_ADDR'], $c->requests_restrict))) {
$e = $q->isRequestable($track);
if ($e instanceof QueueError) {
$response = $e->errorMessage;
} else {
$q->push($track, true, (isset($_REQUEST['userKey'])?$_REQUEST['userKey']:$_SESSION['user']->key), $_REQUEST['client'], $_REQUEST['dedicationName'], $_REQUEST['dedicationRecipient'], $_REQUEST['dedicationMessage']);
}
} else {
$response = "Only Salt Mines workers may request songs";
}
}
// break intentionally omitted
case 'getqueue':
if (isset($_REQUEST['userKey'])) {
$db->query("UPDATE user SET lastseen=UNIX_TIMESTAMP(NOW()), lastclient='".addslashes($_REQUEST['client'])."' WHERE `key`='".addslashes($_REQUEST['userKey'])."'");
}
$tracks = $q->getQueue();
if (count($tracks)>0) {
for ($i=0; $i<count($tracks); $i++) {
$key = $tracks[$i]->key;
$detail = $rdio->get(array("keys"=>$key, "extras"=>"trackNum,bigIcon"));
$tracks[$i]->name = $detail->result->$key->name;
$tracks[$i]->icon = $detail->result->$key->icon;
$tracks[$i]->artist = $detail->result->$key->artist;
$tracks[$i]->album = $detail->result->$key->album;
$tracks[$i]->bigIcon = $detail->result->$key->bigIcon;
$tracks[$i]->canStream = intval($detail->result->$key->canStream);
}
} else {
$tracks = Array();
$tracks[0]->startplay = 0;
$tracks[0]->endplay = 9999999999;
$tracks[0]->user = null;
$tracks[0]->mark = null;
$tracks[0]->key = "";
$tracks[0]->name = "Stream unavailable";
$tracks[0]->album = "";
$tracks[0]->artist = "Rdio servers are unavailable, check back soon!";
$tracks[0]->albumKey = "";
$tracks[0]->artistKey = "";
$tracks[0]->icon = "http://".$c->app_domain."/images/failwhale200.jpg";
$tracks[0]->duration = 9999999999;
$tracks[0]->trackNum = null;
$tracks[0]->canStream = 1;
$tracks[0]->likes = 0;
$tracks[0]->requests = 0;
$tracks[0]->bigIcon = "http://".$c->app_domain."/images/failwhale600.jpg";
}
$listeners = User::getCurrentListeners();
$return = '{ ';
$sqlx = "SELECT volume FROM volume WHERE `set`<NOW() AND (expires>NOW() OR expires IS NULL) ORDER BY (expires IS NULL), `set` DESC LIMIT 1";
$rs = $db->query($sqlx);
if (($rec = mysql_fetch_array($rs)) && is_numeric($rec['volume'])) {
$return .= '"volume" : '.$rec['volume'].', ';
}
if (strlen($response)>0) $return .= '"response": '.json_encode($response).', ';
$return .='"timestamp" : '.time().', "queue" : '.json_encode($tracks).', "listeners" : '.json_encode($listeners);
if ($_SESSION['user']->isCurator) $return .= ', "pendingRequests": '.count(Collection::getPendingRequests());
$return .= ' }';
print $return;
break;
case 'save':
if ($rdio->loggedIn()) {
$rdio->createPlaylist(array('name'=>$_REQUEST['name'], 'description'=>'Exported from '.$c->sitename.' on '.date('F j, Y'), 'tracks'=>$_REQUEST['tracks']));
} else {
print "Not logged in";
}
break;
case 'request':
$item = $rdio->get(array('keys'=>$_REQUEST['item']));
$db->query("INSERT INTO request (albumKey, userKey, requested) VALUES ('".$_REQUEST['item']."', '".$_SESSION['user']->key."', UNIX_TIMESTAMP(NOW()))");
$headers = "Organization: rrrrradio\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$headers .= "From: ".$c->sitename." <admin@".$c->app_domain.">\r\n";
$headers .= "Reply-to: ".$c->admin_email;
mail($c->admin_email, $c->sitename." request", "The following has been requested for addition to the ".$c->sitename." station:\n\n".
"Artist: ".$item->result->$_REQUEST['item']->artist."\n".
"Album: ".$item->result->$_REQUEST['item']->name."\n".
"URL: ".$item->result->$_REQUEST['item']->shortUrl."\n".
"iPhone: ".str_replace("http://","rdio://",$item->result->$_REQUEST['item']->shortUrl)."\n".
"Requested By: ".$_SESSION['user']->firstName." ".$_SESSION['user']->lastName."\n\n".
"Once the item has been added to the collection, flush the API cache with this link:\n".
"http://".$c->app_domain."/controller.php?r=refresh", $headers);
break;
case "deny":
$db->query("DELETE FROM request WHERE albumKey='".$_REQUEST['a']."' LIMIT 1");
break;
case "removefromcollection":
$thekeys = explode(',',$_REQUEST['keys']);
$a = array();
foreach ($thekeys as $key) {
switch (substr($key, 0, 1)) {
case 't':
$t = $rdio->get(array("keys"=>$key));
$a[] = $t->result->$key->albumKey;
break;
case 'a':
$a[] = $key;
break;
}
}
if (isset($a)) {
$t = $rdio->get(array("keys"=>implode(',',$a), "extras"=>"trackKeys"));
$keys = array();
foreach ($t->result as $a) {
foreach ($a->trackKeys as $trackKey) {
$keys[] = $trackKey;
}
}
$rdio->removeFromCollection(array("keys"=>implode(',',$keys)));
$db->query("DELETE FROM queue WHERE trackKey IN ('".implode("','",$keys)."')");
}
break;
case "approve":
$rs = $rdio->get(array('keys'=>$_REQUEST['a'], 'extras'=>'trackKeys'));
$keys = array();
foreach ($rs->result->$_REQUEST['a']->trackKeys as $trackKey) {
$keys[] = $trackKey;
}
$rdio->addToCollection(array('keys'=>implode(',',$keys)));
$db->query("UPDATE request SET approved=UNIX_TIMESTAMP(NOW()) WHERE albumKey='".$_REQUEST['a']."'");
// break intentionally omitted.
case "refresh":
// clear API cache for artist/album requests
$db->query("DELETE FROM api_usage WHERE params LIKE '%getArtistsInCollection%'");
$db->query("DELETE FROM api_usage where params LIKE '%getAlbumsForArtist%'");
break;
case 'finishedTrack':
break;
case 'getrandomtrack':
$c = new Collection();
$t = $c->getRandomTrack();
}