user-connections.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once('database.php');
  16. require_once('templating.php');
  17. require_once('user-menu.php');
  18. require_once('data/User.php');
  19. require_once('data/TagCloud.php');
  20. if ($logged_in == false) {
  21. displayError("Error", "Not logged in. You shouldn't be here.");
  22. }
  23. if (isset($_POST['remote_gnufm_url'])) {
  24. // redirect to a foreign GNU FM service
  25. $remote_url = $_POST['remote_gnufm_url'];
  26. $callback_url = $base_url . '/user-connections.php?webservice_url=' . $remote_url . '/2.0/';
  27. $url = $remote_url . '/api/auth/?api_key=' . $gnufm_key . '&cb=' . rawurlencode($callback_url);
  28. header('Location: ' . $url);
  29. }
  30. if (isset($_GET['token']) && isset($_GET['webservice_url'])) {
  31. // Handle authentication callback from a foreign service
  32. $token = $_GET['token'];
  33. $webservice_url = $_GET['webservice_url'];
  34. $sig = md5('api_key' . $lastfm_key . 'methodauth.getSession' . 'token' . $token . $lastfm_secret);
  35. $xmlresponse = simplexml_load_file($webservice_url . '?method=auth.getSession&token=' . $token . '&api_key=' . $lastfm_key . '&api_sig=' . $sig);
  36. if ($xmlresponse) {
  37. foreach ($xmlresponse->children() as $child => $value) {
  38. if ($child == 'session') {
  39. foreach ($value->children() as $child2 => $value2) {
  40. if ($child2 == 'name') {
  41. $remote_username = $value2;
  42. } else if ($child2 == 'key') {
  43. $remote_key = $value2;
  44. }
  45. }
  46. }
  47. }
  48. } elseif (!$xmlresponse || (!isset($remote_username) || !isset($remote_key))) {
  49. displayError("Error", "Sorry, we weren't able to authenticate your account.");
  50. }
  51. // Delete any old connection to this service
  52. $adodb->Execute('DELETE FROM Service_Connections WHERE '
  53. . 'userid = ' . $this_user->uniqueid . ' AND '
  54. . 'webservice_url = ' . $adodb->qstr($webservice_url));
  55. // Create our new connection
  56. $adodb->Execute('INSERT INTO Service_Connections VALUES('
  57. . $this_user->uniqueid . ', '
  58. . $adodb->qstr($webservice_url) . ', '
  59. . $adodb->qstr($remote_key) . ', '
  60. . $adodb->qstr($remote_username) . ')');
  61. // Flush cache so this change takes effect immediately
  62. $adodb->CacheFlush('SELECT * FROM Service_Connections WHERE userid = ' . $this_user->uniqueid . ' AND forward = 1');
  63. $smarty->assign('connection_added', true);
  64. }
  65. if (isset($_GET['forward']) && isset($_GET['service'])) {
  66. // Update the user's forwarding preferences
  67. $adodb->Execute('UPDATE Service_Connections SET forward = ' . (int) ($_GET['forward'])
  68. . ' WHERE userid = ' . $this_user->uniqueid
  69. . ' AND webservice_url = ' . $adodb->qstr($_GET['service']));
  70. // Flush cache so this change takes effect immediately
  71. $adodb->CacheFlush('SELECT * FROM Service_Connections WHERE userid = ' . $this_user->uniqueid . ' AND forward = 1');
  72. }
  73. if (isset($lastfm_key)) {
  74. $smarty->assign('lastfm_key', $lastfm_key);
  75. }
  76. if (isset($gnufm_key)) {
  77. $smarty->assign('gnufm_key', $gnufm_key);
  78. }
  79. $smarty->assign('connections', $this_user->getConnections());
  80. $submenu = user_menu($this_user, 'Edit');
  81. $smarty->assign('submenu', $submenu);
  82. $smarty->assign('me', $this_user);
  83. $smarty->display('user-connections.tpl');