Jump to content

User:Ale jrb/Scripts/iglooInit.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* ======================================================== *\
** 			igloo frontend manager - init
\* ======================================================== */

	function iglooInitControl() {
		var iglooInit		= this;
		
		// initialisation sequence
		this.init = function(callback) {
			switch (callback) {
				default:
					// 1. Build the interface on the init page.
					//document.getElementsByTagName('title')[0].innerHTML = 
					document.title = 'igloo is loading - please wait';
					runIglooInterface.startInterface();
					break;
				case '1':
					// Nforcer hook - validate versioning
					runIglooInterface.addStatus('- Nforcer is validating your igloo version...');
					var ig_nforcer = new nforcer('User:Ale_jrb/Scripts/iglooSettings.js', 'this.version', iglooSettings.version, 'permission-block:true');
					ig_nforcer.onStatusChange = function (permitted) { if (permitted == true) { runIglooInterface.addStatus('- Nforcer OK!'); iglooInit.init('2'); } else { runIglooInterface.addStatus('<div style="color: #dd6666;">- Nforcer reports that the version of igloo in your browser cache is out of date. Fully clear your browser cache, and refresh this page. Loading stopped.</div>', true); } };
					ig_nforcer.check();
					break;
				case '2':
					// 2. Check browser.
					runIglooInterface.addStatus('- Checking browser for compatibility...');
					var browserTest = /(firefox|msie|opera|safari|chrome)/i;
					browserTest = browserTest.exec(navigator.userAgent.toLowerCase());
					if ( browserTest[1] == 'opera' ) {
						runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: igloo is not compatible with this browser. Loading stopped.</div>', true);
					} else if (browserTest[1] != 'firefox') {
						runIglooInterface.addStatus('<div style="color: #dd6666;">- Notice: igloo is designed for Mozilla Firefox, and may work incorrectly with other browsers.</div>', true);
						setTimeout(function() { iglooInit.init('3'); }, 2500);
					} else {
						runIglooInterface.addStatus('- Browser OK!'); iglooInit.init('3');
					}
					break;
				case '3':
					runIglooInterface.addStatus('- Checking for usergroups...');
					for (var i = 0; i < wgUserGroups.length; i ++) {
						if ( (wgUserGroups[i] == 'rollbacker') || (wgUserGroups[i] == 'sysop') ) { runIglooInterface.addStatus('- Usergroup OK!'); setTimeout(function() { iglooInit.init('4'); }, 500); return true; }
					}
					runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: rollback rights are required to use igloo. Loading stopped.</div>', true);
					break;
				case '4':
					runIglooInterface.addStatus('- Checking read API...');
					if (true == true) { runIglooInterface.addStatus('- Read API OK!'); } else { runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: read API access is requried for igloo for run. Loading stopped.</div>', true); return false; }
					runIglooInterface.addStatus('- Checking write API...');
					if (true == true) { runIglooInterface.addStatus('- Write API OK!'); } else { runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: write API access is requried for igloo for run. Loading stopped.</div>', true); return false; }
					setTimeout(function() { iglooInit.init('5'); }, 500);
					break;
				case '5':
					runIglooInterface.addStatus('- Checking for igloo configuration page...');
					var statusRequest = new wa_mediawikiApi();
					statusRequest.onCompleteAction = function(data) { iglooInit.data = data; iglooInit.init('6'); };
					statusRequest.getPage(iglooSettings.localBase + 'config', 1, 'content');
					break;
				case '6':
					if (this.data['page']['status'] != 'OK') {
						runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: igloo could not locate its configuration page. Loading stopped.</div>', true);
						return false;
					}
					runIglooInterface.addStatus('- Configuration page OK!');
					runIglooInterface.addStatus('- Checking igloo status...');

					var regTest = /(status:enabled;;)/i;
					regResult = regTest.exec(this.data['page']['revisions'][0]['content']);
					if (regResult == null) {
						runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: igloo is not enabled. It may be disabled, or the configuration page may be corrupt. Loading stopped.</div>', true);
						return false;
					}
					if ( (iglooSettings.developerMode == true) && (wgUserName != 'Ale jrb') && (wgUserName != 'Methecooldude') ) {
						runIglooInterface.addStatus('<div style="color: #dd6666;">- Warning: igloo is in developer mode, and is only accessible to developers - check back soon. Loading stopped.</div>', true);
						return false;
					}
					runIglooInterface.addStatus('- igloo enabled OK!');
					runIglooInterface.addStatus('<div style="font-weight: bold; color: #dd6666;">- Notice: igloo is about to establish a connection with iglooNet - a remote, non-Wikimedia server. To prevent this connection, or close it once established, click <span style="cursor: pointer; text-decoration: underline;" onclick="clearInterval(runIglooInit.timeoutInterval); window.close();">here</span>.</div>', true);
					
					i = 10;
					runIglooInit.timeoutInterval = setInterval(function() { 
																	 if (i == 0) { runIglooInterface.addStatus(' ... done!', false, true); runIglooInterface.addStatus('- Launching session manager...'); iglooInit.init('7'); clearInterval(runIglooInit.timeoutInterval); } 
																	 else if (i == 10) { runIglooInterface.addStatus('Connecting in (<span style="cursor: pointer; text-decoration: underline;" onclick="clearInterval(runIglooInit.timeoutInterval); runIglooInterface.addStatus(\' ... done!\', false, true); runIglooInterface.addStatus(\'- Launching session manager...\'); runIglooInit.init(\'7\'); ">skip</span>): '+i, true); i--; }
																	 else { runIglooInterface.addStatus(', '+i, true, true); i--; }
												}, 1000);
					break;
				case '7':
					this.currentSession = new iglooInitSession();
					this.currentSession.manageSession();
					break;
				case '8':
					runIglooInterface.addStatus('');
					runIglooInterface.addStatus('Loading complete! igloo will launch in a few seconds...');
					setTimeout(function() { 
													var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + iglooSettings.localBase + 'run&sessionkey='+runIglooInit.currentSession.sessionKey+'::::';
													window.location = url;
										}, 4000);
					break;
			}
		}
	}
	
	function iglooInitInterface() {
		var iglooInterface			= this;
		this.iglooStatus			= [];
		
		this.startInterface = function() {
			// blank page, generate interface object to work with.
			wa_window.prototype = new wa_document;
			wa_element.prototype = new wa_document;
			
			var docBase = new wa_document();
			docBase.wk_base.innerHTML = '';
			
			this.iglooBack = new wa_window(docBase.wk_base);
			this.iglooBack.win_bg = '#ededff';
			this.iglooBack.win_fill = true;
			this.iglooBack.applyAll();
			
			this.initInterface = new wa_window();
			this.initInterface.win_width = 650;
			this.initInterface.win_height = 330;
			this.initInterface.win_bg = '#ededff';
			this.initInterface.applyAll();
			this.initInterface.center('both', true, new Array(0, -50));
			
			this.initInterfaceStatus = new wa_window(this.initInterface);
			this.initInterfaceStatus.win_width = 450;
			this.initInterfaceStatus.win_height = 300;
			this.initInterfaceStatus.win_top = 17;
			this.initInterfaceStatus.win_left = 100;
			this.initInterfaceStatus.win_bd = '#bbbbff';
			this.initInterfaceStatus.win_bd_wd = 1;
			this.initInterfaceStatus.win_bg = '#fdfdff';
			this.initInterfaceStatus.win_padding = 7;
			this.initInterfaceStatus.win_content = '<div style="padding-top: 9px;">Welcome to igloo! Please be patient while igloo starts up, as this can take some time.<div id="iglooStatusContent" style="padding-top: 6px;"></div></div>';
			this.initInterfaceStatus.applyAll();
			
			this.initInterfaceInit = new wa_window(this.initInterface);
			this.initInterfaceInit.win_width = 230;
			this.initInterfaceInit.win_height = 17;
			this.initInterfaceInit.win_top = 0;
			this.initInterfaceInit.win_left = 370;
			this.initInterfaceInit.win_content = '<div style="color: #555588; font-size: 1.4em; font-weight: bold; text-align: center; width: 100%;">igloo is loading. Please wait...</div>';
			this.initInterfaceInit.win_bd = '#bbbbff';
			this.initInterfaceInit.win_bd_wd = 1;
			this.initInterfaceInit.win_bg = '#fdfdff';
			this.initInterfaceInit.applyAll();
			
			setTimeout(function() { runIglooInit.init('1'); }, 500);
		}
		
		this.addStatus = function(message, noEndline, attachToLast) {
			// add a message to the status output, and display it.
			if (this.iglooStatus.length > 15) this.iglooStatus.splice(0, 1);
			
			if ( (noEndline == null) || (noEndline == false) ) { message += '<br />'; }
			
			if ( (attachToLast == null) || (attachToLast == false) ) {
				this.iglooStatus.push(message);
			} else {
				this.iglooStatus[this.iglooStatus.length - 1] += message;
			}
			
			
			var echoString = '';
			for (var i = 0; i < this.iglooStatus.length; i ++) {
				echoString += this.iglooStatus[i];
			}
			
			if (document.getElementById('iglooStatusContent') != null) {
				document.getElementById('iglooStatusContent').innerHTML = echoString;
				return true;
			} else { return false; }
		}
	}
	
	function iglooInitSession() {
		var iglooSession 	= this;
		this.sessionKey		= false;
		
		this.manageSession = function(callback) {
			switch (callback) {
				default:
					if (this.sessionKey != false) return this.sessionkey;
					
					runIglooInterface.addStatus('- Session manager loaded.');
					runIglooInterface.addStatus('- Attempting to connect to the server, requesting token...');
					
					iglooImport(iglooSettings.remoteHost + 'main.php?action=connect&user=' + encodeURIComponent(wgUserName), true);
					
					this.count = 0;
					this.connection = setInterval('if (typeof iglooSessionRequest == \'undefined\') { if (runIglooInit.currentSession.count < iglooSettings.serverTimeout) { runIglooInit.currentSession.count ++; } else { clearInterval(runIglooInit.currentSession.connection); runIglooInterface.addStatus(\'<div style="color: #dd6666;">- Warning: igloo could not connect to the remote server. Loading stopped.</div>\', true); } } else { clearInterval(runIglooInit.currentSession.connection); runIglooInit.currentSession.sessionKey = iglooSessionRequest; runIglooInit.currentSession.manageSession(\'1\'); }', 1000);
					break;
					
				case '1':
					runIglooInterface.addStatus('- Connected to the remote server.');
					runIglooInterface.addStatus('- Requesting session scheme...');
					
					iglooImport(iglooSettings.remoteHost + 'main.php?action=connect&include=sessionHandleDwa', true);
					
					this.count = 0;
					this.connection = setInterval(''+
												  	'if (typeof iglooJavascriptLoadComplete == \'undefined\') { '+
												  		'if (runIglooInit.currentSession.count < iglooSettings.serverTimeout) { runIglooInit.currentSession.count ++; } else { '+
															'clearInterval(runIglooInit.currentSession.connection); '+
															'runIglooInterface.addStatus(\'<div style="color: #dd6666;">- Warning: igloo lost its connection to the remote server. Loading stopped.</div>\', true); '+
														'} '+
													'} else { '+
														'if (typeof iglooServerError != \'undefined\') { clearInterval(runIglooInit.currentSession.connection); runIglooInterface.addStatus(\'<div style="color: #dd6666;">- Warning: igloo server returned an error: \'+iglooServerError+\'. Loading stopped.</div>\', true); } else { '+
															'runIglooInit.currentSession.sessionKey = genSessionDwa(runIglooInit.currentSession.sessionKey);'+
															'clearInterval(runIglooInit.currentSession.connection);'+
															'runIglooInit.currentSession.manageSession(\'2\');'+
													' }}'+
												'', 1000);
					break;
				case '2':
					runIglooInterface.addStatus('- Obtained session key, checking with server...');
					
					iglooImport(iglooSettings.remoteHost + 'main.php?action=connect&user='+wgUserName+'&session='+encodeURIComponent(runIglooInit.currentSession.sessionKey), true);
					
					this.count = 0;
					this.connection = setInterval(''+
												  	'if (typeof iglooJavascriptSessionComplete == \'undefined\') { '+
												  		'if (runIglooInit.currentSession.count < iglooSettings.serverTimeout) { runIglooInit.currentSession.count ++; } else { '+
															'clearInterval(runIglooInit.currentSession.connection); '+
															'runIglooInterface.addStatus(\'<div style="color: #dd6666;">- Warning: igloo lost its connection to the remote server. Loading stopped.</div>\', true); '+
														'} '+
													'} else { '+
														'if (typeof iglooServerError != \'undefined\') { clearInterval(runIglooInit.currentSession.connection); runIglooInterface.addStatus(\'<div style="color: #dd6666;">- Warning: igloo server returned an error: \'+iglooServerError+\'. Loading stopped.</div>\', true); } else { '+
															'runIglooInit.init(\'8\');'+
															'clearInterval(runIglooInit.currentSession.connection);'+
													' }}'+
												'', 1000);
					break;
			}
		}
	}
	
	var runIglooInit = new iglooInitControl();
	var runIglooInterface = new iglooInitInterface();
	runIglooInit.init();