Commit a4736cbf authored by Franco Fichtner's avatar Franco Fichtner

www: apply PSR2 style to a few files

JavaScript works like a charm, too.
parent 41e50c06
This diff is collapsed.
This diff is collapsed.
...@@ -8,37 +8,53 @@ ...@@ -8,37 +8,53 @@
// Here are the basic overloaded method definitions // Here are the basic overloaded method definitions
// The wrapper must be set BEFORE onreadystatechange is written to, since // The wrapper must be set BEFORE onreadystatechange is written to, since
// a bug in ActiveXObject prevents us from properly testing for it. // a bug in ActiveXObject prevents us from properly testing for it.
CsrfMagic = function(real) { CsrfMagic = function (real) {
// try to make it ourselves, if you didn't pass it // try to make it ourselves, if you didn't pass it
if (!real) try { real = new XMLHttpRequest; } catch (e) {;} if (!real) {
if (!real) try { real = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {;} try {
if (!real) try { real = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {;} real = new XMLHttpRequest; } } catch (e) {
if (!real) try { real = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch (e) {;} ;}
this.csrf = real; if (!real) {
try {
real = new ActiveXObject('Msxml2.XMLHTTP'); } } catch (e) {
;}
if (!real) {
try {
real = new ActiveXObject('Microsoft.XMLHTTP'); } } catch (e) {
;}
if (!real) {
try {
real = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } } catch (e) {
;}
this.csrf = real;
// properties // properties
var csrfMagic = this; var csrfMagic = this;
real.onreadystatechange = function() { real.onreadystatechange = function () {
csrfMagic._updateProps(); csrfMagic._updateProps();
return csrfMagic.onreadystatechange ? csrfMagic.onreadystatechange() : null; return csrfMagic.onreadystatechange ? csrfMagic.onreadystatechange() : null;
}; };
csrfMagic._updateProps(); csrfMagic._updateProps();
} }
CsrfMagic.prototype = { CsrfMagic.prototype = {
open: function(method, url, async, username, password) { open: function (method, url, async, username, password) {
if (method == 'POST') this.csrf_isPost = true; if (method == 'POST') {
this.csrf_isPost = true; }
// deal with Opera bug, thanks jQuery // deal with Opera bug, thanks jQuery
if (username) return this.csrf_open(method, url, async, username, password); if (username) {
else return this.csrf_open(method, url, async); return this.csrf_open(method, url, async, username, password); } else {
return this.csrf_open(method, url, async); }
}, },
csrf_open: function(method, url, async, username, password) { csrf_open: function (method, url, async, username, password) {
if (username) return this.csrf.open(method, url, async, username, password); if (username) {
else return this.csrf.open(method, url, async); return this.csrf.open(method, url, async, username, password); } else {
return this.csrf.open(method, url, async); }
}, },
send: function(data) { send: function (data) {
if (!this.csrf_isPost) return this.csrf_send(data); if (!this.csrf_isPost) {
return this.csrf_send(data); }
prepend = csrfMagicName + '=' + csrfMagicToken + '&'; prepend = csrfMagicName + '=' + csrfMagicToken + '&';
if (this.csrf_purportedLength === undefined) { if (this.csrf_purportedLength === undefined) {
this.csrf_setRequestHeader("Content-length", this.csrf_purportedLength + prepend.length); this.csrf_setRequestHeader("Content-length", this.csrf_purportedLength + prepend.length);
...@@ -47,11 +63,11 @@ CsrfMagic.prototype = { ...@@ -47,11 +63,11 @@ CsrfMagic.prototype = {
delete this.csrf_isPost; delete this.csrf_isPost;
return this.csrf_send(prepend + data); return this.csrf_send(prepend + data);
}, },
csrf_send: function(data) { csrf_send: function (data) {
return this.csrf.send(data); return this.csrf.send(data);
}, },
setRequestHeader: function(header, value) { setRequestHeader: function (header, value) {
// We have to auto-set this at the end, since we don't know how long the // We have to auto-set this at the end, since we don't know how long the
// nonce is when added to the data. // nonce is when added to the data.
if (this.csrf_isPost && header == "Content-length") { if (this.csrf_isPost && header == "Content-length") {
...@@ -60,23 +76,23 @@ CsrfMagic.prototype = { ...@@ -60,23 +76,23 @@ CsrfMagic.prototype = {
} }
return this.csrf_setRequestHeader(header, value); return this.csrf_setRequestHeader(header, value);
}, },
csrf_setRequestHeader: function(header, value) { csrf_setRequestHeader: function (header, value) {
return this.csrf.setRequestHeader(header, value); return this.csrf.setRequestHeader(header, value);
}, },
abort: function() { abort: function () {
return this.csrf.abort(); return this.csrf.abort();
}, },
getAllResponseHeaders: function() { getAllResponseHeaders: function () {
return this.csrf.getAllResponseHeaders(); return this.csrf.getAllResponseHeaders();
}, },
getResponseHeader: function(header) { getResponseHeader: function (header) {
return this.csrf.getResponseHeader(header); return this.csrf.getResponseHeader(header);
} // , } // ,
} }
// proprietary // proprietary
CsrfMagic.prototype._updateProps = function() { CsrfMagic.prototype._updateProps = function () {
this.readyState = this.csrf.readyState; this.readyState = this.csrf.readyState;
if (this.readyState == 4) { if (this.readyState == 4) {
this.responseText = this.csrf.responseText; this.responseText = this.csrf.responseText;
...@@ -85,20 +101,23 @@ CsrfMagic.prototype._updateProps = function() { ...@@ -85,20 +101,23 @@ CsrfMagic.prototype._updateProps = function() {
this.statusText = this.csrf.statusText; this.statusText = this.csrf.statusText;
} }
} }
CsrfMagic.process = function(base) { CsrfMagic.process = function (base) {
var prepend = csrfMagicName + '=' + csrfMagicToken; var prepend = csrfMagicName + '=' + csrfMagicToken;
if (base) return prepend + '&' + base; if (base) {
return prepend + '&' + base; }
return prepend; return prepend;
} }
// callback function for when everything on the page has loaded // callback function for when everything on the page has loaded
CsrfMagic.end = function() { CsrfMagic.end = function () {
// This rewrites forms AGAIN, so in case buffering didn't work this // This rewrites forms AGAIN, so in case buffering didn't work this
// certainly will. // certainly will.
forms = document.getElementsByTagName('form'); forms = document.getElementsByTagName('form');
for (var i = 0; i < forms.length; i++) { for (var i = 0; i < forms.length; i++) {
form = forms[i]; form = forms[i];
if (form.method.toUpperCase() !== 'POST') continue; if (form.method.toUpperCase() !== 'POST') {
if (form.elements[csrfMagicName]) continue; continue; }
if (form.elements[csrfMagicName]) {
continue; }
var input = document.createElement('input'); var input = document.createElement('input');
input.setAttribute('name', csrfMagicName); input.setAttribute('name', csrfMagicName);
input.setAttribute('value', csrfMagicToken); input.setAttribute('value', csrfMagicToken);
...@@ -132,7 +151,7 @@ if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && '\v' != 'v') { ...@@ -132,7 +151,7 @@ if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && '\v' != 'v') {
// jQuery didn't implement a new XMLHttpRequest function, so we have // jQuery didn't implement a new XMLHttpRequest function, so we have
// to do this the hard way. // to do this the hard way.
jQuery.csrf_ajax = jQuery.ajax; jQuery.csrf_ajax = jQuery.ajax;
jQuery.ajax = function( s ) { jQuery.ajax = function ( s ) {
if (s.type && s.type.toUpperCase() == 'POST') { if (s.type && s.type.toUpperCase() == 'POST') {
s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s)); s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
if ( s.data && s.processData && typeof s.data != "string" ) { if ( s.data && s.processData && typeof s.data != "string" ) {
...@@ -140,13 +159,13 @@ if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && '\v' != 'v') { ...@@ -140,13 +159,13 @@ if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && '\v' != 'v') {
} }
s.data = CsrfMagic.process(s.data); s.data = CsrfMagic.process(s.data);
} }
return jQuery.csrf_ajax( s ); return jQuery.csrf_ajax(s);
} }
} }
if (window.Prototype) { if (window.Prototype) {
// This works for script.aculo.us too // This works for script.aculo.us too
Ajax.csrf_getTransport = Ajax.getTransport; Ajax.csrf_getTransport = Ajax.getTransport;
Ajax.getTransport = function() { Ajax.getTransport = function () {
return new CsrfMagic(Ajax.csrf_getTransport()); return new CsrfMagic(Ajax.csrf_getTransport());
} }
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment