searpl

a small php sqlite search engine. <a href="https://thunix.net/~xfnw/search">see it in action</a>
Log | Files | Refs | README

index.php (3245B)


      1 <?php
      2 ini_set('display_errors', '1');
      3 ini_set('display_startup_errors', '1');
      4 error_reporting(E_ALL);
      5 ?>
      6 <!DOCTYPE HTML>
      7 <html lang="en">
      8 <link rel="stylesheet" type="text/css" href="assets/style.css">
      9 <meta name="viewport" content="width=device-width, initial-scale=1" /> 
     10 <meta name="description" content="a search engine">
     11 <title>searpl</title>
     12 
     13 <div class='wrapper'>
     14 <h1>searpl</h1>
     15 
     16 <div class='box search-container'>
     17 <form action="./">
     18       <input type="text" placeholder="Search.." name="q" value="<?php if (isset($_GET['q'])) {echo htmlspecialchars($_GET['q']); } ?>">
     19       <button type="submit"><i class="icon-search"></i></button>
     20     </form>
     21 </div>
     22 
     23 <?php
     24 $db = new PDO("sqlite:db.sqlite");
     25 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
     26 
     27 if (isset($_GET['q']) && preg_replace('/\s+/', '', $_GET['q']) != '') {
     28 
     29 	$sql = 'SELECT * FROM indexed WHERE 1=1';
     30 
     31 	$terms = explode(' ', trim(preg_replace('/\s+/', ' ', $_GET['q'])));
     32 	$params = array();
     33 	foreach ($terms as $term) {
     34 		if (substr($term, 0, 1) == '-') {
     35 
     36 			$sql = $sql . ' AND content NOT LIKE ?';
     37 			array_push($params,'%'.substr($term,1).'%');
     38 		} else {
     39 
     40 			$sql = $sql . ' AND content LIKE ?';
     41 			array_push($params,'%'.$term.'%');
     42 		}
     43 	}
     44 	$sql = $sql . ';';
     45 	
     46 	$stmt = $db->prepare($sql);
     47 	$stmt->execute($params);
     48 
     49 
     50 	$rows = array();
     51 	$scores = array();
     52 	while ($row = $stmt->fetch()) {
     53 		$score = 0;
     54 		foreach ($terms as $param)
     55 			$score = $score + 100*(substr_count(strtolower($row['content']),strtolower($param)) / strlen($row['content']));
     56 			$score = $score + 5000*(substr_count(strtolower($row['url']),strtolower($param)) / strlen($row['url']));
     57 			$score = $score + 3000*(substr_count(strtolower($row['title']),strtolower($param)) / strlen($row['title']));
     58 		array_push($scores, $score);
     59 		$row['score'] = $score;
     60 		array_push($rows, $row);
     61 	}
     62 	array_multisort($scores, SORT_DESC, $rows);
     63 
     64 	$results = false;
     65 	foreach ($rows as $row) {
     66 		$results = true;
     67 ?>
     68 
     69 <div class='box'>
     70 <a href="<?php echo htmlspecialchars($row['url']); ?>"><?php echo htmlspecialchars($row['title']); ?></a>
     71 <br>
     72 <small>(score: <?php echo round($row['score']); ?>) <?php echo htmlspecialchars($row['url']); ?></small>
     73 <br>
     74 ...<?php
     75 		$content = $row['content'];
     76 		foreach ($terms as $param) {
     77 			$pos = strpos(strtolower($content), strtolower($param));
     78 			if ($pos !== false) {
     79 				echo htmlspecialchars(substr($content,$pos-50,50));
     80 				echo '<strong>'.htmlspecialchars($param).'</strong>';
     81 				echo htmlspecialchars(substr($content,$pos+strlen($param),50)).'...';
     82 			}
     83 		}
     84 
     85 ?>
     86 </div>
     87 <?php
     88 
     89 	}
     90 	if (!$results)
     91 		echo '<div class="box">No results.</div>';
     92 
     93 } else {
     94 ?>
     95 
     96 <div class='box'>
     97 <h2>welcome to searpl</h2>
     98 i am an <a href='https://github.com/xfnw/searpl'>open source</a> search
     99 engine that can find stuff :3
    100 </div>
    101 
    102 <div class='box'>
    103 normal words inputted will be tags, a -tag will blacklist the tag and
    104 there is also unsorted SQL LIKE syntax.
    105 <br>
    106 more stuff like site: coming soon!
    107 </div>
    108 
    109 <div class='box'>
    110 i have
    111 <strong>
    112 <?php
    113 echo $db->query('SELECT id FROM indexed ORDER BY id DESC LIMIT 1')->fetchColumn();
    114 ?>
    115 </strong> pages indexed, using <strong>
    116 <?php
    117 echo round(filesize('db.sqlite')/1024/1024);
    118 ?>
    119 </strong>mb of storage
    120 </div>
    121 <?php
    122 }
    123 ?>
    124 </div>
    125