2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Object to handle the creation of operation ingredients.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} config - The configuration object for this ingredient.
|
2017-03-23 17:52:20 +00:00
|
|
|
* @param {App} app - The main view object for CyberChef.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Manager} manager - The CyberChef event manager.
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
var HTMLIngredient = function(config, app, manager) {
|
2016-11-28 10:42:58 +00:00
|
|
|
this.app = app;
|
|
|
|
this.manager = manager;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
this.name = config.name;
|
|
|
|
this.type = config.type;
|
|
|
|
this.value = config.value;
|
|
|
|
this.disabled = config.disabled || false;
|
2017-01-31 18:24:56 +00:00
|
|
|
this.disableArgs = config.disableArgs || false;
|
2016-11-28 10:42:58 +00:00
|
|
|
this.placeholder = config.placeholder || false;
|
|
|
|
this.target = config.target;
|
2017-01-31 18:24:56 +00:00
|
|
|
this.toggleValues = config.toggleValues;
|
|
|
|
this.id = "ing-" + this.app.nextIngId();
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the ingredient in HTML.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
HTMLIngredient.prototype.toHtml = function() {
|
2016-12-14 16:39:17 +00:00
|
|
|
var inline = (this.type === "boolean" ||
|
|
|
|
this.type === "number" ||
|
|
|
|
this.type === "option" ||
|
2017-01-31 18:24:56 +00:00
|
|
|
this.type === "shortString" ||
|
|
|
|
this.type === "binaryShortString"),
|
2016-11-28 10:42:58 +00:00
|
|
|
html = inline ? "" : "<div class='clearfix'> </div>",
|
|
|
|
i, m;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<div class='arg-group" + (inline ? " inline-args" : "") +
|
2016-12-14 16:39:17 +00:00
|
|
|
(this.type === "text" ? " arg-group-text" : "") + "'><label class='arg-label' for='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.id + "'>" + this.name + "</label>";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
switch (this.type) {
|
|
|
|
case "string":
|
2017-01-31 18:24:56 +00:00
|
|
|
case "binaryString":
|
|
|
|
case "byteArray":
|
|
|
|
html += "<input type='text' id='" + this.id + "' class='arg arg-input' arg-name='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.name + "' value='" + this.value + "'" +
|
|
|
|
(this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
|
|
|
|
break;
|
2017-01-31 18:24:56 +00:00
|
|
|
case "shortString":
|
|
|
|
case "binaryShortString":
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<input type='text' id='" + this.id +
|
2017-01-31 18:24:56 +00:00
|
|
|
"'class='arg arg-input short-string' arg-name='" + this.name + "'value='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.value + "'" + (this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
|
|
|
|
break;
|
2017-01-31 18:24:56 +00:00
|
|
|
case "toggleString":
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<div class='input-group'><div class='input-group-btn'>\
|
|
|
|
<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'\
|
|
|
|
aria-haspopup='true' aria-expanded='false'" +
|
2017-01-31 18:24:56 +00:00
|
|
|
(this.disabled ? " disabled='disabled'" : "") + ">" + this.toggleValues[0] +
|
2016-11-28 10:42:58 +00:00
|
|
|
" <span class='caret'></span></button><ul class='dropdown-menu'>";
|
2017-01-31 18:24:56 +00:00
|
|
|
for (i = 0; i < this.toggleValues.length; i++) {
|
|
|
|
html += "<li><a href='#'>" + this.toggleValues[i] + "</a></li>";
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
html += "</ul></div><input type='text' class='arg arg-input toggle-string'" +
|
|
|
|
(this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + "></div>";
|
|
|
|
break;
|
|
|
|
case "number":
|
2017-01-31 18:24:56 +00:00
|
|
|
html += "<input type='number' id='" + this.id + "'class='arg arg-input' arg-name='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.name + "'value='" + this.value + "'" +
|
|
|
|
(this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
|
|
|
|
break;
|
|
|
|
case "boolean":
|
2017-01-31 18:24:56 +00:00
|
|
|
html += "<input type='checkbox' id='" + this.id + "'class='arg' arg-name='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.name + "'" + (this.value ? " checked='checked' " : "") +
|
|
|
|
(this.disabled ? " disabled='disabled'" : "") + ">";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (this.disableArgs) {
|
|
|
|
this.manager.addDynamicListener("#" + this.id, "click", this.toggleDisableArgs, this);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "option":
|
2017-01-31 18:24:56 +00:00
|
|
|
html += "<select class='arg' id='" + this.id + "'arg-name='" + this.name + "'" +
|
2016-11-28 10:42:58 +00:00
|
|
|
(this.disabled ? " disabled='disabled'" : "") + ">";
|
|
|
|
for (i = 0; i < this.value.length; i++) {
|
2016-12-14 16:39:17 +00:00
|
|
|
if ((m = this.value[i].match(/\[([a-z0-9 -()^]+)\]/i))) {
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<optgroup label='" + m[1] + "'>";
|
2016-12-14 16:39:17 +00:00
|
|
|
} else if ((m = this.value[i].match(/\[\/([a-z0-9 -()^]+)\]/i))) {
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "</optgroup>";
|
|
|
|
} else {
|
|
|
|
html += "<option>" + this.value[i] + "</option>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
html += "</select>";
|
|
|
|
break;
|
2017-01-31 18:24:56 +00:00
|
|
|
case "populateOption":
|
|
|
|
html += "<select class='arg' id='" + this.id + "'arg-name='" + this.name + "'" +
|
2016-11-28 10:42:58 +00:00
|
|
|
(this.disabled ? " disabled='disabled'" : "") + ">";
|
|
|
|
for (i = 0; i < this.value.length; i++) {
|
2016-12-14 16:39:17 +00:00
|
|
|
if ((m = this.value[i].name.match(/\[([a-z0-9 -()^]+)\]/i))) {
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<optgroup label='" + m[1] + "'>";
|
2016-12-14 16:39:17 +00:00
|
|
|
} else if ((m = this.value[i].name.match(/\[\/([a-z0-9 -()^]+)\]/i))) {
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "</optgroup>";
|
|
|
|
} else {
|
|
|
|
html += "<option populate-value='" + this.value[i].value + "'>" +
|
|
|
|
this.value[i].name + "</option>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
html += "</select>";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
this.manager.addDynamicListener("#" + this.id, "change", this.populateOptionChange, this);
|
2016-11-28 10:42:58 +00:00
|
|
|
break;
|
2017-01-31 18:24:56 +00:00
|
|
|
case "editableOption":
|
2016-11-28 10:42:58 +00:00
|
|
|
html += "<div class='editable-option'>";
|
|
|
|
html += "<select class='editable-option-select' id='sel-" + this.id + "'" +
|
|
|
|
(this.disabled ? " disabled='disabled'" : "") + ">";
|
|
|
|
for (i = 0; i < this.value.length; i++) {
|
|
|
|
html += "<option value='" + this.value[i].value + "'>" + this.value[i].name + "</option>";
|
|
|
|
}
|
|
|
|
html += "</select>";
|
|
|
|
html += "<input class='arg arg-input editable-option-input' id='" + this.id +
|
2017-01-31 18:24:56 +00:00
|
|
|
"'arg-name='" + this.name + "'" + " value='" + this.value[0].value + "'" +
|
2016-11-28 10:42:58 +00:00
|
|
|
(this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">";
|
|
|
|
html += "</div>";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
this.manager.addDynamicListener("#sel-" + this.id, "change", this.editableOptionChange, this);
|
2016-11-28 10:42:58 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
2017-01-31 18:24:56 +00:00
|
|
|
html += "<textarea id='" + this.id + "' class='arg' arg-name='" +
|
2016-11-28 10:42:58 +00:00
|
|
|
this.name + "'" + (this.disabled ? " disabled='disabled'" : "") +
|
|
|
|
(this.placeholder ? " placeholder='" + this.placeholder + "'" : "") + ">" +
|
|
|
|
this.value + "</textarea>";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
html += "</div>";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for argument disable toggle.
|
2017-01-31 18:24:56 +00:00
|
|
|
* Toggles disabled state for all arguments in the disableArgs list for this ingredient.
|
2016-11-28 10:42:58 +00:00
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
HTMLIngredient.prototype.toggleDisableArgs = function(e) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var el = e.target,
|
|
|
|
op = el.parentNode.parentNode,
|
|
|
|
args = op.querySelectorAll(".arg-group"),
|
|
|
|
els;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
for (var i = 0; i < this.disableArgs.length; i++) {
|
|
|
|
els = args[this.disableArgs[i]].querySelectorAll("input, select, button");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
for (var j = 0; j < els.length; j++) {
|
|
|
|
if (els[j].getAttribute("disabled")) {
|
|
|
|
els[j].removeAttribute("disabled");
|
|
|
|
} else {
|
|
|
|
els[j].setAttribute("disabled", "disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
this.manager.recipe.ingChange();
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for populate option changes.
|
|
|
|
* Populates the relevant argument with the specified value.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
HTMLIngredient.prototype.populateOptionChange = function(e) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var el = e.target,
|
|
|
|
op = el.parentNode.parentNode,
|
|
|
|
target = op.querySelectorAll(".arg-group")[this.target].querySelector("input, select, textarea");
|
|
|
|
|
|
|
|
target.value = el.childNodes[el.selectedIndex].getAttribute("populate-value");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
this.manager.recipe.ingChange();
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for editable option changes.
|
|
|
|
* Populates the input box with the selected value.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
HTMLIngredient.prototype.editableOptionChange = function(e) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var select = e.target,
|
|
|
|
input = select.nextSibling;
|
|
|
|
|
|
|
|
input.value = select.childNodes[select.selectedIndex].value;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
this.manager.recipe.ingChange();
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default HTMLIngredient;
|