Elgg  Version 4.3
SiteSecret.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
9 
25 class SiteSecret {
26 
27  const CONFIG_KEY = '__site_secret__';
28 
32  private $key;
33 
39  public function __construct($key) {
40  $this->key = $key;
41  }
42 
53  public function get($raw = false) {
54  if (!$this->key) {
55  throw new RuntimeException('Secret key is not set');
56  }
57 
58  if (!$raw) {
59  return $this->key;
60  }
61 
62  // try to return binary key
63  if ($this->key[0] === 'z') {
64  // new keys are "z" + base64URL
65  $base64 = strtr(substr($this->key, 1), '-_', '+/');
66  $key = base64_decode($base64);
67  if ($key !== false) {
68  return $key;
69  }
70 
71  // on failure, at least return string key :/
72  return $this->key;
73  }
74 
75  // old keys are hex
76  return hex2bin($this->key);
77  }
78 
87  public function getStrength() {
88  $secret = $this->get();
89  if ($secret[0] !== 'z') {
90  $rand_max = getrandmax();
91  if ($rand_max < pow(2, 16)) {
92  return 'weak';
93  }
94  if ($rand_max < pow(2, 32)) {
95  return 'moderate';
96  }
97  }
98  return 'strong';
99  }
100 
112  public static function regenerate(Crypto $crypto, ConfigTable $table) {
113  $key = 'z' . $crypto->getRandomString(31);
114 
115  $table->set(self::CONFIG_KEY, $key);
116 
117  return new self($key);
118  }
119 
128  public static function fromDatabase(ConfigTable $table) {
129  $key = $table->get(self::CONFIG_KEY);
130  if (!$key) {
131  throw new InstallationException('Site secret is not in the config table.');
132  }
133 
134  return new self($key);
135  }
136 
144  public static function fromConfig(ElggConfig $config) {
145  $key = $config->{self::CONFIG_KEY};
146  if (!$key) {
147  return false;
148  }
149 
150  return new self($key);
151  }
152 }
static fromDatabase(ConfigTable $table)
Create from config/storage.
Definition: SiteSecret.php:128
Cryptographic services.
Definition: Crypto.php:12
Exception thrown if an error which can only be found on runtime occurs.
Manages a site-specific secret key, encoded as a 32 byte string "secret".
Definition: SiteSecret.php:25
__construct($key)
Constructor.
Definition: SiteSecret.php:39
get(string $name)
Gets a configuration value.
Updates the basic settings for the primary site object.
getStrength()
Get the strength of the site secret.
Definition: SiteSecret.php:87
static regenerate(Crypto $crypto, ConfigTable $table)
Initialise the site secret (32 bytes: "z" to indicate format + 186-bit key in Base64 URL) and save to...
Definition: SiteSecret.php:112
static fromConfig(ElggConfig $config)
Create from a config value.
Definition: SiteSecret.php:144
Thrown when there is a major problem with the installation.
getRandomString($length, $chars=null)
Generate a random string of specified length.
Definition: Crypto.php:43
set(string $name, $value)
Add or update a config setting.
Definition: ConfigTable.php:83
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:16
$table
Definition: cron.php:56