You are here

function smart_ip_requirements in Smart IP 7.2

Same name and namespace in other branches
  1. 6.2 smart_ip.install \smart_ip_requirements()
  2. 6 smart_ip.install \smart_ip_requirements()
  3. 7 smart_ip.install \smart_ip_requirements()

Implements hook_requirements().

See also

smart_ip_admin_settings()

File

./smart_ip.install, line 14
Installation callback for Smart IP.

Code

function smart_ip_requirements($phase = 'runtime') {
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();

  // Test PHP version
  $requirements['smart_ip_php'] = array(
    'title' => $t('PHP'),
    'value' => $phase == 'install' ? phpversion() : l(phpversion(), 'admin/reports/status/php'),
  );
  if (version_compare(phpversion(), SMART_IP_MINIMUM_PHP) < 0) {
    $requirements['smart_ip_php']['description'] = $t('Your PHP installation is too old. Smart IP requires at least PHP %version.', array(
      '%version' => SMART_IP_MINIMUM_PHP,
    ));
    $requirements['smart_ip_php']['severity'] = REQUIREMENT_ERROR;
  }
  if ($phase == 'runtime') {
    $correct_key = variable_get('smart_ip_correct_ipinfodb_key', FALSE);
    if (!$correct_key) {
      $smart_ip_source = variable_get('smart_ip_source', 'ipinfodb_service');
      if ($smart_ip_source == 'ipinfodb_service') {

        // Generate an appropriate error message:
        // Missing API keys.
        $ipinfodb_key = variable_get('smart_ip_ipinfodb_key', '');
        if (empty($ipinfodb_key)) {
          $requirements['smart_ip'] = array(
            'title' => 'IPInfoDB API key',
            'severity' => REQUIREMENT_ERROR,
            'value' => t('Not configured'),
            'description' => t('The IPInfoDB API key is not configured yet. Visit Smart IP settings !page.', array(
              '!page' => l(t('page'), 'admin/config/people/smart_ip'),
            )),
          );
        }
        else {
          $response = drupal_http_request(smart_ip_get_ipinfodb_url($ipinfodb_key, '127.0.0.1'));
          if (isset($response->data)) {
            $stat = drupal_json_decode($response->data);
            if (isset($stat['Status']) && ($stat['Status'] == 'MISSING API KEY' || $stat['Status'] == 'INVALID API KEY') || isset($stat['statusCode']) && $stat['statusCode'] == 'ERROR') {
              $requirements['smart_ip'] = array(
                'title' => 'IPInfoDB API key',
                'severity' => REQUIREMENT_ERROR,
                'value' => t('Invalid'),
                'description' => t('The IPInfoDB API key is invalid. Visit Smart IP settings !page.', array(
                  '!page' => l(t('page'), 'admin/config/people/smart_ip'),
                )),
              );
            }
            else {
              variable_set('smart_ip_correct_ipinfodb_key', TRUE);
            }
          }
        }
      }
    }
  }
  return $requirements;
}