Skip to content

Commit e0c7bb9

Browse files
authored
Merge pull request #13 from serkanalgur:serkanalgur/issue12
FileGetContent function
2 parents 07e4993 + 0552c16 commit e0c7bb9

4 files changed

Lines changed: 145 additions & 26 deletions

File tree

array_related_functions.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,24 @@ func IsArray(v interface{}) bool {
139139
}
140140
return false
141141
}
142+
143+
// InArray - Similar function of in_array in PHP
144+
// Original : https://www.php.net/manual/en/function.in-array.php
145+
// func inArray
146+
// needle : string, int
147+
// haystack : should be an array
148+
// strict : set true for type check
149+
// return boolean true / false
150+
func InArray(needle interface{}, haystack interface{}) bool {
151+
switch reflect.TypeOf(haystack).Kind() {
152+
default:
153+
s := reflect.ValueOf(haystack)
154+
for i := 0; i < s.Len(); i++ {
155+
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
156+
return true
157+
}
158+
}
159+
}
160+
return false
161+
162+
}

directory_filesystem.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"fmt"
66
"io"
77
"io/ioutil"
8+
"log"
89
"net/http"
910
"os"
1011
"path/filepath"
12+
"strings"
1113
"syscall"
1214
"time"
1315
)
@@ -201,6 +203,77 @@ func FileType(fs string) (string, error) {
201203
return contentType, nil
202204
}
203205

206+
207+
// FileGetContents - Reads entire file into a string.
208+
// Original : https://www.php.net/manual/en/function.file-get-contents.php
209+
// 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.
210+
// TODO : Context Implementation.
211+
func FileGetContents(path string, includePath bool, context []string, offset int, maxlen int) string {
212+
var v string
213+
if IsURL(path) {
214+
includePath = false
215+
}
216+
217+
if includePath == true {
218+
fileHere := FileExists(path)
219+
if fileHere {
220+
file,_ := FOpen(path,os.O_RDONLY)
221+
222+
if offset >= 0 && maxlen != 0 {
223+
var err error
224+
r := bufio.NewReader(file)
225+
if offset > 0 {
226+
_, err := r.Discard(offset)
227+
if err != nil {
228+
log.Fatalln(err)
229+
}
230+
}
231+
buf := new(strings.Builder)
232+
_, err = io.CopyN(buf, r, int64(maxlen-offset))
233+
if err != nil {
234+
log.Fatal(err)
235+
}
236+
v = buf.String()
237+
238+
} else {
239+
v = FRead(file,512)
240+
}
241+
FClose(file)
242+
}
243+
} else {
244+
u, err := http.Get(path)
245+
if err != nil {
246+
log.Fatalln(err)
247+
}
248+
249+
defer u.Body.Close()
250+
if offset >= 0 && maxlen > 0 {
251+
var err error
252+
r := bufio.NewReader(u.Body)
253+
if offset > 0 {
254+
_, err := r.Discard(offset)
255+
if err != nil {
256+
log.Fatalln(err)
257+
}
258+
}
259+
buf := new(strings.Builder)
260+
_, err = io.CopyN(buf, r, int64(maxlen-offset))
261+
if err != nil {
262+
log.Fatal(err)
263+
}
264+
v = buf.String()
265+
} else {
266+
body, err := ioutil.ReadAll(u.Body)
267+
if err != nil {
268+
log.Fatalln(err)
269+
}
270+
v = string(body)
271+
}
272+
}
273+
274+
return v
275+
}
276+
204277
// Glob - Find pathnames matching a pattern.
205278
// Original : https://www.php.net/manual/en/function.glob.php
206279
// 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.

phpfuncs.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

utils.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package phpfuncs
2+
3+
import (
4+
"net/url"
5+
"regexp"
6+
"strings"
7+
"unicode/utf8"
8+
)
9+
10+
// 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!
11+
12+
// Basic regular expressions for validating strings
13+
const (
14+
maxURLRuneCount = 2083
15+
minURLRuneCount = 3
16+
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]))`
17+
URLSchema string = `((ftp|tcp|udp|wss?|https?):\/\/)`
18+
URLUsername string = `(\S+(:\S*)?@)`
19+
URLPath string = `((\/|\?|#)[^\s]*)`
20+
URLPort string = `(:(\d{1,5}))`
21+
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]))`
22+
URLSubdomain string = `((www\.)|([a-zA-Z0-9]+([-_\.]?[a-zA-Z0-9])*[a-zA-Z0-9]\.[a-zA-Z0-9]+))`
23+
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 + `?$`
24+
SSN string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
25+
)
26+
27+
var rxURL = regexp.MustCompile(URL)
28+
29+
// IsURL function
30+
func IsURL(str string) bool {
31+
if str == "" || utf8.RuneCountInString(str) >= maxURLRuneCount || len(str) <= minURLRuneCount || strings.HasPrefix(str, ".") {
32+
return false
33+
}
34+
strTemp := str
35+
if strings.Contains(str, ":") && !strings.Contains(str, "://") {
36+
// support no indicated urlscheme but with colon for port number
37+
// http:// is appended so url.Parse will succeed, strTemp used so it does not impact rxURL.MatchString
38+
strTemp = "http://" + str
39+
}
40+
u, err := url.Parse(strTemp)
41+
if err != nil {
42+
return false
43+
}
44+
if strings.HasPrefix(u.Host, ".") {
45+
return false
46+
}
47+
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
48+
return false
49+
}
50+
return rxURL.MatchString(str)
51+
}

0 commit comments

Comments
 (0)