Skip to content
Snippets Groups Projects
robotstxt.install 2.81 KiB
Newer Older
<?php
// $Id: robotstxt.install,v 1.13.2.2 2011/01/05 23:24:10 hass Exp $

/**
 * Implements hook_install().
 */
function robotstxt_install() {
  if (file_exists(DRUPAL_ROOT . '/robots.txt')) {
    variable_set('robotstxt', file_get_contents(DRUPAL_ROOT . '/robots.txt'));
  }
  elseif (file_exists(drupal_get_path('module', 'robotstxt') . '/robots.txt')) {
    variable_set('robotstxt', file_get_contents(drupal_get_path('module', 'robotstxt') . '/robots.txt'));
  }
}

/**
 * Implements hook_uninstall().
 */
function robotstxt_uninstall() {
  variable_del('robotstxt');
}

/**
 * Implements hook_requirements().
 */
function robotstxt_requirements($phase) {
  $requirements = array();
  $t = get_t();

  switch ($phase) {
    case 'runtime' :
      // Module cannot work without Clean URLs.
      if (!variable_get('clean_url', 0)) {
        $requirements['robotstxt_cleanurl'] = array(
          'title' => $t('RobotsTxt'),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('<a href="!clean_url">Clean URLs</a> are mandatory for this module.', array('!clean_url' => url('admin/config/search/clean-urls'))),
        );
      }

      // Webservers prefer the robots.txt file on disk and does not allow menu path overwrite.
      if (file_exists(DRUPAL_ROOT . '/robots.txt')) {
        $requirements['robotstxt_file'] = array(
          'title' => $t('RobotsTxt'),
          'severity' => REQUIREMENT_WARNING,
          'value' => $t('RobotsTxt module works only if you remove the existing robots.txt file in your website root.'),
        );
      }
  }
  return $requirements;
}

/**
 * Automatically add the 'administer robots.txt' permission to granted users.
 */
function robotstxt_update_6100() {
  $roles = user_roles(FALSE, 'administer site configuration');
  foreach ($roles as $rid => $role) {
    _update_7000_user_role_grant_permissions($rid, array('administer robots.txt'), 'robotstxt');
  }
  return t("Added 'administer robots.txt' permission to all roles with 'administer site configuration' permission.");
}

/**
 * #337820: Rename menu path 'logout' to 'user/logout' for consistency.
 */
function robotstxt_update_7000() {
  $robotstxt = variable_get('robotstxt');
  $robotstxt = str_replace('Disallow: /logout/', 'Disallow: /user/logout/', $robotstxt);
  $robotstxt = str_replace('Disallow: /?q=logout/', 'Disallow: /?q=user/logout/', $robotstxt);
  variable_set('robotstxt', $robotstxt);

  return t("Renamed menu path 'logout' to 'user/logout'.");
}

/**
 * #494462: Allow crawling of sites/default/files by search engines, don't disallow it in robots.txt.
 */
function robotstxt_update_7100() {
  $robotstxt = variable_get('robotstxt');
  $robotstxt = preg_replace("/Disallow:\s\/sites\/(\r\n?|\n)/", '', $robotstxt);
  variable_set('robotstxt', $robotstxt);

  return t("Allowed crawling of sites/default/files by search engines.");
}