supercookie-8.x-1.x-dev/supercookie.install

supercookie.install
<?php
/**
 * @file
 * supercookie.install
 */

use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

/**
 * Checks folder/file exists.
 */
function _supercookie_library_exists($folder = FALSE, $filename = FALSE) {
  $exists = FALSE;
  $path = realpath(DRUPAL_ROOT . '/libraries/' . $folder);

  if ($path !== FALSE && is_dir($path)) {
    $path .= '/' . $filename;
    $exists = file_exists($path);
  }

  return $exists;
}

/**
 * Implements hook_requirements().
 */
function supercookie_requirements($phase) {

  $config = \Drupal::config('supercookie.settings');

  $requirements = array(
    'supercookie' => array(
      'severity' => REQUIREMENT_OK,
      'title' => t('Supercookie'),
      'description' => [],
    ),
  );

  if (!_supercookie_library_exists('JSON.prune', 'JSON.prune.js')) {
    $requirements['supercookie']['severity'] = REQUIREMENT_ERROR;
    $requirements['supercookie']['value'] = t('Not configured');

    if (PHP_SAPI === 'cli') {
      $requirements['supercookie']['description'][] = t('@introJSON.prune library is not installed correctly. Please download https://rawgit.com/Canop/JSON.prune/master/JSON.prune.js and verify that /libraries/JSON.prune/JSON.prune.js exists.', array(
        '@intro' => ($phase == 'install' ? 'Supercookie requirements: ' : ''),
      ));
    }
    else {
      $requirements['supercookie']['description'][] = t('@intro<code>JSON.prune</code> library is not installed correctly. Please <a href=":href">download</a> and verify that <code>/libraries/JSON.prune/JSON.prune.js</code> exists.', array(
        '@intro' => ($phase == 'install' ? 'Supercookie requirements: ' : ''),
        ':href' => Url::fromUri('https://rawgit.com/Canop/JSON.prune/master/JSON.prune.js')->toString(),
      ));
    }
  }
  if (!_supercookie_library_exists('CryptoJS/rollups', 'md5.js')) {
    $requirements['supercookie']['severity'] = REQUIREMENT_ERROR;
    $requirements['supercookie']['value'] = t('Not configured');

    if (PHP_SAPI === 'cli') {
      $requirements['supercookie']['description'][] = t('@intromd5.js library is not installed correctly. Please download https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/crypto-js/CryptoJS v3.1.2.zip and verify that /libraries/CryptoJS/rollups/md5.js exists.', array(
        '@intro' => ($phase == 'install' ? 'Supercookie requirements: ' : ''),
      ));
    }
    else {
      $requirements['supercookie']['description'][] = t('@intro<code>md5.js</code> library is not installed correctly. Please <a href=":href">download</a> and verify that <code>/libraries/CryptoJS/rollups/md5.js</code> exists.', array(
        '@intro' => ($phase == 'install' ? 'Supercookie requirements: ' : ''),
        ':href' => Url::fromUri('https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/crypto-js/CryptoJS v3.1.2.zip')->toString(),
      ));
    }
  }

  if ($config->get('supercookie_mongodb')) {
    $mongo_support = TRUE;

    if (!extension_loaded('mongodb')) {
      $mongo_support = FALSE;
      $requirements['supercookie']['severity'] = REQUIREMENT_ERROR;
      $requirements['supercookie']['value'] = t('Not configured');
      $requirements['supercookie']['description'][] = t('The mongodb PHP extension is not installed/enabled on your system.');
    }
    if (!file_exists(DRUPAL_ROOT . '/vendor/mongodb/mongodb/src/functions.php')) {
      $mongo_support = FALSE;
      $requirements['supercookie']['severity'] = REQUIREMENT_ERROR;
      $requirements['supercookie']['value'] = t('Not configured');
      $requirements['supercookie']['description'][] = t('The required MongoDB driver has not yet been included in Composer dependencies.');
    }
    if (!$mongo_support) {
      \Drupal::configFactory()
        ->getEditable('supercookie.settings')
        ->set('supercookie_mongodb', FALSE)
        ->save();
    }
  }

  switch ($phase) {
    case 'install':
      if (!empty($requirements['supercookie']['description'])) {
        if (PHP_SAPI === 'cli') {
          $requirements['supercookie']['description'] = implode("\n\n", $requirements['supercookie']['description']);
        }
        else {
          $requirements['supercookie']['description'] = Markup::create(implode("<br/>", $requirements['supercookie']['description']));
        }
      }
      break;

    case 'runtime':
      if (empty($requirements['supercookie']['description'])) {
        $database = \Drupal::database();
        $date_formatter = \Drupal::service('date.formatter');
        $word_formatter = \Drupal::translation();

        if ($database->schema()->tableExists('supercookie')) {
          $count = $database
            ->select('supercookie', 'sc')
            ->where('(sc.modified + :average) >= :time', array(
              ':average' => $config->get('supercookie_pageview_average'),
              ':time' => \Drupal::time()->getRequestTime(),
            ));
          $count
            ->addExpression('COUNT(sc.scid)', 'count');
          $count = $count
            ->execute()
            ->fetchField();

          $requirements['supercookie']['description'] = t('<a href=":report" target="_blank">@count</a> currently connected to %site_name per the <a href=":settings">@interval_expire cookie expiration interval</a> to within a @interval_pageview page view average.', array(
            '@count' => $count . ' ' . $word_formatter->formatPlural($count, 'active user', 'active users')->render(),
            '%site_name' => \Drupal::config('system.site')->get('name'),
            '@interval_expire' => mb_strtolower($date_formatter->formatInterval($config->get('supercookie_expire'))),
            '@interval_pageview' => mb_strtolower($date_formatter->formatInterval($config->get('supercookie_pageview_average'))),
            ':report' => Url::fromRoute('supercookie.admin_report')->toString(),
            ':settings' => Url::fromRoute('supercookie.admin_settings_form')->toString(),
          ));
        }
      }
      break;
  }

  return $requirements;
}
/**
 * Implements hook_schema().
 */
function supercookie_schema() {

  $items = [];

  $items['supercookie'] = array(
    'description' => 'The supercookie base table.',
    'fields' => array(
      'scid' => array(
        'description' => 'The primary identifier for the cookie.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'ID of the Drupal user.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the cookie was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'modified' => array(
        'description' => 'The Unix timestamp when the cookie was modified.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'expires' => array(
        'description' => 'The Unix timestamp when the cookie expires.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'A hash of the client and server variables learned about the user.',
      ),
      'tid' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of tid and count pairs that are related to taxonomy terms the user has viewed.',
      ),
      'nid' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of nid and count pairs that are related to full nodes the user has viewed.',
      ),
      'custom' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of custom data defined by dependent modules.',
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
    ),
    'primary key' => array('scid'),
    'indexes' => array(
      'uid' => array('uid'),
    ),
  );

  return $items;
}
/**
 * Implements hook_uninstall().
 */
function supercookie_uninstall() {

  $config = \Drupal::configFactory();
  $config
    ->getEditable('supercookie.settings')
    ->delete();

  \Drupal::database()
    ->schema()
    ->dropTable('supercookie');
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc