add option/ keyword to not use the default search engine
[consearch.git] / index.php
index fd5297ffdaee8d2601f1c45d60fecd39ce7efd0c..3f1966dc993291449dcfece9a5dbc18444627075 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,6 +1,5 @@
-<!DOCTYPE html>
-
-<!--
+<?php
+/*
 consearch
 
 Copyright (C) 2015 Felix Kästner, consearch @ f p u n k t k . de
@@ -10,15 +9,33 @@ This program is free software; you can redistribute it and/or modify it under th
 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/.
--->
+*/
 
-<?php
 $dse = "";
 if ( isset($_GET['dse']) and preg_match('/^[0-9a-z]{1,5}$/', $_GET['dse']) === 1 ) { // TODO: prevent injection, check whether dse is a valid searchengine
     $dse = $_GET['dse'];
 }
-?>
 
+if ( isset($_GET['get']) and $_GET['get'] === "opensearchdescription" ) {
+    echo '<?xml version="1.0" encoding="UTF-8"?>
+<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
+<ShortName>consearch';
+    if ( $dse ) { echo " ($dse)"; }
+    echo '</ShortName>
+<Description>conveniently use different searchengines</Description>
+<Tags>consearch</Tags>
+<Contact>consearch @ f p u n k t k . de</Contact>
+<Url type="text/html" template="https://www.fpunktk.de/consearch/index.php';
+    if ( $dse ) { echo "?dse=$dse"; }
+    echo '#{searchTerms}" />
+<Image height="16" width="16" type="image/png">https://www.fpunktk.de/consearch/consearch.png</Image>
+</OpenSearchDescription>';
+    
+    exit;
+}
+
+?>
+<!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
@@ -26,7 +43,7 @@ if ( isset($_GET['dse']) and preg_match('/^[0-9a-z]{1,5}$/', $_GET['dse']) === 1
 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
 <title>consearch</title>
 <link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAACMuuz63rNKAAAAK0lEQVQI12P4/58BiM4cZ9gdDUVnboNE/n8Hoc6JDLvLGLrLGF4DyWlAcQBYKBgvsgbJOAAAAABJRU5ErkJggg==" type="image/png">
-<link rel="search" type="application/opensearchdescription+xml" href="opensearchdescription-consearch.xml.php<?php if ( $dse ) { echo "?dse=$dse"; } ?>" title="consearch<?php if ( $dse ) { echo " ($dse)"; } ?>">
+<link rel="search" type="application/opensearchdescription+xml" href="index.php?get=opensearchdescription<?php if ( $dse ) { echo "&amp;dse=$dse"; } ?>" title="consearch<?php if ( $dse ) { echo " ($dse)"; } ?>">
 
 <style type="text/css">
 * {
@@ -55,7 +72,9 @@ input {
 }
 button {
     margin: 0px -5px 3mm 0px; /* trbl */
-    padding: -4px;
+    padding: 3px;
+    background: none;
+    border: 1px solid black;
     width: 13ex; /* 13ex 11ch */
     width: 11ch; /* used twice because some browsers don't understand it */
     height: 13mm;
@@ -121,8 +140,10 @@ se["yt"] = ["youtube", "https://www.youtube.com/results?search_query=", "yt.png"
 se["imdb"] = ["imdb", "http://www.imdb.com/find?s=all&q=", "imdb.png"];
 se["az"] = ["amazon", "https://www.amazon.de/s/?url=search-alias%3Daps&field-keywords=", "az.png"];
 se["php"] = ["php.net", "http://de2.php.net/manual-lookup.php?lang=de&pattern=", "php.png"];
-se["sh"] = ["slfhtml", "http://de.selfhtml.org/navigation/suche/index.htm?Suchanfrage=", "sh.png"];
+se["sh"] = ["slfhtml", "https://wiki.selfhtml.org/index.php?search=", "sh.png"];
 se["down"] = ["down", "http://www.downforeveryoneorjustme.com/", "down.png"];
+se["fefe"] = ["fefe", "https://blog.fefe.de/?q=", ""];
+se["wtr"] = ["wtr.com", "http://www.wetter.com/suche/?o=location&q=", ""];
 
 function display_searchbuttons() {
     document.getElementById("searchbuttons").innerHTML = "";
@@ -138,17 +159,22 @@ function parse_fragmentstring() {
         document.getElementById("querystring").focus();
     } else {
         fragmentarr = fragmentstring.replace("%20", "+").split("+");
-        if ( se[fragmentarr[0]] ) {
-            // first word in query is a keyword
-            kw = fragmentarr[0];
+        kw = fragmentarr[0];
+        if ( se[kw] ) {
+            // first word in query is a search engine keyword
             fragmentarr.shift();
             location.href = se[kw][1] + fragmentarr.join(" ");
         } else {
-            // there is no keyword
 <?php
 if ( $dse ) {
     // use a default searchengine
-    echo '            location.href = se["' . $dse . '"][1] + fragmentarr.join(" ");
+    echo '            if ( kw != "nd" ) {
+                location.href = se["' . $dse . '"][1] + fragmentarr.join(" ");
+            } else {
+                fragmentarr.shift();
+                document.getElementById("querystring").value = fragmentarr.join(" ");
+                display_searchbuttons();
+            }
 ';
 } else {
     // display all buttons and write the fragmentstring into the querystring input