PluginProbe
DoLogin Security / trunk
DoLogin Security vtrunk
5.0.10 4.8.3 trunk 1.0 1.1 1.1.1 1.2 1.2.1 1.2.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 1.6 All 64 releases
dologin / src / s.cls.php

s.cls.php in DoLogin Security trunk, at src/s.cls.php

791 lines 15.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * String handler
4 *
5 * @since 1.3
6 */
7 namespace dologin;
8
9 defined( 'WPINC' ) || exit;
10
11 class s {
12 // xml load sanitize
13 public static function sanitizeXml( $content ) {
14 if ( ! $content ) {
15 return '';
16 }
17 $invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
18 return preg_replace( $invalid_characters, '', $content );
19 }
20
21 // id2shortURL
22 public static function num2code( $id ) {
23 static $_charArr, $_len;
24 if ( ! $_charArr ) {
25 $_charArr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
26 $_len = strlen( $_charArr );
27 }
28 $url = $id >= $_len ? self::num2code( floor( $id / $_len ) ) : '';
29 $url .= $_charArr[ $id % $_len ];
30 return $url;
31 }
32
33 public static function code2num( $url ) {
34 static $_charArr, $_len;
35 if ( ! $_charArr ) {
36 $_charArr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
37 $_len = strlen( $_charArr );
38 }
39 $id = pow( $_len, strlen( $url ) - 1 ) * strpos( $_charArr, substr( $url, 0, 1 ) );
40 if ( strlen( $url ) > 1 ) {
41 $id += self::code2num( substr( $url, 1 ) );
42 }
43 return $id;
44 }
45
46 // parse bbcode
47 public static function bbcode( $str ) {
48 $search = array(
49 '~\[ul\]~',
50 '~\[ol\]~',
51 "~\n*\[li\]~",
52 "~\[/li\]\n*~",
53 "~\n*\[/ul\]\s{0,2}~",
54 "~\n*\[/ol\]\s{0,2}~",
55
56 '~\[/td\][^\[]*~',
57 '~\[/tr\][^\[]*~',
58 '~\[table\]~',
59 '~\[tr\]~',
60 '~\[td\]~',
61 '~\[/table\]~',
62 '~\[hr\]~',
63 '~\[b\]~',
64 '~\[/b\]~',
65 '~\[i\]~',
66 '~\[/i\]~',
67 '~\[u\]~',
68 '~\[/u\]~',
69 '~\[left\]~',
70 '~\[/left\]\s{0,2}~',
71 '~\[center\]~',
72 '~\[/center\]\s{0,2}~',
73 '~\[right\]~',
74 '~\[/right\]\s{0,2}~',
75 '~\[size=(\d+)\]~isU',
76 '~\[/size\]~',
77 '~\[color=([^\]]+)\]~isU',
78 '~\[/color\]~',
79 '~\[font=([^\]]+)\]~isU',
80 '~\[/font\]~',
81 '~\[url=([^\]]+)\]~isU',
82 '~\[/url\]~',
83 '~\[youtube\]([^\[]+)\[/youtube\]~',
84 '~\[iframe\]([^\[]+)\[/iframe\]~',
85 '~\[mp4\]([^\[]+)\[/mp4\]~',
86 "~\n~",
87 );
88
89 $replace = array(
90 '<ul>',
91 '<ol>',
92 '<li>',
93 '</li>',
94 '</ul>',
95 '</ol>',
96
97 '</td>',
98 '</tr>',
99 '<table class="table table-bordered table-striped">',
100 '<tr>',
101 '<td>',
102 '</table>',
103 '<hr />',
104 '<strong>',
105 '</strong>',
106 '<em>',
107 '</em>',
108 '<u>',
109 '</u>',
110 '<p>',
111 '</p>',
112 '<p class="text-center">',
113 '</p>',
114 '<p class="text-right">',
115 '</p>',
116 '<font size="$1">',
117 '</font>',
118 '<font color="$1">',
119 '</font>',
120 '<font face="$1">',
121 '</font>',
122 '<a href="$1" target="_blank">',
123 '</a>',
124 '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>',
125 '<iframe width="640" height="360" src="$1" frameborder="0" allowfullscreen></iframe>',
126 '<video style="max-width:700px;"><source type="video/mp4" src="$1"></video>',
127 '<br />',
128 );
129 return preg_replace( $search, $replace, $str );
130 }
131
132 /**
133 * XML parser
134 */
135 public static function xml2arr( $xml ) {
136 $values = array();
137 $index = array();
138 $array = array();
139 $parser = xml_parser_create();
140 xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
141 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
142 xml_parse_into_struct( $parser, $xml, $values, $index );
143 xml_parser_free( $parser ); // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated -- required for xml parsing on older PHP.
144 $i = 0;
145 $name = $values[ $i ]['tag'];
146 $array[ $name ] = isset( $values[ $i ]['attributes'] ) ? $values[ $i ]['attributes'] : '';
147 $array[ $name ] = self::_xml2arr( $values, $i );
148 return $array;
149 }
150
151 private static function _xml2arr( $values, &$i ) {
152 $child = array();
153 if ( isset( $values[ $i ]['value'] ) ) {
154 array_push( $child, $values[ $i ]['value'] );
155 }
156 while ( $i++ < count( $values ) ) {
157 switch ( $values[ $i ]['type'] ) {
158 case 'cdata':
159 array_push( $child, $values[ $i ]['value'] );
160 break;
161 case 'complete':
162 $name = $values[ $i ]['tag'];
163 if ( ! empty( $name ) ) {
164 if ( isset( $child[ $name ] ) ) {
165 if ( ! isset( $child[ $name ][0] ) ) {
166 $child[ $name ] = array( $child[ $name ] );
167 }
168 if ( isset( $values[ $i ]['attributes'] ) ) {
169 $child[ $name ][] = $values[ $i ]['attributes'];
170 }
171 } else {
172 $child[ $name ] = ! empty( $values[ $i ]['value'] ) ? $values[ $i ]['value'] : '';
173 if ( isset( $values[ $i ]['attributes'] ) ) {
174 $child[ $name ] = $values[ $i ]['attributes'];
175 }
176 }
177 }
178 break;
179 case 'open':
180 $name = $values[ $i ]['tag'];
181 $size = isset( $child[ $name ] ) ? count( $child[ $name ] ) : 0;
182 $child[ $name ][ $size ] = self::_xml2arr( $values, $i );
183 break;
184 case 'close':
185 return $child;
186 }
187 }
188 return $child;
189 }
190
191 /**
192 * code convert
193 */
194 public static function conv( $text, $from = 'utf-8', $to = 'gbk' ) {
195 if ( is_array( $text ) ) {
196 foreach ( $text as $key => $val ) {
197 $text[ $key ] = self::conv( $val, $from, $to );
198 }
199 } else {
200 $text = mb_convert_encoding( $text, $to, $from );
201 }
202 return $text;
203 }
204
205 /**
206 * sub str
207 */
208 public static function rsubstr( $string, $len, $add = 0 ) {
209 $str2 = mb_substr( $string, 0, $len, 'utf-8' );
210 if ( $add != 0 ) {
211 ++$add;
212 }
213 if ( strlen( $str2 ) < strlen( $string ) && $add != 0 ) {
214 ++$add;
215 $leftchars = floor( ( $len * 2 - self::len( $str2 ) ) / 2 );
216 if ( $leftchars > 0 ) {
217 $str2 .= '...';
218 }
219 }
220 return $str2;
221 }
222
223 /**
224 * strlen
225 */
226 public static function len( $string, $charNum = 0 ) {
227 if ( ! $charNum ) {
228 return strlen( self::conv( $string ) );
229 }
230 return mb_strlen( $string, 'utf-8' );
231 }
232
233 /**
234 * br
235 */
236 public static function htmlBr( $string ) {
237 if ( is_array( $string ) ) {
238 foreach ( $string as $k => $v ) {
239 $string[ $k ] = self::htmlBr( $v );
240 }
241 } else {
242 $string = str_replace( array( ' ', "\r" . "\n", "\n", "\t" ), array( '&nbsp;&nbsp;', '<br />', '<br />', '&nbsp;&nbsp;&nbsp;&nbsp;' ), $string );
243 }
244 return $string;
245 }
246
247 /**
248 * HTML
249 */
250 public static function html( $string, $showBr = true, $noConv = array() ) {
251 if ( ! $string ) {
252 return $string;
253 }
254 if ( ! is_array( $noConv ) ) {
255 $noConv = array( $noConv );
256 }
257 if ( is_array( $string ) ) {
258 foreach ( $string as $k => $v ) {
259 if ( isInt( $k ) || ! in_array( $k, $noConv ) ) {
260 $string[ $k ] = self::html( $v, $showBr, $noConv );
261 }
262 }
263 } else {
264 $string = str_replace( array( '&', '<', '>', '"', "'", '\\' ), array( '&amp;', '&lt;', '&gt;', '&quot;', '&#039;', '&#092;' ), $string );
265 }
266 if ( ! is_array( $string ) && $showBr ) {
267 return self::htmlBr( $string );
268 }
269 return $string;
270 }
271
272 /**
273 * convert str to color
274 */
275 public static function color( $str, $returnColor = false ) {
276 static $_color;
277 if ( ! $str ) {
278 return false;
279 }
280 if ( ! $_color ) {
281 $_color = self::colorArr2();// 90
282 }
283
284 $str2 = hexdec( substr( md5( $str ), -3 ) ) % count( $_color );
285 if ( $returnColor ) {
286 return $_color[ $str2 ];
287 }
288 return "<font color='" . $_color[ $str2 ] . "'>$str</font>";
289 }
290
291 /**
292 * Random
293 */
294 public static function rrand( $len, $type = 7 ) {
295 switch ( $type ) {
296 case 0:
297 $charlist = '012';
298 break;
299
300 case 1:
301 $charlist = '0123456789';
302 break;
303
304 case 2:
305 $charlist = 'abcdefghijklmnopqrstuvwxyz';
306 break;
307
308 case 3:
309 $charlist = '0123456789abcdefghijklmnopqrstuvwxyz';
310 break;
311
312 case 4:
313 $charlist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
314 break;
315
316 case 5:
317 $charlist = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
318 break;
319
320 case 6:
321 $charlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
322 break;
323
324 case 7:
325 $charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
326 break;
327
328 }
329
330 $str = '';
331
332 // Use a cryptographically secure RNG for tokens/OTP codes (mt_rand + time-based seed is predictable)
333 $max = strlen( $charlist ) - 1;
334 for ( $i = 0; $i < $len; $i++ ) {
335 $str .= $charlist[ random_int( 0, $max ) ];
336 }
337
338 return $str;
339 }
340
341 /**
342 * seeable color
343 */
344 private static function colorArr2() {
345 return array(
346 '#000000',
347 '#38B0DE',
348 '#00FF00',
349 '#0000FF',
350 '#FF00FF',
351 '#00FFFF',
352 '#70DB93',
353 '#5C3317',
354 '#9F5F9F',
355 '#B5A642',
356 '#D9D919',
357 '#A67D3D',
358 '#8C7853',
359 '#A67D3D',
360 '#5F9F9F',
361 '#D98719',
362 '#B87333',
363 '#FF7F00',
364 '#42426F',
365 '#5C4033',
366 '#4A766E',
367 '#4F4F2F',
368 '#9932CD',
369 '#871F78',
370 '#6B238E',
371 '#2F4F4F',
372 '#97694F',
373 '#7093DB',
374 '#855E42',
375 '#545454',
376 '#856363',
377 '#D19275',
378 '#8E2323',
379 '#238E23',
380 '#CD7F32',
381 '#527F76',
382 '#93DB70',
383 '#215E21',
384 '#4E2F2F',
385 '#9F9F5F',
386 '#32CD32',
387 '#E47833',
388 '#8E236B',
389 '#32CD99',
390 '#3232CD',
391 '#6B8E23',
392 '#9370DB',
393 '#426F42',
394 '#7F00FF',
395 '#7FFF00',
396 '#70DBDB',
397 '#DB7093',
398 '#A68064',
399 '#2F2F4F',
400 '#23238E',
401 '#4D4DFF',
402 '#FF6EC7',
403 '#00009C',
404 '#CFB53B',
405 '#FF7F00',
406 '#236B8E',
407 '#DB70DB',
408 '#8FBC8F',
409 '#BC8F8F',
410 '#EAADEA',
411 '#5959AB',
412 '#6F4242',
413 '#00FF7F',
414 '#238E68',
415 '#6B4226',
416 '#8E6B23',
417 '#3299CC',
418 '#007FFF',
419 '#FF1CAE',
420 );
421 }
422
423 /**
424 * seeable color
425 */
426 private static function colorArr() {
427 return array(
428 array(
429 'color' => '255 0 0',
430 'hexcolor' => '#FF0000',
431 ),
432 array(
433 'color' => '0 255 0',
434 'hexcolor' => '#00FF00',
435 ),
436 array(
437 'color' => '0 0 255',
438 'hexcolor' => '#0000FF',
439 ),
440 array(
441 'color' => '255 0 255',
442 'hexcolor' => '#FF00FF',
443 ),
444 array(
445 'color' => '0 255 255',
446 'hexcolor' => '#00FFFF',
447 ),
448 array(
449 'color' => '255 255 0',
450 'hexcolor' => '#FFFF00',
451 ),
452 array(
453 'color' => '112 219 147',
454 'hexcolor' => '#70DB93',
455 ),
456 array(
457 'color' => '255 255 255',
458 'hexcolor' => '#FFFFFF',
459 ),
460 array(
461 'color' => '0 0 0',
462 'hexcolor' => '#000000',
463 ),
464 array(
465 'color' => '92 51 23',
466 'hexcolor' => '#5C3317',
467 ),
468 array(
469 'color' => '159 95 159',
470 'hexcolor' => '#9F5F9F',
471 ),
472 array(
473 'color' => '181 166 66',
474 'hexcolor' => '#B5A642',
475 ),
476 array(
477 'color' => '217 217 25',
478 'hexcolor' => '#D9D919',
479 ),
480 array(
481 'color' => '166 125 61',
482 'hexcolor' => '#A67D3D',
483 ),
484 array(
485 'color' => '140 120 83',
486 'hexcolor' => '#8C7853',
487 ),
488 array(
489 'color' => '166 125 61',
490 'hexcolor' => '#A67D3D',
491 ),
492 array(
493 'color' => '95 159 159',
494 'hexcolor' => '#5F9F9F',
495 ),
496 array(
497 'color' => '217 135 25',
498 'hexcolor' => '#D98719',
499 ),
500 array(
501 'color' => '184 115 51',
502 'hexcolor' => '#B87333',
503 ),
504 array(
505 'color' => '255 127 0',
506 'hexcolor' => '#FF7F00',
507 ),
508 array(
509 'color' => '66 66 111',
510 'hexcolor' => '#42426F',
511 ),
512 array(
513 'color' => '92 64 51',
514 'hexcolor' => '#5C4033',
515 ),
516 array(
517 'color' => '47 79 47',
518 'hexcolor' => '#2F4F2F',
519 ),
520 array(
521 'color' => '74 118 110',
522 'hexcolor' => '#4A766E',
523 ),
524 array(
525 'color' => '79 79 47',
526 'hexcolor' => '#4F4F2F',
527 ),
528 array(
529 'color' => '153 50 205',
530 'hexcolor' => '#9932CD',
531 ),
532 array(
533 'color' => '135 31 120',
534 'hexcolor' => '#871F78',
535 ),
536 array(
537 'color' => '107 35 142',
538 'hexcolor' => '#6B238E',
539 ),
540 array(
541 'color' => '47 79 79',
542 'hexcolor' => '#2F4F4F',
543 ),
544 array(
545 'color' => '151 105 79',
546 'hexcolor' => '#97694F',
547 ),
548 array(
549 'color' => '112 147 219',
550 'hexcolor' => '#7093DB',
551 ),
552 array(
553 'color' => '133 94 66',
554 'hexcolor' => '#855E42',
555 ),
556 array(
557 'color' => '84 84 84',
558 'hexcolor' => '#545454',
559 ),
560 array(
561 'color' => '133 99 99',
562 'hexcolor' => '#856363',
563 ),
564 array(
565 'color' => '209 146 117',
566 'hexcolor' => '#D19275',
567 ),
568 array(
569 'color' => '142 35 35',
570 'hexcolor' => '#8E2323',
571 ),
572 array(
573 'color' => '35 142 35',
574 'hexcolor' => '#238E23',
575 ),
576 array(
577 'color' => '205 127 50',
578 'hexcolor' => '#CD7F32',
579 ),
580 array(
581 'color' => '219 219 112',
582 'hexcolor' => '#DBDB70',
583 ),
584 array(
585 'color' => '192 192 192',
586 'hexcolor' => '#C0C0C0',
587 ),
588 array(
589 'color' => '82 127 118',
590 'hexcolor' => '#527F76',
591 ),
592 array(
593 'color' => '147 219 112',
594 'hexcolor' => '#93DB70',
595 ),
596 array(
597 'color' => '33 94 33',
598 'hexcolor' => '#215E21',
599 ),
600 array(
601 'color' => '78 47 47',
602 'hexcolor' => '#4E2F2F',
603 ),
604 array(
605 'color' => '159 159 95',
606 'hexcolor' => '#9F9F5F',
607 ),
608 array(
609 'color' => '192 217 217',
610 'hexcolor' => '#C0D9D9',
611 ),
612 array(
613 'color' => '168 168 168',
614 'hexcolor' => '#A8A8A8',
615 ),
616 array(
617 'color' => '143 143 189',
618 'hexcolor' => '#8F8FBD',
619 ),
620 array(
621 'color' => '233 194 166',
622 'hexcolor' => '#E9C2A6',
623 ),
624 array(
625 'color' => '50 205 50',
626 'hexcolor' => '#32CD32',
627 ),
628 array(
629 'color' => '228 120 51',
630 'hexcolor' => '#E47833',
631 ),
632 array(
633 'color' => '142 35 107',
634 'hexcolor' => '#8E236B',
635 ),
636 array(
637 'color' => '50 205 153',
638 'hexcolor' => '#32CD99',
639 ),
640 array(
641 'color' => '50 50 205',
642 'hexcolor' => '#3232CD',
643 ),
644 array(
645 'color' => '107 142 35',
646 'hexcolor' => '#6B8E23',
647 ),
648 array(
649 'color' => '234 234 174',
650 'hexcolor' => '#EAEAAE',
651 ),
652 array(
653 'color' => '147 112 219',
654 'hexcolor' => '#9370DB',
655 ),
656 array(
657 'color' => '66 111 66',
658 'hexcolor' => '#426F42',
659 ),
660 array(
661 'color' => '127 0 255',
662 'hexcolor' => '#7F00FF',
663 ),
664 array(
665 'color' => '127 255 0',
666 'hexcolor' => '#7FFF00',
667 ),
668 array(
669 'color' => '112 219 219',
670 'hexcolor' => '#70DBDB',
671 ),
672 array(
673 'color' => '219 112 147',
674 'hexcolor' => '#DB7093',
675 ),
676 array(
677 'color' => '166 128 100',
678 'hexcolor' => '#A68064',
679 ),
680 array(
681 'color' => '47 47 79',
682 'hexcolor' => '#2F2F4F',
683 ),
684 array(
685 'color' => '35 35 142',
686 'hexcolor' => '#23238E',
687 ),
688 array(
689 'color' => '77 77 255',
690 'hexcolor' => '#4D4DFF',
691 ),
692 array(
693 'color' => '255 110 199',
694 'hexcolor' => '#FF6EC7',
695 ),
696 array(
697 'color' => '0 0 156',
698 'hexcolor' => '#00009C',
699 ),
700 array(
701 'color' => '235 199 158',
702 'hexcolor' => '#EBC79E',
703 ),
704 array(
705 'color' => '207 181 59',
706 'hexcolor' => '#CFB53B',
707 ),
708 array(
709 'color' => '255 127 0',
710 'hexcolor' => '#FF7F00',
711 ),
712 array(
713 'color' => '255 36 0',
714 'hexcolor' => '#FF2400',
715 ),
716 array(
717 'color' => '219 112 219',
718 'hexcolor' => '#DB70DB',
719 ),
720 array(
721 'color' => '143 188 143',
722 'hexcolor' => '#8FBC8F',
723 ),
724 array(
725 'color' => '188 143 143',
726 'hexcolor' => '#BC8F8F',
727 ),
728 array(
729 'color' => '234 173 234',
730 'hexcolor' => '#EAADEA',
731 ),
732 array(
733 'color' => '217 217 243',
734 'hexcolor' => '#D9D9F3',
735 ),
736 array(
737 'color' => '89 89 171',
738 'hexcolor' => '#5959AB',
739 ),
740 array(
741 'color' => '111 66 66',
742 'hexcolor' => '#6F4242',
743 ),
744 array(
745 'color' => '188 23 23',
746 'hexcolor' => '#BC1717',
747 ),
748 array(
749 'color' => '35 142 104',
750 'hexcolor' => '#238E68',
751 ),
752 array(
753 'color' => '107 66 38',
754 'hexcolor' => '#6B4226',
755 ),
756 array(
757 'color' => '142 107 35',
758 'hexcolor' => '#8E6B23',
759 ),
760 array(
761 'color' => '230 232 250',
762 'hexcolor' => '#E6E8FA',
763 ),
764 array(
765 'color' => '50 153 204',
766 'hexcolor' => '#3299CC',
767 ),
768 array(
769 'color' => '0 127 255',
770 'hexcolor' => '#007FFF',
771 ),
772 array(
773 'color' => '255 28 174',
774 'hexcolor' => '#FF1CAE',
775 ),
776 array(
777 'color' => '0 255 127',
778 'hexcolor' => '#00FF7F',
779 ),
780 array(
781 'color' => '35 107 142',
782 'hexcolor' => '#236B8E',
783 ),
784 array(
785 'color' => '56 176 222',
786 'hexcolor' => '#38B0DE',
787 ),
788 );
789 }
790 }
791