Commit 2eb7c6e1 authored by Marc Egger's avatar Marc Egger

Merge branch 'marcRestClientAufDevelop' into 'develop'

Marc rest client auf develop

See merge request !288
parents b6c43d4a 6b4ce59f
Pipeline #3831 passed with stages
in 4 minutes and 24 seconds
......@@ -1411,21 +1411,21 @@ Run a php function defined in an external script.
* All column parameters are passed as an associative array to the function as the first argument.
* The second argument (here called $qfq) is an object which acts as an interface to QFQ functionality.
* The script has access to the following qfq functions using this interface:
* $qfq::apiCall($method, $url, $data = [], $header = [], $timeout = 5)
* string $method can be PUT/POST/GET/DELETE
* $qfq::apiCall($method, $url, $data = '', $header = [], $timeout = 5)
* string $method: can be PUT/POST/GET/DELETE
* string $url
* array $data a json string which will be added as GET parameters or as POST fields respectively.
* array $header is of the form ['Content-type: text/plain', 'Content-length: 100']
* int $timeout is the number of seconds to wait until call is aborted.
* string $data: a json string which will be added as GET parameters or as POST fields respectively.
* array $header: is of the form ['Content-type: text/plain', 'Content-length: 100']
* int $timeout: is the number of seconds to wait until call is aborted.
* $qfq::getVar($key, $useStores = 'FSRVD', $sanitizeClass = '', &$foundInStore = '', $typeMessageViolate = 'c')
* string $key is the name of qfq variable
* string $useStores are the stores in which variable is searched (in order from left to right). see :ref:`store`.
* string $sanitizeClass (see :ref:`sanitize-class`)
* string $foundInStore is filled with the name of the store in which the variable was found.
* string $typeMessageViolate defines what to return if the sanitize class was violated:
* 'c': return '!!<sanitize class>!!'
* '0': return '0'
* 'e': return ''
* string $key: is the name of qfq variable
* string $useStores: are the stores in which variable is searched (in order from left to right). see :ref:`store`.
* string $sanitizeClass: (see :ref:`sanitize-class`)
* string $foundInStore: is filled with the name of the store in which the variable was found.
* string $typeMessageViolate: defines what to return if the sanitize class was violated:
* 'c' : returns '!!<sanitize class>!!'
* '0' : returns '0'
* 'e' : returns ''
* The current working directory is the current web instance (e.g. ``/var/www/html``) .
* All output (e.g. using echo) will be returned by the special column as is.
* If the function returns an associative array, then the key-value pairs will be accessible via the Client store.
......@@ -1436,11 +1436,11 @@ Run a php function defined in an external script.
+-------------------+----------------------------------------------------+------------------------------------------------------------------+
| Token | Example | Comment |
+===================+====================================================+==================================================================+
| scr | scr:fileadmin/scripts/my_script.php | Path to the custom script relative to the current web instance |
| F | F:fileadmin/scripts/my_script.php | Path to the custom script relative to the current web instance |
+-------------------+----------------------------------------------------+------------------------------------------------------------------+
| f | f:my_function | Function name |
| c | c:my_function | PHP function to call |
+-------------------+----------------------------------------------------+------------------------------------------------------------------+
| <whatever> | myParameter:something | All parameters are passed on in an associative array |
| <whatever> | myParameter:something | all parameters are passed on as an array to the function |
+-------------------+----------------------------------------------------+------------------------------------------------------------------+
**Example**
......@@ -1467,13 +1467,13 @@ Run a php function defined in an external script.
* QFQ report ::
5.sql = SELECT "IAmInRecordStore" AS _savedInRecordStore
10.sql = SELECT "scr:fileadmin/scripts/my_script.php|f:my_function|a1:Hello|a2:World" AS _script
10.sql = SELECT "F:fileadmin/scripts/my_script.php|c:my_function|a1:Hello|a2:World" AS _script
20.sql = SELECT "<br><br>Returened value: {{IAmInClientStore:C:alnumx}}"
* Output ::
The first argument contains all attributes including "src" and "f":
Array ( [scr] => fileadmin/scripts/my_script.php [f] => my_function [a1] => Hello [a2] => World )
Array ( [F] => fileadmin/scripts/my_script.php [c] => my_function [a1] => Hello [a2] => World )
get variable from record store:
IAmInRecordStore
......
......@@ -1763,7 +1763,7 @@ const TOKEN_COPY_TO_CLIPBOARD = 'y';
const TOKEN_DROPDOWN = 'z';
const TOKEN_WEBSOCKET = 'w';
const TOKEN_REST_CLIENT = 'n';
const TOKEN_SCRIPT = 'scr';
const TOKEN_SCRIPT = 'F';
const TOKEN_TEXT = 't';
const TOKEN_ALT_TEXT = 'a';
......@@ -1791,7 +1791,7 @@ const TOKEN_FILE = 'F';
const TOKEN_FILE_DEPRECATED = 'f'; // since 5.12.17
const TOKEN_DOWNLOAD_MODE = 'M';
const TOKEN_ATTRIBUTE = 'A';
const TOKEN_FUNCTION = 'f';
const TOKEN_FUNCTION_CALL = 'c';
const TOKEN_THUMBNAIL = 'T';
const TOKEN_THUMBNAIL_DIMENSION = 'W';
......
......@@ -25,7 +25,7 @@ class ColumnScript {
if (empty($param[TOKEN_SCRIPT])) {
throw new \UserReportException("Missing script path.", ERROR_MISSING_VALUE);
}
if (empty($param[TOKEN_FUNCTION])) {
if (empty($param[TOKEN_FUNCTION_CALL])) {
throw new \UserReportException("Missing function name.", ERROR_MISSING_VALUE);
}
......@@ -46,18 +46,18 @@ class ColumnScript {
}
// execute function, write output to buffer
if (!function_exists($param[TOKEN_FUNCTION])) {
if (!function_exists($param[TOKEN_FUNCTION_CALL])) {
throw new \UserReportException("Function doesn't exist.", ERROR_IO_READ_FILE);
}
ob_start();
try {
$return = call_user_func_array($param[TOKEN_FUNCTION], [$param, new ScriptFunctions()]);
$return = call_user_func_array($param[TOKEN_FUNCTION_CALL], [$param, new ScriptFunctions()]);
$output = ob_get_clean();
} catch (\Exception | \Error $e) {
ob_end_clean();
throw new \UserReportException(json_encode([
ERROR_MESSAGE_TO_USER => 'Function execution failed.',
ERROR_MESSAGE_TO_DEVELOPER => "Error message:\n" . $e->getMessage() . "\n\nFunction: " . $param[TOKEN_FUNCTION] . "\n\nParameters:\n" . print_r($param,true)]));
ERROR_MESSAGE_TO_DEVELOPER => "Error message:\n" . $e->getMessage() . "\n\nFunction: " . $param[TOKEN_FUNCTION_CALL] . "\n\nParameters:\n" . print_r($param,true)]));
}
// return buffer output and fill store
......
......@@ -106,15 +106,6 @@ class RestClient {
$header = [];
}
if (strpos($param[TOKEN_L_HEADER], 'content-type:') === false) {
$mime = (($param[TOKEN_L_CONTENT][0] ?? '') == '{') ? 'application/json' : 'text/plain';
$header[] = 'content-type: ' . $mime . '; charset=utf-8';
}
if (strpos($param[TOKEN_L_HEADER], 'connection:') === false) {
$header[] = 'connection: close';
}
// If 'Host' is missing in header: define - useful for Firewall/ Proxy
// CR: if a header 'host' is given, REST calls fails always.
// if (strpos($param[TOKEN_L_HEADER], 'host:') === false) {
......@@ -128,6 +119,17 @@ class RestClient {
public static function callApiCurl(string $method, string $url, string $data = '', array $header = [], int $timeout = 5) {
// Header: Set content-type if not set
if (strpos(join('', $header), 'content-type:') === false) {
$mime = (($data[0] ?? '') == '{') ? 'application/json' : 'text/plain';
$header[] = 'content-type: ' . $mime . '; charset=utf-8';
}
// Header: Set connection if not set
if (strpos(join('', $header), 'connection:') === false) {
$header[] = 'connection: close';
}
$ch = curl_init();
$curlConfig = array(
CURLOPT_RETURNTRANSFER => true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment