Skip to content

Commit c90e28b

Browse files
authored
Merge pull request #17 from serkanalgur/development
Working
2 parents 4fbfd9c + 90ed378 commit c90e28b

5 files changed

Lines changed: 74 additions & 19 deletions

File tree

exec.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,50 @@ package phpfuncs
22

33
import (
44
"bytes"
5-
"fmt"
65
"log"
76
"os"
87
"os/exec"
8+
"regexp"
9+
"strings"
910
)
1011

12+
var rPatt *regexp.Regexp
13+
14+
15+
func init() {
16+
rPatt = regexp.MustCompile(`[^\w@%+=:,./-]`)
17+
}
18+
1119
// Exec - Start a command on system
1220
//
1321
// Original : https://www.php.net/manual/tr/function.exec.php
1422
//
1523
// exec() executes the given command.
16-
func Exec(of string) {
24+
func Exec(of string) string {
1725
var out bytes.Buffer
1826
cmd := exec.Command(of)
1927
cmd.Stdout = &out
2028
err := cmd.Run()
2129
if err != nil {
2230
log.Fatal(err)
2331
}
32+
return out.String()
2433
}
2534

2635
// ShellExec - Execute command via shell and return the complete output as a string
2736
//
2837
// Original : https://www.php.net/manual/en/function.shell-exec.php
2938
//
3039
// This function is identical to the backtick operator.
31-
func ShellExec(of string) {
40+
func ShellExec(of string) string {
3241
var out bytes.Buffer
3342
cmd := exec.Command(of)
3443
cmd.Stdout = &out
3544
err := cmd.Run()
3645
if err != nil {
3746
log.Fatal(err)
3847
}
39-
fmt.Printf("%q\n", out.String())
48+
return out.String()
4049
}
4150

4251
// Exit - Output a message and terminate the current script
@@ -56,3 +65,33 @@ func Exit(of int) {
5665
func Die(of int) {
5766
os.Exit(of)
5867
}
68+
69+
// Escapeshellarg - Escape a string to be used as a shell argument
70+
//
71+
// Original: https://www.php.net/manual/en/function.escapeshellarg.php
72+
//
73+
func Escapeshellarg(s string) string {
74+
if len(s) == 0 {return "''"}
75+
76+
if rPatt.MatchString(s) {
77+
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
78+
}
79+
80+
return s
81+
}
82+
83+
// Escapeshellcmd - Escape shell metacharacters
84+
//
85+
// Original: https://www.php.net/manual/en/function.escapeshellcmd.php
86+
//
87+
func Escapeshellcmd(s string) string {
88+
cmds := Explode(s, " ") // I know thats maybe not proper way...
89+
90+
z := make([]string, len(cmds))
91+
92+
for i ,s := range cmds {
93+
z[i] = Escapeshellarg(s)
94+
}
95+
96+
return Join(" ",z)
97+
}

tests/Encryption/Base64Decode/Base64Decode_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import (
66
"github.com/serkanalgur/phpfuncs"
77
)
88

9+
func CoverCheck(t *testing.T, s, exp string) {
10+
if s != exp {
11+
t.Errorf("%q (expected: %q)", s, exp)
12+
}
13+
}
14+
915
// TestBase64Decode - Test for Encryption
1016
func TestBase64Decode(t *testing.T) {
1117
want := "Hello World"
12-
if got, _ := phpfuncs.Base64Decode("SGVsbG8gV29ybGQ="); got != want {
13-
t.Errorf("Base64Decode() = %v, want %v", got, want)
14-
}
18+
g, _ := phpfuncs.Base64Decode("SGVsbG8gV29ybGQ=")
19+
CoverCheck(t, g, want)
1520
}

tests/Encryption/Base64Encode/Base64Encode_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import (
66
"github.com/serkanalgur/phpfuncs"
77
)
88

9+
func CoverCheck(t *testing.T, s, exp string) {
10+
if s != exp {
11+
t.Errorf("%q (expected: %q)", s, exp)
12+
}
13+
}
14+
915
// TestBase64Encode - Test for Encryption
1016
func TestBase64Encode(t *testing.T) {
1117
want := "SGVsbG8gV29ybGQ="
12-
if got := phpfuncs.Base64Encode("Hello World"); got != want {
13-
t.Errorf("Base64Encode() = %v, want %v", got, want)
14-
}
18+
CoverCheck(t,phpfuncs.Base64Encode("Hello World"),want)
1519
}

tests/Encryption/MD5/MD5_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import (
66
"github.com/serkanalgur/phpfuncs"
77
)
88

9+
func CoverCheck(t *testing.T, s, exp string) {
10+
if s != exp {
11+
t.Errorf("%q (expected: %q)", s, exp)
12+
}
13+
}
14+
15+
916
// TestMD5 - Test for Encryption
1017
func TestMD5(t *testing.T) {
1118
want := "b10a8db164e0754105b7a99be72e3fe5"
12-
if got := phpfuncs.MD5("Hello World"); got != want {
13-
t.Errorf("MD5() = %v, want %v", got, want)
14-
}
19+
CoverCheck(t,phpfuncs.MD5("Hello World"),want)
1520
}

tests/arrays/InArray_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ const TestWords = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
1111

1212
const needle = "est"
1313

14+
func CoverCheck(t *testing.T, s, exp bool) {
15+
if s != exp {
16+
t.Errorf("%v (expected: %v)", s, exp)
17+
}
18+
}
19+
1420
func TestInArrayString(t *testing.T) {
1521
want := true
1622
stack := strings.Split(TestWords, " ")
17-
if got := phpfuncs.InArray(needle, stack); got != want {
18-
t.Errorf("inArray() = %v, want %v", got, want)
19-
}
23+
CoverCheck(t,phpfuncs.InArray(needle, stack),want)
2024
}
2125

2226
func TestInArrrayInt(t *testing.T) {
2327
want := true
2428
var needle = 2
2529
stack := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
26-
if got := phpfuncs.InArray(needle, stack); got != want {
27-
t.Errorf("inArray() = %v, want %v", got, want)
28-
}
30+
CoverCheck(t,phpfuncs.InArray(needle, stack),want)
2931
}

0 commit comments

Comments
 (0)