PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/server/index.ajax.php

https://gitlab.com/x33n/ampache
PHP | 308 lines | 275 code | 9 blank | 24 comment | 58 complexity | 294ef29c181cee2c519e023d863c374b MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. /**
  23. * Sub-Ajax page, requires AJAX_INCLUDE
  24. */
  25. if (!defined('AJAX_INCLUDE')) { exit; }
  26. $results = array();
  27. switch ($_REQUEST['action']) {
  28. case 'random_albums':
  29. $albums = Album::get_random(6);
  30. if (count($albums) AND is_array($albums)) {
  31. ob_start();
  32. require_once AmpConfig::get('prefix') . '/templates/show_random_albums.inc.php';
  33. $results['random_selection'] = ob_get_clean();
  34. } else {
  35. $results['random_selection'] = '<!-- None found -->';
  36. if (Access::check('interface', '100')) {
  37. $catalogs = Catalog::get_catalogs();
  38. if (count($catalogs) == 0) {
  39. $results['random_selection'] = sprintf(T_('No catalog configured yet. To start streaming your media, you now need to %s add a catalog %s'), '<a href="' . AmpConfig::get('web_path') . '/admin/catalog.php?action=show_add_catalog">', '</a>.<br /><br />');
  40. }
  41. }
  42. }
  43. break;
  44. case 'random_videos':
  45. $videos = Video::get_random(6);
  46. if (count($videos) AND is_array($videos)) {
  47. ob_start();
  48. require_once AmpConfig::get('prefix') . '/templates/show_random_videos.inc.php';
  49. $results['random_video_selection'] = ob_get_clean();
  50. } else {
  51. $results['random_video_selection'] = '<!-- None found -->';
  52. }
  53. break;
  54. case 'artist_info':
  55. if (AmpConfig::get('lastfm_api_key') && (isset($_REQUEST['artist']) || isset($_REQUEST['fullname']))) {
  56. if ($_REQUEST['artist']) {
  57. $artist = new Artist($_REQUEST['artist']);
  58. $artist->format();
  59. $biography = Recommendation::get_artist_info($artist->id);
  60. } else {
  61. $biography = Recommendation::get_artist_info(null, rawurldecode($_REQUEST['fullname']));
  62. }
  63. ob_start();
  64. require_once AmpConfig::get('prefix') . '/templates/show_artist_info.inc.php';
  65. $results['artist_biography'] = ob_get_clean();
  66. }
  67. break;
  68. case 'similar_artist':
  69. if (AmpConfig::get('show_similar') && isset($_REQUEST['artist'])) {
  70. $artist = new Artist($_REQUEST['artist']);
  71. $artist->format();
  72. $object_ids = array();
  73. $missing_objects = array();
  74. if ($similars = Recommendation::get_artists_like($artist->id, 10, !AmpConfig::get('wanted'))) {
  75. foreach ($similars as $similar) {
  76. if ($similar['id']) {
  77. $object_ids[] = $similar['id'];
  78. } else {
  79. $missing_objects[] = $similar;
  80. }
  81. }
  82. }
  83. ob_start();
  84. require_once AmpConfig::get('prefix') . '/templates/show_recommended_artists.inc.php';
  85. $results['similar_artist'] = ob_get_clean();
  86. }
  87. break;
  88. case 'similar_now_playing':
  89. $media_id = $_REQUEST['media_id'];
  90. if (AmpConfig::get('show_similar') && isset($media_id) && isset($_REQUEST['media_artist'])) {
  91. $artists = Recommendation::get_artists_like($_REQUEST['media_artist'], 3, false);
  92. $songs = Recommendation::get_songs_like($media_id, 3);
  93. ob_start();
  94. require_once AmpConfig::get('prefix') . '/templates/show_now_playing_similar.inc.php';
  95. $results['similar_items_' . $media_id] = ob_get_clean();
  96. }
  97. break;
  98. case 'concerts':
  99. if (AmpConfig::get('show_concerts') && isset($_REQUEST['artist'])) {
  100. $artist = new Artist($_REQUEST['artist']);
  101. $artist->format();
  102. if ($artist->id) {
  103. $up_concerts = Artist_Event::get_upcoming_events($artist);
  104. $past_concerts = Artist_Event::get_past_events($artist);
  105. $coming_concerts = array();
  106. $concerts = array();
  107. if ($up_concerts) {
  108. foreach ($up_concerts->children() as $item) {
  109. if ($item->getName() == 'event') {
  110. $coming_concerts[] = $item;
  111. }
  112. }
  113. }
  114. if ($past_concerts) {
  115. foreach ($past_concerts->children() as $item) {
  116. if ($item->getName() == 'event') {
  117. $concerts[] = $item;
  118. }
  119. }
  120. }
  121. }
  122. ob_start();
  123. require_once AmpConfig::get('prefix') . '/templates/show_concerts.inc.php';
  124. $results['concerts'] = ob_get_clean();
  125. }
  126. break;
  127. case 'wanted_missing_albums':
  128. if (AmpConfig::get('wanted') && (isset($_REQUEST['artist']) || isset($_REQUEST['artist_mbid']))) {
  129. if (isset($_REQUEST['artist'])) {
  130. $artist = new Artist($_REQUEST['artist']);
  131. $artist->format();
  132. if ($artist->mbid) {
  133. $walbums = Wanted::get_missing_albums($artist);
  134. } else {
  135. debug_event('wanted', 'Cannot get missing albums: MusicBrainz ID required.', '5');
  136. }
  137. } else {
  138. $walbums = Wanted::get_missing_albums(null, $_REQUEST['artist_mbid']);
  139. }
  140. ob_start();
  141. require_once AmpConfig::get('prefix') . '/templates/show_missing_albums.inc.php';
  142. $results['missing_albums'] = ob_get_clean();
  143. }
  144. break;
  145. case 'add_wanted':
  146. if (AmpConfig::get('wanted') && isset($_REQUEST['mbid'])) {
  147. $mbid = $_REQUEST['mbid'];
  148. if (empty($_REQUEST['artist'])) {
  149. $artist_mbid = $_REQUEST['artist_mbid'];
  150. $artist = null;
  151. } else {
  152. $artist = $_REQUEST['artist'];
  153. $aobj = new Artist($artist);
  154. $artist_mbid = $aobj->mbid;
  155. }
  156. $name = $_REQUEST['name'];
  157. $year = $_REQUEST['year'];
  158. if (!Wanted::has_wanted($mbid)) {
  159. Wanted::add_wanted($mbid, $artist, $artist_mbid, $name, $year);
  160. ob_start();
  161. $walbum = new Wanted(Wanted::get_wanted($mbid));
  162. $walbum->show_action_buttons();
  163. $results['wanted_action_' . $mbid] = ob_get_clean();
  164. } else {
  165. debug_event('wanted', 'Already wanted, skipped.', '5');
  166. }
  167. }
  168. break;
  169. case 'remove_wanted':
  170. if (AmpConfig::get('wanted') && isset($_REQUEST['mbid'])) {
  171. $mbid = $_REQUEST['mbid'];
  172. $walbum = new Wanted(Wanted::get_wanted($mbid));
  173. Wanted::delete_wanted($mbid);
  174. ob_start();
  175. $walbum->accepted = false;
  176. $walbum->id = 0;
  177. $walbum->show_action_buttons();
  178. $results['wanted_action_' . $mbid] = ob_get_clean();
  179. }
  180. break;
  181. case 'accept_wanted':
  182. if (AmpConfig::get('wanted') && isset($_REQUEST['mbid'])) {
  183. $mbid = $_REQUEST['mbid'];
  184. $walbum = new Wanted(Wanted::get_wanted($mbid));
  185. $walbum->accept();
  186. ob_start();
  187. $walbum->show_action_buttons();
  188. $results['wanted_action_' . $mbid] = ob_get_clean();
  189. }
  190. break;
  191. case 'reloadnp':
  192. ob_start();
  193. show_now_playing();
  194. $results['now_playing'] = ob_get_clean();
  195. ob_start();
  196. $data = Song::get_recently_played();
  197. Song::build_cache(array_keys($data));
  198. require_once AmpConfig::get('prefix') . '/templates/show_recently_played.inc.php';
  199. $results['recently_played'] = ob_get_clean();
  200. break;
  201. case 'sidebar':
  202. switch ($_REQUEST['button']) {
  203. case 'home':
  204. case 'modules':
  205. case 'localplay':
  206. case 'player':
  207. case 'preferences':
  208. $button = $_REQUEST['button'];
  209. break;
  210. case 'admin':
  211. if (Access::check('interface','100')) { $button = $_REQUEST['button']; } else { exit; }
  212. break;
  213. default:
  214. exit;
  215. } // end switch on button
  216. Ajax::set_include_override(true);
  217. ob_start();
  218. $_SESSION['state']['sidebar_tab'] = $button;
  219. require_once AmpConfig::get('prefix') . '/templates/sidebar.inc.php';
  220. $results['sidebar-content'] = ob_get_contents();
  221. ob_end_clean();
  222. break;
  223. case 'shoutbox':
  224. ob_start();
  225. $since = $_REQUEST['since'];
  226. if ($since) {
  227. $shouts = Shoutbox::get_shouts_since(intval($since / 1000));
  228. echo "<script language='javascript' type='text/javascript'>";
  229. foreach ($shouts as $id) {
  230. $shout = new Shoutbox($id);
  231. echo "noty({text: '" . addslashes($shout->get_display(true)) . "',
  232. type: 'alert', layout: 'bottomRight',
  233. template: '<div class=\"noty_message noty_ampache\"><span class=\"noty_text noty_ampache\"></span><div class=\"noty_close noty_ampache\"></div></div>',
  234. });";
  235. }
  236. echo "</script>";
  237. }
  238. $results['live_shoutbox'] = ob_get_clean();
  239. break;
  240. case 'start_channel':
  241. if (Access::check('interface','75')) {
  242. ob_start();
  243. $channel = new Channel($_REQUEST['id']);
  244. if ($channel->id) {
  245. if ($channel->check_channel()) {
  246. $channel->stop_channel();
  247. }
  248. $channel->start_channel();
  249. sleep(1);
  250. echo $channel->get_channel_state();
  251. }
  252. $results['channel_state_' . $_REQUEST['id']] = ob_get_clean();
  253. }
  254. break;
  255. case 'stop_channel':
  256. if (Access::check('interface','75')) {
  257. ob_start();
  258. $channel = new Channel($_REQUEST['id']);
  259. if ($channel->id) {
  260. $channel->stop_channel();
  261. sleep(1);
  262. echo $channel->get_channel_state();
  263. }
  264. $results['channel_state_' . $_REQUEST['id']] = ob_get_clean();
  265. }
  266. break;
  267. case 'slideshow':
  268. ob_start();
  269. $images = Slideshow::get_current_slideshow();
  270. if (count($images) > 0) {
  271. $fsname = 'fslider_' . time();
  272. echo "<div id='" . $fsname . "'>";
  273. foreach ($images as $image) {
  274. echo "<img src='" . $image['url'] . "' alt='' onclick='update_action();' />";
  275. }
  276. echo "</div>";
  277. $results['fslider'] = ob_get_clean();
  278. ob_start();
  279. echo "<script language='javascript' type='text/javascript'>";
  280. echo "$('#" . $fsname . "').rhinoslider({
  281. showTime: 15000,
  282. effectTime: 2000,
  283. randomOrder: true,
  284. controlsPlayPause: false,
  285. autoPlay: true,
  286. showBullets: 'never',
  287. showControls: 'always',
  288. controlsMousewheel: false,
  289. });";
  290. echo "</script>";
  291. }
  292. $results['fslider_script'] = ob_get_clean();
  293. break;
  294. default:
  295. $results['rfc3514'] = '0x1';
  296. break;
  297. } // switch on action;
  298. // We always do this
  299. echo xoutput_from_array($results);