<?php
/******************************************************************************
	Files(WEBファイル共有PHPスクリプト)
	Files(WEB file share PHP script)

	作成済みの共有フォルダの設定更新処理
	Set update processing of Shared folder

  	modconf.php

	Written by:	Takashi Miyagi

	Copyright (c) 2004-2014 GUSUKU All rights reserved.

	Update:

	Ver.2.6.2
		著作権者変更
		The copyright owner has been changed.

	Ver.2.5
		戻り先URLが正しくセットされなかったので修正した。
		Because return destination URL had not been correctly set, it corrected it. 

		ファイルへの直接リンク用の設定項目を追加した
		I added a setting item for direct links to the file.

	Ver.2.3
		未定義の変数への参照をしないように修正した
		It was corrected that it did not refer to an undefined variable.

	Ver.2.1
		構成ファイル配置設定を追加
		The configuration file arrangement setting is added.

	Ver.2.0
		表示文字列を locale.php から取得するように修正
		The display character string was acquired from locale.php. 

		文字コードの指定も locale.php から取得するように修正
		The specification of the character-code was acquired from locale.php.

		所有者とグループによるアクセス権制御機能を付加
		The access control function by the group is added with the owner. 

		HTML部分に２バイト文字を使用しないように修正した、
		ブラウザの文字コード自動判別に影響しないようにするため
		To make it not misidentified by the character-code automatic operation 
		identification of a browser, it was corrected that Japanese was not 
		used for the HTML part. 

	Ver.1.4 までの更新履歴
	Update history to Ver.1.4
		2005/08/02	ページヘッダー部分を共通化
					A common page header partial function is added. 
******************************************************************************/

require('files_com.php');

$conf = init_files(FALSE);

$scriptname = "modconf.php";

$submit = get_post_get_val("submit");
$reload = get_post_get_val("reload");
$mode = get_post_get_val("mode");
$return_url = get_post_get_val("return_url");
$returl = get_post_get_val("returl");
$config_name = get_post_get_val("config_name");

$s_name = files_session_name();
$s_id = session_id();

$config_dir = make_path(get_conf_top_dir($conf), "", $config_name);

$conf["returl"] = $return_url . "?config=" . $conf["config"];
$conf["returl"] .= "&" . $s_name . "=" . $s_id;

// 設定ファイルを読み込む
//The configuration file is read.
$conf_fname = $config_name . ".conf";
$conf_file = make_path($config_dir, "", $conf_fname);
$old_conf = read_conf_file($conf_file);

?>

<HTML>
<HEAD>
<?php echo get_charset_line(); ?>
<TITLE><?php echo $conf["HEADER_TITLE"] ?></TITLE>
</HEAD>
<BODY <?php echo get_body_bg_str($conf); ?>>

<?php
//ページヘッダーHTMLを取得
//Page header HTML is acquired.
echo get_page_header_HTML($conf, "", FALSE, $conf["SHOW_HELP_LINK"]);
?>

<CENTER>
<HR>

<?php

if ($mode != "send" || $reload != "") {
	//フォルダ設定更新フォーム表示
	//Folder setting update form display
	$title = $old_conf["TITLESTR"];
	if ($title == "") {
		$title = $old_conf["HEADER_TITLE"];
	}
	$max_upload_size = $old_conf["MAX_UPLOAD_SIZE"];
	$max_total_size = $old_conf["MAX_TOTAL_SIZE"];

	$upload_size_unit = 1;
	$upload_size_unit_KB="checked";
	$upload_size_unit_MB="";

	$max_upload_size /= 1000;
	if ($max_upload_size >= 1000 && $max_upload_size % 1000 == 0) {
		$upload_size_unit = 2;
		$upload_size_unit_KB="";
		$upload_size_unit_MB="checked";

		$max_upload_size /= 1000;
	}

	$total_size_unit = 1;
	$total_size_unit_KB="checked";
	$total_size_unit_MB="";
	$total_size_unit_GB="";

	$max_total_size /= 1000;
	if ($max_total_size >= 1000 && $max_total_size % 1000 == 0) {
		$total_size_unit = 2;
		$total_size_unit_KB="";
		$total_size_unit_MB="checked";
		$total_size_unit_GB="";

		$max_total_size /= 1000;

		if ($max_total_size >= 1000 && $max_total_size % 1000 == 0) {
			$total_size_unit = 3;
			$total_size_unit_KB="";
			$total_size_unit_MB="";
			$total_size_unit_GB="checked";

			$max_total_size /= 1000;
		}
	}
?>

<form action="<?php echo $conf["MODCONF_SCRIPT"] ?>" method="post">
	<input type="hidden" name="mode" value="send">
	<input type="hidden" name="config" value="<?php echo $conf["config"] ?>">
	<input type="hidden" name="return_url" value="<?php echo $return_url ?>">
	<input type="hidden" name="config_name" value="<?php echo $config_name ?>">
	<input type="hidden" name="<?php echo $s_name ?>" value="<?php echo $s_id ?>">
	<TABLE border=0><TBODY>
		<TR BGCOLOR="<?php echo $conf["LIST_TITLE_BG"] ?>">
			<TD colspan=2>
				<CENTER><B><?php echo get_txt("update share folder setting"); ?></B></CENTER>
			</TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("config name"); ?></B></TD>
			<TD><?php echo $config_name; ?></TD>
		</TR>
		<TR>
			<TD><B>
				<?php echo get_txt("page title"); ?></B>
			</TD>
			<TD><input name="title" type="text" size=40 maxlength=80
							value="<?php echo $title; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("title image"); ?></B></TD>
			<TD><input name="title_image" type="text" size=40 maxlength=80
							value="<?php echo $old_conf["TITLEIMG"]; ?>">
				&nbsp;W=
				<input name="ptitleimg_w", type="text" size=6 maxlength=4
							value="<?php echo $old_conf["ptitleimg_w"]; ?>">
				&nbsp;H=
				<input name="ptitleimg_h", type="text" size=6 maxlength=4
							value="<?php echo $old_conf["ptitleimg_h"]; ?>">
			</TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("top folder full path"); ?></B></TD>
			<TD><input name="topdir" type="text" size=80 maxlength=128
							value="<?php echo $old_conf["TOPDIR"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("top folder url"); ?></B></TD>
			<TD><input name="topurl" type="text" size=80 maxlength=128
							value="<?php echo $old_conf["TOPURL"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("return url1"); ?></B></TD>
			<TD><input name="returl1" type="text" size=80 maxlength=128
							value="<?php echo $old_conf["RETURL1"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("return url1 title"); ?></B></TD>
			<TD><input name="returl1_title" type="text" size=40 maxlength=80
						value="<?php echo $old_conf["returntitle1"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("return url2"); ?></B></TD>
			<TD><input name="returl2" type="text" size=80 maxlength=128
							value="<?php echo $old_conf["RETURL2"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("return url2 title"); ?></B></TD>
			<TD><input name="returl2_title" type="text" size=40 maxlength=80
						value="<?php echo $old_conf["returntitle2"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("no auth caller url"); ?></B></TD>
			<TD><input name="caller_url" type="text" size=80 maxlength=128
						value="<?php echo $old_conf["CALLER_URL"]; ?>"></TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("max upload size"); ?></B></TD>
			<TD><input name="max_upload_size" type="text" size=14 maxlength=12
					value="<?php echo $max_upload_size; ?>">
				<input name="upload_size_unit" type="radio" value="1"
									<?php echo $upload_size_unit_KB; ?>>KB
				<input name="upload_size_unit" type="radio" value="2"
									<?php echo $upload_size_unit_MB; ?>>MB
			</TD>
		</TR>
		<TR>
			<TD><B><?php echo get_txt("max use capacity"); ?></B></TD>
			<TD><input name="max_total_size" type="text" size=14 maxlength=12
					value="<?php echo $max_total_size; ?>">
				<input name="total_size_unit" type="radio" value="1"
									<?php echo $total_size_unit_KB; ?>>KB
				<input name="total_size_unit" type="radio" value="2"
									<?php echo $total_size_unit_MB; ?>>MB
				<input name="total_size_unit" type="radio" value="3"
									<?php echo $total_size_unit_GB; ?>>GB
			</TD>
		</TR>
		<TR>
			<TD colspan=2>
				<CENTER>
				<input type="submit" name="submit" value="<?php echo get_txt("update"); ?>">
				<input type="submit" name="reload" value="<?php echo get_txt("reset"); ?>">
				</CENTER>
			</TD>
		</TR>
		<TR><TD colspan=2>&nbsp;</TD></TR>
		<TR>
			<TD colspan=2>
				<B><font color="#ff0000">
					<?php echo get_txt("top folder url_caution"); ?>
				</font></B>
			</TD>
		</TR>
	</TBODY></TABLE>
</form>

<?php

} else {
	//フォルダ設定更新処理
	//Folder setting update processing
	$msg = "";
	$sts = TRUE;
	$lock_fp = FALSE;
	$config_file = "";
	$new_conffile = "";
	$new_conf = array();

	if ($submit == "") {
		$sts = FALSE;
	} else {
		// 設定名
		//Set name
		$config_name = get_post_get_val("config_name");
		$topdir = get_post_get_val("topdir");
		$title = get_post_get_val("title");

		if ($sts && $topdir == "") {
			$msg .= get_txt("top folder is not specified") . "<br>\n";
			$sts = FALSE;
			put_log($conf, "Modify folder " . $config_name .
											" failure : no top folder path");
		}
		if ($sts && $title == "") {
			$msg .= get_txt("title is not specified") . "<br>\n";
			$sts = FALSE;
			put_log($conf, "Modify folder " . $config_name .
											" failure : no title");
		}
		if ($sts) {
			$topurl = get_post_get_val("topurl");
			$title_image = get_post_get_val("title_image");
			$ptitleimg_w = get_post_get_val("ptitleimg_w");
			$ptitleimg_h = get_post_get_val("ptitleimg_h");
			$returl1 = get_post_get_val("returl1");
			$returl1_title = get_post_get_val("returl1_title");
			$returl2 = get_post_get_val("returl2");
			$returl2_title = get_post_get_val("returl2_title");
			$caller_url = get_post_get_val("caller_url");
			$max_upload_size = get_post_get_val("max_upload_size");
			$max_total_size = get_post_get_val("max_total_size");

			$upload_size_unit = get_post_get_val("upload_size_unit");
			$total_size_unit = get_post_get_val("total_size_unit");

			//指定の単位からバイト単位に変換する
			if ($upload_size_unit == 1) {
				$max_upload_size *= 1000;
			} else if ($upload_size_unit == 2) {
				$max_upload_size *= (1000 * 1000);
			}
			//指定の単位からバイト単位に変換する
			if ($total_size_unit == 1) {
				$max_total_size *= 1000;
			} else if ($total_size_unit == 2) {
				$max_total_size *= (1000 * 1000);
			} else if ($total_size_unit == 3) {
				$max_total_size *= (1000 * 1000 * 1000);
			}

			// 設定内容を新しい設定にコピーする
			//A set content is copied onto a new setting.
			$new_conf = $old_conf;

			$new_conf["TOPDIR"] = $topdir;

			$new_conf["TOPURL"] = $topurl;

			$new_conf["HEADER_TITLE"] = $title;
			$new_conf["TITLESTR"] = $title;
			$new_conf["TITLEIMG"] = $title_image;

			$new_conf["ptitleimg_w"] = $ptitleimg_w;
			$new_conf["ptitleimg_h"] = $ptitleimg_h;

			$new_conf["RETURL1"] = $returl1;
			$new_conf["returntitle1"] = $returl1_title;
			$new_conf["RETURL2"] = $returl2;
			$new_conf["returntitle2"] = $returl2_title;

			$new_conf["CALLER_URL"] = $caller_url;

			$new_conf["MAX_UPLOAD_SIZE"] = $max_upload_size;
			$new_conf["MAX_TOTAL_SIZE"] = $max_total_size;

			//$new_conf["CUTDIR"] = $new_conf["TOPDIR"] . "/.cutbuf";
			$new_conf["CUTDIR"] = ".cutbuf";
		}
		//ロックする
		//It locks.
		$lockfile = make_path($config_dir, "", $conf["LOCKFILE"]);
		$lock_fp = lock_files($lockfile);
		if (! $lock_fp) {
			$msg .= get_txt("cannot lock") . "<br>\n";
			$sts = FALSE;
			put_log($conf, "Modify folder " . $config_name .
									" failure : can not lock config folder");
		}
		$new_conffile = $conf_file . ".new";
		if ($sts) {
			$sts = write_conf_file($old_conf, $new_conf, $new_conffile);
			if (!$sts) {
				$msg .= get_txt("cannot create config file") . "<br>\n";
				put_log($conf, "Modify folder " . $config_name .
								" failure : can not create new config file");
			}
			chmod($new_conffile, get_conf_file_attr($conf));
		}
		if ($sts) {
			rename($conf_file, $conf_file . ".old");
		}
		if ($sts) {
			$sts = rename($new_conffile, $conf_file);
			if (!$sts) {
				$msg .= get_txt("cannot update config file") . "<br>\n";
				put_log($conf, "Modify folder " . $config_name .
								" failure : can not rename new config file");
			}
		}
		if ($sts) {
			if ($new_conf["TOPDIR"] != $old_conf["TOPDIR"]) {
				// 先頭ディレクトリを移動またはコピーする
				//The first directory is moved or is copied.
				$sts = rename($old_conf["TOPDIR"], $new_conf["TOPDIR"]);
				if (!$sts) {
					// 移動できないのでコピーする
					//Because it is not possible to move, it copies it.
					$sts = copy_dir($old_conf["TOPDIR"], $new_conf["TOPDIR"],
													get_data_dir_attr($conf));
					if ($sts) {
						delete_dir($old_conf["TOPDIR"]);
					}
				}
				if (!$sts) {
					put_log($conf, "Modify folder " . $config_name .
									" failure : can not move data folder");
				}
			}
		}
		//ロックを解除する
		//The lock is released.
		unlock_files($lock_fp, $lockfile);

		if ($sts) {
			put_log($conf, "Modify folder " . $config_name . " success");

			//ファイル一覧にリダイレクトする
			//echo get_redirect_files_JavaScriptHTML($url);

			//呼び出し元にリダイレクトする
			//$url = $return_url . "?config=" . conv_url_code($conf["config"]);
			//$url .= "&returl=" . $conf["returl"];
			$url = $conf["returl"];

			echo get_redirect_url_JavaScriptHTML($url);
			exit;
		}
	}
	if (! $sts) {
		echo $msg;
	}
}
?>

<HR>
<?php

//戻り先１と戻り先２へのリンクを表示するHTMLを生成
//Link HTML to return 1 and return 2 destination is generated.
echo get_return_url_HTML($conf);

//ページフッター（プログラム・バージョンとCopyright表示）
//Page footer(program version and Copyright display)
echo get_page_footer_HTML($conf);

?>
</CENTER>
</BODY>
</HTML>
