You are here

public function Search404Controller::search404SearchEngineQuery in Search 404 8

Same name and namespace in other branches
  1. 2.x src/Controller/Search404Controller.php \Drupal\search404\Controller\Search404Controller::search404SearchEngineQuery()

Detect search from search engine.

1 call to Search404Controller::search404SearchEngineQuery()
Search404Controller::search404GetKeys in src/Controller/Search404Controller.php
Function for searchkeys.

File

src/Controller/Search404Controller.php, line 305

Class

Search404Controller
Route controller for search.

Namespace

Drupal\search404\Controller

Code

public function search404SearchEngineQuery() {
  $engines = [
    'altavista' => 'q',
    'aol' => 'query',
    'google' => 'q',
    'bing' => 'q',
    'lycos' => 'query',
    'yahoo' => 'p',
  ];
  $parsed_url = !empty($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER']) : FALSE;
  $remote_host = !empty($parsed_url['host']) ? $parsed_url['host'] : '';
  $query_string = !empty($parsed_url['query']) ? $parsed_url['query'] : '';
  parse_str($query_string, $query);
  if (!$parsed_url === FALSE && !empty($remote_host) && !empty($query_string) && count($query)) {
    foreach ($engines as $host => $key) {
      if (strpos($remote_host, $host) !== FALSE && array_key_exists($key, $query)) {
        return trim($query[$key]);
      }
    }
  }
  return '';
}