This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.driver.php
139 lines (112 loc) · 4.59 KB
/
extension.driver.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
<?php
define('FINGERPRINT', 'symphony-fingerprint');
class Extension_Fingerprint extends Extension {
/*-------------------------------------------------------------------------
Delegates:
-------------------------------------------------------------------------*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'addCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'actionCreateFingerprint'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'eventPreSaveFilter'
),
);
}
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function actionCreateFingerprint($context) {
if($this->isException()) return true;
$doc = new DOMDocument;
// Suppress warning
if (@!$doc->loadHTML($context['output'])) {
Frontend::instance()->customError(__('Error creating fingerprint'), __('Page could not be read to create fingerprint.'));
} else {
$xpath = new DOMXpath($doc);
$fields = array();
foreach ($xpath->query('//form[@method="post"]//input[@type="hidden"]') as $input) {
$fields[] = $input->getAttribute('name');
$values .= $input->getAttribute('value');
}
$s = & $_SESSION[FINGERPRINT];
$s['fields'] = serialize($fields);
$s['token'] = hash_hmac("sha1", $values, Symphony::Configuration()->get('secret', 'fingerprint'));
}
}
public function eventPreSaveFilter($context) {
if($this->isException()) return true;
$s = & $_SESSION[FINGERPRINT];
$fields = unserialize($s['fields']);
if(is_array($fields)) {
foreach ($fields as $field) {
$values .= $this->getPostValueFromName($field);
}
}
if ($s['token'] == hash_hmac("sha1", $values, Symphony::Configuration()->get('secret', 'fingerprint')))
return true;
else
$context['messages'][] = array('fingerprint', false, __('Fingerprint does not match.'));
unset($_SESSION[FINGERPRINT]);
}
public function getPostValueFromName($name) {
$parts = preg_split('/\[|\]/i', $name, -1, PREG_SPLIT_NO_EMPTY);
if (isset($parts[3])) {
return $_POST[$parts[0]][$parts[1]][$parts[2][$parts[3]]];
} elseif (isset($parts[2])) {
return $_POST[$parts[0]][$parts[1]][$parts[2]];
} elseif (isset($parts[1])) {
return $_POST[$parts[0]][$parts[1]];
} else {
return $_POST[$parts[0]];
}
}
public function isException() {
$params = Frontend::instance()->Page()->Params();
if(is_array($params)) {
foreach ($params['page-types'] as $type) {
if($type == 'no-fingerprint') return true;
}
}
return false;
}
public function addCustomPreferenceFieldsets($context) {
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Fingerprint')));
$div = new XMLElement('div', null);
// Secret
$label = new XMLElement('label', __('Secret'));
$label->appendChild(
Widget::Input('settings[fingerprint][secret]', Symphony::Configuration()->get('secret', 'fingerprint'))
);
$div->appendChild($label);
$fieldset->appendChild($div);
$context['wrapper']->appendChild($fieldset);
}
public function savePreferences($context) {
$settings = $context['settings'];
Symphony::Configuration()->set('secret', $settings['fingerprint']['secret'], 'fingerprint');
return Symphony::Configuration()->write();
}
public function uninstall() {
// Clean configuration
Symphony::Configuration()->remove('secret', 'fingerprint');
return Symphony::Configuration()->write();
}
}