greyhound.php
author Dan
Mon, 31 Mar 2008 07:40:30 -0400
changeset 14 7a1573676cc4
parent 13 b5db2345c397
child 15 2adca0f363fd
permissions -rwxr-xr-x
Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     1
#!/usr/bin/env php
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
<?php
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
/**
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     5
 * 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
     6
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     7
 *
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
     8
 * 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
     9
 * 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
    10
 *
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
 * 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
    12
 * 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
    13
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    15
// Try to trap termination signals to cleanly close the socket when needed
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    16
// AmaroK sends a SIGTERM when it is shut down or the user requests to stop
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    17
// the script
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    18
if ( function_exists('pcntl_signal') )
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    19
{
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    20
  // required for signal handling to work
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    21
  declare(ticks=1);
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    22
  
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    23
  // trap SIGTERM
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    24
  pcntl_signal(SIGTERM, 'sigterm');
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    25
  pcntl_signal(SIGINT,  'sigterm');
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 8
diff changeset
    26
}
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    27
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    28
$public = true;
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    29
$allowcontrol = true;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    30
$theme = 'funkymonkey';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    31
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    32
@ini_set('display_errors', 'on');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    33
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    34
// include files
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    35
require('functions.php');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    36
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    37
// get the root
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    38
define('GREY_ROOT', dirname(__FILE__));
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    39
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    40
// create directories
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    41
@mkdir('./compiled');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    42
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    43
// start up...
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    44
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    45
status('Starting WebControl v0.1-hg');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    46
status('loading files');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    47
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    48
require('webserver.php');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    49
define('SMARTY_DIR', GREY_ROOT . '/smarty/');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    50
require(GREY_ROOT . '/smarty/Smarty.class.php');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    51
require(GREY_ROOT . '/playlist.php');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    52
require(GREY_ROOT . '/json.php');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    53
require(GREY_ROOT . '/ajax.php');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    54
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
status('doing home directory detection');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    56
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    57
// get home directory
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    58
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    59
if ( !isset($_ENV['HOME']) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    60
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    61
  burnout('Could not get your home directory');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    62
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    63
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    64
$homedir =& $_ENV['HOME'];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    65
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    66
// signal handler
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    67
function sigterm($signal)
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    68
{
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    69
  global $httpd;
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    70
  status("Caught SIGTERM, cleaning up.");
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    71
  @socket_close($httpd->sock);
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    72
  exit();
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    73
}
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    74
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    75
status('initializing playlist');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    76
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    77
// init playlist object
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    78
$playlist = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    79
rebuild_playlist();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    80
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    81
// startup webserver
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    82
$ip = ( $public ) ? '0.0.0.0' : '127.0.0.1';
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
    83
$port = 7447;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    84
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    85
try
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    86
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    87
  status('starting PhpHttpd');
13
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
    88
  status('doing PHP capabilities check');
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
    89
  if ( !function_exists('pcntl_signal') )
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
    90
  {
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
    91
    warning('System does not support POSIX functions. Termination signals will result in unclean shutdown.');
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
    92
  }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    93
  $httpd = new WebServer($ip, $port);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    94
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    95
  // setup handlers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    96
  status('initializing handlers');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    97
  $httpd->add_handler('index',                'function', 'amarok_playlist');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    98
  $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    99
  $httpd->add_handler('scripts',              'dir',      GREY_ROOT . '/scripts');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
   100
  $httpd->add_handler('favicon.ico',          'file',     GREY_ROOT . '/amarok_icon.ico');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
   101
  $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   102
  $httpd->allow_dir_list = true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   103
  $httpd->default_document = 'index';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   104
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   105
  status("Entering main server loop - ^C to interrupt, listening on port $port");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   106
  $httpd->serve();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   107
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   108
catch( Exception $e )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   109
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   110
  burnout("Exception caught while running webserver:\n$e");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   111
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   112