Cyber Anakins Lightsaber - A Minishell-based backdoor
function __setchecked(v) { this.setState(v ? 3 : 0); }
var createToolButton = function(toolBar, img, hint, onclick)
{
var btn = createSpeedButton(toolBar, img, hint, onclick);
btn.setChecked = __setchecked;
btn.editor = toolBar.editor;
return btn;
}
function getParentAhref(editor, node)
{
while(node && node!=editor)
{
if (node.tagName=='A')
{
return node;
}
preNode = node;
node = node.parentNode;
}
return null;
}
function saveSelection() {
if (window.getSelection) {
var sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
}
function restoreSelection(range) {
if (range) {
if (window.getSelection) {
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.selection && range.select) {
range.select();
}
}
}
function selectImageDialog(onSelect)
{
sendXHR("api.php","action=images:list",
function(response)
{
var list = response.split("\n");
var dlg = openDialogBox("Выбор картинки");
dlg.innerElement.style.padding="10 10 10 10";
dlg.innerElement.style.width=900;
dlg.innerElement.style.height=450;
dlg.innerElement.style.verticalAlign="top";
var form = dlg.innerElement.appendChild(document.createElement('FORM'));
form.appendChild(document.createElement('DIV')).innerHTML = "Выберите картинку:";
createImageProperty(form, 200,160, list,
function(formData, onOK)
{
formData.append('action','images:upload');
sendXHR('api.php', formData,
function(response)
{
toastr("Файл залит. Ищите его в списке");
onOK(response);
},
function(error)
{
errorBox(error);
});
},
function(fileName, onOK)
{
sendXHR("api.php","action=images:del&file="+encodeURIComponent(fileName),
function(response)
{
onOK();
},
function(error)
{
errorBox(error);
});
},
function(fileName, width, height)
{
dlg.close();
onSelect(fileName, width, height);
}
);
var div = dlg.innerElement.appendChild(document.createElement('DIV'));
div.style.paddingTop = 10;
div.style.textAlign = "center";
var btn = div.appendChild(createInput('button'));
btn.value="Закрыть";
btn.onclick = function() { dlg.close(); }
dlg.alignInScreenCenter();
},
function(error)
{
errorBox("Не удалось загрузить список картинок! "+error);
});
}
function linkURLDialog(defValue, onOK)
{
var myHost = location.protocol + "//"+location.host+"/";
if (defValue.indexOf(myHost)==0)
{
defValue = defValue.substring(myHost.length);
}
var dlg = openDialogBox('Ссылка');
dlg.innerElement.style.padding="15 15 15 15";
dlg.innerElement.style.width = 500;
dlg.innerElement.style.height = 250;
dlg.innerElement.style.verticalAlign="top";
var form = dlg.innerElement.appendChild(document.createElement('FORM'));
form.appendChild(document.createElement('DIV')).innerHTML = "Введите ссылку:";
var input = form.appendChild(createInput('text'));
input.style.width="100%";
input.value = defValue;
var div = form.appendChild(document.createElement('DIV'));
div.innerHTML =
'Добавьте к ссылке <b>http://</b> или <b>https://</b>, если ссылка на другой ресурс.<br>'+
'Удалите из ссылки хост и путь, если ссылка на данный сайт.<br>'+
'Оставьте ссылку пустой, если хотите ее удалить.';
div.style.fontSize="14px";
div.style.paddingBottom=10;
div = form.appendChild(document.createElement('CENTER'));
var btn = div.appendChild(createInput('submit'));
btn.value="OK";
btn = div.appendChild(createInput('button'));
btn.value="Отмена";
btn.onclick=function()
{
dlg.close();
};
form.onsubmit = function(e)
{
if (e.preventDefault) e.preventDefault();
var text = input.value.trim();
dlg.close();
onOK(text);
return false;
};
input.focus();
dlg.alignInScreenCenter();
return dlg;
}
function imageTagDialog(img)
{
var dlg = openDialogBox('Ссылка');
var grid = createPropertyGrid(dlg.innerElement);
var srcText = img.src.replace(/http[s]{0,1}:\/\/[^\/]+\//,"");
var srcEditor = createEditProperty(grid, "Источник:", 250, srcText);
var selectBtn = srcEditor.editor.parentNode.appendChild(document.createElement('BUTTON'));
selectBtn.innerHTML = "<img src='htmleditor/select.png' width=16 height=16>";
selectBtn.style.borderWidth=1;
selectBtn.style.position="relative";
selectBtn.style.top=2;
selectBtn.style.padding="0 0 0 0"
selectBtn.style.minWidth=25;
selectBtn.style.width=25;
selectBtn.style.height=25;
selectBtn.style.borderRadius=0;
selectBtn.title="Выбрать из списка";
selectBtn.onclick = function()
{
selectImageDialog(function(fileName, width, height)
{
srcEditor.setValue(fileName);
widthEditor.setValue(width);
widthEditor.firstChild.innerHTML = "Ширина (ориг. "+width+"):";
heightEditor.setValue(height);
heightEditor.firstChild.innerHTML = "Высота (ориг. "+height+"):";
});
};
var widthEditor = createEditProperty(grid, "Ширина (ориг. "+ parseInt(img.naturalWidth)+"):", 100, parseInt(img.width));
var heightEditor = createEditProperty(grid, "Высота (ориг. "+ parseInt(img.naturalHeight)+"):", 100, parseInt(img.height));
var alignStrings = ["","left","right","center"];
var alignValue = 0;
for(var i=0; i < alignStrings.length; i++)
{
if (img.align == alignStrings[i])
{
alignValue = i;
break;
}
}
var alignEditor = createComboBoxProperty(grid, "Размещение (align):", alignStrings, alignValue);
var titleEditor = createEditProperty(grid, "Всплывающая подсказа:", "100%", img.title);
var marginLeftEditor = createEditProperty(grid, "Отступ слева:", "100", img.style.marginLeft);
var marginTopEditor = createEditProperty(grid, "Отступ сверху:", "100", img.style.marginTop);
var marginRightEditor = createEditProperty(grid, "Отступ справа:", "100", img.style.marginRight);
var marginBottomEditor = createEditProperty(grid, "Отступ снизу:", "100", img.style.marginBottom);
var div = dlg.innerElement.appendChild(document.createElement('DIV'));
div.style.padding="10 10 10 10";
var btn = div.appendChild(createInput('button'));
btn.value = "OK";
btn.style.width=100;
btn.style.height=35;
btn.onclick = function()
{
img.src = srcEditor.getValue();
img.width = widthEditor.getValue();
img.height = heightEditor.getValue();
img.align = alignStrings[alignEditor.getValue()];
img.title = titleEditor.getValue();
var marginLeft = marginLeftEditor.getValue();
var marginTop = marginTopEditor.getValue();
var marginRight = marginRightEditor.getValue();
var marginBottom = marginBottomEditor.getValue();
img.style.marginLeft = marginLeftEditor.getValue();
img.style.marginTop = marginTopEditor.getValue();
img.style.marginRight = marginRightEditor.getValue();
img.style.marginBottom = marginBottomEditor.getValue();
dlg.close();
};
btn = div.appendChild(createInput('button'));
btn.value = "Удалить";
btn.style.width=100;
btn.style.height=35;
btn.onclick = function()
{
yesNoDialog("Удалить данную картинку из текста?", "Удаление картинки",
function()
{
img.parentNode.removeChild(img);
dlg.close();
});
};
btn = div.appendChild(createInput('button'));
btn.value = "Отмена";
btn.style.width=100;
btn.style.height=35;
btn.onclick = function()
{
dlg.close();
};
dlg.alignInScreenCenter();
return dlg;
}
function onHTMLImageClick()
{
imageTagDialog(this);
}
function setOnClickForAllImages(elm)
{
if (elm.tagName=="IMG")
{
elm.onclick = onHTMLImageClick;
}
else
{
elm = elm.firstChild;
while(elm)
{
setOnClickForAllImages(elm);
elm = elm.nextSibling;
}
}
}
function createHTMLEditor(parent, width, height, html, canHTMLPaste)
{
var toolBar = createToolBar(parent);
var editor = parent.appendChild(document.createElement('DIV'));
editor.style.width=width;
editor.style.height=height;
editor.style.padding="10 10 10 10";
editor.style.overflowY="scroll";
editor.style.border="1px solid #cccccc";
editor.className = "htmlEditorClass";
editor.innerHTML = html;
editor.contentEditable=true;
setOnClickForAllImages(editor);
editor.toolBar = toolBar;
toolBar.editor = editor;
toolBar.boldBtn = createToolButton(toolBar,'htmleditor/bold.png', 'Жирный (Ctrl+B)',
function()
{
this.editor.focus();
document.execCommand("bold");
this.editor.onChange();
});
toolBar.italicBtn = createToolButton(toolBar,'htmleditor/italic.png','Наклонный (Ctrl+I)',
function()
{
this.editor.focus();
document.execCommand("italic");
this.editor.onChange();
});
toolBar.underlineBtn = createToolButton(toolBar,'htmleditor/underline.png','Подчёркнутый (Ctrl+U)',
function()
{
this.editor.focus();
document.execCommand("underline");
this.editor.onChange();
});
toolBar.superBtn = createToolButton(toolBar,"htmleditor/super.png","Верхний индекс",
function()
{
this.editor.focus();
document.execCommand("superscript");
this.editor.onChange();
});
toolBar.subBtn = createToolButton(toolBar,"htmleditor/sub.png","Нижний индекс",
function()
{
this.editor.focus();
document.execCommand("subscript");
this.editor.onChange();
});
createSeparator(toolBar);
toolBar.undoBtn = createSpeedButton(toolBar, "htmleditor/stdundobtn.png","Отмена (Ctrl+Z)",
function()
{
this.editor.focus();
document.execCommand("undo");
this.editor.onChange();
});
toolBar.copyBtn = createToolButton(toolBar, "htmleditor/CopyHS.png","Копировать (Ctrl+C)",
function()
{
this.editor.focus();
document.execCommand("copy");
});
toolBar.cutBtn = createToolButton(toolBar, "htmleditor/CutHS.png","Вырезать (Ctrl+X)",
function()
{
this.editor.focus();
document.execCommand("cut");
this.editor.onChange();
});
toolBar.pasteBtn = createToolButton(toolBar, "htmleditor/paste.png","Вставить (Ctrl+V)",
function()
{
errorBox("Для вставки используйте комбинацию клавиш Ctrl+V");
});
createSeparator(toolBar);
toolBar.leftAlignBtn = createToolButton(toolBar,"htmleditor/leftalign.png","Разместить слева",
function()
{
this.editor.focus();
document.execCommand("justifyLeft");
this.editor.onChange();
});
toolBar.centerAlignBtn = createToolButton(toolBar,"htmleditor/centeralign.png","Разместить по центру",
function()
{
this.editor.focus();
document.execCommand("justifyCenter");
this.editor.onChange();
});
toolBar.rightAlignBtn = createToolButton(toolBar,"htmleditor/rightalign.png","Разместить справа",
function()
{
this.editor.focus();
document.execCommand("justifyRight");
this.editor.onChange();
});
toolBar.justifyAlignBtn = createToolButton(toolBar, "htmleditor/justifyalign.png","Разместить справа",
function()
{
this.editor.focus();
document.execCommand("justifyFull");
this.editor.onChange();
});
createSeparator(toolBar);
toolBar.ulBtn = createToolButton(toolBar,"htmleditor/ul.png", "Ненумерованный список",
function()
{
this.editor.focus();
document.execCommand("insertUnorderedList");
this.editor.onChange();
});
toolBar.olBtn = createToolButton(toolBar,"htmleditor/ol.png", "Нумерованный список",
function()
{
this.editor.focus();
document.execCommand("insertOrderedList");
this.editor.onChange();
});
toolBar.linkBtn = createToolButton(toolBar,"htmleditor/link.png", "Ссылка",
function()
{
this.editor.focus();
var editor = this.editor;
var saveSel = saveSelection();
this.editor.focus();
var selection = window.getSelection();
if (selection)
{
var a = getParentAhref(this.editor, selection.focusNode);
if (a)
{
var linkURL = a.href;
linkURLDialog(linkURL,
function(linkURL)
{
restoreSelection(saveSel);
if (linkURL == '')
{
var txt = a.innerHTML;
a.parentNode.removeChild(a);
document.execCommand('insertHTML', false, txt);
}
else
{
a.href=linkURL;
if (linkURL.match(/^http[s]{0,1}:/i))
a.target = "_blank";
else
{
a.removeAttribute("target");
}
}
editor.onChange();
});
return;
}
}
var sText = document.getSelection();
if (sText!='')
{
linkURLDialog('',
function(linkURL)
{
restoreSelection(saveSel);
if (linkURL=='') return;
var html = '<a href="'+linkURL+'"';
if (linkURL.match(/^http[s]{0,1}:/i))
html+=' target=_blank';
html+='>'+sText+'</a>';
document.execCommand('insertHTML', false, html);
editor.onChange();
});
}
});
toolBar.imgBtn = createToolButton(toolBar,"htmleditor/image.png", "Вставить картинку",
function()
{
this.editor.focus();
var editor = this.editor;
var saveSel = saveSelection();
selectImageDialog(function(fileName, width, height)
{
restoreSelection(saveSel);
var html = '<img src="'+fileName+'" width='+width+' height='+height+'>';
document.execCommand('insertHTML', false, html);
editor.onChange();
setOnClickForAllImages(editor);
});
});
toolBar.end();
editor.onChange = function()
{
this.toolBar.boldBtn.setChecked(document.queryCommandValue("bold")=="true");
this.toolBar.italicBtn.setChecked(document.queryCommandValue("italic")=="true");
this.toolBar.underlineBtn.setChecked(document.queryCommandValue("underline")=="true");
this.toolBar.superBtn.setChecked(document.queryCommandValue("superscript")=="true");
this.toolBar.subBtn.setChecked(document.queryCommandValue("subscript")=="true");
this.toolBar.leftAlignBtn.setChecked(document.queryCommandValue("justifyLeft")=="true");
this.toolBar.centerAlignBtn.setChecked(document.queryCommandValue("justifyCenter")=="true");
this.toolBar.rightAlignBtn.setChecked(document.queryCommandValue("justifyRight")=="true");
this.toolBar.justifyAlignBtn.setChecked(document.queryCommandValue("justifyFull")=="true");
this.toolBar.olBtn.setChecked(document.queryCommandValue("insertOrderedList")=="true");
this.toolBar.ulBtn.setChecked(document.queryCommandValue("insertUnorderedList")=="true");
this.focus();
};
editor.addEventListener("keyup", editor.onChange);
editor.addEventListener("cut", editor.onChange);
editor.addEventListener("delete", editor.onChange);
editor.addEventListener("mouseup", editor.onChange);
editor.addEventListener("input", editor.onChange );
if (canHTMLPaste)
editor.addEventListener("paste", editor.onChange);
else
editor.addEventListener('paste', function(e)
{
this.focus();
e.preventDefault();
if (e.clipboardData)
{
var content = (e.originalEvent || e).clipboardData.getData('text/plain');
document.execCommand('insertText', false, content);
}
else
if (window.clipboardData)
{
var content = window.clipboardData.getData('Text');
document.selection.createRange().pasteHTML(content);
}
this.onChange();
/*
this.focus();
e.preventDefault();
var text = '';
if (e.clipboardData || e.originalEvent.clipboardData)
{
text = (e.originalEvent || e).clipboardData.getData('text/plain');
}
else
if (window.clipboardData)
{
text = window.clipboardData.getData('Text');
}
if (document.queryCommandSupported('insertText'))
{
document.execCommand('insertText', false, text);
}
else
{
document.execCommand('paste', false, text);
}
this.onChange();
*/
});
return editor;
}
function openHTMLEditorDialog(title, fileName, html, canHTMLPaste)
{
var dlg = openDialogBoxWithButtons(title, 800, 500);
dlg.innerElement.style.textAlign="left";
var editor = createHTMLEditor(dlg.innerElement, 778, 400, html, canHTMLPaste);
var btn = dlg.buttonPanel.appendChild(createInput('button'));
btn.value="OK";
btn.onclick=function()
{
var html = editor.innerHTML;
var post = "action=html:save";
post+="&fileName="+encodeURIComponent(fileName);
post+="&body="+encodeURIComponent(html);
var btn = this;
btn.disabled=true;
sendXHR("api.php", post, function()
{
location.reload();
},
function(error)
{
btn.disabled=false;
errorBox(error);
});
};
btn = dlg.buttonPanel.appendChild(createInput('button'));
btn.value="Закрыть";
btn.onclick=function()
{
dlg.close();
};
dlg.alignInScreenCenter();
return dlg;
}
function editMe(fileName)
{
var header = document.getElementById('header').innerText;
var html = document.getElementById('htmlCode').innerHTML;
openHTMLEditorWithHeaderDialog(fileName, fileName, html, header);
}
function newFileDialog()
{
var dlg = openDialogBox("Новый файл");
dlg.innerElement.style.width = 500;
dlg.innerElement.style.height = 150;
dlg.innerElement.style.textAlign="left";
var form = dlg.innerElement.appendChild(document.createElement('FORM'));
var div = form.appendChild(document.createElement('DIV'));
div.innerHTML = "Введите название файла (на латинице):";
var fileNameEditor = form.appendChild(createInput("text"));
fileNameEditor.style.width = 470;
div = form.appendChild(document.createElement('CENTER'));
div.style.paddingTop=20;
div.style.paddingBottom=20;
var btn = div.appendChild(createInput('submit'));
btn.value = "Создать";
btn = div.appendChild(createInput('button'));
btn.value = "Отмена";
btn.onclick = function()
{
dlg.close();
};
form.onsubmit = function(e)
{
if (e.preventDefault) e.preventDefault();
var btn = this;
var fileName = fileNameEditor.value.trim();
if (fileName=="")
{
errorBox("Введите имя файла!", fileNameEditor.focus());
return false;
}
btn.disabled=true;
sendXHR("api.php","action=html:newfile&fileName="+encodeURIComponent(fileName),
function(response)
{
location = response;
},
function(error)
{
btn.disabled=false;
errorBox(error);
});
return false;
};
dlg.alignInScreenCenter();
fileNameEditor.focus();
return dlg;
}
function openHTMLEditorWithHeaderDialog(title, fileName, html, header)
{
var dlg = openDialogBoxWithButtons(title, 800, 530);
dlg.innerElement.style.textAlign="left";
var div = dlg.innerElement.appendChild(document.createElement('DIV'));
div.innerHTML = 'Заголовок: ';
div.style.paddingBottom=8;
div.style.whiteSpace="noWrap";
var headerEditor = div.appendChild(createInput('text'));
headerEditor.style.width = 690;
headerEditor.value = header;
var editor = createHTMLEditor(dlg.innerElement, 778, 400, html);
var btn = dlg.buttonPanel.appendChild(createInput('button'));
btn.value="OK";
btn.onclick=function()
{
var html = editor.innerHTML;
var post = "action=html:save";
post+="&fileName="+encodeURIComponent(fileName);
post+='&header='+encodeURIComponent(headerEditor.value.trim());
post+="&body="+encodeURIComponent(html);
var btn = this;
btn.disabled=true;
sendXHR("api.php", post, function()
{
location.reload();
},
function(error)
{
btn.disabled=false;
errorBox(error);
});
};
btn = dlg.buttonPanel.appendChild(createInput('button'));
btn.value="Закрыть";
btn.onclick=function()
{
dlg.close();
};
dlg.alignInScreenCenter();
return dlg;
}
function editHTML(title, fileName, canHTMLPaste)
{
sendXHR(fileName,null,
function(response)
{
openHTMLEditorDialog(title, fileName, response, canHTMLPaste);
},
function(error)
{
errorBox(error);
});
}
May the force be with you, always.