greyhound.php
changeset 48 d643bfb862d8
parent 46 1e58e36b04cd
child 54 6d8b9497e44b
equal deleted inserted replaced
47:b7f1952cef8d 48:d643bfb862d8
    28   pcntl_signal(SIGUSR1, 'handle_refresh_signal');
    28   pcntl_signal(SIGUSR1, 'handle_refresh_signal');
    29 }
    29 }
    30 
    30 
    31 @ini_set('display_errors', 'on');
    31 @ini_set('display_errors', 'on');
    32 
    32 
    33 // include files
       
    34 require('functions.php');
       
    35 
       
    36 // get the root
    33 // get the root
    37 define('GREY_ROOT', dirname(__FILE__));
    34 define('GREY_ROOT', dirname(__FILE__));
    38 
    35 
    39 // ignore this, it allows using a different config file when a Mercurial repository
    36 // what kind of terminal do we have?
    40 // exists in Greyhound's root directory (to allow the devs to have their own config
    37 $use_colors = ( @in_array(@$_SERVER['TERM'], array('linux', 'xterm', 'vt100')) ) ? true : false;
    41 // separate from the default)
    38 require(GREY_ROOT . '/functions.php');
    42 
    39 
    43 if ( @is_dir(GREY_ROOT . '/.hg') )
    40 // start up...
    44   require(GREY_ROOT . '/config.dev.php');
    41 
    45 else
    42 status('Starting Greyhound Web Control v' . GREY_VERSION);
    46   require(GREY_ROOT . '/config.php');
    43 status('loading files');
       
    44 
       
    45 require_once(GREY_ROOT . '/webserver.php');
       
    46 define('SMARTY_DIR', GREY_ROOT . '/smarty/');
       
    47 require_once(GREY_ROOT . '/smarty/Smarty.class.php');
       
    48 require_once(GREY_ROOT . '/playlist.php');
       
    49 require_once(GREY_ROOT . '/json.php');
       
    50 require_once(GREY_ROOT . '/ajax.php');
       
    51 require_once(GREY_ROOT . '/uiconfig.php');
       
    52 require_once(GREY_ROOT . '/imagetools.php');
       
    53 require_once(GREY_ROOT . '/sessions.php');
       
    54 
       
    55 //
       
    56 // LOAD OUR CONFIG
       
    57 // Amarok launches Greyhound with a different wd than Greyhound's root. This means
       
    58 // that we can drop our own config (set up from the web UI) in there and load that
       
    59 // instead of the default config, which comes with greyhound.
       
    60 //
       
    61 
       
    62 grey_reload_config();
    47 
    63 
    48 // create directories
    64 // create directories
    49 @mkdir('./compiled');
    65 @mkdir('./compiled');
    50 
    66 
    51 // what kind of terminal do we have?
       
    52 $use_colors = ( @in_array(@$_SERVER['TERM'], array('linux', 'xterm', 'vt100')) ) ? true : false;
       
    53 
       
    54 // start up...
       
    55 
       
    56 status('Starting Greyhound Web Control v' . GREY_VERSION);
       
    57 status('loading files');
       
    58 
       
    59 require('webserver.php');
       
    60 define('SMARTY_DIR', GREY_ROOT . '/smarty/');
       
    61 require(GREY_ROOT . '/smarty/Smarty.class.php');
       
    62 require(GREY_ROOT . '/playlist.php');
       
    63 require(GREY_ROOT . '/json.php');
       
    64 require(GREY_ROOT . '/ajax.php');
       
    65 require(GREY_ROOT . '/imagetools.php');
       
    66 require(GREY_ROOT . '/sessions.php');
       
    67 
       
    68 // signal handler
    67 // signal handler
    69 function sigterm($signal)
    68 function sigterm($signal)
    70 {
    69 {
    71   global $httpd;
    70   global $httpd, $avahi_process;
    72   if ( !defined('HTTPD_WS_CHILD') )
    71   if ( !defined('HTTPD_WS_CHILD') )
       
    72   {
    73     status("Caught SIGTERM, cleaning up.");
    73     status("Caught SIGTERM, cleaning up.");
       
    74     if ( is_resource($avahi_process) )
       
    75     {
       
    76       @proc_terminate($avahi_process);
       
    77     }
       
    78   }
    74   
    79   
    75   exit(0);
    80   exit(0);
    76 }
    81 }
    77 
    82 
    78 status('doing PHP capabilities check');
    83 status('doing PHP capabilities check');
   105 
   110 
   106 try
   111 try
   107 {
   112 {
   108   status('starting PhpHttpd');
   113   status('starting PhpHttpd');
   109   $httpd = new WebServer($ip, $port);
   114   $httpd = new WebServer($ip, $port);
       
   115   
       
   116   // if we have avahi and proc_open support, publish the service (new)
       
   117   if ( $allowcontrol && function_exists('proc_open') && $path = which('avahi-publish') )
       
   118   {
       
   119     // get our current hostname (hack, sort of)
       
   120     $hostfile = tempnam('hostname', ( function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp' ));
       
   121     system("hostname > '$hostfile' 2>/dev/null");
       
   122     $hostname = trim(@file_get_contents($hostfile));
       
   123     unlink($hostfile);
       
   124     if ( !empty($hostname) )
       
   125     {
       
   126       status('Publishing service on local network with Avahi');
       
   127       $descriptorspec = array(
       
   128           0 => array('pipe', 'r'),
       
   129           1 => array('pipe', 'w'),
       
   130           2 => array('pipe', 'w')
       
   131         );
       
   132       $thisuser = get_current_user();
       
   133       
       
   134       $avahi_command = "'$path' -s \"{$thisuser}'s\"' AmaroK playlist on $hostname' _greyhound._tcp $port";
       
   135       $avahi_process = proc_open($avahi_command, $descriptorspec, $avahi_pipes);
       
   136       if ( !$avahi_process )
       
   137       {
       
   138         warning('proc_open() failed; could not start announcement of service on Avahi network');
       
   139       }
       
   140     }
       
   141   }
   110   
   142   
   111   // setup handlers
   143   // setup handlers
   112   status('initializing handlers');
   144   status('initializing handlers');
   113   $httpd->add_handler('index',                'function', 'amarok_playlist');
   145   $httpd->add_handler('index',                'function', 'amarok_playlist');
   114   $httpd->add_handler('login',                'function', 'greyhound_login_page');
   146   $httpd->add_handler('login',                'function', 'greyhound_login_page');
   115   $httpd->add_handler('logout',               'function', 'greyhound_logout');
   147   $httpd->add_handler('logout',               'function', 'greyhound_logout');
       
   148   $httpd->add_handler('config',               'function', 'greyhound_config');
   116   $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
   149   $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
   117   $httpd->add_handler('artwork',              'function', 'artwork_request_handler');
   150   $httpd->add_handler('artwork',              'function', 'artwork_request_handler');
   118   $httpd->add_handler('scripts',              'dir',      GREY_ROOT . '/scripts');
   151   $httpd->add_handler('scripts',              'dir',      GREY_ROOT . '/scripts');
   119   $httpd->add_handler('favicon.ico',          'file',     GREY_ROOT . '/amarok_icon.ico');
   152   $httpd->add_handler('favicon.ico',          'file',     GREY_ROOT . '/amarok_icon.ico');
   120   $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
   153   $httpd->add_handler('apple-touch-icon.png', 'file',     GREY_ROOT . '/apple-touch-icon.png');
   121   $httpd->add_handler('spacer.gif',           'file',     GREY_ROOT . '/spacer.gif');
   154   $httpd->add_handler('spacer.gif',           'file',     GREY_ROOT . '/spacer.gif');
       
   155   $httpd->threader->ipc_register('reloadconfig', 'grey_reload_config');
   122   // load all themes if forking is enabled
   156   // load all themes if forking is enabled
   123   // Themes are loaded when the playlist is requested. This is fine for
   157   // Themes are loaded when the playlist is requested. This is fine for
   124   // single-threaded operation, but if the playlist handler is only loaded
   158   // single-threaded operation, but if the playlist handler is only loaded
   125   // in a child process, we need to preload all themes into the parent before
   159   // in a child process, we need to preload all themes into the parent before
   126   // children can respond to theme resource requests.
   160   // children can respond to theme resource requests.
   127   if ( $allow_fork )
   161   // if ( $allow_fork )
   128   {
   162   // {
   129     status('Preloading themes');
   163     status('Preloading themes');
   130     
   164     
   131     $dh = @opendir(GREY_ROOT . '/themes');
   165     $dh = @opendir(GREY_ROOT . '/themes');
   132     if ( !$dh )
   166     if ( !$dh )
   133       burnout('Could not open themes directory');
   167       burnout('Could not open themes directory');
   136       if ( $dir == '.' || $dir == '..' )
   170       if ( $dir == '.' || $dir == '..' )
   137         continue;
   171         continue;
   138       if ( is_dir( GREY_ROOT . "/themes/$dir" ) )
   172       if ( is_dir( GREY_ROOT . "/themes/$dir" ) )
   139         load_theme($dir);
   173         load_theme($dir);
   140     }
   174     }
   141   }
   175   // }
   142   $httpd->allow_dir_list = true;
   176   $httpd->allow_dir_list = true;
   143   $httpd->allow_fork = ( $allow_fork ) ? true : false;
   177   $httpd->allow_fork = ( $allow_fork ) ? true : false;
   144   $httpd->default_document = 'index';
   178   $httpd->default_document = 'index';
   145   
   179   
   146   status("Entering main server loop - ^C to interrupt, listening on port $port");
   180   status("Entering main server loop - ^C to interrupt, listening on port $port");
   162     // we're not serving yet.
   196     // we're not serving yet.
   163     return false;
   197     return false;
   164     
   198     
   165   // we've got an httpd instance; rebuild the playlist
   199   // we've got an httpd instance; rebuild the playlist
   166   rebuild_playlist();
   200   rebuild_playlist();
   167   
   201 }
   168   // if this is the parent, also ask the children to rebuild.
   202 
   169   if ( !defined('HTTPD_WS_CHILD') )
       
   170   {
       
   171     foreach ( $httpd->child_list as $pid )
       
   172     {
       
   173       posix_kill($pid, SIGUSR1);
       
   174     }
       
   175   }
       
   176 }