Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // this script is used in conjunction with the javascript found here --> https://pastebin.com/r534eH9w
- //
- // mpd server host & port
- //
- $host="<mpd server name or IP address>";
- $port="6600";
- // determine player state (play, stopped)
- $playerStatus = getStatus("status",$host,$port);
- $state = stripColon($playerStatus['9']);
- if ($state == "stop") {
- echo "0";
- exit(0);
- }
- // get parameter from URL not actually required with the latest version and
- // could be preset as only a single parameter (playlist) is required - assigned to $item below
- //
- $item = check_input($_GET['id']);
- //
- // determine if playing radio or local media
- //
- $type = getStatus("currentsong",$host,$port);
- $file_type = stripColon($type['1']);
- if (empty($file_type)) {
- echo "No current song information returned\n";
- die();
- }
- if (strpos($file_type, 'http') !==false) {
- // echo "Playing radio";
- $play_status = "radio";
- } else {
- // echo "Playing local media ";
- $play_status = "local_media";
- }
- //
- // get playlist id if playing radio (different array returned for radio and local media)
- //
- if (($item == "playlist") && ($play_status == "radio")) {
- $playlist = getStatus("status",$host,$port);
- $playlist = stripColon($playlist['6']);
- echo $playlist;
- if (empty($playlist)) {
- echo "No playlist information returned\n";
- die();
- }
- }
- //
- // get playlist id if playing local media
- //
- if (($item == "playlist") && ($play_status == "local_media")) {
- $playlist = getStatus("status",$host,$port);
- $playlist = stripColon($playlist['11']);
- echo $playlist;
- if (empty($playlist)) {
- echo "No playlist information returned\n";
- die();
- }
- }
- function stripColon($opt) {
- if (!empty($opt)) {
- $state=explode(":",$opt);
- $opt=$state['1']; // get 2nd item in array $state
- $opt = trim($opt);
- } else {
- $opt = "Playlist state is unknown\n";
- die();
- }
- return $opt;
- }
- function getStatus($opt,$host,$port) {
- $mpd_sock = fsockopen($host, $port, $errno, $errstr, 5);
- $mpd_data="";
- if (!$mpd_sock) {
- echo "$errstr ($errno)<br>";
- } else {
- fwrite($mpd_sock, $opt . "\n");
- usleep(250000);
- fwrite($mpd_sock, "\n");
- // print_r($mpd_data); // debugging
- while (!feof($mpd_sock)) {
- $mpd_data .= fgets($mpd_sock);
- }
- $mpd_status = explode("\n", $mpd_data);
- fwrite($mpd_sock, "\n");
- return $mpd_status;
- }
- fclose($mpd_sock);
- }
- function check_input($data) {
- $data = trim($data);
- $data = strip_tags($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
- $data = filter_var($data, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
- return $data;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement