greyhound.php
author Dan
Fri, 15 Aug 2008 23:31:37 -0400
changeset 33 3b4aef1efff6
parent 32 86140ed9087d
child 34 3b817b961984
permissions -rwxr-xr-x
Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
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');
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
    26
  pcntl_signal(SIGUSR1, 'handle_refresh_signal');
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
    27
}
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    28
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    29
//
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    30
// CONFIGURATION
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    31
//
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    32
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    33
// Listen on all interfaces. If this is false, it will only listen on
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    34
// 127.0.0.1 (the loopback interface)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    35
$public = true;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    36
// Allow control of playback. By default this is turned on but if you
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    37
// set this to false, it will only display the playlist.
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 3
diff changeset
    38
$allowcontrol = true;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    39
// The default theme. This should be a name of a directory in ./themes.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    40
$theme = 'funkymonkey';
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    41
// Allow forking when an HTTP request is received. This has advantages
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    42
// and disadvantages. If this experimental option is enabled, it will
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
    43
// result in faster responses and load times but more memory usage.
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    44
$allow_fork = true;
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: 19
diff changeset
    45
// set to true to enable authentication
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: 19
diff changeset
    46
// WARNING: THIS HAS SOME SERIOUS SECURITY PROBLEMS RIGHT NOW. I don't
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: 19
diff changeset
    47
// know what's causing it to not prompt for authentication from any
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: 19
diff changeset
    48
// client after the first successful 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: 19
diff changeset
    49
$use_auth = false;
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: 19
diff changeset
    50
// valid users and passwords
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: 19
diff changeset
    51
$auth_data = array(
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: 19
diff changeset
    52
    'funky' => 'monkey',
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: 19
diff changeset
    53
    'fast' => 'forward'
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: 19
diff changeset
    54
  );
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    56
@ini_set('display_errors', 'on');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    57
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    58
// include files
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    59
require('functions.php');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    60
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    61
// get the root
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    62
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
    63
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    64
// create directories
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    65
@mkdir('./compiled');
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    66
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    67
// start up...
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    68
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
    69
status('Starting Greyhound Web Control v0.1a3');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    70
status('loading files');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    71
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    72
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
    73
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
    74
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
    75
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
    76
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
    77
require(GREY_ROOT . '/ajax.php');
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
    78
require(GREY_ROOT . '/imagetools.php');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    79
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    80
// signal handler
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    81
function sigterm($signal)
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    82
{
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    83
  global $httpd;
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    84
  status("Caught SIGTERM, cleaning up.");
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
    85
  exit(0);
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    86
}
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    87
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    88
status('initializing playlist');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    89
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    90
// init playlist object
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    91
$playlist = array();
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
    92
$amarok_home = false;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    93
rebuild_playlist();
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
// startup webserver
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    96
$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
    97
$port = 7447;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    98
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    99
try
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   100
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   101
  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
   102
  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
   103
  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
   104
  {
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   105
    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
   106
  }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   107
  $httpd = new WebServer($ip, $port);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   108
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   109
  // setup handlers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   110
  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
   111
  $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
   112
  $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   113
  $httpd->add_handler('artwork',              'function', 'artwork_request_handler');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
   114
  $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
   115
  $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
   116
  $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   117
  // load all themes if forking is enabled
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   118
  // Themes are loaded when the playlist is requested. This is fine for
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   119
  // single-threaded operation, but if the playlist handler is only loaded
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   120
  // in a child process, we need to preload all themes into the parent before
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   121
  // children can respond to theme resource requests.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   122
  if ( $allow_fork )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   123
  {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   124
    status('Preloading themes');
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   125
    
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   126
    $dh = @opendir(GREY_ROOT . '/themes');
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   127
    if ( !$dh )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   128
      burnout('Could not open themes directory');
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   129
    while ( $dir = @readdir($dh) )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   130
    {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   131
      if ( $dir == '.' || $dir == '..' )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   132
        continue;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   133
      if ( is_dir( GREY_ROOT . "/themes/$dir" ) )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   134
        load_theme($dir);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   135
    }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   136
  }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   137
  $httpd->allow_dir_list = true;
17
66c3eb4cdc4c Re-enabled multi-threaded operation after trying with a field test
Dan
parents: 16
diff changeset
   138
  $httpd->allow_fork = ( $allow_fork ) ? true : false;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   139
  $httpd->default_document = 'index';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   140
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   141
  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
   142
  $httpd->serve();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   143
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   144
catch( Exception $e )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   145
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   146
  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
   147
}
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   148
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   149
function handle_refresh_signal()
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   150
{
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   151
  global $httpd;
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   152
  if ( !is_object($httpd) )
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   153
    // we're not serving yet.
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   154
    return false;
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   155
    
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   156
  // we've got an httpd instance; rebuild the playlist
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   157
  rebuild_playlist();
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   158
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   159
  // if this is the parent, also ask the children to rebuild.
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   160
  if ( !defined('HTTPD_WS_CHILD') )
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   161
  {
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   162
    foreach ( $httpd->child_list as $pid )
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   163
    {
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   164
      posix_kill($pid, SIGUSR1);
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   165
    }
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   166
  }
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 25
diff changeset
   167
}