ajax.php
changeset 21 74edc873234f
parent 18 69af47034212
child 44 92dd253f501c
equal deleted inserted replaced
20:bb8237ca678d 21:74edc873234f
    32       'error' => $msg
    32       'error' => $msg
    33     ));
    33     ));
    34   return true;
    34   return true;
    35 }
    35 }
    36 
    36 
    37 function ajax_request_handler($httpd)
    37 function ajax_request_handler($httpd, $socket)
    38 {
    38 {
    39   global $playlist, $mime_types, $json, $allowcontrol;
    39   global $playlist, $mime_types, $json, $allowcontrol;
       
    40   global $use_auth, $auth_data;
       
    41   
       
    42   if ( $use_auth )
       
    43   {
       
    44     if ( !isset($_SERVER['PHP_AUTH_USER']) )
       
    45     {
       
    46       $httpd->header('WWW-Authenticate: basic');
       
    47       $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
       
    48       return true;
       
    49     }
       
    50     if ( !isset($auth_data[$_SERVER['PHP_AUTH_USER']]) )
       
    51     {
       
    52       $httpd->header('WWW-Authenticate: basic');
       
    53       $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
       
    54       return true;
       
    55     }
       
    56     else if ( $_SERVER['PHP_AUTH_PW'] !== $auth_data[$_SERVER['PHP_AUTH_USER']] )
       
    57     {
       
    58       $httpd->header('WWW-Authenticate: basic');
       
    59       $httpd->send_http_error($socket, 401, "A username and password are required to access this resource. Either you did not specify a username and password, or the supplied credentials were incorrect.");
       
    60       return true;
       
    61     }
       
    62   }
    40   
    63   
    41   // Set content type
    64   // Set content type
    42   $httpd->header("Content-type: {$mime_types['js']}");
    65   $httpd->header("Content-type: {$mime_types['js']}");
    43   
    66   
    44   // get PATH_INFO
    67   // get PATH_INFO
   115       if ( $playlist_last_refresh + 60 < time() )
   138       if ( $playlist_last_refresh + 60 < time() )
   116       {
   139       {
   117         rebuild_playlist();
   140         rebuild_playlist();
   118       }
   141       }
   119       $current_track = dcop_action('playlist', 'getActiveIndex');
   142       $current_track = dcop_action('playlist', 'getActiveIndex');
       
   143       $current_time = dcop_action('player', 'trackCurrentTime');
       
   144       $is_playing = dcop_action('player', 'isPlaying');
   120       $return = array(
   145       $return = array(
   121           'is_playing' => dcop_action('player', 'isPlaying'),
   146           'is_playing' => $is_playing,
       
   147           'is_paused' => $current_time > 0 && !$is_playing,
   122           'current_track' => $current_track,
   148           'current_track' => $current_track,
   123           'volume' => dcop_action('player', 'getVolume'),
   149           'volume' => dcop_action('player', 'getVolume'),
   124           // include the MD5 of the playlist so that if it changes, the
   150           // include the MD5 of the playlist so that if it changes, the
   125           // client can refresh (otherwise things get madly corrupted)
   151           // client can refresh (otherwise things get madly corrupted)
   126           'playlist_hash' => $playlist_last_md5,
   152           'playlist_hash' => $playlist_last_md5,
   129           'current_track_artist' => false
   155           'current_track_artist' => false
   130         );
   156         );
   131       if ( isset($playlist[$current_track]) )
   157       if ( isset($playlist[$current_track]) )
   132       {
   158       {
   133         $return['current_track_length'] = $playlist[$current_track]['length_int'];
   159         $return['current_track_length'] = $playlist[$current_track]['length_int'];
   134         $return['current_track_pos'] = dcop_action('player', 'trackCurrentTime');
   160         $return['current_track_pos'] = $current_time;
   135         $return['current_track_title'] = $playlist[$current_track]['title'];
   161         $return['current_track_title'] = $playlist[$current_track]['title'];
   136         $return['current_track_artist'] = $playlist[$current_track]['artist'];
   162         $return['current_track_artist'] = $playlist[$current_track]['artist'];
   137         $return['current_track_album'] = $playlist[$current_track]['album'];
   163         $return['current_track_album'] = $playlist[$current_track]['album'];
   138       }
   164       }
   139       echo $json->encode($return);
   165       echo $json->encode($return);