<?php


require_once('scripts/sb_functions.php');
global $logged_in;
$logged_in = logged_in( true, true );
read_config();
//sb_language( 'index' );
sb_language( 'user_admin' );

html_header('',false);


function page_content() {


  $user = array();
  $passFile = 'config/user.php';
  if (file_exists($passFile)){
    include($passFile);
  }

  if ($_POST['permissions']){

      global $all_permisions;
      global $default_permissions;
      foreach ($default_permissions as $task => $enabled){
	  if (isset($_POST["__DEFAULT__:$task"])){
		$all_permissions['__DEFAULT__'][$task] = 
		    $_POST["__DEFAULT__:$task"];
	  }
	  else{
	      $all_permissions['__DEFAULT__'][$task] = 0;
	  }
      }
      foreach ($user as $name => $pass){
	foreach ($default_permissions as $task => $enabled){
	    if (isset($_POST["$name:$task"])){
		$all_permissions[$name][$task] = $_POST["$name:$task"];
	    }
	    else{
		// I'm not sure why this is necessary but when I tried
		// this script at one machine all '.' in the $_POST
		// variable where converted to '_'
		// don't ask me why ....
		$name_with_underscores = str_replace('.','_',$name);
		if (isset($_POST["$name_with_underscores:$task"])){
		    $all_permissions[$name][$task] = 
			$_POST["$name_with_underscores:$task"];
		}
		else{
		    $all_permissions[$name][$task] = 0;
		}
	    }
	}
      }
      set_permissions($all_permissions);
  }

  ///////// delete a user

  if ($_REQUEST['delete']){

    if (isset($user[$_REQUEST['delete']])){
      unset($user[$_REQUEST['delete']]);
      chmod($passFile,0644);
      for ($i=1;$i<5;$i++){
	$fh = fopen($passFile,'w+');
	if ($fh){
	  flock( $fh, LOCK_EX );
	  break;
	}
	sleep(1);
      }

      if ($fh){
	foreach ($user as $name => $pass){
	  $str = "<?php \$user['".$name."'] = '".$pass."'; ?>\n";
	  $result = fwrite( $fh, $str );
	}
	$lock = flock( $fh, LOCK_UN );
	fclose( $fh );
	if (file_exists($passFile)){
	  chmod($passFile,0444);
	}
      }
    }
  }

  /////// show users

  global $lang_string;

  global $default_permissions;
  if (file_exists('config/permissions.php')){
      require('config/permissions.php');
      if (array_key_exists('__DEFAULT__',$all_permissions)){
	  foreach ($all_permissions['__DEFAULT__'] as $key => $val){
	      $default_permissions[$key] = $val;
	  }
	  // $default_permissions = $all_permissions['__DEFAULT__'];
      }
  }
  if (!is_array($all_permissions)){
      $all_permissions=array();
  }

  echo "<h2>".$lang_string['menu_user_admin']."</h2>";

  echo '<p><a href="signup.php">';
  echo($lang_string[ 'menu_signup' ]);
  echo '</a></p>';

  echo('<form name="permissions" method="post" action="'.$_SERVER['PHP_SELF'].'">');

  echo '<hr /><p>';
  echo $lang_string['instructions'];
  echo '<br /><br />';
  echo '<table>';
  echo '<tr><td></td><td></td>';
  foreach ($default_permissions as $task => $enabled){
      if (isset($lang_string[$task])){
	  $task = $lang_string[$task];
      }
      else {
	  $task = str_replace('_',' ',$task);
      }
      echo '<td>'.$task.'</td>';
  }
  echo '</tr>';


  // row for settings default permissions

  echo '<tr><td>default</td>';
  echo '<td></td>';
  foreach ($default_permissions as $task => $enabled){
      $enabled = $default_permissions[$task];
      if (is_array($all_permissions)){
	  if (is_array($all_permissions['__DEFAULT__'])){
	      $enabled = $all_permissions['__DEFAULT__'][$task];
	  }
      }
      echo '<td align="center">';
      echo('<input name="__DEFAULT__:'.$task.'" value="1" type="checkbox"');
      if ($enabled == 1){echo 'checked="checked"';}
      echo('/></td>');
  }
  echo '</tr>';

  // one row per user for setting uer permissions

  foreach ($user as $name => $pass){
    echo '<tr><td>'.$name.'</td>';
    echo '<td>[<a href="?delete='.urlencode($name).'">';
    echo($lang_string[ 'sb_delete' ]);
    echo '</a>]</td>';

    if (! array_key_exists($name,$all_permissions)){
	foreach ($default_permissions as $task => $enabled){
	    $all_permissions[$name][$task] = $enabled;
	}
    }
    foreach ($default_permissions as $task => $enabled){
	if (is_array($all_permissions)){
	    if (is_array($all_permissions[$name])){
		$enabled = $all_permissions[$name][$task];
	    }
	}
	echo '<td align="center">';
	echo('<input name="'.$name.':'.$task.'" value="1" type="checkbox"');
	if ($enabled == 1){echo 'checked="checked"';}
	echo('/></td>');
    }
    echo '</tr>';

  }
  echo '</table></p>';

  echo('<br /><input type="submit" name="permissions" value="');
  echo $lang_string['submit_btn'];
  echo '" />';
  echo '</form>';


}
		

theme_pagelayout();
html_footer();

?>
