greyhound.php
author Dan
Sun, 23 Mar 2008 23:55:50 -0400
changeset 8 a8d108f37363
parent 7 3ba432f8390b
child 12 b3fcc21e557f
permissions -rwxr-xr-x
Modified to trap signals properly and not write to the root directory (only working directory)

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

// 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');
  $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");
}