You are here

function shurly_gsb in ShURLy 8

Same name and namespace in other branches
  1. 7 shurly.module \shurly_gsb()

Check the URL against Google Safe browser for phishing, malware of both.

Parameters

string $url: URL to check.

Return value

bool TRUE if the given URL matches; otherwise FALSE.

1 call to shurly_gsb()
ShurlyCreateForm::validateForm in src/Form/ShurlyCreateForm.php
Form validation handler.
2 string references to 'shurly_gsb'
ShurlyCreateForm::validateForm in src/Form/ShurlyCreateForm.php
Form validation handler.
ShurlySettingsForm::buildForm in src/Form/ShurlySettingsForm.php
Form constructor.

File

./shurly.module, line 766
Description http://www.youtube.com/watch?v=Qo7qoonzTCE.

Code

function shurly_gsb($url) {
  $client = \Drupal::config('shurly.settings')
    ->get('shurly_gsb_config_client');
  $api_key = \Drupal::config('shurly.settings')
    ->get('shurly_gsb_apikey');
  $gsb_url = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" . $api_key;
  $rawdata = [
    'client' => [
      "clientId" => "lccx",
      "clientVersion" => "1.5.2",
    ],
    'threatInfo' => [
      "threatTypes" => [
        "MALWARE",
        "SOCIAL_ENGINEERING",
        "UNWANTED_SOFTWARE",
        "POTENTIALLY_HARMFUL_APPLICATION",
      ],
      "platformTypes" => [
        "ALL_PLATFORMS",
      ],
      "threatEntryTypes" => [
        "URL",
      ],
      "threatEntries" => [
        [
          "url" => $url,
        ],
      ],
    ],
  ];
  $data = json_encode($rawdata);
  $client = \Drupal::httpClient();
  $request = $client
    ->createRequest('POST', $gsb_url, $data);
  $request
    ->addHeader('Content-Type', 'application/json');
  try {
    $response = $client
      ->get($gsb_url, [
      'headers' => [
        'Content-Type',
        'application/json',
      ],
    ]);
    $data = json_decode($response
      ->getBody());
    if ($data->matches) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  } catch (RequestException $e) {
    watchdog_exception('shurly', $e);
  }
}