|
1 | 1 | #!/usr/bin/env ruby |
2 | 2 |
|
| 3 | +require 'rubygems' |
3 | 4 | require 'mysql' |
4 | 5 | require 'optparse' |
5 | 6 | require 'ostruct' |
6 | | - |
7 | | -class DatabaseFinder |
8 | | - attr_accessor :application, :locations, :exists_at, :located_at, :regex, :dbname, :dbuser, :dbpass, :dbhost |
9 | | - |
10 | | - @@supported_applications = [:joomla, :wordpress, :drupal, :phpbb, :gallery, :zencart, :mediawiki] |
11 | | - |
12 | | - @@app_metadata = {} |
13 | | - @@app_metadata[:drupal] = { |
14 | | - :locations => ["sites/default/settings.php"], |
15 | | - :regex => { |
16 | | - :dbname => Regexp.new("^\\$db_url.*?\\/\\/.*?\\/(.*?)'"), |
17 | | - :dbuser => Regexp.new("^\\$db_url.*?\\/\\/(.*)?:.*?'"), |
18 | | - :dbpass => Regexp.new("^\\$db_url.*?\\/\\/.*?:(.*)?@.*?'"), |
19 | | - :dbhost => Regexp.new("^\\$db_url.*?\\/\\/.*?@(.*)?\\/.*?'"), |
20 | | - }, |
21 | | - } |
22 | | - @@app_metadata[:gallery] = { |
23 | | - :locations => ["config.php"], |
24 | | - :regex => { |
25 | | - :dbname => Regexp.new("\\$storeConfig\\['database'.*?'(.*?)'"), |
26 | | - :dbuser => Regexp.new("\\$storeConfig\\['username'.*?'(.*?)'"), |
27 | | - :dbpass => Regexp.new("\\$storeConfig\\['password'.*?'(.*?)'"), |
28 | | - :dbhost => Regexp.new("\\$storeConfig\\['hostname'.*?'(.*?)'"), |
29 | | - }, |
30 | | - } |
31 | | - @@app_metadata[:joomla] = { |
32 | | - :locations => ["configuration.php"], |
33 | | - :regex => { |
34 | | - :dbname => Regexp.new("\\$db\\s*=\\s*'(.*?)'"), |
35 | | - :dbuser => Regexp.new("\\$user\\s*=\\s*'(.*?)'"), |
36 | | - :dbpass => Regexp.new("\\$password\\s*=\\s*'(.*?)'"), |
37 | | - :dbhost => Regexp.new("\\$host\\s*=\\s*'(.*?)'"), |
38 | | - }, |
39 | | - } |
40 | | - @@app_metadata[:mediawiki] = { |
41 | | - :locations => ["LocalSettings.php"], |
42 | | - :regex => { |
43 | | - :dbname => Regexp.new("\\$wgDBname\\s*=\\s*'(.*?)'"), |
44 | | - :dbuser => Regexp.new("\\$wgDBuser\\s*=\\s*'(.*?)'"), |
45 | | - :dbpass => Regexp.new("\\$wgDBpassword\\s*=\\s*'(.*?)'"), |
46 | | - :dbhost => Regexp.new("\\$wgDBserver\\s*=\\s*'(.*?)'"), |
47 | | - }, |
48 | | - } |
49 | | - @@app_metadata[:phpbb] = { |
50 | | - :locations => ["config.php"], |
51 | | - :regex => { |
52 | | - :dbname => Regexp.new("dbname\\s*=\\s*'(.*?)'"), |
53 | | - :dbuser => Regexp.new("dbuser\\s*=\\s*'(.*?)'"), |
54 | | - :dbpass => Regexp.new("dbpasswd\\s*=\\s*'(.*?)'"), |
55 | | - :dbhost => Regexp.new("dbhost\\s*=\\s*'(.*?)'"), |
56 | | - }, |
57 | | - } |
58 | | - @@app_metadata[:wordpress] = { |
59 | | - :locations => ["wp-config.php"], |
60 | | - :regex => { |
61 | | - :dbname => Regexp.new("DB_NAME'.*?'(.*?)'"), |
62 | | - :dbuser => Regexp.new("DB_USER'.*?'(.*?)'"), |
63 | | - :dbpass => Regexp.new("DB_PASSWORD'.*?'(.*?)'"), |
64 | | - :dbhost => Regexp.new("DB_HOST'.*?'(.*?)'"), |
65 | | - }, |
66 | | - } |
67 | | - @@app_metadata[:zencart] = { |
68 | | - :locations => ["includes/configure.php"], |
69 | | - :regex => { |
70 | | - :dbname => Regexp.new("DB_DATABASE'.*?'(.*?)'"), |
71 | | - :dbuser => Regexp.new("DB_SERVER_USERNAME'.*?'(.*?)'"), |
72 | | - :dbpass => Regexp.new("DB_SERVER_PASSWORD'.*?'(.*?)'"), |
73 | | - :dbhost => Regexp.new("DB_SERVER'.*?'(.*?)'"), |
74 | | - }, |
75 | | - } |
76 | | - |
77 | | - def initialize(app) |
78 | | - @exists_at = [] |
79 | | - @dbname = nil |
80 | | - @dbuser = nil |
81 | | - @dbpass = nil |
82 | | - @dbhost = nil |
83 | | - self.application = app |
84 | | - self.locations = @@app_metadata[@application][:locations] |
85 | | - @regex = @@app_metadata[@application][:regex] |
86 | | - end |
87 | | - |
88 | | - def application=(app) |
89 | | - a = String(app).downcase.to_sym |
90 | | - if @@supported_applications.include?(a) |
91 | | - instance_variable_set(:@application, a) |
92 | | - else |
93 | | - raise "That application is not supported yet." |
94 | | - end |
95 | | - end |
96 | | - |
97 | | - def find_db_info |
98 | | - locate_files |
99 | | - @exists_at.each do |file| |
100 | | - # puts "Searching '#{file}' for db connection info..." |
101 | | - File.foreach(file) do |line| |
102 | | - @dbname ||= @regex[:dbname].match(line)[1] if @regex[:dbname] === line |
103 | | - @dbuser ||= @regex[:dbuser].match(line)[1] if @regex[:dbuser] === line |
104 | | - @dbpass ||= @regex[:dbpass].match(line)[1] if @regex[:dbpass] === line |
105 | | - @dbhost ||= @regex[:dbhost].match(line)[1] if @regex[:dbhost] === line |
106 | | - @located_at = file if @dbname |
107 | | - end |
108 | | - end |
109 | | - end |
110 | | - |
111 | | - def manual_connection_string |
112 | | - "mysql -u #{@dbuser} -p#{@dbpass} -h #{@dbhost} #{@dbname}" |
113 | | - end |
114 | | - |
115 | | - private |
116 | | - def locate_files |
117 | | - @locations.each do |loc| |
118 | | - if File.exists?(loc) |
119 | | - @exists_at << loc |
120 | | - end |
121 | | - end |
122 | | - end |
123 | | -end |
124 | | - |
| 7 | +require 'database_sleuth' |
125 | 8 |
|
126 | 9 | def fetch_args(args) |
127 | 10 | options = OpenStruct.new |
|
227 | 110 | def optimize_tables! |
228 | 111 | @@options ||= fetch_args(ARGV) |
229 | 112 |
|
230 | | - df = DatabaseFinder.new(@@options.app_type) |
| 113 | + df = DatabaseSleuth.new(@@options.app_type) |
231 | 114 | df.find_db_info |
232 | 115 |
|
233 | 116 | puts "Connect manually with:" |
|
0 commit comments