<?php

/*
 *	W-AGORA 4.2
 *	-----------
 *	$Id: editmail.php,v 1.20 2005/12/31 09:43:34 mdruilhe Exp $
 *	Usage:		Edit mail template
 *	Author:		Marc Druilhe <mdruilhe@w-agora.net>
 *
 */

	require ("init_admin.inc");


	function setTitle ($title, $anchor="") {
		if (empty($anchor) ) {
			$anchor=$title;
		}
		print "</TABLE><br><A NAME=\"$anchor\"></A><TABLE border=0 cellspacing=2 cellpadding=4 WIDTH='100%'>";
		echo "<TR><TH COLSPAN=3 BGCOLOR=\"#E0E0E0\" ALIGN=\"left\">$title</TH></TR>\n";
	}


# save headers
# -------------
	$headers_save = compact("bn_send_user_address", "bn_mail_from", "bn_return_path", "bn_reply_to", "bn_mail_to");	// Save headers 


# Only a site administrator can edit layout
# -----------------------------------------
	if ($auth->level < ADMIN) {
		MsgForm (ERROR_NO_ADMIN, "admin_site.$ext?site=$site&force_login=1", "", "_top");
		echo "</body></html>";
		exit;
	}

	function display_var($var) {
		$var = ereg_replace("\\$"."([a-zA-Z0-9_]+)", "{\\1}", $var);
		return htmlSpecialChars ($var);
	}

# reads configuration
# -------------------
	require ("$site_cfg_file");
	require_once ("$cfg_file");

# set pathname of file to be edited 
# ----------------------------------
	$page_title = TITLE_EDITING_MAIL;
	$page_info = '';
	if ($editdefault==1) {
		$var_file = "$bn_dir_default/mail_template.$ext";
		$page_info = WARNING_EDITING_DEFAULT;
	} elseif (empty ($bn_mail_template) ) {
		if ($editsite==1) {
			$var_file="$bn_dir/mail_template.$ext"; // default file for site listing
		} else {
			$var_file = "$bn_dir_default/mail_template.$ext";
			$page_info = WARNING_EDITING_DEFAULT;
			if (!empty($bn) ) {
				$page_info .= "<br>" . sprintf(LABEL_SPECIFIC_TEMPLATE, "editconf.$ext?site=$site&bn=$bn&mode=advanced#include");
			}
		}
	} else {
		$var_file="$bn_dir/$bn_mail_template";
	}

# restore headers
# ----------------
	extract ($headers_save);		


# Save file if submit has been sent
# ---------------------------------
	if (isset($_POST['save'])) {
		$conf = new Config;
		$ret = $conf->open($var_file);
		if (!$ret) {
			die (str_replace("{FILE}", $var_file, ERROR_OPEN_FILE));
		}		

		$conf->writeInt("bn_send_user_address", $_POST['bn_send_user_address']);
		$conf->writeString("bn_mail_from", strip_magic_quotes($_POST['bn_mail_from']));
		$conf->writeString("bn_return_path", strip_magic_quotes($_POST['bn_return_path']));
		$conf->writeString("bn_reply_to", strip_magic_quotes($_POST['bn_reply_to']));
		$conf->writeString("bn_mail_to", strip_magic_quotes($_POST['bn_mail_to']));
		$conf->writeString("bn_before_mail",strip_magic_quotes($_POST['bn_before_mail']));
		$conf->writeString("mail_no_subject", strip_magic_quotes($_POST['mail_no_subject']));
		$conf->writeString("mail_subject", strip_magic_quotes($_POST['mail_subject']));
		$conf->writeString("mail_newpost_body", strip_magic_quotes($_POST['mail_newpost_body']));
		$conf->writeString("mail_update_body", strip_magic_quotes($_POST['mail_update_body']));
		$conf->writeString("mail_register_subject", strip_magic_quotes($_POST['mail_register_subject']));
		$conf->writeString("mail_register_body", strip_magic_quotes($_POST['mail_register_body']));
		$conf->writeString("mail_confirm_reg_subject", strip_magic_quotes($_POST['mail_confirm_reg_subject']));
		$conf->writeString("mail_confirm_reg_body", strip_magic_quotes($_POST['mail_confirm_reg_body']));
		$conf->writeString("mail_approve_user_subject", strip_magic_quotes($_POST['mail_approve_user_subject']));
		$conf->writeString("mail_approve_user_body", strip_magic_quotes($_POST['mail_approve_user_body']));
		$conf->writeString("mail_approve_body", strip_magic_quotes($_POST['mail_approve_body']));
		$conf->writeString("mail_password_subject", strip_magic_quotes($_POST['mail_password_subject']));
		$conf->writeString("mail_password_body", strip_magic_quotes($_POST['mail_password_body']));
		$conf->writeString("mail_change_profile_subject", strip_magic_quotes($_POST['mail_change_profile_subject']));
		$conf->writeString("mail_change_profile_body", strip_magic_quotes($_POST['mail_change_profile_body']));
		$conf->close();

# Else : Restore file if UNDO has been requested
# ---------------------------------------
	} elseif (isset($undo)) {
		if (file_exists ("$var_file.bak.$ext"))
			Copy ("$var_file.bak.$ext", "$var_file");
	}


# Now reads the file (PHP variables)
# ----------------------------------
	if (file_exists ($var_file)) {
		 if (isset($choose_lang) && !empty($mail_file)) {	// RESET mail template with language mail template
		     if (file_exists ("$bn_dir_default/$mail_file")) {
					@copy ("$bn_dir_default/$mail_file", "$var_file");
			 }
		 }		 
		include "$var_file";
		$form_title = "<em>" . LABEL_FILE_EDITING . ": $var_file </em>";
	} else {	// File doesn't exist: copy default file	
		if (file_exists ("$bn_dir_default/mail_template.$ext")) {
			@copy ("$bn_dir_default/mail_template.$ext", "$var_file");
			include "$var_file";
		}		
		$form_title = "<em>" . LABEL_NEW_FILE_EDITING . ": $var_file </em>";
	}

	$bn_send_user_address = (empty ($bn_send_user_address) ) ? "0" : "1";
	$bn_mail_from = (empty ($bn_mail_from) ) ? "forums@localhost" : $bn_mail_from;

	$mail_temp_dir = opendir($bn_dir_default);
	$mail_files['0'] = LABEL_FORM_SELECT;
	while ($mail_f = readdir($mail_temp_dir)) {
		$pt_split = explode(".", $mail_f);
		if (substr($mail_f, 0, 14) == 'mail_template_' && count($pt_split) == 2 && $pt_split[1] == $ext) {
		    $mail_files[$mail_f] = $mail_f;
		}
	}

# Displays the FORM
# -----------------
	
	display_header ($page_title);
	echo "<center>$page_info</center><br>";
	table_header ($form_title);
	beginForm();
	hiddenField ('editdefault', $editdefault);
	
		
	
	print "<CENTER><TABLE WIDTH=\"100%\" cellpadding=5>";
	
	print "<TR><TD align='right' valign=top class=label>" . LABEL_SEND_OUT_EMAILS . ":</TD><TD class=label>";
	radioButton ("bn_send_user_address", "1", $bn_send_user_address, LABEL_USER_EMAIL . "<br>");
	radioButton ("bn_send_user_address", "0", $bn_send_user_address, LABEL_THIS_EMAIL . ":<br>");
	textField ("bn_mail_from", display_var($bn_mail_from), 60);
	print "</blockquote></TD></TR>\n";

	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_FIRST_RECIPIENT . ":</TD><TD>";
	textField ("bn_mail_to", display_var($bn_mail_to), 60);
	print "</TD></TR>\n";

	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_RETURN_PATH . ":</TD><TD>";
	textField ("bn_return_path", display_var($bn_return_path), 60);
	print "</TD></TR>\n";
	
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_REPLY_TO . ":</TD><TD>";
	textField ("bn_reply_to", display_var($bn_reply_to), 60);
	print "</TD></TR>\n";
	print "<TR><TD colspan=2><hr noshade>\n";

	print "<TABLE border=0 cellspacing=2 cellpadding=4 WIDTH='100%'>";
	print "<TR><TH COLSPAN=3 BGCOLOR=\"#E0E0E0\" ALIGN=\"center\">" . LABEL_OVERWRITE_MAIL_FILE .  ":<br>" . getListBox("mail_file", $mail_files, '0') . "&nbsp;&nbsp;" . getSubmitButton('choose_lang', BUTTON_SUBMIT) . "</TH></TR>\n";

	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_BEFORE_MAIL . ":</TD><TD>";
	textArea ('bn_before_mail', htmlspecialchars ($bn_before_mail), 6, 60);
	print "</TD></TR>\n";

	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD>";
	textField ("mail_subject", display_var($mail_subject), 60);
	print "</TD></TR>\n";

	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_NO_MAIL_SUBJECT . ":</TD><TD>";
	textField ("mail_no_subject", display_var($mail_no_subject), 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_POSTED);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_BODY. ":</TD><TD>";
	textArea ("mail_newpost_body", display_var($mail_newpost_body), 6, 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_MODIFIED);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_BODY . ":</TD><TD class=label>";
	textArea ("mail_update_body", display_var($mail_update_body), 6, 60);
	print "</TD></TR>\n";
	
	setTitle(LABEL_MAIL_APPROVAL);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_approve_body", display_var($mail_approve_body), 6, 60);
	print "</TD></TR><TR><TD colspan=3><hr noshade></TD></TR>\n";

	setTitle(LABEL_MAIL_REGISTRATION);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD class=label> ";
	textField ("mail_register_subject", display_var($mail_register_subject), 60);
	print "</TD></TR><TR><TD width=\"40%\" align='right' valign=top class=label><BR>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_register_body", display_var($mail_register_body), 6, 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_CONFIRM_REGISTRATION);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD class=label> ";
	textField ("mail_confirm_reg_subject", display_var($mail_confirm_reg_subject), 60);
	print "</TD></TR><TR><TD width=\"40%\" align='right' valign=top class=label><BR>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_confirm_reg_body", display_var($mail_confirm_reg_body), 6, 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_USER_APPROVAL);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD class=label> ";
	textField ("mail_approve_user_subject", display_var($mail_approve_user_subject), 60);
	print "</TD></TR><TR><TD width=\"40%\" align='right' valign=top class=label><BR>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_approve_user_body", display_var($mail_approve_user_body), 6, 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_USER_MODIFICATION);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD class=label> ";
	textField ("mail_change_profile_subject", display_var($mail_change_profile_subject), 60);
	print "</TD></TR><TR><TD width=\"40%\" align='right' valign=top class=label><BR>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_change_profile_body", display_var($mail_change_profile_body), 6, 60);
	print "</TD></TR>\n";

	setTitle(LABEL_MAIL_FORGOT_PASSWORD);
	print "<TR><TD width=\"40%\" align='right' valign=top class=label>" . LABEL_MAIL_SUBJECT . ":</TD><TD class=label> ";
	textField ("mail_password_subject", display_var($mail_password_subject), 60);
	print "</TD></TR><TR><TD width=\"40%\" align='right' valign=top class=label><BR>" . LABEL_MAIL_BODY . ":</TD><TD class=label> ";
	textArea ("mail_password_body", display_var($mail_password_body), 6, 60);
	print "</TD></TR>\n";

	print "</TABLE><CENTER>";
	submitButton ("save", LABEL_SAVE);
	resetButton ("reset", LABEL_RESET);
	submitButton ("undo", LABEL_UNDO);
	print "</CENTER>";
	endForm();
	table_footer();

?>
