<?php if(!defined('master_script')) die();

/**
 * Save user data
 *
 * @category   Extension (users)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-03-27
 */

	if(isset($_POST['save_users']) && $_SESSION['user'] == sha1($administrator_name)) {
		$i = 0;
		
		$new_users = array();
		
		foreach($users as $key => $value) {
			$_POST['user_name'][0] = strip_tags($_POST['user_name'][0]);
			$_POST['user_password'][0] = strip_tags($_POST['user_password'][0]);
			
			if($i == 0) {
				if($_POST['user_name'][0] == '') $_POST['user_name'][$i] = $key;
				if(sha1($_POST['user_name'][0]) != $_SESSION['user']) $_SESSION['user'] = sha1($_POST['user_name'][0]);
				$administrator_name = $_POST['user_name'][0];
			}

			if(isset($_POST['user_name'][$i]))
				$new_users[$_POST['user_name'][$i]] = ($_POST['user_password'][$i] == '' ? $value : md5($_POST['user_password'][$i]));
			
			$i++;
		}
		
		$users = array();
		
		foreach($new_users as $key => $value) if($key != '') $users[$key] = $value;

		// Add new user
		if($_POST['new_user_name'] != '' && $_POST['new_user_password'] != '')
			$users[$_POST['new_user_name']] = md5($_POST['new_user_password']);
		
		connector('users save');
		
		// Save user data
		$open_file_users = fopen($system_data['file']['users'], 'wb');
		foreach($users as $key => $value) if($key != '' && $value != '') fputs($open_file_users, $key . ':' . $value . "\n");
		fclose($open_file_users);
	}
	
?>