From: Moritz Bunkus Date: Thu, 28 Oct 2021 08:40:21 +0000 (+0200) Subject: kivi.js: Helfsfunktion zum Formatieren eines JS-Calls als CURL-Aufruf X-Git-Tag: kivitendo-mebil_0.1-0~10^2~2^2~304 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=c51050b4e3a115d0aa2bb070ab8537990f4b522f;hp=4af639717afee7911c70653b6166d10a7e1f2bae;p=kivitendo-erp.git kivi.js: Helfsfunktion zum Formatieren eines JS-Calls als CURL-Aufruf Nützlich, wenn man mehrfach von der Kommandozeile aus eine Controller-Funktion aufrufen will, ohne jedes Mal zur Maus greifen zu müssen. Aufruf grob so: var data = $('#form').serializeArray(); data.push({ name: 'action', value: 'Order/frobnicate' }); console.log(kivi.call_as_curl({ data: data })); --- diff --git a/js/kivi.js b/js/kivi.js index c6245c855..7fb250149 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -676,6 +676,29 @@ namespace("kivi", function(ns) { $input.prop('selectionStart', position); $input.prop('selectionEnd', position); }; + + ns._shell_escape = function(str) { + if (str.match(/^[a-zA-Z0-9.,_=+/-]+$/)) + return str; + + return "'" + str.replace(/'/, "'\\''") + "'"; + }; + + ns.call_as_curl = function(params) { + params = params || {}; + var uri = document.documentURI.replace(/\?.*/, ''); + var command = ['curl', '--user', kivi.myconfig.login + ':SECRET', '--request', params.method || 'POST'] + + $(params.data || []).each(function(idx, elt) { + command = command.concat([ '--form-string', elt.name + '=' + (elt.value || '') ]); + }); + + command.push(uri); + + return $.map(command, function(elt, idx) { + return kivi._shell_escape(elt); + }).join(' '); + }; }); kivi = namespace('kivi');