-
Notifications
You must be signed in to change notification settings - Fork 90
/
Module.php
342 lines (305 loc) · 11.4 KB
/
Module.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace lajax\translatemanager;
use Yii;
use yii\base\InvalidConfigException;
use yii\web\ForbiddenHttpException;
use yii\web\Response;
/**
* This is the main module class for the TranslateManager module.
*
* Initialisation example:
*
* Simple example:
*
* ~~~
* 'modules' => [
* 'translatemanager' => [
* 'class' => 'lajax\translatemanager\Module',
* ],
* ],
* ~~~
*
* Complex example:
*
* ~~~
* 'modules' => [
* 'translatemanager' => [
* 'class' => 'lajax\translatemanager\Module',
* 'root' => '@app', // The root directory of the project scan.
* 'layout' => 'language', // Name of the used layout. If using own layout use 'null'.
* 'allowedIPs' => ['127.0.0.1'], // IP addresses from which the translation interface is accessible.
* 'roles' => ['@'], // For setting access levels to the translating interface.
* 'tmpDir' => '@runtime', // Writable directory for the client-side temporary language files.
* // IMPORTANT: must be identical for all applications (the AssetsManager serves the JavaScript files containing language elements from this directory).
* 'phpTranslators' => ['::t'], // list of the php function for translating messages.
* 'jsTranslators' => ['lajax.t'], // list of the js function for translating messages.
* 'patterns' => ['*.js', '*.php'],// list of file extensions that contain language elements.
* 'ignoredCategories' => ['yii'], // these categories won’t be included in the language database.
* 'ignoredItems' => ['config'], // these files will not be processed.
* 'languageTable' => 'language', // Name of the database table storing the languages.
* 'scanTimeLimit' => null, // increase to prevent "Maximum execution time" errors, if null the default max_execution_time will be used
* 'searchEmptyCommand' => '!', // the search string to enter in the 'Translation' search field to find not yet translated items, set to null to disable this feature
* 'defaultExportStatus' => 1, // the default selection of languages to export, set to 0 to select all languages by default
* 'defaultExportFormat' => 'json',// the default format for export, can be 'json' or 'xml'
* 'tables' => [ // Properties of individual tables
* [
* 'connection' => 'db', // connection identifier
* 'table' => '{{%language}}', // table name
* 'columns' => ['name', 'name_ascii'], //names of multilingual fields
* 'category' => 'database-table-name', // the category is the database table name
* ]
* ]
* ],
* ],
* ~~~
*
* IMPORTANT: If you want to modify the value of roles (in other words to start using user roles) you need to enable authManager in the common config.
*
* Using of authManager: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
*
* examples:
*
* PhpManager:
*
* ~~~
* 'components' => [
* 'authManager' => [
* 'class' => 'yii\rbac\PhpManager',
* ],
* ],
* ~~~
*
* DbManager:
*
* ~~~
* 'components' => [
* 'authManager' => [
* 'class' => 'yii\rbac\DbManager',
* ],
* ],
* ~~~
*
*
* @author Lajos Molnár <[email protected]>
*
* @since 1.0
*/
class Module extends \yii\base\Module
{
/**
* Session key for storing front end translating privileges.
*/
const SESSION_KEY_ENABLE_TRANSLATE = 'frontendTranslation_EnableTranslate';
/**
* @inheritdoc
*/
public $controllerNamespace = 'lajax\translatemanager\controllers';
/**
* @inheritdoc
*/
public $defaultRoute = 'language/list';
/**
* @var string name of the used layout. If you want to use the site default layout set value null.
*/
public $layout = 'language';
/**
* @var array the list of IPs that are allowed to access this module.
*/
public $allowedIPs = ['127.0.0.1', '::1'];
/**
* @var array the list of rights that are allowed to access this module.
* If you modify, you also need to enable authManager.
* http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
*/
public $roles = [];
/**
* @var string[] List of the categories being ignored.
*/
public $ignoredCategories = [];
/**
* @var string[] List of the categories to be scanned. If empty, all categories will be scanned.
*/
public $onlyCategories = [];
/**
* @var array directories/files being ignored.
*/
public $ignoredItems = [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/BaseYii.php',
'runtime',
'bower',
'nikic',
];
/**
* @var string|array The root directory or directories of the scanning. The path can be an alias or
* a full path.
*
* It is possible to define one root directory as string. In this case the `scanRootParentDirectory` will be used
* when determining the actual directory to scan.
*
* Multiple root directories can be declared in an array. In this case all items must point to the exact directory,
* as `scanRootParentDirectory` **will be omitted**.
*/
public $root = '@app';
/**
* @var bool Whether scan the defined `root` parent directory, or the folder itself. This option is used only,
* when the `root` option contains a single directory as string (e.g. `'root' => '@app'`).
*
* <b>IMPORTANT</b>: Changing this from `true` to `false` could cause loss of translated items, as
* optimize action removes the missing items.
*
* If the configured root is `@app`:
* - `true` means for advanced apps, that the scan runs on the parent directory, which is the root for the entire project.
* This is the desired behavior.
* - `true` means for basic apps, that the scan runs also on the parent directory, which is outside of the project folder
* (as `@app` is equals to the project root). This is not desired behavior, it is preferred to change this option
* to `false`.
*/
public $scanRootParentDirectory = true;
/**
* @var string writeable directory used for keeping the generated javascript files.
*/
public $tmpDir = '@runtime/';
/**
* @var array list of file extensions that contain language elements.
* Only files with these extensions will be processed.
*/
public $patterns = ['*.php', '*.js'];
/**
* @var string name of the subdirectory which contains the language elements.
*/
public $subDir = '/translate/';
/**
* @var string Regular expression to match PHP Yii::t functions.
*
* @deprecated since version 1.2.7
*/
public $patternPhp = '#::t\s*\(\s*(?P<category>\'[\w\d\s_-]+?(?<!\\\\)\'|"[\w\d\s_-]+?(?<!\\\\)"?)\s*,\s*(?P<text>\'.*?(?<!\\\\)\'|".*?(?<!\\\\)"?)\s*[,\)]#s';
/**
* @var string PHP Regular expression to match arrays containing language elements to translate.
*
* @deprecated since version 1.2.7
*/
public $patternArray = "#\@translate[^\$]+\$(?P<text>.+?)[\]\)];#smui";
/**
* @var string PHP Regular expression to detect langualge elements within arrays.
*
* @deprecated since version 1.2.7
*/
public $patternArrayRecursive = '#(?P<category>)(\[|\(|>|,|)\s*(?P<text>\'.*?(?<!\\\\)\'|".*?(?<!\\\\)"?)\s*(,|$)#s';
/**
* @var string Regular expression to detect JavaScript lajax.t functions.
*
* @deprecated since version 1.2.7
*/
public $patternJs = '#lajax\.t\s*\(\s*(?P<text>\'.*?(?<!\\\\)\'|".*?(?<!\\\\)"?)\s*[,\)]#s';
/**
* @var array List of the PHP function for translating messages.
*/
public $phpTranslators = ['::t'];
/**
* @var array List of the JavaScript function for translating messages.
*/
public $jsTranslators = ['lajax.t'];
/**
* @var string PHP Regular expression to match arrays containing language elements to translate.
*/
public $patternArrayTranslator = '#\@translate[^\$]+(?P<translator>[\w\d\s_]+[^\(\[]+)#s';
/**
* @var int The max_execution_time used when scanning, when set to null the default max_execution_time will not be modified.
*/
public $scanTimeLimit = null;
/**
* examples:
*
* ~~~
* $tables = [
* [
* 'connection' => 'db', // connection identifier.
* 'table' => '{{%language}}', // name of the database table to scan.
* 'columns' => ['name', 'name_ascii'] // fields to check.
* ],
* [
* 'connection' => 'db', // connection identifier.
* 'table' => '{{%post}}', // name of the database table to scan.
* 'columns' => ['title', 'description', 'content']// fields to check.
* ],
* ];
* ~~~
*
* @var array identifiers for the database tables containing language elements.
*/
public $tables;
/**
* @var string The database table storing the languages.
*/
public $languageTable = '{{%language}}';
/**
* @var string The search string to find empty translations.
*/
public $searchEmptyCommand = '!';
/**
* @var int The minimum status for a language to be selected by default in the export list.
*/
public $defaultExportStatus = 1;
/**
* @var string The default export format (yii\web\Response::FORMAT_JSON or yii\web\Response::FORMAT_XML).
*/
public $defaultExportFormat = Response::FORMAT_JSON;
/**
* @var string The default db connection
*/
public $connection = 'db';
/**
* @var array Scanners can be overriden here. If not set original set of scanners will be used from Scanner
*/
public $scanners = [];
/**
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if ($this->onlyCategories && $this->ignoredCategories) {
throw new InvalidConfigException("Please configure either 'ignoredCategories', or 'onlyCategories'!");
}
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if ($this->checkAccess()) {
return parent::beforeAction($action);
} else {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
}
/**
* @return bool whether the module can be accessed by the current user
*/
public function checkAccess()
{
$ip = Yii::$app->request->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
Yii::warning('Access to Translate is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
return false;
}
/**
* @return string The full path of the directory containing the generated JavaScript files.
*/
public function getLanguageItemsDirPath()
{
return Yii::getAlias($this->tmpDir) . $this->subDir;
}
}