From 888db2bc72a9951b53051ab32ac25a6eb7341ca8 Mon Sep 17 00:00:00 2001 From: myvesta <38690722+myvesta@users.noreply.github.com> Date: Sat, 15 Apr 2023 15:50:27 +0200 Subject: [PATCH] myvesta php replacement for gnu 'grep' (but without regular expression) --- bin/v-php-func | 2 +- func/bash-to-php-interpreter.php | 27 ++++++++++- func/main.php | 83 ++++++++++++++++++++++++++++++-- func/string.php | 34 +++++++++++++ 4 files changed, 140 insertions(+), 6 deletions(-) diff --git a/bin/v-php-func b/bin/v-php-func index f7a73385..0c789863 100644 --- a/bin/v-php-func +++ b/bin/v-php-func @@ -2,7 +2,7 @@ # info: calling myVesta PHP functions # options: FUNCTION # -# The function is calling myVesta PHP functions. +# The function is calling myVesta or standard PHP functions directly from bash #----------------------------------------------------------# # Action # diff --git a/func/bash-to-php-interpreter.php b/func/bash-to-php-interpreter.php index 461c1af0..baf9475e 100644 --- a/func/bash-to-php-interpreter.php +++ b/func/bash-to-php-interpreter.php @@ -11,7 +11,7 @@ stream_set_blocking(STDIN, false); $myvesta_stdin=''; $myvesta_f = fopen( 'php://stdin', 'r' ); while( $myvesta_line = fgets( $myvesta_f ) ) { - $myvesta_stdin .= $myvesta_line; + $myvesta_stdin .= $myvesta_line; } fclose( $myvesta_f ); @@ -27,13 +27,36 @@ if (!function_exists($func)) { if (!function_exists($func)) myvesta_throw_error(2, 'Function does not exists'); } +$insert_stdin_at_position=false; +if ($func=="myvesta_grep") $insert_stdin_at_position=1; + $params=array(); -if ($myvesta_stdin!='') $params[]=$myvesta_stdin; +$added=0; +$stdin_content=''; + $myvesta_stdin_return_not_found=false; +if ($myvesta_stdin!='' && $insert_stdin_at_position===false) {$params[]=$myvesta_stdin; $added++;} + for ($i=2; $i<$counter; $i++) { $argv[$i]=myvesta_fix_backslashes($argv[$i]); + if ($insert_stdin_at_position!==false && $myvesta_stdin=='') if ($insert_stdin_at_position==$added) {$stdin_content=$argv[$i]; $added++; continue;} $params[]=$argv[$i]; + $added++; } +if ($insert_stdin_at_position!=false) { + if ($myvesta_stdin=='') { + $file_or_stdin=$stdin_content; + if (!file_exists($file_or_stdin)) { + $myvesta_stdin_return_not_found=true; + $myvesta_stdin=''; + } else { + $myvesta_stdin=file_get_contents($file_or_stdin); + } + } + if (isset($params[$insert_stdin_at_position])) array_splice($params, $insert_stdin_at_position, 0, array($myvesta_stdin)); + else $params[$insert_stdin_at_position]=$myvesta_stdin; +} +//print_r($params); exit; $r=call_user_func_array($func, $params); if (is_bool($r)) { diff --git a/func/main.php b/func/main.php index 03a501a1..650f52ef 100644 --- a/func/main.php +++ b/func/main.php @@ -14,9 +14,88 @@ function myvesta_echo($str) { echo $str; } -function myvesta_exit($code) { +function myvesta_exit($code, $echo='') { global $SHLVL, $myvesta_echo_done, $myvesta_last_echo; // myvesta_echo ("==================== ".$argv[0].": ".$code." ====================\n"); + if ($echo!='') myvesta_echo($echo); + if ($SHLVL<3 && $myvesta_echo_done==true) { + $last_char=substr($myvesta_last_echo, -1, 1); + if ($last_char!="\n") echo "\n"; + } + exit($code); +} + +$myvesta_current_user=exec('whoami', $myvesta_output, $myvesta_return_var); +if ($myvesta_current_user != 'root') {myvesta_echo ("ERROR: You must be root to execute this script"); myvesta_exit(1);} + +function myvesta_throw_error($code, $message) { + global $myvesta_exit_on_error; + if ($message!=='') myvesta_echo ("ERROR: ".$message); + if ($myvesta_exit_on_error) myvesta_exit($code); + return $code; +} + +function myvesta_fix_backslashes($s) { + $s=str_replace("\\n", "\n", $s); + $s=str_replace("\\r", "\r", $s); + $s=str_replace("\\t", "\t", $s); + return $s; +} + +function myvesta_check_args ($requried_arguments, $arguments) { + global $argv; + $argument_counter=count($argv); + $argument_counter--; + $argv[0]=str_replace('/usr/local/vesta/bin/', '', $argv[0]); + // myvesta_echo ( "-------------------- ".$argv[0]." --------------------\n"); + if ($argument_counter<$requried_arguments) { + $arguments=str_replace(" ", "' '", $arguments); + $arguments="'".$arguments."'"; + return myvesta_throw_error(MYVESTA_ERROR_MISSING_ARGUMENTS, "Usage: $command $arguments"); + } + $argument_arr=explode(" ", $arguments); + $i=1; + foreach ($argument_arr as $argument) { + $GLOBALS[$argument]=myvesta_fix_backslashes($argv[$i]); + $i++; + } +} + +function myvesta_fix_args() { + global $argv; + $i=0; + foreach ($argv as $argument) { + if ($i==0) {$i++; continue;} + $argv[$i]=myvesta_fix_backslashes($argv[$i]); + $i++; + } +} + +function myvesta_test_func () { + $args=func_get_args(); + myvesta_echo ("You said: "); + myvesta_echo (trim(print_r ($args, true))); +} +