Heimdall/app/Search.php

147 lines
4.5 KiB
PHP
Raw Normal View History

2019-01-16 14:47:32 +00:00
<?php namespace App;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Item;
2019-01-18 15:21:50 +00:00
use App\Setting;
use Form;
use Cache;
2019-01-16 14:47:32 +00:00
abstract class Search
{
2019-01-18 18:21:44 +00:00
/**
* List of all search providers
*
* @return Array
*/
2019-01-16 14:47:32 +00:00
public static function providers()
{
$providers = self::standardProviders();
2019-01-18 15:21:50 +00:00
$providers = $providers + self::appProviders();
2019-01-16 14:47:32 +00:00
return $providers;
}
2019-01-18 18:21:44 +00:00
/**
* Gets details for a single provider
*
* @return Object
*/
2019-01-16 14:47:32 +00:00
public static function providerDetails($provider)
{
$providers = self::providers();
2019-01-18 18:21:44 +00:00
if(!isset($providers[$provider])) return false;
2019-01-16 14:47:32 +00:00
return (object)$providers[$provider] ?? false;
}
2019-01-18 18:21:44 +00:00
/**
* Array of the standard providers
*
* @return Array
*/
2019-01-16 14:47:32 +00:00
public static function standardProviders()
{
return [
'google' => [
'url' => 'https://www.google.com/search',
'var' => 'q',
'method' => 'get',
2019-01-18 18:21:44 +00:00
'type' => 'standard',
2019-01-16 14:47:32 +00:00
],
'ddg' => [
'url' => 'https://duckduckgo.com/',
'var' => 'q',
'method' => 'get',
2019-01-18 18:21:44 +00:00
'type' => 'standard',
2019-01-16 14:47:32 +00:00
],
'bing' => [
'url' => 'https://www.bing.com/search',
'var' => 'q',
'method' => 'get',
2019-01-18 18:21:44 +00:00
'type' => 'standard',
2019-01-16 14:47:32 +00:00
],
2019-06-08 11:02:12 +00:00
'qwant' => [
'url' => 'https://www.qwant.com/',
'var' => 'q',
'method' => 'get',
'type' => 'standard',
],
2019-06-11 11:02:18 +00:00
'startpage' => [
'url' => 'https://www.startpage.com/do/dsearch',
'var' => 'query',
'method' => 'get',
'type' => 'standard',
],
2019-01-16 14:47:32 +00:00
];
}
2019-01-18 18:21:44 +00:00
/**
* Loops through users apps to see if app is a search provider, might be worth
* looking into caching this at some point
*
* @return Array
*/
2019-01-18 15:21:50 +00:00
public static function appProviders()
{
$providers = [];
$userapps = Item::all();
foreach($userapps as $app) {
if(empty($app->class)) continue;
if(($provider = Item::isSearchProvider($app->class)) !== false) {
$name = Item::nameFromClass($app->class);
$providers[strtolower($name)] = [
'type' => $provider->type,
2019-01-18 18:21:44 +00:00
'class' => $app->class,
'url' => $app->url,
2019-01-18 15:21:50 +00:00
];
}
}
return $providers;
}
2019-01-18 18:21:44 +00:00
/**
* Outputs the search form
*
2019-01-18 15:21:50 +00:00
* @return html
*/
public static function form()
{
$output = '';
$homepage_search = Setting::fetch('homepage_search');
$search_provider = Setting::where('key', '=', 'search_provider')->first();
$user_search_provider = Setting::fetch('search_provider');
//die(print_r($search_provider));
//die(var_dump($user_search_provider));
// return early if search isn't applicable
if((bool)$homepage_search !== true) return $output;
2019-01-18 18:21:44 +00:00
$user_search_provider = $user_search_provider ?? 'none';
2019-01-18 15:21:50 +00:00
if((bool)$homepage_search && (bool)$search_provider) {
if((bool)$user_search_provider) {
$name = 'app.options.'.$user_search_provider;
$provider = self::providerDetails($user_search_provider);
$output .= '<div class="searchform">';
$output .= '<form action="'.url('search').'"'.getLinkTargetAttribute().' method="get">';
2019-01-18 15:21:50 +00:00
$output .= '<div id="search-container" class="input-container">';
$output .= '<select name="provider">';
foreach(self::providers() as $key => $searchprovider) {
$selected = ($key === $user_search_provider) ? ' selected="selected"' : '';
$output .= '<option value="'.$key.'"'.$selected.'>'.__('app.options.'.$key).'</option>';
}
$output .= '</select>';
$output .= Form::text('q', null, ['class' => 'homesearch', 'autofocus' => 'autofocus', 'placeholder' => __('app.settings.search').'...']);
$output .= '<button type="submit">'.ucwords(__('app.settings.search')).'</button>';
$output .= '</div>';
$output .= '</form>';
2019-01-18 15:21:50 +00:00
$output .= '</div>';
}
}
return $output;
}
2019-01-16 14:47:32 +00:00
}