<?php
// $Id: preferences.php,v 1.3 2004/10/16 16:09:22 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

// 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;

		$amail          = !empty($xoopsConfig['adminmail']) ? $xoopsConfig['adminmail'] : $xoopsUser->getVar("email", "E");
		$adminmail_text = new XoopsFormText(_MD_AM_ADMINML, "adminmail", 50, 100, $amail);
		$mail_function  = new XoopsFormSelect(_MD_AM_MAILFUNC, "mail_function", $xoopsConfig['mail_function']);
		$mail_function->addOptionArray(array("mail"=>"mail()", "email"=>"email()"));
		$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["-1"] = _NONE;
		$mod_select->addOptionArray($moduleslist);
		$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'], _YES,_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'], _YES,_NO);

		$max_pms              = new XoopsFormText(_MD_AM_MAXPMS, "max_pms", 5, 5, $xoopsConfig['max_pms']);

		$allowhtml_radio      = new XoopsFormRadioYN(_MD_AM_ALLOWHTML, "allow_html", $xoopsConfig['allow_html'], _YES,_NO);
		$allow_register       = new XoopsFormRadioYN(_MD_AM_ALLOW_REGISTER, "allow_register", $xoopsConfig['allow_register'], _YES,_NO);
		$img_verify           = new XoopsFormRadioYN(_MD_AM_VERIFY_IMG, "img_verify", $xoopsConfig['img_verify'], _YES,_NO);
		$allowimage_radio     = new XoopsFormRadioYN(_MD_AM_ALLOWIMAGE, "allow_image", $xoopsConfig['allow_image'], _YES,_NO);

		$libuse_radio    = new XoopsFormRadioYN(_MD_AM_LIBUSE, "allow_library", $xoopsConfig['allow_library'], _YES,_NO);
		$libupload_radio = new XoopsFormRadioYN(_MD_AM_LIBUPLOAD, "lib_allow_upload", $xoopsConfig['lib_allow_upload'], _YES, _NO);
		$libwidth_text   = new XoopsFormText(_MD_AM_LIBW, "lib_width", 4, 4, $xoopsConfig['lib_width']);
		$libheight_text  = new XoopsFormText(_MD_AM_LIBH, "lib_height", 4, 4, $xoopsConfig['lib_height']);
		$libsize_text    = new XoopsFormText(_MD_AM_LIBMAX, "lib_maxsize", 7, 7, $xoopsConfig['lib_maxsize']);
		$lib_tray        = new XoopsFormElementTray(_MD_AM_LIBCONF, "<br />");
		$lib_tray->addElement($libuse_radio);
		$lib_tray->addElement($libupload_radio);
		$lib_tray->addElement($libwidth_text);
		$lib_tray->addElement($libheight_text);
		$lib_tray->addElement($libsize_text);

		$auto_register        = new XoopsFormRadioYN(_MD_AM_AUTOREGISTER, "auto_register", $xoopsConfig['auto_register'], _YES,_NO);
		$newuser_radio        = new XoopsFormRadioYN("", "new_user_notify", $xoopsConfig['new_user_notify'], _YES,_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'], _YES,_NO);
		$loading_radio  = new XoopsFormRadioYN(_MD_AM_LOADINGIMG, "display_loading_img", $xoopsConfig['display_loading_img'], _YES,_NO);
		$gzip_radio     = new XoopsFormRadioYN(sprintf(_MD_AM_USEGZIP,phpversion()), "gzip_compression", $xoopsConfig['gzip_compression'], _YES,_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, "cookie_name", 50, 100, $xoopsConfig['cookie_name']);
		$scookie_text      = new XoopsFormText(_MD_AM_SESSCOOKIE, "session_name", 50, 100, $xoopsConfig['session_name']);
		$scookieold_hidden = new XoopsFormHidden("old_session_name", $xoopsConfig['session_name']);
		$sexpire_text      = new XoopsFormText(_MD_AM_SESSEXPIRE, "session_expire", 15, 8, $xoopsConfig['session_expire']);
		$suse_sessions     = new XoopsFormRadioYN(_MD_AM_SESSUSE, "use_sessions", $xoopsConfig['use_sessions'], _YES, _NO);
		$suse_old_hidden   = new XoopsFormHidden("old_session_use", $xoopsConfig['use_sessions']);

		$banner_radio      = new XoopsFormRadioYN(_MD_AM_BANNERS, "banners", $xoopsConfig['banners'], _YES, _NO);

		$debug_ops[] = ($xoopsConfig['debug_mode'] & 1)  ? 1  : 0;
		$debug_ops[] = ($xoopsConfig['debug_mode'] & 2)  ? 2  : 0;
		$debug_ops[] = ($xoopsConfig['debug_mode'] & 4)  ? 4  : 0;
		$debug_ops[] = ($xoopsConfig['debug_mode'] & 8)  ? 8  : 0;
		$debug_ops[] = ($xoopsConfig['debug_mode'] & 16) ? 16 : 0;
		$debug_radio   = new XoopsFormCheckBox(_MD_AM_DEBUGMODE, "debug_mode", $debug_ops, 1);
		$debug_options = array(
						"1" =>_MD_AM_DBGERR,
						"2" =>_MD_AM_DBGTIME,
						"4" =>_MD_AM_DBGINFO,
						"8" =>_MD_AM_DBGLOG,
						"16"=>_MD_AM_DBGVIS);
		$debug_radio->addOptionArray($debug_options);

		$cache_text = new XoopsFormSelect(_MD_AM_CACHETIME, "cache_time", $xoopsConfig['cache_time']);
		$cache_text->addOptionArray(array(0=>_OFF, 300=>5, 600=>10, 900=>15, 1800=>30, 3600=>60, 5400=>90, 7200=>120, 10800=>180));
		$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));
		$avallow_radio = new XoopsFormRadioYN(_MD_AM_AVATARALLOW, "avatar_allow_upload", $xoopsConfig['avatar_allow_upload'], _YES, _NO);
		$avwidth_text  = new XoopsFormText(_MD_AM_AVATARW, "avatar_width", 4, 4, $xoopsConfig['avatar_width']);
		$avheight_text = new XoopsFormText(_MD_AM_AVATARH, "avatar_height", 4, 4, $xoopsConfig['avatar_height']);
		$avsize_text   = new XoopsFormText(_MD_AM_AVATARMAX, "avatar_maxsize", 7, 7, $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);
		$htmlarray    = XoopsLists::getHtmlList();
		$atags        = array();
		$atags        = explode("|", $xoopsConfig['admin_html']);
		$ahtml_select = new XoopsFormSelect(_MD_AM_ADMTAGS, "atags", $atags, 10, true);
		$ahtml_select->addOptionArray($htmlarray);
		$utags        = array();
		$utags        = explode("|", $xoopsConfig['user_html']);
		$uhtml_select = new XoopsFormSelect(_MD_AM_USRTAGS, "utags", $utags, 10, true);
		$uhtml_select->addOptionArray($htmlarray);
		$html_tray = new XoopsFormElementTray(_MD_AM_DEFHTML, "&nbsp;");
		$html_tray->addElement($ahtml_select);
		$html_tray->addElement($uhtml_select);
		$op_hidden     = new XoopsFormHidden("op", "save");
		$submit_button = new XoopsFormButton("", "button", _UPDATE, "submit");

		$form = new XoopsThemeForm(_MD_AM_SITEPREF, "pref_form", "admin.php?fct=preferences");
		$form->addElement($adminmail_text);
		$form->addElement($mail_function);
		$form->addElement($lang_select);
		$form->addElement($mod_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($max_pms);
		$form->addElement($allowhtml_radio);
		$form->addElement($allowimage_radio);
		$form->addElement($lib_tray);
		$form->addElement($allow_register);
		$form->addElement($img_verify);
		$form->addElement($auto_register);
		$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($suse_sessions);
		$form->addElement($suse_old_hidden);
		$form->addElement($banner_radio);
		$form->addElement($debug_radio);
		$form->addElement($cache_text);
		$form->addElement($cmode_select);
		$form->addElement($corder_select);
		$form->addElement($av_tray);
		$form->addElement($html_tray);
		$form->addElement($op_hidden);
		$form->addElement($submit_button);

		OpenTable();
		$form->display();
		CloseTable();
	}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function save_pref(
		$adminmail,
		$mail_function,
		$language,
		$startpage,
		$default_TZ,
		$default_theme,
		$allow_theme,
		$anonymous,
		$minpass,
		$anonpost,
		$max_pms,
		$allow_register,
		$img_verify,
		$auto_register,
		$new_user_notify,
		$new_user_notify_group,
		$self_delete,
		$display_loading_img,
		$gzip_compression,
		$uname_test_level,
		$cookie_name,
		$session_name,
		$old_session_name,
		$session_expire,
		$use_sessions,
		$old_session_use,
		$banners,
		$com_mode,
		$com_order,
		$atags,
		$admin_html,
		$utags,
		$user_html,
		$allow_html,
		$allow_image,
		$allow_library,
		$lib_allow_upload,
		$lib_width,
		$lib_height,
		$lib_maxsize,
		$debug_mode,
		$cache_time,
		$avatar_allow_upload,
		$avatar_width,
		$avatar_height,
		$avatar_maxsize) {
		global $xoopsConfig, $myts, $HTTP_COOKIE_VARS;

		$error = "";
		if ( !function_exists("$mail_function") ) {
		$error .= "<h4>".sprintf(_MD_AM_INVLDMAILFUNC, $mail_function)."</h4>";
		}
		if ( !is_numeric($minpass) ) {
			$error .= "<h4>"._MD_AM_INVLDMINPASS."</h4>";
		}
		if ( !is_numeric($session_expire) ) {
			$error .= "<h4>"._MD_AM_INVLDSEXP."</h4>";
		}
		if ( empty($cookie_name) || preg_match("/[^a-z0-9_-]/", $cookie_name) ) {
		 	$error .= "<h4>"._MD_AM_INVLDUCOOK."</h4>";
		}
		if ( empty($session_name) || preg_match("/[^a-z0-9_-]/i", $session_name) ) {
		 	$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();
		}

		$admin_html = "";
		if ( count($atags) > 0 ) {
			foreach ( $atags as $atag ) {
				$admin_html .= $atag."|";
			}
			$admin_html = substr($admin_html, 0, -1);
		}
		$user_html = "";
		if ( count($utags) > 0 ) {
			foreach ( $utags as $utag ) {
				$user_html .= $utag."|";
			}
			$user_html = substr($user_html, 0, -1);
		}

		$debug_size = count($debug_mode);
		for ($i=0;$i<$debug_size;$i++) {
			$debug_out += $debug_mode[$i];
		}

$config = "<"."?php
/*********************************************************************
"._MD_AM_REMEMBER."
"._MD_AM_IFUCANT."
*********************************************************************/

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

// "._MD_AM_MAILFUNC."
\$xoopsConfig['mail_function'] = \"".$mail_function."\";

// "._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="._YES." 0="._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="._YES." 0="._NO.")
\$xoopsConfig['anonpost'] = ".intval($anonpost).";

// "._MD_AM_MAXPMS."
\$xoopsConfig['max_pms'] = ".intval($max_pms).";

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

// "._MD_AM_LIBUSE." (1="._YES." 0="._NO.")
\$xoopsConfig['allow_library'] = ".intval($allow_library).";

// "._MD_AM_LIBUPLOAD." (1="._YES." 0="._NO.")
\$xoopsConfig['lib_allow_upload'] = ".intval($lib_allow_upload).";

// "._MD_AM_LIBW."
\$xoopsConfig['lib_width'] = ".intval($lib_width).";

// "._MD_AM_LIBH."
\$xoopsConfig['lib_height'] = ".intval($lib_height).";

// "._MD_AM_LIBMAX."
\$xoopsConfig['lib_maxsize'] = ".intval($lib_maxsize).";

// "._MD_AM_ALLOWIMAGE." (1="._YES." 0="._NO.")
\$xoopsConfig['allow_image'] = ".intval($allow_image).";

// "._MD_AM_ALLOW_REGISTER." (1="._YES." 0="._NO.")
\$xoopsConfig['allow_register'] = ".intval($allow_register).";

// "._MD_AM_VERIFY_IMG." (1="._YES." 0="._NO.")
\$xoopsConfig['img_verify'] = ".intval($img_verify).";

// "._MD_AM_AUTOREGISTER." (1="._YES." 0="._NO.")
\$xoopsConfig['auto_register'] = ".intval($auto_register).";

// "._MD_AM_NEWUNOTIFY." (1="._YES." 0="._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="._YES." 0="._NO.")
\$xoopsConfig['self_delete'] = ".intval($self_delete).";

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

// ".sprintf(_MD_AM_USEGZIP,phpversion())." (1="._YES." 0="._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['cookie_name'] = \"".$cookie_name."\";

// "._MD_AM_SESSCOOKIE."
\$xoopsConfig['session_name'] = \"".$session_name."\";

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

// "._MD_AM_SESSUSE."
\$xoopsConfig['use_sessions'] = ".intval($use_sessions).";

// "._MD_AM_SERVERTZ."
\$xoopsConfig['server_TZ'] = \"".(date('Z')/3600)."\";

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

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

// "._MD_AM_DEBUGMODE."
\$xoopsConfig['debug_mode'] = ".intval($debug_out).";

// "._MD_AM_CACHETIME."
\$xoopsConfig['cache_time'] = ".intval($cache_time).";

// "._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_AVATARALLOW." (1="._YES." 0="._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_ADMHTML."
\$xoopsConfig['admin_html'] = \"".$admin_html."\";

// "._MD_AM_USRHTML."
\$xoopsConfig['user_html'] = \"".$user_html."\";

?".">";
		$file = fopen(XOOPS_ROOT_PATH."/modules/system/cache/config.php", "w");
		if ( -1 != fwrite($file, $config) ) {
			if ( ( $session_name != $old_session_name) || ($use_sessions !=  $old_session_use) ) {
				global $HTTP_SESSION_VARS, $HTTP_COOKIE_VARS;
				if ($use_sessions == 1) {
					if (session_is_registered($old_session_name)) {
						$HTTP_SESSION_VARS[$session_name] = $HTTP_SESSION_VARS[$old_session_name];
						session_unregister($old_session_name);
						} else {
							session_start();
							$HTTP_SESSION_VARS[$session_name] = $HTTP_COOKIE_VARS[$old_session_name];
						}
					} else {
						if (session_is_registered($old_session_name)) {
							cookie($session_name, $HTTP_SESSION_VARS[$old_session_name], 360000);
							} else {
								cookie($session_name, $HTTP_COOKIE_VARS[$old_session_name], 360000);
								cookie($old_session_name);
							}
					}
			}
		fclose($file);
		redirect_header("admin.php?fct=preferences", 2, _UPDATED);
		exit();
	}
}
	} else {
		redirect_header(XOOPS_URL.'/', 2, _NOPERM);
		exit();
	}
?>
