-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.driver.php
executable file
·169 lines (130 loc) · 5.6 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
<?php
Class extension_language_redirect extends Extension{
public function about(){
return array(
'name' => 'Language Redirect',
'version' => '1.0.3',
'release-date' => '2011-07-02',
'author' => array(
array(
'name' => 'Jonas Coch',
'website' => 'http://klaftertief.de',
'email' => '[email protected]'
),
array(
'name' => 'Vlad Ghita',
'email' => '[email protected]'
)
),
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => '__SavePreferences'
),
);
}
public function appendPreferences($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __('Language Redirect')));
$label = Widget::Label(__('Language codes'));
$label->appendChild(Widget::Input('settings[language_redirect][language_codes]', General::Sanitize(Symphony::Configuration()->get('language_codes', 'language_redirect'))));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', __('Comma separated list of supported language codes. First language ist the default language. Leave empty to disable <code>.htaccess</code>-rules.'), array('class' => 'help')));
$context['wrapper']->appendChild($group);
}
public function __SavePreferences($context){
$languageCodes = $_POST['settings']['language_redirect']['language_codes'];
return self::__updateRewriteRules('edit', $languageCodes);
}
public function install(){
return self::__updateRewriteRules('create');
}
public function enable(){
$languageCodes = Symphony::Configuration()->get('language_codes', 'language_redirect');
return self::__updateRewriteRules('edit', $languageCodes);
}
public function update($previousVersion){
if (version_compare($previousVersion, '1.0', '<')) {
$languageCodes = Symphony::Configuration()->get('languages', 'language_redirect');
Symphony::Configuration()->remove('languages', 'language_redirect');
Symphony::Configuration()->set('language_codes', $languageCodes, 'language_redirect');
Administration::instance()->saveConfig();
}
return true;
}
public function disable(){
return self::__updateRewriteRules('edit');
}
public function uninstall(){
Symphony::Configuration()->remove('language_redirect');
Administration::instance()->saveConfig();
return self::__updateRewriteRules('remove');
}
private static function __updateRewriteRules($mode, $languageCodes = NULL){
$htaccess = @file_get_contents(DOCROOT . '/.htaccess');
if($htaccess === false) return false;
switch ($mode) {
case 'create':
$htaccess = self::__createLanguageRules($htaccess);
break;
case 'edit':
$htaccess = self::__editLanguageRules($htaccess, $languageCodes);
break;
case 'remove':
$htaccess = self::__removeLanguageRules($htaccess);
break;
}
return @file_put_contents(DOCROOT . '/.htaccess', $htaccess);
}
private static function __createLanguageRules($htaccess){
$rule = "\t### LANGUAGE REDIRECT RULES start\n\t### no language codes set\n\t### LANGUAGE REDIRECT RULES end";
## Remove existing rules
$htaccess = self::__removeLanguageRules($htaccess);
$htaccess = preg_replace('/(\s?### FRONTEND REWRITE)/', "{$rule}\n\n$1", $htaccess);
return $htaccess;
}
private static function __editLanguageRules($htaccess, $languageCodes = NULL){
## Cannot use $1 in a preg_replace replacement string, so using a token instead
$token_language = md5('language');
$token_region = md5('region');
$token_symphony = md5('symphony-page');
if (isset($languageCodes)) {
$languageCodes = explode(',', $languageCodes);
$languageCodes = array_map('trim', $languageCodes);
$languages = array();
$regions = array();
foreach ($languageCodes as $languageCode) {
$languages[] = substr($languageCode, 0, 2);
$regions[] = substr(strrchr($languageCode, '-'), 1);
}
$languages = array_filter(array_unique($languages));
$regions = array_filter(array_unique($regions));
$languages = (is_array($languages) and !empty($languages)) ? implode('|', $languages) : NULL;
$regions = (is_array($regions) and !empty($regions)) ? implode('|', $regions) : NULL;
$rule = "\n\tRewriteCond %{REQUEST_FILENAME} !-d";
$rule .= "\n\tRewriteCond %{REQUEST_FILENAME} !-f";
$rule .= "\n\tRewriteRule ^({$languages})-?({$regions})?\/(.*\/?)$ index.php?language={$token_language}®ion={$token_region}&symphony-page={$token_symphony}&%{QUERY_STRING} [L]";
} else {
$rule = "\n\t### no language codes set";
}
$htaccess = preg_replace('/(\s+### LANGUAGE REDIRECT RULES start)(.*?)(\s*### LANGUAGE REDIRECT RULES end)/s', "$1{$rule}$3", $htaccess);
## Replace the token with the real value
$htaccess = str_replace($token_language, '$1', $htaccess);
$htaccess = str_replace($token_region, '$2', $htaccess);
$htaccess = str_replace($token_symphony, '$3', $htaccess);
return $htaccess;
}
private static function __removeLanguageRules($htaccess){
return preg_replace('/\s+### LANGUAGE REDIRECT RULES start(.*?)### LANGUAGE REDIRECT RULES end/s', NULL, $htaccess);
}
}