/**
 * Loads the permissions for the profiles
 */
function sportal_load_permissions()
{
    global $context, $user_info;
    $profiles = sportal_get_profiles(null, 1);
    $allowed = array();
    foreach ($profiles as $profile) {
        $result = false;
        if (!empty($profile['groups_denied']) && count(array_intersect($user_info['groups'], $profile['groups_denied'])) > 0) {
            $result = false;
        } elseif (!empty($profile['groups_allowed']) && count(array_intersect($user_info['groups'], $profile['groups_allowed'])) > 0) {
            $result = true;
        }
        if ($result) {
            $allowed[] = $profile['id'];
        }
    }
    $context['SPortal']['permissions'] = array('profiles' => $allowed, 'query' => empty($allowed) ? '0=1' : 'FIND_IN_SET(%s, \'' . implode(',', $allowed) . '\')');
}
 /**
  * Add or edit a portal wide permissions profile
  */
 public function action_sportal_admin_permission_profiles_edit()
 {
     global $context, $txt;
     // New or an edit?
     $context['is_new'] = empty($_REQUEST['profile_id']);
     // Saving the form
     if (!empty($_POST['submit'])) {
         // Security first
         checkSession();
         // Always clean the name
         if (!isset($_POST['name']) || Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) === '') {
             fatal_lang_error('sp_error_profile_name_empty', false);
         }
         $groups_allowed = $groups_denied = '';
         // If specific member groups were picked, build the allow/deny arrays
         if (!empty($_POST['membergroups']) && is_array($_POST['membergroups'])) {
             $groups_allowed = $groups_denied = array();
             foreach ($_POST['membergroups'] as $id => $value) {
                 if ($value == 1) {
                     $groups_allowed[] = (int) $id;
                 } elseif ($value == -1) {
                     $groups_denied[] = (int) $id;
                 }
             }
             $groups_allowed = implode(',', $groups_allowed);
             $groups_denied = implode(',', $groups_denied);
         }
         // Add the data to place in the fields
         $profile_info = array('id' => (int) $_POST['profile_id'], 'type' => 1, 'name' => Util::htmlspecialchars($_POST['name'], ENT_QUOTES), 'value' => implode('|', array($groups_allowed, $groups_denied)));
         // New we simply insert
         $profile_info['id'] = sp_add_permission_profile($profile_info, $context['is_new']);
         redirectexit('action=admin;area=portalprofiles');
     }
     // Not saving, then its time to show the permission form
     if ($context['is_new']) {
         $context['profile'] = array('id' => 0, 'name' => $txt['sp_profiles_default_name'], 'label' => $txt['sp_profiles_default_name'], 'groups_allowed' => array(), 'groups_denied' => array());
     } else {
         $_REQUEST['profile_id'] = (int) $_REQUEST['profile_id'];
         $context['profile'] = sportal_get_profiles($_REQUEST['profile_id']);
     }
     // Sub template time
     $context['profile']['groups'] = sp_load_membergroups();
     $context['page_title'] = $context['is_new'] ? $txt['sp_admin_profiles_add'] : $txt['sp_admin_profiles_edit'];
     $context['sub_template'] = 'permission_profiles_edit';
 }
    /**
     * Edits an existing or adds a new article to the system
     * Handles the previewing of an article
     */
    public function action_sportal_admin_article_edit()
    {
        global $context, $options, $txt;
        $this->_is_new = empty($_REQUEST['article_id']);
        $article_errors = Error_Context::context('article', 0);
        // Going to use editor and post functions
        require_once SUBSDIR . '/Post.subs.php';
        require_once SUBSDIR . '/Editor.subs.php';
        // Convert this to BBC?
        if (!empty($_REQUEST['content_mode']) && $_POST['type'] == 'bbc') {
            $convert = $_REQUEST['content'];
            require_once SUBSDIR . '/Html2BBC.class.php';
            $bbc_converter = new Convert_BBC($convert);
            $convert = $bbc_converter->get_bbc();
            $convert = un_htmlspecialchars($convert);
            $_POST['content'] = $convert;
        }
        // Saving the work?
        if (!empty($_POST['submit']) && !$article_errors->hasErrors()) {
            checkSession();
            $this->_sportal_admin_article_edit_save();
        }
        // Just taking a look before you save?
        if (!empty($_POST['preview']) || $article_errors->hasErrors()) {
            $context['article'] = $this->_sportal_admin_article_preview();
            // Fix any bbc errors they have created
            if ($context['article']['type'] == 'bbc') {
                preparsecode($context['article']['body']);
            }
            loadTemplate('PortalArticles');
            // Showing errors or a preview?
            if ($article_errors->hasErrors()) {
                $context['article_errors'] = array('errors' => $article_errors->prepareErrors(), 'type' => $article_errors->getErrorType() == 0 ? 'minor' : 'serious', 'title' => $txt['sp_form_errors_detected']);
            } else {
                $context['preview'] = true;
            }
        } elseif ($this->_is_new) {
            $context['article'] = array('id' => 0, 'article_id' => 'article' . mt_rand(1, 5000), 'category' => array('id' => 0), 'title' => $txt['sp_articles_default_title'], 'body' => '', 'type' => 'bbc', 'permissions' => 3, 'status' => 1);
        } else {
            $_REQUEST['article_id'] = (int) $_REQUEST['article_id'];
            $context['article'] = sportal_get_articles($_REQUEST['article_id']);
        }
        if ($context['article']['type'] === 'bbc') {
            $context['article']['body'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), un_preparsecode($context['article']['body']));
        }
        // On to the editor
        if ($context['article']['type'] !== 'bbc') {
            // Override user prefs for wizzy mode if they don't need it
            $temp_editor = !empty($options['wysiwyg_default']);
            $options['wysiwyg_default'] = false;
        }
        // Fire up the editor with the values
        $editor_options = array('id' => 'content', 'value' => $context['article']['body'], 'width' => '100%', 'height' => '225px', 'preview_type' => 2);
        create_control_richedit($editor_options);
        $context['post_box_name'] = $editor_options['id'];
        // Restore their settings
        if (isset($temp_editor)) {
            $options['wysiwyg_default'] = $temp_editor;
        }
        // Set the editor box to the right mode based on type (bbc, html, php)
        addInlineJavascript('
			$(window).load(function() {
				diewithfire = window.setTimeout(function() {sp_update_editor("' . $context['article']['type'] . '", "");}, 200);
			});
		');
        // Final bits for the template, category's and permission settings
        $context['article']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
        if (empty($context['article']['permission_profiles'])) {
            fatal_lang_error('error_sp_no_permission_profiles', false);
        }
        $context['article']['categories'] = sportal_get_categories();
        if (empty($context['article']['categories'])) {
            fatal_lang_error('error_sp_no_category', false);
        }
        // Page out values
        $context['is_new'] = $this->_is_new;
        $context['article']['body'] = sportal_parse_content($context['article']['body'], $context['article']['type'], 'return');
        $context['page_title'] = $this->_is_new ? $txt['sp_admin_articles_add'] : $txt['sp_admin_articles_edit'];
        $context['sub_template'] = 'articles_edit';
    }
    /**
     * Interface for adding/editing a page
     */
    public function action_sportal_admin_page_edit()
    {
        global $txt, $context, $options;
        $context['SPortal']['is_new'] = empty($_REQUEST['page_id']);
        $pages_errors = Error_Context::context('pages', 0);
        // Some help will be needed
        require_once SUBSDIR . '/Editor.subs.php';
        require_once SUBSDIR . '/Post.subs.php';
        // Convert this to BBC?
        if (!empty($_REQUEST['content_mode']) && $_POST['type'] === 'bbc') {
            require_once SUBSDIR . 'Html2BBC.class.php';
            $bbc_converter = new Convert_BBC($_REQUEST['content']);
            $_REQUEST['content'] = $bbc_converter->get_bbc();
            $_REQUEST['content'] = un_htmlspecialchars($_REQUEST['content']);
            $_POST['content'] = $_REQUEST['content'];
        }
        // Load in the blocks that can be used on a page
        $this->blocks = getBlockInfo();
        $context['page_blocks'] = $this->_sportal_admin_page_load_blocks();
        // Saving the work?
        if (!empty($_POST['submit']) && !$pages_errors->hasErrors()) {
            checkSession();
            $this->_sportal_admin_page_edit_save();
        }
        // Doing a quick look before you save or you messed up?
        if (!empty($_POST['preview']) || $pages_errors->hasErrors()) {
            $context['SPortal']['page'] = array('id' => $_POST['page_id'], 'page_id' => $_POST['namespace'], 'title' => Util::htmlspecialchars($_POST['title'], ENT_QUOTES), 'body' => Util::htmlspecialchars($_POST['content'], ENT_QUOTES), 'type' => $_POST['type'], 'permissions' => $_POST['permissions'], 'style' => sportal_parse_style('implode'), 'status' => !empty($_POST['status']));
            // Fix up bbc errors before we go to the preview
            if ($context['SPortal']['page']['type'] == 'bbc') {
                preparsecode($context['SPortal']['page']['body']);
            }
            loadTemplate('PortalPages');
            // Showing errors or a preview?
            if ($pages_errors->hasErrors()) {
                $context['pages_errors'] = array('errors' => $pages_errors->prepareErrors(), 'type' => $pages_errors->getErrorType() == 0 ? 'minor' : 'serious', 'title' => $txt['sp_form_errors_detected']);
            } else {
                $context['SPortal']['preview'] = true;
            }
        } elseif ($context['SPortal']['is_new']) {
            $context['SPortal']['page'] = array('id' => 0, 'page_id' => 'page' . mt_rand(1, 5000), 'title' => $txt['sp_pages_default_title'], 'body' => '', 'type' => 'bbc', 'permissions' => 3, 'style' => '', 'status' => 1);
        } else {
            $_REQUEST['page_id'] = (int) $_REQUEST['page_id'];
            $context['SPortal']['page'] = sportal_get_pages($_REQUEST['page_id']);
        }
        if ($context['SPortal']['page']['type'] === 'bbc') {
            $context['SPortal']['page']['body'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), un_preparsecode($context['SPortal']['page']['body']));
        }
        // Set up the editor, values, initial state, etc
        if ($context['SPortal']['page']['type'] !== 'bbc') {
            // No wizzy mode if they don't need it
            $temp_editor = !empty($options['wysiwyg_default']);
            $options['wysiwyg_default'] = false;
        }
        $editorOptions = array('id' => 'content', 'value' => $context['SPortal']['page']['body'], 'width' => '100%', 'height' => '225px', 'preview_type' => 2);
        create_control_richedit($editorOptions);
        $context['post_box_name'] = $editorOptions['id'];
        if (isset($temp_editor)) {
            $options['wysiwyg_default'] = $temp_editor;
        }
        // Set the editor box as needed (editor or textbox, etc)
        addInlineJavascript('
			$(window).load(function() {
				diewithfire = window.setTimeout(function() {sp_update_editor("' . $context['SPortal']['page']['type'] . '", "");}, 200);
			});
		');
        // Permissions
        $context['SPortal']['page']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
        if (empty($context['SPortal']['page']['permission_profiles'])) {
            fatal_lang_error('error_sp_no_permission_profiles', false);
        }
        // And for the template
        $context['SPortal']['page']['style'] = sportal_parse_style('explode', $context['SPortal']['page']['style'], !empty($context['SPortal']['preview']));
        $context['SPortal']['page']['body'] = sportal_parse_content($context['SPortal']['page']['body'], $context['SPortal']['page']['type'], 'return');
        $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp_admin_pages_add'] : $txt['sp_admin_pages_edit'];
        $context['sub_template'] = 'pages_edit';
    }
 /**
  * Edit or add a category
  */
 public function action_sportal_admin_category_edit()
 {
     global $context, $txt;
     loadTemplate('PortalAdminCategories');
     $this->_is_new = empty($_REQUEST['category_id']);
     // Saving the category form
     if (!empty($_POST['submit'])) {
         checkSession();
         // Clean what was sent
         // @todo move all this to validator?
         $name = isset($_POST['name']) ? Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) : '';
         $namespace = isset($_POST['namespace']) ? Util::htmltrim(Util::htmlspecialchars($_POST['namespace'], ENT_QUOTES)) : '';
         $current = isset($_POST['category_id']) ? (int) $_POST['category_id'] : 0;
         $description = isset($_POST['description']) ? Util::htmlspecialchars($_POST['description'], ENT_QUOTES) : '';
         if (empty($name)) {
             fatal_lang_error('sp_error_category_name_empty', false);
         }
         if (empty($namespace)) {
             fatal_lang_error('sp_error_category_namespace_empty', false);
         }
         if (sp_check_duplicate_category($current, $namespace)) {
             fatal_lang_error('sp_error_category_namespace_duplicate', false);
         }
         if (preg_match('~[^A-Za-z0-9_]+~', $namespace) != 0) {
             fatal_lang_error('sp_error_category_namespace_invalid_chars', false);
         }
         if (preg_replace('~[0-9]+~', '', $namespace) === '') {
             fatal_lang_error('sp_error_category_namespace_numeric', false);
         }
         $category_info = array('id' => (int) $_POST['category_id'], 'namespace' => $namespace, 'name' => $name, 'description' => $description, 'permissions' => (int) $_POST['permissions'], 'status' => !empty($_POST['status']) ? 1 : 0);
         $category_info['id'] = sp_update_category($category_info, $this->_is_new);
         redirectexit('action=admin;area=portalcategories');
     }
     // Creating a new category, lets set up some defaults for the form
     if ($this->_is_new) {
         $context['category'] = array('id' => 0, 'category_id' => 'category' . mt_rand(1, 5000), 'name' => $txt['sp_categories_default_name'], 'description' => '', 'permissions' => 3, 'groups_allowed' => array(), 'groups_denied' => array(), 'status' => 1);
     } else {
         $_REQUEST['category_id'] = (int) $_REQUEST['category_id'];
         $context['category'] = sportal_get_categories($_REQUEST['category_id']);
     }
     $context['is_new'] = $this->_is_new;
     $context['category']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
     $context['category']['groups'] = sp_load_membergroups();
     $context['page_title'] = $this->_is_new ? $txt['sp_admin_categories_add'] : $txt['sp_admin_categories_edit'];
     $context['sub_template'] = 'categories_edit';
 }
 /**
  * Adding or editing a block.
  */
 public function action_sportal_admin_block_edit()
 {
     global $txt, $context, $modSettings, $boards;
     // Just in case, the admin could be doing something silly like editing a SP block while SP is disabled. ;)
     require_once SUBSDIR . '/PortalBlocks.subs.php';
     $context['SPortal']['is_new'] = empty($_REQUEST['block_id']);
     // BBC Fix move the parameter to the correct position.
     if (!empty($_POST['bbc_name'])) {
         $_POST['parameters'][$_POST['bbc_name']] = !empty($_POST[$_POST['bbc_parameter']]) ? $_POST[$_POST['bbc_parameter']] : '';
         // If we came from WYSIWYG then turn it back into BBC regardless.
         if (!empty($_REQUEST['bbc_' . $_POST['bbc_name'] . '_mode']) && isset($_POST['parameters'][$_POST['bbc_name']])) {
             require_once SUBSDIR . 'Html2BBC.class.php';
             $bbc_converter = new Convert_BBC($_POST['parameters'][$_POST['bbc_name']]);
             $_POST['parameters'][$_POST['bbc_name']] = $bbc_converter->get_bbc();
             // We need to unhtml it now as it gets done shortly.
             $_POST['parameters'][$_POST['bbc_name']] = un_htmlspecialchars($_POST['parameters'][$_POST['bbc_name']]);
         }
     }
     // Passing the selected type via $_GET instead of $_POST?
     $start_parameters = array();
     if (!empty($_GET['selected_type']) && empty($_POST['selected_type'])) {
         $_POST['selected_type'] = array($_GET['selected_type']);
         if (!empty($_GET['parameters'])) {
             foreach ($_GET['parameters'] as $param) {
                 if (isset($_GET[$param])) {
                     $start_parameters[$param] = $_GET[$param];
                 }
             }
         }
     }
     // Want use a block on the portal?
     if ($context['SPortal']['is_new'] && empty($_POST['selected_type']) && empty($_POST['add_block'])) {
         // Gather the blocks we have available
         $context['SPortal']['block_types'] = getFunctionInfo();
         // Create a list of the blocks in use
         $in_use = getBlockInfo();
         foreach ($in_use as $block) {
             $context['SPortal']['block_inuse'][$block['type']] = array('state' => $block['state'], 'column' => $block['column']);
         }
         $context['location'] = array(1 => $txt['sp-positionLeft'], $txt['sp-positionTop'], $txt['sp-positionBottom'], $txt['sp-positionRight'], $txt['sp-positionHeader'], $txt['sp-positionFooter']);
         if (!empty($_REQUEST['col'])) {
             $context['SPortal']['block']['column'] = $_REQUEST['col'];
         }
         $context['sub_template'] = 'block_select_type';
         $context['page_title'] = $txt['sp-blocksAdd'];
     } elseif ($context['SPortal']['is_new'] && !empty($_POST['selected_type'])) {
         $context['SPortal']['block'] = array('id' => 0, 'label' => $txt['sp-blocksDefaultLabel'], 'type' => $_POST['selected_type'][0], 'type_text' => !empty($txt['sp_function_' . $_POST['selected_type'][0] . '_label']) ? $txt['sp_function_' . $_POST['selected_type'][0] . '_label'] : $txt['sp_function_unknown_label'], 'column' => !empty($_POST['block_column']) ? $_POST['block_column'] : 0, 'row' => 0, 'permissions' => 3, 'state' => 1, 'force_view' => 0, 'mobile_view' => 0, 'display' => '', 'display_custom' => '', 'style' => '', 'parameters' => !empty($start_parameters) ? $start_parameters : array(), 'options' => $_POST['selected_type'][0](array(), false, true), 'list_blocks' => !empty($_POST['block_column']) ? getBlockInfo($_POST['block_column']) : array());
     } elseif (!$context['SPortal']['is_new'] && empty($_POST['add_block'])) {
         $_REQUEST['block_id'] = (int) $_REQUEST['block_id'];
         $context['SPortal']['block'] = current(getBlockInfo(null, $_REQUEST['block_id']));
         $context['SPortal']['block'] += array('options' => $context['SPortal']['block']['type'](array(), false, true), 'list_blocks' => getBlockInfo($context['SPortal']['block']['column']));
     }
     // Want to take a look at how this block will appear, well we try our best
     if (!empty($_POST['preview_block']) || isset($_SESSION['sp_error'])) {
         // An error was generated on save, lets set things up like a preview and return to the preview
         if (isset($_SESSION['sp_error'])) {
             $context['SPortal']['error'] = $_SESSION['sp_error'];
             $_POST = $_SESSION['sp_error_post'];
             $_POST['preview_block'] = true;
             // Clean up
             unset($_SESSION['sp_error'], $_SESSION['sp_error_post'], $_POST['add_block']);
         }
         // Just in case, the admin could be doing something silly like editing a SP block while SP is disabled. ;)
         require_once BOARDDIR . '/SSI.php';
         sportal_init_headers();
         loadTemplate('Portal');
         $type_parameters = $_POST['block_type'](array(), 0, true);
         if (!empty($_POST['parameters']) && is_array($_POST['parameters']) && !empty($type_parameters)) {
             foreach ($type_parameters as $name => $type) {
                 if (isset($_POST['parameters'][$name])) {
                     $this->_prepare_parameters($type, $name);
                 }
             }
         } else {
             $_POST['parameters'] = array();
         }
         // Simple is clean
         if (empty($_POST['display_advanced'])) {
             if (!empty($_POST['display_simple']) && in_array($_POST['display_simple'], array('all', 'sportal', 'sforum', 'allaction', 'allboard', 'allpages'))) {
                 $display = $_POST['display_simple'];
             } else {
                 $display = '';
             }
             $custom = '';
         } else {
             $display = array();
             $custom = array();
             if (!empty($_POST['display_actions'])) {
                 foreach ($_POST['display_actions'] as $action) {
                     $display[] = Util::htmlspecialchars($action, ENT_QUOTES);
                 }
             }
             if (!empty($_POST['display_boards'])) {
                 foreach ($_POST['display_boards'] as $board) {
                     $display[] = 'b' . (int) substr($board, 1);
                 }
             }
             if (!empty($_POST['display_pages'])) {
                 foreach ($_POST['display_pages'] as $page) {
                     $display[] = 'p' . (int) substr($page, 1);
                 }
             }
             if (!empty($_POST['display_custom'])) {
                 $temp = explode(',', $_POST['display_custom']);
                 foreach ($temp as $action) {
                     $custom[] = Util::htmlspecialchars(Util::htmltrim($action), ENT_QUOTES);
                 }
             }
             $display = empty($display) ? '' : implode(',', $display);
             $custom = empty($custom) ? '' : implode(',', $custom);
         }
         // Create all the information we know about this block
         $context['SPortal']['block'] = array('id' => $_POST['block_id'], 'label' => Util::htmlspecialchars($_POST['block_name'], ENT_QUOTES), 'type' => $_POST['block_type'], 'type_text' => !empty($txt['sp_function_' . $_POST['block_type'] . '_label']) ? $txt['sp_function_' . $_POST['block_type'] . '_label'] : $txt['sp_function_unknown_label'], 'column' => $_POST['block_column'], 'row' => !empty($_POST['block_row']) ? $_POST['block_row'] : 0, 'placement' => !empty($_POST['placement']) && in_array($_POST['placement'], array('before', 'after')) ? $_POST['placement'] : '', 'permissions' => $_POST['permissions'], 'state' => !empty($_POST['block_active']), 'force_view' => !empty($_POST['block_force']), 'mobile_view' => !empty($_POST['block_mobile']), 'display' => $display, 'display_custom' => $custom, 'style' => sportal_parse_style('implode'), 'parameters' => !empty($_POST['parameters']) ? $_POST['parameters'] : array(), 'options' => $_POST['block_type'](array(), false, true), 'list_blocks' => getBlockInfo($_POST['block_column']), 'collapsed' => false);
         if (strpos($modSettings['leftwidth'], '%') !== false || strpos($modSettings['leftwidth'], 'px') !== false) {
             $context['widths'][1] = $modSettings['leftwidth'];
         } else {
             $context['widths'][1] = $modSettings['leftwidth'] . 'px';
         }
         if (strpos($modSettings['rightwidth'], '%') !== false || strpos($modSettings['rightwidth'], 'px') !== false) {
             $context['widths'][4] = $modSettings['rightwidth'];
         } else {
             $context['widths'][4] = $modSettings['rightwidth'] . 'px';
         }
         if (strpos($context['widths'][1], '%') !== false) {
             $context['widths'][2] = $context['widths'][3] = 100 - ($context['widths'][1] + $context['widths'][4]) . '%';
             $context['widths'][5] = $context['widths'][6] = '100%';
         } elseif (strpos($context['widths'][1], 'px') !== false) {
             $context['widths'][2] = $context['widths'][3] = 960 - ($context['widths'][1] + $context['widths'][4]) . 'px';
             $context['widths'][5] = $context['widths'][6] = '960px';
         }
         $context['SPortal']['preview'] = true;
     }
     if (!empty($_POST['selected_type']) || !empty($_POST['preview_block']) || !$context['SPortal']['is_new'] && empty($_POST['add_block'])) {
         // Only the admin can use PHP blocks
         if ($context['SPortal']['block']['type'] == 'sp_php' && !allowedTo('admin_forum')) {
             fatal_lang_error('cannot_admin_forum', false);
         }
         loadLanguage('SPortalHelp', sp_languageSelect('SPortalHelp'));
         // Load up the permissions
         $context['SPortal']['block']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
         if (empty($context['SPortal']['block']['permission_profiles'])) {
             fatal_lang_error('error_sp_no_permission_profiles', false);
         }
         $context['simple_actions'] = array('sportal' => $txt['sp-portal'], 'sforum' => $txt['sp-forum'], 'allaction' => $txt['sp-blocksOptionAllActions'], 'allboard' => $txt['sp-blocksOptionAllBoards'], 'allpages' => $txt['sp-blocksOptionAllPages'], 'all' => $txt['sp-blocksOptionEverywhere']);
         $context['display_actions'] = array('portal' => $txt['sp-portal'], 'forum' => $txt['sp-forum'], 'recent' => $txt['recent_posts'], 'unread' => $txt['unread_topics_visit'], 'unreadreplies' => $txt['unread_replies'], 'profile' => $txt['profile'], 'pm' => $txt['pm_short'], 'calendar' => $txt['calendar'], 'admin' => $txt['admin'], 'login' => $txt['login'], 'register' => $txt['register'], 'post' => $txt['post'], 'stats' => $txt['forum_stats'], 'search' => $txt['search'], 'mlist' => $txt['members_list'], 'moderate' => $txt['moderate'], 'help' => $txt['help'], 'who' => $txt['who_title']);
         // Load up boards and pages for selection in the template
         sp_block_template_helpers();
         if (empty($context['SPortal']['block']['display'])) {
             $context['SPortal']['block']['display'] = array('0');
         } else {
             $context['SPortal']['block']['display'] = explode(',', $context['SPortal']['block']['display']);
         }
         if (in_array($context['SPortal']['block']['display'][0], array('all', 'sportal', 'sforum', 'allaction', 'allboard', 'allpages')) || $context['SPortal']['is_new'] || empty($context['SPortal']['block']['display'][0]) && empty($context['SPortal']['block']['display_custom'])) {
             $context['SPortal']['block']['display_type'] = 0;
         } else {
             $context['SPortal']['block']['display_type'] = 1;
         }
         $context['SPortal']['block']['style'] = sportal_parse_style('explode', $context['SPortal']['block']['style'], !empty($context['SPortal']['preview']));
         // Prepare the Textcontent for BBC, only the first bbc will be detected correctly!
         $firstBBCFound = false;
         foreach ($context['SPortal']['block']['options'] as $name => $type) {
             // Selectable Boards :D
             if ($type == 'board_select' || $type == 'boards') {
                 if (empty($boards)) {
                     require_once SUBSDIR . '/Boards.subs.php';
                     getBoardTree();
                 }
                 // Merge the array ;)
                 if (!isset($context['SPortal']['block']['parameters'][$name])) {
                     $context['SPortal']['block']['parameters'][$name] = array();
                 } elseif (!empty($context['SPortal']['block']['parameters'][$name]) && is_array($context['SPortal']['block']['parameters'][$name])) {
                     $context['SPortal']['block']['parameters'][$name] = implode('|', $context['SPortal']['block']['parameters'][$name]);
                 }
                 $context['SPortal']['block']['board_options'][$name] = array();
                 $config_variable = !empty($context['SPortal']['block']['parameters'][$name]) ? $context['SPortal']['block']['parameters'][$name] : array();
                 $config_variable = !is_array($config_variable) ? explode('|', $config_variable) : $config_variable;
                 $context['SPortal']['block']['board_options'][$name] = array();
                 // Create the list for this Item
                 foreach ($boards as $board) {
                     // Ignore the redirected boards :)
                     if (!empty($board['redirect'])) {
                         continue;
                     }
                     $context['SPortal']['block']['board_options'][$name][$board['id']] = array('value' => $board['id'], 'text' => $board['name'], 'selected' => in_array($board['id'], $config_variable));
                 }
             } elseif ($type === 'bbc') {
                 // ELK support only one bbc correct, multiple bbc do not work at the moment
                 if (!$firstBBCFound) {
                     $firstBBCFound = true;
                     // Start Elk BBC System :)
                     require_once SUBSDIR . '/Editor.subs.php';
                     // Prepare the output :D
                     $form_message = !empty($context['SPortal']['block']['parameters'][$name]) ? $context['SPortal']['block']['parameters'][$name] : '';
                     // But if it's in HTML world, turn them into htmlspecialchar's so they can be edited!
                     if (strpos($form_message, '[html]') !== false) {
                         $parts = preg_split('~(\\[/code\\]|\\[code(?:=[^\\]]+)?\\])~i', $form_message, -1, PREG_SPLIT_DELIM_CAPTURE);
                         for ($i = 0, $n = count($parts); $i < $n; $i++) {
                             // It goes 0 = outside, 1 = begin tag, 2 = inside, 3 = close tag, repeat.
                             if ($i % 4 == 0) {
                                 $parts[$i] = preg_replace_callback('~\\[html\\](.+?)\\[/html\\]~is', create_function('$m', 'return "[html]" . preg_replace(\'~<br\\s?/?>~i\', \'&lt;br /&gt;<br />\', "$m[1]") . "[/html]";'), $parts[$i]);
                             }
                         }
                         $form_message = implode('', $parts);
                     }
                     $form_message = preg_replace('~<br(?: /)?' . '>~i', "\n", $form_message);
                     // Prepare the data before i want them inside the textarea
                     $form_message = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
                     $context['SPortal']['bbc'] = 'bbc_' . $name;
                     $message_data = array('id' => $context['SPortal']['bbc'], 'width' => '95%', 'height' => '200px', 'value' => $form_message, 'form' => 'sp_block');
                     // Run the ELK bbc editor routine
                     create_control_richedit($message_data);
                     // Store the updated data on the parameters
                     $context['SPortal']['block']['parameters'][$name] = $form_message;
                 } else {
                     $context['SPortal']['block']['options'][$name] = 'textarea';
                 }
             }
         }
         loadJavascriptFile('portal.js?sp24');
         $context['sub_template'] = 'block_edit';
         $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp-blocksAdd'] : $txt['sp-blocksEdit'];
     }
     // Want to add / edit a block oo the portal
     if (!empty($_POST['add_block'])) {
         checkSession();
         // Only the admin can do php here
         if ($_POST['block_type'] == 'sp_php' && !allowedTo('admin_forum')) {
             fatal_lang_error('cannot_admin_forum', false);
         }
         // Make sure the block name is something safe
         if (!isset($_POST['block_name']) || Util::htmltrim(Util::htmlspecialchars($_POST['block_name']), ENT_QUOTES) === '') {
             fatal_lang_error('error_sp_name_empty', false);
         }
         if ($_POST['block_type'] == 'sp_php' && !empty($_POST['parameters']['content']) && empty($modSettings['sp_disable_php_validation'])) {
             require_once SUBSDIR . '/DataValidator.class.php';
             $validator = new Data_Validator();
             $validator->validation_rules(array('content' => 'php_syntax'));
             $validator->validate(array('content' => $_POST['parameters']['content']));
             $error = $validator->validation_errors();
             if ($error) {
                 $_SESSION['sp_error'] = $error[0];
                 $_SESSION['sp_error_post'] = $_POST;
                 redirectexit('action=admin;area=portalblocks;sa=' . $_REQUEST['sa'] . (!empty($_REQUEST['block_id']) ? ';block_id=' . $_REQUEST['block_id'] : ''));
             }
         }
         // If we have a block ID passed, we must be editing, so the the blocks current data
         if (!empty($_REQUEST['block_id'])) {
             $current_data = current(getBlockInfo(null, $_REQUEST['block_id']));
         }
         // Where are we going to place this new block, before, after, no change
         if (!empty($_POST['placement']) && ($_POST['placement'] === 'before' || $_POST['placement'] === 'after')) {
             if (!empty($current_data)) {
                 $current_row = $current_data['row'];
             } else {
                 $current_row = null;
             }
             // Before or after the chosen block
             if ($_POST['placement'] === 'before') {
                 $row = (int) $_POST['block_row'];
             } else {
                 $row = (int) $_POST['block_row'] + 1;
             }
             if (!empty($current_row) && $row > $current_row) {
                 sp_update_block_row($current_row, $row - 1, $_POST['block_column'], true);
             } else {
                 sp_update_block_row($current_row, $row, $_POST['block_column'], false);
             }
         } elseif (!empty($_POST['placement']) && $_POST['placement'] == 'nochange') {
             $row = 0;
         } else {
             $block_id = !empty($_REQUEST['block_id']) ? (int) $_REQUEST['block_id'] : 0;
             $row = sp_block_nextrow($_POST['block_column'], $block_id);
         }
         $type_parameters = $_POST['block_type'](array(), 0, true);
         if (!empty($_POST['parameters']) && is_array($_POST['parameters']) && !empty($type_parameters)) {
             foreach ($type_parameters as $name => $type) {
                 // Prepare BBC Content for ELK
                 if (isset($_POST['parameters'][$name])) {
                     $this->_prepare_parameters($type, $name);
                 }
             }
         } else {
             $_POST['parameters'] = array();
         }
         // Standard options
         if (empty($_POST['display_advanced'])) {
             if (!empty($_POST['display_simple']) && in_array($_POST['display_simple'], array('all', 'sportal', 'sforum', 'allaction', 'allboard', 'allpages'))) {
                 $display = $_POST['display_simple'];
             } else {
                 $display = '';
             }
             $custom = '';
         } else {
             $display = array();
             if (!empty($_POST['display_actions'])) {
                 foreach ($_POST['display_actions'] as $action) {
                     $display[] = Util::htmlspecialchars($action, ENT_QUOTES);
                 }
             }
             if (!empty($_POST['display_boards'])) {
                 foreach ($_POST['display_boards'] as $board) {
                     $display[] = 'b' . (int) substr($board, 1);
                 }
             }
             if (!empty($_POST['display_pages'])) {
                 foreach ($_POST['display_pages'] as $page) {
                     $display[] = 'p' . (int) substr($page, 1);
                 }
             }
             if (!empty($_POST['display_custom'])) {
                 $custom = array();
                 $temp = explode(',', $_POST['display_custom']);
                 foreach ($temp as $action) {
                     $custom[] = Util::htmlspecialchars(Util::htmltrim($action), ENT_QUOTES);
                 }
             }
             $display = empty($display) ? '' : implode(',', $display);
             if (!allowedTo('admin_forum') && isset($current_data['display_custom']) && substr($current_data['display_custom'], 0, 4) === '$php') {
                 $custom = $current_data['display_custom'];
             } elseif (!empty($_POST['display_custom'])) {
                 if (allowedTo('admin_forum') && substr($_POST['display_custom'], 0, 4) === '$php') {
                     $custom = Util::htmlspecialchars($_POST['display_custom'], ENT_QUOTES);
                 } else {
                     $custom = array();
                     $temp = explode(',', $_POST['display_custom']);
                     foreach ($temp as $action) {
                         $custom[] = Util::htmlspecialchars($action, ENT_QUOTES);
                     }
                     $custom = empty($custom) ? '' : implode(',', $custom);
                 }
             } else {
                 $custom = '';
             }
         }
         $blockInfo = array('id' => (int) $_POST['block_id'], 'label' => Util::htmlspecialchars($_POST['block_name'], ENT_QUOTES), 'type' => $_POST['block_type'], 'col' => $_POST['block_column'], 'row' => $row, 'permissions' => (int) $_POST['permissions'], 'state' => !empty($_POST['block_active']) ? 1 : 0, 'force_view' => !empty($_POST['block_force']) ? 1 : 0, 'mobile_view' => !empty($_POST['block_mobile']) ? 1 : 0, 'display' => $display, 'display_custom' => $custom, 'style' => sportal_parse_style('implode'));
         // Insert a new block in to the portal
         if ($context['SPortal']['is_new']) {
             unset($blockInfo['id']);
             $blockInfo['id'] = sp_block_insert($blockInfo);
         } else {
             sp_block_update($blockInfo);
         }
         // Save any parameters for the block
         if (!empty($_POST['parameters'])) {
             sp_block_insert_parameters($_POST['parameters'], $blockInfo['id']);
         }
         redirectexit('action=admin;area=portalblocks');
     }
 }
 /**
  * Edit an existing shoutbox or add a new one
  */
 public function action_sportal_admin_shoutbox_edit()
 {
     global $txt, $context, $modSettings, $editortxt;
     $context['SPortal']['is_new'] = empty($_REQUEST['shoutbox_id']);
     if (!empty($_POST['submit'])) {
         checkSession();
         if (!isset($_POST['name']) || Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) === '') {
             fatal_lang_error('sp_error_shoutbox_name_empty', false);
         }
         // No two the same
         $has_duplicate = sp_check_duplicate_shoutbox($_POST['name'], $_POST['shoutbox_id']);
         if (!empty($has_duplicate)) {
             fatal_lang_error('sp_error_shoutbox_name_duplicate', false);
         }
         if (isset($_POST['moderator_groups']) && is_array($_POST['moderator_groups']) && count($_POST['moderator_groups']) > 0) {
             foreach ($_POST['moderator_groups'] as $id => $group) {
                 $_POST['moderator_groups'][$id] = (int) $group;
             }
             $_POST['moderator_groups'] = implode(',', $_POST['moderator_groups']);
         } else {
             $_POST['moderator_groups'] = '';
         }
         if (!empty($_POST['allowed_bbc']) && is_array($_POST['allowed_bbc'])) {
             foreach ($_POST['allowed_bbc'] as $id => $tag) {
                 $_POST['allowed_bbc'][$id] = Util::htmlspecialchars($tag, ENT_QUOTES);
             }
             $_POST['allowed_bbc'] = implode(',', $_POST['allowed_bbc']);
         } else {
             $_POST['allowed_bbc'] = '';
         }
         $shoutbox_info = array('id' => (int) $_POST['shoutbox_id'], 'name' => Util::htmlspecialchars($_POST['name'], ENT_QUOTES), 'permissions' => (int) $_POST['permissions'], 'moderator_groups' => $_POST['moderator_groups'], 'warning' => Util::htmlspecialchars($_POST['warning'], ENT_QUOTES), 'allowed_bbc' => $_POST['allowed_bbc'], 'height' => (int) $_POST['height'], 'num_show' => (int) $_POST['num_show'], 'num_max' => (int) $_POST['num_max'], 'reverse' => !empty($_POST['reverse']) ? 1 : 0, 'caching' => !empty($_POST['caching']) ? 1 : 0, 'refresh' => (int) $_POST['refresh'], 'status' => !empty($_POST['status']) ? 1 : 0);
         // Update existing or add a new shoutbox
         $shoutbox_info['id'] = sp_edit_shoutbox($shoutbox_info, $context['SPortal']['is_new']);
         sportal_update_shoutbox($shoutbox_info['id']);
         if ($context['SPortal']['is_new'] && allowedTo(array('sp_admin', 'sp_manage_blocks'))) {
             redirectexit('action=admin;area=portalshoutbox;sa=blockredirect;shoutbox=' . $shoutbox_info['id']);
         } else {
             redirectexit('action=admin;area=portalshoutbox');
         }
     }
     if ($context['SPortal']['is_new']) {
         $context['SPortal']['shoutbox'] = array('id' => 0, 'name' => $txt['sp_shoutbox_default_name'], 'permissions' => 3, 'moderator_groups' => array(), 'warning' => '', 'allowed_bbc' => array('b', 'i', 'u', 's', 'url', 'code', 'quote', 'me'), 'height' => 200, 'num_show' => 20, 'num_max' => 1000, 'reverse' => 0, 'caching' => 1, 'refresh' => 0, 'status' => 1);
     } else {
         $_REQUEST['shoutbox_id'] = (int) $_REQUEST['shoutbox_id'];
         $context['SPortal']['shoutbox'] = sportal_get_shoutbox($_REQUEST['shoutbox_id']);
     }
     loadLanguage('Editor');
     $context['SPortal']['shoutbox']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
     sp_loadMemberGroups($context['SPortal']['shoutbox']['moderator_groups'], 'moderator', 'moderator_groups');
     if (empty($context['SPortal']['shoutbox']['permission_profiles'])) {
         fatal_lang_error('error_sp_no_permission_profiles', false);
     }
     $context['allowed_bbc'] = array('b' => $editortxt['Bold'], 'i' => $editortxt['Italic'], 'u' => $editortxt['Underline'], 's' => $editortxt['Strikethrough'], 'pre' => $editortxt['Preformatted Text'], 'img' => $editortxt['Insert an image'], 'url' => $editortxt['Insert a link'], 'email' => $editortxt['Insert an email'], 'sup' => $editortxt['Superscript'], 'sub' => $editortxt['Subscript'], 'tt' => $editortxt['Teletype'], 'code' => $editortxt['Code'], 'quote' => $editortxt['Insert a Quote'], 'size' => $editortxt['Font Size'], 'font' => $editortxt['Font Name'], 'color' => $editortxt['Font Color'], 'me' => 'me');
     $disabled_tags = array();
     if (!empty($modSettings['disabledBBC'])) {
         $disabled_tags = explode(',', $modSettings['disabledBBC']);
     }
     if (empty($modSettings['enableEmbeddedFlash'])) {
         $disabled_tags[] = 'flash';
     }
     foreach ($disabled_tags as $tag) {
         if ($tag == 'list') {
             $context['disabled_tags']['orderlist'] = true;
         }
         $context['disabled_tags'][trim($tag)] = true;
     }
     $context['page_title'] = $context['SPortal']['is_new'] ? $txt['sp_admin_shoutbox_add'] : $txt['sp_admin_shoutbox_edit'];
     $context['sub_template'] = 'shoutbox_edit';
 }