-
Notifications
You must be signed in to change notification settings - Fork 0
/
includes.js
451 lines (444 loc) · 21.2 KB
/
includes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* @author Toprak Koc
* @constructor
*/
$(document).ready(function(){
JFCustomWidget.subscribe("ready", function (formId) {
setFrameSize(100);
const form_id = formId["formID"];
const filekey = generate_token(16);
const qid = JFCustomWidget.getWidgetSetting("qid");
const clouds = JFCustomWidget.getWidgetSetting("clouds");
checkDB(form_id,qid,clouds);
var status = JSON.parse(document.getElementById("hidden").value);
dropboxAuth(status,clouds,form_id,qid);
driveAuth(status,clouds,form_id,qid);
// mahmutla takılıyoruz
$("#upload").change(function(e) {
e.preventDefault();
startUpload("upload");
$("#url").change(function(e){
returnSubmit(JSON.stringify(document.getElementById("url").value),true);
});
// var folder = document.getElementById("folder").value;
// removeFiles(form_id,folder);
});
JFCustomWidget.subscribe("submit", submitFunc);
/**
* Starts Upload Jobs
* @param {String} elementId - Id of select button
*/
function startUpload(elementId){
var input = document.getElementById(elementId);
var currentHeight = window.innerHeight;
var totalItemHeights = (input.files.length) * 90;
var height = currentHeight + totalItemHeights;
setFrameSize(height);
for(var x = 0;x < input.files.length; x++){
var file = input.files[x];
upload(form_id,qid,file,filekey,x);
}
}
/**
* Checks Database for Any Saved Keys
* @param {String} form_id - Id of form
* @param {String} qid - Question Id
* @param {String} clouds - Selected Cloud Services
*/
function checkDB(form_id,qid,clouds){
var formdata = new FormData();
formdata.append("formid", form_id);
formdata.append("qid",qid);
formdata.append("action", "select");
formdata.append("clouds",clouds);
document.getElementById("hidden").value = ajaxRequest("database.php",formdata,false);
}
/**
* Authorizing Dropbox and Saving Keys
* @param {Object} status - checkDB Result
* @param {String} clouds - Selected Cloud Services
*/
function dropboxAuth(status,clouds){
if((clouds.toLowerCase()).indexOf("dropbox") != -1){
if(empty(status.Dropbox)){
// document.getElementById("dropbox").value = false;
var client_id = "9pdfdpjr2pnqzmo";
var dbx = new Dropbox.Dropbox({clientId: client_id});
var authUrl = dbx.getAuthenticationUrl("https://toprak.jotform.pro/Adapter/oauth.html");
var AuthWindow = window.open(authUrl,"AuthWindow",'location=0,status=0,width=800,height=400');
$("#key").change(function(){
key = document.getElementById("key").value;
var formdata = new FormData();
formdata.append("key",key);
formdata.append("qid",qid);
formdata.append("action", "insert");
formdata.append("value","dropbox");
formdata.append("clouds","Dropbox");
formdata.append("formid", form_id);
ajaxRequest("database.php",formdata,false);
AuthWindow.close();
});
}
}
else{
document.getElementById("dropbox").value = true;
}
}
/**
* Authorizing Drive and Saving Keys
* @param {Object} status - CheckDB Result
* @param {String} clouds - Selected Cloud Services
* @param {String} form_id - Id of Form
* @param {String} qid - Question Id
*/
function driveAuth(status,clouds,form_id,qid){
if((clouds.toLowerCase()).indexOf("drive") != -1){
if(empty(status.Drive)){
// document.getElementById("drive").value = false;
this.onload=function(){};handleClientLoad();
var key = "AIzaSyAbodyJck6jBlV4oV9A3-E6xdMvf3JsdDg";
var client = "424058548993-sbd0hd1dflne6507emj91gad1pf9bebc.apps.googleusercontent.com";
var discovery = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
var scopes = "https://www.googleapis.com/auth/drive";
function handleClientLoad() {
gapi.load("client:auth2",initClient);
}
function initClient() {
gapi.client.init({
apiKey: key,
clientId: client,
discoveryDocs: discovery,
scope: scopes
}).then(function (){
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
handleAuthClick();
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
// console.log("signed");
}
addEventListener
{
// console.log("error");
}
}
function handleAuthClick() {
// console.log("handleAuthClick");
gapi.auth2.getAuthInstance().signIn();
gapi.auth2.getAuthInstance().grantOfflineAccess().then(function(code){
key = JSON.stringify(code);
var formdata = new FormData();
formdata.append("qid",qid);
formdata.append("value","drive");
formdata.append("key",key);
formdata.append("formid", form_id);
formdata.append("clouds", "Drive");
formdata.append("action", "insert");
ajaxRequest("database.php",formdata,false);
});
}
}
}
}
function empty(input) {
return input == "" || input == 0 || input == "0" || input == null || input == false || input == undefined || input == "null" || input == "{}" ? true : false;
}
/**
* Saving selected files to tmp folder then starts uploading to cloud services
* @param {String} formid - Id of Form
* @param {String} qid - Question Id
* @param {File} file - Selected File
* @param {String} filekey - File key is like a form id its uniq and for multiple uploads
* @param {String} progressName - Progressbar's Name
*/
function upload(formid,qid,file,filekey,progressName){
var folder = null;
var formdata = new FormData();
var id = createDiv(generate_token(4),file.name,getFileExtension(file.name));
formdata.append("qid",qid);
formdata.append("file", file);
formdata.append("formid",formid);
formdata.append("filekey",filekey);
formdata.append("action","save");
formdata.append("key",document.getElementById("aws").value);
var progressBar = "progressBar-"+id;
save(formdata,filekey,progressBar).done(function(result){
if(empty(document.getElementById("folder").value)){
document.getElementById("folder").value = result;
folder = JSON.parse(document.getElementById("folder").value);
document.getElementById("folder").value = folder["folder"];
}
let qid = JFCustomWidget.getWidgetSetting("qid");
folder = document.getElementById("folder").value;
var callback = null;
var folderKey = document.getElementById("folderKey").value;
callBack = JSON.parse(sendJob(qid,file.name,formid,folder,folderKey, getAwsKeys()));
if(empty(document.getElementById('folderKey').value)){
let folderId = JSON.parse(callBack.Drive);
folderId = folderId.Folder;
document.getElementById("folderKey").value = folderId;
}
console.log(callBack);
dropbox = JSON.parse(callBack.Dropbox);
dropboxUrl = dropbox.Url;
dropboxError = dropbox.Error;
dropboxRemove = dropbox.Remove;
console.log("----DBX----\n" + dropboxError +"\n"+ dropboxUrl +"\n"+ dropboxRemove+"\n----DBX----");
drive = JSON.parse(callBack.Drive);
driveUrl = drive.Url;
driveError = drive.Error;
driveRemove = drive.Remove;
console.log("----GDR----\n"+driveError +"\n"+ driveUrl +"\n"+ driveRemove+"\n----GDR----");
amazon = JSON.parse(callBack.AmazonWebServices);
amazonUrl = amazon.Url;
amazonError = amazon.Error;
amazonRemove = amazon.Remove;
console.log("----AWS----\n"+amazonError +"\n"+ amazonUrl +"\n"+ amazonRemove+"\n----AWS----");
document.getElementById("url").value = "Dropbox:" + dropboxUrl +"<br>Drive:" + driveUrl + "<br>Amazon Web Services:" + amazonUrl;
var remove = JSON.parse(JSON.stringify({"Dropbox": dropboxRemove,"Drive": driveRemove,"Amazon": amazonRemove}));
document.getElementById("remove-"+id).value = JSON.stringify({"formid": formid,"qid": qid,"Remove":remove});
$("#url").trigger("change");
}).fail(function(){
console.log("An Error Occured");
});
return true;
}
function save(formdata,filekey,progressId){
return jQuery.ajax({
type: "POST",
url: "file.php",
enctype: "multipart/form-data",
data: formdata,
cache: false,
// async: false,
contentType: false,
processData: false,
xhr: function () {
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener("progress", function (e) {
var percent = 0;
var position = e.loaded || e.position;
var total = e.total;
if (e.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
$("#"+progressId).attr("value", percent);
}, true);
}
return xhr;
},
success: function (sccss) {
}
});
}
function ajaxRequest(url,formdata,async){
var result = null;
jQuery.ajax({
type: "POST",
url: url,
enctype: "multipart/form-data",
data: formdata,
async: async,
cache: false,
contentType: false,
processData: false,
success: function(sccss){
result = sccss;
}
});
return result;
}
function sendJob(qid,file,formid,folder,folderKey,key){
var formdata = new FormData();
formdata.append("key",key);
formdata.append("qid",qid);
formdata.append("file",file);
formdata.append("folder",folder);
formdata.append("formid",formid);
formdata.append("action","upload");
formdata.append("folderKey",folderKey);
var response = ajaxRequest("file.php",formdata,false);
return response;
}
function getAwsKeys(){
var access = JFCustomWidget.getWidgetSetting("awsaccess");
var secret = JFCustomWidget.getWidgetSetting("awssecret");
var bucket = JFCustomWidget.getWidgetSetting("awsbucket");
var region = JFCustomWidget.getWidgetSetting("awsregion");
var key =JSON.stringify({access: access,secret: secret, bucket: bucket,region: region});
return key;
}
function urlReplace(url){
do{
old = url;
url = url.replace("\\","");
url = url.replace("{","");
url = url.replace("}","");
url = url.replace(",","<br>");
url = url.replace(" ","%20"); //Space = %20
}
while(old != url);
return url;
}
function generate_token(length){
var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
var b = [];
for (var i=0; i<length; i++) {
var j = (Math.random() * (a.length-1)).toFixed(0);
b[i] = a[j];
}
return b.join("");
}
function returnSubmit(value,valid){
var result = {};
result.value = value;
result.valid = valid;
JFCustomWidget.sendSubmit(result);
}
/**
* Creating UI elements for selected file
* @param {String} id - Uniq id for elements
* @param {String} file - File name for remove func
* @param {String} fileExtension - File Extension for UI Elements
*/
function createDiv(id,file = null,fileExtension = null){ //Creating div
var uploadItem = document.createElement("div");
var imageSection = document.createElement("div");
var img = document.createElement("img");
var fileType = document.createElement("div");
var progressSection = document.createElement("div");
var progressFile = document.createElement("div");
var progressBar = document.createElement("progress");
var remove = document.createElement("button");
uploadItem.id = "uploadItem-"+id;
uploadItem.classList.add("uploadItem");
imageSection.id = "imageSection-"+id;
imageSection.classList.add("imageSection");
img.setAttribute("alt","img");
img.setAttribute("src","https://i.hizliresim.com/dOzJkZ.png");
fileType.id = "fileType-"+id;
fileType.innerHTML = fileExtension.toUpperCase();
progressSection.id = "progressSection-"+id;
progressSection.classList.add("progressSection");
progressFile.id = "progressFile-"+id;
progressFile.innerHTML = file;
progressBar.id = "progressBar-"+id;
remove.classList.add("remove");
remove.id = "remove-"+id;
remove.innerHTML = '✕';
var measurement = document.createElement("span");
document.getElementById("xup").appendChild(measurement);
measurement.id = "measurement-"+id;
measurement.classList.add("measurement");
document.getElementById(measurement.id).innerHTML = file;
if(document.getElementById(measurement.id).offsetWidth > 200){
progressFile.classList.add("progressFile-Long");
var indentValue = document.getElementById(measurement.id).offsetWidth-190;
var css = '#progressFile-'+id+':hover{ text-indent:-'+indentValue+'px; }';
head = document.head || document.getElementsByTagName('head')[0];
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
measurement.remove();
document.getElementById("xup").appendChild(uploadItem);
document.getElementById(uploadItem.id).appendChild(imageSection);
document.getElementById(imageSection.id).appendChild(img);
document.getElementById(imageSection.id).appendChild(fileType);
document.getElementById(uploadItem.id).appendChild(progressSection);
document.getElementById(progressSection.id).appendChild(progressFile);
document.getElementById(progressSection.id).appendChild(progressBar);
document.getElementById(uploadItem.id).appendChild(remove);
$("#"+remove.id).click(function(e){ //If user click remove (X) button this is removing file from clouds and destroying div
e.preventDefault();
var params = JSON.parse(document.getElementById(remove.id).value);
var path = params.Remove;
path = JSON.stringify(path);
console.log(path);
var formdata = new FormData();
formdata.append("action","deleteFile");
formdata.append("formid", params.formid);
formdata.append("qid", params.qid);
formdata.append("remove", path);
formdata.append("aws",getAwsKeys());
ajaxRequest("file.php",formdata,true);
// var oldHeight = empty(document.getElementById("height").value) ? 0 : document.getElementById("height").value;
// var newHeight = oldHeight - 90;
// if((newHeight < 530) && (newHeight > 100)){
// setFrameSize(newHeight);
// }
var elements = empty(document.getElementById("xup").children) ? 0 : document.getElementById("xup").children.length - 3;
var currentHeight = elements * 90;
console.log(elements + "\n" + currentHeight);
var newHeight = currentHeight-80>530 ? 530 : currentHeight - 90;
setFrameSize(currentHeight);
$("#"+uploadItem.id).remove();
});
return id;
}
function getFileExtension(filename){
return filename.split('.').pop();
}
function setFrameSize(height,width = 500){
var size ={};
size.width = width>500 ? 500 : width;
size.height = height<530 ? height>100 ? height : 100 : 530; //Used if statement because height max value have to be 530
JFCustomWidget.requestFrameResize(size);
}
function submitFunc(){
fieldCheck();
}
/**
If field has no file user can't skip the question (if is required) and submit the form
*/
function fieldCheck(){
debugger;
var children = empty(document.getElementById("xup")) ? 0 : document.getElementById("xup").children;
for(let i = 0; i <= children.length - 1; i++){
if(children[i].id.indexOf("uploadItem") == -1){
if(i == (children.length-1)){
console.log(children[i].id);
document.getElementById("url").value = null;
returnSubmit(null,false);
}
}
}
var folder = document.getElementById("folder").value;
if(!empty(document.getElementById("url").value)){
// removeFiles(form_id,folder);
var folder = document.getElementById("folder").value;
returnSubmit(JSON.stringify(document.getElementById("url").value),true);
}
else{
returnSubmit(null,false);
}
}
function removeFiles(){}
window.onerror = function(err,url,line) {
document.getElementById("add").disabled = true;
document.getElementById("upload").disabled = true;
setFrameSize(300);
var error = document.createElement("div");
error.innerHTML = err; //+ "<br>" + url + "<br>" + line;
document.getElementById("xup").appendChild(error);
head = document.head || document.getElementsByTagName('head')[0];
var css = ".addFile{ visibility: hidden}";
style = document.createElement("style");
style.type = "text/css";
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
};
});//JFCustomWdiget.Subscribe
});//Document.Ready