From 604a4d7886427e31f5b0f15b7451cbb8eedcaa7c Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Tue, 26 Nov 2024 00:01:50 +0900 Subject: [PATCH] Add the dynamic loading feature in `relay_invoke_method` method --- assets/php/index.php | 27 +++++++++++++++++++++++++-- console.html | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/assets/php/index.php b/assets/php/index.php index 50ca2c4..5783df0 100644 --- a/assets/php/index.php +++ b/assets/php/index.php @@ -28,6 +28,10 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], "php-httpproxy/") !== 0 && strpos($_SERV exit('It works!

It works!

Download the client

' . DEFAULT_USER_AGENT . '

'); } +function jsonrpc2_cast_to_array($data) { + return is_array($data) ? $data : array($data); +} + function jsonrpc2_encode($method, $params, $id = '') { $data = array( "jsonrpc" => "2.0", @@ -431,7 +435,7 @@ function relay_fetch_url($params) { // check it is POST request if ($data == "POST") { - curl_setopt($ch, CURLOPT_POSTFIELDS, array($data)); + curl_setopt($ch, CURLOPT_POSTFIELDS, jsonrpc2_cast_to_array($data)); curl_setopt($ch, CURLOPT_POST, true); } @@ -492,7 +496,26 @@ function relay_get_geolocation() { function relay_invoke_method($params) { $callback = $params['callback']; - $args = (is_array($params['args']) ? $params['args'] : array()); + $requires = jsonrpc2_cast_to_array($params['requires']); + $args = jsonrpc2_cast_to_array($params['args']); + + foreach($requires as $required_url) { + try { + $result = relay_fetch_url($required_url); + if ($result['success']) { + $fh = tmpfile(); + if ($fh !== false) { + fwrite($fh, $result['data']); + $path = stream_get_meta_data($fh)['uri']; + @require($path); + fclose($fh); + @unlink($path); + } + } + } catch (Exception $e) { + // ignore an exception + } + } try { $data = call_user_func_array($callback, $args); diff --git a/console.html b/console.html index 047340a..783daf5 100644 --- a/console.html +++ b/console.html @@ -251,21 +251,36 @@ var k = (args.length > 0 ? args[0] : ''); var v = (args.length > 1 ? args.slice(1) : []).join(' '); + // "env" is the reserved word if (k == "env") { this.echo("env is the reserved word"); return; } + // check a variable is it Array + if (k in env && env[k] instanceof Array) { + env[k].push(v); + return; + } + // method(relay_web_search) if (env.method == "relay_web_search" && k == "page") { env[k] = parseInt(v); return; } + env[k] = v || null; if (k == "method") { this.set_prompt('method([[b;red;black]' + env.method + '])> '); + + // method(relay_invoke_method) + if (env.method == "relay_invoke_method") { + set_default_env({ + "requires": [] + }); + } // method(relay_sendmail) if (env.method == "relay_sendmail") { @@ -323,6 +338,7 @@ jsonrpc2_request(this, env.method, { "callback": args[0], + "requires": env.requires, "args": args.slice(1) }); return;