forked from x-tools/xtools-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
341 lines (291 loc) · 10.8 KB
/
API.php
File metadata and controls
341 lines (291 loc) · 10.8 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
<?php
class API {
private $mFormats = array(
'json',
'jsonfm',
'php',
'phpfm',
'xml',
'xmlfm',
'yaml',
'yamlfm',
'txt',
'txtfm',
'dbg',
'dbgfm'
);
private $mHeaders = array(
'xml' => 'text/xml',
'json' => 'application/json',
'callback' => 'text/javascript',
'yaml' => 'application/yaml',
'php' => 'application/x-httpd-php',
'txt' => 'text/plain',
'dbg' => 'text/text',
'default' => 'text/html',
);
private $wgUrlProtocols = array(
'http://',
'https://',
'ftp://',
'irc://',
'gopher://',
'telnet://',
'nntp://',
'worldwind://',
'mailto:',
'news:',
'svn://',
);
private $mDefaultFormat = 'xml';
private $mFormat;
private $mIsHtml;
private $mCallback = false;
public function getFormats() {
return $this->mFormats;
}
public function getProp( $default = array() ) {
if( !isset( $_GET['prop'] ) ) {
return $default;
}
return explode( "|", $_GET['prop'] );
}
public function getFormat() {
global $_GET, $_POST;
$req = array_merge( $_GET, $_POST );
if( isset( $req['callback'] ) ) {
$this->mCallback = $req['callback'];
}
if( isset( $req['format'] ) && in_array( $req['format'], $this->mFormats ) ) {
$this->mIsHtml = (substr($req['format'], -2, 2) === 'fm'); // ends with 'fm'
if ($this->mIsHtml) {
$this->mFormat = substr($req['format'], 0, -2); // remove ending 'fm'
}
else {
$this->mFormat = $req['format'];
}
return $req['format'];
}
else {
$this->mFormat = $this->mDefaultFormat;
$this->mIsHtml = true;
return $this->mFormat;
}
}
public function setHeaders() {
if( isset( $this->mHeaders[$this->mFormat] ) && $this->mIsHtml == false ) {
if( $this->mCallback ) {
$mime = $this->mHeaders['callback'];
}
else {
$mime = $this->mHeaders[$this->mFormat];
}
}
else {
$mime = $this->mHeaders['default'];
}
header("Content-Type: $mime; charset=utf-8");
if( $this->mIsHtml ) {
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html>
<head>
<title>API Result</title>
</head>
<body>
<br/>
<small>
You are looking at the HTML representation of the ".strtoupper($this->mFormat)." format.<br/>
HTML is good for debugging, but probably is not suitable for your application.<br/>
</small>
<pre>";
}
}
public function showArray( $array ) {
if( $this->mFormat != 'xml' ) {
$array = $this->removeElementKey( '_element', $array );
}
switch($this->mFormat) {
case 'json':
$prefix = $suffix = "";
if(!is_null($this->mCallback)) {
$prefix = preg_replace("/[^][.\\'\\\"_A-Za-z0-9]/", "", $this->mCallback ) . "(";
$suffix = ")";
}
$this->outputText( $prefix . json_encode( $array ) . $suffix );
break;
case 'php':
$this->outputText( serialize( $array ) );
break;
case 'xml':
$this->outputText('<?xml version="1.0"?>');
$this->outputText(ArrayToXML::recXmlPrint('api',
$array,
$this->mIsHtml ? -2 : null));
break;
case 'yaml':
require_once('/data/project/xtools/yaml.php');
$this->outputText( Spyc :: YAMLDump($array) );
break;
case 'txt':
$this->outputText( print_r( $array, true ) );
break;
case 'dbg':
$this->outputText( var_export( $array, true ) );
break;
}
if( $this->mIsHtml ) {
echo "\n</pre>";
}
}
private function removeElementKey( $torem, $array ) {
foreach( $array as $key => $val ) {
if( is_array( $val ) ) {
$array[$key] = $this->removeElementKey( $torem, $val );
}
elseif( $torem === $key ) {
unset( $array[$torem] );
}
}
return $array;
}
private function outputText($text) {
if ($this->mIsHtml) {
echo $this->formatHTML($text);
} else {
echo $text;
}
}
private function formatHTML($text) {
// Escape everything first for full coverage
$text = htmlspecialchars($text);
// encode all comments or tags as safe blue strings
$text = preg_replace('/\<(!--.*?--|.*?)\>/', '<span style="color:blue;"><\1></span>', $text);
// identify URLs
$protos = implode("|", $this->wgUrlProtocols);
# This regex hacks around bug 13218 (" included in the URL)
$text = preg_replace("#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text);
// identify requests to api.php
$text = preg_replace("#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text);
return $text;
}
}
class ArrayToXML
{
public static function recXmlPrint($elemName, $elemValue, $indent, $doublequote = false) {
$retval = '';
if (!is_null($indent)) {
$indent += 2;
$indstr = "\n" . str_repeat(" ", $indent);
} else {
$indstr = '';
}
$elemName = str_replace(' ', '_', $elemName);
switch (gettype($elemValue)) {
case 'array' :
if (isset ($elemValue['*'])) {
$subElemContent = $elemValue['*'];
if ($doublequote)
$subElemContent = self::encodeAttribute($subElemContent);
unset ($elemValue['*']);
// Add xml:space="preserve" to the
// element so XML parsers will leave
// whitespace in the content alone
$elemValue['xml:space'] = 'preserve';
} else {
$subElemContent = null;
}
if (isset ($elemValue['_element'])) {
$subElemIndName = $elemValue['_element'];
unset ($elemValue['_element']);
} else {
$subElemIndName = null;
}
$indElements = array ();
$subElements = array ();
foreach ($elemValue as $subElemId => & $subElemValue) {
if (is_string($subElemValue) && $doublequote)
$subElemValue = self::encodeAttribute($subElemValue);
if (gettype($subElemId) === 'integer') {
$indElements[] = $subElemValue;
unset ($elemValue[$subElemId]);
} elseif (is_array($subElemValue)) {
$subElements[$subElemId] = $subElemValue;
unset ($elemValue[$subElemId]);
}
}
if (is_null($subElemIndName) && count($indElements))
die("($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
if (count($subElements) && count($indElements) && !is_null($subElemContent))
die("($elemName, ...) has content and subelements");
if (!is_null($subElemContent)) {
$retval .= $indstr . self::element($elemName, $elemValue, $subElemContent);
} elseif (!count($indElements) && !count($subElements)) {
$retval .= $indstr . self::element($elemName, $elemValue);
} else {
$retval .= $indstr . self::element($elemName, $elemValue, null);
foreach ($subElements as $subElemId => & $subElemValue)
$retval .= self::recXmlPrint($subElemId, $subElemValue, $indent);
foreach ($indElements as $subElemId => & $subElemValue)
$retval .= self::recXmlPrint($subElemIndName, $subElemValue, $indent);
$retval .= $indstr . self::closeElement($elemName);
}
break;
case 'object' :
// ignore
break;
default :
$retval .= $indstr . self::element($elemName, null, $elemValue);
break;
}
return $retval;
}
public static function encodeAttribute( $text ) {
$encValue = htmlspecialchars( $text, ENT_QUOTES );
// Whitespace is normalized during attribute decoding,
// so if we've been passed non-spaces we must encode them
// ahead of time or they won't be preserved.
$encValue = strtr( $encValue, array(
"\n" => ' ',
"\r" => ' ',
"\t" => '	',
) );
return $encValue;
}
public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
$out = '<' . $element;
if( !is_null( $attribs ) ) {
$out .= self::expandAttributes( $attribs );
}
if( is_null( $contents ) ) {
$out .= '>';
} else {
if( $allowShortTag && $contents === '' ) {
$out .= ' />';
} else {
$out .= '>' . htmlspecialchars( $contents ) . "</$element>";
}
}
return $out;
}
public static function closeElement( $element ) {
return "</$element>";
}
/**
* Given an array of ('attributename' => 'value'), it generates the code
* to set the XML attributes : attributename="value".
* The values are passed to Sanitizer::encodeAttribute.
* Return null if no attributes given.
* @param $attribs Array of attributes for an XML element
*/
public static function expandAttributes( $attribs ) {
$out = '';
if( is_null( $attribs ) ) {
return null;
} elseif( is_array( $attribs ) ) {
foreach( $attribs as $name => $val )
$out .= " {$name}=\"" . self::encodeAttribute( $val ) . '"';
return $out;
} else {
die( 'Expected attribute array, got something else in ' . __METHOD__ );
}
}
}