1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: kix |
5
|
|
|
* Date: 03/06/14 |
6
|
|
|
* Time: 12:12 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace EMT; |
10
|
|
|
|
11
|
|
|
class AbstractTypograph |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $_text = ""; |
15
|
|
|
private $inited = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Список Трэтов, которые надо применить к типогрфированию |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $trets = array() ; |
23
|
|
|
protected $trets_index = array() ; |
24
|
|
|
protected $tret_objects = array() ; |
25
|
|
|
|
26
|
|
|
public $ok = false; |
27
|
|
|
public $debug_enabled = false; |
28
|
|
|
public $logging = false; |
29
|
|
|
public $logs = array(); |
30
|
|
|
public $errors = array(); |
31
|
|
|
public $debug_info = array(); |
32
|
|
|
|
33
|
|
|
private $use_layout = false; |
34
|
|
|
private $class_layout_prefix = false; |
35
|
|
|
private $use_layout_set = false; |
36
|
|
|
public $disable_notg_replace = false; |
37
|
|
|
public $remove_notg = false; |
38
|
|
|
|
39
|
|
|
public $settings = array(); |
40
|
|
|
|
41
|
|
|
protected function log($str, $data = null) |
42
|
|
|
{ |
43
|
|
|
if(!$this->logging) return; |
44
|
|
|
$this->logs[] = array('class' => '', 'info' => $str, 'data' => $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function tret_log($tret, $str, $data = null) |
48
|
|
|
{ |
49
|
|
|
$this->logs[] = array('class' => $tret, 'info' => $str, 'data' => $data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $data |
54
|
|
|
*/ |
55
|
|
|
protected function error($info, $data = null) |
56
|
|
|
{ |
57
|
|
|
$this->errors[] = array('class' => '', 'info' => $info, 'data' => $data); |
58
|
|
|
$this->log("ERROR $info", $data ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function tret_error($tret, $info, $data = null) |
62
|
|
|
{ |
63
|
|
|
$this->errors[] = array('class' => $tret, 'info' => $info, 'data' => $data); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function debug($class, $place, &$after_text, $after_text_raw = "") |
67
|
|
|
{ |
68
|
|
|
if(!$this->debug_enabled) return; |
69
|
|
|
$this->debug_info[] = array( |
70
|
|
|
'tret' => $class == $this ? false: true, |
71
|
|
|
'class' => is_object($class)? get_class($class) : $class, |
72
|
|
|
'place' => $place, |
73
|
|
|
'text' => $after_text, |
74
|
|
|
'text_raw' => $after_text_raw, |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected $_safe_blocks = array(); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Включить режим отладки, чтобы посмотреть последовательность вызовов |
82
|
|
|
* третов и правил после |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
public function debug_on() |
86
|
|
|
{ |
87
|
|
|
$this->debug_enabled = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Включить режим отладки, чтобы посмотреть последовательность вызовов |
92
|
|
|
* третов и правил после |
93
|
|
|
* |
94
|
|
|
*/ |
95
|
|
|
public function log_on() |
96
|
|
|
{ |
97
|
|
|
$this->logging = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Добавление защищенного блока |
102
|
|
|
* |
103
|
|
|
* <code> |
104
|
|
|
* Jare_Typograph_Tool::addCustomBlocks('<span>', '</span>'); |
105
|
|
|
* Jare_Typograph_Tool::addCustomBlocks('\<nobr\>', '\<\/span\>', true); |
106
|
|
|
* </code> |
107
|
|
|
* |
108
|
|
|
* @param string $id идентификатор |
109
|
|
|
* @param string $open начало блока |
110
|
|
|
* @param string $close конец защищенного блока |
111
|
|
|
* @param string $tag тэг |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
private function _add_safe_block($id, $open, $close, $tag) |
115
|
|
|
{ |
116
|
|
|
$this->_safe_blocks[$id] = array( |
117
|
|
|
'id' => $id, |
118
|
|
|
'tag' => $tag, |
119
|
|
|
'open' => $open, |
120
|
|
|
'close' => $close, |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Список защищенных блоков |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function get_all_safe_blocks() |
130
|
|
|
{ |
131
|
|
|
return $this->_safe_blocks; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Удаленного блока по его номеру ключа |
136
|
|
|
* |
137
|
|
|
* @param string $id идентифиактор защищённого блока |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function remove_safe_block($id) |
141
|
|
|
{ |
142
|
|
|
unset($this->_safe_blocks[$id]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Добавление защищенного блока |
147
|
|
|
* |
148
|
|
|
* @param string $tag тэг, который должен быть защищён |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
public function add_safe_tag($tag) |
152
|
|
|
{ |
153
|
|
|
$open = preg_quote("<", '/'). $tag."[^>]*?" . preg_quote(">", '/'); |
154
|
|
|
$close = preg_quote("</$tag>", '/'); |
155
|
|
|
$this->_add_safe_block($tag, $open, $close, $tag); |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Добавление защищенного блока |
162
|
|
|
* |
163
|
|
|
* @param string $open начало блока |
164
|
|
|
* @param string $close конец защищенного блока |
165
|
|
|
* @param bool $quoted специальные символы в начале и конце блока экранированы |
166
|
|
|
* @param string $id |
167
|
|
|
* @return boolean |
168
|
|
|
*/ |
169
|
|
|
public function add_safe_block($id, $open, $close, $quoted = false) |
170
|
|
|
{ |
171
|
|
|
$open = trim($open); |
172
|
|
|
$close = trim($close); |
173
|
|
|
|
174
|
|
|
if (empty($open) || empty($close)) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (false === $quoted) { |
179
|
|
|
$open = preg_quote($open, '/'); |
180
|
|
|
$close = preg_quote($close, '/'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->_add_safe_block($id, $open, $close, ""); |
184
|
|
|
|
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Сохранение содержимого защищенных блоков |
190
|
|
|
* |
191
|
|
|
* @param string $text |
192
|
|
|
* @param boolean $way |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function safe_blocks($text, $way, $show = true) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
if (count($this->_safe_blocks)) { |
198
|
|
|
$safeType = true === $way |
199
|
|
|
? function ($string) { |
200
|
|
|
return Util::encrypt_tag($string); |
201
|
|
|
} : function ($string) { |
202
|
|
|
return stripslashes(Util::decrypt_tag($string)); |
203
|
|
|
}; |
204
|
|
|
|
205
|
|
|
foreach ($this->_safe_blocks as $block) { |
206
|
|
|
$text = preg_replace_callback( |
207
|
|
|
"/({$block['open']})(.+?)({$block['close']})/s", |
208
|
|
|
function ($m) use ($safeType) { |
209
|
|
|
return $m[1] . $safeType($m[2]) . $m[3]; |
210
|
|
|
}, |
211
|
|
|
$text |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $text; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Декодирование блоков, которые были скрыты в момент типографирования |
221
|
|
|
* |
222
|
|
|
* @param string $text |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function decode_internal_blocks($text) |
226
|
|
|
{ |
227
|
|
|
return Util::decode_internal_blocks($text); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
private function create_object($tret) |
231
|
|
|
{ |
232
|
|
|
$obj = new $tret(); |
233
|
|
|
$obj->EMT = $this; |
234
|
|
|
$obj->logging = $this->logging; |
235
|
|
|
|
236
|
|
|
return $obj; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function get_short_tret($tretname) |
240
|
|
|
{ |
241
|
|
|
if (preg_match("/^EMT_Tret_([a-zA-Z0-9_]+)$/",$tretname, $m)) { |
242
|
|
|
return $m[1]; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $tretname; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
private function _init() |
249
|
|
|
{ |
250
|
|
|
foreach ($this->trets as $tret) { |
251
|
|
|
if(isset($this->tret_objects[$tret])) continue; |
252
|
|
|
$obj = $this->create_object($tret); |
253
|
|
|
if($obj == null) continue; |
254
|
|
|
$this->tret_objects[$tret] = $obj; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!$this->inited) { |
258
|
|
|
$this->add_safe_tag('pre'); |
259
|
|
|
$this->add_safe_tag('script'); |
260
|
|
|
$this->add_safe_tag('style'); |
261
|
|
|
$this->add_safe_tag('notg'); |
262
|
|
|
$this->add_safe_block('span-notg', '<span class="_notg_start"></span>', '<span class="_notg_end"></span>'); |
263
|
|
|
} |
264
|
|
|
$this->inited = true; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Инициализация класса, используется чтобы задать список третов или |
269
|
|
|
* спсиок защищённых блоков, которые можно использовать. |
270
|
|
|
* Такде здесь можно отменить защищённые блоки по умлочнаию |
271
|
|
|
* |
272
|
|
|
*/ |
273
|
|
|
public function init() |
274
|
|
|
{ |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Добавить Трэт, |
280
|
|
|
* |
281
|
|
|
* @param mixed $class - имя класса трета, или сам объект |
282
|
|
|
* @param string $altname - альтернативное имя, если хотим например иметь два одинаоковых терта в обработке |
283
|
|
|
* @return boolean |
284
|
|
|
*/ |
285
|
|
|
public function add_tret($class, $altname = false) |
286
|
|
|
{ |
287
|
|
|
if (is_object($class)) { |
288
|
|
|
if (!is_a($class, "EMT_Tret")) { |
289
|
|
|
$this->error("You are adding Tret that doesn't inherit base class EMT_Tret", get_class($class)); |
290
|
|
|
|
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$class->EMT = $this; |
295
|
|
|
$class->logging = $this->logging; |
296
|
|
|
$this->tret_objects[($altname ? $altname : get_class($class))] = $class; |
297
|
|
|
$this->trets[] = ($altname ? $altname : get_class($class)); |
298
|
|
|
|
299
|
|
|
return true; |
300
|
|
|
} |
301
|
|
|
if (is_string($class)) { |
302
|
|
|
$obj = $this->create_object($class); |
303
|
|
|
if($obj === null) |
304
|
|
|
|
305
|
|
|
return false; |
306
|
|
|
$this->tret_objects[($altname ? $altname : $class)] = $obj; |
307
|
|
|
$this->trets[] = ($altname ? $altname : $class); |
308
|
|
|
|
309
|
|
|
return true; |
310
|
|
|
} |
311
|
|
|
$this->error("Чтобы добавить трэт необходимо передать имя или объект"); |
312
|
|
|
|
313
|
|
|
return false; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Получаем ТРЕТ по идентификатору, т.е. названию класса |
318
|
|
|
* |
319
|
|
|
* @param string $name |
320
|
|
|
*/ |
321
|
|
|
public function get_tret($name) |
322
|
|
|
{ |
323
|
|
|
if (isset($this->tret_objects[$name])) { |
324
|
|
|
return $this->tret_objects[$name]; |
325
|
|
|
} |
326
|
|
|
foreach ($this->trets as $tret) { |
327
|
|
|
if ($tret == $name) { |
328
|
|
|
$this->_init(); |
329
|
|
|
|
330
|
|
|
return $this->tret_objects[$name]; |
331
|
|
|
} |
332
|
|
|
if ($this->get_short_tret($tret) == $name) { |
333
|
|
|
$this->_init(); |
334
|
|
|
|
335
|
|
|
return $this->tret_objects[$tret]; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
$this->error("Трэт с идентификатором $name не найден"); |
339
|
|
|
|
340
|
|
|
return false; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Задаём текст для применения типографа |
345
|
|
|
* |
346
|
|
|
* @param string $text |
347
|
|
|
*/ |
348
|
|
|
public function set_text($text) |
349
|
|
|
{ |
350
|
|
|
$this->_text = $text; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Запустить типограф на выполнение |
355
|
|
|
* |
356
|
|
|
*/ |
357
|
|
|
public function apply($trets = null) |
358
|
|
|
{ |
359
|
|
|
$this->ok = false; |
360
|
|
|
|
361
|
|
|
$this->init(); |
362
|
|
|
$this->_init(); |
363
|
|
|
|
364
|
|
|
$atrets = $this->trets; |
365
|
|
|
if(is_string($trets)) $atrets = array($trets); |
366
|
|
|
elseif(is_array($trets)) $atrets = $trets; |
367
|
|
|
|
368
|
|
|
$this->debug($this, 'init', $this->_text); |
369
|
|
|
|
370
|
|
|
$this->_text = $this->safe_blocks($this->_text, true); |
371
|
|
|
$this->debug($this, 'safe_blocks', $this->_text); |
372
|
|
|
|
373
|
|
|
$this->_text = Util::safe_tag_chars($this->_text, true); |
374
|
|
|
$this->debug($this, 'safe_tag_chars', $this->_text); |
375
|
|
|
|
376
|
|
|
$this->_text = Util::clear_special_chars($this->_text); |
|
|
|
|
377
|
|
|
$this->debug($this, 'clear_special_chars', $this->_text); |
378
|
|
|
|
379
|
|
|
foreach ($atrets as $tret) { |
380
|
|
|
// если установлен режим разметки тэгов то выставим его |
381
|
|
|
if($this->use_layout_set) |
382
|
|
|
$this->tret_objects[$tret]->set_tag_layout_ifnotset($this->use_layout); |
383
|
|
|
|
384
|
|
|
if($this->class_layout_prefix) |
385
|
|
|
$this->tret_objects[$tret]->set_class_layout_prefix($this->class_layout_prefix); |
386
|
|
|
|
387
|
|
|
// влючаем, если нужно |
388
|
|
|
if($this->debug_enabled) $this->tret_objects[$tret]->debug_on(); |
389
|
|
|
if($this->logging) $this->tret_objects[$tret]->logging = true; |
390
|
|
|
|
391
|
|
|
// применяем трэт |
392
|
|
|
//$this->tret_objects[$tret]->set_text(&$this->_text); |
393
|
|
|
$this->tret_objects[$tret]->set_text($this->_text); |
394
|
|
|
$this->tret_objects[$tret]->apply(); |
395
|
|
|
|
396
|
|
|
// соберём ошибки если таковые есть |
397
|
|
|
if(count($this->tret_objects[$tret]->errors)>0) |
398
|
|
|
foreach($this->tret_objects[$tret]->errors as $err ) |
399
|
|
|
$this->tret_error($tret, $err['info'], $err['data']); |
400
|
|
|
|
401
|
|
|
// логгирование |
402
|
|
|
if($this->logging) |
403
|
|
|
if(count($this->tret_objects[$tret]->logs)>0) |
404
|
|
|
foreach($this->tret_objects[$tret]->logs as $log ) |
405
|
|
|
$this->tret_log($tret, $log['info'], $log['data']); |
406
|
|
|
|
407
|
|
|
// отладка |
408
|
|
|
if($this->debug_enabled) |
409
|
|
|
foreach ($this->tret_objects[$tret]->debug_info as $di) { |
410
|
|
|
$unsafetext = $di['text']; |
411
|
|
|
$unsafetext = Util::safe_tag_chars($unsafetext, false); |
412
|
|
|
$unsafetext = $this->safe_blocks($unsafetext, false); |
413
|
|
|
$this->debug($tret, $di['place'], $unsafetext, $di['text']); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$this->_text = $this->decode_internal_blocks($this->_text); |
419
|
|
|
$this->debug($this, 'decode_internal_blocks', $this->_text); |
420
|
|
|
|
421
|
|
|
if ($this->is_on('dounicode')) { |
422
|
|
|
Util::convert_html_entities_to_unicode($this->_text); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$this->_text = Util::safe_tag_chars($this->_text, false); |
426
|
|
|
$this->debug($this, 'unsafe_tag_chars', $this->_text); |
427
|
|
|
|
428
|
|
|
$this->_text = $this->safe_blocks($this->_text, false); |
429
|
|
|
$this->debug($this, 'unsafe_blocks', $this->_text); |
430
|
|
|
|
431
|
|
|
if (!$this->disable_notg_replace) { |
432
|
|
|
$repl = array('<span class="_notg_start"></span>', '<span class="_notg_end"></span>'); |
433
|
|
|
if($this->remove_notg) $repl = ""; |
434
|
|
|
$this->_text = str_replace( array('<notg>','</notg>'), $repl , $this->_text); |
435
|
|
|
} |
436
|
|
|
$this->_text = trim($this->_text); |
437
|
|
|
$this->ok = (count($this->errors)==0); |
438
|
|
|
|
439
|
|
|
return $this->_text; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Получить содержимое <style></style> при использовании классов |
444
|
|
|
* |
445
|
|
|
* @param bool $list false - вернуть в виде строки для style или как массив |
446
|
|
|
* @param bool $compact не выводить пустые классы |
447
|
|
|
* @return string|array |
448
|
|
|
*/ |
449
|
|
|
public function get_style($list = false, $compact = false) |
450
|
|
|
{ |
451
|
|
|
$this->_init(); |
452
|
|
|
|
453
|
|
|
$res = array(); |
454
|
|
|
foreach ($this->trets as $tret) { |
455
|
|
|
$arr =$this->tret_objects[$tret]->classes; |
456
|
|
|
if(!is_array($arr)) continue; |
457
|
|
|
foreach ($arr as $classname => $str) { |
458
|
|
|
if(($compact) && (!$str)) continue; |
459
|
|
|
$clsname = ($this->class_layout_prefix ? $this->class_layout_prefix : "" ).(isset($this->tret_objects[$tret]->class_names[$classname]) ? $this->tret_objects[$tret]->class_names[$classname] :$classname); |
460
|
|
|
$res[$clsname] = $str; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
if($list) return $res; |
464
|
|
|
$str = ""; |
465
|
|
|
foreach ($res as $k => $v) { |
466
|
|
|
$str .= ".$k { $v }\n"; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
return $str; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Установить режим разметки, |
474
|
|
|
* Util::LAYOUT_STYLE - с помощью стилей |
475
|
|
|
* Util::LAYOUT_CLASS - с помощью классов |
476
|
|
|
* Util::LAYOUT_STYLE|Util::LAYOUT_CLASS - оба метода |
477
|
|
|
* |
478
|
|
|
* @param int $layout |
479
|
|
|
*/ |
480
|
|
|
public function set_tag_layout($layout = Util::LAYOUT_STYLE) |
481
|
|
|
{ |
482
|
|
|
$this->use_layout = $layout; |
|
|
|
|
483
|
|
|
$this->use_layout_set = true; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Установить префикс для классов |
488
|
|
|
* |
489
|
|
|
* @param string|bool $prefix если true то префикс 'emt_', иначе то, что передали |
490
|
|
|
*/ |
491
|
|
|
public function set_class_layout_prefix($prefix) |
492
|
|
|
{ |
493
|
|
|
$this->class_layout_prefix = $prefix === true ? "emt_" : $prefix; |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Включить/отключить правила, согласно карте |
498
|
|
|
* Формат карты: |
499
|
|
|
* 'Название трэта 1' => array ( 'правило1', 'правило2' , ... ) |
500
|
|
|
* 'Название трэта 2' => array ( 'правило1', 'правило2' , ... ) |
501
|
|
|
* |
502
|
|
|
* @param array $map |
503
|
|
|
* @param boolean $disable если ложно, то $map соотвествует тем правилам, которые надо включить |
504
|
|
|
* иначе это список правил, которые надо выключить |
505
|
|
|
* @param boolean $strict строго, т.е. те которые не в списку будут тоже обработаны |
506
|
|
|
*/ |
507
|
|
|
public function set_enable_map($map, $disable = false, $strict = true) |
508
|
|
|
{ |
509
|
|
|
if(!is_array($map)) return; |
510
|
|
|
$trets = array(); |
511
|
|
|
foreach ($map as $tret => $list) { |
512
|
|
|
$tretx = $this->get_tret($tret); |
513
|
|
|
if (!$tretx) { |
514
|
|
|
$this->log("Трэт $tret не найден при применении карты включаемых правил"); |
515
|
|
|
continue; |
516
|
|
|
} |
517
|
|
|
$trets[] = $tretx; |
518
|
|
|
|
519
|
|
|
if ($list === true) { // все |
520
|
|
|
$tretx->activate(array(), !$disable , true); |
521
|
|
|
} elseif (is_string($list)) { |
522
|
|
|
$tretx->activate(array($list), $disable , $strict); |
523
|
|
|
} elseif (is_array($list)) { |
524
|
|
|
$tretx->activate($list, $disable , $strict); |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
if ($strict) { |
528
|
|
|
foreach ($this->trets as $tret) { |
529
|
|
|
if(in_array($this->tret_objects[$tret], $trets)) continue; |
530
|
|
|
$this->tret_objects[$tret]->activate(array(), $disable , true); |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* Установлена ли настройка |
538
|
|
|
* |
539
|
|
|
* @param string $key |
540
|
|
|
*/ |
541
|
|
View Code Duplication |
public function is_on($key) |
|
|
|
|
542
|
|
|
{ |
543
|
|
|
if(!isset($this->settings[$key])) return false; |
544
|
|
|
$kk = $this->settings[$key]; |
545
|
|
|
|
546
|
|
|
return ((strtolower($kk)=="on") || ($kk === "1") || ($kk === true) || ($kk === 1)); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Установить настройку |
551
|
|
|
* |
552
|
|
|
* @param mixed $selector |
553
|
|
|
* @param mixed $value |
554
|
|
|
*/ |
555
|
|
|
protected function doset($selector, $key, $value) |
556
|
|
|
{ |
557
|
|
|
$tret_pattern = false; |
558
|
|
|
$rule_pattern = false; |
559
|
|
|
//if(($selector === false) || ($selector === null) || ($selector === false) || ($selector === "*")) $type = 0; |
560
|
|
|
if (is_string($selector)) { |
561
|
|
|
if (strpos($selector,".")===false) { |
562
|
|
|
$tret_pattern = $selector; |
563
|
|
|
} else { |
564
|
|
|
$pa = explode(".", $selector); |
565
|
|
|
$tret_pattern = $pa[0]; |
566
|
|
|
array_shift($pa); |
567
|
|
|
$rule_pattern = implode(".", $pa); |
568
|
|
|
} |
569
|
|
|
} |
570
|
|
|
Util::_process_selector_pattern($tret_pattern); |
571
|
|
|
Util::_process_selector_pattern($rule_pattern); |
572
|
|
|
if($selector == "*") $this->settings[$key] = $value; |
573
|
|
|
|
574
|
|
|
foreach ($this->trets as $tret) { |
575
|
|
|
$t1 = $this->get_short_tret($tret); |
576
|
|
|
if(!Util::_test_pattern($tret_pattern, $t1)) if(!Util::_test_pattern($tret_pattern, $tret)) continue; |
577
|
|
|
$tret_obj = $this->get_tret($tret); |
578
|
|
|
if ($key == "active") { |
579
|
|
|
foreach ($tret_obj->rules as $rulename => $v) { |
580
|
|
|
if(!Util::_test_pattern($rule_pattern, $rulename)) continue; |
581
|
|
View Code Duplication |
if((strtolower($value) === "on") || ($value===1) || ($value === true) || ($value=="1")) $tret_obj->enable_rule($rulename); |
|
|
|
|
582
|
|
View Code Duplication |
if((strtolower($value) === "off") || ($value===0) || ($value === false) || ($value=="0")) $tret_obj->disable_rule($rulename); |
|
|
|
|
583
|
|
|
} |
584
|
|
|
} else { |
585
|
|
|
if ($rule_pattern===false) { |
586
|
|
|
$tret_obj->set($key, $value); |
587
|
|
|
} else { |
588
|
|
|
foreach ($tret_obj->rules as $rulename => $v) { |
589
|
|
|
if(!Util::_test_pattern($rule_pattern, $rulename)) continue; |
590
|
|
|
$tret_obj->set_rule($rulename, $key, $value); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Установить настройки для тертов и правил |
599
|
|
|
* 1. если селектор является массивом, то тогда утсановка правил будет выполнена для каждого |
600
|
|
|
* элемента этого массива, как отдельного селектора. |
601
|
|
|
* 2. Если $key не является массивом, то эта настрока будет проставлена согласно селектору |
602
|
|
|
* 3. Если $key массив - то будет задана группа настроек |
603
|
|
|
* - если $value массив , то настройки определяются по ключам из массива $key, а значения из $value |
604
|
|
|
* - иначе, $key содержит ключ-значение как массив |
605
|
|
|
* |
606
|
|
|
* @param mixed $selector |
607
|
|
|
* @param mixed $key |
608
|
|
|
* @param mixed $value |
609
|
|
|
*/ |
610
|
|
|
public function set($selector, $key , $value = false) |
611
|
|
|
{ |
612
|
|
|
if (is_array($selector)) { |
613
|
|
|
foreach($selector as $val) $this->set($val, $key, $value); |
614
|
|
|
|
615
|
|
|
return; |
616
|
|
|
} |
617
|
|
|
if (is_array($key)) { |
618
|
|
|
foreach ($key as $x => $y) { |
619
|
|
|
if (is_array($value)) { |
620
|
|
|
$kk = $y; |
621
|
|
|
$vv = $value[$x]; |
622
|
|
|
} else { |
623
|
|
|
$kk = $x; |
624
|
|
|
$vv = $y; |
625
|
|
|
} |
626
|
|
|
$this->set($selector, $kk, $vv); |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
$this->doset($selector, $key, $value); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Возвращает список текущих третов, которые установлены |
634
|
|
|
* |
635
|
|
|
*/ |
636
|
|
|
public function get_trets_list() |
637
|
|
|
{ |
638
|
|
|
return $this->trets; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Установка одной метанастройки |
643
|
|
|
* |
644
|
|
|
* @param string $name |
645
|
|
|
* @param mixed $value |
646
|
|
|
*/ |
647
|
|
|
public function do_setup($name, $value) |
648
|
|
|
{ |
649
|
|
|
|
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Установить настройки |
654
|
|
|
* |
655
|
|
|
* @param array $setupmap |
656
|
|
|
*/ |
657
|
|
|
public function setup($setupmap = array()) |
658
|
|
|
{ |
659
|
|
|
if(!is_array($setupmap)) return; |
660
|
|
|
|
661
|
|
|
if (isset($setupmap['map']) || isset($setupmap['maps'])) { |
662
|
|
|
if (isset($setupmap['map'])) { |
663
|
|
|
// $ret['map'] = $test['params']['map']; |
664
|
|
|
// $ret['disable'] = $test['params']['map_disable']; |
665
|
|
|
// $ret['strict'] = $test['params']['map_strict']; |
666
|
|
|
// $test['params']['maps'] = array($ret); |
667
|
|
|
unset($setupmap['map']); |
668
|
|
|
unset($setupmap['map_disable']); |
669
|
|
|
unset($setupmap['map_strict']); |
670
|
|
|
} |
671
|
|
|
if (is_array($setupmap['maps'])) { |
672
|
|
|
foreach ($setupmap['maps'] as $map) { |
673
|
|
|
$this->set_enable_map |
674
|
|
|
($map['map'], |
675
|
|
|
isset($map['disable']) ? $map['disable'] : false, |
676
|
|
|
isset($map['strict']) ? $map['strict'] : false |
677
|
|
|
); |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
unset($setupmap['maps']); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
foreach($setupmap as $k => $v) $this->do_setup($k , $v); |
684
|
|
|
} |
685
|
|
|
} |
686
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.