summaryrefslogtreecommitdiffstats
path: root/server/xml.server.php
blob: 18bf91df62a77c1efc4dd5dd564185bde0b01f40 (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
<?php
/*

 Copyright (c) 2001 - 2007 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License v2
 as published by the Free Software Foundation.

 This program 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 this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/**
 * This is accessed remotly to allow outside scripts access to ampache information 
 * as such it needs to verify the session id that is passed 
 */

define('NO_SESSION','1');
require_once '../lib/init.php';

// If it's not a handshake then we can allow it to take up lots of time
if ($_REQUEST['action'] != 'handshake') { 
	set_time_limit(0); 
} 

/* Set the correct headers */
header("Content-type: text/xml; charset=" . Config::get('site_charset'));
header("Content-Disposition: attachment; filename=information.xml");

// If we don't even have access control on then we can't use this!
if (!Config::get('access_control')) { 
	ob_end_clean(); 
	echo xmlData::error('Access Control not Enabled');
	exit; 
}  

/** 
 * Verify the existance of the Session they passed in we do allow them to
 * login via this interface so we do have an exception for action=login
 */
if ((!vauth::session_exists('api',$_REQUEST['auth']) AND $_REQUEST['action'] != 'handshake') || !Access::check_network('init-api',$_SERVER['REMOTE_ADDR'],$_REQUEST['user'],'5')) { 
	debug_event('Access Denied','Invalid Session or unathorized access attempt to API','5'); 
	ob_end_clean(); 
        echo xmlData::error('Access Denied due to ACL or unauthorized access attempt to API, attempt logged');
	exit(); 
}

// If we make it past the check and we're not a hand-shaking then we should extend the session
if ($_REQUEST['action'] != 'handshake') { 
	vauth::session_extend($_REQUEST['auth']); 
} 

switch ($_REQUEST['action']) { 
	case 'handshake': 
		// Send the data we were sent to the API class so it can be chewed on 
		$token = Api::handshake($_REQUEST['timestamp'],$_REQUEST['auth'],$_SERVER['REMOTE_ADDR'],$_REQUEST['user']); 
		
		if (!$token) { 
			ob_end_clean(); 
			echo xmlData::error('Error Invalid Handshake, attempt logged'); 
		} 
		else { 
			ob_end_clean(); 
			echo xmlData::keyed_array($token); 
		} 

	break; 
	case 'artists': 
		Browse::reset_filters(); 
		Browse::set_type('artist'); 
		Browse::set_sort('name','ASC'); 
	
		if ($_REQUEST['filter']) { 
			Browse::set_filter('alpha_match',$_REQUEST['filter']); 
		} 

		// Set the offset
		xmlData::set_offset($_REQUEST['offset']); 

		$artists = Browse::get_objects(); 
		// echo out the resulting xml document
		ob_end_clean(); 
		echo xmlData::artists($artists); 
	break; 
	case 'artist_albums': 
		$artist = new Artist($_REQUEST['filter']); 

		$albums = $artist->get_albums(); 

                // Set the offset
                xmlData::set_offset($_REQUEST['offset']);
		ob_end_clean(); 
		echo xmlData::albums($albums); 
	break; 
	case 'artist_songs': 
		$artist = new Artist($_REQUEST['filter']); 
		$songs = $artist->get_songs(); 

		// Set the offset
		xmlData::set_offset($_REQUEST['offset']); 
		ob_end_clean(); 
		echo xmlData::songs($songs); 
	break; 
	case 'albums': 
		Browse::reset_filters(); 
		Browse::set_type('album'); 
		Browse::set_sort('name','ASC'); 
		
		if ($_REQUEST['filter']) { 
			Browse::set_filter('alpha_match',$_REQUEST['filter']); 
		} 
		$albums = Browse::get_objects(); 

                // Set the offset
                xmlData::set_offset($_REQUEST['offset']);
		ob_end_clean(); 
		echo xmlData::albums($albums); 
	break; 
	case 'album_songs': 
		$album = new Album($_REQUEST['filter']); 
		$songs = $album->get_songs(); 

                // Set the offset
                xmlData::set_offset($_REQUEST['offset']);
		ob_end_clean(); 
		echo xmlData::songs($songs); 
	break; 
	case 'genres': 
		Browse::reset_filters(); 
		Browse::set_type('genre'); 
		Browse::set_sort('name','ASC'); 
		
		if ($_REQUEST['filter']) { 
			Browse::set_filter('alpha_match',$_REQUEST['filter']); 
		} 
		$genres = Browse::get_objects(); 

                // Set the offset
                xmlData::set_offset($_REQUEST['offset']);
		ob_end_clean(); 
		echo xmlData::genres($genres); 
	break; 
	case 'genre_artists': 
		$genre = new Genre($_REQUEST['filter']); 
		$artists = $genre->get_artists(); 
		ob_end_clean(); 
		echo xmlData::artists($artists); 	
	break; 
	case 'genre_albums': 
		$genre = new Genre($_REQUEST['filter']); 
		$albums = $genre->get_albums(); 
		ob_end_clean(); 
		echo xmlData::albums($albums); 
	break;
	case 'genre_songs': 
		$genre = new Genre($_REQUEST['filter']); 
		$songs = $genre->get_songs(); 
		ob_end_clean(); 	
		echo xmlData::songs($songs); 
	break; 
	case 'songs': 
		Browse::reset_filters(); 
		Browse::set_type('song'); 
		Browse::set_sort('title','ASC'); 
		
		if ($_REQUEST['filter']) { 
			Browse::set_filter('alpha_match',$_REQUEST['filter']); 
		} 
		$songs = Browse::get_objects(); 

                // Set the offset
                xmlData::set_offset($_REQUEST['offset']);
		ob_end_clean(); 
		echo xmlData::songs($songs); 
	break; 
	case 'playlists': 
		Browse::reset_filters(); 
		Browse::set_type('playlist'); 
		Browse::set_sort('name','ASC'); 

		if ($_REQUEST['filter']) { 
			Browse::set_filter('alpha_match',$_REQUEST['filter']); 
		} 

		$playlist_ids = Browse::get_objects(); 

		xmlData::set_offset($_REQUEST['offset']); 
		ob_end_clean(); 
		echo xmlData::playlists($playlist_ids);
	break; 
	case 'playlist_songs': 
		$playlist = new Playlist($_REQUEST['filter']); 
		$items = $playlist->get_items(); 

		foreach ($items as $object) { 
			if ($object['type'] == 'song') { 
				$songs[] = $object['object_id']; 
			} 
		} // end foreach

		xmlData::set_offset($_REQUEST['offset']); 
		ob_end_clean(); 
		echo xmlData::songs($songs); 
	break; 
	default:
                ob_end_clean();
                echo xmlData::error('Invalid Request');
	break;
} // end switch action
?>