<?php
if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
// check if the user is authorised
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
	include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
	function show_pref(){
		global $xoopsConfig, $xoopsUser;
		$sitename_text = new XoopsFormText(_MD_AM_SITENAME, "sitename", 50, 100, $xoopsConfig['sitename']);
		$slogan_text = new XoopsFormText(_MD_AM_SLOGAN, "slogan", 50, 100, $xoopsConfig['slogan']);
		$amail = !empty($xoopsConfig['adminmail']) ? $xoopsConfig['adminmail'] : $xoopsUser->getVar("email", "E");
		$adminmail_text = new XoopsFormText(_MD_AM_ADMINML, "adminmail", 50, 100, $amail);
		$lang_select = new XoopsFormSelectLang(_MD_AM_LANGUAGE, "language", $xoopsConfig['language']);
		$mod_select = new XoopsFormSelect(_MD_AM_STARTPAGE, "startpage", $xoopsConfig['startpage']);
		$moduleslist = XoopsModule::getHasMainModulesList(true);
		$moduleslist["--"] = _MD_AM_NONE;
		$mod_select->addOptionArray($moduleslist);
		$servertz_select = new XoopsFormSelectTimezone(_MD_AM_SERVERTZ, "server_TZ", $xoopsConfig['server_TZ']);
		$defaulttz_select = new XoopsFormSelectTimezone(_MD_AM_DEFAULTTZ, "default_TZ", $xoopsConfig['default_TZ']);
		$theme_select = new XoopsFormSelectTheme("", "default_theme", $xoopsConfig['default_theme']);
		$theme_cbox = new XoopsFormCheckBox("", "changeusers");
		$theme_cbox->addOption(1, _MD_AM_CHNGUTHEME);
		$theme_tray = new XoopsFormElementTray(_MD_AM_DTHEME, "&nbsp;");
		$theme_tray->addElement($theme_select);
		$theme_tray->addElement($theme_cbox);
		$allowtheme_radio = new XoopsFormRadioYN(_MD_AM_ALLOWTHEME, "allow_theme", $xoopsConfig['allow_theme'], _MD_AM_YES,_MD_AM_NO);
		$anon_text = new XoopsFormText(_MD_AM_ANONNAME, "anonymous", 50, 100, $xoopsConfig['anonymous']);
		$minnpass_text = new XoopsFormText(_MD_AM_MINPASS, "minpass", 10, 10, $xoopsConfig['minpass']);
		$anonpost_radio = new XoopsFormRadioYN(_MD_AM_ANONPOST, "anonpost", $xoopsConfig['anonpost'], _MD_AM_YES,_MD_AM_NO);
		//$allowsig_radio = new XoopsFormRadioYN(_MD_AM_ALLOWSIG, "allow_sig", $xoopsConfig['allow_sig'], _MD_AM_YES,_MD_AM_NO);
		$allowhtml_radio = new XoopsFormRadioYN(_MD_AM_ALLOWHTML, "allow_html", $xoopsConfig['allow_html'], _MD_AM_YES,_MD_AM_NO);
		$newuser_radio = new XoopsFormRadioYN("", "new_user_notify", $xoopsConfig['new_user_notify'], _MD_AM_YES,_MD_AM_NO);
		$newuser_group_select = new XoopsFormSelectGroup(_MD_AM_NOTIFYTO, "new_user_notify_group", false, $xoopsConfig['new_user_notify_group'], 1, false);
		$newuser_tray = new XoopsFormElementTray(_MD_AM_NEWUNOTIFY, "<br />");
		$newuser_tray->addElement($newuser_radio);
		$newuser_tray->addElement($newuser_group_select);
		$sdelete_radio = new XoopsFormRadioYN(_MD_AM_SELFDELETE, "self_delete", $xoopsConfig['self_delete'], _MD_AM_YES,_MD_AM_NO);
		$loading_radio = new XoopsFormRadioYN(_MD_AM_LOADINGIMG, "display_loading_img", $xoopsConfig['display_loading_img'], _MD_AM_YES,_MD_AM_NO);
		$gzip_radio = new XoopsFormRadioYN(sprintf(_MD_AM_USEGZIP,phpversion()), "gzip_compression", $xoopsConfig['gzip_compression'], _MD_AM_YES,_MD_AM_NO);
		$unamelv_select = new XoopsFormSelect(_MD_AM_UNAMELVL, "uname_test_level", $xoopsConfig['uname_test_level']);
		$unamelv_select->addOptionArray(array(0=>_MD_AM_STRICT,1=>_MD_AM_MEDIUM,2=>_MD_AM_LIGHT));
		$ucookie_text = new XoopsFormText(_MD_AM_USERCOOKIE, "usercookie", 50, 100, $xoopsConfig['usercookie']);
		$scookie_text = new XoopsFormText(_MD_AM_SESSCOOKIE, "sessioncookie", 50, 100, $xoopsConfig['sessioncookie']);
		$scookieold_hidden = new XoopsFormHidden("old_sessioncookie",$xoopsConfig['sessioncookie']);
		$sexpire_text = new XoopsFormText(_MD_AM_SESSEXPIRE, "sessionexpire", 50, 100, $xoopsConfig['sessionexpire']);
		//$redirect_radio = new XoopsFormRadioYN(_MD_AM_REDIRECT, "redirect_msg", $xoopsConfig['redirect_msg'], _MD_AM_YES, _MD_AM_NO);
		$banner_radio = new XoopsFormRadioYN(_MD_AM_BANNERS, "banners", $xoopsConfig['banners'], _MD_AM_YES, _MD_AM_NO);
		//$agrafic_radio = new XoopsFormRadioYN(_MD_AM_ADMINGRAPHIC, "admingraphic", $xoopsConfig['admingraphic'], _MD_AM_YES, _MD_AM_NO);
		$debug_radio = new XoopsFormRadioYN(_MD_AM_DEBUGMODE, "debug_mode", $xoopsConfig['debug_mode'], _MD_AM_YES, _MD_AM_NO);
		$cmode_select = new XoopsFormSelect(_MD_AM_COMMODE, "com_mode", $xoopsConfig['com_mode']);
		$cmode_select->addOptionArray(array("0"=>_NOCOMMENTS, "flat"=>_FLAT, "thread"=>_THREADED));
		$corder_select = new XoopsFormSelect(_MD_AM_COMORDER, "com_order", $xoopsConfig['com_order']);
		$corder_select->addOptionArray(array("0"=>_OLDESTFIRST, "1"=>_NEWESTFIRST));
		if(!isset($xoopsConfig['my_ip']) || $xoopsConfig['my_ip'] == ""){
			$my_ip = $GLOBALS['REMOTE_ADDR'];
		} else {
			$my_ip = $xoopsConfig['my_ip'];
		}
		$myip_text = new XoopsFormText(_MD_AM_MYIP, "my_ip", 50, 50, $my_ip);
		$avallow_radio = new XoopsFormRadioYN(_MD_AM_AVATARALLOW, "avatar_allow_upload", $xoopsConfig['avatar_allow_upload'], _MD_AM_YES, _MD_AM_NO);
		$avwidth_text = new XoopsFormText(_MD_AM_AVATARW, "avatar_width", 10, 10, $xoopsConfig['avatar_width']);
		$avheight_text = new XoopsFormText(_MD_AM_AVATARH, "avatar_height", 10, 10, $xoopsConfig['avatar_height']);
		$avsize_text = new XoopsFormText(_MD_AM_AVATARMAX, "avatar_maxsize", 10, 10, $xoopsConfig['avatar_maxsize']);
		$av_tray = new XoopsFormElementTray(_MD_AM_AVATARCONF, "<br />");
		$av_tray->addElement($avallow_radio);
		$av_tray->addElement($avwidth_text);
		$av_tray->addElement($avheight_text);
		$av_tray->addElement($avsize_text);
		$tags = array();
		$tags = explode(">",str_replace("<","",$xoopsConfig['allowed_html']));
		$ahtml_select = new XoopsFormSelect(_MD_AM_ALWDHTML, "tags", $tags, 10, true);
		$ahtml_select->addOptionArray(XoopsLists::getHtmlList());
		$op_hidden = new XoopsFormHidden("op","save");
		$submit_button = new XoopsFormButton("", "button", _GO, "submit");
		$form = new XoopsThemeForm(_MD_AM_SITEPREF, "pref_form", "admin.php?fct=preferences");
		$form->addElement($sitename_text);
		$form->addElement($slogan_text);
		$form->addElement($adminmail_text);
		$form->addElement($lang_select);
		$form->addElement($mod_select);
		$form->addElement($servertz_select);
		$form->addElement($defaulttz_select);
		$form->addElement($theme_tray);
		$form->addElement($allowtheme_radio);
		$form->addElement($anon_text);
		$form->addElement($minnpass_text);
		$form->addElement($anonpost_radio);
		//$form->addElement($allowsig_radio);
		$form->addElement($allowhtml_radio);
		$form->addElement($newuser_tray);
		$form->addElement($sdelete_radio);
		$form->addElement($loading_radio);
		$form->addElement($gzip_radio);
		$form->addElement($unamelv_select);
		$form->addElement($ucookie_text);
		$form->addElement($scookie_text);
		$form->addElement($scookieold_hidden);
		$form->addElement($sexpire_text);
		//$form->addElement($redirect_radio);
		$form->addElement($banner_radio);
		//$form->addElement($agrafic_radio);
		$form->addElement($debug_radio);
		$form->addElement($cmode_select);
		$form->addElement($corder_select);
		$form->addElement($myip_text);
		$form->addElement($av_tray);
		$form->addElement($ahtml_select);
		$form->addElement($op_hidden);
		$form->addElement($submit_button);
		OpenTable();
		$form->display();
		CloseTable();
	}

	function save_pref($sitename, $slogan, $adminmail, $language, $startpage, $server_TZ, $default_TZ, $default_theme, $allow_theme, $anonymous, $minpass, $anonpost, $new_user_notify, $new_user_notify_group, $self_delete, $display_loading_img, $gzip_compression, $uname_test_level, $usercookie, $sessioncookie, $old_sessioncookie, $sessionexpire, $banners, $tags, $my_ip, $com_mode, $com_order, $allow_html, $debug_mode, $avatar_allow_upload, $avatar_width, $avatar_height, $avatar_maxsize){
		global $xoopsConfig, $HTTP_COOKIE_VARS;
		if (!xoopsfwrite()) {
			return;
		}
		$myts =& MyTextSanitizer::getInstance();
		$error = "";
		if(!is_numeric($minpass)){
			$error .= "<h4>"._MD_AM_INVLDMINPASS."</h4>";
		}
		if(!is_numeric($sessionexpire)){
			$error .= "<h4>"._MD_AM_INVLDSEXP."</h4>";
		}
		if ((!$usercookie) || ($usercookie=="") || (ereg("[^a-zA-Z0-9_-]",$usercookie))){
		 	$error .= "<h4>"._MD_AM_INVLDUCOOK."</h4>";
		}
		if ((!$sessioncookie) || ($sessioncookie=="") || (ereg("[^a-zA-Z0-9_-]",$sessioncookie))){
		 	$error .= "<h4>"._MD_AM_INVLDSCOOK."</h4>";
		}
		if ( empty($adminmail) ){
		 	$error .= "<h4>"._MD_AM_ADMNOTSET."</h4>";
		}
		if($error != ""){
			xoops_cp_header();
			echo $error;
			xoops_cp_footer();
			exit();
		}
		$allowed_html = "";
		if ( count($tags) > 0 ) {
			foreach ( $tags as $tag ) {
				$allowed_html .= "<".$tag.">";
			}
		}
		$config = "<"."?php
/*********************************************************************
"._MD_AM_REMEMBER."
"._MD_AM_IFUCANT."
*********************************************************************/

// "._MD_AM_SITENAME."
\$xoopsConfig['sitename'] = \"".$myts->makeTboxData4PreviewInForm($sitename)."\";

// "._MD_AM_SLOGAN."
\$xoopsConfig['slogan'] = \"".$myts->makeTboxData4PreviewInForm($slogan)."\";

// "._MD_AM_ADMINML."
\$xoopsConfig['adminmail'] = \"".$myts->makeTboxData4PreviewInForm($adminmail)."\";

// "._MD_AM_LANGUAGE."
\$xoopsConfig['language'] = \"".$myts->makeTboxData4PreviewInForm($language)."\";

// "._MD_AM_STARTPAGE."
\$xoopsConfig['startpage'] = \"".$myts->makeTboxData4PreviewInForm($startpage)."\";

// "._MD_AM_DTHEME."
\$xoopsConfig['default_theme'] = \"".$myts->makeTboxData4PreviewInForm($default_theme)."\";

// "._MD_AM_ALLOWTHEME." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['allow_theme'] = ".intval($allow_theme).";

// "._MD_AM_ANONNAME."
\$xoopsConfig['anonymous'] = \"".$myts->makeTboxData4PreviewInForm($anonymous)."\";

// "._MD_AM_MINPASS."
\$xoopsConfig['minpass'] = ".intval($minpass).";

// "._MD_AM_ANONPOST." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['anonpost'] = ".intval($anonpost).";

// "._MD_AM_ALLOWHTML." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['allow_html'] = ".intval($allow_html).";

// "._MD_AM_NEWUNOTIFY." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['new_user_notify'] = ".intval($new_user_notify).";
// "._MD_AM_NOTIFYTO."
\$xoopsConfig['new_user_notify_group'] = ".intval($new_user_notify_group).";

// "._MD_AM_SELFDELETE." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['self_delete'] = ".intval($self_delete).";

// "._MD_AM_LOADINGIMG." (1="._MD_AM_YES." 0="._MD_AM_NO.") 
\$xoopsConfig['display_loading_img'] = ".intval($display_loading_img).";

// ".sprintf(_MD_AM_USEGZIP,phpversion())." (1="._MD_AM_YES." 0="._MD_AM_NO.") 
\$xoopsConfig['gzip_compression'] = ".intval($gzip_compression).";

// "._MD_AM_UNAMELVL."  ("._MD_AM_STRICT."=0 "._MD_AM_MEDIUM."=1 "._MD_AM_LIGHT."=2)
\$xoopsConfig['uname_test_level'] = ".intval($uname_test_level).";

// "._MD_AM_USERCOOKIE."
\$xoopsConfig['usercookie'] = \"".$myts->makeTboxData4PreviewInForm($usercookie)."\";

// "._MD_AM_SESSCOOKIE."
\$xoopsConfig['sessioncookie'] = \"".$myts->makeTboxData4PreviewInForm($sessioncookie)."\";

// "._MD_AM_SESSEXPIRE."
\$xoopsConfig['sessionexpire'] = ".intval($sessionexpire).";

// "._MD_AM_SERVERTZ."
\$xoopsConfig['server_TZ'] = ".$server_TZ.";

// "._MD_AM_DEFAULTTZ."
\$xoopsConfig['default_TZ'] = ".$default_TZ.";

// "._MD_AM_BANNERS." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['banners'] = ".intval($banners).";

// "._MD_AM_DEBUGMODE." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['debug_mode'] = ".intval($debug_mode).";

// "._MD_AM_COMMODE." (0="._NOCOMMENTS." flat="._FLAT." thread="._THREADED.")
\$xoopsConfig['com_mode'] = \"".$com_mode."\";

// "._MD_AM_COMORDER." (0="._OLDESTFIRST." 1="._NEWESTFIRST.")
\$xoopsConfig['com_order'] = ".intval($com_order).";

// "._MD_AM_MYIP." 
\$xoopsConfig['my_ip'] = \"".$my_ip."\";

// "._MD_AM_AVATARALLOW." (1="._MD_AM_YES." 0="._MD_AM_NO.")
\$xoopsConfig['avatar_allow_upload'] = ".intval($avatar_allow_upload).";
// "._MD_AM_AVATARW."
\$xoopsConfig['avatar_width'] = ".intval($avatar_width).";
// "._MD_AM_AVATARH."
\$xoopsConfig['avatar_height'] = ".intval($avatar_height).";
// "._MD_AM_AVATARMAX."
\$xoopsConfig['avatar_maxsize'] = ".intval($avatar_maxsize).";

// "._MD_AM_ALWDHTML."
\$xoopsConfig['allowed_html'] = \"".$allowed_html."\";

?".">";
		$file=fopen(XOOPS_ROOT_PATH."/modules/system/cache/config.php","w");
		if ( -1 != fwrite($file,$config) ) {
			// if the session cookie name has been changed, create session cookie with the new name
			if ( $old_sessioncookie != $sessioncookie ) {
				setcookie($sessioncookie, $HTTP_COOKIE_VARS[$xoopsConfig['sessioncookie']], time()+360000, "/",  "", 0);
				setcookie($xoopsConfig['sessioncookie']);
			}
		}
		fclose($file);
		redirect_header("admin.php",2,_MD_AM_DBUPDATED);
		exit();
	}

} else {

}

?>