192 lines
7.1 KiB
PHP
192 lines
7.1 KiB
PHP
<?php
|
||
require_once('vendor/autoload.php');
|
||
|
||
$show_results = FALSE;
|
||
$results_html = "";
|
||
$final_result_html = "<hr>";
|
||
|
||
if (isset($_GET['q'])) { // if there's a search query, show the results for it
|
||
$query = urlencode($_GET["q"]);
|
||
$show_results = TRUE;
|
||
$search_url = "https://html.duckduckgo.com/html?q=" . $query;
|
||
|
||
// Configuration de la pagination
|
||
$results_per_page = 5; // Nombre de résultats par page
|
||
$current_page = isset($_GET['p']) ? intval($_GET['p']) : 1;
|
||
if ($current_page < 1) $current_page = 1;
|
||
|
||
$context = stream_context_create([
|
||
'http' => [
|
||
'header' => "Cache-Control: no-cache\r\n" .
|
||
"Pragma: no-cache\r\n" .
|
||
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 2_0 like Mac OS X) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20\r\n"
|
||
]
|
||
]);
|
||
if (!$results_html = file_get_contents($search_url, false, $context)) {
|
||
$error_text .= "Failed to get results, sorry :( <br>";
|
||
}
|
||
$simple_results = $results_html;
|
||
$simple_results = str_replace('strong>', 'b>', $simple_results); //change <strong> to <b>
|
||
$simple_results = str_replace('em>', 'i>', $simple_results); //change <em> to <i>
|
||
$simple_results = clean_str($simple_results);
|
||
|
||
$result_blocks = explode('<h2 class="result__title">', $simple_results);
|
||
$total_results = count($result_blocks) - 1;
|
||
|
||
// Calcul des paramètres de pagination
|
||
$total_pages = ceil($total_results / $results_per_page);
|
||
if ($total_pages == 0) $total_pages = 1; // Éviter la division par zéro
|
||
|
||
$start_result = ($current_page - 1) * $results_per_page + 1;
|
||
$end_result = min($start_result + $results_per_page - 1, $total_results);
|
||
|
||
// Affichage des résultats pour la page actuelle seulement
|
||
for ($x = $start_result; $x <= $end_result; $x++) {
|
||
if ($x <= $total_results && strpos($result_blocks[$x], '<a class="badge--ad">') === false) { //only return non ads
|
||
// result link, redirected through our proxy
|
||
$result_link = explode('class="result__a" href="', $result_blocks[$x])[1];
|
||
$result_topline = explode('">', $result_link);
|
||
$result_link = str_replace('//duckduckgo.com/l/?uddg=', '/read.php?a=', $result_topline[0]);
|
||
// result title
|
||
$result_title = str_replace("</a>", "", explode("\n", $result_topline[1]));
|
||
// result display url
|
||
$result_display_url = explode('class="result__url"', $result_blocks[$x])[1];
|
||
$result_display_url = trim(explode("\n", $result_display_url)[1]);
|
||
// result snippet
|
||
$result_snippet = explode('class="result__snippet"', $result_blocks[$x])[1];
|
||
$result_snippet = explode('">', $result_snippet)[1];
|
||
$result_snippet = explode('</a>', $result_snippet)[0];
|
||
|
||
$final_result_html .= "<br><a href='" . $result_link . "'><font size='4'><b>" . $result_title[0] . "</b></font><br><font color='#008000' size='2'>"
|
||
. $result_display_url . "</font></a><br>" . $result_snippet . "<br><br><hr>";
|
||
}
|
||
}
|
||
|
||
// Ajout des liens de pagination
|
||
$final_result_html .= "<br><center>";
|
||
if ($current_page > 1) {
|
||
$final_result_html .= "<a href='/?q=" . $query . "&p=" . ($current_page - 1) . "'>< Previous</a> ";
|
||
}
|
||
$final_result_html .= "Page " . $current_page . " of " . $total_pages;
|
||
if ($current_page < $total_pages) {
|
||
$final_result_html .= " <a href='/?q=" . $query . "&p=" . ($current_page + 1) . "'>Next ></a>";
|
||
}
|
||
$final_result_html .= "</center>";
|
||
}
|
||
|
||
//replace chars that old machines probably can't handle
|
||
function clean_str($str)
|
||
{
|
||
$str = str_replace("‘", "'", $str);
|
||
$str = str_replace("’", "'", $str);
|
||
$str = str_replace("“", '"', $str);
|
||
$str = str_replace("”", '"', $str);
|
||
$str = str_replace("–", '-', $str);
|
||
$str = str_replace("'", "'", $str);
|
||
|
||
return $str;
|
||
}
|
||
|
||
?>
|
||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 2.0//EN">
|
||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
||
<html>
|
||
|
||
<head>
|
||
<title>AlpineFind!</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
|
||
<style type="text/css">
|
||
a {
|
||
padding: 4px 0;
|
||
display: inline-block;
|
||
}
|
||
|
||
input[type="text"] {
|
||
font-size: 16px;
|
||
height: 24px;
|
||
margin-bottom: 8px;
|
||
width: 80%;
|
||
max-width: 280px;
|
||
}
|
||
|
||
input[type="submit"] {
|
||
font-size: 16px;
|
||
height: 32px;
|
||
padding: 2px 12px;
|
||
margin-top: 5px;
|
||
margin-bottom: 5px;
|
||
display: block;
|
||
}
|
||
|
||
.search-form {
|
||
margin: 10px 0;
|
||
}
|
||
</style>
|
||
<script type="text/javascript">
|
||
function showLoading() {
|
||
document.getElementById('loading').style.display = 'inline';
|
||
return true;
|
||
}
|
||
</script>
|
||
</head>
|
||
|
||
<body style="margin: 5px; padding: 0;">
|
||
|
||
<?php if ($show_results) { // there's a search query in q, so show search results
|
||
?>
|
||
|
||
<form action="/" method="get" onsubmit="return showLoading();" class="search-form">
|
||
<a href="/">
|
||
<font size=6 color="#008000">Alpine</font>
|
||
<font size=6 color="#000000">Find!</font>
|
||
</a>
|
||
<div style="margin: 8px 0;">
|
||
<p>Leap again:</p>
|
||
<input type="text" size="30" name="q" value="<?php echo urldecode($query) ?>">
|
||
<?php if (isset($_GET['p'])) { ?>
|
||
<input type="hidden" name="p" value="1">
|
||
<?php } ?>
|
||
</div>
|
||
<span id="loading" style="display: none;">Loading...</span>
|
||
<input type="submit" value="Ribbbit!">
|
||
</form>
|
||
<hr>
|
||
<br>
|
||
<center>Search Results for <b><?php echo strip_tags(urldecode($query)) ?></b></center>
|
||
<br>
|
||
<?php echo $final_result_html ?>
|
||
|
||
<?php } else { // no search query, so show new search
|
||
?>
|
||
<br><br>
|
||
<center>
|
||
<h1>
|
||
<font size=7>
|
||
<font color="#008000">Alpine</font>Find!
|
||
</font>
|
||
</h1>
|
||
</center>
|
||
<center>
|
||
<h3>The Search Engine for Vintage Computers</h3>
|
||
</center>
|
||
<br><br>
|
||
<center>
|
||
<form action="/" method="get" onsubmit="return showLoading();">
|
||
<p>Leap to:</p>
|
||
<input type="text" size="30" name="q"><br>
|
||
<span id="loading" style="display: none;">Loading...</span>
|
||
<input type="submit" value="Ribbbit!" style="margin: 10px auto;">
|
||
</form>
|
||
</center>
|
||
<br><br><br>
|
||
<small>
|
||
<center>Built by <b><a href="https://youtube.com/ActionRetro">Action Retro</a></b> on YouTube | <a href="about.php">Why build such a thing?</a></center><br>
|
||
<small>
|
||
<center>Powered by DuckDuckGo</center>
|
||
</small>
|
||
</form>
|
||
</form>
|
||
<?php } ?>
|
||
</body>
|
||
</html>
|