/**
  * AdmBlock_init().
  * get all available themes
  * Setup caching and and all themes.
  */
 function pmxc_AdmBlock_init()
 {
     $this->smf_themes = PortaMx_getsmfThemes();
     // get all themes
     $this->can_cached = 1;
     // enable cache
 }
    /**
     * ThemeContent.
     * Prepare the content and save in $this->cfg['content'].
     */
    function ThemeContent()
    {
        global $settings;
        $this->cfg['content'] = '';
        if (isset($this->cfg['config']['settings']['themes'])) {
            $this->availthemes = PortaMx_getsmfThemes();
            // get all themes
            $this->cfg['content'] .= '
									<select class="themsel" id="pmx' . $this->cfg['id'] . 'themeselect" size="1" onchange="setThemeImage(this)">';
            foreach ($this->availthemes as $thid => $data) {
                if (in_array($thid, $this->cfg['config']['settings']['themes'])) {
                    $this->cfg['content'] .= '
										<option value="' . $thid . '"' . ($data['usertheme'] ? ' selected="selected"' : ($settings['theme_id'] == $thid ? ' selected="selected"' : '')) . '>' . $data['name'] . '</option>';
                }
            }
            $this->cfg['content'] .= '
									</select>
									<form id="pmxthemechg' . $this->cfg['id'] . '" method="post"></form>
									<div class="themthumb">
										<img class="tborder" id="thumbnail" src="#" alt="*" title="" onclick="themeChange()" />
									</div>
	<script type="text/javascript">
		var themeimages = new Array();';
            foreach ($this->availthemes as $thid => $data) {
                if (in_array($thid, $this->cfg['config']['settings']['themes'])) {
                    $this->cfg['content'] .= '
		themeimages[' . $thid . '] = "' . $data['images_url'] . '/thumbnail.png";';
                }
            }
            $this->cfg['content'] .= '
		setThemeImage(document.getElementById("pmx' . $this->cfg['id'] . 'themeselect"));
		function setThemeImage(elm)
		{
			var idx = elm.selectedIndex;
			var thid = elm.options[idx].value;
			document.getElementById("thumbnail").src = themeimages[thid];
		}
		function themeChange()
		{
			pmxWinGetTop(\'themeselect' . $this->cfg['id'] . '\');
			var elm = document.getElementById("pmx' . $this->cfg['id'] . 'themeselect");
			document.getElementById("pmxthemechg' . $this->cfg['id'] . '").action = smf_scripturl + "?theme=" + elm.options[elm.selectedIndex].value + ";pmxrd=" + server_query_string;
			document.getElementById("pmxthemechg' . $this->cfg['id'] . '").submit();
		}
</script>';
        }
    }
 /**
  * This Methode is called on loadtime.
  * After all variables initiated, it calls the block dependent init methode.
  * Finaly the css and language is loaded if exist
  */
 function pmxc_AdmBlock_loadinit()
 {
     global $context;
     $this->smf_themes = PortaMx_getsmfThemes();
     // get all themes
     $this->smf_groups = PortaMx_getUserGroups();
     // get all usergroups
     $this->smf_boards = PortaMx_getsmfBoards();
     // get all smf boards
     $this->register_blocks = $context['pmx']['RegBlocks'];
     // get all registered block
     $this->block_classdef = PortaMx_getdefaultClass();
     // get default classes
     $this->title_icons = PortaMx_getAllTitleIcons();
     // get all title icons
     $this->custom_css = PortaMx_getCustomCssDefs();
     // custom css definitions
     $this->can_cached = 0;
     // default no caching
     // sort the registered blocks
     ksort($this->register_blocks, SORT_STRING);
     // call the blockdepend init methode
     $this->pmxc_AdmBlock_init();
 }
/**
* Receive all the Posts from Settings Manager, check and save it.
* Finally the Admin settings are prepared and the templare loaded.
*/
function PortaMx_AdminSettings()
{
    global $boarddir, $scripturl, $smcFunc, $context, $modSettings, $txt, $pmxCacheFunc;
    $admMode = PortaMx_makeSafe($_GET['action']);
    $pmx_area = PortaMx_makeSafe($_GET['area']);
    if (($admMode == 'admin' || $admMode == 'portamx') && ($pmx_area == 'pmx_settings' || $pmx_area == 'pmx_sefengine') && allowPmx('pmx_admin')) {
        if ($admMode == 'admin') {
            // fix the linktree
            foreach ($context['linktree'] as $key => $data) {
                if (strpos($data['url'], 'pmx_settings') !== false || strpos($data['url'], 'pmx_sefengine') !== false) {
                    $context['linktree'] = array_merge(array_slice($context['linktree'], 0, $key), array(array('url' => $scripturl . '?action=admin;area=pmx_center;' . $context['session_var'] . '=' . $context['session_id'], 'name' => $txt['pmx_extension'])), array_slice($context['linktree'], $key, count($context['linktree']) - $key));
                    break;
                }
            }
        }
        require_once $context['pmx_sourcedir'] . 'AdminSubs.php';
        $context['pmx']['subaction'] = isset($_GET['sa']) ? $_GET['sa'] : ($pmx_area == 'pmx_sefengine' ? '' : 'globals');
        // From template ?
        if (PortaMx_checkPOST()) {
            checkSession('post');
            $currentPanel = '';
            // check the Post array
            if (isset($_POST['save_settings']) && !empty($_POST['save_settings'])) {
                // check defined numeric vars (check_num_vars holds the posted array to check like [varname][varname] ...)
                if (isset($_POST['check_num_vars'])) {
                    foreach ($_POST['check_num_vars'] as $val) {
                        $data = explode(',', $val);
                        $post = '$_POST' . str_replace(array('[', ']'), array('[\'', '\']'), $data[0]);
                        if (eval("return isset({$post});") && eval("return !is_numeric({$post});")) {
                            eval("{$post} = {$data['1']};");
                        }
                    }
                    unset($_POST['check_num_vars']);
                }
                if (!empty($_POST['curPanel'])) {
                    $currentPanel = 'pn=' . $_POST['curPanel'] . ';';
                }
                // access update?
                if (!empty($_POST['update_access'])) {
                    $perms = array('pmx_promote' => array(), 'pmx_create' => array(), 'pmx_articles' => array(), 'pmx_blocks' => array(), 'pmx_admin' => array());
                    if (isset($_POST['setaccess'])) {
                        foreach ($_POST['setaccess'] as $acsname => $acsdata) {
                            $perms[$acsname] = $acsdata;
                        }
                    }
                    $smcFunc['db_insert']('replace', '
						{db_prefix}portamx_settings', array('varname' => 'string', 'config' => 'string'), array('permissions', serialize($perms)), array('varname'));
                    // clear settins cache
                    $pmxCacheFunc['clean']();
                } elseif (!empty($_POST['update_pmxsef'])) {
                    $arrayToken = array('pmxsef_stripchars', 'pmxsef_actions');
                    foreach ($_POST as $token => $value) {
                        if (substr($token, 0, 7) == 'pmxsef_') {
                            // check...
                            if ($token == 'pmxsef_spacechar') {
                                $_POST[$token] = !in_array(substr($_POST[$token], 0, 1), array('-', '_', '')) ? '-' : (!empty($_POST[$token]) ? substr($_POST[$token], 0, 1) : '');
                            } elseif ($token == 'pmxsef_ssefspace') {
                                $_POST[$token] = substr($_POST[$token], 0, 1);
                            } elseif ($token == 'pmxsef_aliasactions') {
                                $alias = array();
                                $tmp = Pmx_StrToArray($_POST[$token], ',');
                                foreach ($tmp as $d) {
                                    $t = Pmx_StrToArray($d, '=');
                                    if (!in_array($t[0], array('admin', 'portamx'))) {
                                        $alias[$t[1]] = $t[0];
                                    }
                                }
                                $_POST[$token] = serialize($alias);
                            } elseif ($token == 'pmxsef_ignorerequests') {
                                $alias = array();
                                $tmp = Pmx_StrToArray($_POST[$token], ',');
                                foreach ($tmp as $d) {
                                    $t = Pmx_StrToArray($d, '=');
                                    $alias[$t[0]] = $t[1];
                                }
                                $_POST[$token] = serialize($alias);
                            } elseif (in_array($token, $arrayToken)) {
                                $_POST[$token] = implode(',', Pmx_StrToArray($_POST[$token], ','));
                            }
                            if ($token != 'pmxsef_enable') {
                                $smcFunc['db_insert']('replace', '
									{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array($token, $_POST[$token]), array('variable'));
                            }
                        }
                    }
                    // alway disable SEF if no .htaccess or web.config found
                    if ((file_exists($boarddir . '/.htaccess') || file_exists($boarddir . '/web.config')) == false) {
                        $_POST['pmxsef_enable'] = '0';
                    }
                    // setup the the SMF hooks
                    $hooklist = array('integrate_pre_load' => 'pmxsef_convertSEF', 'integrate_buffer' => 'ob_pmxsef', 'integrate_redirect' => 'pmxsef_Redirect', 'integrate_outgoing_email' => 'pmxsef_EmailOutput', 'integrate_exit' => 'pmxsef_XMLOutput', 'integrate_fix_url' => 'pmxsef_fixurl');
                    // get the hooks from database
                    $smfhooks = array();
                    $request = $smcFunc['db_query']('', '
						SELECT variable, value FROM {db_prefix}settings
						WHERE variable IN ({array_string:hooks})', array('hooks' => array_keys($hooklist)));
                    if ($smcFunc['db_num_rows']($request) > 0) {
                        while ($row = $smcFunc['db_fetch_assoc']($request)) {
                            $smfhooks[$row['variable']] = $row['value'];
                        }
                        $smcFunc['db_free_result']($request);
                    }
                    // update the hooks
                    foreach ($hooklist as $hookname => $value) {
                        if (isset($smfhooks[$hookname])) {
                            $smfhooks[$hookname] = trim((!empty($_POST['pmxsef_enable']) ? $value . ',' : '') . trim(str_replace($value, '', $smfhooks[$hookname]), ','), ',');
                        } else {
                            $smfhooks[$hookname] = trim(!empty($_POST['pmxsef_enable']) ? $value : '');
                        }
                        $smcFunc['db_insert']('replace', '
							{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array($hookname, $smfhooks[$hookname]), array('variable'));
                    }
                    if (!empty($_POST['pmxsef_enable'])) {
                        $oldState = intval(!empty($modSettings['queryless_urls']));
                        updateSettings(array('queryless_urls' => '0'));
                        $smcFunc['db_insert']('replace', '
								{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array('oldstate_queryless_urls', $oldState), array('variable'));
                    } else {
                        if (isset($modSettings['oldstate_queryless_urls'])) {
                            updateSettings(array('queryless_urls' => $modSettings['oldstate_queryless_urls']));
                        }
                    }
                    // clear cache
                    $pmxCacheFunc['clean']();
                } else {
                    $config = array();
                    $request = $smcFunc['db_query']('', '
							SELECT config
							FROM {db_prefix}portamx_settings
							WHERE varname = {string:settings}', array('settings' => 'settings'));
                    if ($smcFunc['db_num_rows']($request) > 0) {
                        $row = $smcFunc['db_fetch_assoc']($request);
                        $smcFunc['db_free_result']($request);
                        $config = unserialize($row['config']);
                    }
                    $setKeys = array_diff(array_keys($_POST), array('pmx_fronttheme', 'pmx_frontthempg', 'pmx_ecl', 'pmx_eclmodal', 'pmx_eclmodalmobi', 'pmx_eclmodaltop', 'pmx_eclmodalaction', 'save_settings', 'sa', 'sc'));
                    foreach ($setKeys as $key) {
                        if ($key == 'promotes') {
                            $promo = Pmx_StrToIntArray($_POST[$key]);
                            $smcFunc['db_query']('', '
								UPDATE {db_prefix}portamx_settings
									SET config = {string:config}
									WHERE varname = {string:settings}', array('config' => serialize($promo), 'settings' => 'promotes'));
                            // find all promoted block
                            $blocks = null;
                            $request = $smcFunc['db_query']('', '
								SELECT id
								FROM {db_prefix}portamx_blocks
								WHERE active = 1 AND blocktype = {string:blocktype}', array('blocktype' => 'promotedposts'));
                            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                                $blocks[] = $row['id'];
                            }
                            $smcFunc['db_free_result']($request);
                            $_SESSION['pmx_refresh_promote'] = $blocks;
                        } else {
                            if ($key == 'dl_access') {
                                $_POST['dl_access'] = implode(',', $_POST['dl_access']);
                            }
                            $config[$key] = $_POST[$key];
                        }
                    }
                    $smcFunc['db_query']('', '
						UPDATE {db_prefix}portamx_settings
							SET config = {string:config}
							WHERE varname = {string:settings}', array('config' => serialize($config), 'settings' => 'settings'));
                    // other settings they stored in smf_settings table
                    $setKeys = array('pmx_fronttheme', 'pmx_frontthempg', 'pmx_ecl', 'pmx_eclmodal', 'pmx_eclmodalmobi', 'pmx_eclmodaltop', 'pmx_eclmodalaction', 'pmx_quickreply');
                    foreach ($setKeys as $key) {
                        if (isset($_POST[$key])) {
                            $smcFunc['db_insert']('replace', '
								{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array($key, $_POST[$key]), array('variable'));
                        }
                    }
                    // set frontmode flag
                    $smcFunc['db_insert']('replace', '
						{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array('pmx_frontmode', $config['frontpage'] == 'none' ? '0' : '1'), array('variable'));
                    if (isset($_POST['pmx_ecl'])) {
                        pmx_setcookie('pmx_eclauth', '');
                        if (!empty($_POST['pmx_ecl'])) {
                            pmx_setECL_Cookie(true);
                        }
                    }
                    // clear cached values
                    $pmxCacheFunc['clean']();
                }
            }
            redirectexit('action=' . $admMode . ';area=' . $pmx_area . (!empty($context['pmx']['subaction']) ? ';sa=' . $context['pmx']['subaction'] : '') . ';' . $currentPanel . $context['session_var'] . '=' . $context['session_id']);
        }
        // SEF engine settings ?
        if ($pmx_area == 'pmx_sefengine') {
            // pmxsef default settings
            $context['pmx']['pmxsef_enable'] = '0';
            $context['pmx']['pmxsef_lowercase'] = '1';
            $context['pmx']['pmxsef_autosave'] = '0';
            $context['pmx']['pmxsef_spacechar'] = '-';
            $context['pmx']['pmxsef_ssefspace'] = '';
            $context['pmx']['pmxsef_stripchars'] = '&quot;,&amp;,&lt;,&gt;,~,!,@,#,$,%,^,&,*,(,),-,=,+,<,[,{,],},>,;,:,\',",/,?,\\,|';
            $context['pmx']['pmxsef_singletoken'] = 'add,advanced,all,asc,calendar,check,children,conversation,desc,home,kstart,nw,profile,save,sound,togglebar,topicseen,view,viewweek,xml';
            $context['pmx']['pmxsef_actions'] = 'about:mozilla,about:unknown,activate,admin,announce,attachapprove,buddy,calendar,clock,collapse,community,coppa,credits,deletemsg,display,dlattach,editpoll,editpoll2,emailuser,findmember,groups,help,helpadmin,im,jseditor,jsmodify,jsoption,keepalive,lock,lockvoting,login,login2,logout,markasread,mergetopics,mlist,moderate,modifycat,modifykarma,movetopic,movetopic2,notify,notifyboard,openidreturn,pm,portamx,post,post2,printpage,profile,promote,quotefast,quickmod,quickmod2,recent,register,register2,reminder,removepoll,removetopic2,reporttm,requestmembers,restoretopic,search,search2,sendtopic,smstats,suggest,spellcheck,splittopics,stats,sticky,trackip,unread,unreadreplies,verificationcode,viewprofile,vote,viewquery,viewsmfile,who,.xml,xmlhttp';
            $context['pmx']['pmxsef_ignoreactions'] = '';
            $context['pmx']['pmxsef_aliasactions'] = '';
            $context['pmx']['pmxsef_ignorerequests'] = '';
            $context['pmx']['pmxsef_codepages'] = '/PortaMx/sefcodepages/x';
            $nocheck = array('pmxsef_enable', 'pmxsef_lowercase', 'pmxsef_spacechar');
            // read the settings from database
            $request = $smcFunc['db_query']('', '
				SELECT variable, value FROM {db_prefix}settings
				WHERE variable LIKE {string:variable}', array('variable' => 'pmxsef_%'));
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    $value = trim($row['value']);
                    if ($row['variable'] == 'pmxsef_aliasactions') {
                        $tmp = unserialize($value);
                        if (!empty($tmp)) {
                            foreach ($tmp as $act => $alias) {
                                $context['pmx'][$row['variable']][] = $alias . '=' . $act;
                            }
                            $context['pmx'][$row['variable']] = implode(',', $context['pmx'][$row['variable']]);
                        }
                    } elseif ($row['variable'] == 'pmxsef_ignorerequests') {
                        $tmp = unserialize($value);
                        if (!empty($tmp)) {
                            foreach ($tmp as $act => $alias) {
                                $context['pmx'][$row['variable']][] = $act . '=' . $alias;
                            }
                            $context['pmx'][$row['variable']] = implode(',', $context['pmx'][$row['variable']]);
                        }
                    } elseif (in_array($row['variable'], $nocheck) || !empty($value)) {
                        $context['pmx'][$row['variable']] = $value;
                    }
                }
                $smcFunc['db_free_result']($request);
            }
            // check if enabled
            $request = $smcFunc['db_query']('', '
				SELECT value FROM {db_prefix}settings
				WHERE variable = {string:hook}', array('hook' => 'integrate_pre_load'));
            if ($smcFunc['db_num_rows']($request) > 0) {
                $row = $smcFunc['db_fetch_assoc']($request);
                $smcFunc['db_free_result']($request);
                if (strpos($row['value'], 'pmxsef_convertSEF') !== false) {
                    $context['pmx']['pmxsef_enable'] = '1';
                }
            }
        } else {
            $context['pmx']['admthemes'] = PortaMx_getsmfThemes();
            $context['pmx']['admgroups'] = PortaMx_getUserGroups(true);
            $context['pmx']['limitgroups'] = PortaMx_getUserGroups(true, false);
            $context['pmx']['acsgroups'] = PortaMx_getUserGroups(false, !empty($context['pmx']['settings']['postcountacs']));
            $context['pmx']['sysstat'] = $pmxCacheFunc['stat']();
            $request = $smcFunc['db_query']('', '
				SELECT variable, value FROM {db_prefix}settings
				WHERE variable IN ({array_string:vars})', array('vars' => array('pmx_fronttheme', 'pmx_frontthempg', 'pmx_ecl', 'pmx_eclmodal', 'pmx_eclmodalmobi', 'pmx_eclmodaltop', 'pmx_eclmodalaction')));
            if ($smcFunc['db_num_rows']($request) > 0) {
                while ($row = $smcFunc['db_fetch_assoc']($request)) {
                    $context['pmx'][$row['variable']] = $row['value'];
                }
                $smcFunc['db_free_result']($request);
            }
        }
        // setup pagetitle
        $context['page_title'] = $txt['pmx_settings'];
        $context['pmx']['AdminMode'] = $admMode;
        // load language and execute template
        loadLanguage($context['pmx_templatedir'] . 'AdminSettings');
        loadTemplate($context['pmx_templatedir'] . 'AdminSettings');
    } else {
        fatal_error($txt['pmx_acces_error']);
    }
}