ajax.php
author Dan
Tue, 23 Dec 2008 20:37:36 -0500
changeset 66 f30ffbf92cdd
parent 55 b8a3c9c54fbe
permissions -rw-r--r--
Release: 0.1 alpha 4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     1
<?php
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     3
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     4
 * Action servlet (play, pause, etc.)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     5
 *
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     6
 * Greyhound - real web management for Amarok
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
    11
 *
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    15
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    16
status('initializing Services_JSON');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    17
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    18
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    19
// keep track of playlist refresh
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    20
$playlist_last_refresh = time();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    21
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    22
/**
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    23
 * Terminate a request with an error string formatted as JSON.
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    24
 * @param string Error message
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    25
 */
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    26
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    27
function json_die($msg)
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    28
{
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    29
  global $json;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    30
  echo $json->encode(array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    31
      'mode' => 'error',
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    32
      'error' => $msg
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    33
    ));
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    34
  return true;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    35
}
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    36
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
    37
function ajax_request_handler($httpd, $socket)
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    38
{
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    39
  global $playlist, $mime_types, $json, $allowcontrol;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
    40
  global $use_auth, $auth_data;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
    41
  
44
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 21
diff changeset
    42
  if ( !session_check() )
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 21
diff changeset
    43
    return true;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    44
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    45
  // Set content type
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    46
  $httpd->header("Content-type: {$mime_types['js']}");
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    47
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    48
  // get PATH_INFO
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    49
  $pathinfo = @substr(@substr($_SERVER['REQUEST_URI'], 1), @strpos(@substr($_SERVER['REQUEST_URI'], 1), '/')+1);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    50
  if ( empty($pathinfo) )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    51
  {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    52
    return json_die('No action specified on URI');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    53
  }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    54
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    55
  $params = explode('/', $pathinfo);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    56
  $action =& $params[0];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    57
  switch ( $action )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    58
  {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    59
    case 'stop':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    60
    case 'next':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    61
    case 'prev':
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    62
      if ( !$allowcontrol )
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    63
        return false;
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 3
diff changeset
    64
      echo dcop_action('player', $action);
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    65
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    66
    case 'play':
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    67
      if ( !$allowcontrol )
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    68
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    69
      echo dcop_action('player', 'playPause');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    70
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    71
    case 'jump':
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    72
      if ( !$allowcontrol )
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    73
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    74
      $tid =& $params[1];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    75
      if ( !preg_match('/^[0-9]+$/', $tid) )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    76
      {
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    77
        return json_die('Invalid track ID');
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    78
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    79
      $tid = intval($tid);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    80
      dcop_action('playlist', "playByIndex $tid");
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    81
      $return = array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    82
        'current_track_length' => $playlist[$tid]['length_int'],
18
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
    83
        'current_track_pos' => 0,
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
    84
        'current_track_title' => $playlist[$tid]['title'],
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
    85
        'current_track_album' => $playlist[$tid]['album'],
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
    86
        'current_track_artist' => $playlist[$tid]['artist']
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    87
      );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    88
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    89
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    90
    case 'volume':
3
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    91
      if ( !$allowcontrol )
e7447a6044ec $allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents: 2
diff changeset
    92
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    93
      $volume =& $params[1];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    94
      if ( !preg_match('/^[0-9]+$/', $volume) )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    95
      {
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
    96
        return json_die('Invalid track ID');
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    97
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    98
      $volume = intval($volume);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    99
      dcop_action('player', "setVolume $volume");
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   100
      $return = array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   101
        'volume' => $volume
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   102
        );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   103
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   104
      break;
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   105
    case 'seek':
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   106
      if ( !$allowcontrol )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   107
        return false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   108
      $pos =& $params[1];
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   109
      if ( !preg_match('/^[0-9]+$/', $pos) )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   110
      {
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   111
        return json_die('Invalid track ID');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   112
      }
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   113
      $pos = intval($pos);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   114
      dcop_action('player', "seek $pos");
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   115
      
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
   116
      break;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   117
    case 'refresh':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   118
      global $playlist_last_refresh, $playlist, $playlist_last_md5;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   119
      if ( $playlist_last_refresh + 60 < time() )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   120
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   121
        rebuild_playlist();
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   122
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   123
      $current_track = dcop_action('playlist', 'getActiveIndex');
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
   124
      $current_time = dcop_action('player', 'trackCurrentTime');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
   125
      $is_playing = dcop_action('player', 'isPlaying');
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   126
      $return = array(
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
   127
          'is_playing' => $is_playing,
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
   128
          'is_paused' => $current_time > 0 && !$is_playing,
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   129
          'current_track' => $current_track,
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   130
          'volume' => dcop_action('player', 'getVolume'),
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   131
          // include the MD5 of the playlist so that if it changes, the
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   132
          // client can refresh (otherwise things get madly corrupted)
18
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   133
          'playlist_hash' => $playlist_last_md5,
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   134
          'current_track_title' => false,
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   135
          'current_track_album' => false,
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   136
          'current_track_artist' => false
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   137
        );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   138
      if ( isset($playlist[$current_track]) )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   139
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   140
        $return['current_track_length'] = $playlist[$current_track]['length_int'];
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 18
diff changeset
   141
        $return['current_track_pos'] = $current_time;
18
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   142
        $return['current_track_title'] = $playlist[$current_track]['title'];
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   143
        $return['current_track_artist'] = $playlist[$current_track]['artist'];
69af47034212 Added page-titling functionality that changes document.title to match the current track; made position slider reset to zero on stop or playlist end
Dan
parents: 10
diff changeset
   144
        $return['current_track_album'] = $playlist[$current_track]['album'];
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   145
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   146
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   147
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   148
    default:
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   149
      return json_die("Undefined action: $action");
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   150
  }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   151
}
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   152
55
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   153
function api_request_handler($httpd, $socket)
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   154
{
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   155
  global $json;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   156
  $httpd->header('Content-type: text/javascript');
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   157
  if ( !isset($_POST['request']) )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   158
  {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   159
    return json_die("No request specified on POST.");
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   160
  }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   161
  $req = $json->decode($_POST['request']);
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   162
  if ( !isset($req['action']) )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   163
  {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   164
    return json_die("No action specified.");
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   165
  }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   166
  switch($req['action'])
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   167
  {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   168
    case 'check_login':
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   169
      global $use_auth, $auth_data;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   170
      if ( $use_auth )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   171
      {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   172
        $return = array(
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   173
          'need_login' => true
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   174
        );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   175
        if ( isset($req['username']) && isset($req['password']) )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   176
        {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   177
          $return['auth_valid'] = ( isset($auth_data[$req['username']]) && $auth_data[$req['username']] === $req['password'] );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   178
        }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   179
      }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   180
      else
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   181
      {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   182
        $return = array(
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   183
          'need_login' => false,
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   184
          'auth_valid' => true
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   185
        );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   186
      }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   187
      break;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   188
    case 'login':
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   189
      global $use_auth, $auth_data;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   190
      if ( $use_auth )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   191
      {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   192
        if ( !isset($req['username']) || !isset($req['password']) )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   193
        {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   194
          return json_die("Username or password not provided");
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   195
        }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   196
        if ( $session = login($req['username'], $req['password']) )
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   197
        {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   198
          $return = array(
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   199
            'need_login' => true,
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   200
            'login_cookie' => $session
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   201
          );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   202
        }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   203
        else
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   204
        {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   205
          $return = array(
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   206
            'need_login' => true,
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   207
            'login_cookie' => false
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   208
          );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   209
        }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   210
      }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   211
      else
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   212
      {
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   213
        $return = array(
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   214
          'need_login' => false,
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   215
          'login_cookie' => false
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   216
        );
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   217
      }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   218
      break;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   219
    default:
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   220
      return json_die("Undefined action '{$req['action']}'.");
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   221
      break;
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   222
  }
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   223
  echo $json->encode($return);
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 44
diff changeset
   224
}