All pastes #510771 Raw Edit

Something

public text v1 · immutable
·published 2007-05-26 01:47 UTC
rendered paste body
<?php
	require_once('lib/misc.php');
	require_once('lib/db.php');
	require_once('lib/geshi/geshi.php');
	
	function get_tag($match)
	{
		$q = 'SELECT text FROM tags WHERE name="' . $match[1] . '";';
		$row = mysql_fetch_assoc(mysql_query($q));
		return eval($row['text']);
	}

	function make_link($match)
	{
		list($pagename, $dispname) = explode('|', $match[1], 2);
		if (!isset($dispname))
			$dispname = $pagename;

		$page = urlencode(trim($pagename));
		$p = get_page($pagename);
		if (isset($p['title']))
			return '<a class="existing_page" href="?page=' . $page . '">' . $dispname . '</a>';
		else
			return '<a class="missing_page" href="?page=' . $page . '">' . $dispname . '</a>';
	}
	
	function make_edit_link($match)
	{
		list($pagename, $dispname) = explode('|', $match[1], 2);
		if (!isset($dispname))
			$dispname = $pagename;

		$page = urlencode(trim($pagename));
		return '<a class="existing_page" href="?do=edit&page=' . $page . '">' . $dispname . '</a>';
	}

	$toc = array();
	$toc_entries = 0;
	function make_h1($match)
	{
		global $toc, $toc_entries;
		$toc_entries++;
		$toc['s_' . $toc_entries] = $match[1];
		return '<a name="s_' . $toc_entries . '"></a><h1>' . $match[1] .
			'</h1><br><hr class="hr2">';
	}
	
	function make_h2($match)
	{
		return '<h2>' . trim($match[1]) . '</h2><br><hr class="hr3">';
	}
	
	function make_h3($match)
	{
		return '<h3>' . trim($match[1]) . '</h3><br>';
	}

	function convert_urls($text)
	{
		$text = preg_replace('!(^|([^\'"]\s*))((http|ftp|https|ftps):\/\/[^\s<>"\'()]{4,})!mi',
			'$2<a href="$3"><img src="images/icons/extlink.png" alt="external link"> $3</a>', $text);
		$text = preg_replace('!<a href="([^"]+)[\.:,\]]">!', '<a href="$1">', $text);
		$text = preg_replace('!([\.:,\]])</a>!', '</a>$1', $text);
		return $text;
	}

	$list_count = 0;
	function make_list($match)
	{
		global $list_count;
	
		if ($match[0] == '[list]')
		{
			$list_count++;
			
			return '<ul class="ul' . ($list_count < 3? $list_count : 3) . '">';
		}
		if ($match[0] == '[#list]')
		{
			$list_count++;
			
			return '<ol>';
		}
		else if ($match[0] == '[endlist]')
		{
			if ($list_count == 0)
				return '';

			$list_count--;
			return '</ul>';
		}
		else if ($match[0] == '[end#list]')
		{
			if ($list_count == 0)
				return '';
			
			$list_count--;
			
			return '</ol>';
		}
	}
	
	function make_li($match)
	{
		return '<li>' . trim($match[2]) . '</li>';
	}
	
	$cbnum = 0;
	function code_block($match)
	{
		global $cbnum;
		$cbnum++;
		$geshi =& new GeSHi(trim(str_replace('<br>', '', $match[2])), $match[1]);
		$geshi->set_header_type(GESHI_HEADER_NONE);
		$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
		$geshi->set_line_style('color: #666699', 'color: #000000;');

		switch ($match[1])
		{
		case 'php':
			$cbclass = '_php';
			break;
		case 'html4strict':
			$cbclass = '_html';
			break;
		case 'c':
		case 'cpp':
			$cbclass = '_c';
			break;
		case 'asm':
			$cbclass = '_asm';
			break;
		}
		
		return '<div class="codeblock' . $cbclass . '">' .
			'<div id="cb' . $cbnum . '_v" style="display: block;">' . $geshi->parse_code() .
			'<input type="button" value="Copy" onClick="javascript:show_copy(' . $cbnum . ');"></div>' .
			'<div id="cb' . $cbnum . '_c" style="display: none;">' .
			'<textarea class="codebox" rows="10" cols="80">' . br2nl($match[2]) . '</textarea>' .
			'<input type="button" value="View" onClick="javascript:show_view(' . $cbnum . ');"></div>' .
			'</div>';
	}
	
	function parse_smileys($text)
	{
		$result = mysql_query('SELECT * FROM smileys;');
		
		for ($i = 0; $i < mysql_num_rows($result); $i++)
		{
			$row = mysql_fetch_assoc($result);
			$text = str_replace($row['text'], '<img src="images/smileys/' . $row['image'] . '" alt="' . $row['text'] . '">', $text);
		}
		
		return $text;
	}
	
	function parse($text)
	{
		$text = strip_tags($text, '<div><img>');

		// bold, italic, underline, and strikethrough
		// These are worse than sed. Not only do you have to escape stuff, you have to
		// escape it again for PHP.
		$text = preg_replace('/(?<!\\\\)\*\*(.*)(?<!\\\\)\*\*/sU', '<b>\1</b>', $text);
		$text = preg_replace('/%\*\*/', '**', $text);
		$text = preg_replace('/(?<!\\\\)\/\/(.*)(?!\\\\)\/\//sU', '<i>\1</i>', $text);
		$text = preg_replace('/(\\\\)\/\//', '//', $text);
		$text = preg_replace('/(?<!\\\\)__(.*)(?!\\\\)__/sU', '<u>\1</u>', $text);
		$text = preg_replace('/(\\\\)__/', '__', $text);
		$text = preg_replace('/(?<!\\\\)--(.*)(?!\\\\)--/sU', '<del>\1</del>', $text);
		$text = preg_replace('/(\\\\)--/', '--', $text);

		// lists
		$text = preg_replace_callback('/\[(end)?(#)?list\]/m', 'make_list', $text);
		$text = preg_replace_callback('/(\s*)\*(.+)$/smU', 'make_li', $text);

		// external links
		$text = convert_urls($text);

		// links to wiki pages
		$text = preg_replace_callback('/(?<!\\\\)\[\[EDIT:(.+)(?!\\\\)\]\]/sU', 'make_edit_link', $text);
		$text = preg_replace_callback('/(?<!\\\\)\[\[(.+)(?!\\\\)\]\]/sU', 'make_link', $text);
		$text = preg_replace('/(\\\\)\\[\\[/', '[[', $text);
		$text = preg_replace('/(\\\\)\\]\\]/', ']]', $text);
		// edit links
		
		// horizontal rules
		$text = preg_replace('/====+/', '<hr>', $text);
		
		// headers
		$text = preg_replace_callback('/(?<!\\\\)==\(1\)(.+)(?!\\\\)==/sU', 'make_h1', $text);
		$text = preg_replace('/(\\\\)==\\(1\\)/', '==(1)', $text);
		$text = preg_replace_callback('/(?<!\\\\)==\(2\)(.+)(?!\\\\)==/sU', 'make_h2', $text);
		$text = preg_replace('/(\\\\)==\\(2\\)/', '==(2)', $text);
		$text = preg_replace_callback('/(?<!\\\\)==\(3\)(.+)(?!\\\\)==/sU', 'make_h3', $text);
		$text = preg_replace('/(\\\\)==\\(3\\)/', '==(3)', $text);
		$text = preg_replace('/(\\\\)==/', '==', $text);

		$text = parse_smileys($text);

		// newlines
		$text = preg_replace('/(?:\n[ \t\r]*){2,}/', "\n\n", $text);
		$text = preg_replace('/\n/', '<br>', $text);
		$text = str_replace('</li><br>', '</li>', $text);

		// code blocks
		$text = preg_replace_callback('/\[code=(.+)\](.+)\[endcode\]/smU', 'code_block', $text);

		$text = preg_replace_callback('/~~(.*)~~/sU', 'get_tag', $text);

		return $text;
	}
?>