#!/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() if ($ENV{REQUEST_METHOD} eq 'POST');

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

exit (0);

sub prepare {
	&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 has already been taken","name") if (&user_exist($tokens{name}));
	&error("The username field must be between $tokens{name_min} to $tokens{name_max} characters in length","name") if (length($tokens{name}) < $tokens{name_min} || length($tokens{name}) > $tokens{name_max});

	&error("The password field is required","password") if (!$tokens{password});
	&error("The password field must be between $tokens{password_min} to $tokens{password_max} characters in length","password") if (length($tokens{password}) < $tokens{password_min} || length($tokens{password}) > $tokens{password_max});
	&error("The password field must not be the same as your username","password") if ($tokens{password} eq $tokens{name});
	&error("The password field doesn't match what you re-typed","password") if ($tokens{password} ne $tokens{password2});

	&error("The email address field is required","email") if (!$tokens{email});
	&error("The email address field contains invalid characters","email") if ($tokens{email} !~ /.+\@[\w\-\.]{4,}/);

	if ($tokens{distinct_email} > 0 && $tokens{email}) {
		my $count=&index_display("email",$tokens{email});
		&error("The email address you entered has already been assigned to $count account(s)","email") if ($count >= $tokens{distinct_email});
	}

	if ($tokens{email_domains}) {
		my $allowed = 0;
		foreach (split(/[\s\,]+/,$tokens{email_domains})) {
			$allowed = 1 if (lc(($tokens{email} =~ /\@(.+)$/)[0]) eq lc($_));
		}
		&error("The email address you entered wasn't found in our list of allowed domains","email") if ($allowed != 1);
	}

	&error("The title field is required","title") if (!$tokens{title});
	&error("The title field must be between $tokens{title_min} to $tokens{title_max} characters in length","title") if (length($tokens{title}) < $tokens{title_min} || length($tokens{title}) > $tokens{title_max});

	&error("The description field is required","description") if (!$tokens{description} && $tokens{description_required} eq 'on');
	&error("The description field must be less then or equal to $tokens{description_max} characters in length","description") if (length($tokens{description}) > $tokens{description_max});

	&error("The category field is required","category") if (!$tokens{category});
	&error("The category field contains invalid characters","category") if ($tokens{category} !~ /^[\w\-\_]+$/);
	&error("The category you entered wasn't found in our database","category") if (!-f "$tokens{data_dir}/categories/$tokens{category}");

	foreach (split(/\n/,&file_read("$tokens{data_dir}/text/banned"))) {
		my @line=split(/=/,$_,2);
		&error("The following field has reserved or banned data",$line[0]) if (lc($tokens{$line[0]}) eq lc($line[1]) || ($line[0] eq 'email' && $tokens{email} =~ /^.*\Q$line[1]\E$/i));
	}

	&error("The username you entered has already been taken","name") if (-e "$tokens{root_dir}/$tokens{name}");

	if (!$tokens{error_message}) {

		$tokens{usage} = 0;
		$tokens{package} = $tokens{package_default};
		$tokens{date_modified} = $tokens{date_created} = time();

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

		if (-d "$tokens{data_dir}/default.$tokens{category}") {
			$tokens{default_dir} = "$tokens{data_dir}/default.$tokens{category}";
		} else {
			$tokens{default_dir} = "$tokens{data_dir}/default";
		}

		&default_copy();

		if ($tokens{approval_method} eq 'email') {
			srand(time);
			$tokens{validation_code} = undef;
			@idset = ('0'..'9','a'..'z');

			for ($i=1;$i<=15;$i++) {
				$tokens{validation_code} .= @idset[int(rand($#idset + 1))];
			}

			&file_write("$tokens{data_dir}/validation/$tokens{validation_code}",$tokens{name});

			$tokens{status} = 0;
		} elsif ($tokens{approval_method} eq 'admin') {
			$tokens{status} = 0;
		} elsif ($tokens{approval_method} eq 'payment') {
			$tokens{status} = 0;
		} else {
			$tokens{status} = 1;
		}

		my %data = map { $_ => $tokens{$_} } ('password','email','title','description','category','package','status','usage','date_created','date_modified');

		&user_create($tokens{name},%data);

		my @data;
		foreach (grep(/^user_/,keys(%tokens))) { push (@data,(/^user_(.+)/)[0]."=$tokens{$_}"); }
		$users_data{$tokens{name}} = join("\t",@data);

		my %mail = ();

		$mail{from} = $tokens{site_email};
		$mail{to} = $tokens{email};
		$mail{subject} = "$tokens{site_title} Account";
		$mail{message} = &file("$tokens{data_dir}/mail/user_create");

		&mail(%mail);

		&notice('user_create','created an account');

		if ($tokens{approval_method} eq 'payment') {
			print "Location: $tokens{script_url}/payment.cgi?name=$tokens{name}\n\n";
			exit(0);
		} else {
			$tokens{c} = "done";
		}
	}
}

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

	if ($dir ne "") {
		mkdir("$tokens{root_dir}/$tokens{name}/$dir",0755) || &error("Can't create directory ($tokens{root_dir}/$tokens{name}/$dir): $!");

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

		foreach (@dir) {
			next if ($_ =~ /^\.+$/);

			if (-d "$tokens{data_dir}/default/$dir/$_") {
				&default_copy("$dir/$_");
			} elsif (/\.(html|htm|asp|shtml|shtm|php)$/i) {
				&file_write("$tokens{root_dir}/$tokens{name}/$dir/$_",&file_includes(&file("$tokens{data_dir}/default/$dir/$_")));
				$tokens{usage} += (stat("$tokens{root_dir}/$tokens{name}/$dir/$_"))[7];
			} else {
				&file_write("$tokens{root_dir}/$tokens{name}/$dir/$_",&file_read("$tokens{data_dir}/default/$dir/$_"));
				$tokens{usage} += (stat("$tokens{root_dir}/$tokens{name}/$dir/$_"))[7];
			}
		}
	} else {
		mkdir("$tokens{root_dir}/$tokens{name}",0755) || &error("Can't create directory ($tokens{root_dir}/$tokens{name}): $!");

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

		foreach (@dir) {
			next if ($_ =~ /^\.+$/);

			if (-d "$tokens{data_dir}/default/$_") {
				&default_copy($_);
			} elsif (/\.(html|htm|asp|shtml|shtm|php)$/i) {
				&file_write("$tokens{root_dir}/$tokens{name}/$_",&file_includes(&file("$tokens{data_dir}/default/$_")));
				$tokens{usage} += (stat("$tokens{root_dir}/$tokens{name}/$_"))[7];
			} else {
				&file_write("$tokens{root_dir}/$tokens{name}/$_",&file_read("$tokens{data_dir}/default/$_"));
				$tokens{usage} += (stat("$tokens{root_dir}/$tokens{name}/$_"))[7];
			}
		}
	}
}

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

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

	$text =~ s/<!-- _(HEADER|FOOTER)_START_ -->.*?<!-- _(HEADER|FOOTER)_END_ -->//gsi;

	if ($tokens{use_header} eq 'on') {
		my $header=&file("$tokens{data_dir}/text/header.$tokens{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{category}","$tokens{data_dir}/text/footer");
		$text .= "<!-- _FOOTER_START_ -->$footer<!-- _FOOTER_END_ -->" if (!($text =~ s/(<\/body>)/<!-- _FOOTER_START_ -->$footer<!-- _FOOTER_END_ -->$1/si));
	}

	return ($text);
}