playlist.php
author Dan
Tue, 05 Aug 2008 13:17:57 -0400
changeset 27 20a36fe254c9
parent 25 5c377ceb0e4c
child 33 3b4aef1efff6
permissions -rw-r--r--
Merging artwork (nighthawk) + keepalive (scribus) branches
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
 * Playlist displayer
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: 4
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: 4
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: 4
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: 4
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: 4
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: 4
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
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: 10
diff changeset
    16
function amarok_playlist($httpd, $socket)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    17
{
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    18
  global $theme, $playlist, $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: 10
diff changeset
    19
  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: 10
diff changeset
    20
  
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: 10
diff changeset
    21
  if ( $use_auth )
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: 10
diff changeset
    22
  {
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: 10
diff changeset
    23
    if ( !isset($_SERVER['PHP_AUTH_USER']) )
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: 10
diff changeset
    24
    {
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: 10
diff changeset
    25
      $httpd->header('WWW-Authenticate: basic');
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: 10
diff changeset
    26
      $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.");
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: 10
diff changeset
    27
      return true;
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: 10
diff changeset
    28
    }
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: 10
diff changeset
    29
    if ( !isset($auth_data[$_SERVER['PHP_AUTH_USER']]) )
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: 10
diff changeset
    30
    {
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: 10
diff changeset
    31
      $httpd->header('WWW-Authenticate: basic');
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: 10
diff changeset
    32
      $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.");
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: 10
diff changeset
    33
      return true;
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: 10
diff changeset
    34
    }
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: 10
diff changeset
    35
    else if ( $auth_data[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'] )
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: 10
diff changeset
    36
    {
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: 10
diff changeset
    37
      $httpd->header('WWW-Authenticate: basic');
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: 10
diff changeset
    38
      $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.");
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: 10
diff changeset
    39
      return true;
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: 10
diff changeset
    40
    }
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: 10
diff changeset
    41
  }
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    42
  
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
    43
  $iphone = ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    44
       strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') ||
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    45
       strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') ||
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
    46
       isset($_GET['m']) )
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
    47
       && !isset($_GET['f'])
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    48
       );
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    49
  $theme_id = ( $iphone ) ? 'iphone' : $theme;
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    50
  $smarty = load_theme($theme_id);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    51
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    52
  $active = dcop_action('playlist', 'getActiveIndex');
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    53
  $smarty->assign('theme', $theme_id);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    54
  $smarty->assign('playlist', $playlist);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
  $smarty->assign('active', $active);
1
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    56
  $smarty->assign('scripts', array(
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    57
      'ajax.js',
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 1
diff changeset
    58
      'domutils.js',
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
    59
      'volume.js',
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
    60
      'dom-drag.js',
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
    61
      'position.js'
1
cddc2ba706d6 Preliminary groundwork for AJAX interface
Dan
parents: 0
diff changeset
    62
    ));
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
    63
  $smarty->assign('allow_control', $allowcontrol);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    64
  $smarty->display('playlist.tpl');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    65
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    66
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    67
function artwork_request_handler($httpd, $socket)
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    68
{
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    69
  global $homedir;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    70
  
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    71
  if ( !isset($_GET['artist']) || !isset($_GET['album']) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    72
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    73
    echo 'Please specify artist and album.';
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    74
    return;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    75
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    76
  // get hash
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    77
  $artwork_hash = md5( strtolower(trim($_GET['artist'])) . strtolower(trim($_GET['album'])) );
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    78
  $artwork_dir = "$homedir/.kde/share/apps/amarok/albumcovers";
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    79
  if ( file_exists("$artwork_dir/large/$artwork_hash") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    80
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    81
    // artwork file found - scale and convert to PNG
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    82
    if ( !is_dir("$artwork_dir/greyhoundthumbnails") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    83
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    84
      if ( !@mkdir("$artwork_dir/greyhoundthumbnails") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    85
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    86
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    87
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    88
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    89
    // check for the scaled cover image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    90
    $target_file = "$artwork_dir/greyhoundthumbnails/$artwork_hash.png";
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    91
    if ( !file_exists($target_file) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    92
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    93
      // not scaled yet, scale to uniform 50x50 image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    94
      $artwork_filetype = get_image_filetype("$artwork_dir/large/$artwork_hash");
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    95
      if ( !$artwork_filetype )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    96
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    97
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    98
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    99
      // we'll need to copy the existing artwork file to our thumbnail dir to let scale_image() detect the type properly (it doesn't use magic bytes)
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   100
      if ( !copy("$artwork_dir/large/$artwork_hash", "$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   101
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   102
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   103
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   104
      // finally, scale the image
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   105
      if ( !scale_image("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype", $target_file, 50, 50) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   106
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   107
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   108
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   109
      // delete our temp file
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   110
      if ( !unlink("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   111
      {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   112
        echo 'Couldn\'t delete the temp file';
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   113
        return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   114
      }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   115
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   116
    // we have it now, send the image through
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   117
    $fh = @fopen($target_file, 'r');
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   118
    if ( !$fh )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   119
      return false;
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   120
    $httpd->header('Content-type: image/png');
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   121
    $httpd->header('Content-length: ' . filesize($target_file));
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   122
    $httpd->header('Expires: Wed, 1 Jan 2020 01:00:00 GMT');
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   123
    while ( !feof($fh) )
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   124
    {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   125
      socket_write($socket, fread($fh, 51200));
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   126
    }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   127
    fclose($fh);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   128
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   129
  else
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   130
  {
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   131
    // artwork file doesn't exist
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   132
    $ar = htmlspecialchars($_GET['artist']);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   133
    $al = htmlspecialchars($_GET['album']);
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   134
    $httpd->send_http_error($socket, 404, "The requested artwork file for $ar:$al could not be found on this server.");
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   135
  }
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   136
}
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   137
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   138