Systemtest_tk/script/userarea.js
2019-08-10 15:39:07 +03:00

798 lines
25 KiB
JavaScript

/**
* /script/userarea.js
* @version 1.7
* @desc Script file for the userarea tab
* @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
* @copy 2018 Fándly Gergő Zoltán
* License:
Systemtest.tk website's.
Copyright (C) 2018 Fándly Gergő Zoltán
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
var quillOpts={
modules: {
toolbar: [
["bold", "italic", "underline", "strike"],
["blockquote", "code-block"],
[{"script": "sub"}, {"script": "super"}],
[{"indent": "-1"}, {"indent": "+1"}],
[{"header": [1, 2, 3, 4, 5, 6, false]}],
[{"color": []}, {"background": []}],
[{"font": []}],
[{"align": []}],
["clean"]
],
history: {
delay: 2000,
maxStack: 200,
userOnly: true
}
},
theme: "snow"
};
/*
* FILESHARE
*/
function getMatchingIcon(ext){
var ficon="fa-file-alt";
if(ext=="doc" || ext=="docx" || ext=="docm"){
ficon="fa-file-word";
}
else if(ext=="ppt" || ext=="pptx" || ext=="pptm"){
ficon="fa-file-powerpoint";
}
else if(ext=="jpg" || ext=="png" || ext=="jpeg" || ext=="gif" || ext=="bmp"){
ficon="fa-file-image";
}
else if(ext=="xls" || ext=="xlsx" || ext=="xlsm"){
ficon="fa-file-excel";
}
else if(ext=="c" || ext=="cpp" || ext=="java" || ext=="js" || ext=="php" || ext=="cs" || ext=="css"){
ficon="fa-file-code";
}
else if(ext=="wav" || ext=="mp3" || ext=="flac" || ext=="ogg" || ext=="3gp"){
ficon="fa-file-audio";
}
else if(ext=="mp4" || ext=="avi" || ext=="mkv"){
ficon="fa-file-video";
}
else if(ext=="zip" || ext=="gz" || ext=="rar" || ext=="tar" || ext=="7z"){
ficon="fa-file-archive";
}
else if(ext=="pdf"){
ficon="fa-file-pdf";
}
return ficon;
}
function copyRefToClipboard(el){
$(el).prev("textarea").select();
document.execCommand("copy");
}
function loadFileList(el){
var filename=new RegExp("(.*)[.]");
var extension=new RegExp("(?:[a-zA-Z0-9](?![.]))+$");
$.each(el.files, function(i, val){
//check if file is bigger than 500M. In that case output error
if(val.size>500000000){
alert($("#langFileTooBig").text());
}
else{
if(filename.exec(val.name)){
var name=filename.exec(val.name)[1];
var ext=extension.exec(val.name)[0].toLowerCase();
}
else{
//if filename doesn't contain any dots
var name=val.name;
var ext="";
}
var obj=$("<div></div>");
var cont="<table style=\"width: 100%\"><tr><td rowspan=\"3\" style=\"width: 10em\"><i class=\"fa "+getMatchingIcon(ext)+"\" style=\"font-size: 10em\" name=\"fileicon\"></i></td>";
cont+="<td>"+$("#langName").text()+": <input type=\"text\" name=\"fname\" style=\"width: 95%\" value=\""+name+"\"></td></tr>";
cont+="<tr><td>"+$("#langExtension").text()+": "+ext+"</td></tr>";
cont+="<tr><td name=\"control\"><button class=\"red\" type=\"button\" onclick=\"removeMyFile(this)\"><i class=\"fa fa-trash\"></i></button></td></tr></table>";
cont+="<hr class=\"separator\">";
obj.html(cont);
var file=$("<span name=\"file\"></span>");
file.data("fileobj", val);
file.appendTo(obj);
obj.hide().appendTo("#files").slideDown();
}
});
$(el).val("");
}
function removeMyFile(el){
$(el).parent("td").parent("tr").parent("tbody").parent("table").parent("div").slideUp(function(){
$(this).remove();
});
}
function clearMyFiles(){
$("#files").slideUp(function(){
$("#files").html("");
});
$("#files").slideDown();
}
function startFileUpload(){
$("#files").children("div").each(function(){
var base=$(this).children("table").children("tbody").children("tr");
var name=base.children("td").children("input[name=fname]").val();
var file=$(this).children("span[name=file]").data("fileobj");
base.children("td[name=control]").html("<div class=\"progressbar\"><div style=\"width: 0%\"><span>0%</span></div></div>");
var progressbar=base.children("td[name=control]").children("div.progressbar");
var icon=base.children("td").children("i[name=fileicon]");
var data=new FormData();
data.append("upload_name", name);
data.append("upload_file", file);
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: data,
cache: false,
contentType: false,
processData: false,
xhr: function(){
var myXHR=$.ajaxSettings.xhr();
if(myXHR.upload){
myXHR.upload.addEventListener("progress", function(e){
if(e.lengthComputable){
var percent=Math.round(e.loaded*100/e.total*10)/10;
percent=percent.toString()+"%";
//show change on progressbar
progressbar.children("div").css("width", percent);
progressbar.children("div").children("span").text(percent);
//show change on icon
icon.css("background-image", "linear-gradient(to top, rgba(74,230,74,0.9), rgba(74,230,74,0.9) "+percent+", rgb(0,0,0) "+percent+")");
icon.css("color", "transparent");
icon.css("-webkit-background-clip", "text");
icon.css("background-clip", "text");
}
});
}
return myXHR;
},
success: function(response){
if(response=="quota"){
loadMessage();
var text=$("#langQuotaErr").text();
}
else if(response=="error"){
loadMessage();
var text="Error";
}
else{
var text="<textarea rows=3 cols=30 readonly>"+response+"</textarea><button type=\"button\" onclick=\"copyRefToClipboard(this)\">"+$("#langCopyToClip").text()+"</button>";
}
//replace progressbar
progressbar.parent().html(text);
}
});
});
}
function deleteFileFromServer(fid, caller){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"delete_file":fid},
success: function(response){
loadMessage();
if(response=="ok"){
$(caller).parent("td").parent("tr").css("background", "red").fadeOut(function(){
$(this).remove();
});
}
}
});
}
}
/*
* BLOG
*/
var quill;
function resetBlog(){
$("#postEdit").children("form")[0].reset();
quill=null;
$("#editorContainer").html("<div id=\"editor\"></div>");
$("#lastSaved").html("");
}
function newBlog(){
$("#postEdit").slideUp(function(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"blog_new":true},
success: function(response){
loadMessage();
if(response!="error"){
resetBlog();
$("#blog_id").val(response);
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
//init quill
quill=new Quill("#editor", quillOpts);
//slide down editor
$("#postEdit").slideDown();
}
}
});
});
}
function blogAutoSave(){
if($("#autosave").is(":checked")){
blogSave();
setTimeout(blogAutoSave, 180*1000);
}
}
function blogSave(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {
"blog_id": $("#blog_id").val(),
"blog_title": $("[name=blog_title]").val(),
"blog_content": JSON.stringify(quill.getContents()),
"blog_tags": $("[name=blog_tags]").val(),
"blog_published": ($("[name=blog_published]").is(":checked")?1:0)
},
success: function(response){
loadMessage();
if(response!="error"){
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
}
}
});
}
function blogDiscard(){
$("#postEdit").slideUp(function(){
resetBlog();
});
}
function blogEdit(id){
$("#postEdit").slideUp(function(){
$.ajax({
url: "/subs/loader.php",
type: "GET",
data: {"load": "userarea", "backend": true, "blog_get": id},
success: function(response){
loadMessage();
if(response!="error"){
var blog=(response instanceof Object)?repsonse:JSON.parse(response);
resetBlog();
//insert data
$("#blog_id").val(blog.id);
$("[name=blog_title]").val(blog.title);
$("[name=blog_tags]").val(blog.tags);
$("[name=blog_published]").attr("checked", blog.published=="1");
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
//init quill
quill=new Quill("#editor", quillOpts);
//check if delta is valid JSON
if(blog.content!=""){
quill.setContents(JSON.parse(blog.content));
}
//slide down editor
$("#postEdit").slideDown();
}
}
});
});
}
function blogDelete(id, el){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"blog_delete": id},
success: function(response){
loadMessage();
if(response=="ok"){
$(el).parent("td").parent("tr").css("background", "red").fadeOut(function(){
$(this).remove();
});
}
}
});
}
}
/*
* NEWS
*/
var quillEng;
var quillHun;
var quillRou;
function resetNews(){
$("#newsEdit").children("form")[0].reset();
quillEng=null;
quillHun=null;
quillRou=null;
$("#engEditorContainer").html("<div id=\"engEditor\"></div>");
$("#hunEditorContainer").html("<div id=\"hunEditor\"></div>");
$("#rouEditorContainer").html("<div id=\"rouEditor\"></div>");
$("#lastSaved").html("");
}
function newNews(){
$("#newsEdit").slideUp(function(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"news_new": true},
success: function(response){
loadMessage();
if(response!="error"){
resetNews();
//load id
$("#news_id").val(response);
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
//init quill instances
quillEng=new Quill("#engEditor", quillOpts);
quillHun=new Quill("#hunEditor", quillOpts);
quillRou=new Quill("#rouEditor", quillOpts);
//slide down
$("#newsEdit").slideDown();
}
}
});
});
}
function newsAutoSave(){
if($("#autosave").is(":checked")){
newsSave();
setTimeout(newsAutoSave, 180*1000);
}
}
function newsSave(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {
"news_id": $("#news_id").val(),
"news_subject_eng": $("[name=news_subject_eng]").val(),
"news_subject_hun": $("[name=news_subject_hun]").val(),
"news_subject_rou": $("[name=news_subject_rou]").val(),
"news_content_eng": JSON.stringify(quillEng.getContents()),
"news_content_hun": JSON.stringify(quillHun.getContents()),
"news_content_rou": JSON.stringify(quillRou.getContents()),
"news_published": ($("[name=news_published]").is(":checked")?1:0)
},
success: function(response){
loadMessage();
if(response!="error"){
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
}
}
});
}
function newsDiscard(){
$("#newsEdit").slideUp(function(){
resetNews();
});
}
function newsEdit(id){
$("#newsEdit").slideUp(function(){
$.ajax({
url: "/subs/loader.php",
type: "GET",
data: {"load": "userarea", "backend": true, "news_get": id},
success: function(response){
loadMessage();
if(response!="error"){
var news=(response instanceof Object)?response:JSON.parse(response);
resetNews();
//insert data
$("#news_id").val(news.id);
$("[name=news_subject_eng]").val(news.subject_eng);
$("[name=news_subject_hun]").val(news.subject_hun);
$("[name=news_subject_rou]").val(news.subject_rou);
$("[name=news_published]").attr("checked", news.published=="1");
//last saved
var now=new Date();
var h=checkTime(now.getHours());
var m=checkTime(now.getMinutes());
var s=checkTime(now.getSeconds());
$("#lastSaved").html(h+":"+m+":"+s);
//init quill instances
quillEng=new Quill("#engEditor", quillOpts);
quillHun=new Quill("#hunEditor", quillOpts);
quillRou=new Quill("#rouEditor", quillOpts);
if(news.content_eng!=""){
quillEng.setContents(JSON.parse(news.content_eng));
}
if(news.content_hun!=""){
quillHun.setContents(JSON.parse(news.content_hun));
}
if(news.content_rou!=""){
quillRou.setContents(JSON.parse(news.content_rou));
}
//slide down editor
$("#newsEdit").slideDown();
}
}
});
});
}
function newsDelete(id, el){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"news_delete": id},
success: function(response){
loadMessage();
if(response=="ok"){
$(el).parent("td").parent("tr").css("background", "red").fadeOut(function(){
$(this).remove();
});
}
}
});
}
}
/*
* ADMIN AREA
*/
function adminNewPassword(id){
var response=prompt($("#langEnterPassword").text());
if(response!=null && response!=""){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"new_password_user": id, "new_password": response},
success: function(){
loadMessage();
}
});
}
}
function adminChangeLevel(id){
var response=prompt($("#langEnterAccesslevel").text());
if(response!=null && response!=""){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"new_accesslevel_user": id, "new_accesslevel": response},
success: function(){
loadMessage();
}
});
}
}
function adminChangeQuota(id){
var response=prompt($("#langEnterQuota").text());
if(response!=null && response!=""){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"new_quota_user": id, "new_quota": response},
success: function(){
loadMessage();
}
});
}
}
function adminNewUser(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: $("#usernewForm").serialize(),
success: function(response){
loadMessage();
if(response=="ok"){
$("#usernewForm")[0].reset();
}
}
});
}
function adminFinishRequest(id, el){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"admin_finish_request": id},
success: function(response){
loadMessage();
if(response=="ok"){
$(el).parent("td").parent("tr").css("background", "red").fadeOut(function(){
$(this).remove();
});
}
}
});
}
}
/*
* PROFILE
*/
function profileUpdate(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: $("#profileForm").serialize(),
success: function(){
loadMessage();
}
});
}
function profileUpdatePassword(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: $("#profilePassword").serialize(),
success: function(response){
loadMessage();
if(response=="ok"){
$("#profilePassword").reset();
}
}
});
}
function profileSubmitShipping(){
$.ajax({
url: "/subs/pubkey.php",
type: "GET",
success: function(pubkey){
//set up encryptor
var encrypt=new JSEncrypt();
encrypt.setPublicKey(pubkey);
//update status
$("#encStatus").hide().removeClass().addClass("red").html("<i class=\"fa fa-lock-open\"></i> "+$("#langEncrypting").text()).fadeIn();
//encrypt data
var name=encrypt.encrypt($("[name=profile_shipping_name]").val());
var addr="Country: "+$("[name=address_country]").val()+"<br>";
addr+="Region: "+$("[name=address_region]").val()+"<br>";
addr+="City: "+$("[name=address_city]").val()+"<br>";
addr+="Address Line 1: "+$("[name=address_line1]").val()+"<br>";
addr+="Address line 2: "+$("[name=address_line2]").val()+"<br>";
addr+="Postal code: "+$("[name=address_zip]").val();
addr=encrypt.encrypt(addr);
var email=encrypt.encrypt($("[name=profile_shipping_email]").val());
var phone=encrypt.encrypt($("[name=profile_shipping_phone]").val());
//update status
$("#encStatus").fadeOut(function(){
$(this).removeClass().addClass("green").html("<i class=\"fa fa-lock\"></i> "+$("#langEncrypted").text()).fadeIn();
});
//post
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {
"profile_shipping_name": name,
"profile_shipping_address": addr,
"profile_shipping_email": email,
"profile_shipping_phone": phone
},
success: function(){
loadMessage();
}
});
}
});
}
function profileDeleteShipping(){
if(confirm($("#langConfDelShipping").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"profile_shipping_delete": true},
success: function(){
loadMessage();
}
});
}
}
function deleteProfile(){
if($("#delete_profile_box1").is(":checked") && $("#delete_profile_box2").is(":checked") && $("#delete_profile_box3").is(":checked")){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"delete_profile": true},
success: function(){
loadMessage();
}
});
}
}
}
function requestProfileData(){
if($("#request_profile_data_pgp").val()!="" && (!$("#request_profile_data_pgp").val().includes("-----BEGIN PGP PUBLIC KEY BLOCK-----") || !$("#request_profile_data_pgp").val().includes("-----END PGP PUBLIC KEY BLOCK-----"))){
alert($("#langPGPNotValid").text());
}
else{
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"request_profile_data": true, "request_profile_data_pgp": $("#request_profile_data_pgp").val()},
success: function(){
loadMessage();
}
});
}
}
/*
* Projects
*/
function projectsNew(){
$("#projectEditor")[0].reset();
$("#editor").slideDown();
$("#project_id").val("new");
}
function projectsSave(){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: $("#projectEditor").serialize(),
success: function(response){
loadMessage();
if(response!="err"){
$("#editor").slideUp(function(){
$("#projectEditor")[0].reset();
});
}
}
});
}
function projectsEditCancel(){
$("#editor").slideUp(function(){
$("#projectEditor")[0].reset();
});
}
function projectsEdit(id){
$.ajax({
url: "/subs/loader.php",
type: "GET",
data: {"load": "userarea", "backend": true, "projects_get": id},
success: function(response){
loadMessage();
if(response!="err"){
var cur=(response instanceof Object)?response:JSON.parse(response);
$("#project_id").val(cur.id);
$("[name=project_name").val(cur.name);
$("[name=project_desc").val(cur.description);
$("[name=project_path").val(cur.path);
$("[name=project_repo").val(cur.repo);
$("[name=project_status").val(cur.status);
$("[name=project_image").val(cur.image);
$("#editor").slideDown();
}
}
});
}
function projectsDelete(id, el){
if(confirm($("#langSure").text())){
$.ajax({
url: "/subs/loader.php?load=userarea&backend",
type: "POST",
data: {"projects_delete": id},
success: function(response){
loadMessage();
if(response=="ok"){
$(this).parent("td").parent("tr").css("background", "red").fadeOut(function(){
$(this).remove();
});
}
}
});
}
}