-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.driver.php
219 lines (176 loc) · 6.09 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
/*
Copyight: Solutions Nitriques 2011
License: MIT
*/
class extension_force_domain_name extends Extension {
const EXT_NAME = 'Force Domain Name';
/**
* Regular expression for validating a domain name
* @var string
* Credits: http://www.shauninman.com/archive/2006/05/08/validating_domain_names
*/
const REGEXP_DOMAIN = '/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i';
/**
* Key of the domain setting
* @var string
*/
const SETTING_NAME = 'domain';
/**
* Key of the group of setting
* @var string
*/
const SETTING_GROUP = 'force-domain';
/**
* Header to mark the redirection as permanent (301)
* @var string
*/
const HEADER_MOVE = 'HTTP/1.1 301 Moved Permanently';
/**
* private variable for holding the errors encountered when saving
* @var string
*/
protected $error = '';
/**
* Credits for the extension
*/
public function about() {
return array(
'name' => self::EXT_NAME,
'version' => '1.0',
'release-date' => '2011-06-15',
'author' => array(
'name' => 'Solutions Nitriques',
'website' => 'http://www.nitriques.com/',
'email' => 'nico (at) nitriques.com'
),
'description' => 'Really simple ext that force user to a specified domain name',
'compatibility' => array(
'2.2.1' => true,
'2.2' => true
)
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendPrePageResolve',
'callback' => 'frontendPrePageResolve'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'addCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'save'
),
);
}
/**
* Utiliy function that retreives the value of the setting
* @return string
*/
public function getDomainInUse() {
return Symphony::Configuration()->get(self::SETTING_NAME, self::SETTING_GROUP);
}
/**
* Delegate handle that resolve the page's url
* @param string $page
* @param array $context
*/
public function frontendPrePageResolve($context) {
// assure we can detect the current domain name
// and that is it not localhost
if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != 'localhost') {
// domain in use
$cur_domain = $_SERVER['HTTP_HOST'];
// configured domain
$conf_domain = $this->getDomainInUse();
// if a domain was configured and domains does not match
if (strlen($conf_domain) > 0 && $cur_domain != $conf_domain) {
// redirect to good domain
// while keeping the url intact
// get the protocol
$protocol = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://';
// get the uri
$new_location = $_SERVER["REQUEST_URI"];
$about = $this->about();
// permanent redirect
header('X-Redirected-By: ' . self::EXT_NAME);
header(self::HEADER_MOVE);
header("Location: $protocol$conf_domain$new_location");
// stop process immediatly
exit();
}
}
}
/**
* 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', __('Force Domain Name')));
// create a paragraph for short intructions
$p = new XMLElement('p', __('Define here the domain name you wanna use (without http://)'), array('class' => 'help'));
// append intro paragraph
$fieldset->appendChild($p);
// create a wrapper
$wrapper = new XMLElement('div');
// wrapper into fieldset
$fieldset->appendChild($wrapper);
// create the label and the input field
$label = Widget::Label();
$input = Widget::Input('settings[' . self::SETTING_GROUP . '][' . self::SETTING_NAME .']', $this->getDomainInUse(), 'text');
// set the input into the label
$label->setValue(__('Domain Name'). ' ' . $input->generate());
// append label to field set
$wrapper->appendChild($label);
// error management
if (strlen($this->error) > 0) {
// set css and anchor
$wrapper->setAttribute('id', 'error');
$wrapper->setAttribute('class', 'invalid');
// adds error message
$err = new XMLElement('p', $this->error);
// append to $wrapper
$wrapper->appendChild($err);
}
// adds the field set to the wrapper
$context['wrapper']->appendChild($fieldset);
}
/**
* Delegate handle that saves the preferences
* @param string $page
* @param array $context
*/
public function save($context){
//var_dump(self::REGEXP_DOMAIN);die;
//var_dump($context['settings']['force-domain']['domain']);die;
// gets the input
$domain = $context['settings'][self::SETTING_GROUP][self::SETTING_NAME];
// verify it is a good domain
if (preg_match(self::REGEXP_DOMAIN, $domain) == 1) {
// set config (name, value, group)
Symphony::Configuration()->set(self::SETTING_NAME, $domain, self::SETTING_GROUP);
// save it
Administration::instance()->saveConfig();
} else {
// don't save
// set error message
$this->error = __('"%s" is not a valid domain', array($domain));
//echo $error;die;
// add an error into the stack
$context['errors'][self::SETTING_GROUP][self::SETTING_NAME] = $this->error;
}
}
}
?>