-
Notifications
You must be signed in to change notification settings - Fork 6
/
extension.driver.php
144 lines (101 loc) · 4.82 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
<?php
if( !defined('__IN_SYMPHONY__') ) die('<h2>Error</h2><p>You cannot directly access this file</p>');
require_once(EXTENSIONS.'/frontend_localisation/lib/class.FLang.php');
Class extension_flang_detection_gtlds extends Extension
{
/*------------------------------------------------------------------------------------------------*/
/* Installation */
/*------------------------------------------------------------------------------------------------*/
public function install(){
return $this->__updateRewriteRules('create') && $this->__updateRewriteRules('edit', FLang::getLangs());
}
public function enable(){
return $this->__updateRewriteRules('create') && $this->__updateRewriteRules('edit', FLang::getLangs());
}
public function disable(){
return $this->__updateRewriteRules('edit');
}
public function uninstall(){
return $this->__updateRewriteRules('remove');
}
/*------------------------------------------------------------------------------------------------*/
/* Delegates */
/*------------------------------------------------------------------------------------------------*/
public function getSubscribedDelegates(){
return array(
array(
'page' => '/extensions/frontend_localisation/',
'delegate' => 'FLSavePreferences',
'callback' => 'dFLSavePreferences'
),
);
}
/*------------------------------------------------------------------------------------------------*/
/* .htacces management */
/*------------------------------------------------------------------------------------------------*/
public function dFLSavePreferences($context){
if( false === $this->__updateRewriteRules('edit', $context['new_langs']) ){
$context['errors']['flang_detection_gtlds'] = __('There were errors writing the <code>.htaccess</code> file. Please verify it is writable.');
return false;
}
return true;
}
/*------------------------------------------------------------------------------------------------*/
/* Utilities */
/*------------------------------------------------------------------------------------------------*/
private function __updateRewriteRules($mode, $langs = array()){
$htaccess = @file_get_contents(DOCROOT.'/.htaccess');
if( $htaccess === false ) return false;
switch( $mode ){
case 'create':
$htaccess = $this->__createLanguageRules($htaccess);
break;
case 'edit':
$htaccess = $this->__editLanguageRules($htaccess, $langs);
break;
case 'remove':
$htaccess = $this->__removeLanguageRules($htaccess);
break;
}
return @file_put_contents(DOCROOT.'/.htaccess', $htaccess);
}
private 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 = $this->__removeLanguageRules($htaccess);
$htaccess = preg_replace('/(\s?### FRONTEND REWRITE)/', "{$rule}\n\n$1", $htaccess);
return $htaccess;
}
private function __editLanguageRules($htaccess, $langs = array()){
## 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 (!empty($langs)) {
$languages = array();
$regions = array();
foreach ($langs as $lang_code) {
$languages[] = substr($lang_code, 0, 2);
$regions[] = substr(strrchr($lang_code, '-'), 1);
}
$languages = array_filter(array_unique($languages));
$regions = array_filter(array_unique($regions));
$languages = (is_array($languages) && !empty($languages)) ? implode('|', $languages) : null;
$regions = (is_array($regions) && !empty($regions)) ? implode('|', $regions) : null;
$rule = "\n\tRewriteCond %{REQUEST_FILENAME} !-d";
$rule .= "\n\tRewriteCond %{REQUEST_FILENAME} !-f";
$rule .= "\n\tRewriteRule ^({$languages})-?({$regions})?\/(.*\/?)$ index.php?fl-language={$token_language}&fl-region={$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 function __removeLanguageRules($htaccess){
return preg_replace('/\s+### LANGUAGE REDIRECT RULES start(.*?)### LANGUAGE REDIRECT RULES end/s', NULL, $htaccess);
}
}