ajax.php
author Dan
Sun, 23 Mar 2008 22:03:57 -0400
changeset 4 cde92f6ec29f
parent 3 e7447a6044ec
child 5 9b96265b5918
permissions -rw-r--r--
Should be completely iPhone/iPod Touch-friendly now :)
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
 *
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     6
 * Web control interface script for Amarok
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     7
 * Written by Dan Fuhry - 2008
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     9
 * This script is in the public domain. Use it for good, not evil.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    10
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    11
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    12
status('initializing Services_JSON');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    13
$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
    14
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    15
// 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
    16
$playlist_last_refresh = time();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    17
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    18
/**
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    19
 * 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
    20
 * @param string Error message
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    21
 */
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
function json_die($msg)
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    24
{
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    25
  global $json;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    26
  echo $json->encode(array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    27
      'mode' => 'error',
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    28
      'error' => $msg
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    29
    ));
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    30
  return true;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    31
}
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    32
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    33
function ajax_request_handler($httpd)
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    34
{
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
    35
  global $playlist, $mime_types, $json, $allowcontrol;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    36
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    37
  // Set content type
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    38
  $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
    39
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    40
  // get PATH_INFO
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    41
  $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
    42
  if ( empty($pathinfo) )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    43
  {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    44
    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
    45
  }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    46
  
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    47
  $params = explode('/', $pathinfo);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    48
  $action =& $params[0];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    49
  switch ( $action )
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    50
  {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    51
    case 'stop':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    52
    case 'next':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    53
    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
    54
      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
    55
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    56
      echo dcop_action('player', 'stop');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    57
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    58
    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
    59
      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
    60
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    61
      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
    62
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    63
    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
    64
      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
    65
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    66
      $tid =& $params[1];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    67
      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
    68
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    69
        return die_json('Invalid track ID');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    70
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    71
      $tid = intval($tid);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    72
      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
    73
      $return = array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    74
        'current_track_length' => $playlist[$tid]['length_int'],
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    75
        'current_track_pos' => 0
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    76
      );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    77
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    78
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    79
    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
    80
      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
    81
        return false;
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    82
      $volume =& $params[1];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    83
      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
    84
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    85
        return die_json('Invalid track ID');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    86
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    87
      $volume = intval($volume);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    88
      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
    89
      $return = array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    90
        'volume' => $volume
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    91
        );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    92
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    93
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    94
    case 'refresh':
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    95
      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
    96
      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
    97
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    98
        rebuild_playlist();
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
    99
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   100
      $current_track = dcop_action('playlist', 'getActiveIndex');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   101
      $return = array(
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   102
          'is_playing' => dcop_action('player', 'isPlaying'),
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   103
          'current_track' => $current_track,
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   104
          '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
   105
          // 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
   106
          // client can refresh (otherwise things get madly corrupted)
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   107
          'playlist_hash' => $playlist_last_md5
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   108
        );
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   109
      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
   110
      {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   111
        $return['current_track_length'] = $playlist[$current_track]['length_int'];
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   112
        $return['current_track_pos'] = dcop_action('player', 'trackCurrentTime');
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   113
      }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   114
      echo $json->encode($return);
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   115
      break;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   116
    default:
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   117
      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
   118
  }
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   119
}
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   120