You are here

ais.install in Adaptive Image Styles (ais) 7

Install file for the threshold-ais module

File

ais.install
View source
<?php

/**
 * @file
 * Install file for the threshold-ais module
 *
 */

/**
 * Implements hook_install().
 */
function ais_install() {
  $defaults = array(
    array(
      'name' => 'threshold-480',
      'size' => 480,
    ),
    array(
      'name' => 'threshold-768',
      'size' => 768,
    ),
    array(
      'name' => 'threshold-992',
      'size' => 992,
    ),
    array(
      'name' => 'threshold-1382',
      'size' => 1382,
    ),
  );
  variable_set("ais_adaptive_styles", $defaults);
}

/**
 * Implements hook_uninstall().
 */
function ais_unistall() {
  variable_del("ais_adaptive_styles");
}

/**
 * Force a rebuild of the menu router
 */
function ais_update_7000() {
  menu_rebuild();
}

/**
 * Implements hook_requirements().
 */
function ais_requirements($phase) {
  $t = get_t();
  $requirements = array();
  $okrequirements['ais'] = array(
    'title' => $t("Adaptive Image Styles"),
    'description' => "AIS appears to be configured correctly",
    'severity' => REQUIREMENT_OK,
    'value' => array(),
  );
  if ($phase == 'runtime') {
    if (!preg_match("/apache/i", php_sapi_name()) and !preg_match("/apache/i", $_SERVER["SERVER_SOFTWARE"])) {
      $requirements['apache'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("It doesn't look like you're running Apache.  Please verify that the web server settings required are in place for AIS to work."),
        'severity' => REQUIREMENT_WARNING,
        'value' => array(),
      );
      return $requirements;
    }
    $okrequirements['ais']['value'][] = "Apache Detected";
    if (!file_exists(".htaccess")) {
      $requirements['htaccess'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("The htaccess file could not be checked, please verify that AIS is working correctly."),
        'severity' => REQUIREMENT_WARNING,
        'value' => array(),
      );
      return $requirements;
    }
    $okrequirements['ais']['value'][] = ".htaccess file found";
    $htaccess = array();
    $rawhtaccess = file(".htaccess");
    foreach ($rawhtaccess as $ht) {
      $comment = strpos($ht, '#');
      if ($comment !== FALSE) {
        $ht = substr($ht, 0, $comment);
      }
      $ht = trim($ht);
      if (!empty($ht)) {
        $htaccess[] = $ht;
      }
    }
    $htaccess = implode("\n", $htaccess);
    $after = array(
      "RewriteCond %{REQUEST_FILENAME} !-f",
      "RewriteCond %{REQUEST_FILENAME} !-d",
      "RewriteCond %{REQUEST_URI} !=/favicon.ico",
      "RewriteRule ^ index.php [L]",
    );
    $after_loc = strpos($htaccess, implode("\n", $after));
    if ($after_loc === FALSE) {
      $requirements['parsing'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("There was a problem finding standard Drupal rules in the htaccess file, please verify that AIS is working correctly."),
        'severity' => REQUIREMENT_WARNING,
        'value' => array(),
      );
      return $requirements;
    }
    $okrequirements['ais']['value'][] = "Drupal Clean URL Rewrite found";
    $lastrule = 0;
    $rewritebase = "/RewriteBase\\s+(\\S+)/";
    $matches = array();
    preg_match_all($rewritebase, $htaccess, $matches);
    if (sizeof($matches[1]) > 1) {
      $requirements['multiplerewritebase'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("Found multiple RewriteBases configured in the .htaccess file, please correct this."),
        'severity' => REQUIREMENT_ERROR,
        'value' => array(),
      );
    }
    elseif (sizeof($matches[1]) != 1 or !isset($matches[1][0])) {
      $requirements['norewritebase'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("RewriteBase not configured in .htaccess file, AIS requires that the rewritebase be configured."),
        'severity' => REQUIREMENT_ERROR,
        'value' => array(),
      );
    }
    $okrequirements['ais']['value'][] = "Found RewriteBase rule";
    $rewritebase = !empty($matches[1][0]) ? $matches[1][0] : NULL;
    $script = dirname($_SERVER['SCRIPT_NAME']);
    if ($rewritebase != $script) {

      // Rewrite base appears to be misconfigured
      $requirements['badrewritebase'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("RewriteBase appears to be configured incorrectly in the .htaccess file.  The RewriteBase is set to '%bad', but should be '%good'.", array(
          '%bad' => $rewritebase,
          '%good' => $script,
        )),
        'severity' => REQUIREMENT_ERROR,
        'value' => array(),
      );
    }
    if (!empty($matches[1][0])) {
      $okrequirements['ais']['value'][] = "RewriteBase correctly configured: " . $matches[1][0];
    }
    $oldrules = array(
      "RewriteCond %{REQUEST_URI} ^(.+)/files/styles/adaptive/(.+)\$",
      "RewriteCond %{REQUEST_URI} !/modules/image/sample.png",
      "RewriteCond %{HTTP_COOKIE} ais=([a-z0-9-_]+)",
      "RewriteRule ^(.+)/files/styles/adaptive/(.+)\$ \$1/files/styles/%1/\$2 [R=302,L]",
    );
    $rules = array(
      "RewriteCond %{REQUEST_URI} ^(.+)/files/styles/adaptive/(.+)\$",
      "RewriteCond %{REQUEST_URI} !/modules/image/sample.png",
      "RewriteCond %{HTTP_COOKIE} ais=([a-z0-9_-]+)",
      "RewriteRule ^(.+)/files/styles/adaptive/(.+)\$ \$1/files/styles/%1/\$2 [R=302,L]",
    );
    $old_rules_loc = strpos($htaccess, implode("\n", $oldrules));
    $rules_loc = strpos($htaccess, implode("\n", $rules));
    if ($rules_loc === FALSE and $old_rules_loc === FALSE) {
      $requirements['patched'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("Couldn't find the AIS htaccess rules in the htaccess file.  Without these rules, AIS will not work."),
        'severity' => REQUIREMENT_ERROR,
        'value' => array(),
      );
      return $requirements;
    }
    $okrequirements['ais']['value'][] = "AIS rules found";
    if ($rules_loc > $after_loc) {
      $requirements['wrongspot'] = array(
        'title' => $t("Adaptive Image Styles"),
        'description' => $t("The rules added for AIS to work need to come before Drupals index.php redirect rules."),
        'severity' => REQUIREMENT_ERROR,
        'value' => array(),
      );
      return $requirements;
    }
    $okrequirements['ais']['value'][] = "AIS rules found before Clean URLs rules";
    $okrequirements['ais']['value'] = implode("<br />", $okrequirements['ais']['value']);
    return $okrequirements;
  }
  return array();
}

Functions

Namesort descending Description
ais_install Implements hook_install().
ais_requirements Implements hook_requirements().
ais_unistall Implements hook_uninstall().
ais_update_7000 Force a rebuild of the menu router