@@ -80,7 +80,9 @@ protected function debug($class, $place, &$after_text, $after_text_raw = "")
80
80
81
81
82
82
83
- protected $ _safe_blocks = array ();
83
+ protected $ _safe_blocks = array ();
84
+ protected $ _safe_sequences = array ();
85
+ protected $ _safe_sequence_mark = "SAFESEQUENCENUM " ;
84
86
85
87
86
88
/**
@@ -127,6 +129,38 @@ private function _add_safe_block($id, $open, $close, $tag)
127
129
);
128
130
}
129
131
132
+ /**
133
+ * Добавление защищенного блока
134
+ *
135
+ * @param string $type тип последовательности
136
+ * 0 - URL
137
+ * 1 - почта
138
+ * @param string $content реальное содержимое
139
+ * @return void
140
+ */
141
+ private function _add_safe_sequence ($ type , $ content )
142
+ {
143
+ $ this ->_safe_sequences [] = array (
144
+ 'type ' => $ type ,
145
+ 'content ' => $ content ,
146
+ );
147
+ }
148
+
149
+ /**
150
+ * Вычисляем тэг, которого нет в заданном тексте
151
+ *
152
+ * @return array
153
+ */
154
+ protected function detect_safe_mark () {
155
+ $ seq = $ this ->_safe_sequence_mark ;
156
+ $ i = 0 ;
157
+ while (strpos ($ this ->_text , $ seq ) !== false ) {
158
+ $ seq = str_replace ("SAFESEQUENCENUM " ,"SAFESEQUENCE " .$ i ."NUM " , $ this ->_safe_sequence_mark );
159
+ $ i ++;
160
+ }
161
+ $ this ->_safe_sequence_mark = $ seq ;
162
+ }
163
+
130
164
/**
131
165
* Список защищенных блоков
132
166
*
@@ -137,6 +171,16 @@ public function get_all_safe_blocks()
137
171
return $ this ->_safe_blocks ;
138
172
}
139
173
174
+ /**
175
+ * Список защищенных последовательностей
176
+ *
177
+ * @return array
178
+ */
179
+ public function get_all_safe_sequences ()
180
+ {
181
+ return $ this ->_safe_sequences ;
182
+ }
183
+
140
184
/**
141
185
* Удаленного блока по его номеру ключа
142
186
*
@@ -150,7 +194,6 @@ public function remove_safe_block($id)
150
194
}
151
195
}
152
196
153
-
154
197
/**
155
198
* Добавление защищенного блока
156
199
*
@@ -217,6 +260,81 @@ public function safe_blocks($text, $way, $show = true)
217
260
return $ text ;
218
261
}
219
262
263
+ /**
264
+ * Кодирование УРЛа
265
+ *
266
+ * @param regex array $m
267
+ * @return unknown
268
+ */
269
+ function safe_sequence_url ($ m ) {
270
+ $ id = count ($ this ->_safe_sequences );
271
+ $ this ->_add_safe_sequence (0 , $ m [0 ]);
272
+ return "http://mdash.ru/A0 " .$ this ->_safe_sequence_mark .$ id ."ID " ;
273
+ }
274
+
275
+ /**
276
+ * Кодирование Почты
277
+ *
278
+ * @param regex array $m
279
+ * @return unknown
280
+ */
281
+ function safe_sequence_email ($ m ) {
282
+ $ id = count ($ this ->_safe_sequences );
283
+ $ this ->_add_safe_sequence (1 , $ m [0 ]);
284
+ return "A1 " .
$ this ->
_safe_sequence_mark .
$ id.
"[email protected] " ;
285
+ }
286
+
287
+ /**
288
+ * Декодирование УРЛа
289
+ *
290
+ * @param regex array $m
291
+ * @return unknown
292
+ */
293
+ function unsafe_sequence_url ($ m ) {
294
+ return $ this ->_safe_sequences [$ m [1 ]]['content ' ];
295
+ }
296
+
297
+ /**
298
+ * Декодирование УРЛа с удалением http://
299
+ *
300
+ * @param regex array $m
301
+ * @return unknown
302
+ */
303
+ function unsafe_sequence_url_nohttp ($ m ) {
304
+ $ z = $ this ->_safe_sequences [$ m [1 ]]['content ' ];
305
+ return preg_replace ("~([^:]+)://~ " , "" , $ z );
306
+ }
307
+
308
+
309
+ /**
310
+ * Декодирование Почты
311
+ *
312
+ * @param regex array $m
313
+ * @return unknown
314
+ */
315
+ function unsafe_sequence_email ($ m ) {
316
+ return $ this ->_safe_sequences [$ m [1 ]]['content ' ];
317
+ }
318
+
319
+ /**
320
+ * Сохранение защищенных последовательностей
321
+ *
322
+ * @param string $text
323
+ * @param bool $safe если true, то содержимое блоков будет сохранено, иначе - раскодировано.
324
+ * @return string
325
+ */
326
+ public function safe_sequences ($ text , $ way , $ show = true )
327
+ {
328
+ if (true === $ way ) {
329
+ $ text = preg_replace_callback (EMT_Lib::url_regex (), array ($ this , "safe_sequence_url " ) , $ text );
330
+ $ text = preg_replace_callback (EMT_Lib::email_regex (), array ($ this , "safe_sequence_email " ) , $ text );
331
+ } else {
332
+ $ text = preg_replace_callback ('~http://mdash.ru/A0 ' .$ this ->_safe_sequence_mark .'(\d+)ID~ims ' , array ($ this , "unsafe_sequence_url " ) , $ text );
333
+ $ text = preg_replace_callback ('~mdash.ru/A0 ' .$ this ->_safe_sequence_mark .'(\d+)ID~ims ' , array ($ this , "unsafe_sequence_url_nohttp " ) , $ text );
334
+ $ text =
preg_replace_callback (
'~A1 ' .
$ this ->
_safe_sequence_mark .
'(\d+)[email protected] ~ims ' ,
array (
$ this ,
"unsafe_sequence_email " ) ,
$ text);
335
+ }
336
+ return $ text ;
337
+ }
220
338
221
339
/**
222
340
* Декодирование блоков, которые были скрыты в момент типографирования
@@ -287,6 +405,8 @@ private function _init()
287
405
$ this ->add_safe_block ('span-notg ' , '<span class="_notg_start"></span> ' , '<span class="_notg_end"></span> ' );
288
406
}
289
407
$ this ->inited = true ;
408
+
409
+ $ this ->detect_safe_mark ();
290
410
}
291
411
292
412
@@ -394,6 +514,9 @@ public function apply($trets = null)
394
514
395
515
$ this ->debug ($ this , 'init ' , $ this ->_text );
396
516
517
+ $ this ->_text = $ this ->safe_sequences ($ this ->_text , true );
518
+ $ this ->debug ($ this , 'safe_sequences ' , $ this ->_text );
519
+
397
520
$ this ->_text = $ this ->safe_blocks ($ this ->_text , true );
398
521
$ this ->debug ($ this , 'safe_blocks ' , $ this ->_text );
399
522
@@ -403,6 +526,7 @@ public function apply($trets = null)
403
526
$ this ->_text = EMT_Lib::clear_special_chars ($ this ->_text );
404
527
$ this ->debug ($ this , 'clear_special_chars ' , $ this ->_text );
405
528
529
+
406
530
foreach ($ atrets as $ tret )
407
531
{
408
532
// если установлен режим разметки тэгов то выставим его
@@ -460,6 +584,9 @@ public function apply($trets = null)
460
584
$ this ->_text = $ this ->safe_blocks ($ this ->_text , false );
461
585
$ this ->debug ($ this , 'unsafe_blocks ' , $ this ->_text );
462
586
587
+ $ this ->_text = $ this ->safe_sequences ($ this ->_text , false );
588
+ $ this ->debug ($ this , 'unsafe_sequences ' , $ this ->_text );
589
+
463
590
if (!$ this ->disable_notg_replace )
464
591
{
465
592
$ repl = array ('<span class="_notg_start"></span> ' , '<span class="_notg_end"></span> ' );
0 commit comments