"<p id="userloginlink"><p>Don't have an account" is illegal xhtml
[mediawiki.git] / includes / AjaxFunctions.php
blob6e1b6be3ec402b017d42b1406f6aeb99a4dec07e
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
11 /**
12 * Function converts an Javascript escaped string back into a string with
13 * specified charset (default is UTF-8).
14 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
16 * @param $source String escaped with Javascript's escape() function
17 * @param $iconv_to String destination character set will be used as second parameter
18 * in the iconv function. Default is UTF-8.
19 * @return string
21 function js_unescape( $source, $iconv_to = 'UTF-8' ) {
22 $decodedStr = '';
23 $pos = 0;
24 $len = strlen ( $source );
26 while ( $pos < $len ) {
27 $charAt = substr ( $source, $pos, 1 );
28 if ( $charAt == '%' ) {
29 $pos++;
30 $charAt = substr ( $source, $pos, 1 );
32 if ( $charAt == 'u' ) {
33 // we got a unicode character
34 $pos++;
35 $unicodeHexVal = substr ( $source, $pos, 4 );
36 $unicode = hexdec ( $unicodeHexVal );
37 $decodedStr .= code2utf( $unicode );
38 $pos += 4;
39 } else {
40 // we have an escaped ascii character
41 $hexVal = substr ( $source, $pos, 2 );
42 $decodedStr .= chr ( hexdec ( $hexVal ) );
43 $pos += 2;
45 } else {
46 $decodedStr .= $charAt;
47 $pos++;
51 if ( $iconv_to != "UTF-8" ) {
52 $decodedStr = iconv( "UTF-8", $iconv_to, $decodedStr );
55 return $decodedStr;
58 /**
59 * Function coverts number of utf char into that character.
60 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
62 * @param $num Integer
63 * @return utf8char
65 function code2utf( $num ) {
66 if ( $num < 128 ) {
67 return chr( $num );
70 if ( $num < 2048 ) {
71 return chr( ( $num >> 6 ) + 192 ) . chr( ( $num&63 ) + 128 );
74 if ( $num < 65536 ) {
75 return chr( ( $num >> 12 ) + 224 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
78 if ( $num < 2097152 ) {
79 return chr( ( $num >> 18 ) + 240 ) . chr( ( ( $num >> 12 )&63 ) + 128 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
82 return '';
85 /**
86 * Called in some places (currently just extensions)
87 * to get the thumbnail URL for a given file at a given resolution.
89 function wfAjaxGetThumbnailUrl( $file, $width, $height ) {
90 $file = wfFindFile( $file );
92 if ( !$file || !$file->exists() ) {
93 return null;
96 $url = $file->getThumbnail( $width, $height )->url;
98 return $url;
102 * Called in some places (currently just extensions)
103 * to get the URL for a given file.
105 function wfAjaxGetFileUrl( $file ) {
106 $file = wfFindFile( $file );
108 if ( !$file || !$file->exists() ) {
109 return null;
112 $url = $file->getUrl();
114 return $url;