Update plugin capabilities and prepare OnlyOffice API

This commit is contained in:
yflory 2024-04-30 18:13:52 +02:00
parent 2557b506ab
commit 131d3e8b4d
5 changed files with 97 additions and 17 deletions

View File

@ -78,15 +78,26 @@ COMMANDS.TOTP_REVOKE = TOTP.TOTP_REVOKE;
COMMANDS.TOTP_WRITE_BLOCK = TOTP.TOTP_WRITE_BLOCK; // Password change only for now (v5.5.0)
COMMANDS.TOTP_REMOVE_BLOCK = TOTP.TOTP_REMOVE_BLOCK;
try {
// SSO plugin may not be installed
const SSO = plugins.SSO && plugins.SSO.challenge;
COMMANDS.SSO_AUTH = SSO.SSO_AUTH;
COMMANDS.SSO_AUTH_CB = SSO.SSO_AUTH_CB;
COMMANDS.SSO_WRITE_BLOCK = SSO.SSO_WRITE_BLOCK; // Account creation only
COMMANDS.SSO_UPDATE_BLOCK = SSO.SSO_UPDATE_BLOCK; // Password change
COMMANDS.SSO_VALIDATE = SSO.SSO_VALIDATE;
} catch (e) {}
// Load challenges added by plugins
Object.keys(plugins || {}).forEach(id => {
try {
let plugin = plugins[id];
if (!plugin.challenge) { return; }
let commands = plugin.challenge;
Object.keys(commands).forEach(cmd => {
if (COMMANDS[cmd]) { return; } // Don't overwrite
COMMANDS[cmd] = commands[cmd];
});
} catch (e) {}
});
/*
const SSO = plugins.SSO && plugins.SSO.challenge;
COMMANDS.SSO_AUTH = SSO.SSO_AUTH;
COMMANDS.SSO_AUTH_CB = SSO.SSO_AUTH_CB;
COMMANDS.SSO_WRITE_BLOCK = SSO.SSO_WRITE_BLOCK; // Account creation only
COMMANDS.SSO_UPDATE_BLOCK = SSO.SSO_UPDATE_BLOCK; // Password change
COMMANDS.SSO_VALIDATE = SSO.SSO_VALIDATE;
*/
var randomToken = () => Nacl.util.encodeBase64(Nacl.randomBytes(24)).replace(/\//g, '-');

View File

@ -513,6 +513,11 @@ app.use("/block", (req, res, next) => {
next();
});
Object.keys(plugins || {}).forEach(name => {
let plugin = plugins[name];
if (!plugin.addHttpEndpoints) { return; }
plugin.addHttpEndpoints(Env, app);
});
app.use("/customize", Express.static('customize'));
app.use("/customize", Express.static('customize.dist'));

View File

@ -14,6 +14,13 @@
var getTxid = function () {
return Math.random().toString(16).replace('0.', '');
};
var getInstanceURL = () => {
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length - 1; i >= 0; i--) {
var match = scripts[i].src.match(/(.*)web-apps\/apps\/api\/documents\/api.js/i);
if (match) { return match[1]; }
}
};
var makeChan = function (iframe, iOrigin) {
var handlers = {};
@ -83,7 +90,8 @@
var getBlob = function (cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', config.document.url, true);
let url = config.document.url;
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (this.status === 200) {
@ -131,9 +139,18 @@
var getSession = function (cb) {
chan.send('GET_SESSION', {
key: key
key: key,
keepOld: !config.events.onNewKey
}, function (obj) {
if (obj && obj.error) { reject(obj.error); return console.error(obj.error); }
// OnlyOffice
if (!config.events.onNewKey) {
key = obj.key;
console.error(key, obj);
return void cb();
}
if (obj.key !== key) {
// The outside app may reject our new key if the "old" one is deprecated.
// This will happen if multiple users try to update the key at the same
@ -159,15 +176,21 @@
});
chan.on('RELOAD', function () {
config.document.blob = blob;
document.getElementById('cryptpad-editor').remove();
if (!config.editorConfig) { // Not OnlyOffice shim
document.getElementById('cryptpad-editor').remove();
}
makeIframe(config);
});
chan.on('HAS_UNSAVED_CHANGES', function(unsavedChanges, cb) {
config.events.onHasUnsavedChanges(unsavedChanges);
if (config.events.onHasUnsavedChanges) {
config.events.onHasUnsavedChanges(unsavedChanges);
}
cb();
});
chan.on('ON_INSERT_IMAGE', function(data, cb) {
config.events.onInsertImage(data, cb);
if (config.events.onIntertImage) {
config.events.onInsertImage(data, cb);
} else { cb(); }
});
});
@ -192,6 +215,26 @@
* @return {promise}
*/
var init = function (cryptpadURL, containerId, config) {
// OnlyOffice shim: don't provide a URL
if (!config && typeof(containerId) === "object") {
config = containerId;
containerId = cryptpadURL;
cryptpadURL = getInstanceURL();
}
// OnlyOffice shim
let url = config.document.url;
if (/^http:\/\/localhost\/cache\/files\//.test(url)) {
url = url.replace(/(http:\/\/localhost\/cache\/files\/)/, getInstanceURL() + 'ooapi/');
}
config.document.url = url;
if (config.documentType === "spreadsheet") {
config.documentType = "sheet";
}
if (config.documentType === "text") {
config.documentType = "doc";
}
return new Promise(function (resolve, reject) {
setTimeout(function () {
@ -209,8 +252,8 @@
if (!config) { return reject('Missing args: no data provided'); }
if(['document.url', 'document.fileType', 'documentType',
'events.onSave', 'events.onHasUnsavedChanges',
'events.onNewKey', 'events.onInsertImage'].some(function (k) {
/*'events.onSave', 'events.onHasUnsavedChanges',
'events.onNewKey', 'events.onInsertImage'*/].some(function (k) {
var s = k.split('.');
var c = config;
return s.some(function (key) {
@ -235,8 +278,17 @@
makeIframe = function (config) {
var iframe = document.createElement('iframe');
iframe.setAttribute('id', 'cryptpad-editor');
iframe.setAttribute('name', 'frameEditor');
iframe.setAttribute('align', 'top');
iframe.setAttribute("src", url);
container.appendChild(iframe);
iframe.setAttribute("width", config.width);
iframe.setAttribute("height", config.height);
if (config.editorConfig) { // OnlyOffice
container.replaceWith(iframe);
container = iframe;
} else {
container.appendChild(iframe);
}
var onMsg = function (msg) {
var data = typeof(msg.data) === "string" ? JSON.parse(msg.data) : msg.data;
@ -252,6 +304,10 @@
});
};
init.version = () => { return '7.3.0'; };
init.DocEditor = init; // OnlyOffice shim
window.DocsAPI = init;
return init;
};

View File

@ -96,6 +96,13 @@ define([
http.send();
};
chan.on('GET_SESSION', function (data, cb) {
if (data.keepOld) {
var key = data.key + "000000000000000000000000000000000";
console.warn('KEY', key);
return void cb({
key: `/2/integration/edit/${key.slice(0,24)}/`
});
}
var getHash = function () {
//isNew = true;
return Hash.createRandomHash('integration');

View File

@ -0,0 +1 @@
../../../../cryptpad-api.js