-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextension.driver.php
162 lines (136 loc) · 4.13 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
Copyight: Deux Huit 2012, 2014
License: MIT, see the LICENCE file
*/
if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
/**
*
* Block user agent Decorator/Extension
* @author nicolasbrassard
*
*/
class extension_block_user_agent extends Extension {
/**
* Name of the extension
* @var string
*/
const EXT_NAME = 'Block User Agent';
const SETTING_GROUP = 'block-user-agent';
/**
* private variable for holding the errors encountered when saving
* @var array
*/
protected $errors = array();
/**
*
* Symphony utility function that permits to
* implement the Observer/Observable pattern.
* We register here delegate that will be fired by Symphony
*/
public function getSubscribedDelegates(){
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsPostResolve',
'callback' => 'frontendParamsPostResolve'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'addCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'save'
)
);
}
public static function getRegEx() {
return Symphony::Configuration()->get('regex', self::SETTING_GROUP);
}
public static function isBlocked() {
$regex = self::getRegEx();
$val = 0;
if (!empty($regex)) {
try {
$val = preg_match($regex, $_SERVER['HTTP_USER_AGENT']);
} catch (Exception $e) {
$val = $e->getMessage();
}
}
return $val;
}
public function frontendParamsPostResolve($params) {
$blocked = self::isBlocked();
$params['params']['block-user-agent'] = ($blocked === 1 ? 'Yes' : ($blocked === 0 ? 'No' : $blocked));
$params['params']['block-user-agent-regex'] = self::getRegEx();
}
/**
*
* Delegate fired when the extension is install
*/
public function install() {
Symphony::Configuration()->set('regex', '', self::SETTING_GROUP);
Symphony::Configuration()->write();
}
/**
*
* Delegate fired when the extension is updated (when version changes)
* @param string $previousVersion
*/
public function update($previousVersion = false) {
}
/**
*
* Delegate fired when the extension is uninstall
* Cleans settings and Database
*/
public function uninstall() {
Symphony::Configuration()->remove(self::SETTING_GROUP);
Symphony::Configuration()->write();
}
/**
* Delegate handle that adds Custom Preference Fieldsets
* @param string $page
* @param array $context
*/
public function addCustomPreferenceFieldsets($context) {
// creates the field set
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', self::EXT_NAME));
// create a paragraph for short intructions
$p = new XMLElement('p', __('Define how UA are blocked'), array('class' => 'help'));
// append intro paragraph
$fieldset->appendChild($p);
// outter wrapper
$out_wrapper = new XMLElement('div');
// create a wrapper
//$wrapper = new XMLElement('div');
//$wrapper->setAttribute('class', 'group');
// create the label and the input field
$label = Widget::Label();
$input = Widget::Input(
'settings[' . self::SETTING_GROUP . '][regex]',
self::getRegEx(),
'text'
);
// set the input into the label
$label->setValue(__('Specify here the regex to test against UA'). ' ' . $input->generate() . $err);
//$wrapper->appendChild($label);
$out_wrapper->appendChild($label);
// wrapper into fieldset
$fieldset->appendChild($out_wrapper);
// adds the field set to the wrapper
$context['wrapper']->appendChild($fieldset);
}
/**
* Delegate handle that saves the preferences
* Saves settings and cleans the database acconding to the new settings
* @param array $context
*/
public function save(&$context) {
}
}