greyhound.php
changeset 48 d643bfb862d8
parent 46 1e58e36b04cd
child 54 6d8b9497e44b
--- a/greyhound.php	Mon Sep 01 17:03:44 2008 -0400
+++ b/greyhound.php	Tue Sep 23 23:24:13 2008 -0400
@@ -30,47 +30,52 @@
 
 @ini_set('display_errors', 'on');
 
-// include files
-require('functions.php');
-
 // get the root
 define('GREY_ROOT', dirname(__FILE__));
 
-// ignore this, it allows using a different config file when a Mercurial repository
-// exists in Greyhound's root directory (to allow the devs to have their own config
-// separate from the default)
-
-if ( @is_dir(GREY_ROOT . '/.hg') )
-  require(GREY_ROOT . '/config.dev.php');
-else
-  require(GREY_ROOT . '/config.php');
-
-// create directories
-@mkdir('./compiled');
-
 // what kind of terminal do we have?
 $use_colors = ( @in_array(@$_SERVER['TERM'], array('linux', 'xterm', 'vt100')) ) ? true : false;
+require(GREY_ROOT . '/functions.php');
 
 // start up...
 
 status('Starting Greyhound Web Control v' . GREY_VERSION);
 status('loading files');
 
-require('webserver.php');
+require_once(GREY_ROOT . '/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');
-require(GREY_ROOT . '/imagetools.php');
-require(GREY_ROOT . '/sessions.php');
+require_once(GREY_ROOT . '/smarty/Smarty.class.php');
+require_once(GREY_ROOT . '/playlist.php');
+require_once(GREY_ROOT . '/json.php');
+require_once(GREY_ROOT . '/ajax.php');
+require_once(GREY_ROOT . '/uiconfig.php');
+require_once(GREY_ROOT . '/imagetools.php');
+require_once(GREY_ROOT . '/sessions.php');
+
+//
+// LOAD OUR CONFIG
+// Amarok launches Greyhound with a different wd than Greyhound's root. This means
+// that we can drop our own config (set up from the web UI) in there and load that
+// instead of the default config, which comes with greyhound.
+//
+
+grey_reload_config();
+
+// create directories
+@mkdir('./compiled');
 
 // signal handler
 function sigterm($signal)
 {
-  global $httpd;
+  global $httpd, $avahi_process;
   if ( !defined('HTTPD_WS_CHILD') )
+  {
     status("Caught SIGTERM, cleaning up.");
+    if ( is_resource($avahi_process) )
+    {
+      @proc_terminate($avahi_process);
+    }
+  }
   
   exit(0);
 }
@@ -108,24 +113,53 @@
   status('starting PhpHttpd');
   $httpd = new WebServer($ip, $port);
   
+  // if we have avahi and proc_open support, publish the service (new)
+  if ( $allowcontrol && function_exists('proc_open') && $path = which('avahi-publish') )
+  {
+    // get our current hostname (hack, sort of)
+    $hostfile = tempnam('hostname', ( function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp' ));
+    system("hostname > '$hostfile' 2>/dev/null");
+    $hostname = trim(@file_get_contents($hostfile));
+    unlink($hostfile);
+    if ( !empty($hostname) )
+    {
+      status('Publishing service on local network with Avahi');
+      $descriptorspec = array(
+          0 => array('pipe', 'r'),
+          1 => array('pipe', 'w'),
+          2 => array('pipe', 'w')
+        );
+      $thisuser = get_current_user();
+      
+      $avahi_command = "'$path' -s \"{$thisuser}'s\"' AmaroK playlist on $hostname' _greyhound._tcp $port";
+      $avahi_process = proc_open($avahi_command, $descriptorspec, $avahi_pipes);
+      if ( !$avahi_process )
+      {
+        warning('proc_open() failed; could not start announcement of service on Avahi network');
+      }
+    }
+  }
+  
   // setup handlers
   status('initializing handlers');
   $httpd->add_handler('index',                'function', 'amarok_playlist');
   $httpd->add_handler('login',                'function', 'greyhound_login_page');
   $httpd->add_handler('logout',               'function', 'greyhound_logout');
+  $httpd->add_handler('config',               'function', 'greyhound_config');
   $httpd->add_handler('action.json',          'function', 'ajax_request_handler');
   $httpd->add_handler('artwork',              'function', 'artwork_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->add_handler('spacer.gif',           'file',     GREY_ROOT . '/spacer.gif');
+  $httpd->threader->ipc_register('reloadconfig', 'grey_reload_config');
   // load all themes if forking is enabled
   // Themes are loaded when the playlist is requested. This is fine for
   // single-threaded operation, but if the playlist handler is only loaded
   // in a child process, we need to preload all themes into the parent before
   // children can respond to theme resource requests.
-  if ( $allow_fork )
-  {
+  // if ( $allow_fork )
+  // {
     status('Preloading themes');
     
     $dh = @opendir(GREY_ROOT . '/themes');
@@ -138,7 +172,7 @@
       if ( is_dir( GREY_ROOT . "/themes/$dir" ) )
         load_theme($dir);
     }
-  }
+  // }
   $httpd->allow_dir_list = true;
   $httpd->allow_fork = ( $allow_fork ) ? true : false;
   $httpd->default_document = 'index';
@@ -164,13 +198,5 @@
     
   // we've got an httpd instance; rebuild the playlist
   rebuild_playlist();
-  
-  // if this is the parent, also ask the children to rebuild.
-  if ( !defined('HTTPD_WS_CHILD') )
-  {
-    foreach ( $httpd->child_list as $pid )
-    {
-      posix_kill($pid, SIGUSR1);
-    }
-  }
 }
+