<?php
/**
 * @file
 * Main file for Copy Prevention module.
 */

/**
 * Implements hook_permission().
 */
function copyprevention_permission() {
  return array(
    'bypass copy prevention' => array(
      'title' => t('Bypass Copy prevention'),
      'description' => t('Bypass copy prevention methods.')),
    'administer copy prevention' => array(
      'title' => t('Administer Copy prevention'),
      'description' => t('Administer copy prevention settings.')),
  );
}

/**
 * Implements hook_menu().
 */
function copyprevention_menu() {
  $items = array(
    'admin/config/user-interface/copyprevention' => array(
      'title' => 'Copy Prevention',
      'description' => 'Configure Copy Prevention settings.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('copyprevention_settings_form'),
      'access arguments' => array('administer copy prevention'),
      'file' => 'copyprevention.admin.inc',
      'type' => MENU_NORMAL_ITEM,
    ),
  );

  return $items;
}

/**
 * Implements hook_preprocess_html().
 */
function copyprevention_preprocess_html(&$vars) {
  if (user_access('bypass copy prevention')) {
    return;
  }

  $body_settings = array_filter(variable_get('copyprevention_body', array()));
  foreach ($body_settings as $value) {
    $vars['attributes_array']['on' . $value] = 'return false;';
  }
}

/**
 * Implements hook_page_build().
 */
function copyprevention_page_build(&$page) {
  $children = element_children($page);
  $region = array_shift($children);

  $copyprevention_images_search = array_filter(variable_get('copyprevention_images_search', array()));
  if (array_key_exists('httpheader', $copyprevention_images_search)) {
    $page[$region]['#attached']['drupal_add_http_header'][] = array('X-Robots-Tag', 'noimageindex', TRUE);
    // drupal_add_http_header('X-Robots-Tag', 'noimageindex', TRUE);
  }
  if (array_key_exists('pagehead', $copyprevention_images_search)) {
    $data = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'robots',
        'content' => 'noimageindex',
      ),
    );
    $page[$region]['#attached']['drupal_add_html_head'][] = array($data, 'copyprevention_images_search');
  }

  if (user_access('bypass copy prevention')) {
    return;
  }

  $path = drupal_get_path('module', 'copyprevention');
  $page[$region]['#attached']['js'][] = array(
    'data' => array(
      'copyprevention' => array(
        'body' => array_filter(variable_get('copyprevention_body', array())),
        'images' => array_filter(variable_get('copyprevention_images', array())),
        'images_min_dimension' => variable_get('copyprevention_images_min_dimension', 150),
        'transparentgif' => file_create_url($path . '/transparent.gif'),
      ),
    ),
    'type' => 'setting',
  );

  $page[$region]['#attached']['js'][$path . '/copyprevention.js'] = array('scope' => 'footer');
}

/**
 * Implements hook_robotstxt().
 */
function copyprevention_robotstxt() {
  $copyprevention_images_search = array_filter(variable_get('copyprevention_images_search', array()));

  if (array_key_exists('robotstxt', $copyprevention_images_search)) {
    return array(
      '#Copy Prevention: protect/hide images from search engines indexing',
      'Disallow: *.jpg',
      'Disallow: *.JPG',
      'Disallow: *.jpeg',
      'Disallow: *.JPEG',
      'Disallow: *.png',
      'Disallow: *.PNG',
      'Disallow: *.gif',
      'Disallow: *.GIF',
    );
  }

  return array();
}
