Advertisement
TonyAR

php script to get info from mpd server

Aug 4th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2. //
  3. // this script is used in conjunction with the javascript found here --> https://pastebin.com/r534eH9w
  4. //
  5. // mpd server host & port
  6. //
  7. $host="<mpd server name or IP address>";
  8. $port="6600";
  9.  
  10. // determine player state (play, stopped)
  11.  
  12. $playerStatus = getStatus("status",$host,$port);
  13.  
  14. $state = stripColon($playerStatus['9']);
  15.  
  16. if ($state == "stop") {
  17.         echo "0";
  18.         exit(0);
  19. }
  20. // get parameter from URL not actually required with the latest version and
  21. // could be preset as only a single parameter (playlist) is required - assigned to $item below
  22. //
  23. $item = check_input($_GET['id']);
  24. //
  25. // determine if playing radio or local media
  26. //
  27. $type = getStatus("currentsong",$host,$port);
  28.         $file_type = stripColon($type['1']);
  29.         if (empty($file_type)) {
  30.         echo "No current song information returned\n";
  31.         die();
  32.         }
  33.         if (strpos($file_type, 'http') !==false) {
  34.                 // echo "Playing  radio";
  35.                 $play_status = "radio";
  36.         } else {
  37.                 // echo "Playing local media  ";
  38.                 $play_status = "local_media";
  39. }
  40. //
  41. // get playlist id if playing radio (different array returned for radio and local media)
  42. //
  43. if (($item == "playlist") && ($play_status == "radio")) {
  44.         $playlist = getStatus("status",$host,$port);
  45.         $playlist = stripColon($playlist['6']);
  46.         echo $playlist;
  47.         if (empty($playlist)) {
  48.         echo "No playlist information returned\n";
  49.         die();
  50.         }
  51. }
  52. //
  53. // get playlist id if playing local media
  54. //
  55. if (($item == "playlist") && ($play_status == "local_media")) {
  56.         $playlist = getStatus("status",$host,$port);
  57.         $playlist = stripColon($playlist['11']);
  58.         echo $playlist;
  59.         if (empty($playlist)) {
  60.         echo "No playlist information returned\n";
  61.         die();
  62.         }
  63. }
  64.  
  65. function stripColon($opt) {
  66.         if (!empty($opt)) {
  67.         $state=explode(":",$opt);
  68.         $opt=$state['1']; // get 2nd item in array $state
  69.         $opt = trim($opt);
  70.         } else {
  71.         $opt = "Playlist state is unknown\n";
  72.         die();
  73.         }
  74.         return $opt;
  75. }
  76.  
  77. function getStatus($opt,$host,$port) {
  78. $mpd_sock = fsockopen($host, $port, $errno, $errstr, 5);
  79. $mpd_data="";
  80.  
  81. if (!$mpd_sock) {
  82.         echo "$errstr ($errno)<br>";
  83.         } else {
  84.         fwrite($mpd_sock, $opt . "\n");
  85.         usleep(250000);
  86.         fwrite($mpd_sock, "\n");
  87. //      print_r($mpd_data); // debugging
  88.  
  89.         while (!feof($mpd_sock)) {
  90.                 $mpd_data .= fgets($mpd_sock);
  91.                 }
  92.                 $mpd_status = explode("\n", $mpd_data);
  93.                 fwrite($mpd_sock, "\n");
  94.                 return $mpd_status;
  95.         }
  96.         fclose($mpd_sock);
  97. }
  98.  
  99. function check_input($data) {
  100.         $data = trim($data);
  101.         $data = strip_tags($data);
  102.         $data = stripslashes($data);
  103.         $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
  104.         $data = filter_var($data, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  105.         return $data;
  106. }
  107. ?>
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement