<?php

$copyright_customized = '';
$copyright_default = '<div><center><b style="font: bold 10pt Tahoma,sans-serif">© Powered by <a href="http://delavo.com">Delavo</a></b></center></div>';
$advertisement = '<div align="center">
<table border="0" width="700" cellpadding="5" id="table39" cellspacing="5">
		<tr>
			<td colspan="2"><hr>
			<p align="center"><b><font face="Tahoma">
			<span style="background-color: #FFFF00">
			<a href="http://use-sell.com/2/1510997uk/order">
			Click here to resell John\'s Little Script and add 
			your affiliate ID to all banners and llinks</a></span></font></b><br>
			<a href="http://use-sell.com/2/1510997uk/order">
			<span style="background-color: #FFFF00"><b><i><font face="Tahoma">or 
			remove this section from the version you\'ll be reselling.</font></i></b></span></a></td>
		</tr>
		<tr>
			<td>
			<p align="center">
			<a target="_blank" href="http://www.jvmanager.com">
      <img border="0" src="http://jvmanager.com/fantasos300.jpg" width="300" height="54" alt="Fantasos(TM)"></a>
			</p>
			<p align="center">
			<a target="_blank" href="http://www.contepass.com">
      <img border="0" src="http://contepass.com/images/contepass-logo-300.jpg" width="300" height="127" alt="ContePass(TM)"></a></td>
			<td>
			<p align="center">
			<a target="_blank" href="http://www.turbomembership.com">
      <img border="0" src="http://www.turbomembership.com/banners/tm300x250.gif" width="300" height="250" alt="Turbo Membership"></a></td>
		</tr>
	</table>
</div>';

$passwd_file = '';

function print_copyright()
{
	global $copyright_customized, $copyright_default;
	print $copyright_customized == '' ? $copyright_default : $copyright_customized;
}

function print_advertisement()
{
	global $advertisement;
	print $advertisement;
}

function is_admin($username)
{
	return $username == 'admin' ? 1 : 0;
}

function locate_passwd_file()
{
	global $passwd_file;
	
	if ($passwd_file != '') return $passwd_file;

	if( ! file_exists('.htaccess_old') )
		die("FAILURE: cannot locate .htaccess_old file");

	foreach ( file(".htaccess_old") as $line )
	{
		preg_match("/^AuthUserFile\\s+(\"?)([^\s\"]+)\\1/i", chop($line), $matches);
		if (! isset($matches[2])) continue;
		$passwd_file = $matches[2];
	}
	
	if (! $passwd_file)
		die('FAILURE: .htaccess_old file does not contain AuthUserFile directive!');
	
	if (! file_exists($passwd_file))
		die('FAILURE: .htpasswd file specified in .htaccess_old not found!');
	
	return $passwd_file;
}

function save_user($username, $password)
{
	if ( ! $username || ! $password)
		die("int.error: no username or password");

	$username = strtolower($username);
	
	$file = locate_passwd_file();
	$lines = file($file);	

	if ( isset($lines[0]) && preg_match('/\$apr1\$/', $lines[0]) )
		$myline = $username . ':' . crypt_apr1_md5($password, '') . "\n";
	else
		$myline = $username . ':' . crypt($password) . "\n";

	$fh = fopen($file, "w")
		or die("FAILURE: cannot create/rewrite $file");
	
	$changed = false;
	
	foreach ($lines as $line)
	{
		$line = chop($line);
		if ( strpos($line, "$username:") === 0 )
		{
			fwrite($fh, $myline);
			$changed = true;
		}
		else
		{
			fwrite($fh, "$line\n");
		}
	}
	
	if (! $changed)
	{
		fwrite($fh, $myline);
	}
		
	fclose($fh);
}

function has_user($username)
{
	if ( ! $username )
		return 0;
	
	$passwd_file = locate_passwd_file();
	
	foreach (file($passwd_file) as $line)
	{
		if ( strpos($line, "$username:") === 0 )
			return 1;
	}
	
	return 0;
}

function delete_user($username)
{
	if ( ! $username )
		return;
	
	$files = array(locate_passwd_file(), "altauth_userdb.txt");
	
	foreach ($files as $file)
	{
		if ( ! file_exists($file) )continue;
		
		$file_array = file($file);
		
		$fh = fopen($file, "w+")
			or die("FAILURE: cannot create/rewrite $file");
		
		foreach ($file_array as $line)
		{
			if ( preg_match("/^$username$/i", chop($line)) )continue;
			if ( preg_match("/^$username:/i", chop($line)) )continue;
			if ( preg_match("/^$username=/i", chop($line)) )continue;
			fwrite($fh, $line);
		}
		
		fclose($fh);
	}
}

function load_userdb()
{
	$userdb = array();
	
	$file = 'altauth_userdb.txt';
	
	if ( ! file_exists($file) )
		return $userdb;
	
	foreach (file($file) as $line)
	{
		list($email, $data) = split('=', chop($line));
		list($regdate, $regcode, $ip) = split('::', $data);
		$userdb[$email]['regdate'] = $regdate;
		$userdb[$email]['regcode'] = $regcode;
		$userdb[$email]['ip'] = preg_split('/,/', $ip, -1, PREG_SPLIT_NO_EMPTY);
	}
	
	return $userdb;
}

function save_userdb($userdb)
{
	$file = 'altauth_userdb.txt';
	$fh = fopen($file, "w+")
		or die("FAILURE: cannot open $file to write.");
	
	foreach( array_keys($userdb) as $key )
	{
		$regdate = isset($userdb[$key]['regdate']) ? $userdb[$key]['regdate'] : '';
		$regcode = isset($userdb[$key]['regcode']) ? $userdb[$key]['regcode'] : '';
		if (isset($userdb[$key]['ip']))
		{
			sort($userdb[$key]['ip']);
			$ip = join(',', $userdb[$key]['ip']);
		}
		else
			$ip = '';
		
		fwrite($fh, "$key=$regdate::$regcode::$ip\n");
	}
	
	fclose($fh);
}

function crypt_apr1_md5($plainpasswd, $cryptpass) 
{
	if ( $cryptpass == "" ) 
		$salt = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8);
	else
		$salt = substr($cryptpass, 6, 8);
	
	$len = strlen($plainpasswd);
	$text = $plainpasswd.'$apr1$'.$salt;
	$bin = pack("H32", md5($plainpasswd.$salt.$plainpasswd));
	
	for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); }
	for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; }
	
	$bin = pack("H32", md5($text));
	
	for($i = 0; $i < 1000; $i++) 
	{
		$new = ($i & 1) ? $plainpasswd : $bin;
		if ($i % 3) $new .= $salt;
		if ($i % 7) $new .= $plainpasswd;
		$new .= ($i & 1) ? $bin : $plainpasswd;
		$bin = pack("H32", md5($new));
	}
	$tmp = "";
	for ($i = 0; $i < 5; $i++) 
	{
		$k = $i + 6;
		$j = $i + 12;
		if ($j == 16) $j = 5;
		$tmp = $bin[$i].$bin[$k].$bin[$j].$tmp;
	}
	
	$tmp = chr(0).chr(0).$bin[11].$tmp;
	$tmp = strtr(strrev(substr(base64_encode($tmp), 2)),
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
	
	return "$"."apr1"."$".$salt."$".$tmp;
}

?>
