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

/**
 * Copyright (C) Nico Wenig
 * All rights reserved
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @filesource
 */



/**
 * Login (and create account if not exists)
 *
 * @category   Extension (login)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-03-27
 */

	if(isset($_POST['username']) && isset($_POST['password'])) {
		connector('login before');

		// Parse data
		$_POST['username'] = strip_tags($_POST['username']);
		$_POST['password'] = strip_tags($_POST['password']);
		
		// Handle user
		if($_POST['username'] != '' && $_POST['password'] != '') {
			if(!file_exists($system_data['file']['users'])) {
				// Save new user
				$open_file_users = fopen($system_data['file']['users'], 'wb');
				fputs($open_file_users, $_POST['username'] . ':' . md5($_POST['password']));
				fclose($open_file_users);
				
				// Create user related vars
				$_SESSION['user'] = sha1($_POST['username']);
				$administrator_name = $_POST['username'];
				$users[$_POST['username']] = md5($_POST['password']);
				
				// Go to backend start page
				header('location: index.php?backend');
			} else {
				// Login for existing user
				for($i = 0; $i < count($users); $i++) {
					if(isset($users[$_POST['username']]) && $users[$_POST['username']] == md5($_POST['password'])) {
						// Create user related vars
						$_SESSION['user'] = sha1($_POST['username']);
						
						// Go to backend start page
						header('location: index.php?backend');
					} else {
						// Invalid login
						$login_error = true;
					}
				}
			}
		}
	}

?>