user-connections.php 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php

/* GNU FM -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Free Software Foundation, Inc

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

require_once('database.php');
require_once('templating.php');
24 25 26 27
require_once('user-menu.php');
require_once('data/User.php');
require_once('data/TagCloud.php');

28
if ($logged_in == false) {
29
	displayError("Error", "Not logged in. You shouldn't be here.");
30
}
31

32 33 34 35 36 37 38 39
if (isset($_POST['remote_gnufm_url'])) {
	// redirect to a foreign GNU FM service
	$remote_url = $_POST['remote_gnufm_url'];
	$callback_url = $base_url . '/user-connections.php?webservice_url=' . $remote_url . '/2.0/';
	$url = $remote_url . '/api/auth/?api_key=' . $gnufm_key . '&cb=' . rawurlencode($callback_url);
	header('Location: ' . $url);
}

40
if (isset($_GET['token']) && isset($_GET['webservice_url'])) {
41
	// Handle authentication callback from a foreign service
42 43
	$token = $_GET['token'];
	$webservice_url = $_GET['webservice_url'];
44
	$sig = md5('api_key' . $lastfm_key . 'methodauth.getSession' . 'token' . $token . $lastfm_secret);
45
	$xmlresponse = simplexml_load_file($webservice_url . '?method=auth.getSession&token=' . $token . '&api_key=' . $lastfm_key . '&api_sig=' . $sig);
46 47 48 49 50 51 52 53 54
	if ($xmlresponse) {
		foreach ($xmlresponse->children() as $child => $value) {
			if ($child == 'session') {
				foreach ($value->children() as $child2 => $value2) {
					if ($child2 == 'name') {
						$remote_username = $value2;
					} else if ($child2 == 'key') {
						$remote_key = $value2;
					}
55 56 57
				}
			}
		}
58
	} elseif (!$xmlresponse || (!isset($remote_username) || !isset($remote_key))) {
59
		displayError("Error", "Sorry, we weren't able to authenticate your account.");
60 61 62 63 64
	}

	// Delete any old connection to this service
	$adodb->Execute('DELETE FROM Service_Connections WHERE '
		. 'userid = ' . $this_user->uniqueid . ' AND '
65
		. 'webservice_url = ' . $adodb->qstr($webservice_url));
66 67 68 69 70

	// Create our new connection
	$adodb->Execute('INSERT INTO Service_Connections VALUES('
		. $this_user->uniqueid . ', '
		. $adodb->qstr($webservice_url) . ', '
71 72
		. $adodb->qstr($remote_key) . ', '
		. $adodb->qstr($remote_username) . ')');
73

74 75 76
	// Flush cache so this change takes effect immediately
	$adodb->CacheFlush('SELECT * FROM Service_Connections WHERE userid = ' . $this_user->uniqueid . ' AND forward = 1');

77 78
	$smarty->assign('connection_added', true);
}
79

80
if (isset($_GET['forward']) && isset($_GET['service'])) {
81 82 83 84
	// Update the user's forwarding preferences
	$adodb->Execute('UPDATE Service_Connections SET forward = ' . (int) ($_GET['forward'])
		. ' WHERE userid = ' . $this_user->uniqueid
		. ' AND webservice_url = ' . $adodb->qstr($_GET['service']));
85 86 87

	// Flush cache so this change takes effect immediately
	$adodb->CacheFlush('SELECT * FROM Service_Connections WHERE userid = ' . $this_user->uniqueid . ' AND forward = 1');
88 89
}

90
if (isset($lastfm_key)) {
91
	$smarty->assign('lastfm_key', $lastfm_key);
92
}
93 94 95
if (isset($gnufm_key)) {
	$smarty->assign('gnufm_key', $gnufm_key);
}
96

97 98
$smarty->assign('connections', $this_user->getConnections());

99 100 101 102 103
$submenu = user_menu($this_user, 'Edit');
$smarty->assign('submenu', $submenu);

$smarty->assign('me', $this_user);
$smarty->display('user-connections.tpl');