<?php
$template = <<<END
<!-- block_login -->
END;

$block_login = array(
	"title" => "ログイン",
	"name" => "block_login",		// 名称、重複すると正しく動作しません
	"template" => $template,		// ブロックのテンプレート
	"input" => array(
		"help" => array(
			"title" => "使い方",
			"type" => "comment",
			"link" => DOCUMENT1 . "/?act=block_login",
			"class" => "notice",
		),
	),
	"func" => "func_login",
);

block_add($block_login);

function func_login($def, $info)
{
	if (!function_exists("logwrite")) {
		include("log.php");
	}
	$common = get_setup(INFO_COMMON);
	$reject_url = "./";		// トップページ
	if (!isset($_SESSION["LOGIN"])) {	// 管理者ではない
		if (!isset($_SERVER['PHP_AUTH_USER'])) {	// ログイン済みではない
			// 利用不可
			$_SESSION["LOGIN_URI"] = $_SERVER["REQUEST_URI"];
		    header("WWW-Authenticate: Basic realm=\"My Realm\"");
		    header("HTTP/1.0 401 Unauthorized");
		    //
		    echo "キャンセルしました\n";
		    exit;
		} else {
			if ($common["login_id"] && ($_SERVER['PHP_AUTH_USER'] == $common["login_id"]) && ($_SERVER['PHP_AUTH_PW'] == $common["passwd"])) {
				// OK
				if (function_exists("logwritecsv")) {
					logwritecsv(array($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $_SERVER["REMOTE_ADDR"]));
				}
			} else if (($common["login_id"] == "") && ($_SERVER['PHP_AUTH_PW'] == $common["passwd"])) {
				// OK
				if (function_exists("logwritecsv")) {
					logwritecsv(array($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $_SERVER["REMOTE_ADDR"]));
				}
			} else {
				// Error
				header("HTTP/1.0 401 Unauthorized");
				header("location: ./reject.html");
			    exit;
			}
		}
	}
	return $info;
}
