<?php
defined('_UEXEC') or die('Restricted access');

/************************************************************************
	UebiMiau is a GPL'ed software developed by 
	
	 - Aldoir Ventura - aldoir@users.sourceforge.net
	 - http://uebimiau.sourceforge.net
	
	Fell free to contact, send donations or anything to me :-)
	São Paulo - Brasil
	
	**********************************************
	Version 3.2.0 Upgrades and templates developed by
	 
	 - Todd Henderson - Lead Developer
	 - http://tdah.us
	 - Email: todd@tdah.us
	 
	 - Dave Rodgers - Project Consultant
	 - http://www.manvel.net 
	 - Email: Davenmanvel@inbox.com
	
	Feel free to contact us, send donations or anything to me as well...
	
	Special thanks to the developers at telaen.org, codeworxtech.com, tinymce.moxiecode.com
	without help this project wouldn't be possible 

	**********************************************
	- File:			admin/common.php
	- Developer: 	Todd Henderson / Mods by Laurent (AdNovea)
	- Date:			
	- version:		
	- Description:  Admin Users management


*************************************************************************/
		
	$lid = (isset($_POST['lid']) ? $_POST['lid'] : (isset($_GET['lid']) ? $_GET['lid'] : "default"));
	$tid = (isset($_POST['tid']) ? $_POST['tid'] : (isset($_GET['tid']) ? $_GET['tid'] : "default"));
  	
 	define("RPATH_ROOT","../");							// Relative path to Root

	require(RPATH_ROOT."inc/config/config.uebimiau.php");
	require(RPATH_ROOT."inc/lib.php");
  require(RPATH_ROOT."inc/config/config.paths.php");
	define("ADMIN_DATABASE",RPATH_ROOT.$temporary_directory.DB_ADMIN);
	define("ADMIN_LOGS",RPATH_ROOT.$temporary_directory.DB_LOG_FILE);
	session_start();
	error_reporting (E_ALL );

		if (!file_exists(RPATH_ROOT.$temporary_directory.'backups')) 
	mkdir(RPATH_ROOT.$temporary_directory.'backups');
	
	// Add a new Admin user
	function registerUser($user,$email,$pass1,$pass2){
	
		global $admin_pwd_notidentical, $admin_pwd_tooshort, $admin_userexist, $admin_invalid;
		
		// Check passwords
		$errorText = '';
		if ($user == "") return $admin_invalid;
		if ($pass1 != $pass2) $errorText = $admin_pwd_notidentical;
		elseif (strlen($pass1) < 6) $errorText = $admin_pwd_tooshort;
		
		// Check user existence	
		$pfile = fopen(ADMIN_DATABASE,"a+");
		rewind($pfile);
	
		while (!feof($pfile)) {
			$line = fgets($pfile);
			$tmp = explode(':', $line);
			if ($tmp[0] == $user) {
				$errorText = $admin_userexist;
				break;
			}
		}
		
		// If everything is OK -> store user data
		if ($errorText == ''){
			// Secure password string
			$userpass = md5($pass1);
			fwrite($pfile, "$user:$userpass\n");
		}
		
		fclose($pfile);
		
		return $errorText;
	}
	
	// Admin user login procedure
	function loginUser($user,$pass){
	
		global $admin_invalid;
		
		// Check user existance	
		$date = date(LOG_DATE);
		$errorText = '';
		$validUser = false;
		$pfile = fopen(ADMIN_DATABASE,"r");
		rewind($pfile);
	
		while (!feof($pfile)) {
			$line = fgets($pfile);
			$tmp = explode(':', $line);
			if ($tmp[0] == $user) {
				// User exists, check password
				if (trim($tmp[1]) == trim(md5($pass))){
					$validUser= true;
					$_SESSION['userName'] = $user;
				}
				break;
			}
		}
		fclose($pfile);
	
		if ($validUser == true) {
			$_SESSION['validUser'] = true;
			$line = "<span class=\"log_date\">$date</span><span class=\"log_event\"><b>$user</b> has entered the Administration.</span><br>\n";
		} else {
			$_SESSION['validUser'] = false;
			$line = "<span class=\"log_date\">$date</span><span class=\"log_bad\"><b>$user</b> has failed to enter the Administration (<i>$admin_invalid</i>).</span><br>\n";
			$errorText = $admin_invalid;
		}
		
	// Log report	
		$file = fopen(ADMIN_LOGS, 'a+');
		fwrite($file, $line);
		fclose($file);

		return $errorText;	
	}
	
	// Logout the Admin user
	function logoutUser(){
		unset($_SESSION['validUser']);
		unset($_SESSION['userName']);
	}
	
	// Redirect to the login page if noone is connected
	function checkUser(){
		global $lid, $tid;
		if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
			header("Location: login.php?lid=$lid&tid=$tid");
		}
	}

	// Remove Admin user
	function removeUser($user) {
	
		global $admin_last;
		
		if (count(file(ADMIN_DATABASE)) < 2) return $admin_last;
		
		if (file_exists(ADMIN_DATABASE)) {
			$f_text = file(ADMIN_DATABASE);
			$file_handle = fopen(ADMIN_DATABASE,"w");
			foreach( $f_text as $line ) {
				 if ( !strstr($line, $user) )
					   fputs($file_handle, $line);
			}
			fclose($file_handle);
		}
	}

	// List Admin user
	function listUsers() {
		$content = "";
		if (file_exists(ADMIN_DATABASE)) {
			$f_text = file(ADMIN_DATABASE);
			foreach( $f_text as $line ) {
				$tmp = explode(':', trim($line)); 
				if ($tmp[0] != "") $content .= "<option value=\"".$tmp[0]."\">".$tmp[0]."</option>\n";
			}
		}
		return $content;
	}

?>