Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions array_related_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,24 @@ func IsArray(v interface{}) bool {
}
return false
}

// InArray - Similar function of in_array in PHP
// Original : https://www.php.net/manual/en/function.in-array.php
// func inArray
// needle : string, int
// haystack : should be an array
// strict : set true for type check
// return boolean true / false
func InArray(needle interface{}, haystack interface{}) bool {
switch reflect.TypeOf(haystack).Kind() {
default:
s := reflect.ValueOf(haystack)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
return true
}
}
}
return false

}
73 changes: 73 additions & 0 deletions directory_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"syscall"
"time"
)
Expand Down Expand Up @@ -201,6 +203,77 @@ func FileType(fs string) (string, error) {
return contentType, nil
}


// FileGetContents - Reads entire file into a string.
// Original : https://www.php.net/manual/en/function.file-get-contents.php
// This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.
// TODO : Context Implementation.
func FileGetContents(path string, includePath bool, context []string, offset int, maxlen int) string {
var v string
if IsURL(path) {
includePath = false
}

if includePath == true {
fileHere := FileExists(path)
if fileHere {
file,_ := FOpen(path,os.O_RDONLY)

if offset >= 0 && maxlen != 0 {
var err error
r := bufio.NewReader(file)
if offset > 0 {
_, err := r.Discard(offset)
if err != nil {
log.Fatalln(err)
}
}
buf := new(strings.Builder)
_, err = io.CopyN(buf, r, int64(maxlen-offset))
if err != nil {
log.Fatal(err)
}
v = buf.String()

} else {
v = FRead(file,512)
}
FClose(file)
}
} else {
u, err := http.Get(path)
if err != nil {
log.Fatalln(err)
}

defer u.Body.Close()
if offset >= 0 && maxlen > 0 {
var err error
r := bufio.NewReader(u.Body)
if offset > 0 {
_, err := r.Discard(offset)
if err != nil {
log.Fatalln(err)
}
}
buf := new(strings.Builder)
_, err = io.CopyN(buf, r, int64(maxlen-offset))
if err != nil {
log.Fatal(err)
}
v = buf.String()
} else {
body, err := ioutil.ReadAll(u.Body)
if err != nil {
log.Fatalln(err)
}
v = string(body)
}
}

return v
}

// Glob - Find pathnames matching a pattern.
// Original : https://www.php.net/manual/en/function.glob.php
// The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.
Expand Down
26 changes: 0 additions & 26 deletions phpfuncs.go

This file was deleted.

51 changes: 51 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package phpfuncs

import (
"net/url"
"regexp"
"strings"
"unicode/utf8"
)

// Since only this function is needed, this control function is taken from https://github.com/asaskevich/govalidator since it is an MIT license. Thanks for MIT license Alex!

// Basic regular expressions for validating strings
const (
maxURLRuneCount = 2083
minURLRuneCount = 3
IP string = `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
URLSchema string = `((ftp|tcp|udp|wss?|https?):\/\/)`
URLUsername string = `(\S+(:\S*)?@)`
URLPath string = `((\/|\?|#)[^\s]*)`
URLPort string = `(:(\d{1,5}))`
URLIP string = `([1-9]\d?|1\d\d|2[01]\d|22[0-3]|24\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-5]))`
URLSubdomain string = `((www\.)|([a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])*[a-zA-Z0-9]\.[a-zA-Z0-9]+))`
URL = `^` + URLSchema + `?` + URLUsername + `?` + `((` + URLIP + `|(\[` + IP + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-_]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + URLSubdomain + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))\.?` + URLPort + `?` + URLPath + `?$`
SSN string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
)

var rxURL = regexp.MustCompile(URL)

// IsURL function
func IsURL(str string) bool {
if str == "" || utf8.RuneCountInString(str) >= maxURLRuneCount || len(str) <= minURLRuneCount || strings.HasPrefix(str, ".") {
return false
}
strTemp := str
if strings.Contains(str, ":") && !strings.Contains(str, "://") {
// support no indicated urlscheme but with colon for port number
// http:// is appended so url.Parse will succeed, strTemp used so it does not impact rxURL.MatchString
strTemp = "http://" + str
}
u, err := url.Parse(strTemp)
if err != nil {
return false
}
if strings.HasPrefix(u.Host, ".") {
return false
}
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
return false
}
return rxURL.MatchString(str)
}