/** * Get information about new versions of ocPortal (or more accurately, what's wrong with this version). * * @return tempcode Information about the installed ocPortal version */ function get_future_version_information() { require_lang('version'); $url = 'http://ocportal.com/version.php?version=' . rawurlencode(ocp_version_full()) . '&lang=' . rawurlencode(user_lang()); $data = http_download_file($url, NULL, false); if (!is_null($data)) { $data = str_replace('"../upgrader.php"', '"' . get_base_url() . '/upgrader.php"', $data); if ($GLOBALS['XSS_DETECT']) { ocp_mark_as_escaped($data); } require_code('character_sets'); $data = convert_to_internal_encoding($data); $table = new ocp_tempcode(); $lines = explode(chr(10), $data); foreach ($lines as $line) { if (trim($line) != '') { $table->attach(paragraph($line)); } } $table = make_string_tempcode(preg_replace('#<p>\\s*</p>#', '', $table->evaluate())); } else { $table = paragraph(do_lang_tempcode('CANNOT_CONNECT_HOME'), 'dfsdff32ffd'); } require_code('xhtml'); /*$table->attach('<script type="text/javascript">// <![CDATA[ window.open(\''.$url.'\'); //]]></script>');*/ return make_string_tempcode(xhtmlise_html($table->evaluate())); }
/** * Standard modular run function. * * @return tempcode The result of execution. */ function run() { if (get_file_base() != get_custom_file_base()) { warn_exit(do_lang_tempcode('SHARED_INSTALL_PROHIBIT')); } require_lang('menus'); get_page_title('PHP_INFO'); $GLOBALS['SCREEN_TEMPLATE_CALLED'] = ''; $GLOBALS['TITLE_CALLED'] = true; require_lang('menus'); $GLOBALS['HELPER_PANEL_TEXT'] = comcode_lang_string('DOC_PHP_INFO'); ob_start(); phpinfo(); require_code('xhtml'); $out = xhtmlise_html(ob_get_contents()); ob_end_clean(); $out = preg_replace('#<!DOCTYPE[^>]*>#s', '', preg_replace('#</body[^>]*>#', '', preg_replace('#<body[^>]*>#', '', preg_replace('#</html[^>]*>#', '', preg_replace('#<html[^>]*>#', '', $out))))); $matches = array(); if (preg_match('#<style[^>]*>#', $out, $matches) != 0) { $offset = strpos($out, $matches[0]) + strlen($matches[0]); $end = strpos($out, '</style>', $offset); if ($end !== false) { $style = substr($out, $offset - strlen($matches[0]), $end - $offset + strlen('</style>') + strlen($matches[0])); //$GLOBALS['EXTRA_HEAD']=make_string_tempcode($style); $out = substr($out, 0, $offset) . substr($out, $end); } } $out = preg_replace('#<head[^>]*>.*</head[^>]*>#s', '', $out); $out = str_replace(' width="600"', ' width="100%"', $out); $out = preg_replace('#([^\\s<>"\']{65}&[^;]+;)#', '${1}<br />', $out); $out = preg_replace('#([^\\s<>"\']{95})#', '${1}<br />', $out); $url_parts = parse_url(get_base_url()); $out = str_replace('<img border="0" src="/', '<img border="0" style="padding-top: 20px" src="http://' . escape_html($url_parts['host']) . '/', $out); require_css('phpinfo'); require_code('xhtml'); $ret = make_string_tempcode(xhtmlise_html($out)); return $ret; }
/** * Get an array of topics in the given forum. Each topic is an array with the following attributes: * - id, the topic ID * - title, the topic title * - lastusername, the username of the last poster * - lasttime, the timestamp of the last reply * - closed, a Boolean for whether the topic is currently closed or not * - firsttitle, the title of the first post * - firstpost, the first post (only set if $show_first_posts was true) * * @param mixed The forum name or an array of forum IDs * @param integer The limit * @param integer The start position * @param integer The total rows (not a parameter: returns by reference) * @param SHORT_TEXT The topic title filter * @param boolean Whether to show the first posts * @param string The date key to sort by * @set lasttime firsttime * @param boolean Whether to limit to hot topics * @param SHORT_TEXT The topic description filter * @return ?array The array of topics (NULL: error) */ function show_forum_topics($name, $limit, $start, &$max_rows, $filter_topic_title = '', $show_first_posts = false, $date_key = 'lasttime', $hot = false, $filter_topic_description = '') { require_code('xhtml'); if (is_integer($name)) { $id_list = 'forum_id=' . strval((int) $name); } elseif (!is_array($name)) { if ($name == '<announce>' || is_null($name)) { $id_list = '(forum_id IS NULL)'; } else { $id = $this->forum_id_from_name($name); if (is_null($id)) { return NULL; } $id_list = 'forum_id=' . strval((int) $id); } } else { $id_list = ''; $id_list_2 = ''; foreach (array_keys($name) as $id) { if ($id_list != '') { $id_list .= ' OR '; } if (is_null($id) || $id == '') { $id_list .= '(forum_id IS NULL)'; } else { $id_list .= 'forum_id=' . strval((int) $id); } } if ($id_list == '') { return NULL; } } $topic_filter = $filter_topic_title != '' ? 'AND title LIKE \'' . db_encode_like($this->ipb_escape($filter_topic_title)) . '\'' : ''; if ($filter_topic_description != '') { $topic_filter .= ' AND description LIKE \'' . db_encode_like($this->ipb_escape($filter_topic_description)) . '\''; } $rows = $this->connection->query('SELECT * FROM ' . $this->connection->get_table_prefix() . 'topics WHERE (' . $id_list . ') ' . $topic_filter . ' ORDER BY ' . ($date_key == 'lasttime' ? 'last_post' : 'start_date') . ' DESC', $limit, $start); $max_rows = $this->connection->query_value_null_ok_full('SELECT COUNT(*) FROM ' . $this->connection->get_table_prefix() . 'topics WHERE (' . $id_list . ') ' . $topic_filter); $emoticons_set_dir = $this->get_emo_dir(); $out = array(); foreach ($rows as $i => $r) { $out[$i] = array(); $out[$i]['id'] = $r['tid']; $out[$i]['num'] = $r['posts']; $out[$i]['title'] = $this->ipb_unescape($r['title']); $out[$i]['firstusername'] = $this->ipb_unescape($r['starter_name']); $out[$i]['lastusername'] = $this->ipb_unescape($r['last_poster_name']); $out[$i]['firstmemberid'] = $r['starter_id']; $out[$i]['lastmemberid'] = $r['last_poster_id']; $out[$i]['firsttime'] = $r['start_date']; $out[$i]['lasttime'] = $r['last_post']; $out[$i]['closed'] = $r['state'] == 'closed'; $fp_rows = $this->connection->query('SELECT * FROM ' . $this->connection->get_table_prefix() . 'posts WHERE post NOT LIKE \'' . db_encode_like(do_lang('SPACER_POST', '', '', '', get_site_default_lang()) . '%') . '\' AND topic_id=' . strval((int) $out[$i]['id']) . ' ORDER BY post_date', 1); if (!array_key_exists(0, $fp_rows)) { unset($out[$i]); continue; } $out[$i]['firsttitle'] = $this->ipb_unescape($fp_rows[0]['post_title']); if ($show_first_posts) { $post_id = $fp_rows[0]['pid']; $post = $fp_rows[0]['post']; if (array_key_exists('post_htmlstate', $fp_rows[0]) && $fp_rows[0]['post_htmlstate'] != 0) { if ($fp_rows[0]['post_htmlstate'] == 1) { $post = str_replace('<br />', '', $post); } $post = @html_entity_decode($post, ENT_QUOTES, get_charset()); } $post = preg_replace('#public/style_emoticons/<\\#EMO_DIR\\#>(.+?)\'#is', $emoticons_set_dir . '\\1\'', $post); $post = str_replace("class='quotetop'", "class='comcode_quote_h4'", $post); $post = str_replace("class='quotemain'", "class='comcode_quote_content'", $post); // Attachments $attachments = $this->connection->query_select('attachments', array('attach_member_id', 'attach_id', 'attach_file', 'attach_location', 'attach_thumb_location', 'attach_is_image', 'attach_filesize', 'attach_hits'), array('attach_post_key' => $fp_rows[0]['post_key'])); foreach ($attachments as $attachment) { if ($attachment['attach_thumb_location'] != '' || $attachment['attach_is_image'] == 0) { $url = get_forum_base_url() . '/index.php?act=Attach&type=post&id=' . $attachment['attach_id']; if ($attachment['attach_thumb_location'] != '') { $special = do_template('FORUM_ATTACHMENT_IMAGE_THUMB', array('_GUID' => '98a66462f270f53101c4c0a1b63f0bfc', 'FULL' => $url, 'URL' => get_forum_base_url() . '/uploads/' . $attachment['attach_thumb_location'])); } else { $special = do_template('FORUM_ATTACHMENT_LINK', array('_GUID' => '002a3220f35debbe567ce7a225aa221e', 'FULL' => $url, 'FILENAME' => $attachment['attach_file'], 'CLEAN_SIZE' => clean_file_size($attachment['attach_filesize']), 'NUM_DOWNLOADS' => integer_format($attachment['attach_hits']))); } } else { $special = do_template('FORUM_ATTACHMENT_IMAGE', array('_GUID' => '49dbf65cb5e20340a5ad4379ea6344c3', 'URL' => get_forum_base_url() . '/uploads/' . $attachment['attach_location'])); } // See if we have to place it somewhere special inside the post $old_post = $post; $post = str_replace('[attachmentid=' . $attachment['attach_id'] . ']', $special->evaluate(), $post); if ($old_post == $post) { $post .= $special->evaluate(); } } global $LAX_COMCODE; $end = 0; while (($pos = strpos($post, '[right]', $end)) !== false) { $e_pos = strpos($post, '[/right]', $pos); if ($e_pos === false) { break; } $end = $e_pos + strlen('[/right]'); $segment = substr($post, $pos, $end - $pos); $temp = $LAX_COMCODE; $LAX_COMCODE = true; $comcode = comcode_to_tempcode($segment, $r['starter_id']); $LAX_COMCODE = $temp; $post = substr($post, 0, $pos) . $comcode->evaluate() . substr($post, $end); } $temp = $LAX_COMCODE; $LAX_COMCODE = true; $out[$i]['firstpost'] = comcode_to_tempcode(xhtmlise_html($post), $r['starter_id'], false, 60, NULL, NULL, false, false, true); // Assumes HTML for posts $LAX_COMCODE = $temp; } } if (count($out) != 0) { return $out; } return NULL; }
/** * The UI for the zone editor. * * @return tempcode The UI */ function _editor() { $id = get_param('id', ''); // '' needed for short URLs if ($id == '/') { $id = ''; } $GLOBALS['EXTRA_HEAD']->attach(make_string_tempcode('<base target="_blank" />')); $nice_zone_name = $id == '' ? do_lang('_WELCOME') : $id; $title = get_page_title('_ZONE_EDITOR', true, array(escape_html($nice_zone_name))); $lang = choose_language($title, true); if (is_object($lang)) { return $lang; } require_javascript('javascript_zone_editor'); require_javascript('javascript_ajax'); require_javascript('javascript_more'); require_javascript('javascript_posting'); require_javascript('javascript_editing'); require_javascript('javascript_validation'); require_code('form_templates'); require_lang('comcode'); if (!has_js()) { // Send them to the page permissions screen $url = build_url(array('page' => '_SELF', 'type' => 'edit'), '_SELF'); require_code('site2'); assign_refresh($url, 5.0); return do_template('REDIRECT_SCREEN', array('_GUID' => '20ed5fa100b87756a77c48988ef856ae', 'URL' => $url, 'TITLE' => $title, 'TEXT' => do_lang_tempcode('NO_JS_ADVANCED_SCREEN_ZONE_EDITOR'))); } // After completion prep/relay $_default_redirect = build_url(array('page' => ''), $id); $default_redirect = $_default_redirect->evaluate(); $post_url = build_url(array('page' => '_SELF', 'type' => '__editor', 'lang' => $lang, 'redirect' => get_param('redirect', $default_redirect), 'id' => $id), '_SELF'); // Zone editing stuff $rows = $GLOBALS['SITE_DB']->query_select('zones', array('*'), array('zone_name' => $id), '', 1); if (!array_key_exists(0, $rows)) { warn_exit(do_lang_tempcode('MISSING_RESOURCE')); } $row = $rows[0]; $header_text = get_translated_text($row['zone_header_text'], NULL, $lang); $default_page = $row['zone_default_page']; list($fields, , ) = $this->get_form_fields(true, get_translated_text($row['zone_title'], NULL, $lang), $default_page, $header_text, $row['zone_theme'], $row['zone_wide'], $row['zone_require_session'], $row['zone_displayed_in_menu'], $id); // Page editing stuff $editor = array(); foreach (array('panel_left', $default_page, 'panel_right') as $i => $for) { $page_info = _request_page($for, $id, NULL, $lang); if ($page_info === false) { $page_info = array('COMCODE_CUSTOM', $id, $for, $lang); } $is_comcode = false; $redirecting_to = NULL; $current_for = $for; $pure = false; switch ($page_info[0]) { case 'COMCODE_CUSTOM_PURE': $pure = true; case 'COMCODE': case 'COMCODE_CUSTOM': $is_comcode = true; $type = do_lang_tempcode('COMCODE_PAGE'); break; case 'HTML': case 'HTML_CUSTOM': $type = protect_from_escaping(escape_html('HTML')); break; case 'MODULES': case 'MODULES_CUSTOM': $type = do_lang_tempcode('MODULE'); break; case 'MINIMODULES': case 'MINIMODULES_CUSTOM': $type = do_lang_tempcode('MINIMODULE'); break; case 'REDIRECT': $type = do_lang_tempcode('REDIRECT_PAGE_TO', escape_html($page_info[1]['r_to_zone']), escape_html($page_info[1]['r_to_page'])); $redirecting_to = $page_info[1]['r_to_zone']; $current_for = $page_info[1]['r_to_page']; $page_info = _request_page($current_for, $redirecting_to, NULL, $lang); if ($page_info !== false) { switch ($page_info[0]) { case 'COMCODE_CUSTOM_PURE': $pure = true; case 'COMCODE': case 'COMCODE_CUSTOM': $is_comcode = true; break; } } break; default: $type = do_lang_tempcode('UNKNOWN'); break; } $class = ''; $w = false; $current_zone = is_null($redirecting_to) ? $id : $redirecting_to; $default_parsed = NULL; if ($is_comcode) { $fullpath = zone_black_magic_filterer(($page_info[0] == 'comcode' || $pure ? get_file_base() : get_custom_file_base()) . '/' . $current_zone . '/pages/' . strtolower($page_info[0]) . '/' . $lang . '/' . $current_for . '.txt'); if (!file_exists($fullpath)) { $fullpath = zone_black_magic_filterer(($page_info[0] == 'comcode' || $pure ? get_file_base() : get_custom_file_base()) . '/' . $current_zone . '/pages/' . strtolower($page_info[0]) . '/' . get_site_default_lang() . '/' . $current_for . '.txt'); } if (file_exists($fullpath)) { $comcode = file_get_contents($fullpath, FILE_TEXT); $default_parsed = comcode_to_tempcode($comcode, NULL, false, 60, NULL, NULL, true); } else { $comcode = ''; } $edit_url = build_url(array('page' => 'cms_comcode_pages', 'type' => '_ed', 'page_link' => $current_zone . ':' . $current_for), get_module_zone('cms_comcode_pages')); // WYSIWYG? require_javascript('javascript_editing'); $w = has_js() && (browser_matches('wysiwyg') && strpos($comcode, '{$,page hint: no_wysiwyg}') === false); global $JAVASCRIPT, $WYSIWYG_ATTACHED; if (!$WYSIWYG_ATTACHED) { $JAVASCRIPT->attach(do_template('HTML_EDIT')); } $WYSIWYG_ATTACHED = true; if ($w) { @header('Content-type: text/html; charset=' . get_charset()); $class .= ' wysiwyg'; } } else { $comcode = NULL; $edit_url = new ocp_tempcode(); } $field_name = 'edit_' . $for . '_textarea'; if ($i == 1) { $settings = $fields; $comcode_editor = get_comcode_editor($field_name); } else { $settings = NULL; $button = 'block'; $comcode_editor = new ocp_tempcode(); $comcode_editor->attach(do_template('COMCODE_EDITOR_BUTTON', array('_GUID' => '0acc5dcf299325d0cf55871923148a54', 'DIVIDER' => false, 'FIELD_NAME' => $field_name, 'TITLE' => do_lang_tempcode('INPUT_COMCODE_' . $button), 'B' => $button))); $button = 'comcode'; $comcode_editor->attach(do_template('COMCODE_EDITOR_BUTTON', array('_GUID' => '1acc5dcf299325d0cf55871923148a54', 'DIVIDER' => false, 'FIELD_NAME' => $field_name, 'TITLE' => do_lang_tempcode('INPUT_COMCODE_' . $button), 'B' => $button))); } global $TEMPCODE_SETGET; if ($for == $default_page) { $TEMPCODE_SETGET['in_panel'] = '0'; } else { $TEMPCODE_SETGET['in_panel'] = '1'; } $preview = substr($page_info[0], 0, 6) == 'MODULE' ? NULL : request_page($for, false, $id, NULL, true); if (!is_null($preview)) { $_preview = $preview->evaluate(); if (!$is_comcode || strpos($comcode, '<') !== false) { require_code('xhtml'); $_preview = xhtmlise_html($_preview, true); // Fix potential errors by passing it through our XHTML fixer functions } else { $new = $_preview; if (preg_replace('#\\s+#', '', $new) != preg_replace('#\\s+#', '', $_preview)) { $_preview = $new; $_preview .= do_lang('BROKEN_XHTML_FIXED'); } } } else { $_preview = NULL; } $is_panel = substr($for, 0, 6) == 'panel_'; require_code('zones3'); $zone_list = $for == $current_for ? nice_get_zones($redirecting_to, array($id)) : new ocp_tempcode(); $editor[$for] = static_evaluate_tempcode(do_template('ZONE_EDITOR_PANEL', array('_GUID' => 'f32ac84fe18b90497acd4afa27698bf0', 'DEFAULT_PARSED' => $default_parsed, 'CLASS' => $class, 'CURRENT_ZONE' => $current_zone, 'ZONES' => $zone_list, 'COMCODE' => $comcode, 'PREVIEW' => $_preview, 'ZONE' => $id, 'ID' => $for, 'IS_PANEL' => $is_panel, 'TYPE' => $type, 'EDIT_URL' => $edit_url, 'SETTINGS' => $settings, 'COMCODE_EDITOR' => $comcode_editor))); } breadcrumb_set_parents(array(array('_SELF:_SELF:editor', do_lang_tempcode('CHOOSE')))); breadcrumb_set_self($nice_zone_name); list($warning_details, $ping_url) = handle_conflict_resolution($id); $GLOBALS['HELPER_PANEL_PIC'] = ''; $GLOBALS['HELPER_PANEL_TUTORIAL'] = ''; $GLOBALS['HELPER_PANEL_TEXT'] = ''; return do_template('ZONE_EDITOR_SCREEN', array('_GUID' => '3cb1aab6b16444484e82d22f2c8f1e9a', 'ID' => $id, 'LANG' => $lang, 'PING_URL' => $ping_url, 'WARNING_DETAILS' => $warning_details, 'TITLE' => $title, 'URL' => $post_url, 'LEFT_EDITOR' => $editor['panel_left'], 'RIGHT_EDITOR' => $editor['panel_right'], 'MIDDLE_EDITOR' => $editor[$default_page])); }
/** * Convert Semi-HTML into comcode. Cleanup where possible * * @param LONG_TEXT The Semi-HTML to converted * @param boolean Whether to force full conversion regardless of settings * @return LONG_TEXT The equivalent comcode */ function semihtml_to_comcode($semihtml, $force = false) { require_code('obfuscate'); $semihtml = trim($semihtml); @ini_set('pcre.backtrack_limit', '10000000'); do { $semihtml_before = $semihtml; $semihtml = preg_replace_callback('#<input [^>]*class="ocp_keep_ui_controlled" [^>]*title="([^"]*)" [^>]*type="text" [^>]*value="[^"]*"[^>]*/?' . '>#siU', 'debuttonise', $semihtml_before); } while ($semihtml != $semihtml_before); $array_html_preg_replace = array(); $semihtml = str_replace('​', '', $semihtml); if (strtolower(get_charset()) == 'utf-8') { $semihtml = str_replace(chr(hexdec('e2')) . chr(hexdec('80')) . chr(hexdec('8b')), '', $semihtml); } $array_html_preg_replace[] = array('#^<kbd class="(ocp_keep|ocp_keep_block)"[^>]*>(.*)</kbd>$#siU', "\${2}"); $semihtml = array_html_preg_replace('kbd', $array_html_preg_replace, $semihtml); $semihtml = str_replace('<!-- >', '', $semihtml); $semihtml = preg_replace('#<span id="cke_bm_[^"]+" style="display: none;\\s*"> </span>#', '', $semihtml); require_code('xhtml'); $semihtml = xhtmlise_html($semihtml, true); // Needed so we can parse it right $semihtml = preg_replace('#(\\[[\\w\\_]+) #', '${1} ', $semihtml); $matches = array(); if (!$force && (get_option('eager_wysiwyg') == '0' && (substr_count($semihtml, '://') <= preg_match_all('#(href|src)="[^"]*://[^"]*"#', $semihtml, $matches) || count(find_all_hooks('systems', 'comcode_link_handlers')) == 0) && has_specific_permission(get_member(), 'allow_html')) || strpos($semihtml, '{$,page hint: no_smart_conversion}') !== false) { $semihtml = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*) />#siU', '_img_tag_fixup_raw', $semihtml); // Resolve relative URLs $semihtml = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*)>#siU', '_img_tag_fixup_raw', $semihtml); // Resolve relative URLs if (strpos($semihtml, 'data:') === false) { $count = substr_count($semihtml, '[/') + substr_count($semihtml, '{') + substr_count($semihtml, '[[') + substr_count($semihtml, '<h1'); if ($count == 0 && strpos($semihtml, '<h1') === false) { return $semihtml == '' ? '' : '[html]' . $semihtml . '[/html]'; } $count2 = substr_count($semihtml, '[/attachment]') + substr_count($semihtml, '<h1'); if ($count2 == $count) { if ($semihtml != '') { $semihtml = '[html]' . $semihtml . '[/html]'; } $semihtml = preg_replace('#<h1[^>]*>\\s*<span class="inner">(.*)</span>\\s*</h1>#Us', '[/html][semihtml][title]${1}[/title][/semihtml][html]', $semihtml); $semihtml = preg_replace('#<h1[^>]*>(.*)</h1>#Us', '[/html][semihtml][title]${1}[/title][/semihtml][html]', $semihtml); $semihtml = str_replace('[attachment', '[/html][semihtml][attachment', str_replace('[/attachment]', '[/attachment][/semihtml][html]', $semihtml)); $semihtml = str_replace('[/html][html]', '', $semihtml); $semihtml = str_replace('[html][/html]', '', $semihtml); return $semihtml; } } if ($semihtml != '') { $semihtml = '[semihtml]' . $semihtml . '[/semihtml]'; } $semihtml = preg_replace('#<h1[^>]*>\\s*<span class="inner">(.*)</span>\\s*</h1>#Us', '[title]${1}[/title]', $semihtml); $semihtml = preg_replace('#<h1[^>]*>(.*)</h1>#Us', '[title]${1}[/title]', $semihtml); return $semihtml; } // Safety from if these are typed in (could cause problems) $semihtml = str_replace('[html', '[ html', $semihtml); $semihtml = str_replace('[semihtml', '[ semihtml', $semihtml); $semihtml = str_replace('[/html', '[ / html', $semihtml); $semihtml = str_replace('[/semihtml', '[ / semihtml', $semihtml); // Try and retain image attachments $semihtml = preg_replace('#<script[^>]*>(//\\s*<!\\[CDATA\\[)?(<!--)?\\s*var te_[\\d\\w\\-]*="[^"]*&for\\_session=[^"]*";\\s*(//\\]\\]>)?(-->)?</script>#', '', $semihtml); foreach (array('&thumb=0[^"]*' => ' thumb="0"', '&thumb=1[^"]*' => ' thumb="1"', '' => '') as $thumb_bit => $comcode_to) { $semihtml = preg_replace('#<div [^>]*class="attachment_left"[^>]*>\\s*<img src="[^"]*?id=(\\d+)[^"]*' . $thumb_bit . '(&for_session=[\\w\\d]+)?" /></div>#i', '[attachment_safe' . $comcode_to . ' type="inline_left"]${1}[/attachment_safe]', $semihtml); $semihtml = preg_replace('#<div [^>]*class="attachment_right"[^>]*>\\s*<img src="[^"]*?id=(\\d+)[^"]*' . $thumb_bit . '(&for_session=[\\w\\d]+)?" /></div>#i', '[attachment_safe' . $comcode_to . ' type="inline_right"]${1}[/attachment_safe]', $semihtml); $regexp = '#'; $regexp .= $comcode_to == ' thumb="1"' ? '(<a [^>]*>)?\\s*' : '()'; $regexp .= '<img [^>]*[^>]*src="[^"]*?id=(\\d+)[^"]*' . $thumb_bit . '(&for_session=[\\w\\d]+)?" [^>]*title="([^"]*)" [^>]*/?' . '>\\s*'; $regexp .= $comcode_to == ' thumb="1"' ? '(</a>)?' : '()'; $regexp .= '#i'; $semihtml = preg_replace($regexp, '[attachment_safe' . $comcode_to . ' type="inline" description="${4}"]${2}[/attachment_safe]', $semihtml); } $semihtml = preg_replace('#&keep_session=[\\d]*(&for_session=[\\w\\d]*)?#', '', $semihtml); // This is useful for generally stripping sensitive information anyway. Should be null-op if anti-leech was on, but worth doing just-in-case. // We must protect anything that is in CDATA from whitespace/entity cleanup (HTML or XHTML - we have to use lowest common denominator) $semihtml = preg_replace_callback('#(<script[^>]*>)(.*)(</script>)#siU', '_cdata_protect', $semihtml); $semihtml = preg_replace_callback('#(<style[^>]*>)(.*)(</style>)#siU', '_cdata_protect', $semihtml); $semihtml = preg_replace_callback('#(<textarea[^>]*>)(.*)(</textarea>)#siU', '_cdata_protect', $semihtml); $semihtml = preg_replace_callback('#(<pre[^>]*>)(.*)(</pre>)#siU', '_cdata_protect', $semihtml); $semihtml = preg_replace_callback('#(<![CDATA[)(.*)(]]>)#siU', '_cdata_protect', $semihtml); // And use same method to protect our code tags /* foreach (array_keys($GLOBALS['CODE_TAGS']) as $code_tag) $semihtml=preg_replace_callback('#(\['.$code_tag.'[^\]]*\])(.*)(\[/'.$code_tag.'\])#siU','_codetag_protect',$semihtml); Actually no, we don't want this. These tags are typed potentially to show HTML and thus the entities must get decoded */ // Cleanup from certain word processors // LibreOffice $semihtml = str_replace('<h2 class="western">', '<h2>', $semihtml); $semihtml = preg_replace('#</(ul|ol|h1|h2|h3|h4|h5|h6)>\\s*<p style="margin-bottom: 0(cm|em|px)?">\\s* \\s*</p>\\s*#Us', '</${1}>', $semihtml); $semihtml = preg_replace('#<p style="margin-bottom: 0(cm|em|px)?">\\s* \\s*</p>\\s*#Us', '', $semihtml); $semihtml = preg_replace('#<li>\\s*<p style="margin-bottom: 0(cm|em|px)?">(.*)</p>\\s*</li>#Us', '<li>${2}</li>', $semihtml); $semihtml = preg_replace('#<p style="margin-bottom: 0(cm|em|px)?">(.*)</p>\\s*<(ul|ol|h1|h2|h3|h4|h5|h6)>#Us', '${2}<${3}>', $semihtml); $semihtml = str_replace('<p style="margin-bottom: 0cm">', '<p>', $semihtml); $semihtml = preg_replace('#(<style[^>]*>)(.*)(</style>)#siU', '', $semihtml); // We shouldn't allow this nested anyway (invalid XHTML), and word abuses it $semihtml = preg_replace('#<span class="Apple-style-span"[^>]*>(.*)</span>#siU', '${1}', $semihtml); // webkit $semihtml = preg_replace('#<meta[^>]*>#siU', '', $semihtml); // We shouldn't allow this nested anyway (invalid XHTML), and word abuses it $semihtml = preg_replace('#(<[^>]*) lang="[^"]*"#i', '${1}', $semihtml); $semihtml = preg_replace('#(<[^>]*) style="margin-right: 0\\w*;?"#i', '${1}', $semihtml); $semihtml = preg_replace('#(<[^>]*) dir="' . do_lang('dir') . '"#i', '${1}', $semihtml); $semihtml = preg_replace_callback('#<[^>"]* style="([^">]*"[^">]*)*"#i', '_css_quot_fixup', $semihtml); $semihtml = preg_replace('#<a name="OLE_LINK1">([^<]*)</a>#siU', '${1}', $semihtml); $semihtml = preg_replace('#(<[^>]* align="right"[^>]*) style="(margin-right: [\\d\\.]+pt;\\s*)?text-align: right;?"#is', '${1}', $semihtml); // trim off redundancy $semihtml = preg_replace('#(<[^>]* align="center"[^>]*) style="(margin-right: [\\d\\.]+pt;\\s*)?text-align: center;?"#is', '${1}', $semihtml); // trim off redundancy $semihtml = preg_replace('#(?U)(<[^>]* style="[^"]*)(?-U);?\\s*page-break-after: avoid;?"#is', '${1}"', $semihtml); $semihtml = str_replace('<place>', '', $semihtml); $semihtml = str_replace('</place>', '', $semihtml); $semihtml = preg_replace('#<link [^>]*href="file://[^"]*"[^>]*/>#sU', '', $semihtml); $semihtml = preg_replace('#<!--\\[if(.*)-->#sU', '', $semihtml); $semihtml = preg_replace('#<!--(.*)\\[endif(.*)-->#sU', '', $semihtml); $semihtml = str_replace('<!-- >', '', $semihtml); $semihtml = preg_replace('#</?[ovw]:[^>]*>#s', '', $semihtml); $semihtml = preg_replace('#(<[^>]*) [ovw]:[^>"]*"[^"]*"([^>]*>)#s', '${1}${2}', $semihtml); $semihtml = preg_replace('#</?st1:[^>]*>#', '', $semihtml); // Word smart tags $semihtml = str_replace('<br class="Apple-interchange-newline" />', '<br />', $semihtml); $semihtml = str_replace(chr(10), ' ', $semihtml); // Clean some whitespace (they have a special Comcode meaning, but no special HTML meaning) $inline_elements = array('font', 's', 'u', 'strike', 'span', 'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'strong', 'kbd', 'q', 'samp', 'var', 'sub', 'sup', 'tt', 'del', 'ruby', 'a', 'bdo', 'img', 'ins', 'param', 'textarea', 'button', 'input', 'select', 'object', 'caption', 'label', 'b', 'i', 'small', 'big'); $semihtml = preg_replace('#(<(' . implode('|', $inline_elements) . ')( [^>]*)?' . '>)\\s+#', '${1}</CDATA__space>', $semihtml); $semihtml = preg_replace('#\\s+(</(' . implode('|', $inline_elements) . ')>)#', '</CDATA__space>${1}', $semihtml); $semihtml = preg_replace('#([^\\>\\s])\\s+(<(' . implode('|', $inline_elements) . ')( [^>]*)?' . '>)#', '${1}</CDATA__space>${2}', $semihtml); $semihtml = preg_replace('#(</(' . implode('|', $inline_elements) . ')>)\\s+#', '${1}</CDATA__space>', $semihtml); $semihtml = preg_replace('#>\\s+#', '>', $semihtml); $semihtml = preg_replace('#\\s+<#', '<', $semihtml); $semihtml = preg_replace('#\\s+#', ' ', $semihtml); // Cleanup impossible stuff in code tags global $CODE_TAGS; foreach (array_keys($CODE_TAGS) as $tag) { $semihtml = comcode_preg_replace($tag, '#^(\\[' . $tag . '\\])(.*)(\\[/' . $tag . '\\])$#si', array('comcode_strip_html_tags'), $semihtml); } // Cleanup how blocks are converted into a line break model. We need to clean up the case where inline leads onto block, by adding a linebreak inbetween. Note that this kind of break does not go *between* blocks, which is the reason we can't arbitrarily place it later on. $semihtml = preg_replace('#([^\\s<>]|</(' . implode('|', $inline_elements) . ')>)(<(div|p))#', '${1}<br />${3}', $semihtml); // Reorder XHTML attributes alphabetically, so our regexp's match better $semihtml = preg_replace_callback('#<([^>\\s]+)\\s([^>]+)>#', '_reorder_xhtml_attributes', $semihtml); // Reorder style properties alphabetically, so our regexp's match better $semihtml = preg_replace_callback('#(<[^>]*style=")([^"]*)("[^>]*>)#', '_reorder_css_properties', $semihtml); $semihtml = str_replace('<p />', '<br /><br />', str_replace('<p />', '<br /><br />', $semihtml)); // Remove proprietary stylings put in by RTF->HTML conversions performed by certain browsers $old_semihtml = ''; do { $old_semihtml = $semihtml; $semihtml = preg_replace('#(<[^>]* style="(?U)[^">]*(?-U))-\\w+-[^";>]*(;\\s*)?#s', '${1}', $semihtml); } while ($semihtml != $old_semihtml); // Perform lots of conversions. We can't convert everything. Sometimes we reverse-convert what Comcode forward-converts; sometimes we match generic HTML; sometimes we match Microsoft Word or Open Office; sometimes we do lossy match $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<h1 id="screen_title">\\s*<span class="inner">(.*)</span>\\s*</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 class="screen_title">\\s*<span class="inner">(.*)</span>\\s*</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title" class="screen_title">\\s*<span class="inner">(.*)</span>\\s*</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title">(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 class="screen_title">(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title" class="screen_title">(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1>(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $semihtml = array_html_preg_replace('h1', $array_html_preg_replace, $semihtml); $semihtml = preg_replace('#^\\s*<h1[^>]+>(.*)</h1>#siU', '[title="1"]${1}[/title]', $semihtml); for ($i = 2; $i <= 4; $i++) { $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<h' . strval($i) . '><span class="inner">(.*)</span></h' . strval($i) . '>$#siU', '[title="' . strval($i) . '"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h' . strval($i) . '>(.*)</h' . strval($i) . '>$#siU', '[title="' . strval($i) . '"]${1}[/title]'); $semihtml = array_html_preg_replace('h' . strval($i) . '', $array_html_preg_replace, $semihtml); } $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<h1 id="screen_title"[^<>]*><span class="inner">(.*)</span></h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 class="screen_title"[^<>]*><span class="inner">(.*)</span></h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title" class="screen_title"><span class="inner">(.*)</span></h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title"[^<>]*>(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 class="screen_title"[^<>]*>(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h1 id="screen_title" class="screen_title"[^<>]*>(.*)</h1>$#siU', '[title="1"]${1}[/title]'); $semihtml = array_html_preg_replace('h1', $array_html_preg_replace, $semihtml); $semihtml = preg_replace('#^\\s*<h1[^>]+>(.*)</h1>#siU', '[title="1"]${1}[/title]', $semihtml); for ($i = 2; $i <= 4; $i++) { $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<h' . strval($i) . '><span class="inner">(.*)</span></h' . strval($i) . '>$#siU', '[title="' . strval($i) . '"]${1}[/title]'); $array_html_preg_replace[] = array('#^<h' . strval($i) . '>(.*)</h' . strval($i) . '>$#siU', '[title="' . strval($i) . '"]${1}[/title]'); $semihtml = array_html_preg_replace('h' . strval($i), $array_html_preg_replace, $semihtml); } $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<span>(.*)</span>$#siU', '${1}'); $array_html_preg_replace[] = array('#^<span( charset="[^"]*")?( content="[^"]*")?( name="[^"]*")?' . '>(.*)</span>$#siU', '${4}'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-family:\\s*monospace;\\s*?font-size:\\s*[\\d\\.]*em;?">(.*)</span>$#siU', '[tt]${1}[/tt]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-weight:\\s*bold;?">(.*)</span>$#siU', '[b]${1}[/b]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-style:\\s*italic;?">(.*)</span>$#siU', '[i]${1}[/i]'); $array_html_preg_replace[] = array('#^<span style="\\s*?text-decoration:\\s*underline;?">(.*)</span>$#siU', '[u]${1}[/u]'); $array_html_preg_replace[] = array('#^<span( href="[^"]*")?( rel="[^"]*")?' . '>(.*)</span>$#siU', '${3}'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-weight:\\s*bold;?">(.*)</span>$#siU', '[b]${1}[/b]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-style:\\s*italic;?">(.*)</span>$#siU', '[i]${1}[/i]'); $array_html_preg_replace[] = array('#^<span style="\\s*?text-decoration:\\s*underline;?">(.*)</span>$#siU', '[u]${1}[/u]'); $array_html_preg_replace[] = array('#^<span style="\\s*?color:\\s*?([^";]+);\\s*?font-family:\\s*?([^";]+);\\s*?font-size:\\s*?([^";]+);?"([^>]*)>(.*)</span>$#siU', '[font param="${2}" color="${1}" size="${3}"]${5}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?color:\\s*?([^";]+);\\s*?font-size:\\s*?([^";]+);?"([^>]*)>(.*)</span>$#siU', '[font color="${1}" size="${2}"]${4}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?color:\\s*?([^";]+);\\s*?font-family:\\s*?([^";]+);?"([^>]*)>(.*)</span>$#siU', '[font param="${2}" color="${1}"]${4}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?color:\\s*?([^";]+);?\\s*?"([^>]*)>(.*)</span>$#siU', '[font color="${1}"]${3}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-family:\\s*?([^";]+);\\s*?font-size:\\s*?([^";]+);?"([^>]*)>(.*)</span>$#siU', '[font param="${1}" size="${2}"]${4}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-size:\\s*?([0-9\\.]+\\w\\w);?\\s*?"([^>]*)>(.*)</span>$#siU', '[font size="${1}"]${3}[/font]'); $array_html_preg_replace[] = array('#^<span style="\\s*?font-family:\\s*?([^";]+);?\\s*?"([^>]*)>(.*)</span>$#siU', '[font param="${1}"]${3}[/font]'); $complex_equivs = array('<span class="comcode_highlight">(.*)</span>' => 'highlight', '<span class="comcode_bold">(.*)</span>' => 'b', '<span class="comcode_italic">(.*)</span>' => 'i', '<span class="comcode_underline">(.*)</span>' => 'u'); foreach ($complex_equivs as $from => $to) { $array_html_preg_replace[] = array('#^' . $from . '$#siU', '[' . $to . ']${1}[/' . $to . ']'); } $semihtml = array_html_preg_replace('span', $array_html_preg_replace, $semihtml); $_array_html_preg_replace = array(); foreach ($array_html_preg_replace as $i => $x) { $_array_html_preg_replace[$i] = array(); $_array_html_preg_replace[$i][0] = str_replace('span', 'div', $x[0]); $_array_html_preg_replace[$i][1] = '<div>' . $x[1] . '</div>'; } $semihtml = array_html_preg_replace('div', $_array_html_preg_replace, $semihtml); $_array_html_preg_replace = array(); foreach ($array_html_preg_replace as $i => $x) { $_array_html_preg_replace[$i] = array(); $_array_html_preg_replace[$i][0] = str_replace('div', 'p', $x[0]); $_array_html_preg_replace[$i][1] = '<p>' . $x[1] . '</p>'; } $semihtml = array_html_preg_replace('p', $_array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<font>(.*)</font>$#siU', '${1}'); $array_html_preg_replace[] = array('#^<font ([^>]*)size="(\\d+)"([^>]*)>(.*)</font>$#siU', '[font ${1}${3} size="${2}of"]${4}[/font]'); $array_html_preg_replace[] = array('#^<font([^>]*)>(.*)</font>$#siU', '[font${1}]${2}[/font]'); $semihtml = array_html_preg_replace('font', $array_html_preg_replace, $semihtml); $semihtml = preg_replace_callback('#(\\[font [^\\]]*color=")rgb\\((\\s*\\d+\\s*),(\\s*\\d+\\s*),(\\s*\\d+\\s*)\\)("[^\\]]*\\])#', '_css_color_fixup', $semihtml); $semihtml = preg_replace_callback('#<a ([^>]*)href="([^"]*)"([^>]*)>#', '_a_tag_link_fixup', $semihtml); $array_html_preg_replace = array(); require_code('obfuscate'); $array_html_preg_replace[] = array('#^<a ([^>]*)href="mailto:(?-U) ?(?U)([^"]+)"([^>]*)>(.*)</a>$#siU', '[email="${2}"]${4}[/email]'); $array_html_preg_replace[] = array('#^<a ([^>]*)href="' . str_replace('#', '\\#', str_replace('&', '\\&', mailto_obfuscated())) . '([^"]+)"([^>]*)>(.*)</a>$#siU', '[email="${4}"]${2}[/email]'); $array_html_preg_replace[] = array('#^<a ([^>]*)href="([^"]+)"([^>]*) target="([^"]*)"([^>]*)>(.*)</a>$#siU', '[url="${2}" target="${4}"]${6}[/url]'); $array_html_preg_replace[] = array('#^<a ([^>]*)href="([^"]+)"([^>]*)>(.*)</a>$#siU', '[url="${2}"]${4}[/url]'); $semihtml = array_html_preg_replace('a', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<p class="msoNormal">\\s*(.*)\\s*</p>$#siU', '${1}<br />'); $array_html_preg_replace[] = array('#^<p align="(\\w+)" class="msoNormal">\\s*(.*)\\s*</p>$#siU', '[align="${1}"]${2}[/align]'); $array_html_preg_replace[] = array('#^<p class="msoNormal" style="margin: \\d+pt 0[\\w;]*">\\s*(.*)\\s*</p>$#siU', '<br />${1}<br />'); // Cleanup from Word $array_html_preg_replace[] = array('#^<p class="msoNormal" style="margin: 0[\\w;]* 0[\\w;]* 0[\\w;]*">\\s*(.*)\\s*</p>$#siU', '${1}<br />'); // Cleanup from Word $array_html_preg_replace[] = array('#^<p style="margin: \\d+pt 0[\\w;]*">\\s*(.*)\\s*</p>$#siU', '<br />${1}<br />'); // Cleanup from Word $array_html_preg_replace[] = array('#^<p style="margin: 0[\\w;]* 0[\\w;]* 0[\\w;]*">\\s*(.*)\\s*</p>$#siU', '${1}<br />'); // Cleanup from Word $array_html_preg_replace[] = array('#^<p class="Mso\\w*" style="[^"]*">\\s*(.*)\\s*</p>$#siU', '<br />${1}<br />'); // Aggressive cleanup from Word (it's here last because we want the nicer matches to get a chance to work instead. It's a shame we need to do this, as we are throwing away potentially important styling (although actually the spans etc far above will have got most of this - we only match p level styling here)- but Word throws so much into a mix it's impossible to "remove the wheat from the chaff". People will need to put it back in using the WYSIWYG editor directly. $array_html_preg_replace[] = array('#^<p>\\s*(.*)\\s*</p>$#siU', '${1}<br /><br />'); $array_html_preg_replace[] = array('#^<p align="(\\w+)">\\s*(.*)\\s*</p>$#siU', '[align="${1}"]${2}[/align]'); $semihtml = array_html_preg_replace('p', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<div align="justify">(.*)</div>$#siU', '[align="justify"]${1}[/align]'); $array_html_preg_replace[] = array('#^<div style="text-align:\\s*?justify;?">(.*)</div>$#siU', '[align="justify"]${1}[/align]'); $complex_equivs = array('<div align="right">(.*)</div>' => 'right', '<div align="left">(.*)</div>' => 'left', '<div align="center">(.*)</div>' => 'center', '<div style="text-align:\\s*?right;?">(.*)</div>' => 'right', '<div style="text-align:\\s*?left;?">(.*)</div>' => 'left', '<div style="text-align:\\s*?center;*">(.*)</div>' => 'center'); foreach ($complex_equivs as $from => $to) { $array_html_preg_replace[] = array('#^' . $from . '$#siU', '[' . $to . ']${1}[/' . $to . ']'); } $array_html_preg_replace[] = array('#^<div style="margin-left:\\s*?(\\d+)px;?">(.*)</div>$#siU', '[indent="${1}"]${2}[/indent]'); $array_html_preg_replace[] = array('#^<div class="([^"]+)">(.*)</div>$#siU', '[surround="${1}"]${2}[/surround]'); $array_html_preg_replace[] = array('#^<div>(.*)</div>$#siU', '${1}<br />'); $semihtml = array_html_preg_replace('div', $array_html_preg_replace, $semihtml); $_array_html_preg_replace = array(); foreach ($array_html_preg_replace as $i => $x) { $_array_html_preg_replace[$i] = array(); $_array_html_preg_replace[$i][0] = str_replace('div', 'span', $x[0]); $_array_html_preg_replace[$i][1] = $x[1]; } $semihtml = array_html_preg_replace('span', $_array_html_preg_replace, $semihtml); $_array_html_preg_replace = array(); foreach ($array_html_preg_replace as $i => $x) { $_array_html_preg_replace[$i] = array(); $_array_html_preg_replace[$i][0] = str_replace('div', 'p', $x[0]); $_array_html_preg_replace[$i][1] = str_replace('<br />', '<br /><br />', $x[1]); } $semihtml = array_html_preg_replace('p', $_array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<kbd>(.*)</kbd>$#siU', "[tt]\${1}[/tt]"); $semihtml = array_html_preg_replace('kbd', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<ul dir="ltr">(.*)</ul>$#siU', '[list]' . '${1}[/list]'); $array_html_preg_replace[] = array('#^<ul>(.*)</ul>$#siU', '[list]' . '${1}[/list]'); $semihtml = array_html_preg_replace('ul', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<ol>(.*)</ol>$#siU', '[list="1"]' . '${1}[/list]'); $array_html_preg_replace[] = array('#^<ol style="list-style-type:\\s*?([^";]*);?">(.*)(</ol>|<ol />)$#siU', '[list="${1}"]' . '${2}[/list]'); $array_html_preg_replace[] = array('#^<ol type="([^"]*)">(.*)</ol>$#siU', '[list="${1}"]' . '${2}[/list]'); $semihtml = array_html_preg_replace('ol', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); if (strpos($semihtml, '[list') !== false) { $array_html_preg_replace[] = array('#^<li>(.*)</li>$#siU', '[*]${1}[/*]' . chr(10)); $semihtml = array_html_preg_replace('li', $array_html_preg_replace, $semihtml); } $semihtml = str_replace('<strong class="comcode_bold">', '<strong>', $semihtml); $semihtml = str_replace('<em class="comcode_italic">', '<em>', $semihtml); $equivs = array('blockquote' => 'indent', 'code' => 'code', 'tt' => 'tt', 'sub' => 'sub', 'sup' => 'sup', 'center' => 'center', '!abbr' => 'abbr', '!acronym' => 'acronym', 'address' => 'address', 'dfn' => 'dfn', 'cite' => 'cite', 'strong' => 'b', 'b' => 'b', 'em' => 'i', 'i' => 'i', 'u' => 'u', 'strike' => 's', 'del' => 'del', 'ins' => 'ins'); foreach ($equivs as $from => $to) { $array_html_preg_replace = array(); if ($from[0] == '!') { $from = substr($from, 1); $array_html_preg_replace[] = array('#^<' . $from . '([^>]*)>(.*)</' . $from . '>$#siU', '[' . $to . '${1}]${2}[/' . $to . ']'); } else { $array_html_preg_replace[] = array('#^<' . $from . '>(.*)</' . $from . '>$#siU', '[' . $to . ']${1}[/' . $to . ']'); } $semihtml = array_html_preg_replace($from, $array_html_preg_replace, $semihtml); } // Fonts that set nothing $test = preg_replace('#\\[font param="verdana,arial,helvetica,sans-serif"#', '', $semihtml); $test = preg_replace('#\\[font="verdana,arial,helvetica,sans-serif"#', '', $test); if (strpos($test, '[font=') === false && strpos($test, '[font param=') === false) { $semihtml = comcode_preg_replace('font', '#^\\[font( param)?="verdana,arial,helvetica,sans-serif"](.*)\\[/font\\]$#si', '${2}', $semihtml); $semihtml = str_replace(' param="verdana,arial,helvetica,sans-serif"', '', $semihtml); $semihtml = str_replace('="verdana,arial,helvetica,sans-serif"', '', $semihtml); } // Our cleanup loop. These optimisations trickle-through, as they depend on each other. We keep looping until we've done all we can. $old_semihtml = ''; $text_formatting_tags = array('b', 'i', 'u', 'tt', 'font', 'title', 'center', 'left', 'right', 'color'); do { $old_semihtml = $semihtml; // Empty tags $semihtml = preg_replace('#\\<(\\w+)\\>\\</\\1\\>#', '', $semihtml); $semihtml = preg_replace('#\\[font[^\\]]*\\]\\[/font\\]#', '', $semihtml); $semihtml = preg_replace('#\\[b[^\\]]*\\]\\[/b\\]#', '', $semihtml); $semihtml = preg_replace('#\\[i[^\\]]*\\]\\[/i\\]#', '', $semihtml); // Canonical order to make sure we can find pointless nestings. Unfortunately we can only bubble out one level due to constraints in our regexp checking (we need to make sure we don't cross-tags, but we can't in a regexp unless we make sure we have no nesting at all) foreach ($text_formatting_tags as $i => $tag) { foreach ($text_formatting_tags as $j => $tag_2) { if ($i < $j) { $semihtml = comcode_preg_replace($tag_2, '#^(\\[' . $tag_2 . '( [^\\]]*)?\\])(\\[' . $tag . '( [^\\]]*\\])?)([^\\[\\]]*)(\\[/' . $tag . '[^\\]]*\\])(\\[/' . $tag_2 . '[^\\]]*\\])$#si', '${3}${1}${5}${7}${6}', $semihtml); } } } // Cleanup nested fonts $semihtml = preg_replace('#<span[^<>]*></span>#siU', '', $semihtml); $semihtml = comcode_preg_replace('font', '#^\\[font([^\\]]*)\\](\\s*)\\[font([^\\]]*)\\](.*)\\[/font\\](\\s*)\\[/font\\]$#si', '[font${1}${3}]${2}${4}${5}[/font]', $semihtml); $semihtml = preg_replace('#\\[font ([^\\]]*)face="([^"]*)"([^\\]]*)face="([^"]*)"([^\\]]*)\\]#si', '[font ${1}${3}${5} face="${4}"]', $semihtml); $semihtml = preg_replace('#\\[font ([^\\]]*)size="([^"]*)"([^\\]]*)size="([^"]*)"([^\\]]*)\\]#si', '[font ${1}${3}${5} size="${4}"]', $semihtml); // This is imperfect (due to relative font sizes), but at least it encourages cleanup $semihtml = preg_replace('#\\[font ([^\\]]*)color="([^"]*)"([^\\]]*)color="([^"]*)"([^\\]]*)\\]#si', '[font ${1}${3}${5} color="${4}"]', $semihtml); $semihtml = preg_replace('#\\[font ([^\\]]*)param="([^"]*)"([^\\]]*)param="([^"]*)"([^\\]]*)\\]#si', '[font ${1}${3}${5} param="${4}"]', $semihtml); $semihtml = preg_replace('#(\\[font.*)(?-U)\\s+(?U)(.*\\])#U', '${1} ${2}', $semihtml); // safe because no whitespace runs can be expected within a font tag // Cleanup other nestings / close then reopen patterns foreach (array('b', 'i', 'u', 'tt', 'font size="[^"]*"') as $tag) { $tagx = strpos($tag, ' ') !== false ? substr($tag, 0, strpos($tag, ' ')) : $tag; $semihtml = comcode_preg_replace($tagx, '#^(\\[' . $tag . '\\])(.*)\\1(.*)\\[/' . $tagx . '\\](.*)\\[/' . $tagx . '\\]$#si', '${1}${2}${3}${4}[/' . $tagx . ']', $semihtml); $semihtml = preg_replace('#(\\[' . $tag . '\\])([^\\[\\]]*)\\[/' . $tagx . '\\](( |</CDATA\\_\\_space>|\\s)*)\\1#si', '${1}${2}${3}', $semihtml); // Only works in simple case, not when there are tags nested within first tag. Can't use comcode_preg_replace as we are joining two tags (i.e. not operating over single bind) } // Cleanup lines filled with spaces/font-junk foreach ($text_formatting_tags as $tag) { $semihtml = preg_replace('#(\\[' . $tag . '[^\\]]*\\])(( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*)#i', '${2}${1}', $semihtml); // Tag starting unnecessarily early -> Move it back $semihtml = preg_replace('#(( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*)(\\[/' . $tag . '\\])#i', '${3}${1}', $semihtml); // Tag ending unnecessarily late -> Move it back $semihtml = preg_replace('#\\[' . $tag . '[^\\]]*\\](( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*)\\[/' . $tag . '\\]#i', '${1}', $semihtml); // Tag wrapping whitespace -> White space } $semihtml = preg_replace('#( |</CDATA\\_\\_space>|\\s)*<br\\s*/>#i', '<br />', $semihtml); // Spaces on end of line -> (Remove) } while ($semihtml != $old_semihtml); // Undone center tagging $semihtml = comcode_preg_replace('left', '#^\\[left\\]\\[center\\](.*)\\[/center\\]\\[/left\\]$#si', '[left]${1}[/left]', $semihtml); $semihtml = comcode_preg_replace('right', '#^\\[right\\]\\[center\\](.*)\\[/center\\]\\[/right\\]$#si', '[right]${1}[/right]', $semihtml); $semihtml = comcode_preg_replace('center', '#^\\[center\\]\\[left\\](.*)\\[/left\\]\\[/center\\]$#si', '[center]${1}[/center]', $semihtml); $semihtml = comcode_preg_replace('center', '#^\\[center\\]\\[right\\](.*)\\[/right\\]\\[/center\\]$#si', '[center]${1}[/center]', $semihtml); // Cleanup list Comcode (nice and pretty) $semihtml = preg_replace('#( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*\\[/\\*\\]( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*#', '[/*]', $semihtml); $semihtml = preg_replace('#( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*\\[\\*\\]( |</CDATA\\_\\_space>|\\s|<br\\s*/>|\\n)*#', '[*]', $semihtml); $semihtml = preg_replace('#\\[/\\*\\]([^\\s])#', '[/*]<ocpbr />${1}', $semihtml); $semihtml = preg_replace('#\\[list\\]([^\\s])#', '[list]<ocpbr />${1}', $semihtml); // Cleanup various blocks where we can afford a blank line global $BLOCK_TAGS; foreach (array_keys($BLOCK_TAGS) as $tag) { if (strpos($semihtml, '[' . $tag) !== false) { $semihtml = preg_replace('#( |</CDATA\\_\\_space>)*(\\[' . $tag . '[\\] ])#', '${2}', $semihtml); $semihtml = preg_replace('#\\[/' . $tag . '\\](?!\\[/)(?!<br)#', '[/' . $tag . ']' . ($tag == 'title' ? '<ocpbr /><ocpbr />' : '<ocpbr />'), $semihtml); } } // Remove our CDATA protections $semihtml = str_replace('</CDATA__space>', ' ', $semihtml); $semihtml = str_replace('</CDATA__tab>', "\t", $semihtml); $semihtml = str_replace('</CDATA__nl>', chr(10), $semihtml); $semihtml = str_replace('</CDATA__lf>', chr(13), $semihtml); $semihtml = str_replace('</CDATA__amp>', '&', $semihtml); /*// And use same method to protect our code tags foreach (array_keys($GLOBALS['CODE_TAGS']) as $code_tag) $semihtml=preg_replace_callback('#(\['.$code_tag.'[^\]]*\])(.*)(\[/'.$code_tag.'\])#siU','_codetag_unprotect',$semihtml);*/ // Tempcode escaping /*$semihtml=str_replace('{+','\{+',$semihtml); No - people should be able to type this if they want $semihtml=str_replace('{$','\{$',$semihtml); $semihtml=str_replace('{!','\{!',$semihtml);*/ $semihtml = str_replace('[ html', '[html', $semihtml); $semihtml = str_replace('[ semihtml', '[semihtml', $semihtml); $semihtml = str_replace('[ / html', '[/html', $semihtml); $semihtml = str_replace('[ / semihtml', '[/semihtml', $semihtml); // People without comcode_dangerous have further cleanups, that might lose some quality... if (!has_specific_permission(get_member(), 'allow_html')) { $semihtml2 = $semihtml; $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<table summary="([^"]*)"([^>]*)>(.*)</table>$#siU', chr(10) . "{| \${2}\${3}'.chr(10).'|}" . chr(10)); $array_html_preg_replace[] = array('#^<table([^>]*)>(.*)</table>$#siU', chr(10) . "{|" . chr(10) . "\${2}" . chr(10) . "|}" . chr(10)); $semihtml2 = array_html_preg_replace('table', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<thead([^>]*)>(.*)</thead>$#siU', ''); $semihtml2 = array_html_preg_replace('thead', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<tbody([^>]*)>(.*)</tbody>$#siU', '${2}'); $semihtml2 = array_html_preg_replace('tbody', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<tfoot([^>]*)>(.*)</tfoot>$#siU', ''); $semihtml2 = array_html_preg_replace('tfoot', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<tr([^>]*)>(.*)</tr>$#siU', chr(10) . "|-" . chr(10) . "\${2}"); $semihtml2 = array_html_preg_replace('tr', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<th([^>]*)>(.*)</th>$#siU', chr(10) . chr(10) . "! \${2}"); $semihtml2 = array_html_preg_replace('th', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<td([^>]*)>(.*)</td>$#siU', chr(10) . "| \${2}"); $semihtml2 = array_html_preg_replace('td', $array_html_preg_replace, $semihtml2); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<span style="font-family: monospace; font-size: 1.2em;">(.*)</span>$#siU', "[tt]\${1}[/tt]"); $semihtml2 = array_html_preg_replace('span', $array_html_preg_replace, $semihtml2); if (strpos($semihtml, '[code') === false) { $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<pre>(.*)</pre>$#siU', "[code]\${1}[/code]"); $semihtml2 = array_html_preg_replace('pre', $array_html_preg_replace, $semihtml2); } $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<table([^>]*)>(.*)</table>$#siU', "<table class=\"bordered_table\">\${2}</table>"); $semihtml = array_html_preg_replace('table', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<tr([^>]*)>(.*)</tr>$#siU', "<tr>\${2}</tr>"); $semihtml = array_html_preg_replace('tr', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<th([^>]*)>(.*)</th>$#siU', "<th>\${2}</th>"); $semihtml = array_html_preg_replace('th', $array_html_preg_replace, $semihtml); $array_html_preg_replace = array(); $array_html_preg_replace[] = array('#^<td([^>]*)>(.*)</td>$#siU', "<td>\${2}</td>"); $semihtml = array_html_preg_replace('td', $array_html_preg_replace, $semihtml); } else { $semihtml2 = $semihtml; } // Cleanup impossible stuff in code tags foreach (array_keys($CODE_TAGS) as $tag) { $semihtml2 = comcode_preg_replace($tag, '#^(\\[' . $tag . '\\])([.\\n]*)(\\[/' . $tag . '\\])$#i', array('semihtml_to_comcode_wrap'), $semihtml2); } // These can only be used outside semihtml - so we do them in a copy of our output, and only use that copy if we find we are able to do a 100% Comcode conversion $semihtml2 = str_replace('<ocpbr />', chr(10), $semihtml2); $semihtml2 = str_replace('<br />', chr(10), $semihtml2); $semihtml2 = str_replace('<br />', chr(10), $semihtml2); $semihtml2 = str_replace('<br>', chr(10), $semihtml2); $semihtml2 = str_replace('<hr width="100%" size="2" />', '<hr />', $semihtml2); $semihtml2 = str_replace('<hr size="2" width="100%" />', '<hr />', $semihtml2); $semihtml2 = str_replace('<hr width="100%" />', '<hr />', $semihtml2); $semihtml2 = str_replace(chr(10) . '<hr />', chr(10) . "---------------" . chr(10), $semihtml2); $semihtml2 = str_replace(chr(10) . '<hr>', chr(10) . "---------------" . chr(10), $semihtml2); $semihtml2 = preg_replace('#<hr\\s*/>#', chr(10) . "---------------" . chr(10), $semihtml2); $semihtml2 = str_replace('<hr>', chr(10) . "---------------" . chr(10), $semihtml2); // We transform any HTML in there to Comcode if we can $smilies = $GLOBALS['FORUM_DRIVER']->find_emoticons(); foreach ($smilies as $code => $imgcode) { if ($imgcode[0] == 'EMOTICON_IMG_CODE_THEMED') { $imgcode[1] = find_theme_image($imgcode[1], true); if ($imgcode[1] == '') { continue; } // Theme image gone missing } $imgcode[1] = str_replace(get_base_url(), '', $imgcode[1]); $semihtml2 = preg_replace('#<img [^>]*src="[^"]*' . str_replace('#', '\\#', preg_quote(escape_html($imgcode[1]))) . '"[^>]*>\\s*#si', $code, $semihtml2); } $semihtml2 = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*) />#siU', '_img_tag_fixup', $semihtml2); $semihtml2 = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*)>#siU', '_img_tag_fixup', $semihtml2); // Then, if there is no HTML left, we can avoid the 'semihtml' tag if (strpos($semihtml2, '<') === false) { // $semihtml2=str_replace(array('<','>','&'),array('___lt___','___gt___','___amp___'),$semihtml2); $semihtml2 = @html_entity_decode($semihtml2, ENT_QUOTES, get_charset()); // $semihtml2=str_replace(array('___lt___','___gt___','___amp___'),array('<','>','&'),$semihtml2); return $semihtml2; } // Oh well, we couldn't do a perfect conversion, so we'll have to use semihtml. $semihtml = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*) />#siU', '_img_tag_fixup', $semihtml); $semihtml = preg_replace_callback('#<img([^>]*) src="([^"]*)"([^>]*)>#siU', '_img_tag_fixup', $semihtml); $semihtml = str_replace('<ocpbr />', chr(10), $semihtml); // Make it look slightly reasonable first (to the reader of the Comcode) $semihtml = str_replace('<br />', '<br />', $semihtml); $semihtml = str_replace('<br />', '<br />' . chr(10), $semihtml); $semihtml = str_replace('</p>', '</p>' . chr(10), $semihtml); $semihtml = str_replace('[/align]', '[/align]' . chr(10), $semihtml); return '[semihtml]' . $semihtml . '[/semihtml]'; }
/** * Standard modular run function. * * @param array A map of parameters. * @return tempcode The result of execution. */ function run($map) { if (has_no_forum()) { return new ocp_tempcode(); } require_lang('news'); require_css('news'); require_code('xhtml'); $num_topics = array_key_exists('param', $map) ? intval($map['param']) : 14; $forum_name = array_key_exists('forum', $map) ? $map['forum'] : do_lang('NEWS'); $num_topics = intval($num_topics); $date_key = array_key_exists('date_key', $map) ? $map['date_key'] : 'lasttime'; $rows = array(); $archive_url = NULL; $submit_url = new ocp_tempcode(); $forum_ids = array(); if (get_forum_type() == 'ocf' && (strpos($forum_name, ',') !== false || preg_match('#\\d[-\\*\\+]#', $forum_name) != 0 || is_numeric($forum_name))) { require_code('ocfiltering'); $forum_names = ocfilter_to_idlist_using_db($forum_name, 'id', 'f_forums', 'f_forums', 'f_parent_forum', 'f_parent_forum', 'id', true, true, $GLOBALS['FORUM_DB']); } else { $forum_names = explode(',', $forum_name); } foreach ($forum_names as $forum_name) { $forum_name = is_integer($forum_name) ? strval($forum_name) : trim($forum_name); if ($forum_name == '<announce>') { $forum_id = NULL; } else { $forum_id = is_numeric($forum_name) ? intval($forum_name) : $GLOBALS['FORUM_DRIVER']->forum_id_from_name($forum_name); } if (!is_null($forum_id)) { $forum_ids[$forum_id] = $forum_name; if (is_null($archive_url)) { $archive_url = $GLOBALS['FORUM_DRIVER']->forum_url($forum_id); // First forum will count as archive if (get_forum_type() == 'ocf') { $submit_url = build_url(array('page' => 'topics', 'type' => 'new_topic', 'id' => $forum_id), get_module_zone('topics')); } } } } // if ((method_exists($GLOBALS['FORUM_DRIVER'],'supports_multi_show_forum_topics')) && ($GLOBALS['FORUM_DRIVER']->supports_multi_show_forum_topics()) && (count($forum_ids)!=0)) // { $max_rows = 0; $rows = $GLOBALS['FORUM_DRIVER']->show_forum_topics($forum_ids, $num_topics, 0, $max_rows, '', true, $date_key); if (is_null($rows)) { $rows = array(); } /* } else { foreach ($forum_ids as $forum_id=>$forum_name) { $max_rows=0; $t=$GLOBALS['FORUM_DRIVER']->show_forum_topics($forum_name,$num_topics,0,$max_rows,'',true); if (!is_null($t)) $rows=array_merge($rows,$t); } }*/ global $M_SORT_KEY; $M_SORT_KEY = $date_key; usort($rows, 'multi_sort'); $rows = array_reverse($rows, false); $_title = do_lang_tempcode('NEWS'); if (array_key_exists('title', $map) && $map['title'] != '') { $_title = protect_from_escaping(escape_html($map['title'])); } $i = 0; $news_text = new ocp_tempcode(); while (array_key_exists($i, $rows)) { $myrow = $rows[$i]; $id = $myrow['id']; $date = get_timezoned_date($myrow[$date_key]); $author_url = array_key_exists('member_based', $map) && $map['member_based'] == '1' || !addon_installed('authors') ? new ocp_tempcode() : build_url(array('page' => 'authors', 'type' => 'misc', 'author' => $myrow['firstusername']), get_module_zone('authors')); $author = $myrow['firstusername']; $news_title = escape_html($myrow['title']); $news = is_object($myrow['firstpost']) ? $myrow['firstpost'] : make_string_tempcode(xhtmlise_html($myrow['firstpost'])); if (is_null($news)) { $news = ''; } $full_url = $GLOBALS['FORUM_DRIVER']->topic_url($id, ''); $news_text->attach(do_template('NEWS_PIECE_SUMMARY', array('_GUID' => '2edf18daf5510495fd588cad062aec4e', 'TRUNCATE' => false, 'BLOG' => false, 'FIRSTTIME' => strval($myrow['firsttime']), 'LASTTIME' => strval($myrow['lasttime']), 'CLOSED' => strval($myrow['closed']), 'FIRSTUSERNAME' => $myrow['firstusername'], 'LASTUSERNAME' => $myrow['lastusername'], 'FIRSTMEMBERID' => strval($myrow['firstmemberid']), 'LASTMEMBERID' => strval($myrow['lastmemberid']), 'ID' => strval($id), 'FULL_URL' => $full_url, 'SUBMITTER' => strval($myrow['firstmemberid']), 'DATE' => $date, 'DATE_RAW' => strval($myrow[$date_key]), 'NEWS_TITLE' => $news_title, 'CATEGORY' => '', 'IMG' => '', 'AUTHOR' => $author, 'AUTHOR_URL' => $author_url, 'NEWS' => $news))); $i++; if ($i == $num_topics) { break; } } if ($news_text->is_empty()) { return do_template('BLOCK_NO_ENTRIES', array('_GUID' => 'f55c90205b4c80162494fc5e2b565ce6', 'HIGH' => false, 'TITLE' => $_title, 'MESSAGE' => do_lang_tempcode('NO_NEWS'), 'ADD_NAME' => do_lang_tempcode('ADD_TOPIC'), 'SUBMIT_URL' => $submit_url)); } if (is_null($forum_id)) { $archive_url = ''; } return do_template('BLOCK_MAIN_FORUM_NEWS', array('_GUID' => '36b05da9aed5a2056bdb266e2ce4be9f', 'TITLE' => $_title, 'FORUM_NAME' => array_key_exists('forum', $map) ? $map['forum'] : do_lang('NEWS'), 'CONTENT' => $news_text, 'BRIEF' => new ocp_tempcode(), 'ARCHIVE_URL' => $archive_url, 'SUBMIT_URL' => $submit_url, 'RSS_URL' => '', 'ATOM_URL' => '')); }
/** * Parse the complete text of the inside of the tag. * * @param object The parser object (same as 'this') * @param string The text */ function trueStartText($parser, $data) { unset($parser); $prelast_tag = array_peek($this->tag_stack, 2); $last_tag = array_peek($this->tag_stack); $attributes = array_peek($this->attribute_stack); switch ($this->type) { case 'RSS': switch ($prelast_tag) { case 'CHANNEL': switch ($last_tag) { // dc namespace case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:PUBLISHER': $this->gleamed_feed['author'] = $data; break; case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:CREATOR': $this->gleamed_feed['author_email'] = $data; break; case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:RIGHTS': $this->gleamed_feed['copyright'] = $data; break; case 'TITLE': $this->gleamed_feed['title'] = $data; break; case 'LINK': $this->gleamed_feed['url'] = $data; break; case 'DESCRIPTION': if ($this->version == '0.90' || $this->version == '0.91' || $this->version == '0.94' && $attributes['type'] != 'text/html') { $data = str_replace("\n", '<br />', escape_html($data)); } $this->gleamed_feed['description'] = $data; break; case 'COPYRIGHT': if (strpos($data, '(C)') !== false) { $data2 = preg_replace_callback('(http://[^, \\)]+[^\\. ])', 'extract_plain_links', escape_html($data)); $data2 = str_replace('(C)', '©', $data2); } else { $data2 = $data; } $this->gleamed_feed['copyright'] = $data2; break; case 'MANAGINGEDITOR': $bracket = strpos($data, '('); if ($bracket !== false) { $bracket2 = strrpos(substr($data, $bracket), ')') + $bracket; if ($bracket2 === false) { $bracket2 = $bracket; } $this->gleamed_feed['author'] = substr($data, $bracket + 1, $bracket2 - $bracket - 1); $this->gleamed_feed['author_email'] = substr($data, 0, $bracket); } else { $this->gleamed_feed['author_email'] = $data; } break; case 'CLOUD': $cloud = array(); $cloud['domain'] = $attributes['DOMAIN']; $cloud['port'] = $attributes['PORT']; $cloud['path'] = $attributes['PATH']; $cloud['registerProcedure'] = $attributes['REGISTERPROCEDURE']; $cloud['protocol'] = $attributes['PROTOCOL']; $this->gleamed_feed['cloud'] = $cloud; break; } break; case 'ITEM': $current_item =& $this->gleamed_items[count($this->gleamed_items) - 1]; switch ($last_tag) { // dc namespace case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:CREATOR': $current_item['author'] = $data; break; case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:SUBJECT': $current_item['category'] = $data; break; case 'HTTP://PURL.ORG/DC/ELEMENTS/1.1/:DATE': $a = cleanup_date($data); $current_item['add_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_add_date'] = $a[1]; } break; case 'HTTP://PURL.ORG/RSS/1.0/MODULES/CONTENT/:ENCODED': $current_item['news'] = $data; if (preg_match('#[<>]#', $current_item['news']) == 0 && preg_match('#[<>]#', html_entity_decode($current_item['news'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['news'] = @html_entity_decode($current_item['news'], ENT_QUOTES); } elseif (preg_match('#&(?!amp;)#', $current_item['news']) == 0 && preg_match('#&#', html_entity_decode($current_item['news'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['news'] = @html_entity_decode($current_item['news'], ENT_QUOTES); } if (preg_match('#^http://ocportal.com/#', $this->feed_url) == 0) { require_code('xhtml'); $current_item['news'] = xhtmlise_html($current_item['news']); } break; // slash namespace // slash namespace case 'HTTP://PURL.ORG/RSS/1.0/modules/slash:SECTION': $current_item['category'] = $data; break; case 'TITLE': $current_item['title'] = $data; if (preg_match('#[<>]#', $current_item['title']) == 0 && preg_match('#[<>]#', html_entity_decode($current_item['title'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['title'] = @html_entity_decode($current_item['title'], ENT_QUOTES); } elseif (preg_match('#&(?!amp;)#', $current_item['title']) == 0 && preg_match('#&#', html_entity_decode($current_item['title'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['title'] = @html_entity_decode($current_item['title'], ENT_QUOTES); } if (preg_match('#^http://ocportal.com/#', $this->feed_url) == 0) { require_code('xhtml'); $current_item['title'] = xhtmlise_html($current_item['title']); } break; case 'DESCRIPTION': //echo "here";exit(); $current_item['news'] = $data; if (preg_match('#[<>]#', $current_item['news']) == 0 && preg_match('#[<>]#', html_entity_decode($current_item['news'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['news'] = @html_entity_decode($current_item['news'], ENT_QUOTES); } elseif (preg_match('#&(?!amp;)#', $current_item['news']) == 0 && preg_match('#&#', html_entity_decode($current_item['news'], ENT_QUOTES)) != 0) { // Double escaped HTML $current_item['news'] = @html_entity_decode($current_item['news'], ENT_QUOTES); } elseif (strpos($current_item['news'], '>') === false) { $current_item['news'] = nl2br(escape_html($current_item['news'])); } if (preg_match('#^http://ocportal.com/#', $this->feed_url) == 0) { require_code('xhtml'); $current_item['news'] = xhtmlise_html($current_item['news']); } break; case 'PUBDATE': $a = cleanup_date($data); $current_item['add_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_add_date'] = $a[1]; } break; case 'LINK': $current_item['full_url'] = $data; break; case 'AUTHOR': $bracket = strpos($data, '('); if ($bracket !== false) { $bracket2 = strpos($data, ')', $bracket); $current_item['author'] = substr($data, $bracket + 1, $bracket2 - $bracket - 1); $current_item['author_email'] = rtrim(substr($data, 0, $bracket)); } else { $current_item['author_email'] = $data; } if ($current_item['author_email'] == get_option('staff_address')) { unset($current_item['author_email']); } break; case 'CATEGORY': $current_item['category'] = $data; break; case 'SOURCE': $current_item['author'] = $data; break; case 'COMMENTS': $current_item['comment_url'] = $data; break; case 'GUID': if (!array_key_exists('ISPERMALINK', $attributes) || $attributes['ISPERMALINK'] == 'true') { $current_item['guid'] = $data; } break; } break; } break; case 'ATOM': if (array_key_exists('TYPE', $attributes)) { $type = str_replace('text/', '', $attributes['TYPE']); } else { $type = 'plain'; } if (array_key_exists('MODE', $attributes)) { $mode = $attributes['MODE']; } else { $mode = 'xml'; } if ($mode == 'BASE64') { $data = base64_decode($data); } if (function_exists('xml_set_start_namespace_decl_handler')) { $prefix = 'HTTP://PURL.ORG/ATOM/NS#:'; } else { $prefix = ''; } if (!is_null($prelast_tag)) { $prelast_tag = str_replace('HTTP://WWW.W3.ORG/2005/ATOM:', $prefix, $prelast_tag); } $last_tag = str_replace('HTTP://WWW.W3.ORG/2005/ATOM:', $prefix, $last_tag); switch ($prelast_tag) { case $prefix . 'AUTHOR': $preprelast_tag = array_peek($this->tag_stack, 3); switch ($preprelast_tag) { case $prefix . 'FEED': switch ($last_tag) { case $prefix . 'NAME': $this->gleamed_feed['author'] = $data; break; case $prefix . 'URL': $this->gleamed_feed['author_url'] = $data; break; case $prefix . 'EMAIL': $this->gleamed_feed['author_email'] = $data; break; } break; case $prefix . 'ENTRY': $current_item =& $this->gleamed_items[count($this->gleamed_items) - 1]; switch ($last_tag) { case $prefix . 'NAME': $current_item['author'] = $data; break; case $prefix . 'URL': $current_item['author_url'] = $data; break; case $prefix . 'EMAIL': $current_item['author_email'] = $data; break; } break; } case $prefix . 'FEED': switch ($last_tag) { case $prefix . 'TITLE': $this->gleamed_feed['title'] = $data; break; case $prefix . 'LINK': $rel = array_key_exists('REL', $attributes) ? $attributes['REL'] : 'alternate'; if ($rel == 'alternate') { $this->gleamed_feed['url'] = array_key_exists('HREF', $attributes) ? $attributes['HREF'] : $data; } break; case $prefix . 'UPDATED': $a = cleanup_date($data); $current_item['edit_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_edit_date'] = $a[1]; } break; case $prefix . 'MODIFIED': $a = cleanup_date($data); $current_item['edit_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_edit_date'] = $a[1]; } break; case $prefix . 'RIGHTS': $this->gleamed_feed['copyright'] = $data; break; case $prefix . 'COPYRIGHT': $this->gleamed_feed['copyright'] = $data; break; case $prefix . 'SUBTITLE': $this->gleamed_feed['description'] = $data; break; case $prefix . 'TAGLINE': $this->gleamed_feed['description'] = $data; break; } break; case $prefix . 'ENTRY': $current_item =& $this->gleamed_items[count($this->gleamed_items) - 1]; switch ($last_tag) { case $prefix . 'TITLE': $current_item['title'] = $data; break; case $prefix . 'LINK': if (!array_key_exists('REL', $attributes) || $attributes['REL'] == 'alternate') { $current_item['full_url'] = array_key_exists('HREF', $attributes) ? $attributes['HREF'] : $data; } break; case $prefix . 'MODIFIED': case $prefix . 'UPDATED': $a = cleanup_date($data); $current_item['edit_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_edit_date'] = $a[1]; } break; case $prefix . 'PUBLISHED': case $prefix . 'ISSUED': $a = cleanup_date($data); $current_item['add_date'] = $a[0]; if (array_key_exists(1, $a)) { $current_item['clean_add_date'] = $a[1]; } break; case $prefix . 'ID': $current_item['guid'] = $data; break; case $prefix . 'SUMMARY': if ($type != 'html') { $data = str_replace("\n", '<br />', $data); } $current_item['news'] = $data; if (preg_match('#^http://ocportal.com/#', $this->feed_url) == 0) { require_code('xhtml'); $current_item['news'] = xhtmlise_html($current_item['news']); } break; case $prefix . 'CONTENT': if ($type != 'html') { $data = str_replace("\n", '<br />', $data); } $current_item['news_article'] = $data; if (preg_match('#^http://ocportal.com/#', $this->feed_url) == 0) { require_code('xhtml'); $current_item['news_article'] = xhtmlise_html($current_item['news_article']); } break; case $prefix . 'CATEGORY': if ($data == '' && array_key_exists('TERM', $attributes)) { $data = $attributes['TERM']; } if ($data != '' && strpos($data, '#') === false) { if (array_key_exists('category', $current_item)) { if (!array_key_exists('extra_categories', $current_item)) { $current_item['extra_categories'] = array(); } $current_item['extra_categories'][] = $data; } else { $current_item['category'] = $data; } } if (array_key_exists('TERM', $attributes) && strpos($attributes['TERM'], 'post') === false && strpos($attributes['TERM'], '://') !== false) { $current_item['bogus'] = true; } break; case 'HTTP://SEARCH.YAHOO.COM/MRSS/:THUMBNAIL': if (array_key_exists('URL', $attributes)) { $current_item['rep_image'] = $attributes['URL']; } break; } break; } break; } }
/** * Filter HTML that has been read, to make it more compatible with ocPortal. * * @param string The HTML * @param BINARY Whether to fix XHTML errors * @param PATH The base URL of the old site * @param array A list of all files on the site * @param PATH The base directory we are importing from * @return string Filtered HTML */ function _html_filter($file_contents, $fix_html, $base_url, $files, $file_base) { // If selected, clean up all the HTML if ($fix_html == 1) { require_code('xhtml'); $file_contents = xhtmlise_html($file_contents); } // Strip base URL if ($base_url != '') { $file_contents = str_replace($base_url . '/', '', $file_contents); $file_contents = str_replace(escape_html($base_url . '/'), '', $file_contents); } // Extra sense for rewriting local URLs in templates to image links and page-links $matches = array(); $num_matches = preg_match_all('# (src|href)="([^"]*)"#', $file_contents, $matches); for ($i = 0; $i < $num_matches; $i++) { $this_url = $matches[2][$i]; $this_url = preg_replace('#^\\.*/#', '', $this_url); if (trim($this_url) == '') { continue; } if (strpos($this_url, '://') === false || substr($this_url, 0, strlen($base_url)) == $base_url) { if (strpos($this_url, '://') !== false) { $this_url = substr($this_url, strlen($base_url)); } $decoded_url = rawurldecode($this_url); if (substr($decoded_url, 0, 2) == './') { $decoded_url = substr($decoded_url, 2); } // Links to directories in a deep path will be changed so underscores replace slashes if (substr(trim($decoded_url), -4) == '.htm' || substr(trim($decoded_url), -5) == '.html') { if (substr_count($decoded_url, '/') > 1) { $last_slash_pos = strrpos($decoded_url, '/'); $decoded_url = str_replace('/', '_', substr($decoded_url, 0, $last_slash_pos)) . substr($decoded_url, 0, $last_slash_pos); } $decoded_url = trim(preg_replace('#(^|/)index\\.htm[l]#', '${1}start.htm', $decoded_url)); $stripped_decoded_url = preg_replace('#\\..*$#', '', $decoded_url); if (strpos($stripped_decoded_url, '/') === false) { $stripped_decoded_url = '/' . $stripped_decoded_url; } list($zone, $page) = explode('/', $stripped_decoded_url, 2); if ($page == 'index') { $page = 'start'; } $file_contents = str_replace($matches[2][$i], '{$PAGE_LINK*,' . $zone . ':' . $page . '}', $file_contents); } else { if (in_array($decoded_url, $files)) { $target = get_custom_file_base() . '/uploads/website_specific/' . $decoded_url; $create_path = $target; do { @mkdir(dirname($create_path), 0777); $create_path = dirname($create_path); } while (strlen($create_path) > 1); @unlink($target); @copy($file_base . '/' . $decoded_url, $target); /*if (substr($decoded_url,-4)=='.css') Not needed, as relative paths maintained { $css_file=file_get_contents($target); $css_file=preg_replace('#(url\([\'"]?)(\.*'.'/)?#','${1}{$BASE_URL;}/uploads/website_specific/',$css_file); $my_css_file=fopen($target,'wt'); fwrite($my_css_file,$css_file); fclose($my_css_file); }*/ fix_permissions($target); sync_file($target); } $decoded_url = 'uploads/website_specific/' . $decoded_url; $file_contents = str_replace('src="' . $matches[2][$i] . '"', 'src="{$BASE_URL*}/' . str_replace('%2F', '/', rawurlencode($decoded_url)) . '"', $file_contents); $file_contents = str_replace('href="' . $matches[2][$i] . '"', 'href="{$BASE_URL*}/' . str_replace('%2F', '/', rawurlencode($decoded_url)) . '"', $file_contents); } } } return $file_contents; }
/** * Render a code box. * * @param string The data type (e.g. file extension) we are rendering. * @param tempcode Contents (code) to render. * @param boolean Whether to show line numbers. * @param boolean Whether what we have came from inside a semihtml tag * @param boolean Whether what we have came from semihtml mode * @return array A pair: The tempcode for the code box, and the title of the box */ function do_code_box($type, $embed, $numbers = true, $in_semihtml = false, $is_all_semihtml = false) { $_embed = mixed(); $title = mixed(); if (file_exists(get_file_base() . '/sources/geshi/' . filter_naughty(strtolower($type)) . '.php') || file_exists(get_file_base() . '/sources_custom/geshi/' . filter_naughty($type) . '.php')) { $evaluated = $embed->evaluate(); if ($in_semihtml || $is_all_semihtml) { require_code('comcode_from_html'); $evaluated = semihtml_to_comcode($evaluated, true); } require_code('geshi'); if (class_exists('GeSHi')) { require_code('developer_tools'); destrictify(); $geshi = new GeSHi($evaluated, strtolower($type)); $geshi->set_header_type(GESHI_HEADER_DIV); if ($numbers) { $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); } require_lang('comcode'); $title = do_lang_tempcode('comcode:CODE_IN_LANGUAGE', escape_html($type)); require_code('xhtml'); $_embed = xhtmlise_html($geshi->parse_code()); restrictify(); } } else { switch (strtolower($type)) { case 'php': if (!function_exists('highlight_string')) { break; } $evaluated = $embed->evaluate(); if ($in_semihtml || $is_all_semihtml) { require_code('comcode_from_html'); $evaluated = semihtml_to_comcode($evaluated); } if (strpos($evaluated, '<' . '?php') === false) { $strip = true; $evaluated = "<" . "?php\n" . $evaluated . "\n?" . ">"; } else { $strip = false; } require_code('xhtml'); if (defined('HIPHOP_PHP')) { $h_result = nl2br(escape_html($evaluated)); } else { ob_start(); highlight_string($evaluated); $h_result = ob_get_contents(); ob_end_clean(); } $_embed = xhtmlise_html($h_result); if ($strip) { $_embed = str_replace('<?php<br />', '', $_embed); $_embed = str_replace('?>', '', $_embed); } $title = do_lang_tempcode('PHP_CODE'); break; } } return array($_embed, $title); }
/** * Standard modular run function for OcCLE hooks. * * @param array The options with which the command was called * @param array The parameters with which the command was called * @param array A reference to the OcCLE filesystem object * @return array Array of stdcommand, stdhtml, stdout, and stderr responses */ function run($options, $parameters, &$occle_fs) { require_code('obfuscate'); if (array_key_exists('h', $options) || array_key_exists('help', $options)) { return array('', do_command_help('feed_display', array('h', 'm'), array(true)), '', ''); } else { if (!array_key_exists(0, $parameters)) { return array('', '', '', do_lang('MISSING_PARAM', '1', 'feed_display')); } require_lang('news'); require_code('rss'); $rss = new rss($parameters[0]); if (!is_null($rss->error)) { return array('', '', '', $rss->error); } if (!array_key_exists('title', $rss->gleamed_feed)) { $rss->gleamed_feed['title'] = do_lang('RSS_STREAM'); } if (!array_key_exists('copyright', $rss->gleamed_feed)) { $rss->gleamed_feed['copyright'] = ''; } // Reduce what we collected about the feed to a minimum. This is very awkward, as we don't know what's here. if (array_key_exists('author', $rss->gleamed_feed)) { $__author = NULL; $_author = $rss->gleamed_feed['author']; if (array_key_exists('url', $rss->gleamed_feed)) { $__author = hyperlink($rss->gleamed_feed['url'], escape_html($_author), true); } elseif (array_key_exists('author_url', $rss->gleamed_feed)) { $__author = hyperlink($rss->gleamed_feed['author_url'], escape_html($_author), true); } elseif (array_key_exists('author_email', $rss->gleamed_feed)) { $__author = hyperlink(mailto_obfuscated() . obfuscate_email_address($rss->gleamed_feed['author_email']), escape_html($_author), true); } if (!is_null($__author)) { $_author = $__author->evaluate(); } $_author = do_lang('RSS_SOURCE_FROM', $_author); } else { $_author = ''; } // Now for the actual stream contents $max = array_key_exists('max', $options) ? intval($options['max']) : 5; $content = new ocp_tempcode(); require_code('xhtml'); foreach ($rss->gleamed_items as $i => $item) { if ($i >= $max) { break; } if (array_key_exists('guid', $item)) { $full_url = $item['guid']; } elseif (array_key_exists('comment_url', $item)) { $full_url = $item['comment_url']; } elseif (array_key_exists('full_url', $item)) { $full_url = $item['full_url']; } else { $full_url = ''; } $_title = $item['title']; $date = array_key_exists('clean_add_date', $item) ? get_timezoned_date($item['clean_add_date']) : array_key_exists('add_date', $item) ? $item['add_date'] : ''; $content->attach(do_template('OCCLE_RSS_ITEM', array('_GUID' => 'd6b811d17956ebd82c9911fbda19eeec', 'FULL_URL' => $full_url, 'NEWS_TITLE' => $_title, 'DATE' => $date, 'SUMMARY' => xhtmlise_html($item['news'])))); } return array('', do_template('OCCLE_RSS', array('TITLE' => $rss->gleamed_feed['title'], 'CONTENT' => $content)), '', ''); } }
/** * AJAX script for dynamic inclusion of XHTML snippets. */ function snippet_script() { header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header('Content-Type: text/plain; charset=' . get_charset()); $hook = filter_naughty_harsh(get_param('snippet')); require_code('hooks/systems/snippets/' . $hook, true); $object = object_factory('Hook_' . $hook); $tempcode = $object->run(); $out = $tempcode->evaluate(); if (strpos($out, chr(10)) !== false && $hook != 'css_compile') { require_code('xhtml'); $out = xhtmlise_html($out, true); } // End early execution listening (this means register_shutdown_function will run after connection closed - faster) if (function_exists('apache_setenv')) { @apache_setenv('no-gzip', '1'); } @ini_set('zlib.output_compression', 'Off'); $size = strlen($out); header('Connection: close'); ignore_user_abort(true); header('Content-Encoding: none'); header('Content-Length: ' . strval($size)); echo $out; @ob_end_flush(); flush(); }
/** * Render a PHP function to display in a template. * * @param array The map of function information * @param array The map of class information * @param boolean Show filenames in the function description * @return array A pair: The rendered function, The rendered summary (for a TOC) */ function render_php_function($function, $class, $show_filename = false) { $parameters = new ocp_tempcode(); $full_parameters = new ocp_tempcode(); foreach ($function['parameters'] as $parameter) { // if (!array_key_exists('type',$parameter)) exit($function['name']); if (!$parameters->is_empty()) { $parameters->attach(', '); } $parameters->attach(do_template('PHP_PARAMETER_LIST', array('_GUID' => '03e76c19ec2cf9cb7f283db72728fc13', 'TYPE' => $parameter['type'], 'NAME' => $parameter['name']))); $bits = render_php_function_do_bits($parameter); $full_parameters->attach(do_template('PHP_PARAMETER', array('_GUID' => 'fa1f59637723d35da5e210e4efa0e27c', 'BITS' => $bits))); } if (array_key_exists('return', $function)) { $return = render_php_function_do_bits($function['return']); $return_type = $function['return']['type']; } else { $return = new ocp_tempcode(); $return_type = 'void'; } $description = comcode_to_tempcode($function['description']); if (function_exists('highlight_string') && array_key_exists('code', $function) && $function['filename'] != 'sources/phpstub.php') { $_code = "<" . "?php\n" . $function['code'] . "\n?" . ">"; ob_start(); highlight_string($_code); $code = ob_get_contents(); ob_end_clean(); $code = str_replace('<?php<br />', '', $code); $code = str_replace('?>', '', $code); require_code('xhtml'); $code = xhtmlise_html($code); } else { $code = ''; } $filename = $show_filename ? $function['filename'] : ''; if (!isset($class['name'])) { $class['name'] = ''; } $a = do_template('PHP_FUNCTION', array('_GUID' => 'f01224ffadc5cde023a1777b9267da61', 'FILENAME' => $filename, 'CODE' => $code, 'RETURN_TYPE' => $return_type, 'FUNCTION' => $function['name'], 'CLASS' => $class['name'], 'PARAMETERS' => $parameters, 'DESCRIPTION' => $description, 'FULL_PARAMETERS' => $full_parameters, 'RETURN' => $return)); $b = do_template('PHP_FUNCTION_SUMMARY', array('_GUID' => 'ac91501d0fcef2f17c7f068f0d506d42', 'FILENAME' => $filename, 'RETURN_TYPE' => $return_type, 'CLASS' => $class['name'], 'FUNCTION' => $function['name'], 'PARAMETERS' => $parameters)); return array($a, $b); }
/** * Highlight keywords in an extracted portion of a piece of text. * * @param string What was searched * @param array List of words searched * @return string Highlighted portion */ function generate_text_summary($_temp_summary, $words_searched) { require_code('xhtml'); $summary = ''; global $SEARCH__CONTENT_BITS; $_temp_summary_lower = strtolower($_temp_summary); // Add in some highlighting direct to XHTML $all_occurrences = array(); foreach ($words_searched as $content_bit) { if ($content_bit == '') { continue; } $last_pos = 0; $content_bit_pos = 0; do { $content_bit_matched = $content_bit; if (strtoupper($content_bit) == $content_bit) { $content_bit_pos = strpos($_temp_summary, $content_bit, $last_pos); } else { $content_bit_pos = strpos($_temp_summary_lower, strtolower($content_bit), $last_pos); if (strpos($content_bit, '-') !== false) { $content_bit_pos_2 = strpos($_temp_summary_lower, strtolower(str_replace('-', '', $content_bit)), $last_pos); if ($content_bit_pos_2 !== false && ($content_bit_pos === false || $content_bit_pos_2 < $content_bit_pos)) { $content_bit_pos = $content_bit_pos_2; $content_bit_matched = str_replace('-', '', $content_bit); } } } if ($content_bit_pos !== false) { $last_gt = strrpos(substr($_temp_summary, 0, $content_bit_pos), '>'); $last_lt = strrpos(substr($_temp_summary, 0, $content_bit_pos), '<'); if ($last_gt === false || $last_gt > $last_lt) { $extra_pre = '<span class="comcode_highlight">'; $extra_post = '</span>'; $_temp_summary = substr($_temp_summary, 0, $content_bit_pos) . $extra_pre . substr($_temp_summary, $content_bit_pos, strlen($content_bit_matched)) . $extra_post . substr($_temp_summary, $content_bit_pos + strlen($content_bit_matched)); $_temp_summary_lower = strtolower($_temp_summary); $last_pos = $content_bit_pos + strlen($extra_pre) + strlen($content_bit_matched) + strlen($extra_post); // Adjust all stores occurrence offsets foreach ($all_occurrences as $i => $occ) { if ($occ[0] > $last_pos) { $all_occurrences[$i][0] += strlen($extra_pre) + strlen($extra_post); $all_occurrences[$i][1] += strlen($extra_pre) + strlen($extra_post); } elseif ($occ[0] > $content_bit_pos) { $all_occurrences[$i][0] += strlen($extra_pre); $all_occurrences[$i][1] += strlen($extra_pre); } } $all_occurrences[] = array($content_bit_pos, $last_pos); } else { $last_pos = $content_bit_pos + strlen($content_bit_matched); } } } while ($content_bit_pos !== false); } if (strlen($_temp_summary) < 500) { $summary = $_temp_summary; } else { // Find optimal position $len = strlen($_temp_summary); $best_yet = 0; $best_pos_min = 250; $best_pos_max = 250; if (count($all_occurrences) < 60) { for ($i = 250; $i < $len - 250; $i++) { $count = 0; $i_pre = $i - 250; $i_post = $i + 250; foreach ($all_occurrences as $occ) { $occ_pre = $occ[0]; $occ_post = $occ[1]; if ($occ_pre >= $i_pre && $occ_pre <= $i_post && $occ_post >= $i_pre && $occ_post <= $i_post) { $count++; if ($count > 5) { break; } // Good enough } } if ($count > $best_yet || $best_yet == $count && $i - 500 < $best_pos_min) { if ($best_yet == $count) { $best_pos_max = $i; } else { $best_yet = $count; $best_pos_min = $i; $best_pos_max = $i; } if ($count > 5) { break; } // Good enough } } $best_pos = intval(floatval($best_pos_min + $best_pos_max) / 2.0) - 250; // Move it from center pos, to where we want to start from } else { $best_pos = 0; } // Render (with ellipses if required) if (false) { // Far far too slow $summary = xhtml_substr($_temp_summary, $best_pos, min(500, $len - $best_pos), true, true); } else { $summary = substr($_temp_summary, $best_pos, min(500, $len - $best_pos)); $summary = xhtmlise_html($summary, true); if ($best_pos > 0) { $summary = '…' . $summary; } if ($best_pos + 500 < strlen($_temp_summary)) { $summary .= '…'; } } } return $summary; }