#!/usr/bin/perl
# EasyHost
# Built by CyberScript
# Website: http://www.cyberscript.net
# Email: rcampbell@cyberscript.net
# (C) Copyright 2001, All Rights Reserved.
#####################################################
# The following code may not be modified            #
# or redistributed by anyone other then CyberScript #
#####################################################

$pwd = ($0 =~ /^(.+)[\/\\]/)[0] || '.';
require ("$pwd/cs-lib.pl");

&prepare();

print &ct("text/html").&display("manager");

exit (0);

sub file_includes {
	my ($file,$text)=@_;

	return ($file,$text) if ($tokens{include_handler} eq 'dynamic');

	if ($file =~ /\.(html|htm|asp|shtml|shtm|php)$/i) {
		$text =~ s/<!-- _(HEADER|FOOTER)_START_ -->.*?<!-- _(HEADER|FOOTER)_END_ -->//gsi;

		if ($tokens{use_header} eq 'on') {
			my $header=&file("$tokens{data_dir}/text/header.$tokens{login_category}","$tokens{data_dir}/text/header");
			$text = "<!-- _HEADER_START_ -->$header<!-- _HEADER_END_ -->$text" if (!($text =~ s/(<body.*?>)/$1<!-- _HEADER_START_ -->$header<!-- _HEADER_END_ -->/si));
		}

		if ($tokens{use_footer} eq 'on') {
			my $footer=&file("$tokens{data_dir}/text/footer.$tokens{login_category}","$tokens{data_dir}/text/footer");
			$text .= "<!-- _FOOTER_START_ -->$footer<!-- _FOOTER_END_ -->" if (!($text =~ s/(<\/body>)/<!-- _FOOTER_START_ -->$footer<!-- _FOOTER_END_ -->$1/si));
		}
	}

	return ($file,$text);
}

sub login {
	if ($tokens{login_id}) {
		&error("The login id field contains invalid characters") if ($tokens{login_id} !~ /^[\w]+$/);

		if (!-f "$tokens{data_dir}/logins/$tokens{login_id}") {
			&cookie_set("login_id");
			&error("The login id you entered has expired");
		}

		foreach my $line (split(/\n/,&file_read("$tokens{data_dir}/logins/$tokens{login_id}"))) {
			my @line=split(/=/,$line,2);
			$tokens{"login_$line[0]"}=$line[1];
		}

		if ($tokens{login_address} ne $ENV{REMOTE_ADDR}) {
			&cookie_set("login_id");
			&error("The login id you entered has expired");
		}

		if (!&user_exist($tokens{login_name})) {
			&cookie_set("login_id");
			&error("The username you entered wasn't found in our database");
		}

		&login_data();

		$tokens{c} = 'main' if ($tokens{c} eq 'login');
	} elsif ($tokens{c} eq 'login') {
		&error("The username field is required","name") if (! ( $tokens{name}=lc($tokens{name}) ) );
		&error("The username field contains invalid characters","name") if ($tokens{name} !~ /^[\w\-]+$/);
		&error("The username you entered wasn't found in our database","name") if (!&user_exist($tokens{name}));

		&error("The password field is required","password") if (!$tokens{password});

		if (!$tokens{error_message}) {
			$tokens{login_name}=$tokens{name};
			&login_data;
		}

		if (!$tokens{error_message}) {
			srand(time);
			$tokens{login_id} = undef;
			@idset = ('0'..'9','a'..'z','A'..'Z');
			for ($i=1;$i<=15;$i++) { $tokens{login_id} .= @idset[int(rand($#idset + 1))]; }

			&file_write("$tokens{data_dir}/logins/$tokens{login_id}","name=$tokens{login_name}\naddress=$ENV{REMOTE_ADDR}");
			&cookie_set('login_id',$tokens{login_id}) if ($tokens{use_cookie} eq 'on');

			&notice('login','logged in');

			$tokens{c} = 'main';
		}
	} else {
		$tokens{c} = 'login';
	}
}

sub login_data {
	my %user = &user_display($tokens{login_name});
	foreach (keys(%user)) {
		$tokens{'login_'.$_}=$user{$_};
	}

	if ($tokens{c} eq 'login' && crypt($tokens{password},'as') ne $tokens{login_password}) {
		&notice('login_incorrect','entered an incorrect password');
		&error("The password you entered is incorrect","password");
	}

	if ($tokens{login_status} != 1) {
		&cookie_set("login_id");
		&notice('login_incorrect','tried to login to a disabled account');
		&error("Your account has not been activated or has been disabled");
	}

	if (!$tokens{error_message}) {
		foreach my $line (split(/\n/,&file_read("$tokens{data_dir}/packages/$tokens{login_package}"))) {
			my @line=split(/=/,$line,2);
			$tokens{"$line[0]"}=$line[1];
		}
	}
}

sub prepare {
	&login;

	if ($tokens{login_id}) {
		$tokens{home_dir} = $tokens{root_dir}.'/'.$tokens{login_name};
		$tokens{home_url} = $tokens{login_alternate_url} || $tokens{root_url}.'/'.$tokens{login_name};

		if ($tokens{dir} = $tokens{d}) {
			&error("Invalid directory") if ($tokens{dir} =~ /[\.]{2,}/);
			$tokens{current_dir} = $tokens{home_dir}.$tokens{dir};
			$tokens{current_url} = $tokens{home_url}.$tokens{dir};
		} else {
			$tokens{dir} = $tokens{d} = undef;
			$tokens{current_dir} = $tokens{home_dir};
			$tokens{current_url} = $tokens{home_url};
		}

		&usage();

		$tokens{no_space} = 'on' if ($tokens{space_limit} > 0 && $tokens{count_usage} > ($tokens{space_limit} * 1024));

		&cmd("manager");

		my %data = ();

		$data{usage} = $tokens{count_usage};
		$data{date_modified} = time();

		&user_modify($tokens{login_name},%data);

		if ($tokens{space_limit} > 0 && $tokens{count_usage} > ($tokens{space_limit} * 1024)) { $tokens{no_space} = 'on'; }
		else { delete $tokens{no_space}; }

		opendir(DIR,$tokens{current_dir}) || &error("Can't open directory: $!");
		my @dir = readdir(DIR);
		closedir(DIR);

		my %extensions;

		$tokens{count_total} = 0;
		$tokens{count_directories} = 0;
		$tokens{count_files} = 0;

		$tokens{total_directories} = 0;
		$tokens{total_files} = 0;

		foreach my $file (@dir) {
			next if ($file =~ /^[\.]*$/);

			$tokens{count_total}++;

			if (-d "$tokens{current_dir}/$file") {
				$tokens{count_directories}++;
			} else {
				$tokens{count_files}++;
				$tokens{count_size} += (stat("$tokens{current_dir}/$file"))[7];

				$extensions{($file =~ /([^\.]*)$/)[0]} = 1;
			}
		}

		@extensions = sort {lc($a) cmp lc($b)} keys(%extensions);
		@directories = sort {lc($a) cmp lc($b)} @directories;

		$tokens{count_size} = &size_format($tokens{count_size});
		$tokens{space_avail} = &size_format(($tokens{space_limit} * 1024) - $tokens{count_usage});
		$tokens{count_usage} = &size_format($tokens{count_usage});
		$tokens{space_limit} = &size_format($tokens{space_limit} * 1024);
	}
}

sub usage {
	my ($dir)=@_;

	opendir(DIR,"$tokens{home_dir}/$dir");
	my @dir = readdir(DIR);
	closedir(DIR);

	foreach my $file (@dir) {
		next if ($file =~ /^[\.]+$/);

		if (-d "$tokens{home_dir}/$dir/$file") { &usage("$dir/$file"); $tokens{total_directories}++; }
		else {
			$tokens{count_usage} += (stat("$tokens{home_dir}/$dir/$file"))[7];
			$tokens{total_files}++;
		}

		
	}

	$tokens{count_usage} += (stat("$tokens{home_dir}/$dir"))[7];

	push (@directories,$dir) if ($dir);

}