You are here

robotstxt.module in RobotsTxt 5

Same filename and directory in other branches
  1. 8 robotstxt.module
  2. 6 robotstxt.module
  3. 7 robotstxt.module

File

robotstxt.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function robotstxt_help($section) {
  switch ($section) {
    case 'admin/help#robotstxt':
      return '<p>' . t('In a multisite environment, there is no mechanism for having a separate robots.txt file for each site. This module addresses that need by letting you administer the robots.txt file from the settings interface.') . '</p>';
      break;
    case 'admin/settings/robotstxt':
      if (file_exists('./robots.txt')) {
        drupal_set_message(t('One or more problems have been detected with the RobotsTxt configuration. Check the <a href="@status">status report</a> for more information.', array(
          '@status' => url('admin/logs/status'),
        )), 'warning');
      }
      return t('See <a href="http://www.robotstxt.org/">http://www.robotstxt.org/</a> for more information concerning how to write your <a href="@robotstxt">robots.txt</a> file.', array(
        '@robotstxt' => base_path() . 'robots.txt',
      ));
      break;
  }
}

/**
 * Defines the robots.txt path as a callback.
 */
function robotstxt_menu($maycache) {
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'robots.txt',
      'access' => TRUE,
      'callback' => 'robotstxt_robots',
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/robotstxt',
      'title' => t('RobotsTxt'),
      'description' => t('Manage your robots.txt file.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'robotstxt_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}

/**
 * You can edit the robots.txt for your site under admin/settings/robotstxt
 */
function robotstxt_admin_settings() {
  $form['robotstxt'] = array(
    '#type' => 'textarea',
    '#title' => t('Contents of robots.txt'),
    '#default_value' => _robotstxt_get_content(),
    '#cols' => 60,
    '#rows' => 20,
    '#required' => FALSE,
    '#wysiwyg' => FALSE,
  );
  return system_settings_form($form);
}

/**
 * Show the robots.txt file.
 */
function robotstxt_robots() {
  $content = array();
  $content[] = _robotstxt_get_content();

  // Hook other modules for adding additional lines.
  if ($additions = module_invoke_all('robotstxt')) {
    $content = array_merge($content, $additions);
  }

  // Trim any extra whitespace and filter out empty strings.
  $content = array_map('trim', $content);
  $content = array_filter($content);
  drupal_set_header('Content-type: text/plain');
  echo implode("\n", $content);
  exit;
}

/**
 * Retrieve contents of robots.txt from the database variable, site root, or
 * module directory.
 */
function _robotstxt_get_content() {
  $content = variable_get('robotstxt', FALSE);
  if ($content === FALSE) {
    $files = array(
      './robots.txt',
      drupal_get_path('module', 'robotstxt') . '/robots.txt',
    );
    foreach ($files as $file) {
      if (file_exists($file) && is_readable($file)) {
        $content = file_get_contents($file);
        break;
      }
    }
  }
  return $content;
}

Functions

Namesort descending Description
robotstxt_admin_settings You can edit the robots.txt for your site under admin/settings/robotstxt
robotstxt_help Implementation of hook_help().
robotstxt_menu Defines the robots.txt path as a callback.
robotstxt_robots Show the robots.txt file.
_robotstxt_get_content Retrieve contents of robots.txt from the database variable, site root, or module directory.