<?php

// defines if this script requires to be logged in
define( "PRIVATE", true );

require_once('../lib/inc-admin.php');
require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-admin_prefs-edit.php");
  
$pref_file = FS_PATH.'/etc/inc-prefs.php';
  
$ogg123 = substr (`which ogg123`, 0, -1);
$mpg321 = substr (`which mpg321`, 0, -1);
$mpg123 = substr (`which mpg123`, 0, -1);

if ($_REQUEST['do'] == 'save') {

  # define if we can use persistent connections
  # for the selected database type
  switch (strtolower(DB_TYPE)) {
    case "postgres":
      if (ini_get("pgsql.allow_persistent") == 1) $persistent = 1;
      break;
    case "postgres7":
      if (ini_get("pgsql.allow_persistent") == 1) $persistent = 1;
      break;
    case "mysql":
      if (ini_get("mysql.allow_persistent") == 1) $persistent = 1;
      break;
  }

  if (strlen($_REQUEST['db_passField']) > 0) {
    $db_passwd = $_REQUEST['db_passField'];
  } else {
    $db_passwd = obfuscate_undo(DB_PASS);
  }
  
  $safe_dbconn = &ADONewConnection(DB_TYPE);

  # connect using PConnect or Connect and print error if any.
  if ($persistent == 1) {
    if ( !$safe_dbconn->PConnect($_REQUEST['db_hostField'],$_REQUEST['db_userField'],$db_passwd,$_REQUEST['db_nameField']) ) {
      alert( ADMPREF_ERR_DBCONN . " (\$dbconn->PConnect)" );
    }
  } else {
    if ( !$safe_dbconn->Connect($_REQUEST['db_hostField'],$_REQUEST['db_userField'],$db_passwd,$_REQUEST['db_nameField']) ) {
      alert( ADMPREF_ERR_DBCONN . " (\$dbconn->Connect)" );
    }
  }
  
  $safe_dbconn->Close();

  if (strlen($_REQUEST['sec_keyField']) >= 30) {
    // generate a new security key based on new user input
    $sec_key = encode_security_key($_REQUEST['sec_keyField']);
  } else {
    // take advantage of the save to update the current security key
    $sec_key = encode_security_key(SECURITY_KEY);
  }
  
  $jukebox_plist = separatorCleanup($_REQUEST['jukebox_plistField']);

  if (    ($_REQUEST['jukebox_playerField'] != "")
       && ($_REQUEST['jukebox_plistField'] != "") ) {

    $temp = explode('/', $jukebox_plist);
    $unused = array_pop($temp);
    $jukebox_plist_dir = separatorCleanup(implode('/',$temp));
    
    if ( (!@is_writeable($jukebox_plist_dir)) || (!@touch($jukebox_plist)) ) {
      alert( ADMPREF_ERR_JUKEBOXPLIST );
    }
    
    $jukebox_player_pid = $jukebox_plist_dir.'/player.pid';
    
    unset($temp, $jukebox_plist_dir);
  
  }

  if (    ($_REQUEST['jukebox_playerField'] != "")
       && ($_REQUEST['jukebox_player_pathField'] != "") ) {
  
    $jukebox_player_path = separatorCleanup($_REQUEST['jukebox_player_pathField']);
    
    if (!@file_exists($jukebox_player_path)) {
      alert( ADMPREF_ERR_JUKEBOXPLAYERPATH );
    }
  
  }
  
  $radio_plist = separatorCleanup(separatorCleanup($_REQUEST['radio_plistField']));

  if (    ($_REQUEST['radio_typeField'] != "")
       && ($_REQUEST['radio_plistField'] != "") ) {

    $temp = explode('/', $radio_plist);
    $unused = array_pop($temp);
    $radio_plist_dir = implode('/',$temp);
    
    if ( (!@is_writeable($radio_plist_dir)) || (!@touch($radio_plist)) ) {
      alert( ADMPREF_ERR_RADIOPLIST );
    }
    
    unset($temp, $radio_plist_dir);
  
  }
  
  $data = array(   'SECURITY_MODE' => $_REQUEST['sec_modeField']
                 , 'SECURITY_KEY' => $sec_key 
                 , 'DB_TYPE' => $_REQUEST['db_typeField']
                 , 'DB_HOST' => $_REQUEST['db_hostField']
                 , 'DB_USER' => $_REQUEST['db_userField']
                 , 'DB_PASS' => obfuscate_apply($db_passwd)
                 , 'DB_NAME' => $_REQUEST['db_nameField']
                 , 'SUPPORTED_FORMATS' => 'mp3,mp2,ogg,wma,ra,rm'
                 , 'STREAM_SRVR' => separatorCleanup($_REQUEST['stream_srvrField'])
                 , 'MUSIC_DIR' => separatorCleanup($_REQUEST['music_dirField'])
                 , 'DATA_DIR_IMPORT' => 'var/data/import'
                 , 'DATA_DIR_BACKUP' => 'var/data/backup'
                 , 'ARTWORK_DIR' => 'var/artwork'
                 , 'JUKEBOX_PLAYER' => $_REQUEST['jukebox_playerField']
                 , 'JUKEBOX_PLAYER_PATH' => separatorCleanup($_REQUEST['jukebox_player_pathField'])
                 , 'JUKEBOX_PLAYER_PID' => $jukebox_player_pid
                 , 'JUKEBOX_PLIST' => separatorCleanup($jukebox_plist)
                 , 'RADIO_TYPE' => $_REQUEST['radio_typeField']
                 , 'RADIO_URL' => separatorCleanup($_REQUEST['radio_urlField'])
                 , 'RADIO_PLIST' => separatorCleanup($radio_plist) );
  
  SavePrefFile($pref_file, $_REQUEST['save_to_fileField'], $data);

} else {
 
  $section = "sysadmin";
  include (INTERFACE_HEADER);
  
  if (@is_writable($pref_file)) $write_check = 'checked';
  
  // 1.0b16 upgrade
  if (!defined('JUKEBOX_PLAYER')) define('JUKEBOX_PLAYER','');
  if (!defined('JUKEBOX_PLAYER_PATH')) define('JUKEBOX_PLAYER_PATH','');
  if (!defined('JUKEBOX_PLAYER_PID')) define('JUKEBOX_PLAYER_PATH',FS_PATH.'/var/data/jukebox/player.pid');
  if (!defined('JUKEBOX_PLIST')) define('JUKEBOX_PLIST',FS_PATH.'/var/data/jukebox/playlist.m3u');

?>

<script language='Javascript'>

  function security_note() {
  
     var myalert = "";
     
     myalert += "<?php echo  ADMPREF_FORMS_SAVETOFILE_HELP_1 ?>";
  
     alert(myalert);
  
  }

  function security_def() {
  
     var myalert = "";
     
     myalert += "<?php echo  ADMPREF_FORMS_SECMODE_HELP_1_1 . ADMPREF_FORMS_SECMODE_HELP_1_2 ?>";
  
     alert(myalert);
  
  }

  function jukebox_help() {
  
     var myalert = "";
     
     myalert += "<?php echo  ADMPREF_FORMS_JUKEBOX_HELP_1 ?>";
  
     alert(myalert);
  
  }

  function radio_help() {
  
     var myalert = "";
     
     myalert += "<?php echo  ADMPREF_FORMS_RADIO_HELP_1 ?>";
  
     alert(myalert);
  
  }

  function checkForm () {

    var msg = '';

    if (    (document.prefsForm.sec_keyField.value.length > 0)
         && (document.prefsForm.sec_keyField.value.length < 30) ) {
      msg = msg + '- <?php echo  ADMPREF_CHECKFORM_SECKEY ?>.\n';  
    }
    if (document.prefsForm.db_nameField.value.length == 0) {
      msg = msg + '- <?php echo  ADMPREF_CHECKFORM_DBNAME ?>.\n';  
    }
    if (document.prefsForm.stream_srvrField.value.length == 0) {
      msg = msg + '- <?php echo  ADMPREF_CHECKFORM_STREAM ?>.\n';  
    }

    if (msg == '') {

      return (true);

    } else {

      alert(msg);

      return (false);

    }
		
	}
	
	function detect_player (player) {
	
		var pname = player.options[player.selectedIndex].value;
		
		if (pname == 'mpg123') {
			document.prefsForm.jukebox_player_pathField.value = '<?php echo $mpg123 ?>';
			return (true);
		
		}
		
		if (pname == 'mpg321') {
			document.prefsForm.jukebox_player_pathField.value = '<?php echo $mpg321 ?>';
			return (true);
		
		}
		
		if (pname == 'ogg123') {
			document.prefsForm.jukebox_player_pathField.value = '<?php echo $ogg123 ?>';
			return (true);
		
		}
		
		if (player.selectedIndex == 0 || player.selectedIndex == 4) {
			document.prefsForm.jukebox_player_pathField.value = '';
			return (true);
		
		}
		
	}
	
</script>
<div align=center>

<form action='<?php echo $_SERVER['PHP_SELF']?>' method=post name='prefsForm' target="_self" onSubmit='return checkForm ();'>
<input type=hidden name="do" value="save">

<table width='550' border=0 cellspacing=1 cellpadding=3 class='border'>
<tr>
  <td class='header' nowrap><B><?php echo  ADMPREF_HEADER_1 ?></B></td>
</tr>
<tr>
  <td class='content' nowrap align=center>
    <table border=0 cellspacing=0 cellpadding=2>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td width="30%" align=right nowrap><?php echo  ADMPREF_FORMS_SAVETOFILE ?></td>
      <td width="70%" align=left nowrap>
        <input type="checkbox" name="save_to_fileField" value="1" <?php echo $write_check?>> <?php echo  ADMPREF_FORMS_CAPT_ENABLED ?>
        &nbsp; (<a href="javascript:security_note();"><?php echo  ADMPREF_FORMS_SAVETOFILE_HELP_2 ?></a>)
      </td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td width="30%" align=right nowrap><?php echo  ADMPREF_FORMS_SECMODE ?></td>
      <td width="70%" align=left nowrap>
        <?php echo sec_mode_menu("sec_modeField",SECURITY_MODE,"")?>
        &nbsp; (<a href="javascript:security_def();"><?php echo  ADMPREF_FORMS_SECMODE_HELP_2 ?></a>)
      </td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_SECKEY ?></td>
      <td align=left nowrap>
        <input type=text name='sec_keyField' size='50' maxlength='500' value="" class=input_content>
      </td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_DBTYPE ?></td>
      <td align=left nowrap><?php echo db_type_menu("db_typeField",DB_TYPE)?></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_DBHOST ?></td>
      <td align=left nowrap><input type=text name='db_hostField' size='50' maxlength='256' value="<?php echo DB_HOST?>" class=input_content></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_DBUSER ?></td>
      <td align=left nowrap><input type=text name='db_userField' size='50' maxlength='256' value="<?php echo DB_USER?>" class=input_content></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_DBPASS ?></td>
      <td align=left nowrap><input type=password name='db_passField' size='50' maxlength='256' value="" class=input_content></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_DBNAME ?></td>
      <td align=left nowrap><input type=text name='db_nameField' size='50' maxlength='256' value="<?php echo DB_NAME?>" class=input_content></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_STREAM ?></td>
      <td align=left nowrap><input type=text name='stream_srvrField' size='50' maxlength='500' value="<?php echo STREAM_SRVR?>" class=input_content title="eg: http://your.host.dom/path/to/your/music"></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_MUSICDIR ?></td>
      <td align=left nowrap><input type=text name='music_dirField' size='50' maxlength='500' value="<?php echo MUSIC_DIR?>" class=input_content title="eg: /path/to/your/music"></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=center nowrap colspan=2>
        <input type=submit value="<?php echo  ADMPREF_FORMS_BTN_SAVE ?>" class='btn_content'>
        <input type=reset value="<?php echo  ADMPREF_FORMS_BTN_RESET ?>" class='btn_content'>
      </td>
    </tr>
  </table>
</tr>
</table>

<br>

<table width='550' border=0 cellspacing=1 cellpadding=3 class='border'>
<tr>
  <td class='header' nowrap><B><?php echo  ADMPREF_HEADER_5 ?></B></td>
</tr>
<tr>
  <td class='content' nowrap align=center>
    <table border=0 cellspacing=0 cellpadding=2>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_JUKEBOXPLAYER ?></td>
      <td align=left nowrap>
        <?php echo jukebox_player_menu("jukebox_playerField",JUKEBOX_PLAYER,"javascript:detect_player(this);")?>
        &nbsp; (<a href="javascript:jukebox_help();"><?php echo  ADMPREF_FORMS_JUKEBOX_HELP_2 ?></a>)
      </td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_JUKEBOXPLAYERPATH ?></td>
      <td align=left nowrap><input type=text name='jukebox_player_pathField' size='50' maxlength='256' value='<?php echo JUKEBOX_PLAYER_PATH?>' class=input_content></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_JUKEBOXPLIST ?></td>
      <td align=left nowrap><input type=text name='jukebox_plistField' size='50' maxlength='256' value='<?php echo JUKEBOX_PLIST?>' class=input_content></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=center nowrap colspan=2>
        <input type=submit value='<?php echo  ADMPREF_FORMS_BTN_SAVE ?>' class='btn_content'>
        <input type=reset value='<?php echo  ADMPREF_FORMS_BTN_RESET ?>' class='btn_content'>
      </td>
    </tr>
  </table>
</tr>
</table>

<br>

<table width='550' border=0 cellspacing=1 cellpadding=3 class='border'>
<tr>
  <td class='header' nowrap><B><?php echo  ADMPREF_HEADER_3 ?></B></td>
</tr>
<tr>
  <td class='content' nowrap align=center>
    <table border=0 cellspacing=0 cellpadding=2>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_RADIOTYPE ?></td>
      <td align=left nowrap>
        <?php echo radio_type_menu("radio_typeField",RADIO_TYPE)?>
        &nbsp; (<a href="javascript:radio_help();"><?php echo  ADMPREF_FORMS_RADIO_HELP_2 ?></a>)
      </td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_RADIOPLIST ?></td>
      <td align=left nowrap><input type=text name='radio_plistField' size='50' maxlength='256' value='<?php echo RADIO_PLIST?>' class=input_content></td>
    </tr>
    <tr>
      <td align=right nowrap><?php echo  ADMPREF_FORMS_RADIOURL ?></td>
      <td align=left nowrap><input type=text name='radio_urlField' size='50' maxlength='256' value='<?php echo RADIO_URL?>' class=input_content></td>
    </tr>
    <tr>
      <td align=left nowrap colspan=2>&nbsp;</td>
    </tr>
    <tr>
      <td align=center nowrap colspan=2>
        <input type=submit value='<?php echo  ADMPREF_FORMS_BTN_SAVE ?>' class='btn_content'>
        <input type=reset value='<?php echo  ADMPREF_FORMS_BTN_RESET ?>' class='btn_content'>
      </td>
    </tr>
  </table>
</tr>
</table>

<form>

</div>

<?php

  include (INTERFACE_FOOTER);

}

exit;


function sec_mode_menu($name, $selected_val="", $onchange="") {

  $html = "<select name=\"$name\" onchange=\"$onchange\" class=input_content>";
  
  $values = array("0.0","0.1","0.2","1.0","1.1","1.2");
  
  foreach ($values as $value) {
  
    if ($value == $selected_val) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    
    $html .= "<option $selected>$value</option>";
  
  }

  $html .= "</select>";
  
  return $html;

}

function db_type_menu($name, $selected_val="", $onchange="") {

  $html = "<select name=\"$name\" onchange=\"$onchange\" class=input_content>";
  
  $values = array( "mysql"=>"MySQL", "postgres"=>"Postgres 6.x", "postgres7"=>"Postgres 7.x" );
  
  foreach ($values as $key => $value) {
  
    if ($key == $selected_val) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    
    $html .= "<option value=\"$key\" $selected>$value</option>";
  
  }

  $html .= "</select>";
  
  return $html;

}

function jukebox_player_menu($name, $selected_val="", $onchange="") {

  $html = "<select name=\"$name\" onchange=\"$onchange\" class=input_content>";
  
  $players = array('mpg123','mpg321','ogg123','winamp');
  
  $values = array( ""=>ADMPREF_FORMS_JUKEBOXPLAYER_CAPTION
                 , 'mpg123'=>'mpg123 (osx, linux, etc.)'
                 , 'mpg321'=>'mpg321 (linux, etc.)'
                 , 'ogg123'=>'ogg123 (linux, etc.)'
                 , 'winamp'=>'winamp (windows)' );
  
  foreach ($values as $key => $value) {
  
    if ($key == $selected_val) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    
    $html .= "<option value=\"$key\" $selected>$value</option>";
  
  }

  $html .= "</select>";
  
  return $html;

}

function radio_type_menu($name, $selected_val="", $onchange="") {

  $html = "<select name=\"$name\" onchange=\"$onchange\" class=input_content>";
  
  $values = array( ""=>ADMPREF_FORMS_RADIOTYPE_CAPTION_1, "QTSS4"=>ADMPREF_FORMS_RADIOTYPE_CAPTION_2, "text"=>ADMPREF_FORMS_RADIOTYPE_CAPTION_3 );
  
  foreach ($values as $key => $value) {
  
    if ($key == $selected_val) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    
    $html .= "<option value=\"$key\" $selected>$value</option>";
  
  }

  $html .= "</select>";
  
  return $html;

}

?>
