<?php

//   -------------------------------------------------------------------------------
//  |                  net2ftp: a web based FTP client                              |
//  |              Copyright (c) 2003-2005 by David Gartner                         |
//  |                                                                               |
//  | This program is free software; you can redistribute it and/or                 |
//  | modify it under the terms of the GNU General Public License                   |
//  | as published by the Free Software Foundation; either version 2                |
//  | of the License, or (at your option) any later version.                        |
//  |                                                                               |
//   -------------------------------------------------------------------------------




// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_loginform($net2ftp_directory, $net2ftp_url) {

// -------------------------------------------------------------------------
// This function prints the net2ftp login form.
//
// ----- VARIABLES -----
// $includes_directory = directory where the net2ftp includes are located, for example 
//                          /usr/web/net2ftp/includes
//                       Note that there is a leading / but no trailing / at the end.
// $net2ftp_url        = URL of net2ftp on your website, for example
//                          http://www.mydomain.com/tools/net2ftp/index.php
//
// ----- STYLE -----
// The look of the login form can be customized with CSS (cascading style sheets) classes.
// The input textboxes use class "input" and the login button uses class "button".
// -------------------------------------------------------------------------


// -------------------------------------------------------------------------
// Includes
// -------------------------------------------------------------------------
	require_once($net2ftp_directory . "/settings.inc.php");
	require_once($net2ftp_directory . "/settings_authorizations.inc.php");
	require_once($net2ftp_directory . "/settings_screens.inc.php");
	require_once($net2ftp_directory . "/includes/errorhandling.inc.php");
	require_once($net2ftp_directory . "/includes/languages.inc.php");
	require_once($net2ftp_directory . "/includes/skins.inc.php");


// -------------------------------------------------------------------------
// Global variables and settings
// -------------------------------------------------------------------------
	global $settings;
	global $net2ftpcookie_ftpserver;
	global $net2ftpcookie_ftpserverport;
	global $net2ftpcookie_username;
	global $net2ftpcookie_language;
	global $net2ftpcookie_skin;
	global $net2ftpcookie_ftpmode;
	global $net2ftpcookie_passivemode;
	global $net2ftpcookie_sslconnect;
	global $net2ftpcookie_viewmode;
	global $net2ftpcookie_directory;
//	global $net2ftpcookie_sort;
//	global $net2ftpcookie_sortorder;

	global $PHP_SELF;

	$net2ftp_allowed_ftpservers    = $settings["net2ftp_allowed_ftpservers"];
	$net2ftp_allowed_ftpserverport = $settings["net2ftp_allowed_ftpserverport"];
	$logindirectory                = $settings["logindirectory"];


// -------------------------------------------------------------------------
// Load the language file
// -------------------------------------------------------------------------
	if (is_array($settings) == false) {
		includeLanguageFile();
	}


// -------------------------------------------------------------------------
// Begin output
// -------------------------------------------------------------------------
	echo "\n\n\n\n\n";
	echo "<!-- This login form is generated by the net2ftp function net2ftp_loginform() -->\n";
	echo "<!-- net2ftp's main code is free software, released under the GNU/GPL license -->\n";


// -------------------------------------------------------------------------
// Javascript
// -------------------------------------------------------------------------
		echo "<script type=\"text/javascript\"><!--\n";

// Check that some values are entered on the input form
		echo "function CheckInput(form) {\n";
		echo "	var u,p1,p2,e;\n";
		echo "	s=form.ftpserver.value;\n";
		echo "	u=form.username.value;\n";
		echo "	p=form.password.value;\n";

		echo "	if (s.length==0) {\n";
		echo "		form.ftpserver.focus();\n";
		echo "		alert(\"" . __("Please enter an FTP server.") . "\");\n";
		echo "		return false;\n";
		echo "	}\n";

		echo "	if (u.length==0) {\n";
		echo "		form.username.focus();\n";
		echo "		alert(\"" . __("Please enter a username.") . "\");\n";
		echo "		return false;\n";
		echo "	}\n";

//		echo "	if (p.length==0) {\n";
//		echo "		form.password.focus();\n";
//		echo "		alert(\"" . __("Please enter a password.") . "\");\n";
//		echo "		return false;\n";
//		echo "	}\n";

		echo "	return true;\n";
		echo "}\n";

// Anonymous login
		echo "function do_anonymous(form) {\n";
		echo "	var checked = form.anonymous.checked;\n";
		echo "	if (checked == true) {\n";
		echo "		vars_defined = 'true';\n";
		echo "		last_username = form.username.value;\n";
		echo "		last_password = form.password.value;\n";
		echo "		form.username.value = \"anonymous\";\n";
		echo "		form.password.value = \"user@net2ftp\";\n";
		echo "	} else {\n";
		echo "		form.username.value = last_username;\n";
		echo "		form.password.value = last_password;\n";
		echo "	}\n";
		echo "	return true;\n";
		echo "}\n";
		
// Clear Cookies
		echo "function ClearCookies() {\n";
		echo "	document.LoginForm.state.value='homepage';\n";
		echo "	document.LoginForm.state2.value='';\n";
		echo "	document.LoginForm.cookieset.value='clear';\n";
		echo "	document.LoginForm.submit();\n";
		echo "}\n";

		echo "//--></script>\n";

// -------------------------------------------------------------------------
// Form and table start
// -------------------------------------------------------------------------
	echo "<form name=\"LoginForm\" id=\"LoginForm\" action=\"$net2ftp_url\" method=\"post\" onSubmit=\"return CheckInput(this);\">\n";
	echo "<div style=\"border: 1px solid black; background-color: #DDDDDD; margin-top: 10px; padding: 10px; width: 420px;\">";
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";

	echo "<tr>\n";



// -------------------------------------------------------------------------
// Row 1: FTP server + port + explanation
// -------------------------------------------------------------------------

// ---------------------------------------
// FTP server
// ---------------------------------------

// Print the FTP server
	if ($settings['fielduse_login_ftpserver'] == "yes") {
		echo "<td valign=\"top\">" . __("FTP server") . "</td>\n";
		echo "<td valign=\"top\" colspan=\"2\">\n";
		
		// All FTP servers are allowed
		if ($net2ftp_allowed_ftpservers[1] == "ALL") {
			echo "<input type=\"text\" class=\"input\" name=\"ftpserver\" value=\"$net2ftpcookie_ftpserver\" />\n";
		}
		// Only a list of FTP servers are allowed
		elseif (sizeof($net2ftp_allowed_ftpservers) > 1) {
			echo "<select name=\"ftpserver\">\n";
			for ($i=1; $i<=sizeof($net2ftp_allowed_ftpservers); $i=$i+1) {
				// Select the first entry by default
				if ($i == 1) { $selected = "selected"; }
				else { $selected = ""; }
	
				echo "<option value=\"$net2ftp_allowed_ftpservers[$i]\" $selected>$net2ftp_allowed_ftpservers[$i]</option>\n";
			} // end for
			echo "</select>\n";
		}
		// Only 1 FTP server is allowed. Print a hidden field and text.
		elseif (sizeof($net2ftp_allowed_ftpservers) == 1) {
			echo "<input type=\"hidden\" name=\"ftpserver\" value=\"$net2ftp_allowed_ftpservers[1]\" />\n"; echo "<b>$net2ftp_allowed_ftpservers[1]</b>\n"; 
		}
		else {
			$errormessage = "There is an error in the net2ftp configuration file <b>settings_authorizations.inc.php</b>: variable <b>\$net2ftp_allowed_ftpservers</b> does not follow the expected format.";
			setErrorVars(false, $errormessage, debug_backtrace());
			return false;
		}
	}

// Do not print the FTP server, only a hidden input field
	else {
		// Only 1 FTP server is allowed. Print a hidden field but NO text.
		if (sizeof($net2ftp_allowed_ftpservers) == 1) {
			echo "<input type=\"hidden\" name=\"ftpserver\" value=\"$net2ftp_allowed_ftpservers[1]\" />\n";
		}
		else {
			$errormessage = "There is an error in the net2ftp configuration file <b>settings_authorizations.inc.php</b>: variable <b>\$net2ftp_allowed_ftpservers</b> should contain exactly 1 FTP server.";
			setErrorVars(false, $errormessage, debug_backtrace());
			return false;
		}
	}
	
// ---------------------------------------
// FTP server port
// ---------------------------------------

// Print the FTP server port
	if ($settings['fielduse_login_ftpserverport'] == "yes") {
		if ($net2ftp_allowed_ftpserverport == "ALL") {
			if ($net2ftpcookie_ftpserverport != "") {
				echo " port <input type=\"text\" class=\"input\" size=\"3\" maxlength=\"5\" name=\"ftpserverport\" value=\"$net2ftpcookie_ftpserverport\" />\n";
			}
			else {
				echo " port <input type=\"text\" class=\"input\" size=\"3\" maxlength=\"5\" name=\"ftpserverport\" value=\"21\" />\n";
			}
		}
		else {
			echo "<input type=\"hidden\" name=\"ftpserverport\" value=\"$net2ftp_allowed_ftpserverport\" />\n";
		}
	}
	
// Do not print the FTP server port, only a hidden input field
	else {
		if ($net2ftp_allowed_ftpserverport != "ALL") {
			echo "<input type=\"hidden\" name=\"ftpserverport\" value=\"$net2ftp_allowed_ftpserverport\" />\n";
		}
		else {
			echo "<input type=\"hidden\" name=\"ftpserverport\" value=\"21\" />\n";
		}
	}

// ---------------------------------------
// Explanation
// ---------------------------------------
	if ($net2ftp_allowed_ftpservers[1] == "ALL" && $settings['fielduse_login_ftpserver'] == "yes") { 
		echo "<div style=\"font-size: 80%;\">\n";
		echo __("Example") . ": ftp.server.com or 192.123.45.67\n";
		echo "</div>\n";
	}

	if ($settings['fielduse_login_ftpserver'] == "yes") {
		echo "</td>\n";
		echo "</tr>\n";
	}

// -------------------------------------------------------------------------
// Row 2: username and anonymous checkbox
// -------------------------------------------------------------------------
	echo "<tr valign=\"middle\">\n";
	echo "<td>" . __("Username") . "</td>\n";
	echo "<td><input type=\"text\" class=\"input\" name=\"username\" value=\"$net2ftpcookie_username\" /></td>\n";
	echo "<td>\n";
	if ($settings['fielduse_login_anonymous'] == "yes") {
		echo "<span style=\"font-size: 80%;\"><input type=\"checkbox\" name=\"anonymous\" value=\"1\" onclick=\"do_anonymous(form);\" /> " . __("Anonymous") . "</span>\n";
	}
	echo "</td>\n";
	echo "</tr>\n";


// -------------------------------------------------------------------------
// Row 3: password and passive mode checkbox
// -------------------------------------------------------------------------
	echo "<tr valign=\"middle\">\n";
	echo "<td>" . __("Password") . "</td>\n";
	echo "<td><input type=\"password\" class=\"input\" name=\"password\" /></td>\n";
	if ($net2ftpcookie_passivemode == "yes") { $input_passivemode_checked = "checked"; }
	else                                     { $input_passivemode_checked = ""; }
	echo "<td>\n";
	if ($settings['fielduse_login_passivemode'] == "yes") {
		echo "<span style=\"font-size: 80%;\"><input type=\"checkbox\" name=\"passivemode\" value=\"yes\" $input_passivemode_checked /> " . __("Passive mode") . "</span>\n";
	}
	echo "</td>\n";
	echo "</tr>\n";


// -------------------------------------------------------------------------
// Row 4: initial directory and SSL connect checkbox
// -------------------------------------------------------------------------

	if ($settings['fielduse_login_directory'] == "yes" || $settings['fielduse_login_sslconnect'] == "yes") {
		echo "<tr valign=\"middle\">\n";
	
	// Initial directory	
		if ($settings['fielduse_login_directory'] == "yes") {
			if (strlen($net2ftpcookie_directory) > 1) { $input_directory = $net2ftpcookie_directory; }
			else                                      { $input_directory = $logindirectory; }
			echo "<td>" . __("Initial directory") . "</td>\n";
			echo "<td><input type=\"text\" class=\"input\" name=\"directory\" value=\"$input_directory\" /></td>\n";
		}
		else {
			echo "<td></td>\n";
			echo "<td></td>\n";
		}
	
	// SSL connect
		if ($settings['fielduse_login_sslconnect'] == "yes") {
			if     (!function_exists("ftp_ssl_connect")) { $input_sslconnect_status = "disabled"; }
			elseif ($net2ftpcookie_sslconnect == "yes")  { $input_sslconnect_status = "checked"; }
			echo "<td><span style=\"font-size: 80%;\"><input type=\"checkbox\" name=\"sslconnect\" value=\"yes\" $input_sslconnect_status/> SSL</span></td>\n";
		}
		else {
			echo "<td></td>\n";
		}
		
		echo "</tr>\n";
	}

// -------------------------------------------------------------------------
// Row 5-6: optional entries
// -------------------------------------------------------------------------	
	if ($settings['fielduse_login_language'] == "yes") {
		echo "<tr valign=\"middle\">\n";
		echo "<td>" . __("Language") . "</td>\n";
		echo "<td>\n";
		$onchange = "document.location='$PHP_SELF?language='+document.LoginForm.language.value;";
		printLanguageSelect("language", $onchange);
		echo "</td>\n";
		echo "<td></td>\n";
		echo "</tr>\n";
	}

	if ($settings['fielduse_login_skin'] == "yes") {
		echo "<tr valign=\"middle\">\n";
		echo "<td>" . __("Skin") . "</td>\n";
		echo "<td>\n";
		printSkinSelect("skin", "");
		echo "</td>\n";
		echo "<td></td>\n";
		echo "</tr>\n";
	}
	
// -------------------------------------------------------------------------
// Row 7: FTP mode
// -------------------------------------------------------------------------
	if ($settings['fielduse_login_ftpmode'] == "yes") {
		if ($net2ftpcookie_ftpmode == "automatic") { $ftpmode_automatic_checked = "checked"; $ftpmode_binary_checked = ""; }
		else                                       { $ftpmode_automatic_checked = "";        $ftpmode_binary_checked = "checked"; }
		echo "<tr valign=\"middle\">\n";
		echo "<td>" . __("FTP mode") . "</td>\n";
		echo "<td colspan=\"2\">\n";
		echo "<input type=\"radio\" name=\"ftpmode\" value=\"binary\" $ftpmode_binary_checked /> Binary &nbsp <input type=\"radio\" name=\"ftpmode\" value=\"automatic\" $ftpmode_automatic_checked /> Automatic \n";
		echo "</td>\n";
		echo "</tr>\n";
	}
	
// -------------------------------------------------------------------------
// Row 8: login button
// -------------------------------------------------------------------------
// Login button
	echo "<tr valign=\"middle\"><td colspan=\"2\" style=\"text-align: center;\">\n";
	echo "<input type=\"hidden\" name=\"state\"     value=\"browse\" />\n";
	echo "<input type=\"hidden\" name=\"state2\"    value=\"main\" />\n";
	echo "<input type=\"hidden\" name=\"cookieset\" value=\"\" />\n";
	echo "<input type=\"submit\" class=\"button\"   value=\"" . __("Login") . "\" title=\"Login (accesskey l)\" accesskey=\"l\"/>\n";
	echo "</td>\n";
	echo "<td></td>\n";
	echo "</tr>\n";

// -------------------------------------------------------------------------
// Row 9: clear cookies and admin
// -------------------------------------------------------------------------
	
	if ($settings['fielduse_login_clearcookies'] == "yes" || $settings['fielduse_login_admin'] == "yes") {
		echo "<tr valign=\"middle\">\n";
	
// Clear cookies
		echo "<td colspan=\"2\" style=\"text-align: center;\">\n";
		if ($settings['fielduse_login_clearcookies'] == "yes") {
			echo "<span style=\"font-size: 80%;\"><a href=\"javascript:ClearCookies();\">" . __("Clear cookies") . "</a></span>\n";
		}
		echo "</td>\n";
	
// Admin panel
		echo "<td style=\"text-align: right; font-size: 80%;\">\n";
		if ($settings['fielduse_login_admin'] == "yes") {
			echo "<a href=\"$PHP_SELF?state=admin&amp;state2=main\">Admin</a>\n";
		}
		echo "</td>\n";
	
		echo "</tr>\n";
	}
		
	echo "</table>\n";
	echo "</div>\n";
	echo "</form>\n";


// -------------------------------------------------------------------------
// Set focus on the first input textbox
// -------------------------------------------------------------------------
	echo "<script type=\"text/javascript\"><!--\n";
	if   ($net2ftp_allowed_ftpservers[1] == "ALL") { echo "document.LoginForm.ftpserver.focus();\n"; }
	else                                           { echo "document.LoginForm.username.focus();\n"; }
	echo "//--></script>\n";

// -------------------------------------------------------------------------
// End output
// -------------------------------------------------------------------------
	echo "<!-- End of net2ftp login form -->\n";
	echo "\n\n\n\n\n";

} // End function net2ftp_loginform

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************



?>