This repository was archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenFunctions.php
More file actions
507 lines (447 loc) · 13.1 KB
/
GenFunctions.php
File metadata and controls
507 lines (447 loc) · 13.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/*
This file is part of Peachy MediaWiki Bot API
Peachy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Stores general functions that do not belong in a class
*/
/**
* Case insensitive in_array function
*
* @param mixed $needle What to search for
* @param array $haystack Array to search in
* @param bool $strict
* @return bool True if $needle is found in $haystack, case insensitive
* @link http://us3.php.net/in_array
*/
function iin_array( $needle, $haystack, $strict = false ) {
return in_array_recursive( strtoupper_safe( $needle ), array_map( 'strtoupper_safe', $haystack ), $strict );
}
/**
* @return string
*/
function strtoupper_safe( $str ) {
if( is_string( $str ) ) return strtoupper( $str );
if( is_array( $str ) ) $str = array_map( 'strtoupper_safe', $str );
return $str;
}
/**
* Returns whether or not a string is found in another
* Shortcut for strpos()
*
* @param string $needle What to search for
* @param string $haystack What to search in
* @param bool $insensitive Whether or not to do a case-insensitive search
* @return bool True if $needle is found in $haystack
* @link http://us3.php.net/strpos
*/
function in_string( $needle, $haystack, $insensitive = false ) {
$fnc = 'strpos';
if( $insensitive ) $fnc = 'stripos';
return $fnc( $haystack, $needle ) !== false;
}
/**
* Recursive in_array function
*
* @param string $needle What to search for
* @param array $haystack What to search in
* @param bool $insensitive Whether or not to do a case-insensitive search
* @return bool True if $needle is found in $haystack
* @link http://us3.php.net/in_array
*/
function in_array_recursive( $needle, $haystack, $insensitive = false ) {
$fnc = 'in_array';
if( $insensitive ) $fnc = 'iin_array';
if( $fnc( $needle, $haystack ) ) return true;
foreach( $haystack as $val ){
if( is_array( $val ) ) {
return in_array_recursive( $needle, $val );
}
}
return false;
}
/**
* Recursive glob() function.
*
* @access public
* @param string $pattern . (default: '*')
* @param int $flags . (default: 0)
* @param string $path . (default: '')
* @return array
*/
function rglob( $pattern = '*', $flags = 0, $path = '' ) {
$paths = glob( $path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT );
$files = glob( $path . $pattern, $flags );
foreach( $paths as $path ){
$files = array_merge( $files, rglob( $pattern, $flags, $path ) );
}
return $files;
}
/**
* Detects the presence of a nobots template or one that denies editing by ours
*
* @access public
* @param Wiki $wiki Wiki class
* @param string $text Text of the page to check (default: '')
* @param string $pgUsername Username to search for in the template (default: null)
* @param string|null $optout Text to search for in the optout= parameter. (default: null)
* @param string|null $taskname (default: null)
* @return bool True on match of an appropriate nobots template
*/
function checkExclusion( Wiki $wiki, $text = '', $pgUsername = null, $optout = null, $taskname = null ) {
if( !$wiki->get_nobots() ) return false;
if( in_string( "{{nobots}}", $text ) ) return true;
if( in_string( "{{bots}}", $text ) ) return false;
if( preg_match( '/\{\{bots\s*\|\s*allow\s*=\s*(.*?)\s*\}\}/i', $text, $allow ) ) {
if( $allow[1] == "all" ) return false;
if( $allow[1] == "none" ) return true;
$allow = array_map( 'trim', explode( ',', $allow[1] ) );
if( !is_null( $pgUsername ) && in_array( trim( $pgUsername ), $allow ) ) {
return false;
}
return true;
}
if( preg_match( '/\{\{(no)?bots\s*\|\s*deny\s*=\s*(.*?)\s*\}\}/i', $text, $deny ) ) {
if( $deny[2] == "all" ) return true;
if( $deny[2] == "none" ) return false;
$allow = array_map( 'trim', explode( ',', $deny[2] ) );
if( ( !is_null( $pgUsername ) && in_array( trim( $pgUsername ), $allow ) ) || ( !is_null( $taskname ) && in_array( trim( $taskname ), $allow ) ) ) {
return true;
}
return false;
}
if( !is_null( $optout ) && preg_match( '/\{\{(no)?bots\s*\|\s*optout\s*=\s*(.*?)\s*\}\}/i', $text, $allow ) ) {
if( $allow[1] == "all" ) return true;
$allow = array_map( 'trim', explode( ',', $allow[2] ) );
if( in_array( trim( $optout ), $allow ) ) {
return true;
}
return false;
}
return false;
}
/**
* Outputs text if the given category is in the allowed types
*
* @param string $text Text to display
* @param int $cat Category of text, such as PECHO_WARN, PECHO_NORMAL
* @param string $func
* @return void
*/
function outputText( $text, $cat = 0, $func = 'echo' ) {
global $pgVerbose;
Hooks::runHook( 'OutputText', array( &$text, &$cat, &$func ) );
if( in_array( $cat, $pgVerbose ) ) {
if( $func == 'echo' ) {
echo $text;
} else {
$func( $text );
}
}
}
/**
* Shortcut for {@link outputText}
*
* @param string $text Text to display
* @param int $cat Category of text, such as PECHO_WARN, PECHO_NORMAL
* @param string $func
* @link outputText
* @return void
*/
function pecho( $text, $cat = 0, $func = 'echo' ) {
global $pgWebOutput;
if( $pgWebOutput ) $text = str_replace( "\n", "<br>", $text );
outputText( $text, $cat, $func );
}
/**
* Echo function with color capabilities.
*
* Syntax:
*
* <i>[Text to colorize|NAME] where NAME is the name of a defined style.</i> For example:
*
* <i>This text is standard terminal color. [This text will be yellow.|COMMENT] [This text will be white on red.|ERROR]</i>
*
* Defined styles:
* <ul>
* <li>ERROR: White on red, bold</li>
* <li>INFO: Green text, bold</li>
* <li>PARAMETER: Cyan text</li>
* <li>COMMENT: Yellow text</li>
* <li>GREEN_BAR: White on green, bold</li>
* <li>RED_BAR: White on red, bold</li>
* <li>YELLOW_BAR: Black on yellow, bold</li>
* <li>INFO_BAR: Cyan text, bold</li>
* </ul>
*
* You can define your own styles by using this syntax:
*
* <code>lime_colorizer::style('STYLE_NAME', array('bg' => 'red', 'fg' => 'white'));</code>
*
* (Available colors: black, red, green, yellow, blue, magenta, cyan, white)
*
* You can also set options for how the text is formatted (not available on all systems):
*
* <code>lime_colorizer::style('STYLE_NAME', array('bg' => 'red', 'fg' => 'white', 'bold' => true ));</code> (sets bold text)
*
* Available options: bold, underscore, blink, reverse, conceal
*
*
* @access public
* @param string $text
* @param bool $return
* @return string
*/
function cecho( $text, $return = false ) {
global $pgColorizer;
if( !isset( $pgColorizer ) ) $pgColorizer = new lime_colorizer( true );
$text = preg_replace_callback( '/\[(.+?)\|(\w+)\]/s', function ($m) {
global $pgColorizer;
return $pgColorizer->colorize($m[1], $m[2]);
}, $text );
if( $return ) return $text;
echo $text;
}
/**
* Generates a diff between two strings
*
* @package Text_Diff
* @deprecated since 18 June 2013
*/
function getTextDiff() {
Peachy::deprecatedWarn( 'getTextDiff()', 'Diff::load()' );
$args = func_get_args();
return call_user_func_array( array( 'Diff', 'load' ), $args );
}
/**
* Gets the first defined Wiki object
*
* @return Wiki|bool
* @package initFunctions
*/
function &getSiteObject() {
foreach( $GLOBALS as $var ){
if( is_object( $var ) ) {
if( get_class( $var ) == "Wiki" ) {
return $var;
}
}
}
return false;
}
/**
* Returns an instance of the Page class as specified by $title or $pageid
*
* @param mixed $title Title of the page (default: null)
* @param mixed $pageid ID of the page (default: null)
* @param bool $followRedir Should it follow a redirect when retrieving the page (default: true)
* @param bool $normalize Should the class automatically normalize the title (default: true)
* @return Page
* @package initFunctions
*/
function &initPage( $title = null, $pageid = null, $followRedir = true, $normalize = true ) {
$wiki = getSiteObject();
if( !$wiki ) return false;
$page = new Page( $wiki, $title, $pageid, $followRedir, $normalize );
return $page;
}
/**
* Returns an instance of the User class as specified by $pgUsername
*
* @param mixed $pgUsername Username
* @return User|false
* @package initFunctions
*/
function &initUser( $pgUsername ) {
$wiki = getSiteObject();
if( !$wiki ) return false;
return new User( $wiki, $pgUsername );
}
/**
* Returns an instance of the Image class as specified by $filename or $pageid
*
* @param string $filename Filename
* @return Image
* @package initFunctions
*/
function &initImage( $filename = null ) {
$wiki = getSiteObject();
if( !$wiki ) return false;
$image = new Image( $wiki, $filename );
return $image;
}
if( !function_exists( 'mb_strlen' ) ) {
/**
* Fallback implementation of mb_strlen.
*
* @link http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/includes/GlobalFunctions.php
* @param string $str String to get
* @return int
* @package Fallback
*/
function mb_strlen( $str ) {
$counts = count_chars( $str );
$total = 0;
// Count ASCII bytes
for( $i = 0; $i < 0x80; $i++ ){
$total += $counts[$i];
}
// Count multibyte sequence heads
for( $i = 0xc0; $i < 0xff; $i++ ){
$total += $counts[$i];
}
return $total;
}
}
if( !function_exists( 'mb_substr' ) ) {
/**
* Fallback implementation for mb_substr. This is VERY slow, from 5x to 100x slower. Use only if necessary.
* @link http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/includes/GlobalFunctions.php
* @package Fallback
*/
function mb_substr( $str, $start, $count = 'end' ) {
if( $start != 0 ) {
$split = mb_substr_split_unicode( $str, intval( $start ) );
$str = substr( $str, $split );
}
if( $count !== 'end' ) {
$split = mb_substr_split_unicode( $str, intval( $count ) );
$str = substr( $str, 0, $split );
}
return $str;
}
/**
* Continuing support for mb_substr. Do not use.
* @link http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3/includes/GlobalFunctions.php
* @package Fallback
* @param integer $splitPos
* @return int
*/
function mb_substr_split_unicode( $str, $splitPos ) {
if( $splitPos == 0 ) {
return 0;
}
$byteLen = strlen( $str );
if( $splitPos > 0 ) {
if( $splitPos > 256 ) {
// Optimize large string offsets by skipping ahead N bytes.
// This will cut out most of our slow time on Latin-based text,
// and 1/2 to 1/3 on East European and Asian scripts.
$bytePos = $splitPos;
while( $bytePos < $byteLen && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ){
++$bytePos;
}
$charPos = mb_strlen( substr( $str, 0, $bytePos ) );
} else {
$charPos = 0;
$bytePos = 0;
}
while( $charPos++ < $splitPos ){
++$bytePos;
// Move past any tail bytes
while( $bytePos < $byteLen && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ){
++$bytePos;
}
}
} else {
$splitPosX = $splitPos + 1;
$charPos = 0; // relative to end of string; we don't care about the actual char position here
$bytePos = $byteLen;
while( $bytePos > 0 && $charPos-- >= $splitPosX ){
--$bytePos;
// Move past any tail bytes
while( $bytePos > 0 && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ){
--$bytePos;
}
}
}
return $bytePos;
}
}
if( !function_exists( 'iconv' ) ) {
/**
* Fallback iconv function.
*
* iconv support is not in the default configuration and so may not be present.
* Assume will only ever use utf-8 and iso-8859-1.
* This will *not* work in all circumstances.
*
* @access public
* @param mixed $from
* @param mixed $to
* @param mixed $string
* @return void
* @package Fallback
*/
function iconv( $from, $to, $string ) {
if( substr( $to, -8 ) == '//IGNORE' ) $to = substr( $to, 0, strlen( $to ) - 8 );
if( strcasecmp( $from, $to ) == 0 ) return $string;
if( strcasecmp( $from, 'utf-8' ) == 0 ) return utf8_decode( $string );
if( strcasecmp( $to, 'utf-8' ) == 0 ) return utf8_encode( $string );
return $string;
}
}
if( !function_exists( 'istainted' ) ) {
/**
* Fallback istainted function.
*
* @access public
* @param mixed $var
* @return integer
* @package Fallback
*/
function istainted( $var ) {
return 0;
}
/**
* Fallback taint function.
*
* @access public
* @param mixed $var
* @param int $level
* @return void
* @package Fallback
*/
function taint( $var, $level = 0 ) { }
/**
* Fallback untaint function.
*
* @access public
* @param mixed $var
* @param int $level
* @return void
* @package Fallback
*/
function untaint( $var, $level = 0 ) { }
/**
* @package Fallback
*/
define( 'TC_HTML', 1 );
/**
* @package Fallback
*/
define( 'TC_SHELL', 1 );
/**
* @package Fallback
*/
define( 'TC_MYSQL', 1 );
/**
* @package Fallback
*/
define( 'TC_PCRE', 1 );
/**
* @package Fallback
*/
define( 'TC_SELF', 1 );
}