All pastes #456960 Raw Edit

Stuff

public text v1 · immutable
#456960 ·published 2007-04-25 02:34 UTC
rendered paste body
<?php

header("Content-type: text/html; charset=utf-8");

include_once "../markdown.php";
include_once "../smartypants.php";

function html($output) {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Search [Newbold]</title>
<link href="/style.css" rel="stylesheet" type="text/css" media="all">
<link rel="alternate" type="application/atom+xml" title="Feed" href="http://newbold.name/feed.xml">
<link rel="home" title="Home" href="http://newbold.name/">
<script src="/mint/?js" type="text/javascript"></script>
</head>
<body>

<h1>Search</h1>

<form action="/search/" method="get">
<p><input type="text" name="q"><input type="submit" value="Search"></p>
</form>

'.$output.'

<p><a href="/" accesskey="1" title="Go home">Home</a></p>

<p>&copy; <a href="http://newbold.name/adam/" title="Adam Newbold\'s web page">Adam Newbold</a></p>

</body>
</html>';	
}

if(isset($_REQUEST['q']) && ($_REQUEST['q'] !== '')) {
	$q = strip_tags($_REQUEST['q']);
	$connection = mysql_connect('content.newbold.name', 'content', '') or die('Could not connect: ' . mysql_error());
	mysql_select_db('content') or die('Could not select database');
	$query = 'SELECT * FROM content WHERE page like "%'.$q.'%" OR title like "%'.$q.'%" OR content like "%'.$q.'%" ORDER BY modified DESC';
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$count = mysql_num_rows($result);
	if($count == 0) $rs = ' did not match any items.';
	if($count == 1) $rs = ' matched one item.';
	if($count >  1) $rs = ' matched '.$count.' items.';
	if($count >= 1) {
		$output = '<p>Your search for <strong>'.$q.'</strong>'.$rs.'</p>';
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$found = array();
			$content = strip_tags(SmartyPants(Markdown($line['content'])));
			$cr = explode(" ", $content);
			foreach($cr as $k=>$v) if(stristr($v, $q) !== false) $found[] = $k;
			if(count($found) !== 0) {
				$offset = 10;
				if($found[0] < $offset) { $start = 0; $p1 = ''; }
				else { $start = $found[0] - $offset; $p1 = '...'; }
				if($found[0]+$offset >= count($cr)) { $p2 = ''; }
				else { $p2 = '...'; }
				$cr[$found[0]] = '<strong>'.$cr[$found[0]].'</strong>';
				$context = array_slice($cr, $start, $offset*2);
				$context = $p1.implode(" ", $context).$p2.'<br>';
			}
			$output .= '<p><a href="http://newbold.name/'.$line['page'].'">'.$line['title'].'</a><br>'.$context.'<span style="color: #666;">newbold.name/'.$line['page'].'/ &nbsp;-&nbsp; '.date("F j, Y", $line['modified']).'</span></p>';
		}
	
	}
	else $output = '<p>Your search for <strong>'.$q.'</strong>'.$rs.'</p>';
	mysql_close($connection);
	
}
else $output .= '<p>You can search the content on this site using the form above.</p>';

html($output);

?>