summaryrefslogtreecommitdiffstats
path: root/lib/Drupal/cryptolog/CryptologManager.php
blob: 29c983656ffc8202bfc8d143eaf0b70ba6e3fbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

/**
 * @file
 * Definition of Drupal\cryptolog\CryptologManager.
 */

namespace Drupal\cryptolog;

/**
 * Cryptolog Manager.
 */
class CryptologManager {

  /**
   * The memory-based data store.
   */
  protected $cache;

  /**
   * Construct the Cryptolog Manager.
   */
  public function __construct() {
    foreach (array('dmemcache_set' => 'dmemcache_get', 'apc_store' => 'apc_fetch', 'xcache_set' => 'xcache_get') as $set => $get) {
      if (function_exists($set)) {
        $this->cache = $get('cryptolog');
        $day = gmdate('zY');
        if (!$this->cache || $this->cache['day'] !== $day) {
          $this->cache = array('day' => $day, 'salt' => drupal_random_bytes(32));
          $set('cryptolog', $this->cache, 86400);
        }
        break;
      }
    }
  }

  /**
   * Sets the client IP address.
   */
  public function setClientIP() {
    global $request;
    if ($this->cache) {
      // Generate a keyed hash in IPv6 address notation.
      $_SERVER['REMOTE_ADDR'] = inet_ntop(hash_hmac('md5', ip_address(), $this->cache['salt'], TRUE));
      drupal_static_reset('ip_address');
      $request->server->set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
    }
  }

}