/
spotify.c
436 lines (339 loc) · 11.6 KB
/
spotify.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Copyright (c) 2011 Vilhelm K. Vardøy, vilhelmkv@gmail.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "php_spotify.h"
zend_class_entry *spotify_ce;
zend_object_handlers spotify_object_handlers;
static void playlistcontainer_complete(sp_playlistcontainer *pc, void *userdata);
static void logged_in(sp_session *session, sp_error error);
static void logged_out(sp_session *session);
static void log_message(sp_session *session, const char *data);
static sp_session_callbacks callbacks = {
&logged_in,
&logged_out,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&log_message
};
PHP_METHOD(Spotify, __construct)
{
zval *object = getThis();
zval *z_key, *z_user, *z_pass;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &z_key, &z_user, &z_pass) == FAILURE) {
return;
}
sp_session_config config;
sp_error error;
sp_session *session;
FILE *fp;
long key_size;
char *key_filename;
spotify_object *obj = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);
memset(&config, 0, sizeof(config));
config.api_version = SPOTIFY_API_VERSION;
config.cache_location = INI_STR("spotify.cache_location");
config.settings_location = INI_STR("spotify.settings_location");
config.user_agent = "libspotify-php";
key_filename = Z_STRVAL_P(z_key);
fp = fopen(key_filename, "rb");
if (!fp) {
zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Unable to open spotify key file", 0 TSRMLS_CC);
return;
}
fseek(fp, 0L, SEEK_END);
key_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (key_size > 4096) {
fclose(fp);
zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Key file is way too large to be a key file", 0 TSRMLS_CC);
return;
}
obj->key_data = (char*)emalloc(sizeof(char) * key_size);
if (fread(obj->key_data, 1, key_size, fp) != key_size) {
fclose(fp);
zend_throw_exception((zend_class_entry*)zend_exception_get_default(), "Failed reading key file", 0 TSRMLS_CC);
return;
}
fclose(fp);
config.application_key = obj->key_data;
config.application_key_size = key_size;
config.callbacks = &callbacks;
config.userdata = obj;
error = sp_session_create(&config, &session);
if (SP_ERROR_OK != error) {
zend_throw_exception((zend_class_entry*)zend_exception_get_default(), sp_error_message(error), 0 TSRMLS_CC);
return;
}
sp_session_login(session, Z_STRVAL_P(z_user), Z_STRVAL_P(z_pass), 0);
obj->session = session;
obj->timeout = 0;
obj->playlistcontainer = NULL;
do {
sp_session_process_events(obj->session, &obj->timeout);
} while (obj->timeout == 0 || !obj->is_logged_in);
}
PHP_METHOD(Spotify, __destruct)
{
spotify_object *obj = (spotify_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
int timeout = 0;
if (obj->playlistcontainer != NULL) {
//sp_playlistcontainer_release(obj->playlistcontainer);
}
do {
sp_session_process_events(obj->session, &timeout);
} while (timeout == 0);
sp_session_logout(obj->session);
timeout = 0;
do {
sp_session_process_events(obj->session, &timeout);
} while (!obj->is_logged_out || timeout == 0);
efree(obj->key_data);
}
PHP_METHOD(Spotify, getPlaylists)
{
zval *thisptr = getThis(), tempretval;
int i, timeout = 0, num_playlists;
spotify_object *p = (spotify_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
SPOTIFY_METHOD(Spotify, initPlaylistContainer, &tempretval, thisptr);
array_init(return_value);
num_playlists = sp_playlistcontainer_num_playlists(p->playlistcontainer);
for (i=0; i<num_playlists; i++) {
sp_playlist *playlist = sp_playlistcontainer_playlist(p->playlistcontainer, i);
zval *z_playlist;
ALLOC_INIT_ZVAL(z_playlist);
object_init_ex(z_playlist, spotifyplaylist_ce);
SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &tempretval, z_playlist, thisptr, playlist);
add_next_index_zval(return_value, z_playlist);
}
}
PHP_METHOD(Spotify, getStarredPlaylist)
{
zval *object = getThis();
zval temp;
spotify_object *obj = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);
do {
sp_session_process_events(obj->session, &obj->timeout);
} while (obj->timeout == 0);
sp_playlist *playlist = sp_session_starred_create(obj->session);
object_init_ex(return_value, spotifyplaylist_ce);
SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &temp, return_value, object, playlist);
}
PHP_METHOD(Spotify, getPlaylistByURI)
{
zval *uri, temp, *object = getThis();
int timeout = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uri) == FAILURE) {
return;
}
spotify_object *p = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);
sp_link *link = sp_link_create_from_string(Z_STRVAL_P(uri));
if (NULL == link) {
RETURN_FALSE;
}
if (SP_LINKTYPE_PLAYLIST != sp_link_type(link)) {
RETURN_FALSE;
}
sp_playlist *playlist = sp_playlist_create(p->session, link);
if (NULL == playlist) {
RETURN_FALSE;
}
while (!sp_playlist_is_loaded(playlist)) {
sp_session_process_events(p->session, &timeout);
}
object_init_ex(return_value, spotifyplaylist_ce);
SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &temp, return_value, object, playlist);
sp_link_release(link);
}
PHP_METHOD(Spotify, getTrackByURI)
{
zval *uri, temp, *object = getThis();
int timeout = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uri) == FAILURE) {
return;
}
spotify_object *p = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);
sp_link *link = sp_link_create_from_string(Z_STRVAL_P(uri));
if (NULL == link) {
RETURN_FALSE;
}
if (SP_LINKTYPE_TRACK != sp_link_type(link)) {
RETURN_FALSE;
}
sp_track *track = sp_link_as_track(link);
while (!sp_track_is_loaded(track)) {
sp_session_process_events(p->session, &timeout);
}
object_init_ex(return_value, spotifytrack_ce);
SPOTIFY_METHOD2(SpotifyTrack, __construct, &temp, return_value, object, track);
sp_link_release(link);
}
PHP_METHOD(Spotify, getAlbumByURI)
{
zval *uri, temp, *object = getThis();
int timeout = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uri) == FAILURE) {
return;
}
spotify_object *p = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);
sp_link *link = sp_link_create_from_string(Z_STRVAL_P(uri));
if (NULL == link) {
RETURN_FALSE;
}
if (SP_LINKTYPE_ALBUM != sp_link_type(link)) {
RETURN_FALSE;
}
sp_album *album = sp_link_as_album(link);
while (!sp_album_is_loaded(album)) {
sp_session_process_events(p->session, &timeout);
}
object_init_ex(return_value, spotifyalbum_ce);
SPOTIFY_METHOD2(SpotifyAlbum, __construct, &temp, return_value, object, album);
sp_link_release(link);
}
static sp_playlistcontainer_callbacks playlistcontainer_callbacks = {
NULL,
NULL,
NULL,
playlistcontainer_complete
};
static void playlistcontainer_complete(sp_playlistcontainer *pc, void *userdata)
{
spotify_object *p = (spotify_object*)userdata;
p->playlistcontainer = pc;
sp_playlistcontainer_remove_callbacks(pc, &playlistcontainer_callbacks, userdata);
}
PHP_METHOD(Spotify, initPlaylistContainer)
{
int timeout = 0;
spotify_object *p = (spotify_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
if (p->playlistcontainer != NULL) {
RETURN_TRUE;
}
sp_playlistcontainer *tempcontainer = sp_session_playlistcontainer(p->session);
sp_playlistcontainer_add_callbacks(tempcontainer, &playlistcontainer_callbacks, p);
while (p->playlistcontainer == NULL) {
sp_session_process_events(p->session, &timeout);
}
RETURN_TRUE;
}
static void logged_in(sp_session *session, sp_error error)
{
spotify_object *p = sp_session_userdata(session);
p->is_logged_in = 1;
if (SP_ERROR_OK != error) {
p->is_logged_out = 1;
sp_session_release(session);
char *errMsg;
spprintf(&errMsg, 0, "login failed: %s", sp_error_message(error));
zend_throw_exception((zend_class_entry*)zend_exception_get_default(), errMsg, 0 TSRMLS_CC);
}
}
static void logged_out(sp_session *session)
{
spotify_object *p = sp_session_userdata(session);
p->is_logged_out = 1;
}
static void log_message(sp_session *session, const char *data)
{
//php_printf("SPOTIFY_DEBUG: %s", data);
}
function_entry spotify_methods[] = {
PHP_ME(Spotify, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Spotify, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(Spotify, getPlaylists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Spotify, getStarredPlaylist, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Spotify, getPlaylistByURI, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Spotify, getTrackByURI, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Spotify, getAlbumByURI, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Spotify, initPlaylistContainer, NULL, ZEND_ACC_PRIVATE)
{NULL, NULL, NULL}
};
void spotify_free_storage(void *object TSRMLS_DC)
{
spotify_object *obj = (spotify_object*)object;
sp_session_release(obj->session);
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
}
zend_object_value spotify_create_handler(zend_class_entry *type TSRMLS_DC)
{
zval *tmp;
zend_object_value retval;
spotify_object *obj = (spotify_object *)emalloc(sizeof(spotify_object));
memset(obj, 0, sizeof(spotify_object));
// obj->std.ce = type;
zend_object_std_init(&obj->std, type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &type->default_properties,
(copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
retval.handle = zend_objects_store_put(obj, NULL,
spotify_free_storage, NULL TSRMLS_CC);
retval.handlers = &spotify_object_handlers;
return retval;
}
PHP_INI_BEGIN()
PHP_INI_ENTRY("spotify.cache_location", "spotify_cache/", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("spotify.settings_location", "spotify_settings/", PHP_INI_ALL, NULL)
PHP_INI_END()
PHP_MINIT_FUNCTION(spotify)
{
REGISTER_INI_ENTRIES();
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Spotify", spotify_methods);
spotify_ce = zend_register_internal_class(&ce TSRMLS_CC);
spotify_ce->create_object = spotify_create_handler;
memcpy(&spotify_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
spotify_object_handlers.clone_obj = NULL;
spotify_init_playlist(TSRMLS_C);
spotify_init_track(TSRMLS_C);
spotify_init_artist(TSRMLS_C);
spotify_init_album(TSRMLS_C);
spotify_init_user(TSRMLS_C);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(spotify)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
zend_module_entry spotify_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_SPOTIFY_EXTNAME,
NULL, /* Functions */
PHP_MINIT(spotify), /* MINIT */
PHP_MSHUTDOWN(spotify), /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_SPOTIFY_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SPOTIFY
ZEND_GET_MODULE(spotify)
#endif