2018-01-24 10:59:30 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
|
|
|
const log = require('../../services/log');
|
2018-01-24 11:53:27 +08:00
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const protected_session = require('../../services/protected_session');
|
2018-01-24 10:59:30 +08:00
|
|
|
|
|
|
|
router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
log.info('Executing script: ' + req.body.script);
|
|
|
|
|
|
|
|
const ret = await eval("(" + req.body.script + ")()");
|
|
|
|
|
|
|
|
log.info('Execution result: ' + ret);
|
|
|
|
|
|
|
|
res.send(ret);
|
|
|
|
}));
|
|
|
|
|
2018-01-24 11:53:27 +08:00
|
|
|
router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
res.send(await getSubTreeScripts(noteId, [noteId], req));
|
2018-01-24 11:53:27 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
|
2018-01-25 11:13:41 +08:00
|
|
|
const children = await sql.getAll(`SELECT notes.note_id, notes.note_title, notes.note_text, notes.is_protected
|
2018-01-24 11:53:27 +08:00
|
|
|
FROM notes JOIN notes_tree USING(note_id)
|
|
|
|
WHERE notes_tree.is_deleted = 0 AND notes.is_deleted = 0
|
|
|
|
AND notes_tree.parent_note_id = ? AND notes.type = 'code'
|
2018-01-24 12:41:22 +08:00
|
|
|
AND (notes.mime = 'application/javascript' OR notes.mime = 'text/html')`, [parentId]);
|
2018-01-24 11:53:27 +08:00
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNotes(dataKey, children);
|
|
|
|
|
2018-01-24 11:53:27 +08:00
|
|
|
let script = "\r\n";
|
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
for (const child of children) {
|
2018-01-24 11:53:27 +08:00
|
|
|
if (includedNoteIds.includes(child.note_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
includedNoteIds.push(child.note_id);
|
|
|
|
|
|
|
|
script += await getSubTreeScripts(child.note_id, includedNoteIds, dataKey);
|
|
|
|
|
|
|
|
script += child.note_text + "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2018-01-24 10:59:30 +08:00
|
|
|
module.exports = router;
|