You are here

site_verify.module in Site verification 7.2

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

Site Verify provides a tool for verification codes (meta tags or custom pages) to be added to the site, for verification with external search engines.

File

site_verify.module
View source
<?php

/**
 * @file
 * Site Verify provides a tool for verification codes (meta tags or custom
 * pages) to be added to the site, for verification with external search
 * engines.
 */

/**
 * Implements hook_menu().
 */
function site_verify_menu() {

  // List all verification records.
  $items['admin/config/search/verifications'] = array(
    'title' => 'Verifications',
    'description' => 'Add, change or remove verifications for your site.',
    'page callback' => 'site_verify_list',
    'access arguments' => array(
      'administer site verify',
    ),
    'file' => 'site_verify.admin.inc',
  );

  // Add a verification record.
  $items['admin/config/search/verifications/add'] = array(
    'title' => 'Add verification',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'site_verify_edit_form',
    ),
    'access arguments' => array(
      'administer site verify',
    ),
    'file' => 'site_verify.admin.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/search/verifications/add/%site_verify_engine'] = array(
    'title' => 'Add',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'site_verify_edit_form',
      array(),
      5,
    ),
    'access arguments' => array(
      'administer site verify',
    ),
    'file' => 'site_verify.admin.inc',
  );
  $items['admin/config/search/verifications/%site_verify/edit'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'site_verify_edit_form',
      4,
    ),
    'access arguments' => array(
      'administer site verify',
    ),
    'file' => 'site_verify.admin.inc',
  );
  $items['admin/config/search/verifications/%site_verify/delete'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'site_verify_delete_form',
      4,
    ),
    'access arguments' => array(
      'administer site verify',
    ),
    'file' => 'site_verify.admin.inc',
  );

  // Add the verification paths.
  $verifications = db_query("SELECT svid, file FROM {site_verify} WHERE file <> ''")
    ->fetchAll();
  foreach ($verifications as $verification) {
    $items[$verification->file] = array(
      'page callback' => 'site_verify_output',
      'page arguments' => array(
        (string) $verification->svid,
      ),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 *
 * Implements hook_permission().
 *
*/
function site_verify_permission() {
  return array(
    'administer site verify' => array(
      'title' => t('Administer site verification meta tags'),
      'description' => t('Administer search engine site ownership verification meta tags.'),
    ),
    'administer site verify uploads' => array(
      'title' => t('Administer site verification uploads'),
      'description' => t('Administer search engine site ownership verification that requires file uploads.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_init().
 *
 * @todo Move this to hook_html_head_alter()?
 */
function site_verify_init() {

  // Add the verification meta tags to the front page.
  if (drupal_is_front_page()) {
    $meta_tags = db_query("SELECT svid, meta FROM {site_verify} WHERE meta <> ''")
      ->fetchAllKeyed();
    foreach ($meta_tags as $svid => $meta_tag) {
      $data = array(
        '#type' => 'markup',
        '#markup' => $meta_tag . "\n",
      );
      drupal_add_html_head($data, 'site_verify:' . $svid);
    }
  }
}

/**
 * Menu load callback; loads a site verification record.
 *
 * This also loads the engine details if the record was found.
 *
 * @param $svid
 *   A site verification ID.
 * @return
 *   An array of the site verification record, or FALSE if not found.
 */
function site_verify_load($svid) {
  $record = db_query("SELECT svid, engine, file, file_contents, meta FROM {site_verify} WHERE svid = :svid", array(
    ':svid' => $svid,
  ))
    ->fetchAssoc();
  if ($record) {
    $record['engine'] = site_verify_engine_load($record['engine']);
  }
  return $record;
}

/**
 * Menu load callback; loads engine details.
 *
 * @param $engine
 *   A string with the engine shortname.
 * @return
 *   An arary of the engine details, or FALSE if not found.
 */
function site_verify_engine_load($engine) {
  $engines = site_verify_get_engines();
  return isset($engines[$engine]) ? $engines[$engine] : FALSE;
}

/**
 * Implements hook_site_verify_engine_info().
 */
function site_verify_site_verify_engine_info() {
  $engines['google'] = array(
    'name' => t('Google'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'file_example' => 'google1234567890abcdef.html',
    'meta' => TRUE,
    'meta_example' => '<meta name="google-site-verification" content="NbwaW3WIDp_SPsSsfl78Ive7F34-znm9lxLJXjuWNGE" />',
  );
  $engines['yahoo'] = array(
    'name' => t('Yahoo!'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  );
  $engines['bing'] = array(
    'name' => t('Bing'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  );
  $engines['yandex'] = array(
    'name' => t('Yandex'),
    'file' => TRUE,
    'file_example' => 'yandex_b5741169901f6c20.txt',
    'meta' => TRUE,
    'meta_example' => '<meta name="yandex-verification" content="b5741169901f6c20" />',
  );
  $engines['custom'] = array(
    'name' => t('Custom verification'),
    'file' => TRUE,
    'file_contents' => TRUE,
    'meta' => TRUE,
  );
  return $engines;
}

/**
 * Fetch an array of supported search engines.
 */
function site_verify_get_engines() {
  static $engines;
  if (!isset($engines)) {

    // Fetch the list of engines and allow other modules to alter it.
    $engines = module_invoke_all('site_verify_engine_info');
    drupal_alter('site_verify_engine', $engines);

    // Merge the default values for each engine entry.
    foreach ($engines as $key => $engine) {
      $engines[$key] += array(
        'key' => $key,
        //'name' => drupal_ucfirst($engine),
        'file' => FALSE,
        'file_example' => FALSE,
        'file_validate' => array(),
        'file_contents' => FALSE,
        'file_contents_example' => FALSE,
        'file_contents_validate' => array(),
        'meta' => FALSE,
        'meta_example' => FALSE,
        'meta_validate' => array(),
      );
    }
  }
  return $engines;
}

/**
 * Output the page contents for a file-based custom verification page.
 */
function site_verify_output($svid) {
  $verification = site_verify_load($svid);
  if ($verification['file_contents'] && $verification['engine']['file_contents']) {
    echo $verification['file_contents'];
  }
  else {
    drupal_set_title(t('Verification page'));
    return t('This is a verification page for the @title search engine.', array(
      '!title' => $verification['engine']['name'],
    ));
  }
}

Functions

Namesort descending Description
site_verify_engine_load Menu load callback; loads engine details.
site_verify_get_engines Fetch an array of supported search engines.
site_verify_init Implements hook_init().
site_verify_load Menu load callback; loads a site verification record.
site_verify_menu Implements hook_menu().
site_verify_output Output the page contents for a file-based custom verification page.
site_verify_permission Implements hook_permission().
site_verify_site_verify_engine_info Implements hook_site_verify_engine_info().