/**
* Handle basic posting setup and some basic checks
*/
function handle_basic_posting_data($check = false, $page = 'blog', $mode = 'add')
{
    global $auth, $blog_attachment, $blog_id, $config, $db, $template, $user, $phpbb_root_path, $phpEx, $category_ary;
    $submit = isset($_POST['submit']) ? true : false;
    $preview = isset($_POST['preview']) ? true : false;
    $refresh = isset($_POST['add_file']) || isset($_POST['delete_file']) || isset($_POST['cancel_unglobalise']) ? true : false;
    $submitted = $submit || $preview || $refresh ? true : false;
    // shortcut for any of the 3 above
    if ($check) {
        $error = array();
        // check the captcha
        if ($mode == 'add') {
            if (!handle_captcha('check')) {
                $error[] = $user->lang['CONFIRM_CODE_WRONG'];
            }
        }
        // check the form key
        if (!check_form_key('postform')) {
            $error[] = $user->lang['FORM_INVALID'];
        }
        return $error;
    } else {
        $above_subject = $above_message = $above_submit = $panel_data = '';
        $panels = array('options-panel' => $user->lang['OPTIONS']);
        if ($page == 'blog') {
            $category_list = make_category_select($category_ary);
            if ($category_list) {
                $panels['categories-panel'] = $user->lang['CATEGORIES'];
            }
            $panels['poll-panel'] = $user->lang['ADD_POLL'];
            if ($user->data['is_registered']) {
                // Build permissions box
                permission_settings_builder(true, $mode);
                $panels['permissions-panel'] = $user->lang['PERMISSIONS'];
            }
            // Some variables
            $template->assign_vars(array('CATEGORY_LIST' => $category_list, 'S_CAT_0_SELECTED' => is_array($category_ary) && in_array(0, $category_ary), 'S_SHOW_POLL_BOX' => true));
        }
        if ($mode == 'add') {
            // setup the captcha
            handle_captcha('build');
        }
        // Subscriptions
        if ($config['user_blog_subscription_enabled'] && $user->data['is_registered']) {
            $panels['subscriptions-panel'] = $user->lang['SUBSCRIPTION'];
            $subscription_types = get_blog_subscription_types();
            $subscribed = array();
            if ($page == 'blog' && $mode == 'add' && !$submitted) {
                // check default subscription settings from user_settings
                global $user_settings;
                get_user_settings($user->data['user_id']);
                if (isset($user_settings[$user->data['user_id']])) {
                    foreach ($subscription_types as $type => $name) {
                        // Bitwise check
                        if ($user_settings[$user->data['user_id']]['blog_subscription_default'] & $type) {
                            $subscribed[$type] = true;
                        }
                    }
                }
            } else {
                if (!$submitted) {
                    // check set subscription settings
                    $sql = 'SELECT * FROM ' . BLOGS_SUBSCRIPTION_TABLE . '
					WHERE sub_user_id = ' . $user->data['user_id'] . '
						AND blog_id = ' . intval($blog_id);
                    $result = $db->sql_query($sql);
                    while ($row = $db->sql_fetchrow($result)) {
                        $subscribed[$row['sub_type']] = true;
                    }
                }
            }
            foreach ($subscription_types as $type => $name) {
                $template->assign_block_vars('subscriptions', array('TYPE' => 'subscription_' . $type, 'NAME' => isset($user->lang[$name]) ? $user->lang[$name] : $name, 'S_CHECKED' => $submitted && request_var('subscription_' . $type, false) || isset($subscribed[$type]) ? true : false));
            }
        }
        // Attachments
        $attachment_data = $blog_attachment->attachment_data;
        $filename_data = $blog_attachment->filename_data;
        $form_enctype = @ini_get('file_uploads') == '0' || strtolower(@ini_get('file_uploads')) == 'off' || @ini_get('file_uploads') == '0' || !$config['allow_attachments'] || !$auth->acl_get('u_attach') ? '' : ' enctype="multipart/form-data"';
        posting_gen_inline_attachments($attachment_data);
        if ($auth->acl_get('u_blogattach') && $config['allow_attachments'] && $form_enctype) {
            $allowed_extensions = $blog_attachment->obtain_blog_attach_extensions();
            if (sizeof($allowed_extensions['_allowed_'])) {
                $blog_attachment->posting_gen_attachment_entry($attachment_data, $filename_data);
                $panels['attach-panel'] = $user->lang['ADD_ATTACHMENT'];
            }
        }
        // Add the forum key
        add_form_key('postform');
        // Generate smiley listing
        generate_smilies('inline', false);
        // Build custom bbcodes array
        display_custom_bbcodes();
        $temp = compact('page', 'mode', 'panels', 'panel_data', 'above_subject', 'above_message', 'above_submit');
        blog_plugins::plugin_do_ref('function_handle_basic_posting_data', $temp);
        extract($temp);
        $template->assign_vars(array('EXTRA_ABOVE_SUBJECT' => $above_subject, 'EXTRA_ABOVE_MESSAGE' => $above_message, 'EXTRA_ABOVE_SUBMIT' => $above_submit, 'EXTRA_PANELS' => $panel_data, 'JS_PANELS_LIST' => "'" . implode("', '", array_keys($panels)) . "'", 'UA_PROGRESS_BAR' => append_sid("{$phpbb_root_path}posting.{$phpEx}", "mode=popup", false), 'S_BLOG' => $page == 'blog' ? true : false, 'S_REPLY' => $page == 'reply' ? true : false, 'S_CLOSE_PROGRESS_WINDOW' => isset($_POST['add_file']) ? true : false, 'S_FORM_ENCTYPE' => $form_enctype));
        foreach ($panels as $name => $title) {
            $template->assign_vars(array('S_' . strtoupper(str_replace('-', '_', $name)) => true));
            $template->assign_block_vars('panel_list', array('NAME' => $name, 'TITLE' => $title));
        }
    }
}
    function categories($id, $mode)
    {
        global $db, $user, $auth, $template, $cache;
        global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
        $this->tpl_name = 'acp_blog_categories';
        $this->page_title = 'ACP_BLOG_CATEGORIES';
        $form_key = 'acp_blog';
        $action = request_var('action', '');
        $update = isset($_POST['update']) ? true : false;
        $category_id = request_var('c', 0);
        $this->parent_id = request_var('parent_id', 0);
        $category_data = $errors = array();
        // Clear the categories cache
        $cache->destroy('_blog_categories');
        // Major routines
        if ($update) {
            switch ($action) {
                case 'delete':
                    $action_subcategories = request_var('action_subcategories', '');
                    $subcategories_to_id = request_var('subcategories_to_id', 0);
                    $action_blogs = request_var('action_blogs', '');
                    $blogs_to_id = request_var('blogs_to_id', 0);
                    $row = $this->get_category_info($category_id);
                    $errors = $this->delete_category($category_id, $action_blogs, $action_subcategories, $blogs_to_id, $subcategories_to_id);
                    blog_plugins::plugin_do_ref('acp_category_delete', $errors);
                    if (sizeof($errors)) {
                        break;
                    }
                    add_log('admin', 'LOG_BLOG_CATEGORY_DELETE', $row['category_name']);
                    trigger_error($user->lang['CATEGORY_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
                    break;
                case 'edit':
                    $category_data = array('category_id' => $category_id);
                    // No break here
                // No break here
                case 'add':
                    $category_data += array('parent_id' => request_var('category_parent_id', $this->parent_id), 'category_name' => utf8_normalize_nfc(request_var('category_name', '', true)), 'category_description' => utf8_normalize_nfc(request_var('category_description', '', true)), 'category_description_bitfield' => '', 'category_description_uid' => '', 'category_description_options' => 7, 'rules' => utf8_normalize_nfc(request_var('rules', '', true)), 'rules_bitfield' => '', 'rules_uid' => '', 'rules_options' => 7);
                    // Get data for category rules if specified...
                    if ($category_data['rules']) {
                        generate_text_for_storage($category_data['rules'], $category_data['rules_uid'], $category_data['rules_bitfield'], $category_data['rules_options'], request_var('rules_parse_bbcode', false), request_var('rules_parse_urls', false), request_var('rules_parse_smilies', false));
                    }
                    // Get data for category description if specified
                    if ($category_data['category_description']) {
                        generate_text_for_storage($category_data['category_description'], $category_data['category_description_uid'], $category_data['category_description_bitfield'], $category_data['category_description_options'], request_var('desc_parse_bbcode', false), request_var('desc_parse_urls', false), request_var('desc_parse_smilies', false));
                    }
                    $temp = compact('action', 'category_data');
                    blog_plugins::plugin_do_ref('acp_category_add_edit', $temp);
                    extract($temp);
                    $errors = $this->update_category_data($category_data);
                    if (!sizeof($errors)) {
                        $message = $action == 'add' ? $user->lang['CATEGORY_CREATED'] : $user->lang['CATEGORY_UPDATED'];
                        trigger_error($message . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
                    }
                    break;
            }
        }
        switch ($action) {
            case 'move_up':
            case 'move_down':
                if (!$category_id) {
                    trigger_error($user->lang['NO_CATEGORY'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
                }
                $sql = 'SELECT *
					FROM ' . BLOGS_CATEGORIES_TABLE . "\n\t\t\t\t\tWHERE category_id = {$category_id}";
                $result = $db->sql_query($sql);
                $row = $db->sql_fetchrow($result);
                $db->sql_freeresult($result);
                if (!$row) {
                    trigger_error($user->lang['NO_CATEGORY'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
                }
                $move_category_name = $this->move_category_by($row, $action, 1);
                break;
            case 'add':
            case 'edit':
                // Show form to create/modify a category
                if ($action == 'edit') {
                    $this->page_title = 'EDIT_CATEGORY';
                    $row = $this->get_category_info($category_id);
                    if (!$update) {
                        $category_data = $row;
                    } else {
                        $category_data['left_id'] = $row['left_id'];
                        $category_data['right_id'] = $row['right_id'];
                    }
                    // Make sure no direct child categories are able to be selected as parents.
                    $exclude_categories = array();
                    foreach (get_category_branch($category_id, 'children') as $row) {
                        $exclude_categories[] = $row['category_id'];
                    }
                    $parents_list = make_category_select($category_data['parent_id'], $exclude_categories);
                } else {
                    $this->page_title = 'CREATE_CATEGORY';
                    $category_id = $this->parent_id;
                    $parents_list = make_category_select($this->parent_id);
                    // Fill category data with default values
                    if (!$update) {
                        $category_data = array('parent_id' => $this->parent_id, 'category_name' => utf8_normalize_nfc(request_var('category_name', '', true)), 'category_description' => '', 'rules' => '');
                    }
                }
                $rules_data = array('text' => $category_data['rules'], 'allow_bbcode' => true, 'allow_smilies' => true, 'allow_urls' => true);
                $category_description_data = array('text' => $category_data['category_description'], 'allow_bbcode' => true, 'allow_smilies' => true, 'allow_urls' => true);
                $rules_preview = '';
                // Parse rules if specified
                if ($category_data['rules']) {
                    if (!isset($category_data['rules_uid'])) {
                        // Before we are able to display the preview and plane text, we need to parse our request_var()'d value...
                        $category_data['rules_uid'] = '';
                        $category_data['rules_bitfield'] = '';
                        $category_data['rules_options'] = 0;
                        generate_text_for_storage($category_data['rules'], $category_data['rules_uid'], $category_data['rules_bitfield'], $category_data['rules_options'], request_var('rules_allow_bbcode', false), request_var('rules_allow_urls', false), request_var('rules_allow_smilies', false));
                    }
                    // Generate preview content
                    $rules_preview = generate_text_for_display($category_data['rules'], $category_data['rules_uid'], $category_data['rules_bitfield'], $category_data['rules_options']);
                    // decode...
                    $rules_data = generate_text_for_edit($category_data['rules'], $category_data['rules_uid'], $category_data['rules_options']);
                }
                // Parse desciption if specified
                if ($category_data['category_description']) {
                    if (!isset($category_data['category_description_uid'])) {
                        // Before we are able to display the preview and plane text, we need to parse our request_var()'d value...
                        $category_data['category_description_uid'] = '';
                        $category_data['category_description_bitfield'] = '';
                        $category_data['category_description_options'] = 0;
                        generate_text_for_storage($category_data['category_description'], $category_data['category_description_uid'], $category_data['category_description_bitfield'], $category_data['category_description_options'], request_var('desc_allow_bbcode', false), request_var('desc_allow_urls', false), request_var('desc_allow_smilies', false));
                    }
                    // decode...
                    $category_description_data = generate_text_for_edit($category_data['category_description'], $category_data['category_description_uid'], $category_data['category_description_options']);
                }
                $sql = 'SELECT category_id
					FROM ' . BLOGS_CATEGORIES_TABLE . "\n\t\t\t\t\t\tWHERE category_id <> {$category_id}";
                $result = $db->sql_query($sql);
                if ($db->sql_fetchrow($result)) {
                    $template->assign_vars(array('S_MOVE_CATEGORY_OPTIONS' => make_category_select($category_data['parent_id'], $category_id)));
                }
                $db->sql_freeresult($result);
                $template->assign_vars(array('S_ADD_ACTION' => $mode == 'add' ? true : false, 'S_EDIT_CATEGORY' => true, 'S_ERROR' => sizeof($errors) ? true : false, 'S_PARENT_ID' => $this->parent_id, 'S_CATEGORY_PARENT_ID' => $category_data['parent_id'], 'S_PARENT_OPTIONS' => $parents_list, 'U_BACK' => $this->u_action . '&amp;parent_id=' . $this->parent_id, 'U_EDIT_ACTION' => $this->u_action . "&amp;parent_id={$this->parent_id}&amp;action={$action}&amp;c={$category_id}", 'L_TITLE' => $user->lang[$this->page_title], 'ERROR_MSG' => sizeof($errors) ? implode('<br />', $errors) : '', 'CATEGORY_NAME' => $category_data['category_name'], 'RULES' => $category_data['rules'], 'RULES_PREVIEW' => $rules_preview, 'RULES_PLAIN' => $rules_data['text'], 'S_BBCODE_CHECKED' => $rules_data['allow_bbcode'] ? true : false, 'S_SMILIES_CHECKED' => $rules_data['allow_smilies'] ? true : false, 'S_URLS_CHECKED' => $rules_data['allow_urls'] ? true : false, 'CATEGORY_DESCRIPTION' => $category_description_data['text'], 'S_DESC_BBCODE_CHECKED' => $category_description_data['allow_bbcode'] ? true : false, 'S_DESC_SMILIES_CHECKED' => $category_description_data['allow_smilies'] ? true : false, 'S_DESC_URLS_CHECKED' => $category_description_data['allow_urls'] ? true : false, 'S_CATEGORY_OPTIONS' => make_category_select($action == 'add' ? $category_data['parent_id'] : false, $action == 'edit' ? $category_data['category_id'] : false)));
                blog_plugins::plugin_do('acp_category_add_edit_initial');
                return;
                break;
            case 'delete':
                if (!$category_id) {
                    trigger_error($user->lang['NO_CATEGORY'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
                }
                $category_data = $this->get_category_info($category_id);
                $subcategories_id = array();
                $subcategories = get_category_branch($category_id, 'children');
                foreach ($subcategories as $row) {
                    $subcategories_id[] = $row['category_id'];
                }
                $categories_list = make_category_select($category_data['parent_id'], $subcategories_id);
                $sql = 'SELECT category_id
					FROM ' . BLOGS_CATEGORIES_TABLE . "\n\t\t\t\t\t\tWHERE category_id <> {$category_id}";
                $result = $db->sql_query($sql);
                if ($db->sql_fetchrow($result)) {
                    $template->assign_vars(array('S_MOVE_CATEGORY_OPTIONS' => make_category_select($category_data['parent_id'], $subcategories_id)));
                }
                $db->sql_freeresult($result);
                $parent_id = $this->parent_id == $category_id ? 0 : $this->parent_id;
                $template->assign_vars(array('S_DELETE_CATEGORY' => true, 'U_ACTION' => $this->u_action . "&amp;parent_id={$parent_id}&amp;action=delete&amp;c={$category_id}", 'U_BACK' => $this->u_action . '&amp;parent_id=' . $this->parent_id, 'CATEGORY_NAME' => $category_data['category_name'], 'S_HAS_SUBCATEGORYS' => $category_data['right_id'] - $category_data['left_id'] > 1 ? true : false, 'S_CATEGORIES_LIST' => $categories_list, 'S_ERROR' => sizeof($errors) ? true : false, 'ERROR_MSG' => sizeof($errors) ? implode('<br />', $errors) : ''));
                return;
                break;
        }
        // Default management page
        if (!$this->parent_id) {
            $navigation = $user->lang['CATEGORY_INDEX'];
        } else {
            $navigation = '<a href="' . $this->u_action . '">' . $user->lang['CATEGORY_INDEX'] . '</a>';
            $category_nav = get_category_branch($this->parent_id, 'parents', 'descending');
            foreach ($category_nav as $row) {
                if ($row['category_id'] == $this->parent_id) {
                    $navigation .= ' -&gt; ' . $row['category_name'];
                } else {
                    $navigation .= ' -&gt; <a href="' . $this->u_action . '&amp;parent_id=' . $row['category_id'] . '">' . $row['category_name'] . '</a>';
                }
            }
        }
        // Jumpbox
        $category_box = make_category_select($this->parent_id);
        $sql = 'SELECT *
			FROM ' . BLOGS_CATEGORIES_TABLE . "\n\t\t\tWHERE parent_id = {$this->parent_id}\n\t\t\tORDER BY left_id";
        $result = $db->sql_query($sql);
        if ($row = $db->sql_fetchrow($result)) {
            do {
                $url = $this->u_action . "&amp;parent_id={$this->parent_id}&amp;c={$row['category_id']}";
                $category_title = $row['category_name'];
                $template->assign_block_vars('categories', array('CATEGORY_NAME' => $row['category_name'], 'CATEGORY_DESCRIPTION' => generate_text_for_display($row['category_description'], $row['category_description_uid'], $row['category_description_bitfield'], $row['category_description_options']), 'U_CATEGORY' => $this->u_action . '&amp;parent_id=' . $row['category_id'], 'U_MOVE_UP' => $url . '&amp;action=move_up', 'U_MOVE_DOWN' => $url . '&amp;action=move_down', 'U_EDIT' => $url . '&amp;action=edit', 'U_DELETE' => $url . '&amp;action=delete'));
            } while ($row = $db->sql_fetchrow($result));
        } else {
            if ($this->parent_id) {
                $row = $this->get_category_info($this->parent_id);
                $url = $this->u_action . '&amp;parent_id=' . $this->parent_id . '&amp;c=' . $row['category_id'];
                $template->assign_vars(array('S_NO_CATEGORIES' => true, 'U_EDIT' => $url . '&amp;action=edit', 'U_DELETE' => $url . '&amp;action=delete'));
            }
        }
        $db->sql_freeresult($result);
        $template->assign_vars(array('ERROR_MSG' => sizeof($errors) ? implode('<br />', $errors) : '', 'NAVIGATION' => $navigation, 'CATEGORY_BOX' => $category_box, 'U_SEL_ACTION' => $this->u_action, 'U_ACTION' => $this->u_action . '&amp;parent_id=' . $this->parent_id));
    }