-
Notifications
You must be signed in to change notification settings - Fork 6
/
esi.inc
126 lines (108 loc) · 2.94 KB
/
esi.inc
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
// $Id: esi.inc,v 1.2 2010/11/17 11:37:25 manarth Exp $
/**
* @file
* Helper functions for the ESI module.
*/
/**
* Get the current seed key.
*/
function _esi__get_seed_key() {
$seed = variable_get('esi_seed_key', FALSE);
if (!$seed) {
$seed = _esi__rotate_seed_key();
}
return $seed;
}
/**
* Rotate the seed key.
*/
function _esi__rotate_seed_key() {
$seed = _esi__get_random_seed();
variable_set('esi_seed_key', $seed);
variable_set('esi_seed_key_last_changed', time());
return $seed;
}
/**
* Get a random 32-character string
*/
function _esi__get_random_seed() {
$seed = '';
for ($i=0; $i<32; $i++) {
// get a random character from the printable ASCII character set: 32-176
$seed += chr(mt_rand(32, 176));
}
return $seed;
}
/**
* Get the hash for a set of roles.
*
* @param array $rids
* An array of role-ids
*/
function _esi__get_roles_hash($rids) {
$seed = _esi__get_seed_key();
$hash = implode(':', $rids);
$hash = md5($seed . md5($hash));
return $hash;
}
/**
* Get the themed HTML for a particular block
*
* @param String $theme
* The name of the theme: e.g. "garland"
* @param String $region
* The name of the region the block is in: e.g. "left"
* @param String $module
* @param String $delta
*/
function _esi__get_block($theme, $region, $module, $delta) {
global $theme_key;
$theme_key = $theme;
init_theme();
$blocks = block_list($region);
return $blocks["{$module}_{$delta}"];
}
/**
* Get / Set the config for a particular block.
*/
function _esi__block_settings($module, $delta, $config = NULL) {
// TODO: replace with something more suited to in-code config than variables.
$key = "{$module}_{$delta}";
$global = variable_get('esi_block_config', array());
if ($config) {
$global[$key] = $config;
variable_set('esi_block_config', $global);
}
elseif (array_key_exists($key, $global)) {
$config = $global[$key];
}
else {
$config = new stdClass;
$config->enabled = ESI__BLOCK_CONFIG_DEFAULT__IS_ENABLED;
$config->ttl = ESI__BLOCK_CONFIG_DEFAULT__TTL;
}
return $config;
}
/**
* Get a list of possible ttl choices.
*/
function _esi__ttl_options($current_ttl) {
$options = array(
'5' => t('@count seconds', array('@count' => 5)),
'30' => t('@count seconds', array('@count' => 30)),
'60' => t('1 minute'),
'300' => t('@count minutes', array('@count' => 5)),
'1800' => t('@count minutes', array('@count' => 30)),
'3600' => t('1 hour'),
'10800' => t('@count hours', array('@count' => 3)),
'21600' => t('@count hours', array('@count' => 6)),
'43200' => t('@count hours', array('@count' => 12)),
'86400' => t('1 day'),
);
// if the given ttl isn't one of our options, add the current ttl as a custom option.
if (!array_key_exists($current_ttl, $options)) {
$options[$current_ttl] = format_plural($current_ttl, 'Custom: @count second', 'Custom: @count seconds');
}
return $options;
}