(bug 24022) Don't check extensions of files in uploadpage script when $wgStrictFileEx...
[lhc/web/wiklou.git] / skins / common / upload.js
1 function licenseSelectorCheck() {
2 var selector = document.getElementById( "wpLicense" );
3 var selection = selector.options[selector.selectedIndex].value;
4 if( selector.selectedIndex > 0 ) {
5 if( selection == "" ) {
6 // Option disabled, but browser is broken and doesn't respect this
7 selector.selectedIndex = 0;
8 }
9 }
10 // We might show a preview
11 wgUploadLicenseObj.fetchPreview( selection );
12 }
13
14 function wgUploadSetup() {
15 // Disable URL box if the URL copy upload source type is not selected
16 var e = document.getElementById( 'wpSourceTypeurl' );
17 if( e ) {
18 if( !e.checked ) {
19 var ein = document.getElementById( 'wpUploadFileURL' );
20 if(ein)
21 ein.setAttribute( 'disabled', 'disabled' );
22 }
23 }
24
25 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
26 // But for some reason, setting the text to itself works
27 var selector = document.getElementById("wpLicense");
28 if (selector) {
29 var ua = navigator.userAgent;
30 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
31 if (isMacIe) {
32 for (var i = 0; i < selector.options.length; i++) {
33 selector.options[i].text = selector.options[i].text;
34 }
35 }
36 }
37
38 // Toggle source type
39 var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
40 for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
41 sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
42 }
43
44 // AJAX wpDestFile warnings
45 if ( wgAjaxUploadDestCheck ) {
46 // Insert an event handler that fetches upload warnings when wpDestFile
47 // has been changed
48 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
49 wgUploadWarningObj.checkNow(this.value);
50 };
51 // Insert a row where the warnings will be displayed just below the
52 // wpDestFile row
53 var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
54 var row = optionsTable.insertRow( 1 );
55 var td = document.createElement( 'td' );
56 td.id = 'wpDestFile-warning';
57 td.colSpan = 2;
58
59 row.appendChild( td );
60 }
61
62 if ( wgAjaxLicensePreview ) {
63 // License selector check
64 document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
65
66 // License selector table row
67 var wpLicense = document.getElementById( 'wpLicense' );
68 var wpLicenseRow = wpLicense.parentNode.parentNode;
69 var wpLicenseTbody = wpLicenseRow.parentNode;
70
71 var row = document.createElement( 'tr' );
72 var td = document.createElement( 'td' );
73 row.appendChild( td );
74 td = document.createElement( 'td' );
75 td.id = 'mw-license-preview';
76 row.appendChild( td );
77
78 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
79 }
80
81
82 // fillDestFile setup
83 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
84 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
85 fillDestFilename( this.id );
86 };
87 }
88
89 /**
90 * Iterate over all upload source fields and disable all except the selected one.
91 *
92 * @param enabledId The id of the selected radio button
93 * @return emptiness
94 */
95 function toggleUploadInputs() {
96 // Iterate over all rows with UploadSourceField
97 var rows;
98 if ( document.getElementsByClassName ) {
99 rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
100 } else {
101 // Older browsers don't support getElementsByClassName
102 rows = new Array();
103
104 var allRows = document.getElementsByTagName( 'tr' );
105 for ( var i = 0; i < allRows.length; i++ ) {
106 if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
107 rows.push( allRows[i] );
108 }
109 }
110
111 for ( var i = 0; i < rows.length; i++ ) {
112 var inputs = rows[i].getElementsByTagName( 'input' );
113
114 // Check if this row is selected
115 var isChecked = true; // Default true in case wpSourceType is not found
116 for ( var j = 0; j < inputs.length; j++ ) {
117 if ( inputs[j].name == 'wpSourceType' )
118 isChecked = inputs[j].checked;
119 }
120
121 // Disable all unselected rows
122 for ( var j = 0; j < inputs.length; j++ ) {
123 if ( inputs[j].type != 'radio')
124 inputs[j].disabled = !isChecked;
125 }
126 }
127 }
128
129 var wgUploadWarningObj = {
130 'responseCache' : { '' : '&nbsp;' },
131 'nameToCheck' : '',
132 'typing': false,
133 'delay': 500, // ms
134 'timeoutID': false,
135
136 'keypress': function () {
137 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
138
139 // Find file to upload
140 var destFile = document.getElementById('wpDestFile');
141 var warningElt = document.getElementById( 'wpDestFile-warning' );
142 if ( !destFile || !warningElt ) return ;
143
144 this.nameToCheck = destFile.value ;
145
146 // Clear timer
147 if ( this.timeoutID ) {
148 window.clearTimeout( this.timeoutID );
149 }
150 // Check response cache
151 for (cached in this.responseCache) {
152 if (this.nameToCheck == cached) {
153 this.setWarning(this.responseCache[this.nameToCheck]);
154 return;
155 }
156 }
157
158 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
159 },
160
161 'checkNow': function (fname) {
162 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
163 if ( this.timeoutID ) {
164 window.clearTimeout( this.timeoutID );
165 }
166 this.nameToCheck = fname;
167 this.timeout();
168 },
169
170 'timeout' : function() {
171 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
172 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
173
174 // Get variables into local scope so that they will be preserved for the
175 // anonymous callback. fileName is copied so that multiple overlapping
176 // ajax requests can be supported.
177 var obj = this;
178 var fileName = this.nameToCheck;
179 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
180 function (result) {
181 obj.processResult(result, fileName)
182 }
183 );
184 },
185
186 'processResult' : function (result, fileName) {
187 removeSpinner( 'destcheck' );
188 this.setWarning(result.responseText);
189 this.responseCache[fileName] = result.responseText;
190 },
191
192 'setWarning' : function (warning) {
193 var warningElt = document.getElementById( 'wpDestFile-warning' );
194 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
195
196 this.setInnerHTML(warningElt, warning);
197
198 // Set a value in the form indicating that the warning is acknowledged and
199 // doesn't need to be redisplayed post-upload
200 if ( warning == '' || warning == '&nbsp;' ) {
201 ackElt[0].value = '';
202 } else {
203 ackElt[0].value = '1';
204 }
205
206 },
207 'setInnerHTML' : function (element, text) {
208 // Check for no change to avoid flicker in IE 7
209 if (element.innerHTML != text) {
210 element.innerHTML = text;
211 }
212 }
213 }
214
215 function fillDestFilename(id) {
216 if (!wgUploadAutoFill) {
217 return;
218 }
219 if (!document.getElementById) {
220 return;
221 }
222 // Remove any previously flagged errors
223 var e = document.getElementById( 'mw-upload-permitted' );
224 if( e ) e.className = '';
225
226 var e = document.getElementById( 'mw-upload-prohibited' );
227 if( e ) e.className = '';
228
229 var path = document.getElementById(id).value;
230 // Find trailing part
231 var slash = path.lastIndexOf('/');
232 var backslash = path.lastIndexOf('\\');
233 var fname;
234 if (slash == -1 && backslash == -1) {
235 fname = path;
236 } else if (slash > backslash) {
237 fname = path.substring(slash+1, 10000);
238 } else {
239 fname = path.substring(backslash+1, 10000);
240 }
241
242 // Clear the filename if it does not have a valid extension.
243 // URLs are less likely to have a useful extension, so don't include them in the
244 // extension check.
245 if( $wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
246 var found = false;
247 if( fname.lastIndexOf( '.' ) != -1 ) {
248 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
249 for( var i = 0; i < wgFileExtensions.length; i++ ) {
250 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
251 found = true;
252 break;
253 }
254 }
255 }
256 if( !found ) {
257 // Not a valid extension
258 // Clear the upload and set mw-upload-permitted to error
259 document.getElementById(id).value = '';
260 var e = document.getElementById( 'mw-upload-permitted' );
261 if( e ) e.className = 'error';
262
263 var e = document.getElementById( 'mw-upload-prohibited' );
264 if( e ) e.className = 'error';
265
266 // Clear wpDestFile as well
267 var e = document.getElementById( 'wpDestFile' )
268 if( e ) e.value = '';
269
270 return false;
271 }
272 }
273
274 // Capitalise first letter and replace spaces by underscores
275 // FIXME: $wgCapitalizedNamespaces
276 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
277
278 // Output result
279 var destFile = document.getElementById('wpDestFile');
280 if (destFile) {
281 destFile.value = fname;
282 wgUploadWarningObj.checkNow(fname) ;
283 }
284 }
285
286 function toggleFilenameFiller() {
287 if(!document.getElementById) return;
288 var upfield = document.getElementById('wpUploadFile');
289 var destName = document.getElementById('wpDestFile').value;
290 if (destName=='' || destName==' ') {
291 wgUploadAutoFill = true;
292 } else {
293 wgUploadAutoFill = false;
294 }
295 }
296
297 var wgUploadLicenseObj = {
298
299 'responseCache' : { '' : '' },
300
301 'fetchPreview': function( license ) {
302 if( !wgAjaxLicensePreview ) return;
303 for (cached in this.responseCache) {
304 if (cached == license) {
305 this.showPreview( this.responseCache[license] );
306 return;
307 }
308 }
309 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
310
311 var title = document.getElementById('wpDestFile').value;
312 if ( !title ) title = 'File:Sample.jpg';
313
314 var url = wgScriptPath + '/api' + wgScriptExtension
315 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
316 + '&title=' + encodeURIComponent( title )
317 + '&prop=text&pst&format=json';
318
319 var req = sajax_init_object();
320 req.onreadystatechange = function() {
321 if ( req.readyState == 4 && req.status == 200 )
322 wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
323 };
324 req.open( 'GET', url, true );
325 req.send( '' );
326 },
327
328 'processResult' : function( result, license ) {
329 removeSpinner( 'license' );
330 this.responseCache[license] = result['parse']['text']['*'];
331 this.showPreview( this.responseCache[license] );
332
333 },
334
335 'showPreview' : function( preview ) {
336 var previewPanel = document.getElementById( 'mw-license-preview' );
337 if( previewPanel.innerHTML != preview )
338 previewPanel.innerHTML = preview;
339 }
340
341 }
342
343 addOnloadHook( wgUploadSetup );