greyhound.php
author Dan
Fri, 12 Jun 2009 13:50:13 -0400
changeset 78 08f8a72b1f7b
parent 70 efabb54a418d
permissions -rwxr-xr-x
Added Offline Mode - automatically turned on and off based on connectivity to server. Version bumped to 0.1a5.
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
78
08f8a72b1f7b Added Offline Mode - automatically turned on and off based on connectivity to server. Version bumped to 0.1a5.
Dan
parents: 70
diff changeset
    15
define('GREY_VERSION', '0.1a5');
44
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
    16
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
    17
// 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
    18
// 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
    19
// 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
    20
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
    21
{
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
  // 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
    23
  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
    24
  
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
  // 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
    26
  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
    27
  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
    28
}
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    29
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    30
@ini_set('display_errors', 'on');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    31
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    32
// get the root
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    33
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
    34
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33 31
diff changeset
    35
// what kind of terminal do we have?
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 58
diff changeset
    36
$use_colors = ( @in_array(@$_SERVER['TERM'], array('linux', 'xterm', 'vt100', 'screen')) ) ? true : false;
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    37
require(GREY_ROOT . '/functions.php');
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33 31
diff changeset
    38
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    39
// start up...
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    40
44
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
    41
status('Starting Greyhound Web Control v' . GREY_VERSION);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    42
status('loading files');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    43
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    44
require_once(GREY_ROOT . '/webserver.php');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    45
define('SMARTY_DIR', GREY_ROOT . '/smarty/');
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    46
require_once(GREY_ROOT . '/smarty/Smarty.class.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    47
require_once(GREY_ROOT . '/playlist.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    48
require_once(GREY_ROOT . '/json.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    49
require_once(GREY_ROOT . '/ajax.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    50
require_once(GREY_ROOT . '/uiconfig.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    51
require_once(GREY_ROOT . '/imagetools.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    52
require_once(GREY_ROOT . '/sessions.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    53
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    54
//
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    55
// LOAD OUR CONFIG
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    56
// Amarok launches Greyhound with a different wd than Greyhound's root. This means
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    57
// that we can drop our own config (set up from the web UI) in there and load that
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    58
// instead of the default config, which comes with greyhound.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    59
//
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    60
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    61
grey_reload_config();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    62
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    63
// create directories
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    64
@mkdir('./compiled');
0
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
{
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    69
  global $httpd, $avahi_process;
36
70ef461bbffa Children are no longer noisy when dying (don't I sound perverted)
Dan
parents: 34
diff changeset
    70
  if ( !defined('HTTPD_WS_CHILD') )
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    71
  {
36
70ef461bbffa Children are no longer noisy when dying (don't I sound perverted)
Dan
parents: 34
diff changeset
    72
    status("Caught SIGTERM, cleaning up.");
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    73
    if ( is_resource($avahi_process) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    74
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    75
      @proc_terminate($avahi_process);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    76
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
    77
  }
36
70ef461bbffa Children are no longer noisy when dying (don't I sound perverted)
Dan
parents: 34
diff changeset
    78
  
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
    79
  exit(0);
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    80
}
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
    81
31
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    82
status('doing PHP capabilities check');
54
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    83
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    84
if ( version_compare(PHP_VERSION, '5.1.0', '<') )
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    85
{
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    86
  burnout('The minimum required version of PHP for Greyhound is 5.1.0. PHP 5.2.x is recommended.');
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    87
}
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    88
31
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    89
if ( !function_exists('pcntl_signal') )
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    90
{
54
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    91
  warning('System does not support POSIX functions. Termination signals will result in unclean shutdown and multi-process webserving will not work.');
6d8b9497e44b Added check for PHP 5.1.0 and improved error handling for missing pcntl
Dan
parents: 48
diff changeset
    92
  $allow_fork = false;
31
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    93
}
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    94
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    95
if ( !function_exists('simplexml_load_file') )
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    96
{
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    97
  warning('Can\'t find support for SimpleXML, which is needed to parse the Amarok playlist file.');
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    98
  burnout('SimpleXML required to continue. You may have an outdated version of PHP; most versions of PHP 5 have SimpleXML built-in. Check your distribution\'s documentation to find out how to enable PHP\'s SimpleXML support.');
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
    99
}
48004643a8a5 Made Greyhound check for SimpleXML.
Dan
parents: 29
diff changeset
   100
44
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   101
if ( !function_exists('imagepng') || !function_exists('imagecopyresampled') || !function_exists('imagecreatefromjpeg') || !function_exists('imagecreatefromwbmp') )
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   102
{
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   103
  warning('Can\'t find support for GD 2.0. Artwork will not be displayed. Look around in your distro\'s package manager for php-gd or php5-gd.');
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   104
}
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   105
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   106
status('initializing playlist');
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
// init playlist object
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   109
$playlist = array();
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
   110
$amarok_home = false;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   111
rebuild_playlist();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   112
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   113
// startup webserver
58
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   114
if ( $public )
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   115
{
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   116
  if ( $enable_ipv6 )
70
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   117
    $ip = '::';
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   118
  else
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   119
    $ip = '0.0.0.0';
58
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   120
}
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   121
else
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   122
{
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   123
  if ( $enable_ipv6 )
70
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   124
    $ip = '::1';
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   125
  else
efabb54a418d Core: should properly handle IPv4/IPv6 toggle preferences now
Dan
parents: 64
diff changeset
   126
    $ip = '127.0.0.1';
58
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   127
}
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   128
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
   129
$port = 7447;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   130
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   131
try
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   132
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   133
  status('starting PhpHttpd');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   134
  $httpd = new WebServer($ip, $port);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   135
  
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   136
  // if we have avahi and proc_open support, publish the service (new)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   137
  if ( $allowcontrol && function_exists('proc_open') && $path = which('avahi-publish') )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   138
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   139
    // get our current hostname (hack, sort of)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   140
    $hostfile = tempnam('hostname', ( function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp' ));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   141
    system("hostname > '$hostfile' 2>/dev/null");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   142
    $hostname = trim(@file_get_contents($hostfile));
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   143
    unlink($hostfile);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   144
    if ( !empty($hostname) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   145
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   146
      status('Publishing service on local network with Avahi');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   147
      $descriptorspec = array(
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   148
          0 => array('pipe', 'r'),
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   149
          1 => array('pipe', 'w'),
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   150
          2 => array('pipe', 'w')
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   151
        );
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   152
      $thisuser = get_current_user();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   153
      
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   154
      $avahi_command = "'$path' -s \"{$thisuser}'s\"' AmaroK playlist on $hostname' _greyhound._tcp $port";
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   155
      $avahi_process = proc_open($avahi_command, $descriptorspec, $avahi_pipes);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   156
      if ( !$avahi_process )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   157
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   158
        warning('proc_open() failed; could not start announcement of service on Avahi network');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   159
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   160
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   161
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   162
  
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   163
  // setup handlers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   164
  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
   165
  $httpd->add_handler('index',                'function', 'amarok_playlist');
44
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   166
  $httpd->add_handler('login',                'function', 'greyhound_login_page');
92dd253f501c First shot at getting a session management system in place. Login and logout pages are there, and auth seems to be working and sufficiently secure for the moment. Sessions last indefinitely and are cookie-based.
Dan
parents: 40
diff changeset
   167
  $httpd->add_handler('logout',               'function', 'greyhound_logout');
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   168
  $httpd->add_handler('config',               'function', 'greyhound_config');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
   169
  $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
25
5c377ceb0e4c Added initial album artwork support.
Dan
parents: 21
diff changeset
   170
  $httpd->add_handler('artwork',              'function', 'artwork_request_handler');
55
b8a3c9c54fbe Added login and credentials JSON API for some... upcoming new features.
Dan
parents: 54
diff changeset
   171
  $httpd->add_handler('api',                  'function', 'api_request_handler');
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 7
diff changeset
   172
  $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
   173
  $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
   174
  $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 36
diff changeset
   175
  $httpd->add_handler('spacer.gif',           'file',     GREY_ROOT . '/spacer.gif');
58
05a69bab5f10 Added custom volume setting function (press v in the playlist window)
Dan
parents: 55
diff changeset
   176
  $httpd->add_handler('trans80.png',          'file',     GREY_ROOT . '/trans80.png');
78
08f8a72b1f7b Added Offline Mode - automatically turned on and off based on connectivity to server. Version bumped to 0.1a5.
Dan
parents: 70
diff changeset
   177
  $httpd->add_handler('offlinemode.png',      'file',     GREY_ROOT . '/offlinemode.png');
08f8a72b1f7b Added Offline Mode - automatically turned on and off based on connectivity to server. Version bumped to 0.1a5.
Dan
parents: 70
diff changeset
   178
  $httpd->add_handler('offlinemodelarge.png', 'file',     GREY_ROOT . '/offlinemodelarge.png');
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   179
  $httpd->threader->ipc_register('reloadconfig', 'grey_reload_config');
78
08f8a72b1f7b Added Offline Mode - automatically turned on and off based on connectivity to server. Version bumped to 0.1a5.
Dan
parents: 70
diff changeset
   180
  $httpd->threader->ipc_register('reloadplaylist', 'rebuild_playlist_ipc');
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   181
  // 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
   182
  // 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
   183
  // 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
   184
  // 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
   185
  // children can respond to theme resource requests.
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   186
  // if ( $allow_fork )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   187
  // {
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   188
    status('Preloading themes');
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   189
    
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   190
    $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
   191
    if ( !$dh )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   192
      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
   193
    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
   194
    {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   195
      if ( $dir == '.' || $dir == '..' )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   196
        continue;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   197
      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
   198
        load_theme($dir);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   199
    }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents: 46
diff changeset
   200
  // }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   201
  $httpd->allow_dir_list = true;
17
66c3eb4cdc4c Re-enabled multi-threaded operation after trying with a field test
Dan
parents: 16
diff changeset
   202
  $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
   203
  $httpd->default_document = 'index';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   204
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   205
  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
   206
  $httpd->serve();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   207
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   208
catch( Exception $e )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   209
{
40
bd3372a2afc1 Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents: 36
diff changeset
   210
  if ( strstr(strval($e), "Could not bind") || strstr(strval($e), "Address already in use") )
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33 31
diff changeset
   211
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33 31
diff changeset
   212
    burnout("Could not bind to the port $ip:$port. Is Greyhound already running? Sometimes browsers don't close off their connections until Greyhound has been dead for about a minute, so try starting Greyhound again in roughly 60 seconds. If that doesn't work, type \"killall -9 php\" at a terminal and try starting Greyhound again in 60 seconds.");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33 31
diff changeset
   213
  }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   214
  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
   215
}
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
   216