forked from chyrp/chyrp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggers.rb
executable file
·150 lines (127 loc) · 4.48 KB
/
triggers.rb
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require "find"
require "optparse"
OPTIONS = {
:exclude => [".git", "modules", "lib", "feathers", "themes", "config.yaml.php"]
}
ARGV.options do |o|
script_name = File.basename($0)
o.banner = "Usage: #{script_name} [directory] [OPTIONS]"
o.define_head "Scans [directory] recursively for Trigger calls and filters."
o.separator ""
o.on("--exclude=[val1,val1]", Array,
"A list of directories to exclude from the scan.") { |OPTIONS[:exclude]| }
o.separator ""
o.on_tail("-h", "--help", "Show this help message.") do
puts o
exit
end
o.parse!
end
class Triggers
def initialize(start)
@start, @files, @triggers = start, [], { :calls => {}, :filters => {} }
prepare_files
do_scan
output
end
def prepare_files
Find.find(@start) do |path|
cleaned = path.sub("./", "")
if FileTest.directory?(path)
if OPTIONS[:exclude].include?(cleaned)
Find.prune
else
next
end
else
next unless path =~ /\.(php|twig)/
@files << [cleaned, path] if File.read(path) =~ /(\$trigger|Trigger::current\(\))->call\("[^"]+"(.*?)\)/
@files << [cleaned, path] if File.read(path) =~ /(\$trigger|Trigger::current\(\))->filter\(([^,]+), ?"[^"]+"(.*?)\)/
@files << [cleaned, path] if File.read(path) =~ /\$\{ ?trigger\.call\("[^"]+"(, ?(.+))?\) ?\}/
end
end
end
def do_scan
@files.each do |cleaned, file|
counter = 1
File.open(file, "r") do |infile|
while line = infile.gets
line.gsub!("\\\"", "{QUOTE}") # So that [^"]+ doesn't match \"'s in the translation.
scan_call line, counter, file, cleaned
scan_filter line, counter, file, cleaned
scan_twig_call line, counter, file, cleaned
counter += 1
end
end
end
end
def scan_call(text, line, filename, clean_filename)
text.gsub(/(\$trigger|Trigger::current\(\))->call\("([^"]+)"(, ?(.+))?\)/) do
if @triggers[:calls][$2].nil?
@triggers[:calls][$2] = { :places => [clean_filename + ":" + line.to_s],
:arguments => $4 }
elsif not @triggers[:calls][$2][:places].include?(clean_filename + ":" + line.to_s)
@triggers[:calls][$2][:places] << clean_filename + ":" + line.to_s
end
end
end
def scan_filter(text, line, filename, clean_filename)
text.gsub(/(\$trigger|Trigger::current\(\))->filter\(([^,]+), ?"([^"]+)"(, ?(.+))?\)/) do
if @triggers[:filters][$3].nil?
@triggers[:filters][$3] = { :places => [clean_filename + ":" + line.to_s],
:target => $2,
:arguments => $5 }
elsif not @triggers[:filters][$3][:places].include?(clean_filename + ":" + line.to_s)
@triggers[:filters][$3][:places] << clean_filename + ":" + line.to_s
end
end
end
def scan_twig_call(text, line, filename, clean_filename)
text.gsub(/\$\{ ?trigger\.call\("([^"]+)"(, ?(.+))?\) ?\}/) do
if @triggers[:calls][$1].nil?
@triggers[:calls][$1] = { :places => [clean_filename + ":" + line.to_s],
:arguments => $3 }
elsif not @triggers[:calls][$1][:places].include?(clean_filename + ":" + line.to_s)
@triggers[:calls][$1][:places] << clean_filename + ":" + line.to_s
end
end
end
def output
puts "=============================================="
puts " Trigger Calls"
print "=============================================="
@triggers[:calls].each do |name, trigger|
puts "\n\n"
puts name
name.length.times do
print "-"
end
puts "\n"
puts "Called from:"
trigger[:places].each { |place| puts "\t" + place.split(":").join(" on line ") }
next if trigger[:arguments].nil?
puts "Arguments:"
puts "\t" + trigger[:arguments]
end
puts "\n\n\n"
puts "=============================================="
puts " Trigger Filters"
print "=============================================="
@triggers[:filters].each do |name, trigger|
puts "\n\n"
puts name
name.length.times do
print "-"
end
puts "\n"
puts "Target:"
puts "\t" + trigger[:target]
puts "Called from:"
trigger[:places].each { |place| puts "\t" + place.split(":").join(" on line ") }
next if trigger[:arguments].nil?
puts "Arguments:"
puts "\t" + trigger[:arguments]
end
end
end
Triggers.new ARGV[0] || "."