RemoteUser.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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($install_path . '/database.php');
  16. require_once($install_path . '/data/sanitize.php');
  17. require_once($install_path . '/utils/human-time.php');
  18. require_once($install_path . '/data/Server.php');
  19. require_once($install_path . '/data/Tag.php');
  20. require_once($install_path . '/data/User.php');
  21. /**
  22. * Represents User data
  23. *
  24. * General attributes are accessible as public variables.
  25. *
  26. */
  27. class RemoteUser extends User {
  28. public $domain;
  29. public $lastfm = false;
  30. public $remote = true;
  31. private $nowplaying = array();
  32. /**
  33. * User constructor
  34. *
  35. * @param string $name The name of the user to load
  36. */
  37. function __construct($name, $data = null) {
  38. global $base_url, $lastfm_key;
  39. $base = preg_replace('#/$#', '', $base_url);
  40. $components = explode('@', $name, 2);
  41. $this->username = $components[0];
  42. $this->domain = $components[1];
  43. if (strstr($this->domain, 'last.fm') || strstr($this->domain, 'ws.audioscrobbler.com')) {
  44. $this->domain = 'ws.audioscrobbler.com';
  45. $this->lastfm = true;
  46. }
  47. if ($this->lastfm && !isset($lastfm_key)) {
  48. throw new Exception('This server isn\'t configured to communicate with Last.fm');
  49. }
  50. if (is_array($data)) {
  51. $row = $data;
  52. } else {
  53. global $adodb;
  54. $row = $this->getXML('?method=user.getInfo&user=' . $this->username);
  55. if (!isset($row->user)) {
  56. throw new Exception('EUSER', 22);
  57. } else {
  58. $row = $row->user;
  59. }
  60. }
  61. $this->name = $name;
  62. $this->email = $row->email;
  63. $this->fullname = $row->realname;
  64. $this->url = $row->url;
  65. $this->homepage = $row->homepage;
  66. $this->bio = $row->bio;
  67. $this->location = $row->location;
  68. $this->location_uri = $row->location_uri;
  69. $this->id = $row->webid_uri;
  70. $this->webid_uri = $row->webid_uri;
  71. $this->avatar_uri = $row->image[2];
  72. $this->laconica_profile = $row->laconica_profile;
  73. $this->journal_rss = $row->journal_rss;
  74. $this->acctid = $row->url . '#acct';
  75. $this->created = $row->created;
  76. $this->modified = $row->modified;
  77. $this->playcount = $row->playcount;
  78. if (!preg_match('/\:/', $this->id)) {
  79. $this->id = $this->getURL() . '#me';
  80. }
  81. }
  82. function save() {
  83. throw Exception('Unable to save remote user');
  84. }
  85. /**
  86. * Get a user's scrobbles ordered by time
  87. *
  88. * @param int $number The number of scrobbles to return
  89. * @param int $offset The position of the first scrobble to return
  90. * @return array An array of scrobbles with human time
  91. */
  92. function getScrobbles($number, $offset = 0) {
  93. $page = (int) $offset / $number + 1;
  94. $xml = $this->getXML('?method=user.getRecentTracks&user=' . $this->username . '&limit=' . $number . '&page=' . $page);
  95. $tracks = array();
  96. foreach($xml->recenttracks->track as $xmltrack) {
  97. $track = array();
  98. $track['artist'] = $xmltrack->artist;
  99. $track['album'] = $xmltrack->album;
  100. $track['track'] = $xmltrack->name;
  101. $track['artisturl'] = $xmltrack->url;
  102. $track['albumurl'] = $xmltrack->url;
  103. $track['trackurl'] = $xmltrack->url;
  104. $track['time'] = (int) $xmltrack->date->attributes()->uts;
  105. $track['timehuman'] = human_timestamp($track['time']);
  106. if($xmltrack->attributes()->nowplaying == true) {
  107. $this->nowplaying[] = $track;
  108. } else {
  109. $tracks[] = $track;
  110. }
  111. }
  112. return $tracks;
  113. }
  114. /**
  115. * Retrieve a user's avatar via the gravatar service
  116. *
  117. * @param int $size The desired size of the avatar (between 1 and 512 pixels)
  118. * @return array A URL to the user's avatar image
  119. */
  120. function getAvatar($size = 64) {
  121. if (!empty($this->avatar_uri)) {
  122. return $this->avatar_uri;
  123. }
  124. return 'https://www.gravatar.com/avatar/' . md5(strtolower($this->email)) . '?s=' . $size . '&d=monsterid';
  125. }
  126. function getURL($component = 'profile', $params = false) {
  127. return Server::getUserURL($this->name, $component, $params);
  128. }
  129. /**
  130. * Get a user's now-playing tracks
  131. *
  132. * @return array An array of nowplaying data
  133. */
  134. function getNowPlaying($number) {
  135. if(!empty($this->nowplaying)) {
  136. return $this->nowplaying;
  137. }
  138. $this->getScrobbles($number);
  139. return $this->nowplaying;
  140. }
  141. /**
  142. * Log in to the gnukebox server
  143. *
  144. * @return array A string containing the session key to be used for scrobbling
  145. */
  146. function getScrobbleSession() {
  147. return false;
  148. }
  149. /**
  150. * Log in to the radio server
  151. *
  152. * @param string $station The station to be played
  153. * @return array A string containing the session key to be used for streaming
  154. */
  155. function getRadioSession($station) {
  156. return false;
  157. }
  158. /**
  159. * Log in to the web services
  160. *
  161. * @return array A string containing the web session key
  162. */
  163. function getWebServiceSession() {
  164. return false;
  165. }
  166. /**
  167. * Get this user's top artists
  168. *
  169. * @param int $limit The number of artists to return
  170. * @param int $offset Skip this number of rows before returning artists
  171. * @param bool $streamable Only return streamable artists
  172. * @param int $begin Only use scrobbles with time higher than this timestamp
  173. * @param int $end Only use scrobbles with time lower than this timestamp
  174. * @param int $cache Caching period in seconds
  175. * @return array An array of artists ((artist, freq, artisturl) ..) or empty array in case of failure
  176. */
  177. function getTopArtists($limit = 20, $offset = 0, $streamable = False, $begin = null, $end = null, $cache = 600) {
  178. return array();
  179. }
  180. /**
  181. * Get this user's top tracks
  182. *
  183. * @param int $limit The number of tracks to return
  184. * @param int $offset Skip this number of rows before returning tracks
  185. * @param bool $streamable Only return streamable tracks
  186. * @param int $begin Only use scrobbles with time higher than this timestamp
  187. * @param int $end Only use scrobbles with time lower than this timestamp
  188. * @param int $cache Caching period in seconds
  189. * @return array An array of tracks ((artist, track, freq, listeners, artisturl, trackurl) ..) or empty array in case of failure
  190. */
  191. function getTopTracks($limit = 20, $offset = 0, $streamable = False, $begin = null, $end = null, $cache = 600) {
  192. return array();
  193. }
  194. public function getTotalTracks($since = null) {
  195. return $this->playcount;
  196. }
  197. /**
  198. * Get a user's top tags, ordered by tag count
  199. *
  200. * @param int $limit The number of tags to return (default is 10)
  201. * @param int $offset The position of the first tag to return (default is 0)
  202. * @param int $cache Caching period of query in seconds (default is 600)
  203. * @return An array of tag details ((tag, freq) .. )
  204. */
  205. function getTopTags($limit=10, $offset=0, $cache=600) {
  206. return array();
  207. }
  208. /**
  209. * Get artists, albums, or tracks tagged with tag by user
  210. *
  211. * @param string $tag Items are tagged by this tag
  212. * $param string $taggingtype Type of tags to return (artist|album|track)
  213. * @param int $limit The number of items to return (default is 10)
  214. * @param int $offset The position of the first item to return (default is 0)
  215. * @param int $cache Caching period of query in seconds (default is 600)
  216. * @param bool $streamable Show only content by streamable artists (default is False)
  217. * @return An array of item details ((artist, .. , freq) .. )
  218. */
  219. function getPersonalTags($tag, $taggingtype, $limit=10, $offset=0, $cache=600, $streamable=False) {
  220. if(isset($tag) and isset($taggingtype)) {
  221. return array();
  222. }
  223. }
  224. /**
  225. * Get a user's tags for a specific artist
  226. *
  227. * @param string $artist The name of the artist to fetch tags for
  228. * @param int $limit The number of tags to return (default is 10)
  229. * @param int $offset The position of the first tag to return (default is 0)
  230. * @param int $cache Caching period of query in seconds (default is 600)
  231. * @return An array of tag details ((tag, freq) .. )
  232. */
  233. function getTagsForArtist($artist, $limit=10, $offset=0, $cache=0) {
  234. if(isset($artist)) {
  235. return array();
  236. }
  237. }
  238. /**
  239. * Get tag count for tag and user
  240. *
  241. * @param string $tag The tag to show user's tag count for
  242. * @param int $cache Caching period of query in seconds (default is 600)
  243. * @return An array of tag details ((tag, freq) .. )
  244. */
  245. function getTagInfo($tag, $cache=600) {
  246. if(isset($tag)) {
  247. return array();
  248. }
  249. }
  250. /**
  251. * Retrieves a list of user's loved tracks
  252. *
  253. * @param int $limit The number of tracks to return
  254. * @param int $offset Skip this number of rows before returning tracks
  255. * @param bool $streamable Only return streamable tracks
  256. * @param int $artist Only return results from this artist
  257. * @param int $cache Caching period in seconds
  258. * @return array An array of tracks ((artist, track, freq, listeners, artisturl, trackurl) ..) or empty array in case of failure
  259. */
  260. function getLovedTracks($limit = 20, $offset = 0, $streamable = False, $artist = null, $cache = 600) {
  261. $page = (int) $offset / $limit + 1;
  262. $xml = $this->getXML('?method=user.getLovedTracks&user=' . $this->username . '&limit=' . $limit . '&page=' . $page);
  263. $loved = array();
  264. foreach($xml->lovedtracks->track as $l) {
  265. $track = array();
  266. $track['artist'] = $l->artist->name;
  267. $track['track'] = $l->name;
  268. $track['artisturl'] = $l->artist->url;
  269. $track['trackurl'] = $l->url;
  270. $loved[] = $track;
  271. }
  272. return $loved;
  273. }
  274. /**
  275. * Retrieves a list of user's loved artists
  276. *
  277. * @param int $limit The number of artists to return
  278. * @param int $offset Skip this number of rows before returning artists
  279. * @param bool $streamable Only return streamable artists
  280. * @param int $cache Caching period in seconds
  281. * @return array An array of artists ((artist, freq, artisturl) ..) or empty array in case of failure
  282. */
  283. function getLovedArtists($limit = 20, $offset = 0, $streamable = False, $cache = 600) {
  284. return array();
  285. }
  286. /**
  287. * Get a user's banned tracks
  288. *
  289. * @param int $limit The number of tracks to return (defaults to 50)
  290. * @return array An array of track details
  291. */
  292. function getBannedTracks($limit = 50, $offset = 0) {
  293. return array();
  294. }
  295. /**
  296. * Get details of any connections this user has setup to other services.
  297. *
  298. * @return array An array of service connection details
  299. */
  300. function getConnections() {
  301. return array();
  302. }
  303. /**
  304. * Get artists recommended for this user
  305. *
  306. * @param int $limit The number of artists to return (defaults to 10)
  307. * @param bool $randomised Pick artists at random
  308. * @return array An array of artist details
  309. */
  310. function getRecommended($limit = 10, $random = false) {
  311. global $adodb;
  312. $loved = $this->getLovedTracks(50);
  313. $artists = array();
  314. for ($i = 0; $i < min($limit, count($loved) - 1); $i++) {
  315. if ($random) {
  316. $n = rand(0, count($loved) - 1);
  317. } else {
  318. $n = $i;
  319. }
  320. $artists[] = $loved[$n]['artist'];
  321. }
  322. $recommendedArtists = array();
  323. foreach ($artists as $artist_name) {
  324. try {
  325. $artist = new Artist($artist_name);
  326. } catch (Exception $e) {
  327. continue;
  328. }
  329. $similar = $artist->getSimilar(5);
  330. foreach ($similar as $sa) {
  331. if (!array_key_exists($sa['artist'], $recommendedArtists)) {
  332. $recommendedArtists[$sa['artist']] = $sa;
  333. }
  334. }
  335. }
  336. $limit = min($limit, count($recommendedArtists) - 1);
  337. if ($random) {
  338. $randomArtists = array();
  339. $keys = array_keys($recommendedArtists);
  340. for ($i = 0; $i < $limit; $i++) {
  341. $randomArtists[] = $recommendedArtists[$keys[rand(0, count($recommendedArtists) - 1)]];
  342. }
  343. return $randomArtists;
  344. } else {
  345. return array_slice($recommendedArtists, 0, $limit);
  346. }
  347. }
  348. /**
  349. * Determines whether a user has permission to manage an artist
  350. *
  351. * @oaram string $artist The name of the artist to check
  352. * @return bool Boolean indicating whether this user can edit the artist or not.
  353. */
  354. function manages($artist) {
  355. return false;
  356. }
  357. /**
  358. * Checks whether this user has any loved tracks
  359. *
  360. * @return bool Boolean indicating whether this user has marked any tracks as being loved in the past.
  361. */
  362. function hasLoved() {
  363. return count($this->getLovedTracks(1)) == 1;
  364. }
  365. /**
  366. * Find the neighbours of this user based on the number of loved artists shared between them and other users.
  367. *
  368. * @param int The number of neighbours to return (defaults to 10).
  369. * @return array An array of userids, User objects and the number of loved artists shared with this user.
  370. */
  371. function getNeighbours($limit=10) {
  372. return array();
  373. }
  374. /**
  375. * Fetch XML data from a remote GNU FM (or compatible) server. Requests to servers which don't respond to
  376. * GNU FM requests will result in that domain being blacklisted for an hour.
  377. *
  378. * @param string The 2.0 webservice parameters to contact the remote server with
  379. * @return A SimpleXML object
  380. */
  381. function getXML($params) {
  382. global $lastfm_key, $adodb;
  383. // Delete any expired blacklist entries
  384. $adodb->Execute('DELETE FROM Domain_Blacklist WHERE expires < ' . time());
  385. // Check that we haven't blacklisted this domain
  386. $rawdomain = explode('/', $this->domain, 2)[0];
  387. $blackcheck = sprintf('SELECT COUNT(*) FROM Domain_Blacklist WHERE domain=%s', $adodb->qstr($rawdomain));
  388. if($adodb->CacheGetOne(200, $blackcheck) > 0) {
  389. // This domain is blacklisted
  390. throw new Exception('EUSER', 22);
  391. }
  392. $wsurl = 'http://' . $this->domain . '/2.0/' . $params;
  393. if ($this->lastfm) {
  394. $wsurl .= '&api_key=' . $lastfm_key;
  395. }
  396. $response = @simplexml_load_file($wsurl);
  397. if($response == false) {
  398. // Blacklist this domain for the next hour
  399. $blackq = sprintf('INSERT INTO Domain_Blacklist VALUES(%s, %d)', $adodb->qstr($rawdomain), time() + 3600);
  400. $adodb->Execute($blackq);
  401. $adodb->CacheFlush($blackcheck);
  402. throw new Exception('EUSER', 22);
  403. }
  404. return $response;
  405. }
  406. }