uiconfig.php
changeset 50 1b4288399b1f
child 64 ee64bb096f56
equal deleted inserted replaced
49:d62212462f9b 50:1b4288399b1f
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Greyhound - real web management for Amarok
       
     5  * Copyright (C) 2008 Dan Fuhry
       
     6  *
       
     7  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     8  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    11  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    12  */
       
    13 
       
    14 // rebooting doesn't work at this point
       
    15 define('REBOOT_SUPPORT', false);
       
    16 
       
    17 function greyhound_config($httpd, $socket)
       
    18 {
       
    19   global $use_auth;
       
    20   
       
    21   // validate the session
       
    22   if ( !session_check() )
       
    23   {
       
    24     $httpd->header('HTTP/1.1 307 Temporary Redirect');
       
    25     $httpd->header('Location: /login');
       
    26     
       
    27     return;
       
    28   }
       
    29   
       
    30   $tried = false;
       
    31   $success = false;
       
    32   $needpass = false;
       
    33   $error = false;
       
    34   
       
    35   // if a config password is set, make sure it matches.
       
    36   $authed = false;
       
    37   if ( empty($GLOBALS['configpass']) )
       
    38   {
       
    39     $authed = true;
       
    40   }
       
    41   else
       
    42   {
       
    43     $needpass = true;
       
    44     if ( isset($_POST['configpass']) && sha1($_POST['configpass']) === $GLOBALS['configpass'] )
       
    45     {
       
    46       $authed = true;
       
    47       $tried = true;
       
    48     }
       
    49     else if ( isset($_POST['configpass']) )
       
    50     {
       
    51       $error = 'You didn\'t enter the right configuration password, so the changes weren\'t saved.';
       
    52       $tried = true;
       
    53     }
       
    54   }
       
    55   
       
    56   if ( $authed && isset($_POST['submit']) )
       
    57   {
       
    58     $need_reboot = ( isset($_POST['public']) !== $GLOBALS['public'] ||
       
    59                      isset($_POST['allow_fork']) !== $GLOBALS['allow_fork']);
       
    60     
       
    61     // compile the new config file
       
    62     $public = ( isset($_POST['public']) ) ? 'true' : 'false';
       
    63     $allowcontrol = ( isset($_POST['allowcontrol']) ) ? 'true' : 'false';
       
    64     $allow_fork = ( isset($_POST['allow_fork']) ) ? 'true' : 'false';
       
    65     $use_auth = ( isset($_POST['use_auth']) ) ? 'true' : 'false';
       
    66     
       
    67     // for auth_data, we're going to merge the data from POST with the current auth_data array.
       
    68     $auth_data = $GLOBALS['auth_data'];
       
    69     $auth_changed = false;
       
    70     foreach ( $auth_data as $user => $pass )
       
    71     {
       
    72       if ( !in_array($user, $_POST['users']) )
       
    73       {
       
    74         $auth_changed = true;
       
    75         unset($auth_data[$user]);
       
    76       }
       
    77     }
       
    78     if ( isset($_POST['users_add']) )
       
    79     {
       
    80       foreach ( $_POST['users_add'] as $user => $pass )
       
    81       {
       
    82         $auth_changed = true;
       
    83         $auth_data[$user] = $pass;
       
    84       }
       
    85     }
       
    86     $auth_data = var_export_string($auth_data);
       
    87     
       
    88     $new_configpass = ( is_string($_POST['newconfigpass']) && $_POST['newconfigpass'] != '____________________' ) ? sha1($_POST['newconfigpass']) : $GLOBALS['configpass'];
       
    89     
       
    90     $config_file = <<<EOF
       
    91 <?php
       
    92 
       
    93 //
       
    94 // GREYHOUND CONFIGURATION
       
    95 //
       
    96 
       
    97 // Listen on all interfaces. If this is false, it will only listen on
       
    98 // 127.0.0.1 (the loopback interface)
       
    99 \$GLOBALS['public'] = $public;
       
   100 
       
   101 // Allow control of playback. By default this is turned on but if you
       
   102 // set this to false, it will only display the playlist.
       
   103 \$GLOBALS['allowcontrol'] = $allowcontrol;
       
   104 
       
   105 // The default theme. This should be a name of a directory in ./themes.
       
   106 \$GLOBALS['theme'] = 'grey';
       
   107 
       
   108 // Allow forking when an HTTP request is received. This has advantages
       
   109 // and disadvantages. If this experimental option is enabled, it will
       
   110 // result in faster responses and load times but more memory usage.
       
   111 \$GLOBALS['allow_fork'] = $allow_fork;
       
   112 
       
   113 // set to true to enable authentication
       
   114 // this uses cookies, so make sure they're enabled in your browser
       
   115 \$GLOBALS['use_auth'] = $use_auth;
       
   116 
       
   117 // valid users and passwords
       
   118 \$GLOBALS['auth_data'] = $auth_data;
       
   119 
       
   120 // password for site configuration
       
   121 \$GLOBALS['configpass'] = '$new_configpass';
       
   122 
       
   123 EOF;
       
   124     $error = 'Couldn\'t write to the config file (./greyhound-config.php).';
       
   125     
       
   126     $fp = @fopen('./greyhound-config.php', 'w');
       
   127     if ( $fp )
       
   128     {
       
   129       fwrite($fp, $config_file);
       
   130       fclose($fp);
       
   131       
       
   132       $passblurb = $auth_changed ? "\n<small>If you changed your own password, you've been logged out so you'll need to log back in again.</small>" : '';
       
   133       if ( REBOOT_SUPPORT )
       
   134       {
       
   135         $rebootblurb = $need_reboot ? "\n<small>Since you changed some core configuration values, the Greyhound server has been restarted.</small>\n<small>If Greyhound doesn't respond, wait 20 seconds and then restart it using Amarok's script manager.</small>" : '';
       
   136       }
       
   137       else
       
   138       {
       
   139         $rebootblurb = $need_reboot ? "\n<small>Please restart Greyhound using Amarok's script manager for public IP and multithreading options to take effect.</small>" : '';
       
   140       }
       
   141       $success = "Configuration changes successful.{$passblurb}{$rebootblurb}";
       
   142       $httpd->threader->ipc_send(array('action' => 'reloadconfig', 'propagate' => true));
       
   143       
       
   144       if ( $need_reboot && REBOOT_SUPPORT )
       
   145       {
       
   146         $addr = $GLOBALS['public'] ? '0.0.0.0' : '127.0.0.1';
       
   147         $fork = $GLOBALS['allow_fork'];
       
   148         $httpd->reboot($addr, null, $fork);
       
   149       }
       
   150     }
       
   151   }
       
   152   
       
   153   global $theme;
       
   154   $iphone = ( ( strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') ||
       
   155        strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') ||
       
   156        strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') ||
       
   157        isset($_GET['m']) )
       
   158        && !isset($_GET['f'])
       
   159        );
       
   160   $theme_id = ( $iphone ) ? 'iphone' : $theme;
       
   161   $smarty = load_theme($theme_id);
       
   162   
       
   163   $smarty->assign('theme', $theme_id);
       
   164   $smarty->assign('greyhound_version', GREY_VERSION);
       
   165   $smarty->assign('tried', $tried);
       
   166   $smarty->assign('success', $success);
       
   167   $smarty->assign('needpass', $needpass);
       
   168   $smarty->assign('use_auth', $use_auth);
       
   169   $smarty->assign('public', $GLOBALS['public']);
       
   170   $smarty->assign('allowcontrol', $GLOBALS['allowcontrol']);
       
   171   $smarty->assign('allow_fork', $GLOBALS['allow_fork']);
       
   172   $smarty->assign('use_auth', $GLOBALS['use_auth']);
       
   173   $smarty->assign('users', $GLOBALS['auth_data']);
       
   174   $smarty->assign('error', $error);
       
   175   $smarty->display('config.tpl');
       
   176 }