Beispiel #1
0
/**
 * This is it - the start of rendering of a website page.
 * Take in all inputs, sends them to the correct functions to process, gathers up all the outputs, sticks them together and echoes them.
 */
function do_site()
{
    // More SEO redirection (monikers)
    // Does this URL arrangement support monikers?
    $url_id = get_param('id', NULL, true);
    if ($url_id !== NULL && url_monikers_enabled()) {
        $type = get_param('type', 'misc');
        $looking_for = '_SEARCH:' . get_page_name() . ':' . $type . ':_WILD';
        $hooks = find_all_hooks('systems', 'content_meta_aware');
        $ob_info = NULL;
        foreach (array_keys($hooks) as $hook) {
            require_code('hooks/systems/content_meta_aware/' . filter_naughty($hook));
            $ob = object_factory('Hook_content_meta_aware_' . $hook, true);
            if ($ob === NULL) {
                continue;
            }
            $ob_info = $ob->info();
            $ob_info['view_pagelink_pattern'] = preg_replace('#:[^:]*$#', ':_WILD', $ob_info['view_pagelink_pattern']);
            if ($ob_info['view_pagelink_pattern'] == $looking_for && $ob_info['support_url_monikers']) {
                if (is_numeric($url_id)) {
                    $correct_moniker = find_id_moniker(array('page' => get_page_name(), 'type' => get_param('type', 'misc'), 'id' => $url_id));
                    if ($correct_moniker !== NULL && $correct_moniker != $url_id && count($_POST) == 0) {
                        header('HTTP/1.0 301 Moved Permanently');
                        $_new_url = build_url(array('page' => '_SELF', 'id' => $correct_moniker), '_SELF', NULL, true);
                        $new_url = $_new_url->evaluate();
                        header('Location: ' . $new_url);
                        exit;
                    }
                } else {
                    // See if it is deprecated
                    if (strpos(get_db_type(), 'mysql') !== false) {
                        $monikers = $GLOBALS['SITE_DB']->query_select('url_id_monikers USE INDEX (uim_moniker)', array('m_resource_id', 'm_deprecated'), array('m_resource_page' => get_page_name(), 'm_resource_type' => get_param('type', 'misc'), 'm_moniker' => $url_id));
                    } else {
                        $monikers = $GLOBALS['SITE_DB']->query_select('url_id_monikers', array('m_resource_id', 'm_deprecated'), array('m_resource_page' => get_page_name(), 'm_resource_type' => get_param('type', 'misc'), 'm_moniker' => $url_id));
                    }
                    if (!array_key_exists(0, $monikers)) {
                        warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
                    }
                    $deprecated = $monikers[0]['m_deprecated'] == 1;
                    if ($deprecated && count($_POST) == 0) {
                        $correct_moniker = find_id_moniker(array('page' => get_page_name(), 'type' => get_param('type', 'misc'), 'id' => $monikers[0]['m_resource_id']));
                        header('HTTP/1.0 301 Moved Permanently');
                        $_new_url = build_url(array('page' => '_SELF', 'id' => $correct_moniker), '_SELF', NULL, true);
                        $new_url = $_new_url->evaluate();
                        header('Location: ' . $new_url);
                        exit;
                    } else {
                        $_GET['id'] = $monikers[0]['m_resource_id'];
                        // We need to know the ID number rather than the moniker
                    }
                }
                break;
            }
        }
    }
    // Any messages to output?
    if (get_param_integer('redirected', 0) == 1) {
        $messages = $GLOBALS['SITE_DB']->query_select('messages_to_render', array('r_message', 'r_type'), array('r_session_id' => get_session_id()), 'ORDER BY r_time DESC');
        foreach ($messages as $message) {
            if ($GLOBALS['XSS_DETECT']) {
                ocp_mark_as_escaped($message['r_message']);
            }
            attach_message(protect_from_escaping($message['r_message']), $message['r_type']);
        }
        if (count($messages) != 0) {
            $GLOBALS['SITE_DB']->query('DELETE FROM ' . $GLOBALS['SITE_DB']->get_table_prefix() . 'messages_to_render WHERE r_session_id=' . strval((int) get_session_id()) . ' OR r_time<' . strval(time() - 60 * 60));
        }
    }
    if (in_safe_mode()) {
        $disable_safe_mode_url = get_self_url(true, true, array('keep_safe_mode' => NULL));
        attach_message(do_lang_tempcode('CURRENTLY_HAS_KEEP_SAFE_MODE', escape_html($disable_safe_mode_url)), 'notice');
    }
    if (get_param_integer('keep_fatalistic', 0) == 1) {
        $disable_fatalistic_url = get_self_url(true, true, array('keep_fatalistic' => NULL));
        attach_message(do_lang_tempcode('CURRENTLY_HAS_KEEP_FATALISTIC', escape_html($disable_fatalistic_url)), 'notice');
    }
    $special_page_type = get_param('special_page_type', 'view');
    global $ZONE;
    $keep_markers = get_param_integer('keep_markers', 0);
    $show_edit_links = get_param_integer('show_edit_links', 0);
    global $KEEP_MARKERS, $SHOW_EDIT_LINKS;
    $KEEP_MARKERS = $keep_markers == 1 || $special_page_type == 'show_markers';
    if ($KEEP_MARKERS && !headers_sent()) {
        header('Content-type: text/html; charset=' . get_charset());
    }
    $SHOW_EDIT_LINKS = $show_edit_links == 1 || $special_page_type == 'show_edit_links';
    $out_evaluated = NULL;
    if ($special_page_type != 'view' && $special_page_type != 'show_markers') {
        require_code('view_modes');
        initialise_special_page_types($special_page_type);
    }
    // Set up Xdebug profiling
    if ($special_page_type == 'profile') {
        if (function_exists('xdebug_start_profiling')) {
            xdebug_start_profiling();
        }
        // xdebug 1 style
        if (ini_get('xdebug.profiler_enable') != '1') {
            attach_message(escape_html('Profiling must be enabled in php.ini'), 'warn');
        }
        // xdebug 2 style
        if (!is_writable_wrap(ini_get('xdebug.profiler_output_dir'))) {
            attach_message(escape_html('xdebug.profiler_output_dir needs setting to a writable directory'), 'warn');
        }
    }
    // Allow the site to be closed
    $site_closed = get_option('site_closed');
    if ($site_closed == '1' && !has_specific_permission(get_member(), 'access_closed_site') && !$GLOBALS['IS_ACTUALLY_ADMIN']) {
        require_code('site2');
        closed_site();
    }
    // Work out which page we're viewing
    global $PAGE;
    $PAGE = get_page_name();
    $doing_special_page_type = $special_page_type != 'view' && $special_page_type != 'show_markers' && $special_page_type != 'show_edit_links' && $special_page_type != 'memory' && (has_specific_permission(get_member(), 'view_profiling_modes') || $GLOBALS['IS_ACTUALLY_ADMIN']);
    // Load up our frames into strings. Note that the header and the footer are fixed already.
    $middle = request_page($PAGE, true);
    global $CYCLES;
    $CYCLES = array();
    // Here we reset some Tempcode environmental stuff, because template compilation or preprocessing may have dirtied things
    if ($middle === NULL || $middle->is_definitely_empty()) {
        $GLOBALS['HTTP_STATUS_CODE'] = '404';
        if (!headers_sent()) {
            if (!browser_matches('ie') && strpos(ocp_srv('SERVER_SOFTWARE'), 'IIS') === false) {
                header('HTTP/1.0 404 Not Found');
            }
        }
        $title = get_page_title('ERROR_OCCURRED');
        $text = do_lang_tempcode('NO_PAGE_OUTPUT');
        $middle = warn_screen($title, $text, false);
    }
    // Extra stuff we can tag on (like messages)
    $additional = new ocp_tempcode();
    $site_closed = get_option('site_closed');
    // May have been JUST changed in page load - think Setup Wizard
    if ($site_closed == '1' && $PAGE != 'login' && $PAGE != 'join' && get_param_integer('wide_high', 0) == 0) {
        $additional->attach(do_template('ADDITIONAL', array('_GUID' => '03a41a91606b3ad05330e7d6f3e741c1', 'TYPE' => 'notice', 'MESSAGE' => do_lang_tempcode(has_specific_permission(get_member(), 'access_closed_site') ? 'SITE_SPECIAL_ACCESS' : 'SITE_SPECIAL_ACCESS_SU'))));
    }
    if ($GLOBALS['IS_ACTUALLY_ADMIN']) {
        $unsu_link = get_self_url(true, true, array('keep_su' => NULL));
        $su_username = $GLOBALS['FORUM_DRIVER']->get_username(get_member());
        $additional->attach(do_template('ADDITIONAL', array('_GUID' => '13a41a91606b3ad05330e7d6f3e741c1', 'TYPE' => 'notice', 'MESSAGE' => do_lang_tempcode('USING_SU', escape_html($unsu_link), escape_html($su_username)))));
    }
    $out = new ocp_tempcode();
    // This is important - it makes sure the tempcode tree appears nicely
    $middle->handle_symbol_preprocessing();
    // Due to the '->evaluate()' below, plus so that some symbol preprocessing can be passed into header
    $out->attach(do_header());
    if (function_exists('memory_get_usage') && get_param('special_page_type', '') == 'memory') {
        if (function_exists('memory_get_peak_usage')) {
            $memory_usage = memory_get_peak_usage();
        } else {
            $memory_usage = memory_get_usage();
        }
        $additional->attach(do_template('ADDITIONAL', array('_GUID' => 'd605c0d111742a8cd2d4ef270a1e5fe1', 'TYPE' => 'inform', 'MESSAGE' => do_lang_tempcode('MEMORY_USAGE', float_format(round(floatval($memory_usage) / 1024.0 / 1024.0, 2))))));
    }
    // Whack it into our global template
    global $ATTACHED_MESSAGES;
    $global_template = 'GLOBAL';
    if (get_option('show_docs') == '0') {
        $GLOBALS['HELPER_PANEL_TUTORIAL'] = '';
    }
    $helper_panel_pic = $GLOBALS['HELPER_PANEL_PIC'];
    if ($helper_panel_pic != '') {
        if (find_theme_image($helper_panel_pic, true) == '') {
            $helper_panel_pic = '';
        }
    }
    $global = do_template($global_template, array('HELPER_PANEL_TUTORIAL' => $GLOBALS['HELPER_PANEL_TUTORIAL'], 'HELPER_PANEL_HTML' => $GLOBALS['HELPER_PANEL_HTML'], 'HELPER_PANEL_TEXT' => $GLOBALS['HELPER_PANEL_TEXT'], 'HELPER_PANEL_PIC' => $helper_panel_pic, 'MIDDLE' => $doing_special_page_type ? $middle : $middle->evaluate(), 'MESSAGE_TOP' => $ATTACHED_MESSAGES, 'MESSAGE' => $additional, 'BREADCRUMBS' => breadcrumbs()));
    unset($middle);
    $out->attach($global);
    $out->attach(do_footer());
    $out->handle_symbol_preprocessing();
    if (get_value('xhtml_strict') === '1') {
        $out = make_xhtml_strict($out);
    }
    // Validation
    $novalidate = get_param_integer('keep_novalidate', get_param_integer('novalidate', 0));
    $show_edit_links = get_param_integer('show_edit_links', 0);
    if ((in_array(ocp_srv('HTTP_HOST'), array('localhost', 'test.ocportal.com')) || $GLOBALS['FORUM_DRIVER']->is_staff(get_member())) && ($special_page_type == 'code' || $novalidate == 0 && get_option('validation') == '1') && $GLOBALS['REFRESH_URL'][0] == '' && $show_edit_links == 0) {
        require_code('view_modes');
        $out_evaluated = $out->evaluate(NULL, false);
        do_xhtml_validation($out_evaluated, $special_page_type == 'code' && get_param_integer('preview_mode', NULL) === NULL, get_param_integer('preview_mode', 0));
    }
    // Cacheing for spiders
    if (running_script('index') && count($_POST) == 0 && isset($GLOBALS['SITE_INFO']['fast_spider_cache']) && $GLOBALS['SITE_INFO']['fast_spider_cache'] == '1' && is_guest()) {
        $bot_type = get_bot_type();
        if (($bot_type !== NULL || isset($GLOBALS['SITE_INFO']['any_guest_cached_too']) && $GLOBALS['SITE_INFO']['any_guest_cached_too'] == '1') && can_fast_spider_cache()) {
            $fast_cache_path = get_custom_file_base() . '/persistant_cache/' . md5(serialize(get_self_url_easy()));
            if ($bot_type === NULL) {
                $fast_cache_path .= '__non-bot';
            }
            if (!array_key_exists('js_on', $_COOKIE)) {
                $fast_cache_path .= '__no-js';
            }
            if (is_mobile()) {
                $fast_cache_path .= '_mobile';
            }
            $fast_cache_path .= '.gcd';
            if (!is_dir(get_custom_file_base() . '/persistant_cache/')) {
                if (@mkdir(get_custom_file_base() . '/persistant_cache/', 0777)) {
                    fix_permissions(get_custom_file_base() . '/persistant_cache/', 0777);
                    sync_file(get_custom_file_base() . '/persistant_cache/');
                } else {
                    intelligent_write_error($fast_cache_path);
                }
            }
            $out_evaluated = $out->evaluate(NULL, false);
            $myfile = @fopen($fast_cache_path, 'wb') or intelligent_write_error($fast_cache_path);
            if (function_exists('gzencode')) {
                fwrite($myfile, gzencode($out_evaluated, 9));
            } else {
                fwrite($myfile, $out_evaluated);
            }
            fclose($myfile);
            fix_permissions($fast_cache_path);
            sync_file($fast_cache_path);
        }
    }
    if ($doing_special_page_type) {
        special_page_types($special_page_type, $out, $out_evaluated);
    }
    // We calculated the time before outputting so that latency and bandwidth do not adversely affect the result
    global $PAGE_START_TIME, $PAGE_STRING;
    $page_generation_time = microtime_diff($PAGE_START_TIME, microtime(false));
    if (!$GLOBALS['QUICK_REDIRECT']) {
        if ($out_evaluated !== NULL) {
            echo $out_evaluated;
        } else {
            $GLOBALS['FINISHING_OUTPUT'] = true;
            $out->evaluate_echo();
        }
    }
    // Finally, stats
    if ($PAGE_STRING !== NULL) {
        log_stats($PAGE_STRING, intval($page_generation_time));
    }
    // When someone hits the Admin Zone front page.
    if ($ZONE['zone_name'] == 'adminzone' && get_page_name() == 'start') {
        // Security feature admins can turn on
        require_code('notifications');
        $current_username = $GLOBALS['FORUM_DRIVER']->get_username(get_member());
        $subject = do_lang('AFA_NOTIFICATION_MAIL_SUBJECT', $current_username, get_site_name(), get_ip_address());
        $mail = do_lang('AFA_NOTIFICATION_MAIL', comcode_escape(get_site_name()), comcode_escape($current_username), comcode_escape(get_ip_address()));
        dispatch_notification('adminzone_frontpage_accessed', NULL, $subject, $mail);
        // Track very basic details of what sites use ocPortal. You can remove if you like.
        if (preg_match('#^localhost[\\.\\:$]?#', ocp_srv('HTTP_HOST')) == 0) {
            global $EXPIRE, $KEY;
            $timeout_before = @ini_get('default_socket_timeout');
            @ini_set('default_socket_timeout', '3');
            http_download_file('http://ocportal.com/user.php?url=' . urlencode(get_base_url()) . '&name=' . urlencode(get_site_name()) . '&registered=2&key=' . ($KEY === NULL ? '' : strval($KEY)) . '&expire=' . ($EXPIRE === NULL ? '' : strval($EXPIRE)) . '&version=' . urlencode(ocp_version_full()), NULL, false);
            @ini_set('default_socket_timeout', $timeout_before);
        }
    }
    // Little disk space check
    $last_space_check = get_value('last_space_check');
    if ($last_space_check === NULL || intval($last_space_check) < time() - 60 * 60 * 3) {
        set_value('last_space_check', strval(time()));
        $low_space_check = intval(get_option('low_space_check')) * 1024 * 1024;
        $disk_space = @disk_free_space(get_file_base());
        if (is_integer($disk_space) && $disk_space < $low_space_check) {
            require_code('notifications');
            $subject = do_lang('LOW_DISK_SPACE_SUBJECT', NULL, NULL, NULL, get_site_default_lang());
            $message = do_lang('LOW_DISK_SPACE_MAIL', strval(intval(round($disk_space / 1024 / 1024))), NULL, NULL, get_site_default_lang());
            dispatch_notification('low_disk_space', NULL, $subject, $message, NULL, A_FROM_SYSTEM_PRIVILEGED);
        }
    }
    //exit();
}
Beispiel #2
0
/**
 * Build up a preview based on what was submitted.
 *
 * @param  boolean	Whether to return additional data
 * @return mixed		Either tempcode for the preview, or a tuple of details
 */
function build_preview($multi_return = false)
{
    // Check CAPTCHA if it is passed
    if (addon_installed('captcha')) {
        if (array_key_exists('post', $_POST) && $_POST['post'] != '' && array_key_exists('security_image', $_POST)) {
            require_code('captcha');
            enforce_captcha(false);
        }
    }
    require_code('attachments2');
    $hooks = find_all_hooks('systems', 'preview');
    $output = NULL;
    $new_post_value = NULL;
    $attachment_type = NULL;
    $forum_db = false;
    $limit_to = NULL;
    foreach (array_keys($hooks) as $hook) {
        require_code('hooks/systems/preview/' . $hook);
        $object = object_factory('Hook_Preview_' . $hook, true);
        if (is_null($object)) {
            continue;
        }
        $apply_bits = $object->applies();
        $applies = $apply_bits[0];
        if ($applies) {
            $attachment_type = $apply_bits[1];
            $forum_db = array_key_exists(2, $apply_bits) ? $apply_bits[2] : false;
            $limit_to = array_key_exists(3, $apply_bits) ? $apply_bits[3] : NULL;
            if (method_exists($object, 'run')) {
                list($output, $new_post_value) = $object->run();
            }
            break;
        }
    }
    $validation = new ocp_tempcode();
    $keyword_density = new ocp_tempcode();
    $spelling = new ocp_tempcode();
    $meta_keywords = post_param('meta_keywords', '');
    $spellcheck = post_param_integer('perform_spellcheck', 0) == 1;
    $keywordcheck = post_param_integer('perform_keywordcheck', 0) == 1 && $meta_keywords != '';
    if (post_param_integer('perform_validation', 0) != 0) {
        foreach ($_POST as $key => $val) {
            if (!is_string($val)) {
                continue;
            }
            $val = post_param($key, '');
            // stripslashes, and wysiwyg output handling
            $tempcodecss = post_param_integer('tempcodecss__' . $key, 0) == 1;
            $supports_comcode = post_param_integer('comcode__' . $key, 0) == 1;
            if ($supports_comcode) {
                $temp = $_FILES;
                $_FILES = array();
                $valt = comcode_to_tempcode($val);
                $_FILES = $temp;
                require_code('view_modes');
                require_code('obfuscate');
                require_code('validation');
                $validation->attach(do_xhtml_validation($valt->evaluate(), false, post_param_integer('perform_validation', 0), true));
            } elseif ($tempcodecss) {
                $i = 0;
                $color = post_param(strval($i), '');
                while ($color != '') {
                    $val = str_replace('<color-' . strval($i) . '>', '#' . $color, $val);
                    $i++;
                    $color = post_param(strval($i), '');
                }
                $_val_orig = $val;
                require_lang('validation');
                require_css('adminzone');
                require_code('view_modes');
                require_code('obfuscate');
                require_code('validation');
                require_code('validation2');
                $error = check_css($_val_orig);
                $show = count($error['errors']) != 0;
                if ($show) {
                    $validation->attach(display_validation_results($_val_orig, $error, true, true));
                }
            }
        }
    }
    if ($spellcheck) {
        if (addon_installed('wordfilter')) {
            $words_skip = collapse_1d_complexity('w_replacement', $GLOBALS['SITE_DB']->query_select('wordfilter', array('w_replacement')));
        } else {
            $words_skip = array();
        }
        require_once get_file_base() . '/data/areaedit/plugins/SpellChecker/spell-check-logic.php';
    }
    $db = $forum_db ? $GLOBALS['FORUM_DB'] : $GLOBALS['SITE_DB'];
    $view_space_map = array();
    require_code('templates_view_space');
    foreach ($_POST as $key => $val) {
        if (!is_string($val)) {
            continue;
        }
        if (!is_null($limit_to) && !in_array($key, $limit_to)) {
            continue;
        }
        $val = post_param($key, '');
        // stripslashes, and wysiwyg output handling
        if ($val == '0') {
            $val = do_lang('NO');
        }
        if ($val == '1') {
            $val = do_lang('YES');
        }
        if (substr($key, 0, 14) == 'review_rating' || substr($key, 0, 7) == 'rating') {
            $val .= '/10';
        }
        $is_hidden = in_array($key, array('from_url', 'password', 'confirm_password', 'edit_password', 'MAX_FILE_SIZE', 'perform_validation', '_validated', 'id', 'posting_ref_id', 'f_face', 'f_colour', 'f_size', 'http_referer')) || strpos($key, 'hour') !== false || strpos($key, 'access_') !== false || strpos($key, 'minute') !== false || strpos($key, 'confirm') !== false || strpos($key, 'pre_f_') !== false || strpos($key, 'label_for__') !== false || strpos($key, 'wysiwyg_version_of_') !== false || strpos($key, 'is_wysiwyg') !== false || strpos($key, 'require__') !== false || strpos($key, 'tempcodecss__') !== false || strpos($key, 'comcode__') !== false || strpos($key, '_parsed') !== false || preg_match('#^caption\\d+$#', $key) != 0 || preg_match('#^attachmenttype\\d+$#', $key) != 0 || substr($key, 0, 1) == '_' || substr($key, 0, 9) == 'hidFileID' || substr($key, 0, 11) == 'hidFileName';
        if (substr($key, 0, 14) == 'tick_on_form__') {
            if (post_param_integer(substr($key, 14), 0) == 1) {
                $is_hidden = true;
            } else {
                $key = substr($key, 14);
            }
        }
        if (substr($key, -4) == '_day') {
            $key = substr($key, 0, strlen($key) - 4);
            $timestamp = get_input_date($key);
            if (is_null($timestamp)) {
                $is_hidden = true;
            } else {
                $val = get_timezoned_date($timestamp, false, true, false, true);
            }
        } elseif (substr($key, -6) == '_month' || substr($key, -5) == '_year') {
            $is_hidden = true;
        }
        $key_nice = post_param('label_for__' . $key, ucwords(str_replace('_', ' ', $key)));
        if ($key_nice == '') {
            $is_hidden = true;
        }
        if (!$is_hidden) {
            if ($spellcheck) {
                require_code('comcode_from_html');
                $mispellings = spellchecklogic('check', strip_comcode(semihtml_to_comcode($val, true)), $words_skip, true);
                $_misspellings = array();
                foreach ($mispellings as $misspelling) {
                    list($word_bad, $words_good) = $misspelling;
                    $_misspellings[] = array('WORD' => $word_bad, 'CORRECTIONS' => implode(', ', $words_good));
                }
                if (count($_misspellings) != 0) {
                    $spelling->attach(do_template('PREVIEW_SCRIPT_SPELLING', array('_GUID' => '9649572982c01995a8f47c58d16fda39', 'FIELD' => $key_nice, 'MISSPELLINGS' => $_misspellings)));
                }
            }
            if ($keywordcheck && (strpos($val, ' ') !== false || $key == 'title')) {
                $keyword_explode = explode(',', $meta_keywords);
                $keywords = array();
                $word_count = str_word_count($val);
                if ($word_count != 0) {
                    foreach ($keyword_explode as $meta_keyword) {
                        $meta_keyword = trim($meta_keyword);
                        if ($meta_keyword != '') {
                            $density = substr_count($val, $meta_keyword) / $word_count;
                            $ideal_density = 1.0 / (9.0 * count($keyword_explode));
                            // Pretty rough -- common sense is needed
                            $keywords[] = array('sort' => $ideal_density, 'KEYWORD' => $meta_keyword, 'IDEAL_DENSITY' => strval(intval(round($ideal_density * 100))), 'DENSITY' => strval(intval(round($density * 100))));
                        }
                    }
                    global $M_SORT_KEY;
                    $M_SORT_KEY = 'sort';
                    usort($keywords, 'multi_sort');
                    foreach ($keywords as $ti => $meta_keyword) {
                        unset($keywords[$ti]['sort']);
                    }
                    if (count($keywords) != 0) {
                        $keyword_density->attach(do_template('PREVIEW_SCRIPT_KEYWORD_DENSITY', array('_GUID' => '4fa05e9f52023958a3594d1610b00747', 'FIELD' => $key_nice, 'KEYWORDS' => $keywords)));
                    }
                }
            }
        }
        if (is_null($output)) {
            if (is_null($attachment_type) || $key != 'post') {
                $tempcodecss = post_param_integer('tempcodecss__' . $key, 0) == 1;
                $supports_comcode = post_param_integer('comcode__' . $key, 0) == 1;
                $preformatted = post_param_integer('pre_f_' . $key, 0) == 1;
                if ($is_hidden) {
                    continue;
                }
                if ($preformatted) {
                    $valt = with_whitespace($val);
                } elseif ($supports_comcode) {
                    $valt = comcode_to_tempcode($val);
                } elseif ($tempcodecss) {
                    $i = 0;
                    $color = post_param(strval($i), '');
                    while ($color != '') {
                        $val = str_replace('<color-' . strval($i) . '>', '#' . $color, $val);
                        $i++;
                        $color = post_param(strval($i), '');
                    }
                    $_val_orig = $val;
                    $valt = comcode_to_tempcode("[code=\"CSS\"]" . $val . "[/code]");
                } else {
                    $valt = make_string_tempcode(escape_html($val));
                }
                $view_space_map[$key_nice] = $valt;
            } else {
                $tempcodecss = false;
                $posting_ref_id = post_param_integer('posting_ref_id');
                if ($posting_ref_id < 0) {
                    fatal_exit(do_lang_tempcode('INTERNAL_ERROR'));
                }
                $post_bits = do_comcode_attachments($val, $attachment_type, strval(-$posting_ref_id), true, $db);
                $new_post_value = $post_bits['comcode'];
                $view_space_map[$key_nice] = $post_bits['tempcode'];
                $val = $post_bits['tempcode'];
                $supports_comcode = true;
            }
        }
    }
    // Make attachments temporarily readable without any permission context
    global $COMCODE_ATTACHMENTS;
    $posting_ref_id = post_param_integer('posting_ref_id', NULL);
    if (!is_null($posting_ref_id)) {
        if (array_key_exists(strval(-$posting_ref_id), $COMCODE_ATTACHMENTS)) {
            foreach ($COMCODE_ATTACHMENTS[strval(-$posting_ref_id)] as $attachment) {
                $db->query_delete('attachment_refs', array('r_referer_type' => 'null', 'r_referer_id' => strval(-$posting_ref_id), 'a_id' => $attachment['id']), '', 1);
                $db->query_insert('attachment_refs', array('r_referer_type' => 'null', 'r_referer_id' => strval(-$posting_ref_id), 'a_id' => $attachment['id']));
            }
        }
    }
    if (is_null($output)) {
        if (count($view_space_map) == 1) {
            $output = array_pop($view_space_map);
        } else {
            $view_space_fields = new ocp_tempcode();
            foreach ($view_space_map as $key => $val) {
                $view_space_fields->attach(view_space_field($key, $val, true));
            }
            $output = do_template('VIEW_SPACE', array('_GUID' => '3f548883b9eb37054c500d1088d9efa3', 'WIDTH' => '170', 'FIELDS' => $view_space_fields));
        }
    }
    // This is to get the Comcode attachments updated to the new IDs
    if (!is_null($new_post_value)) {
        $new_post_value_html = comcode_to_tempcode($new_post_value, NULL, false, 60, NULL, $db, true);
        if (strpos($new_post_value_html->evaluate(), '<!-- CC-error -->') === false) {
            $output->attach(do_template('PREVIEW_SCRIPT_CODE', array('_GUID' => 'bc7432af91e1eaf212dc210f3bf2f756', 'NEW_POST_VALUE_HTML' => $new_post_value_html, 'NEW_POST_VALUE' => $new_post_value)));
        }
    }
    $output->handle_symbol_preprocessing();
    if ($multi_return) {
        return array($output, $validation, $keyword_density, $spelling);
    }
    return $output;
}