greyhound.php
author Dan
Wed, 26 Mar 2008 11:10:20 -0400
changeset 13 b5db2345c397
parent 12 b3fcc21e557f
child 15 2adca0f363fd
permissions -rwxr-xr-x
Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations

#!/usr/bin/env php
<?php

/**
 * 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.
 */

// Try to trap termination signals to cleanly close the socket when needed
// AmaroK sends a SIGTERM when it is shut down or the user requests to stop
// the script
if ( function_exists('pcntl_signal') )
{
  // required for signal handling to work
  declare(ticks=1);
  
  // trap SIGTERM
  pcntl_signal(SIGTERM, 'sigterm');
  pcntl_signal(SIGINT,  'sigterm');
}

$public = true;
$allowcontrol = true;
$theme = 'funkymonkey';

@ini_set('display_errors', 'on');

// include files
require('functions.php');

// get the root
define('GREY_ROOT', dirname(__FILE__));

// create directories
@mkdir('./compiled');

// start up...

status('Starting WebControl v0.1-hg');
status('loading files');

require('webserver.php');
define('SMARTY_DIR', GREY_ROOT . '/smarty/');
require(GREY_ROOT . '/smarty/Smarty.class.php');
require(GREY_ROOT . '/playlist.php');
require(GREY_ROOT . '/json.php');
require(GREY_ROOT . '/ajax.php');

status('doing home directory detection');

// get home directory

if ( !isset($_ENV['HOME']) )
{
  burnout('Could not get your home directory');
}

$homedir =& $_ENV['HOME'];

// signal handler
function sigterm($signal)
{
  global $httpd;
  status("Caught SIGTERM, cleaning up.");
  @socket_close($httpd->sock);
  exit();
}

status('initializing playlist');

// init playlist object
$playlist = array();
rebuild_playlist();

// startup webserver
$ip = ( $public ) ? '0.0.0.0' : '127.0.0.1';
$port = 7447;

try
{
  status('starting PhpHttpd');
  status('doing PHP capabilities check');
  if ( !function_exists('pcntl_signal') )
  {
    warning('System does not support POSIX functions. Termination signals will result in unclean shutdown.');
  }
  $httpd = new WebServer($ip, $port);
  
  // setup handlers
  status('initializing handlers');
  $httpd->add_handler('index',                'function', 'amarok_playlist');
  $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
  $httpd->add_handler('scripts',              'dir',      GREY_ROOT . '/scripts');
  $httpd->add_handler('favicon.ico',          'file',     GREY_ROOT . '/amarok_icon.ico');
  $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
  $httpd->allow_dir_list = true;
  $httpd->default_document = 'index';
  
  status("Entering main server loop - ^C to interrupt, listening on port $port");
  $httpd->serve();
}
catch( Exception $e )
{
  burnout("Exception caught while running webserver:\n$e");
}