]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/AjaxFunctions.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / AjaxFunctions.php
1 <?php
2 /**
3  * Handler functions for Ajax requests
4  *
5  * @file
6  * @ingroup Ajax
7  */
8
9 if ( !defined( 'MEDIAWIKI' ) ) {
10         die( 1 );
11 }
12
13 /**
14  * Function converts an Javascript escaped string back into a string with
15  * specified charset (default is UTF-8).
16  * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
17  *
18  * @param $source String escaped with Javascript's escape() function
19  * @param $iconv_to String destination character set will be used as second parameter
20  * in the iconv function. Default is UTF-8.
21  * @return string
22  */
23 function js_unescape( $source, $iconv_to = 'UTF-8' ) {
24         $decodedStr = '';
25         $pos = 0;
26         $len = strlen ( $source );
27
28         while ( $pos < $len ) {
29                 $charAt = substr ( $source, $pos, 1 );
30                 if ( $charAt == '%' ) {
31                         $pos++;
32                         $charAt = substr ( $source, $pos, 1 );
33
34                         if ( $charAt == 'u' ) {
35                                 // we got a unicode character
36                                 $pos++;
37                                 $unicodeHexVal = substr ( $source, $pos, 4 );
38                                 $unicode = hexdec ( $unicodeHexVal );
39                                 $decodedStr .= code2utf( $unicode );
40                                 $pos += 4;
41                         } else {
42                                 // we have an escaped ascii character
43                                 $hexVal = substr ( $source, $pos, 2 );
44                                 $decodedStr .= chr ( hexdec ( $hexVal ) );
45                                 $pos += 2;
46                         }
47                 } else {
48                         $decodedStr .= $charAt;
49                         $pos++;
50                 }
51         }
52
53         if ( $iconv_to != "UTF-8" ) {
54                 $decodedStr = iconv( "utf-8", $iconv_to, $decodedStr );
55         }
56
57         return $decodedStr;
58 }
59
60 /**
61  * Function coverts number of utf char into that character.
62  * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
63  *
64  * @param $num Integer
65  * @return utf8char
66  */
67 function code2utf( $num ) {
68         if ( $num < 128 ) {
69                 return chr( $num );
70         }
71
72         if ( $num < 2048 ) {
73                 return chr( ( $num >> 6 ) + 192 ) . chr( ( $num&63 ) + 128 );
74         }
75
76         if ( $num < 65536 ) {
77                 return chr( ( $num >> 12 ) + 224 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
78         }
79
80         if ( $num < 2097152 ) {
81                 return chr( ( $num >> 18 ) + 240 ) . chr( ( ( $num >> 12 )&63 ) + 128 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
82         }
83
84         return '';
85 }
86
87 /**
88  * Called in some places (currently just extensions)
89  * to get the URL for a given file.
90  */
91 function wfAjaxGetFileUrl( $file ) {
92         $file = wfFindFile( $file );
93
94         if ( !$file || !$file->exists() ) {
95                 return null;
96         }
97
98         $url = $file->getUrl();
99
100         return $url;
101 }