playlist.php
author Dan
Mon, 01 Sep 2008 13:05:52 -0400
changeset 39 38dbcda3cf20
parent 33 3b4aef1efff6
child 40 bd3372a2afc1
permissions -rw-r--r--
Fixed scroll-to-current-track being about 30 pixels off under iPhone

<?php

/**
 * Playlist displayer
 *
 * Greyhound - real web management for Amarok
 * Copyright (C) 2008 Dan Fuhry
 *
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
 */

function amarok_playlist($httpd, $socket)
{
  global $theme, $playlist, $allowcontrol;
  global $use_auth, $auth_data;
  
  if ( $use_auth )
  {
    if ( !isset($_SERVER['PHP_AUTH_USER']) )
    {
      $httpd->header('WWW-Authenticate: basic');
      $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.");
      return true;
    }
    if ( !isset($auth_data[$_SERVER['PHP_AUTH_USER']]) )
    {
      $httpd->header('WWW-Authenticate: basic');
      $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.");
      return true;
    }
    else if ( $auth_data[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'] )
    {
      $httpd->header('WWW-Authenticate: basic');
      $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.");
      return true;
    }
  }
  
  $iphone = ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
       strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') ||
       strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') ||
       isset($_GET['m']) )
       && !isset($_GET['f'])
       );
  $theme_id = ( $iphone ) ? 'iphone' : $theme;
  $smarty = load_theme($theme_id);
  
  $active = dcop_action('playlist', 'getActiveIndex');
  $smarty->assign('theme', $theme_id);
  $smarty->assign('playlist', $playlist);
  $smarty->assign('active', $active);
  $smarty->assign('scripts', array(
      'ajax.js',
      'domutils.js',
      'volume.js',
      'dom-drag.js',
      'position.js'
    ));
  $smarty->assign('allow_control', $allowcontrol);
  $smarty->display('playlist.tpl');
}

function artwork_request_handler($httpd, $socket)
{
  global $amarok_home;
  
  if ( !isset($_GET['artist']) || !isset($_GET['album']) )
  {
    echo 'Please specify artist and album.';
    return;
  }
  // get hash
  $artwork_hash = md5( strtolower(trim($_GET['artist'])) . strtolower(trim($_GET['album'])) );
  $artwork_dir = "$amarok_home/albumcovers";
  if ( file_exists("$artwork_dir/large/$artwork_hash") )
  {
    // artwork file found - scale and convert to PNG
    if ( !is_dir("$artwork_dir/greyhoundthumbnails") )
    {
      if ( !@mkdir("$artwork_dir/greyhoundthumbnails") )
      {
        return false;
      }
    }
    // check for the scaled cover image
    $target_file = "$artwork_dir/greyhoundthumbnails/$artwork_hash.png";
    if ( !file_exists($target_file) )
    {
      // not scaled yet, scale to uniform 50x50 image
      $artwork_filetype = get_image_filetype("$artwork_dir/large/$artwork_hash");
      if ( !$artwork_filetype )
      {
        return false;
      }
      // 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)
      if ( !copy("$artwork_dir/large/$artwork_hash", "$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
      {
        return false;
      }
      // finally, scale the image
      if ( !scale_image("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype", $target_file, 50, 50) )
      {
        return false;
      }
      // delete our temp file
      if ( !unlink("$artwork_dir/greyhoundthumbnails/tmp{$artwork_hash}.$artwork_filetype") )
      {
        echo 'Couldn\'t delete the temp file';
        return false;
      }
    }
    // we have it now, send the image through
    $fh = @fopen($target_file, 'r');
    if ( !$fh )
      return false;
    $httpd->header('Content-type: image/png');
    $httpd->header('Content-length: ' . filesize($target_file));
    $httpd->header('Expires: Wed, 1 Jan 2020 01:00:00 GMT');
    while ( !feof($fh) )
    {
      socket_write($socket, fread($fh, 51200));
    }
    fclose($fh);
  }
  else
  {
    // artwork file doesn't exist
    $ar = htmlspecialchars($_GET['artist']);
    $al = htmlspecialchars($_GET['album']);
    $httpd->send_http_error($socket, 404, "The requested artwork file for $ar:$al could not be found on this server.");
  }
}