Skip to content

Instantly share code, notes, and snippets.

@euantorano
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euantorano/8eccd4fb9eeab567de75 to your computer and use it in GitHub Desktop.
Save euantorano/8eccd4fb9eeab567de75 to your computer and use it in GitHub Desktop.
A tool to find calls to PHP functions withina directory. Created as an experiment after this thread on the XenForo Community: http://xenforo.com/community/threads/php-functions-via-the-disable_functions-directive-in-php-ini.72121
#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: php_function_finder.rb [options]"
options[:php_source_path] = ""
opts.on( '-p', '--path PATH', 'Specify a path to the root of your PHP source files. Defaults to current directory.' ) do |php_source_path|
options[:php_source_path] = php_source_path.strip.chomp("\\").chomp("/") + "/"
end
options[:output_file] = "php_functions.txt"
opts.on('-o', '--output PATH', 'Specify an output file to put the list of functions in. Defaults to php_functions.txt.') do |output_file_path|
options[:output_file] = output_file_path
end
opts.on( '-h', '--help', 'Display this help screen' ) do
puts opts
exit
end
end.parse!
functions = []
num_functions = 0
File.open(options[:output_file], "w+") do |file_output|
Dir.glob(options[:php_source_path] + "**/*.php") do |php_file|
file = File.open(php_file, "r")
line_no = 0
file_name = php_file[options[:php_source_path].length, php_file.length - options[:php_source_path].length]
while !file.eof?
line = file.readline
line_no += 1
if (/(?<preceding_text>[^\s]*(::|->|new|function|\/\/|\* )[\s]*)?(?<function_name>[a-zA-Z_]+)\((?<function_params>(.*))\)/i =~ line)
if ((preceding_text.nil? || preceding_text.empty?) && !functions.include?(function_name))
num_functions += 1
functions.push function_name
file_output.puts "#{function_name} - found on line #{line_no} of #{file_name}"
end
end
end
end
end
puts "Wrote list of #{num_functions} functions to #{options[:output_file]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment