-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
346 lines (278 loc) · 6.98 KB
/
functions.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
<?php
/*
Copyright 2012 Ole Jon Bjørkum
This file is part of SpotCommander.
SpotCommander is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SpotCommander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SpotCommander. If not, see <http://www.gnu.org/licenses/>.
*/
// Remote control
function remote()
{
global $spotify_ps;
if($spotify_ps == 1 || $action == 'spotify-launch')
{
$spath = "unix://".getcwd()."/sh/spotcommander.socket";
$conn = stream_socket_client($spath,$errno,$errstr);
if (!$conn) {
echo "<h2>Error connecting to spotcommander daemon</h2>$errstr<br />$spath<br />";
exit;
}
fwrite($conn, json_encode(func_get_args())."\n");
$res = '';
while(!feof($conn))
$res .= fread($conn,4096);
fclose($conn);
return json_decode($res,true);
}
}
function current_volume()
{
return remote('volume-get');
}
function volume_before_mute()
{
$volume = trim(file_get_contents('sh/volume-before-mute.txt'));
return($volume);
}
// Now playing
function get_nowplaying()
{
$res = remote('metadata-get');
$length = $res['length']/1000000;
$res['length'] = floor($length / 60) . ':' . sprintf("%02s", $length % 60);
$res['year'] = substr($res['year'], 0, 4);
return($res);
}
// Recently played
function save_recently_played($artist, $title, $uri)
{
$db = new SQLite3('db/recently-played.db');
$query = $db->query("SELECT COUNT(*) as count FROM recently_played WHERE uri='$uri'");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] > 0)
{
$db->exec("DELETE FROM recently_played WHERE uri = '$uri'");
}
$query = $db->query("SELECT COUNT(*) as count FROM recently_played");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] == 10)
{
$db->exec("DELETE FROM recently_played WHERE id = (SELECT id FROM recently_played ORDER BY id LIMIT 1)");
}
elseif($row['count'] > 10)
{
$db->exec("DELETE FROM recently_played");
}
$db->exec("INSERT INTO recently_played (artist,title,uri) VALUES ('$artist','$title','$uri')");
}
function clear_recently_played()
{
$db = new SQLite3('db/recently-played.db');
$db->exec("DELETE FROM recently_played");
}
// Playlists
function save_playlist($uris)
{
$uris = trim($uris);
$uris = preg_replace('/\s+/', '', $uris);
$uris = explode(',', $uris);
foreach($uris as $uri)
{
$uri = url_to_uri($uri);
if(!playlist_exists($uri))
{
$get_context = stream_context_create(array('http'=>array('timeout'=>5)));
@$get = file_get_contents('https://embed.spotify.com/?uri=' . $uri, false, $get_context);
if(empty($get))
{
$no_match = true;
}
else
{
preg_match_all("'<title>(.*?)</title>'si", $get, $name);
if($name[1])
{
$name = hscd(strstr($name[1][0], ' by', true));
}
else
{
$no_match = true;
}
}
if(isset($no_match))
{
$name = 'Unknown';
}
$db = new SQLite3('db/playlists.db');
$name = sqlite_escape($name);
$query = $db->query("SELECT COUNT(*) as count FROM playlists WHERE name='$name' AND uri='$uri' COLLATE NOCASE");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] == 0)
{
$db->exec("INSERT INTO playlists (name,uri) VALUES ('$name','$uri')");
}
}
}
if(isset($no_match))
{
return(0);
}
else
{
return(1);
}
}
function playlist_exists($uri)
{
$db = new SQLite3('db/playlists.db');
$query = $db->query("SELECT COUNT(*) as count FROM playlists WHERE uri='$uri' COLLATE NOCASE");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] != 0)
{
return(true);
}
else
{
return(false);
}
}
function remove_playlist($id)
{
$db = new SQLite3('db/playlists.db');
$db->exec("DELETE FROM playlists WHERE id='$id'");
}
// Starred
function star($type, $artist, $title, $uri)
{
$db = new SQLite3('db/starred.db');
$query = $db->query("SELECT COUNT(*) as count FROM starred WHERE uri='$uri' COLLATE NOCASE");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] == 0)
{
$db->exec("INSERT INTO starred (type,artist,title,uri) VALUES ('$type','$artist','$title','$uri')");
}
}
function unstar($uri)
{
$db = new SQLite3('db/starred.db');
$db->exec("DELETE FROM starred WHERE uri='$uri'");
}
function is_starred($uri)
{
$db = new SQLite3('db/starred.db');
$query = $db->query("SELECT COUNT(*) as count FROM starred WHERE uri='$uri' COLLATE NOCASE");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] == 1)
{
return('starred');
}
else
{
return('star');
}
}
// Search
function save_search_history($string)
{
$db = new SQLite3('db/search-history.db');
$query = $db->query("SELECT COUNT(*) as count FROM search_history WHERE string='$string' COLLATE NOCASE");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] > 0)
{
$db->exec("DELETE FROM search_history WHERE string='$string' COLLATE NOCASE");
}
$query = $db->query("SELECT COUNT(*) as count FROM search_history");
$row = $query->fetchArray(SQLITE3_ASSOC);
if($row['count'] == 10)
{
$db->exec("DELETE FROM search_history WHERE id = (SELECT id FROM search_history ORDER BY id LIMIT 1)");
}
elseif($row['count'] > 10)
{
$db->exec("DELETE FROM search_history");
}
$db->exec("INSERT INTO search_history (string) VALUES ('$string')");
}
function clear_search_history()
{
$db = new SQLite3('db/search-history.db');
$db->exec("DELETE FROM search_history");
}
// Files
function file_write($file, $content)
{
$fwrite = fopen($file, 'w');
fwrite($fwrite, $content);
fclose($fwrite);
}
// Strings
function true_or_false($var)
{
if($var)
{
return('true');
}
else
{
return('false');
}
}
function strip_string($string)
{
$string = preg_replace('/[^a-zA-Z0-9-\s]/', '', $string);
return($string);
}
function uri_to_url($uri)
{
if(strstr($uri, 'spotify:track:'))
{
$uri = str_replace('spotify:track:', 'http://open.spotify.com/track/', $uri);
}
if(strstr($uri, 'spotify:local:'))
{
$uri = 'local';
}
if(strstr($uri, 'spotify:album:'))
{
$uri = str_replace('spotify:album:', 'http://open.spotify.com/album/', $uri);
}
elseif(strstr($uri, 'spotify:user:'))
{
$uri = explode(':', $uri);
$uri = 'http://open.spotify.com/user/' . $uri[2] . '/playlist/' . $uri[4];
}
return($uri);
}
function url_to_uri($url)
{
if(strstr($url, 'http://open.spotify.com/user/'))
{
$url = str_replace('http://open.spotify.com/user/', '', $url);
$url = str_replace('/', ':', $url);
$url = 'spotify:user:' . $url;
}
return($url);
}
function hsc($string)
{
$string = htmlspecialchars($string, ENT_QUOTES);
return($string);
}
function hscd($string)
{
$string = htmlspecialchars_decode($string, ENT_QUOTES);
return($string);
}
function sqlite_escape($string)
{
return(SQLite3::escapeString($string));
}
?>