LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

tools.php (4595B)

      1 <?php
      2     function get_base_url($url) {
      3         $parsed = parse_url($url);
      4         return $parsed["scheme"] . "://" . $parsed["host"] . "/";
      5     }
      6 
      7     function get_root_domain($url) {
      8         return parse_url($url, PHP_URL_HOST);
      9     }
     10 
     11     function try_replace_with_frontend($url, $frontend, $original, $opts) {
     12         $frontends = $opts->frontends;
     13 
     14         if (array_key_exists($frontend, $opts->frontends)) {
     15             $frontend = $frontends[$frontend]["instance_url"];
     16 
     17             if (empty(trim($frontend)))
     18                 return $url;
     19 
     20             if (strpos($url, "wikipedia.org") !== false) {
     21                 $wiki_split = explode(".", $url);
     22                 if (count($wiki_split) > 1) {
     23                     $lang = explode("://", $wiki_split[0])[1];
     24                     $url =  $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?")  . "lang=" . $lang;
     25                 }
     26             } else if (strpos($url, "fandom.com") !== false) {
     27                 $fandom_split = explode(".", $url);
     28                 if (count($fandom_split) > 1) {
     29                     $wiki_name = explode("://", $fandom_split[0])[1];
     30                     $url =  $frontend . "/" . $wiki_name . explode($original, $url)[1];
     31                 }
     32             } else if (strpos($url, "gist.github.com") !== false) {
     33                 $gist_path = explode("gist.github.com", $url)[1];
     34                 $url = $frontend . "/gist" . $gist_path;
     35             } else if (strpos($url, "stackexchange.com") !== false) {
     36                 $se_domain = explode(".", explode("://", $url)[1])[0];
     37                 $se_path = explode("stackexchange.com", $url)[1];
     38                 $url = $frontend . "/exchange" . "/" . $se_domain . $se_path;
     39             } else {
     40                 $url =  $frontend . explode($original, $url)[1];
     41             }
     42 
     43 
     44             return $url;
     45         }
     46 
     47         return $url;
     48     }
     49 
     50     function check_for_privacy_frontend($url, $opts) {
     51         if ($opts->disable_frontends)
     52             return $url;
     53 
     54         foreach($opts->frontends as $frontend => $data) {
     55             $original = $data["original_url"];
     56 
     57             if (strpos($url, $original)) {
     58                 $url = try_replace_with_frontend($url, $frontend, $original, $opts);
     59                 break;
     60             } else if (strpos($url, "stackexchange.com")) {
     61                 $url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com", $opts);
     62                 break;
     63             }
     64         }
     65 
     66         return $url;
     67     }
     68 
     69     function get_xpath($response) {
     70         if (!$response)
     71             return null;
     72 
     73         $htmlDom = new DOMDocument;
     74         @$htmlDom->loadHTML($response);
     75         $xpath = new DOMXPath($htmlDom);
     76 
     77         return $xpath;
     78     }
     79 
     80     function request($url, $conf) {
     81         $ch = curl_init($url);
     82         curl_setopt_array($ch, $conf);
     83         $response = curl_exec($ch);
     84 
     85         return $response;
     86     }
     87 
     88     function human_filesize($bytes, $dec = 2) {
     89         $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
     90         $factor = floor((strlen($bytes) - 1) / 3);
     91 
     92         return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
     93     }
     94 
     95     function remove_special($string) {
     96         $string = preg_replace("/[\r\n]+/", "\n", $string);
     97         return trim(preg_replace("/\s+/", ' ', $string));
     98      }
     99 
    100     function print_elapsed_time($start_time) {
    101             $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
    102             echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
    103         }
    104 
    105     function print_next_page_button($text, $page, $query, $type) {
    106         echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
    107         echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
    108         echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
    109         echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
    110         echo "<button type=\"submit\">$text</button>";
    111         echo "</form>";
    112     }
    113 
    114     function copy_cookies($curl) {
    115         if (array_key_exists("HTTP_COOKIE", $_SERVER))
    116             curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
    117     }
    118 
    119     
    120     function get_country_emote($code) {
    121         $emoji = [];
    122         foreach(str_split($code) as $c) {
    123             if(($o = ord($c)) > 64 && $o % 32 < 27) {
    124                 $emoji[] = hex2bin("f09f87" . dechex($o % 32 + 165));
    125                 continue;
    126             }
    127             
    128             $emoji[] = $c;
    129         }
    130 
    131         return join($emoji);
    132     }
    133 
    134 ?>