Elgg  Version 1.11
SiteSecret.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
22 class SiteSecret {
23 
34  function init() {
35  $secret = 'z' . _elgg_services()->crypto->getRandomString(31);
36 
37  if (_elgg_services()->datalist->set('__site_secret__', $secret)) {
38  return $secret;
39  }
40 
41  return false;
42  }
43 
54  function get($raw = false) {
55  $secret = _elgg_services()->datalist->get('__site_secret__');
56  if (!$secret) {
57  $secret = init_site_secret();
58  }
59 
60  if ($raw) {
61  // try to return binary key
62  if ($secret[0] === 'z') {
63  // new keys are "z" + base64URL
64  $base64 = strtr(substr($secret, 1), '-_', '+/');
65  $key = base64_decode($base64);
66  if ($key !== false) {
67  // on failure, at least return string key :/
68  return $key;
69  }
70  } else {
71  // old keys are hex
72  return hex2bin($secret);
73  }
74  }
75 
76  return $secret;
77  }
78 
88  function getStrength() {
89  $secret = get_site_secret();
90  if ($secret[0] !== 'z') {
91  $rand_max = getrandmax();
92  if ($rand_max < pow(2, 16)) {
93  return 'weak';
94  }
95  if ($rand_max < pow(2, 32)) {
96  return 'moderate';
97  }
98  }
99  return 'strong';
100  }
101 
102 }
init()
Initialise the site secret (32 bytes: "z" to indicate format + 186-bit key in Base64 URL)...
Definition: SiteSecret.php:34
init_site_secret()
Initialise the site secret (32 bytes: "z" to indicate format + 186-bit key in Base64 URL)...
Definition: actions.php:182
getStrength()
Get the strength of the site secret.
Definition: SiteSecret.php:88
$key
Definition: summary.php:34
_elgg_services()
Definition: autoloader.php:14
get_site_secret()
Returns the site secret.
Definition: actions.php:195