Skip to content
Snippets Groups Projects
Forked from project / copyprevention
8 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
copyprevention.module 3.59 KiB
<?php

/**
 * @file
 * Main file for Copy Prevention module.
 */

use Drupal\Core\Template\Attribute;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function copyprevention_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.copyprevention':
      $output = '<p>' . t('This module includes several different technical ways/methods to make
copying/stealing information/images from your site harder than it usually is
Disable text selection
Disable copy to clipboard
Disable right-click context menu on all site content
Disable right-click context menu only on images (<img> tag)
Place transparent image above all your images - this will protect your real
images from being saved using context menu or drag-and-drop to desktop.
Protect/hide your images from search engine indexes so that your images don\'t
show up in image searches - add "noimageindex" robots tag and disallow image
files indexing in robots.txt') . '</p>';

      return $output;
  }
}
/**
 * Implements hook_preprocess_html().
 */
function copyprevention_preprocess_html(&$vars) {
  if (\Drupal::currentUser()->hasPermission('bypass copy prevention')) {
    return;
  }

  $body_settings = array_filter(\Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_body'));
  if (is_array($vars['attributes'])) {
    $vars['attributes'] = new Attribute();
  }
  foreach ($body_settings as $value) {
    $vars['attributes']->setAttribute('on' . $value, 'return false');
  }
}

/**
 * Implements hook_page_attachments().
 */
function copyprevention_page_attachments(&$page) {
  global $base_url;

  $copyprevention_images_search = array_filter(\Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_images_search'));
  if (array_key_exists('httpheader', $copyprevention_images_search)) {
    $page['#attached']['http_header'][] = ['X-Robots-Tag', 'noimageindex', TRUE];
  }
  if (array_key_exists('pagehead', $copyprevention_images_search)) {
    $data = [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'robots',
        'content' => 'noimageindex',
      ],
    ];
    $page['#attached']['html_head'][] = [$data, 'copyprevention_images_search'];
  }

  if (\Drupal::currentUser()->hasPermission('bypass copy prevention')) {
    return;
  }
  $path = $base_url . '/' . drupal_get_path('module', 'copyprevention');
  $settings = [
    'body' => array_filter(\Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_body')),
    'images' => array_filter(\Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_images')),
    'images_min_dimension' => \Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_images_min_dimension'),
    'transparentgif' => file_create_url($path . '/transparent.gif'),

  ];
  $page['#attached']['drupalSettings']['copyprevention'] = $settings;
  $page['#attached']['library'][] = 'copyprevention/copyprevention';
}

/**
 * Implements hook_robotstxt().
 */
function copyprevention_robotstxt() {
  $copyprevention_images_search = array_filter(\Drupal::configFactory()->getEditable('copyprevention.settings')->get('copyprevention_images_search'));

  if (array_key_exists('robotstxt', $copyprevention_images_search)) {
    return [
      '#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 [];
}