-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp.checkForBadRequires
executable file
·88 lines (75 loc) · 1.71 KB
/
php.checkForBadRequires
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
set -euo pipefail
usage(){
detail -3 "Usage : $(basename "$0") <php file>"
}
arguments=()
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit
;;
--)
shift
arguments+=("$@")
set --
break;
;;
--*)
echo "Unknown long option $1" >&2
exit 3
;;
-*)
if [ "${#1}" -eq 1 ]; then
echo "Invalid token -" >&2
exit 1
fi
if [ "${#1}" -eq 2 ]; then
echo "unknown option $1" >&2
exit 2
fi
first=${1:1:1}
rest=${1:2}
shift
set -- -"$first" -"$rest" "$@"
continue
;;
*)
arguments+=("$1")
;;
esac
shift
done
set -- "${arguments[@]}"
if [ "$#" -lt 1 ]; then
exitError 1 "Missing filename"
fi
file="$1"
if [ ! -f "$file" ]; then
exitError 2 "Not a file $file";
fi
if [ ! which cleanPhp >&2 2> /dev/null ]; then
exitError 3 "Missing cleanPhp executable"
fi
dirname=$(dirname "$file")
dirname=${dirname%%/}
cd $dirname
basefile="$(basename $file)"
mapfile -t content < <(cleanPhp "$basefile" \
| grep -oE "require_once\s+__DIR__[^;]+" \
| grep -v '\$'
)
bad=("In $PWD/$basefile :")
for instruction in "${content[@]}"
do
trimed=${instruction%%\"}
trimed=${instruction%%\'}
lookFor=$(echo "$trimed" | sed -re "s#^.*__DIR__\s*\.\s*.##")
if [ ! -f "$PWD$lookFor" ]; then
bad+=(" could not resolve $PWD$lookFor from $instruction")
fi
done
if [ "${#bad[@]}" -gt 1 ]; then
printf "%s\n" "${bad[@]}"
fi