summaryrefslogtreecommitdiffstats
path: root/apps/media/lib_ampache.php
blob: 3cd9bd4ab26e5ed86a347d214ba83a81361bb459 (plain)
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
<?php

/**
* ownCloud - media plugin
*
* @author Robin Appelman
* @copyright 2010 Robin Appelman icewind1991@gmail.com
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either 
* version 3 of the License, or any later version.
* 
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*  
* You should have received a copy of the GNU Lesser General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

//implementation of ampache's xml api
class OC_MEDIA_AMPACHE{
	
	/**
	* do the initial handshake
	* @param array params
	*/
	public static function handshake($params){
		$auth=(isset($params['auth']))?$params['auth']:false;
		$user=(isset($params['user']))?$params['user']:false;
		$time=(isset($params['timestamp']))?$params['timestamp']:false;
		$now=time();
		if($now-$time>(10*60)){
			echo("<root>
	<error code='400'>timestamp is more then 10 minutes old</error>
</root>");
		}
		if($auth and $user and $time){
			$query=OC_DB::prepare("SELECT user_id, user_password_sha256 from *PREFIX*media_users WHERE user_id=?");
			$users=$query->execute(array($user))->fetchAll();
			if(count($users)>0){
				$pass=$users[0]['user_password_sha256'];
				$key=hash('sha256',$time.$pass);
				if($key==$auth){
					$token=hash('sha256','oc_media_'.$key);
					OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
					$date=date('c');//todo proper update/add/clean dates
					$songs=OC_MEDIA_COLLECTION::getSongCount();
					$artists=OC_MEDIA_COLLECTION::getArtistCount();
					$albums=OC_MEDIA_COLLECTION::getAlbumCount();
					$query=OC_DB::prepare("INSERT INTO *PREFIX*media_sessions (`session_id`, `token`, `user_id`, `start`) VALUES (NULL, ?, ?, now());");
					$query->execute(array($token,$user));
					$expire=date('c',time()+600);
					echo("<root>
	<auth>$token</auth>
	<version>350001</version>
	<update>$date</update>
	<add>$date</add>
	<clean>$date</clean>
	<songs>$songs</songs>
	<artists>$artists</artists>
	<albums>$albums</albums>\
	<session_length>600</session_length>
	<session_expire>$expire</session_expire>
	<tags>0</tags>
	<videos>0</videos>
</root>");
					return;
				}
			}
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
		}else{
			echo("<root>
	<error code='400'>Missing arguments</error>
</root>");
		}
	}
	
	public static function ping($params){
		if(isset($params['auth'])){
			if(self::checkAuth($params['auth'])){
				self::updateAuth($params['auth']);
			}else{
				echo("<root>
	<error code='400'>Invalid login</error>
</root>");
				return;
			}
		}
		echo('<root>');
		echo('<version>350001</version>');
		echo('</root>');
	}
	
	public static function checkAuth($auth){
		if(is_array($auth)){
			if(isset($auth['auth'])){
				$auth=$auth['auth'];
			}else{
				return false;
			}
		}
		//remove old sessions
		$query=OC_DB::prepare("DELETE from *PREFIX*media_sessions WHERE start<(NOW()-600)");
		$query->execute();
		
		$query=OC_DB::prepare("SELECT user_id from *PREFIX*media_sessions WHERE token=?");
		$users=$query->execute(array($auth))->fetchAll();
		if(count($users)>0){
			OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
			return $users[0]['user_id'];
		}else{
			return false;
		}
	}
	
	public static function updateAuth($auth){
		$query=OC_DB::prepare("UPDATE *PREFIX*media_sessions SET start=CURRENT_TIMESTAMP WHERE token=?");
		$query->execute(array($auth));
	}
	
	private static function printArtist($artist){
		$albums=count(OC_MEDIA_COLLECTION::getAlbums($artist['artist_id']));
		$songs=count(OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
		$id=$artist['artist_id'];
		$name=utf8_decode(htmlentities($artist['artist_name']));
		echo("\t<artist id='$id'>\n");
		echo("\t\t<name>$name</name>\n");
		echo("\t\t<albums>$albums</albums>\n");
		echo("\t\t<songs>$songs</songs>\n");
		echo("\t\t<rating>0</rating>\n");
		echo("\t\t<preciserating>0</preciserating>\n");
		echo("\t</artist>\n");
	}
	
	private static function printAlbum($album,$artistName=false){
		if(!$artistName){
			$artistName=OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
		}
		$artistName=utf8_decode(htmlentities($artistName));
		$songs=count(OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
		$id=$album['album_id'];
		$name=utf8_decode(htmlentities($album['album_name']));
		$artist=$album['album_artist'];
		echo("\t<album id='$id'>\n");
		echo("\t\t<name>$name</name>\n");
		echo("\t\t<artist id='$artist'>$artistName</artist>\n");
		echo("\t\t<tracks>$songs</tracks>\n");
		echo("\t\t<rating>0</rating>\n");
		echo("\t\t<preciserating>0</preciserating>\n");
		echo("\t</album>\n");
	}
	
	private static function printSong($song,$artistName=false,$albumName=false){
		global $WEBROOT;
		if(!$artistName){
			$artistName=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
		}
		if(!$albumName){
			$albumName=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
		}
		$artistName=utf8_decode(htmlentities($artistName));
		$albumName=utf8_decode(htmlentities($albumName));
		if (isset($_SERVER['HTTPS'])) {
			$PROTO="https://";
		} else {
			$PROTO="http://";
		}
		$id=$song['song_id'];
		$name=utf8_decode(htmlentities($song['song_name']));
		$artist=$song['song_artist'];
		$album=$song['song_album'];
		echo("\t<song id='$id'>\n");
		echo("\t\t<title>$name</title>\n");
		echo("\t\t<artist id='$artist'>$artistName</artist>\n");
		echo("\t\t<album id='$album'>$albumName</album>\n");
		$url="$PROTO{$_SERVER["HTTP_HOST"]}$WEBROOT/apps/media/server/xml.server.php?action=play&song=$id&auth={$_GET['auth']}";
		$url=htmlentities($url);
		echo("\t\t<url>$url</url>\n");
		echo("\t\t<time>{$song['song_length']}</time>\n");
		echo("\t\t<track>{$song['song_track']}</track>\n");
		echo("\t\t<size>{$song['song_size']}</size>\n");
		echo("\t\t<art></art>\n");
		echo("\t\t<rating>0</rating>\n");
		echo("\t\t<preciserating>0</preciserating>\n");
		echo("\t</song>\n");
	}
	
	public static function artists($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$filter=isset($params['filter'])?$params['filter']:'';
		$exact=isset($params['exact'])?($params['exact']=='true'):false;
		$artists=OC_MEDIA_COLLECTION::getArtists($filter,$exact);
		echo('<root>');
		foreach($artists as $artist){
			self::printArtist($artist);
		}
		echo('</root>');
	}
	
	public static function artist_songs($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		global $SITEROOT;
		$filter=$params['filter'];
		$songs=OC_MEDIA_COLLECTION::getSongs($filter);
		$artist=OC_MEDIA_COLLECTION::getArtistName($filter);
		echo('<root>');
		foreach($songs as $song){
			self::printSong($song,$artist);
		}
		echo('</root>');
	}
	
	public static function artist_albums($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		global $SITEROOT;
		$filter=$params['filter'];
		$albums=OC_MEDIA_COLLECTION::getAlbums($filter);
		$artist=OC_MEDIA_COLLECTION::getArtistName($filter);
		echo('<root>');
		foreach($albums as $album){
			self::printAlbum($album,$artist);
		}
		echo('</root>');
	}
	
	public static function albums($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$filter=isset($params['filter'])?$params['filter']:'';
		$exact=isset($params['exact'])?($params['exact']=='true'):false;
		$albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter,$exact);
		echo('<root>');
		foreach($albums as $album){
			self::printAlbum($album,$artist);
		}
		echo('</root>');
	}
	
	public static function album_songs($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$songs=OC_MEDIA_COLLECTION::getSongs(0,$params['filter']);
		if(count($songs)>0){
			$artist=OC_MEDIA_COLLECTION::getArtistName($songs[0]['song_artist']);
		}
		echo('<root>');
		foreach($songs as $song){
			self::printSong($song,$artist);
		}
		echo('</root>');
	}
	
	public static function songs($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$filter=isset($params['filter'])?$params['filter']:'';
		$exact=isset($params['exact'])?($params['exact']=='true'):false;
		$songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter,$exact);
		echo('<root>');
		foreach($songs as $song){
			self::printSong($song);
		}
		echo('</root>');
	}
	
	public static function song($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		if($song=OC_MEDIA_COLLECTION::getSong($params['filter'])){
			echo('<root>');
			self::printSong($song);
			echo('</root>');
		}
	}
	
	public static function play($params){
		$username=!self::checkAuth($params);
		if($username){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		if($song=OC_MEDIA_COLLECTION::getSong($params['song'])){
			OC_UTIL::setupFS($song["song_user"]);

			header('Content-type: '.OC_FILESYSTEM::getMimeType($song['song_path']));
			header('Content-Length: '.$song['song_size']);
			OC_FILESYSTEM::readfile($song['song_path']);
		}
	}
	
	public static function url_to_song($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$url=$params['url'];
		$songId=substr($url,strrpos($url,'song=')+5);
		if($song=OC_MEDIA_COLLECTION::getSong($songId)){
			echo('<root>');
			self::printSong($song);
			echo('</root>');
		}
	}
	
	public static function search_songs($params){
		if(!self::checkAuth($params)){
			echo("<root>
	<error code='400'>Invalid login</error>
</root>");
			return;
		}
		$filter=$params['filter'];
		$artists=OC_MEDIA_COLLECTION::getArtists($filter);
		$albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter);
		$songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter);
		foreach($artists as $artist){
			$songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
		}
		foreach($albums as $album){
			$songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
		}
		echo('<root>');
		foreach($songs as $song){
			self::printSong($song);
		}
		echo('</root>');
	}
}

?>