1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
ar_pinp::allow( 'ar_nls' ); |
4
|
|
|
ar_pinp::allow( 'ar_nlsDictionary', array("load", "setLanguage", "getLanguage") ); |
5
|
|
|
|
6
|
|
|
class ar_nls extends arBase { |
7
|
|
|
|
8
|
|
|
public static function dict($defaultLanguage, $currentLanguage = null, $defaultVarName = "ARnls", $baseDir = null ) { |
9
|
|
|
if ( !$baseDir ) { |
10
|
|
|
global $store; // FIXME: remove dependency on $store, use arbasedir in some way. |
11
|
|
|
$baseDir = $store->get_config("code")."nls/"; |
12
|
|
|
} |
13
|
|
|
return new ar_nlsDictionary($baseDir, $defaultLanguage, $currentLanguage, $defaultVarName ); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
class ar_nlsDictionary extends arBase implements ArrayAccess, Iterator { |
19
|
|
|
|
20
|
|
|
private $currentList = null; |
21
|
|
|
private $defaultList = null; |
22
|
|
|
private $baseDir = null; |
23
|
|
|
private $languages = array(); |
24
|
|
|
private $loaded = array(); |
25
|
|
|
public $currentLanguage = null; |
26
|
|
|
public $defaultLanguage = null; |
27
|
|
|
|
28
|
|
|
public function __construct( $baseDir, $defaultLanguage, $currentLanguage = null, $defaultVarName = "ARnls" ) { |
29
|
|
|
|
30
|
|
|
$this->baseDir = $baseDir; |
31
|
|
|
$this->defaultVarName = $defaultVarName; |
|
|
|
|
32
|
|
|
|
33
|
|
|
if( !isset($currentLanguage) ) { |
34
|
|
|
$currentLanguage = $defaultLanguage; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->languages[$defaultLanguage] = array(); |
38
|
|
|
$this->languages[$currentLanguage] = array(); |
39
|
|
|
|
40
|
|
|
$this->defaultList = &$this->languages[$defaultLanguage]; |
41
|
|
|
$this->currentList = &$this->languages[$currentLanguage]; |
42
|
|
|
|
43
|
|
|
$this->currentLanguage = $currentLanguage; |
44
|
|
|
$this->defaultLanguage = $defaultLanguage; |
45
|
|
|
|
46
|
|
|
$this->load(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setLanguage( $language ) { |
50
|
|
|
if( !isset($language) || $language == "" ) { |
51
|
|
|
$language = $this->defaultLanguage; |
52
|
|
|
} |
53
|
|
|
if( !isset( $this->languages[$language] ) ) { |
54
|
|
|
$this->languages[$language] = array(); |
55
|
|
|
} |
56
|
|
|
$this->currentList = &$this->languages[$language]; |
57
|
|
|
$this->currentLanguage = $language; |
58
|
|
|
$this->load(); |
59
|
|
|
|
60
|
|
|
foreach( $this->loaded as $section => $nlslist ) { |
61
|
|
|
if( !isset($nlslist[$language]) ) { |
62
|
|
|
$this->load($section); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getLanguage( $language = null ) { |
69
|
|
|
return isset($language) ? $this->languages[$language] : $this->currentList; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function load( $section = "", $nls = null, $varName = null ) { |
73
|
|
|
if( !isset($nls) ) { |
74
|
|
|
$nls = $this->currentLanguage; |
75
|
|
|
} |
76
|
|
|
if( !isset($varName) ) { |
77
|
|
|
$varName = $this->defaultVarName; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$nls = preg_replace('|[^a-z]*|i','',$nls); |
81
|
|
|
$re = '|[^a-z-_.:0-9/]*|i'; // only allow 'normal' characters |
82
|
|
|
$section = str_replace('//', '/', // protects against urls in the form of //google.com |
83
|
|
|
str_replace('..', '', // protects against ../../../../../etc/passwd |
84
|
|
|
preg_replace($re, '', $section))); // add .js if not set, remove .. and other dirty characters |
85
|
|
|
|
86
|
|
|
if (strpos($section, 'current:')!==false) { |
87
|
|
|
$context = pobject::getContext(); |
88
|
|
|
$arLibraryPath = $context['arLibraryPath']; |
89
|
|
|
$sectionCacheName = str_replace( 'current:', $arLibraryPath.':', $section); |
90
|
|
|
} else { |
91
|
|
|
$sectionCacheName = $section; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if( !$section ) { |
95
|
|
|
if( !$this->loaded[$sectionCacheName][$nls] && ($fullFile = $this->baseDir.$nls) && file_exists($fullFile) ) { |
96
|
|
|
include($fullFile); |
97
|
|
|
$this->loaded[$sectionCacheName][$nls] = true; |
98
|
|
|
$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$$varName); |
99
|
|
|
} |
100
|
|
|
} elseif( !$this->loaded[$sectionCacheName][$nls] ) { |
101
|
|
|
$this->loaded[$sectionCacheName][$nls] = true; |
102
|
|
|
$fullFile = $this->baseDir.$section.".".$nls; |
103
|
|
|
if( file_exists($fullFile) ) { |
104
|
|
|
include($fullFile); |
105
|
|
|
$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$$varName); |
106
|
|
|
} else { |
107
|
|
|
// FIXME: UGLY hack stolen from pobject::loadtext() |
108
|
|
|
global $ARCurrent; |
109
|
|
|
$context = pobject::getContext(); |
110
|
|
|
$me = $context["arCurrentObject"]; |
111
|
|
|
if ( !$me || !is_object($me) ) { |
112
|
|
|
//FIXME: this should not happen, but ldObjectNotFound() sometimes triggers this |
113
|
|
|
// the problem is that there is no arCurrentObject pushed on the stack |
114
|
|
|
// generally we can just return and nothing serious will happen |
115
|
|
|
debug('No current object found on the context stack, skipping loadtext', 'all'); |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
$arResult = $ARCurrent->arResult; |
119
|
|
|
$me->pushContext(array()); |
120
|
|
|
$oldnls = $me->reqnls; |
121
|
|
|
$me->reqnls = $nls; |
122
|
|
|
$oldAllnls = $ARCurrent->allnls; |
123
|
|
|
$ARCurrent->allnls = true; |
124
|
|
|
$me->CheckConfig($section, array("nls" => $nls)); |
125
|
|
|
$ARCurrent->allnls = $oldAllnls; |
126
|
|
|
$me->reqnls = $oldnls; |
127
|
|
|
$me->popContext(); |
128
|
|
|
|
129
|
|
|
$nlsarray = array(); |
130
|
|
|
if( is_array($ARCurrent->arResult) ) { |
131
|
|
|
$nlsarray = $ARCurrent->arResult; |
132
|
|
|
} elseif( is_array($me->{$varName}) ) { |
133
|
|
|
$nlsarray = $me->{$varName}; |
134
|
|
|
} elseif( is_array($ARCurrent->{$varName}) ) { |
135
|
|
|
$nlsarray = $ARCurrent->{$varName}; |
136
|
|
|
} |
137
|
|
|
$ARCurrent->arResult = $arResult; |
138
|
|
|
$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$nlsarray); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/* ArrayAccess */ |
145
|
|
|
|
146
|
1 |
|
public function offsetSet( $offset, $value ) { |
147
|
1 |
|
if ($offset == "") { |
148
|
|
|
$this->currentList[] = $value; |
149
|
|
|
} else { |
150
|
1 |
|
$this->currentList[$offset] = $value; |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
|
154
|
|
|
public function offsetExists( $offset ) { |
155
|
|
|
return ( $this->getEntry( $offset ) !== null ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function offsetUnset( $offset ) { |
159
|
|
|
unset($this->currentList[$offset]); |
160
|
|
|
} |
161
|
|
|
|
162
|
7 |
|
private function getEntry( $offset ) { |
163
|
7 |
View Code Duplication |
if( isset( $this->currentList[$offset] ) ) { |
164
|
7 |
|
return $this->currentList[$offset]; |
165
|
|
|
} elseif( strpos($offset, ":") !== false ) { // $ARnls["ariadne:foo"] => try and autoload "ariadne.$currentLanguage" |
166
|
|
|
list($section, $rest) = explode(":", $offset, 2); |
|
|
|
|
167
|
|
|
$this->load($section, $this->currentLanguage); |
168
|
|
|
if( isset( $this->currentList[$offset] ) ) { |
169
|
|
|
return $this->currentList[$offset]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
View Code Duplication |
if( isset( $this->defaultList[$offset] ) ) { |
173
|
|
|
return $this->defaultList[$offset]; |
174
|
|
|
} elseif( strpos($offset, ":") !== false ) { // $ARnls["ariadne:foo"] => try and autoload "ariadne.$defaultLanguage" |
175
|
|
|
list($section, $rest) = explode(":", $offset, 2); |
|
|
|
|
176
|
|
|
$this->load($section, $this->defaultLanguage); |
177
|
|
|
if( isset( $this->defaultList[$offset] ) ) { |
178
|
|
|
return $this->defaultList[$offset]; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
7 |
|
public function offsetGet( $offset ) { |
186
|
7 |
|
$value = $this->getEntry( $offset ); |
187
|
7 |
|
return ( isset( $value ) ? $value : "{".$offset."}" ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/* Iterator */ |
191
|
|
|
public function current() { |
192
|
|
|
return current($this->currentList); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function key() { |
196
|
|
|
return key($this->currentList); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function next() { |
200
|
|
|
return next($this->currentList); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function rewind() { |
204
|
|
|
return reset($this->currentList); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function valid() { |
208
|
|
|
$value = key($this->currentList); |
209
|
|
|
return isset($value); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: