-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrpow.php
160 lines (140 loc) · 4.15 KB
/
rpow.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require_once 'rpow.civix.php';
use CRM_Rpow_ExtensionUtil as E;
/**
* Initialize civirpow state.
*
* @param array $config
* Ex: [
* 'slaves' => ['mysql://ro_user:ro_pass@ro_host/ro_db?new_link=true'],
* 'masters' => ['mysql://rw_user:rw_pass@rw_host/rw_db?new_link=true'],
* 'cookieSigningKey' => 'asdf12344321fdsa',
* ])
*/
function rpow_init($config = []) {
$defaultCookieSigningKey = md5(json_encode([
$_SERVER['HTTP_HOST'] ?? NULL,
$config['masters'],
$config['slaves'],
$_SERVER['HTTP_HOST'] ?? NULL,
]));
$defaults = [
'onReconnect' => [
'_rpow_update_cookie',
],
'cookieSigningKey' => $defaultCookieSigningKey,
'cookieName' => 'rpow' . substr(md5('cookie::' . $defaultCookieSigningKey), 0, 8),
'cookieTtl' => 90,
'stateMachine' => new CRM_Rpow_StateMachine(),
'debug' => 1,
];
global $civirpow;
$civirpow = array_merge($defaults, $civirpow ?: [], $config);
// FIXME: cookie expires relative to first edit; should be relative to last edit
if (_rpow_has_cookie($civirpow)) {
$civirpow['forceWrite'] = 1;
}
define('CIVICRM_DSN', 'civirpow://');
switch (getenv('RPOW') ?: '') {
case 'ro':
case 'slave':
define('CIVICRM_CLI_DSN', $config['slaves'][0] ?? $config['masters'][0]);
break;
case 'rw':
case 'master':
case '':
define('CIVICRM_CLI_DSN', $config['masters'][0]);
break;
default:
throw new \Exception("Unrecognized RPOW");
}
// define('CIVICRM_DSN', $config['masters'][0]);
// define('CIVICRM_DSN', $config['slaves'][0]);
}
function _rpow_signer($config) {
return new \CRM_Utils_Signer($config['cookieSigningKey'], ['exp']);
}
function _rpow_has_cookie($config) {
if (isset($_COOKIE[$config['cookieName']])) {
$cookie = json_decode($_COOKIE[$config['cookieName']], TRUE);
}
else {
$cookie = NULL;
}
if (isset($cookie['exp']) && $cookie['exp'] > time() && _rpow_signer($config)->validate($cookie['sig'], $cookie)) {
return TRUE;
}
else {
return FALSE;
}
}
function _rpow_update_cookie($config, $db) {
$signer = _rpow_signer($config);
$expires = time() + $config['cookieTtl'];
$buffer = '';
if ($config['debug']) {
foreach ($config['stateMachine']->getBuffer() as $i => $line) {
$buffer .= "$i: $line\n";
}
}
$value = json_encode([
'exp' => $expires,
'sig' => $signer->sign(['exp' => $expires]),
'cause' => $buffer,
]);
if (defined('CIVICRM_TEST') && CIVICRM_TEST) {
// Return before setting a cookie in the test context as it can
// cause test mischief - ie. https://github.com/totten/rpow/issues/8
return;
}
setcookie($config['cookieName'], $value, $expires, '/');
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function rpow_civicrm_config(&$config) {
_rpow_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function rpow_civicrm_install() {
_rpow_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function rpow_civicrm_enable() {
_rpow_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function rpow_civicrm_navigationMenu(&$menu) {
_rpow_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_rpow_civix_navigationMenu($menu);
} // */