Add compatibility file_get_contents; fix compat iconv
[mediawiki.git] / includes / GlobalFunctions.php
blob22ae3ca6597e81a9470aad848c39a4586f6a15f3
1 <?
2 # Global functions used everywhere
4 $wgNumberOfArticles = -1; # Unset
5 $wgTotalViews = -1;
6 $wgTotalEdits = -1;
8 global $IP;
9 include_once( "$IP/DatabaseFunctions.php" );
10 include_once( "$IP/UpdateClasses.php" );
11 include_once( "$IP/LogPage.php" );
13 # PHP 4.1+ has array_key_exists, PHP 4.0.6 has key_exists instead, and earlier
14 # versions of PHP have neither. So we roll our own. Note that this
15 # function will return false even for keys that exist but whose associated
16 # value is NULL.
18 if ( phpversion() == "4.0.6" ) {
19 function array_key_exists( $k, $a ) {
20 return key_exists( $k, $a );
22 } else if (phpversion() < "4.1") {
23 function array_key_exists( $k, $a ) {
24 return isset($a[$k]);
28 if( !function_exists('iconv') ) {
29 # Assume will only ever use utf-8 and iso-8859-1.
30 # This will *not* work in all circumstances.
31 function iconv( $from, $to, $string ) {
32 if(strcasecmp( $from, $to ) == 0) return $string;
33 if(strcasecmp( $from, "utf-8" ) == 0) return utf8_decode( $string );
34 if(strcasecmp( $to, "utf-8" ) == 0) return utf8_encode( $string );
35 return $string;
39 if( !function_exists('file_get_contents') ) {
40 # Exists in PHP 4.3.0+
41 function file_get_contents( $filename ) {
42 return implode( "", file( $filename ) );
46 $wgRandomSeeded = false;
48 function wfSeedRandom()
50 global $wgRandomSeeded;
52 if ( ! $wgRandomSeeded ) {
53 $seed = hexdec(substr(md5(microtime()),-8)) & 0x7fffffff;
54 mt_srand( $seed );
55 $wgRandomSeeded = true;
59 function wfLocalUrl( $a, $q = "" )
61 global $wgServer, $wgScript, $wgArticlePath;
63 $a = str_replace( " ", "_", $a );
64 #$a = wfUrlencode( $a ); # This stuff is _already_ URL-encoded.
66 if ( "" == $a ) {
67 if( "" == $q ) {
68 $a = $wgScript;
69 } else {
70 $a = "{$wgScript}?{$q}";
72 } else if ( "" == $q ) {
73 $a = str_replace( "$1", $a, $wgArticlePath );
74 } else {
75 $a = "{$wgScript}?title={$a}&{$q}";
77 return $a;
80 function wfLocalUrlE( $a, $q = "" )
82 return wfEscapeHTML( wfLocalUrl( $a, $q ) );
85 function wfFullUrl( $a, $q = "" ) {
86 global $wgServer;
87 return $wgServer . wfLocalUrl( $a, $q );
90 function wfFullUrlE( $a, $q = "" ) {
91 return wfEscapeHTML( wfFullUrl( $a, $q ) );
94 function wfImageUrl( $img )
96 global $wgUploadPath;
98 $nt = Title::newFromText( $img );
99 if( !$nt ) return "";
101 $name = $nt->getDBkey();
102 $hash = md5( $name );
104 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
105 substr( $hash, 0, 2 ) . "/{$name}";
106 return wfUrlencode( $url );
109 function wfImageArchiveUrl( $name )
111 global $wgUploadPath;
113 $hash = md5( substr( $name, 15) );
114 $url = "{$wgUploadPath}/archive/" . $hash{0} . "/" .
115 substr( $hash, 0, 2 ) . "/{$name}";
116 return $url;
119 function wfUrlencode ( $s )
121 $ulink = urlencode( $s );
122 $ulink = preg_replace( "/%3[Aa]/", ":", $ulink );
123 $ulink = preg_replace( "/%2[Ff]/", "/", $ulink );
124 return $ulink;
127 function wfUtf8Sequence($codepoint) {
128 if($codepoint < 0x80) return chr($codepoint);
129 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
130 chr($codepoint & 0x3f | 0x80);
131 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
132 chr($codepoint >> 6 & 0x3f | 0x80) .
133 chr($codepoint & 0x3f | 0x80);
134 if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this
135 chr($codepoint >> 12 & 0x3f | 0x80) .
136 chr($codepoint >> 6 & 0x3f | 0x80) .
137 chr($codepoint & 0x3f | 0x80);
138 # Doesn't yet handle outside the BMP
139 return "&#$codepoint;";
142 function wfMungeToUtf8($string) {
143 global $wgInputEncoding; # This is debatable
144 #$string = iconv($wgInputEncoding, "UTF-8", $string);
145 $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
146 $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
147 # Should also do named entities here
148 return $string;
151 function wfDebug( $text, $logonly = false )
153 global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly;
155 if ( $wgDebugComments && !$logonly ) {
156 $wgOut->debug( $text );
158 if ( "" != $wgDebugLogFile && !$wgProfileOnly ) {
159 error_log( $text, 3, $wgDebugLogFile );
163 function wfReadOnly()
165 global $wgReadOnlyFile;
167 if ( "" == $wgReadOnlyFile ) { return false; }
168 return is_file( $wgReadOnlyFile );
171 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
173 # Get a message from anywhere
174 function wfMsg( $key ) {
175 $args = func_get_args();
176 if ( count( $args ) ) {
177 array_shift( $args );
179 return wfMsgReal( $key, $args, true );
182 # Get a message from the language file
183 function wfMsgNoDB( $key ) {
184 $args = func_get_args();
185 if ( count( $args ) ) {
186 array_shift( $args );
188 return wfMsgReal( $key, $args, false );
191 # Really get a message
192 function wfMsgReal( $key, $args, $useDB ) {
193 global $wgLang, $wgReplacementKeys, $wgMemc, $wgDBname;
194 global $wgUseDatabaseMessages, $wgUseMemCached, $wgOut;
195 global $wgAllMessagesEn, $wgLanguageCode;
197 $fname = "wfMsg";
198 wfProfileIn( $fname );
200 static $messageCache = false;
201 $memcKey = "$wgDBname:messages";
202 $fname = "wfMsg";
203 $message = false;
205 # newFromText is too slow!
206 $title = ucfirst( $key );
207 if ( $messageCache ) {
208 $message = $messageCache[$title];
209 } elseif ( !$wgUseDatabaseMessages || !$useDB ) {
210 $message = $wgLang->getMessage( $key );
213 if ( !$message && $wgUseMemCached ) {
214 # Try memcached
215 if ( !$messageCache ) {
216 $messageCache = $wgMemc->get( $memcKey );
219 # If there's nothing in memcached, load all the messages from the database
220 # This should only happen on server reset -- ordinary changes should update
221 # memcached in editUpdates()
222 if ( !$messageCache ) {
223 # Other threads don't need to load the messages if another thread is doing it.
224 $wgMemc->set( $memcKey, "loading", time() + 60 );
225 $messageCache = wfLoadAllMessages();
226 # Save in memcached
227 $wgMemc->set( $memcKey, $messageCache, time() + 3600 );
231 if ( is_array( $messageCache ) && array_key_exists( $title, $messageCache ) ) {
232 $message = $messageCache[$title];
236 # If there was no MemCached, load each message from the DB individually
237 if ( !$message ) {
238 if ( $useDB ) {
239 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
240 " AND cur_title='$title'";
241 $res = wfQuery( $sql, DB_READ, $fname );
243 if ( wfNumRows( $res ) ) {
244 $obj = wfFetchObject( $res );
245 $message = $obj->cur_text;
246 wfFreeResult( $res );
251 # Try the array in $wgLang
252 if ( !$message ) {
253 $message = $wgLang->getMessage( $key );
256 # Try the English array
257 if ( !$message && $wgLanguageCode != "en" ) {
258 $message = Language::getMessage( $key );
261 # Replace arguments
262 if( count( $args ) ) {
263 $message = str_replace( $wgReplacementKeys, $args, $message );
265 wfProfileOut( $fname );
266 if ( !$message ) {
267 # Failed, message does not exist
268 return "&lt;$key&gt;";
270 return $message;
273 function wfCleanFormFields( $fields )
275 global $HTTP_POST_VARS;
276 global $wgInputEncoding, $wgOutputEncoding, $wgEditEncoding, $wgLang;
278 if ( get_magic_quotes_gpc() ) {
279 foreach ( $fields as $fname ) {
280 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
281 $HTTP_POST_VARS[$fname] = stripslashes(
282 $HTTP_POST_VARS[$fname] );
284 global ${$fname};
285 if ( isset( ${$fname} ) ) {
286 ${$fname} = stripslashes( ${$fname} );
290 $enc = $wgOutputEncoding;
291 if( $wgEditEncoding != "") $enc = $wgEditEncoding;
292 if ( $enc != $wgInputEncoding ) {
293 foreach ( $fields as $fname ) {
294 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
295 $HTTP_POST_VARS[$fname] = $wgLang->iconv(
296 $wgOutputEncoding, $wgInputEncoding,
297 $HTTP_POST_VARS[$fname] );
299 global ${$fname};
300 if ( isset( ${$fname} ) ) {
301 ${$fname} = $wgLang->iconv(
302 $enc, $wgInputEncoding, ${$fname} );
308 function wfMungeQuotes( $in )
310 $out = str_replace( "%", "%25", $in );
311 $out = str_replace( "'", "%27", $out );
312 $out = str_replace( "\"", "%22", $out );
313 return $out;
316 function wfDemungeQuotes( $in )
318 $out = str_replace( "%22", "\"", $in );
319 $out = str_replace( "%27", "'", $out );
320 $out = str_replace( "%25", "%", $out );
321 return $out;
324 function wfCleanQueryVar( $var )
326 global $wgLang;
327 if ( get_magic_quotes_gpc() ) {
328 $var = stripslashes( $var );
330 return $wgLang->recodeInput( $var );
333 function wfSpecialPage()
335 global $wgUser, $wgOut, $wgTitle, $wgLang;
337 /* FIXME: this list probably shouldn't be language-specific, per se */
338 $validSP = $wgLang->getValidSpecialPages();
339 $sysopSP = $wgLang->getSysopSpecialPages();
340 $devSP = $wgLang->getDeveloperSpecialPages();
342 $wgOut->setArticleFlag( false );
343 $wgOut->setRobotpolicy( "noindex,follow" );
345 $par = NULL;
346 list($t, $par) = split( "/", $wgTitle->getDBkey(), 2 );
348 if ( array_key_exists( $t, $validSP ) ||
349 ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) ||
350 ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) {
351 if($par !== NULL)
352 $wgTitle = Title::makeTitle( Namespace::getSpecial(), $t );
354 $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) );
356 $inc = "Special" . $t . ".php";
357 include_once( $inc );
358 $call = "wfSpecial" . $t;
359 $call( $par );
360 } else if ( array_key_exists( $t, $sysopSP ) ) {
361 $wgOut->sysopRequired();
362 } else if ( array_key_exists( $t, $devSP ) ) {
363 $wgOut->developerRequired();
364 } else {
365 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
369 function wfSearch( $s )
371 $se = new SearchEngine( wfCleanQueryVar( $s ) );
372 $se->showResults();
375 function wfGo( $s )
376 { # pick the nearest match
377 $se = new SearchEngine( wfCleanQueryVar( $s ) );
378 $se->goResult();
381 function wfNumberOfArticles()
383 global $wgNumberOfArticles;
385 wfLoadSiteStats();
386 return $wgNumberOfArticles;
389 /* private */ function wfLoadSiteStats()
391 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
392 if ( -1 != $wgNumberOfArticles ) return;
394 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
395 "FROM site_stats WHERE ss_row_id=1";
396 $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" );
398 if ( 0 == wfNumRows( $res ) ) { return; }
399 else {
400 $s = wfFetchObject( $res );
401 $wgTotalViews = $s->ss_total_views;
402 $wgTotalEdits = $s->ss_total_edits;
403 $wgNumberOfArticles = $s->ss_good_articles;
407 function wfEscapeHTML( $in )
409 return str_replace(
410 array( "&", "\"", ">", "<" ),
411 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
412 $in );
415 function wfEscapeHTMLTagsOnly( $in ) {
416 return str_replace(
417 array( "\"", ">", "<" ),
418 array( "&quot;", "&gt;", "&lt;" ),
419 $in );
422 function wfUnescapeHTML( $in )
424 $in = str_replace( "&lt;", "<", $in );
425 $in = str_replace( "&gt;", ">", $in );
426 $in = str_replace( "&quot;", "\"", $in );
427 $in = str_replace( "&amp;", "&", $in );
428 return $in;
431 function wfImageDir( $fname )
433 global $wgUploadDirectory;
435 $hash = md5( $fname );
436 $oldumask = umask(0);
437 $dest = $wgUploadDirectory . "/" . $hash{0};
438 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
439 $dest .= "/" . substr( $hash, 0, 2 );
440 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
442 umask( $oldumask );
443 return $dest;
446 function wfImageArchiveDir( $fname )
448 global $wgUploadDirectory;
450 $hash = md5( $fname );
451 $oldumask = umask(0);
452 $archive = "{$wgUploadDirectory}/archive";
453 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
454 $archive .= "/" . $hash{0};
455 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
456 $archive .= "/" . substr( $hash, 0, 2 );
457 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
459 umask( $oldumask );
460 return $archive;
463 function wfRecordUpload( $name, $oldver, $size, $desc )
465 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
466 global $wgUseCopyrightUpload , $wpUploadCopyStatus , $wpUploadSource ;
468 $fname = "wfRecordUpload";
470 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
471 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
472 $res = wfQuery( $sql, DB_READ, $fname );
474 $now = wfTimestampNow();
475 $won = wfInvertTimestamp( $now );
476 $size = IntVal( $size );
478 if ( $wgUseCopyrightUpload )
480 $textdesc = "== " . wfMsg ( "filedesc" ) . " ==\n" . $desc . "\n" .
481 "== " . wfMsg ( "filestatus" ) . " ==\n" . $wpUploadCopyStatus . "\n" .
482 "== " . wfMsg ( "filesource" ) . " ==\n" . $wpUploadSource ;
484 else $textdesc = $desc ;
486 $now = wfTimestampNow();
487 $won = wfInvertTimestamp( $now );
489 if ( 0 == wfNumRows( $res ) ) {
490 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
491 "img_description,img_user,img_user_text) VALUES ('" .
492 wfStrencode( $name ) . "',$size,'{$now}','" .
493 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
494 "', '" . wfStrencode( $wgUser->getName() ) . "')";
495 wfQuery( $sql, DB_WRITE, $fname );
497 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
498 Namespace::getImage() . " AND cur_title='" .
499 wfStrencode( $name ) . "'";
500 $res = wfQuery( $sql, DB_READ, $fname );
501 if ( 0 == wfNumRows( $res ) ) {
502 $common =
503 Namespace::getImage() . ",'" .
504 wfStrencode( $name ) . "','" .
505 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
506 wfStrencode( $wgUser->getName() ) . "','" . $now .
507 "',1";
508 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
509 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
510 "cur_text,inverse_timestamp,cur_touched) VALUES (" .
511 $common .
512 ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')";
513 wfQuery( $sql, DB_WRITE, $fname );
514 $id = wfInsertId() or 0; # We should throw an error instead
515 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
516 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
517 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
518 wfQuery( $sql, DB_WRITE, $fname );
519 $u = new SearchUpdate( $id, $name, $desc );
520 $u->doUpdate();
522 } else {
523 $s = wfFetchObject( $res );
525 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
526 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
527 wfStrencode( $s->img_name ) . "','" .
528 wfStrencode( $oldver ) .
529 "',{$s->img_size},'{$s->img_timestamp}','" .
530 wfStrencode( $s->img_description ) . "','" .
531 wfStrencode( $s->img_user ) . "','" .
532 wfStrencode( $s->img_user_text) . "')";
533 wfQuery( $sql, DB_WRITE, $fname );
535 $sql = "UPDATE image SET img_size={$size}," .
536 "img_timestamp='" . wfTimestampNow() . "',img_user='" .
537 $wgUser->getID() . "',img_user_text='" .
538 wfStrencode( $wgUser->getName() ) . "', img_description='" .
539 wfStrencode( $desc ) . "' WHERE img_name='" .
540 wfStrencode( $name ) . "'";
541 wfQuery( $sql, DB_WRITE, $fname );
543 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" .
544 Namespace::getImage() . " AND cur_title='" .
545 wfStrencode( $name ) . "'";
546 wfQuery( $sql, DB_WRITE, $fname );
549 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
550 $da = wfMsg( "uploadedimage", "[[:" . $wgLang->getNsText(
551 Namespace::getImage() ) . ":{$name}|{$name}]]" );
552 $ta = wfMsg( "uploadedimage", $name );
553 $log->addEntry( $da, $desc, $ta );
557 /* Some generic result counters, pulled out of SearchEngine */
559 function wfShowingResults( $offset, $limit )
561 return wfMsg( "showingresults", $limit, $offset+1 );
564 function wfShowingResultsNum( $offset, $limit, $num )
566 return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
569 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
571 global $wgUser;
572 $prev = wfMsg( "prevn", $limit );
573 $next = wfMsg( "nextn", $limit );
575 $sk = $wgUser->getSkin();
576 if ( 0 != $offset ) {
577 $po = $offset - $limit;
578 if ( $po < 0 ) { $po = 0; }
579 $q = "limit={$limit}&offset={$po}";
580 if ( "" != $query ) { $q .= "&{$query}"; }
581 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
582 } else { $plink = $prev; }
584 $no = $offset + $limit;
585 $q = "limit={$limit}&offset={$no}";
586 if ( "" != $query ) { $q .= "&{$query}"; }
588 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
589 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
590 wfNumLink( $offset, 50, $link, $query ) . " | " .
591 wfNumLink( $offset, 100, $link, $query ) . " | " .
592 wfNumLink( $offset, 250, $link, $query ) . " | " .
593 wfNumLink( $offset, 500, $link, $query );
595 return wfMsg( "viewprevnext", $plink, $nlink, $nums );
598 function wfNumLink( $offset, $limit, $link, $query = "" )
600 global $wgUser;
601 if ( "" == $query ) { $q = ""; }
602 else { $q = "{$query}&"; }
603 $q .= "limit={$limit}&offset={$offset}";
605 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
606 return $s;
609 function wfClientAcceptsGzip() {
610 global $wgUseGzip;
611 if( $wgUseGzip ) {
612 # FIXME: we may want to blacklist some broken browsers
613 if( preg_match(
614 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
615 $_SERVER["HTTP_ACCEPT_ENCODING"],
616 $m ) ) {
617 if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
618 wfDebug( " accepts gzip\n" );
619 return true;
622 return false;
625 # Yay, more global functions!
626 function wfCheckLimits( $deflimit = 50, $optionname = "rclimit" ) {
627 global $wgUser;
629 $limit = (int)$_REQUEST['limit'];
630 if( $limit < 0 ) $limit = 0;
631 if( ( $limit == 0 ) && ( $optionname != "" ) ) {
632 $limit = (int)$wgUser->getOption( $optionname );
634 if( $limit <= 0 ) $limit = $deflimit;
635 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
637 $offset = (int)$_REQUEST['offset'];
638 $offset = (int)$offset;
639 if( $offset < 0 ) $offset = 0;
640 if( $offset > 65000 ) $offset = 65000; # do we need a max? what?
642 return array( $limit, $offset );
645 # Escapes the given text so that it may be output using addWikiText()
646 # without any linking, formatting, etc. making its way through. This
647 # is achieved by substituting certain characters with HTML entities.
648 # As required by the callers, <nowiki> is not used. It currently does
649 # not filter out characters which have special meaning only at the
650 # start of a line, such as "*".
651 function wfEscapeWikiText( $text )
653 $text = str_replace(
654 array( '[', "'", 'ISBN ' , '://' , "\n=" ),
655 array( '&#91;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
656 htmlspecialchars($text) );
657 return $text;
660 # Loads the entire MediaWiki namespace, returns the array
661 function wfLoadAllMessages()
663 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
664 $res = wfQuery( $sql, DB_READ, $fname );
666 $messages = array();
667 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
668 $messages[$row->cur_title] = $row->cur_text;
670 wfFreeResult( $res );
671 return $messages;
674 function wfQuotedPrintable( $string, $charset = "" )
676 # Probably incomplete; see RFC 2045
677 if( empty( $charset ) ) {
678 global $wgInputEncoding;
679 $charset = $wgInputEncoding;
681 $charset = strtoupper( $charset );
682 $charset = str_replace( "ISO-8859", "ISO8859", $charset ); // ?
684 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
685 $replace = $illegal . '\t ?_';
686 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
687 $out = "=?$charset?Q?";
688 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
689 $out .= "?=";
690 return $out;