Alteration of security fix to make it very slightly less ugly
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
1 <?
2 # Global functions used everywhere
3
4 $wgNumberOfArticles = -1; # Unset
5 $wgTotalViews = -1;
6 $wgTotalEdits = -1;
7
8 include_once( "DatabaseFunctions.php" );
9 include_once( "UpdateClasses.php" );
10 include_once( "LogPage.php" );
11
12 /*
13 * Compatibility functions
14 */
15
16 # PHP <4.3.x is not actively supported; 4.1.x and 4.2.x might or might not work.
17 # <4.1.x will not work, as we use a number of features introduced in 4.1.0
18 # such as the new autoglobals.
19
20 if( !function_exists('iconv') ) {
21 # iconv support is not in the default configuration and so may not be present.
22 # Assume will only ever use utf-8 and iso-8859-1.
23 # This will *not* work in all circumstances.
24 function iconv( $from, $to, $string ) {
25 if(strcasecmp( $from, $to ) == 0) return $string;
26 if(strcasecmp( $from, "utf-8" ) == 0) return utf8_decode( $string );
27 if(strcasecmp( $to, "utf-8" ) == 0) return utf8_encode( $string );
28 return $string;
29 }
30 }
31
32 if( !function_exists('file_get_contents') ) {
33 # Exists in PHP 4.3.0+
34 function file_get_contents( $filename ) {
35 return implode( "", file( $filename ) );
36 }
37 }
38
39 $wgRandomSeeded = false;
40
41 function wfSeedRandom()
42 {
43 global $wgRandomSeeded;
44
45 if ( ! $wgRandomSeeded ) {
46 $seed = hexdec(substr(md5(microtime()),-8)) & 0x7fffffff;
47 mt_srand( $seed );
48 $wgRandomSeeded = true;
49 }
50 }
51
52 function wfLocalUrl( $a, $q = "" )
53 {
54 global $wgServer, $wgScript, $wgArticlePath;
55
56 $a = str_replace( " ", "_", $a );
57 #$a = wfUrlencode( $a ); # This stuff is _already_ URL-encoded.
58
59 if ( "" == $a ) {
60 if( "" == $q ) {
61 $a = $wgScript;
62 } else {
63 $a = "{$wgScript}?{$q}";
64 }
65 } else if ( "" == $q ) {
66 $a = str_replace( "$1", $a, $wgArticlePath );
67 } else {
68 $a = "{$wgScript}?title={$a}&{$q}";
69 }
70 return $a;
71 }
72
73 function wfLocalUrlE( $a, $q = "" )
74 {
75 return wfEscapeHTML( wfLocalUrl( $a, $q ) );
76 }
77
78 function wfFullUrl( $a, $q = "" ) {
79 global $wgServer;
80 return $wgServer . wfLocalUrl( $a, $q );
81 }
82
83 function wfFullUrlE( $a, $q = "" ) {
84 return wfEscapeHTML( wfFullUrl( $a, $q ) );
85 }
86
87 function wfImageUrl( $img )
88 {
89 global $wgUploadPath;
90
91 $nt = Title::newFromText( $img );
92 if( !$nt ) return "";
93
94 $name = $nt->getDBkey();
95 $hash = md5( $name );
96
97 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
98 substr( $hash, 0, 2 ) . "/{$name}";
99 return wfUrlencode( $url );
100 }
101
102 function wfImageArchiveUrl( $name )
103 {
104 global $wgUploadPath;
105
106 $hash = md5( substr( $name, 15) );
107 $url = "{$wgUploadPath}/archive/" . $hash{0} . "/" .
108 substr( $hash, 0, 2 ) . "/{$name}";
109 return $url;
110 }
111
112 function wfUrlencode ( $s )
113 {
114 $ulink = urlencode( $s );
115 $ulink = preg_replace( "/%3[Aa]/", ":", $ulink );
116 $ulink = preg_replace( "/%2[Ff]/", "/", $ulink );
117 return $ulink;
118 }
119
120 function wfUtf8Sequence($codepoint) {
121 if($codepoint < 0x80) return chr($codepoint);
122 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
123 chr($codepoint & 0x3f | 0x80);
124 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
125 chr($codepoint >> 6 & 0x3f | 0x80) .
126 chr($codepoint & 0x3f | 0x80);
127 if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this
128 chr($codepoint >> 12 & 0x3f | 0x80) .
129 chr($codepoint >> 6 & 0x3f | 0x80) .
130 chr($codepoint & 0x3f | 0x80);
131 # Doesn't yet handle outside the BMP
132 return "&#$codepoint;";
133 }
134
135 function wfMungeToUtf8($string) {
136 global $wgInputEncoding; # This is debatable
137 #$string = iconv($wgInputEncoding, "UTF-8", $string);
138 $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
139 $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
140 # Should also do named entities here
141 return $string;
142 }
143
144 function wfDebug( $text, $logonly = false )
145 {
146 global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly;
147
148 if ( $wgDebugComments && !$logonly ) {
149 $wgOut->debug( $text );
150 }
151 if ( "" != $wgDebugLogFile && !$wgProfileOnly ) {
152 error_log( $text, 3, $wgDebugLogFile );
153 }
154 }
155
156 function logProfilingData()
157 {
158 global $wgRequestTime, $wgDebugLogFile;
159 global $wgProfiling, $wgProfileStack, $wgProfileLimit, $wgUser;
160 list( $usec, $sec ) = explode( " ", microtime() );
161 $now = (float)$sec + (float)$usec;
162
163 list( $usec, $sec ) = explode( " ", $wgRequestTime );
164 $start = (float)$sec + (float)$usec;
165 $elapsed = $now - $start;
166 if ( "" != $wgDebugLogFile ) {
167 $prof = wfGetProfilingOutput( $start, $elapsed );
168 if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
169 $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR'];
170 if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
171 $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP'];
172 if( !empty( $_SERVER['HTTP_FROM'] ) )
173 $forward .= " from " . $_SERVER['HTTP_FROM'];
174 if( $forward )
175 $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
176 if($wgUser->getId() == 0)
177 $forward .= " anon";
178 $log = sprintf( "%s\t%04.3f\t%s\n",
179 gmdate( "YmdHis" ), $elapsed,
180 urldecode( $_SERVER['REQUEST_URI'] . $forward ) );
181 error_log( $log . $prof, 3, $wgDebugLogFile );
182 }
183 }
184
185
186 function wfReadOnly()
187 {
188 global $wgReadOnlyFile;
189
190 if ( "" == $wgReadOnlyFile ) { return false; }
191 return is_file( $wgReadOnlyFile );
192 }
193
194 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
195
196 # Get a message from anywhere
197 function wfMsg( $key ) {
198 $args = func_get_args();
199 if ( count( $args ) ) {
200 array_shift( $args );
201 }
202 return wfMsgReal( $key, $args, true );
203 }
204
205 # Get a message from the language file
206 function wfMsgNoDB( $key ) {
207 $args = func_get_args();
208 if ( count( $args ) ) {
209 array_shift( $args );
210 }
211 return wfMsgReal( $key, $args, false );
212 }
213
214 # Really get a message
215 function wfMsgReal( $key, $args, $useDB ) {
216 global $wgReplacementKeys, $wgMessageCache, $wgLang;
217
218 $fname = "wfMsg";
219 wfProfileIn( $fname );
220 if ( $wgMessageCache ) {
221 $message = $wgMessageCache->get( $key, $useDB );
222 } elseif ( $wgLang ) {
223 $message = $wgLang->getMessage( $key );
224 } else {
225 wfDebug( "No language object when getting $key\n" );
226 $message = "&lt;$key&gt;";
227 }
228
229 # Replace arguments
230 if( count( $args ) ) {
231 $message = str_replace( $wgReplacementKeys, $args, $message );
232 }
233 wfProfileOut( $fname );
234 return $message;
235 }
236
237 function wfCleanFormFields( $fields )
238 {
239 global $HTTP_POST_VARS;
240 global $wgInputEncoding, $wgOutputEncoding, $wgEditEncoding, $wgLang;
241
242 if ( get_magic_quotes_gpc() ) {
243 foreach ( $fields as $fname ) {
244 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
245 $HTTP_POST_VARS[$fname] = stripslashes(
246 $HTTP_POST_VARS[$fname] );
247 }
248 global ${$fname};
249 if ( isset( ${$fname} ) ) {
250 ${$fname} = stripslashes( ${$fname} );
251 }
252 }
253 }
254 $enc = $wgOutputEncoding;
255 if( $wgEditEncoding != "") $enc = $wgEditEncoding;
256 if ( $enc != $wgInputEncoding ) {
257 foreach ( $fields as $fname ) {
258 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
259 $HTTP_POST_VARS[$fname] = $wgLang->iconv(
260 $wgOutputEncoding, $wgInputEncoding,
261 $HTTP_POST_VARS[$fname] );
262 }
263 global ${$fname};
264 if ( isset( ${$fname} ) ) {
265 ${$fname} = $wgLang->iconv(
266 $enc, $wgInputEncoding, ${$fname} );
267 }
268 }
269 }
270 }
271
272 function wfMungeQuotes( $in )
273 {
274 $out = str_replace( "%", "%25", $in );
275 $out = str_replace( "'", "%27", $out );
276 $out = str_replace( "\"", "%22", $out );
277 return $out;
278 }
279
280 function wfDemungeQuotes( $in )
281 {
282 $out = str_replace( "%22", "\"", $in );
283 $out = str_replace( "%27", "'", $out );
284 $out = str_replace( "%25", "%", $out );
285 return $out;
286 }
287
288 function wfCleanQueryVar( $var )
289 {
290 global $wgLang;
291 if ( get_magic_quotes_gpc() ) {
292 $var = stripslashes( $var );
293 }
294 return $wgLang->recodeInput( $var );
295 }
296
297 function wfSpecialPage()
298 {
299 global $wgUser, $wgOut, $wgTitle, $wgLang;
300
301 /* FIXME: this list probably shouldn't be language-specific, per se */
302 $validSP = $wgLang->getValidSpecialPages();
303 $sysopSP = $wgLang->getSysopSpecialPages();
304 $devSP = $wgLang->getDeveloperSpecialPages();
305
306 $wgOut->setArticleFlag( false );
307 $wgOut->setRobotpolicy( "noindex,follow" );
308
309 $par = NULL;
310 list($t, $par) = split( "/", $wgTitle->getDBkey(), 2 );
311
312 if ( array_key_exists( $t, $validSP ) ||
313 ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) ||
314 ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) {
315 if($par !== NULL)
316 $wgTitle = Title::makeTitle( Namespace::getSpecial(), $t );
317
318 $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) );
319
320 $inc = "Special" . $t . ".php";
321 include_once( $inc );
322 $call = "wfSpecial" . $t;
323 $call( $par );
324 } else if ( array_key_exists( $t, $sysopSP ) ) {
325 $wgOut->sysopRequired();
326 } else if ( array_key_exists( $t, $devSP ) ) {
327 $wgOut->developerRequired();
328 } else {
329 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
330 }
331 }
332
333 function wfSearch( $s )
334 {
335 $se = new SearchEngine( wfCleanQueryVar( $s ) );
336 $se->showResults();
337 }
338
339 function wfGo( $s )
340 { # pick the nearest match
341 $se = new SearchEngine( wfCleanQueryVar( $s ) );
342 $se->goResult();
343 }
344
345
346 /* private */ $wgAbruptExitCalled = false;
347
348 # Just like exit() but makes a note of it.
349 function wfAbruptExit(){
350 // Safety to avoid infinite recursion in case of (unlikely) bugs somewhere
351 global $wgAbruptExitCalled;
352 if ( $wgAbruptExitCalled ){
353 exit();
354 }
355 $wgAbruptExitCalled = true;
356
357 if( ! function_exists( "debug_backtrace" )){ // for php < 4.3.0
358 wfDebug("WARNING: Abrupt exit from somewhere.\n");
359 exit();
360 }
361
362 wfDebug("WARNING: Abrupt exit. Backtrace follows:\n");
363 $bt = debug_backtrace();
364 for($i = 0; $i < count($bt) ; $i++){
365 $file = $bt[$i]["file"];
366 $line = $bt[$i]["line"];
367 wfDebug("Abrupt exit in $file at line $line\n");
368 }
369 exit();
370 }
371
372 function wfNumberOfArticles()
373 {
374 global $wgNumberOfArticles;
375
376 wfLoadSiteStats();
377 return $wgNumberOfArticles;
378 }
379
380 /* private */ function wfLoadSiteStats()
381 {
382 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
383 if ( -1 != $wgNumberOfArticles ) return;
384
385 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
386 "FROM site_stats WHERE ss_row_id=1";
387 $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" );
388
389 if ( 0 == wfNumRows( $res ) ) { return; }
390 else {
391 $s = wfFetchObject( $res );
392 $wgTotalViews = $s->ss_total_views;
393 $wgTotalEdits = $s->ss_total_edits;
394 $wgNumberOfArticles = $s->ss_good_articles;
395 }
396 }
397
398 function wfEscapeHTML( $in )
399 {
400 return str_replace(
401 array( "&", "\"", ">", "<" ),
402 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
403 $in );
404 }
405
406 function wfEscapeHTMLTagsOnly( $in ) {
407 return str_replace(
408 array( "\"", ">", "<" ),
409 array( "&quot;", "&gt;", "&lt;" ),
410 $in );
411 }
412
413 function wfUnescapeHTML( $in )
414 {
415 $in = str_replace( "&lt;", "<", $in );
416 $in = str_replace( "&gt;", ">", $in );
417 $in = str_replace( "&quot;", "\"", $in );
418 $in = str_replace( "&amp;", "&", $in );
419 return $in;
420 }
421
422 function wfImageDir( $fname )
423 {
424 global $wgUploadDirectory;
425
426 $hash = md5( $fname );
427 $oldumask = umask(0);
428 $dest = $wgUploadDirectory . "/" . $hash{0};
429 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
430 $dest .= "/" . substr( $hash, 0, 2 );
431 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
432
433 umask( $oldumask );
434 return $dest;
435 }
436
437 function wfImageArchiveDir( $fname )
438 {
439 global $wgUploadDirectory;
440
441 $hash = md5( $fname );
442 $oldumask = umask(0);
443 $archive = "{$wgUploadDirectory}/archive";
444 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
445 $archive .= "/" . $hash{0};
446 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
447 $archive .= "/" . substr( $hash, 0, 2 );
448 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
449
450 umask( $oldumask );
451 return $archive;
452 }
453
454 function wfRecordUpload( $name, $oldver, $size, $desc )
455 {
456 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
457 global $wgUseCopyrightUpload , $wpUploadCopyStatus , $wpUploadSource ;
458
459 $fname = "wfRecordUpload";
460
461 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
462 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
463 $res = wfQuery( $sql, DB_READ, $fname );
464
465 $now = wfTimestampNow();
466 $won = wfInvertTimestamp( $now );
467 $size = IntVal( $size );
468
469 if ( $wgUseCopyrightUpload )
470 {
471 $textdesc = "== " . wfMsg ( "filedesc" ) . " ==\n" . $desc . "\n" .
472 "== " . wfMsg ( "filestatus" ) . " ==\n" . $wpUploadCopyStatus . "\n" .
473 "== " . wfMsg ( "filesource" ) . " ==\n" . $wpUploadSource ;
474 }
475 else $textdesc = $desc ;
476
477 $now = wfTimestampNow();
478 $won = wfInvertTimestamp( $now );
479
480 if ( 0 == wfNumRows( $res ) ) {
481 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
482 "img_description,img_user,img_user_text) VALUES ('" .
483 wfStrencode( $name ) . "',$size,'{$now}','" .
484 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
485 "', '" . wfStrencode( $wgUser->getName() ) . "')";
486 wfQuery( $sql, DB_WRITE, $fname );
487
488 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
489 Namespace::getImage() . " AND cur_title='" .
490 wfStrencode( $name ) . "'";
491 $res = wfQuery( $sql, DB_READ, $fname );
492 if ( 0 == wfNumRows( $res ) ) {
493 $common =
494 Namespace::getImage() . ",'" .
495 wfStrencode( $name ) . "','" .
496 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
497 wfStrencode( $wgUser->getName() ) . "','" . $now .
498 "',1";
499 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
500 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
501 "cur_text,inverse_timestamp,cur_touched) VALUES (" .
502 $common .
503 ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')";
504 wfQuery( $sql, DB_WRITE, $fname );
505 $id = wfInsertId() or 0; # We should throw an error instead
506 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
507 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
508 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
509 wfQuery( $sql, DB_WRITE, $fname );
510 $u = new SearchUpdate( $id, $name, $desc );
511 $u->doUpdate();
512 }
513 } else {
514 $s = wfFetchObject( $res );
515
516 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
517 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
518 wfStrencode( $s->img_name ) . "','" .
519 wfStrencode( $oldver ) .
520 "',{$s->img_size},'{$s->img_timestamp}','" .
521 wfStrencode( $s->img_description ) . "','" .
522 wfStrencode( $s->img_user ) . "','" .
523 wfStrencode( $s->img_user_text) . "')";
524 wfQuery( $sql, DB_WRITE, $fname );
525
526 $sql = "UPDATE image SET img_size={$size}," .
527 "img_timestamp='" . wfTimestampNow() . "',img_user='" .
528 $wgUser->getID() . "',img_user_text='" .
529 wfStrencode( $wgUser->getName() ) . "', img_description='" .
530 wfStrencode( $desc ) . "' WHERE img_name='" .
531 wfStrencode( $name ) . "'";
532 wfQuery( $sql, DB_WRITE, $fname );
533
534 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" .
535 Namespace::getImage() . " AND cur_title='" .
536 wfStrencode( $name ) . "'";
537 wfQuery( $sql, DB_WRITE, $fname );
538 }
539
540 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
541 $da = wfMsg( "uploadedimage", "[[:" . $wgLang->getNsText(
542 Namespace::getImage() ) . ":{$name}|{$name}]]" );
543 $ta = wfMsg( "uploadedimage", $name );
544 $log->addEntry( $da, $desc, $ta );
545 }
546
547
548 /* Some generic result counters, pulled out of SearchEngine */
549
550 function wfShowingResults( $offset, $limit )
551 {
552 return wfMsg( "showingresults", $limit, $offset+1 );
553 }
554
555 function wfShowingResultsNum( $offset, $limit, $num )
556 {
557 return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
558 }
559
560 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
561 {
562 global $wgUser;
563 $prev = wfMsg( "prevn", $limit );
564 $next = wfMsg( "nextn", $limit );
565
566 $sk = $wgUser->getSkin();
567 if ( 0 != $offset ) {
568 $po = $offset - $limit;
569 if ( $po < 0 ) { $po = 0; }
570 $q = "limit={$limit}&offset={$po}";
571 if ( "" != $query ) { $q .= "&{$query}"; }
572 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
573 } else { $plink = $prev; }
574
575 $no = $offset + $limit;
576 $q = "limit={$limit}&offset={$no}";
577 if ( "" != $query ) { $q .= "&{$query}"; }
578
579 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
580 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
581 wfNumLink( $offset, 50, $link, $query ) . " | " .
582 wfNumLink( $offset, 100, $link, $query ) . " | " .
583 wfNumLink( $offset, 250, $link, $query ) . " | " .
584 wfNumLink( $offset, 500, $link, $query );
585
586 return wfMsg( "viewprevnext", $plink, $nlink, $nums );
587 }
588
589 function wfNumLink( $offset, $limit, $link, $query = "" )
590 {
591 global $wgUser;
592 if ( "" == $query ) { $q = ""; }
593 else { $q = "{$query}&"; }
594 $q .= "limit={$limit}&offset={$offset}";
595
596 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
597 return $s;
598 }
599
600 function wfClientAcceptsGzip() {
601 global $wgUseGzip;
602 if( $wgUseGzip ) {
603 # FIXME: we may want to blacklist some broken browsers
604 if( preg_match(
605 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
606 $_SERVER["HTTP_ACCEPT_ENCODING"],
607 $m ) ) {
608 if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
609 wfDebug( " accepts gzip\n" );
610 return true;
611 }
612 }
613 return false;
614 }
615
616 # Yay, more global functions!
617 function wfCheckLimits( $deflimit = 50, $optionname = "rclimit" ) {
618 global $wgUser;
619
620 $limit = (int)$_REQUEST['limit'];
621 if( $limit < 0 ) $limit = 0;
622 if( ( $limit == 0 ) && ( $optionname != "" ) ) {
623 $limit = (int)$wgUser->getOption( $optionname );
624 }
625 if( $limit <= 0 ) $limit = $deflimit;
626 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
627
628 $offset = (int)$_REQUEST['offset'];
629 $offset = (int)$offset;
630 if( $offset < 0 ) $offset = 0;
631 if( $offset > 65000 ) $offset = 65000; # do we need a max? what?
632
633 return array( $limit, $offset );
634 }
635
636 # Escapes the given text so that it may be output using addWikiText()
637 # without any linking, formatting, etc. making its way through. This
638 # is achieved by substituting certain characters with HTML entities.
639 # As required by the callers, <nowiki> is not used. It currently does
640 # not filter out characters which have special meaning only at the
641 # start of a line, such as "*".
642 function wfEscapeWikiText( $text )
643 {
644 $text = str_replace(
645 array( '[', '|', "'", 'ISBN ' , '://' , "\n=" ),
646 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
647 htmlspecialchars($text) );
648 return $text;
649 }
650
651 function wfQuotedPrintable( $string, $charset = "" )
652 {
653 # Probably incomplete; see RFC 2045
654 if( empty( $charset ) ) {
655 global $wgInputEncoding;
656 $charset = $wgInputEncoding;
657 }
658 $charset = strtoupper( $charset );
659 $charset = str_replace( "ISO-8859", "ISO8859", $charset ); // ?
660
661 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
662 $replace = $illegal . '\t ?_';
663 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
664 $out = "=?$charset?Q?";
665 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
666 $out .= "?=";
667 return $out;
668 }
669
670 # Changes the first character to an HTML entity
671 function wfHtmlEscapeFirst( $text ) {
672 $ord = ord($text);
673 $newText = substr($text, 1);
674 return "&#$ord;$newText";
675 }
676
677
678 ?>