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

	構成ファイル配置設定処理
	Configuration file placement setting

  	setconf.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.1
		新規作成
		New making

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

require('files_com.php');

$conf = init_files(FALSE);

$scriptname = "setconf.php";

$submit = get_post_get_val("submit");
$mode = get_post_get_val("mode");
$return_url = get_post_get_val("return_url");

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

$script_dir = ".";

$top_dir = $conf["TOPDIR"];
$conftop = get_conf_top_dir($conf);

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

$new_conf = $conf;

?>

<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") {
	//ディレクトリ配置設定フォーム表示
	//Directory placement setting form indication

	//設定ファイル用ディレクトリ
	//directory for config file
	$config_top_dir = get_conf_top_dir($conf);
	$entry_page_top_dir = get_entry_page_top_dir($conf);
	$entry_page_top_url = get_entry_page_top_url($conf);

?>

<form action="<?php echo $scriptname; ?>" 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="<?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("config file placement setting"); ?>
				</B></CENTER>
			</TD>
		</TR>
		<TR>
			<TD><font color="#ff0000"><B>
					<?php echo get_txt("folder for config file"); ?>
				</font></B></TD>
			<TD><input name="new_config_file_dir" type="text" size=80
				maxlength=128 value="<?php echo $config_top_dir; ?>">
			</TD>
		</TR>
		<TR>
			<TD><B>
				<?php echo get_txt("folder for entry pages"); ?>
				</font></B></TD>
			<TD><input name="new_entry_page_dir" type="text" size=80 
				maxlength=128 value="<?php echo $entry_page_top_dir ?>"></TD>
		</TR>
		<TR>
			<TD><B>
				<?php echo get_txt("url of folder for entry pages"); ?>
				</font></B></TD>
			<TD><input name="new_entry_page_url" type="text" size=80 
				maxlength=128 value="<?php echo $entry_page_top_url ?>"></TD>
		</TR>
		<TR>
			<TD colspan=2>
				<CENTER>
				<input type="submit" name="submit"
								value="<?php echo get_txt("exec"); ?>">
				<input type="reset" value="<?php echo get_txt("reset"); ?>">
				</CENTER>
			</TD>
		</TR>
	</TBODY></TABLE>
</form>

<?php

} else {
	//フォルダ作成処理
	//Folder making processing
	$msg = "";
	$sts = TRUE;
	$lock_fp = FALSE;

	if ($submit == "") {
		$sts = FALSE;
	} else {
		// 設定ファイル名
		//Configuration file name
		$config_file = "files.conf";

		// パスワードファイル名
		//Password file name
		$passwd_file =  get_filename($conf["PASSWD_FILE"]);

		// 設定ディレクトリ
		//Set directory
		$config_top_dir = get_conf_top_dir($conf);

		$old_entry_dir = get_entry_page_top_dir($conf);

		if (!file_exists($old_entry_dir)) {
			$old_entry_dir = $conf["CONFIG_TOP_DIR"];
		}

		$old_config_file = $conf["config"];

		// パスワードファイルのフルパス
		//Full path of password file
		$old_passwd_file = $conf["PASSWD_FILE"];

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

		//設定名
		//Set name
		$new_conf_dir = get_post_get_val("new_config_file_dir");
		$new_entry_dir = get_post_get_val("new_entry_page_dir");
		$new_entry_url = get_post_get_val("new_entry_page_url");

		if ($new_conf_dir == "") {
			$msg .= get_txt("config folder is not specified") . "<br>\n";
			$sts = FALSE;
			put_log($conf, "Set config folder failure: no config folder path");
		}
		if ($new_entry_dir == "") {
			$new_entry_dir = $conf["ENTRY_PAGE_TOP_DIR"];
		}
		if ($new_entry_url == "") {
			$new_entry_url = $conf["ENTRY_PAGE_TOP_URL"];
		}
		$new_config_file = "";

		if ($sts) {
			// 設定ファイルのフルパス
			//Full path of configuration file
			$new_config_file = make_path($new_conf_dir, "", $config_file);

			// パスワードファイルのフルパス
			//Full path of password file
			$new_passwd_file = make_path($new_conf_dir, "", $passwd_file);

			$new_conf["PASSWD_FILE"] = $new_passwd_file;
			$new_conf["CONFIG_TOP_DIR"] = $new_conf_dir;
			$new_conf["ENTRY_PAGE_TOP_DIR"] = $new_entry_dir;
			$new_conf["ENTRY_PAGE_TOP_URL"] = $new_entry_url;
			$new_conf["config"] = $new_config_file;

			//Make config directory

			// 設定ディレクトリの有無を確認
			//The presence of a set directory is confirmed.
			if (!file_exists($new_conf_dir)) {
				//無ければディレクトリ作成
				//It makes it the directory in case of not being.
				$sts = make_dir($new_conf_dir, get_conf_dir_attr($conf));
				if (!$sts) {
					$msg .= get_txt("cannot make config folder") . "<br>\n";
					put_log($conf, "Create new folder " . $new_conf_dir .
								" failure : can not create config folder");
				}
			} else {
				chmod($new_conf_dir, get_conf_dir_attr($conf));
			}
			//Make entry page directory
			if ($sts) {
				// エントリー・ページ用ディレクトリの有無を確認
				//The presence of a directory of entry page files is confirmed.
				if (!file_exists($new_entry_dir)) {
					//無ければディレクトリ作成
					//It makes it the directory in case of not being.
					$sts = make_dir($new_entry_dir,
										get_conf_dir_attr($conf));	
					if (!$sts) {
						$msg .= get_txt("cannot make config folder") . "<br>\n";
						put_log($conf, "Create new folder " . $new_entry_dir .
									" failure : can not create entry page folder");
					}
				} else {
					chmod($new_entry_dir, get_conf_dir_attr($conf));
				}
			}
		}
		//Write new config file to new config directory
		if ($sts) {
			$sts = write_conf_file($conf, $new_conf, $new_config_file);
			if (!$sts) {
				$msg .= get_txt("cannot create config file") . "<br>\n";
				put_log($conf, "Create new config file " . $new_conffile .
								" failure : can not create config file");
			} else {
				chmod($new_config_file, get_conf_file_attr($conf));
			}
		}
		//Copy password file to new config directory
		if ($sts) {
			if (file_exists($old_passwd_file)) {
				if (strcasecmp(realpath($old_passwd_file),
								realpath($new_passwd_file)) != 0) {
					$sts = copy($old_passwd_file, $new_passwd_file);
					if ($sts) {
						chmod($new_passwd_file, get_pwd_file_attr($conf));
					} else {
						$msg .= get_txt("cannot create password file") . "<br>\n";
						put_log($conf, "Create new password file " . 
									$new_passwd_file .
									" failure : can not create password file");
					}
				}
			}
		}
		if ($sts) {
			// 設定ファイル用のディレクトリ内のファイルをサーチする
			//It searches for the file in the directory for the configuration file.
			if ($dh1 = @opendir($config_top_dir)) {
				while (($name = readdir($dh1)) !== false) {
					// フォルダ設定ディレクトリか（ディレクトリ名が名称になる）
					//Is it a folder setting directory?
					//(The directory name becomes a name. )
					if ($name == "." || $name == "..") {
						continue;
					}
					// フォルダ設定ディレクトリから設定ファイルを見つける
					//The configuration file is found from the folder
					//setting directory.
					$confdir = make_path($config_top_dir, "", $name);
					if (!is_dir($confdir)) {
						// ディレクトリ以外は対象ではない
						//It is not an object excluding the directory.
						continue;
					}
					if ($dh2 = @opendir($confdir)) {
						while (($fname = readdir($dh2)) !== false) {
							// 拡張子が conf のファイルを探す
							//The extension looks for the file of conf.
							$ext = substr($fname, strrpos($fname, '.') + 1);
							if ($ext == "conf") {
								// 設定ファイルを見つけた
								//The configuration file was found.

								// 設定内容を読み込む
								//A set content is read.
								$conf_file = make_path($confdir, "", $fname);

								$cf = read_conf_file($conf_file);

								$new_confdir = make_path($new_conf_dir, "", $name);
								if (!file_exists($new_confdir)) {
									$sts = make_dir($new_confdir,
													get_conf_dir_attr($conf));
									if (!$sts) {
										$msg .= get_txt("cannot make config folder") . "<br>\n";
										put_log($conf, "Make config folder failure :" .
													" can not make " . $new_confdir);
										break;
									}
								} else {
									if (!is_dir($new_confdir)) {
										$msg .= get_txt("cannot copy config folder") . "<br>\n";
										put_log($conf, "Config folder copy failure :" .
													$new_confdir . " is already exist");
										
										$sts = FALSE;
										break;
									}
								}
								//パスワードファイルをコピーする
								//The password file is copied.

								$pwdfile = $cf["PASSWD_FILE"];
								$new_pwdfile = make_path($new_confdir, "", $passwd_file);
								if (file_exists($pwdfile)) {
									if (strcasecmp(realpath($pwdfile),
												realpath($new_pwdfile)) == 0) {
										$sts = TRUE;
									} else {
										$sts = copy($pwdfile, $new_pwdfile);
									}
									if (!$sts) {
										$msg .= get_txt("cannot create password file") . "<br>\n";
										put_log($conf, "Make config folder failure :" .
													" can not copy " . $pwdfile);
										break;
									}
								}
								$cf["CONFIG_TOP_DIR"] = $new_conf_dir;
								$cf["PASSWD_FILE"] = $new_pwdfile;
								$cf["ENTRY_PAGE_TOP_DIR"] = $new_entry_dir;
								$cf["ENTRY_PAGE_TOP_URL"] = $new_entry_url;

								$new_conffile = make_path($new_confdir, "", $fname);
								$sts = write_conf_file($new_conf, $cf, $new_conffile);
								if (!$sts) {
									$msg .= get_txt("cannot create config file") . "<br>\n";
									put_log($conf, "Config folder copy failure :" .
													"write error " . $new_conffile);
									break;
								}
								if (realpath($new_conffile) != realpath($conf_file)) {
									unlink($conf_file);
								}
								if (realpath($pwdfile) != realpath($new_pwdfile)) {
									unlink($pwdfile);
								}
							}
						}
						closedir($dh2);
						if (!$sts) {
							break;
						}
					}
				}
				closedir($dh1);
			}
		}
		if ($sts) {
			// エントリー・ページ用のディレクトリ内のファイルをサーチする
			//It searches for the file in the directory for the entry page file.
			if ($dh1 = @opendir($old_entry_dir)) {
				while (($name = readdir($dh1)) !== false) {
					// フォルダ設定ディレクトリか（ディレクトリ名が名称になる）
					//Is it a folder setting directory?
					//(The directory name becomes a name. )
					if ($name == "." || $name == "..") {
						continue;
					}
					//エントリー・ページ用のフォルダがあるか
					//Is there a folder for the entry page?
					$entry_dir = make_path($old_entry_dir, "", $name);
					if (!file_exists($entry_dir) || !is_dir($entry_dir)) {
						// ディレクトリ以外は対象ではない
						//It is not an object excluding the directory.
						continue;
					}
					//エントリー・ページ・ファイルはあるか
					//Is there entry page file?
					$entry_page = make_path($entry_dir, "", "index.php");
					if (!file_exists($entry_page)) {
						continue;
					}
					//新しいエントリー・ページ用のフォルダを作成する
					//The folder for a new entry page is made. 
					$new_entrydir = make_path($new_entry_dir, "", $name);
					if (!file_exists($new_entrydir)) {
						$sts = make_dir($new_entrydir, get_conf_dir_attr($conf));
						if (!$sts) {
							$msg .= get_txt("cannot copy index.php") . "<br>\n";
							put_log($conf, "Make entry page folder failure :" .
										" can not make " . $new_entrydir);
							break;
						}
					} else {
						if (!is_dir($new_entrydir)) {
							$msg .= get_txt("cannot copy index.php") . "<br>\n";
							put_log($conf, "Entry folder copy failure :" .
										$new_entrydir . " is already exist");
							
							$sts = FALSE;
							break;
						}
					}
					//エントリー・ページ・ファイルを作成する
					//The entry page file is made.
					$new_entry_page = make_path($new_entrydir, "", "index.php");
					$new_entry_page_tmp = $new_entry_page . ".tmp";
					
					$fname = $name . ".conf";
					$new_confdir = make_path($new_conf_dir, "", $name);
					$new_conffile = make_path($new_confdir, "", $fname);

					$srchstr = "config=";
					$replace = "\$url = \"" . $conf["SCRIPT_DIR_URL"];
					$replace .= "/" . $conf["LOGIN_SCRIPT"];
					$replace .= "?config=" . conv_url_code($new_conffile) . "\";";

					$sts = replace_line_file_copy($entry_page, $new_entry_page_tmp, 
														$srchstr, $replace);
					if (!$sts) {
						$msg .= get_txt("cannot copy index.php") . "<br>\n";
						put_log($conf, "Change entry page folder failure :" .
									" can not copy " . $entry_page .
									" to " . $new_entry_page_tmp);
						break;
					}
					rename($new_entry_page_tmp, $new_entry_page);
					chmod($new_entry_page, get_conf_file_attr($conf));

					//Delete old entry page
					if (realpath($new_entry_page) != realpath($entry_page)) {
						unlink($entry_page);
					}
				}
				closedir($dh1);
			}
		}
		if ($sts) {
			$old_entry_page = make_path($script_dir, "", "index.php");
			if (file_exists($old_entry_page)) {
				$new_entry_page = make_path($script_dir, "", "index.new");
				$srchstr = "config=";
				$replace = "\$url = \"" . $conf["SCRIPT_DIR_URL"];
				$replace .= "/" . $conf["LOGIN_SCRIPT"];
				$replace .= "?config=" . conv_url_code($new_config_file) . "\";";

				$sts = replace_line_file_copy($old_entry_page, $new_entry_page, 
													$srchstr, $replace);
				if (!$sts) {
					$msg .= get_txt("cannot copy index.php") . "<br>\n";
					put_log($conf, "Change entry page folder failure :" .
								" can not copy " . $old_entry_page .
								" to " . $new_entry_page);
				} else {
					rename($old_entry_page, $old_entry_page . ".old");

					$sts = rename($new_entry_page, $old_entry_page);
					if (!$sts) {
						put_log($conf, "Change entry page folder " . $new_entry_page .
										" failure : can not rename new config file");
					} else {
						chmod($old_entry_page, get_conf_file_attr($conf));
					}
				}
			}
		}
		if ($sts) {
			put_log($conf, "Change config folder " . $new_conf_dir . " success");

			if (realpath($config_top_dir) != realpath($new_conf_dir) &&
						realpath($config_top_dir) != realpath($new_entry_dir)) {
				$dh = opendir($config_top_dir);
				while (($fname = readdir($dh)) !== false) {
					if (strncmp($fname, ".", 2) != 0 && strncmp($fname, "..", 3) != 0) {
						$file_path = make_path($config_top_dir, "", $fname);
						if (is_dir($file_path)) {
							$sts = delete_dir($file_path);
						}
						if (! $sts) {
							break;
						}
					}
				}
				closedir($dh);
			}
			if (realpath($old_entry_dir) != realpath($new_entry_dir) &&
						realpath($old_entry_dir) != realpath($new_conf_dir)) {
				$dh = opendir($old_entry_dir);
				while (($fname = readdir($dh)) !== false) {
					if (strncmp($fname, ".", 2) != 0 && strncmp($fname, "..", 3) != 0) {
						$file_path = make_path($old_entry_dir, "", $fname);
						if (is_dir($file_path)) {
							$sts = delete_dir($file_path);
						}
						if (! $sts) {
							break;
						}
					}
				}
				closedir($dh);
			}
			if (realpath($old_config_file) != realpath($new_config_file)) {
				unlink($old_config_file);
			}
			if (realpath($old_passwd_file) != realpath($new_passwd_file)) {
				unlink($old_passwd_file);
			}

			set_session_value("CHANGE_CONFIG_FILE", "TRUE");

			//呼び出し元にリダイレクトする
			//It redirects it to the call origin.
			$url = $return_url . "?config=" . conv_url_code($new_conf["config"]);
			$url .= "&returl=" . $new_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($new_conf);

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

?>

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