]> gitweb.michael.orlitzky.com - dead/janitor.git/blob - src/apache_conf_file.rb
Initial commit.
[dead/janitor.git] / src / apache_conf_file.rb
1 class ApacheConfFile
2
3 def initialize(conf_file_path)
4 if conf_file_path.nil? or not File.file?(conf_file_path)
5 raise(ArgumentError.new('How about passing in a real file?'))
6 end
7
8 # Read each line of the conf file into an array.
9 # Apache config directives are all one-line, right?
10 @file_lines = []
11
12 File::open(conf_file_path) do |f|
13 @file_lines = f.readlines()
14 end
15
16 if @file_lines.length < 1
17 raise(ArgumentError.new('Your file sucks.'))
18 end
19 end
20
21
22
23 def get_php_admin_values(directive_name)
24 # First, we have to regex-ize the directive name,
25 # since they can contain periods (at the least!).
26 directive_name.sub!('.', '\.')
27
28 # Our return variable, an array of matching directive values.
29 values = []
30
31 # Loop through each line in the conf file looking a
32 # matching pattern.
33 @file_lines.each do |line|
34 matches = line.match(/php_admin_value[[:space:]]+#{directive_name}[[:space:]](.*)$/)
35
36 if not matches.nil?
37 # If there's a match, there should be only one
38 # (in addition to [0], the entire matched string).
39 if matches.length > 2
40 raise(StandardError.new('Matched the Regex more than once?'))
41 end
42
43 # If there's only one, add it to our list.
44 # These are probably likely to have some extra
45 # whitespace around them one way or another.
46 values << matches[1].strip
47 end
48 end
49
50 return values
51 end
52
53
54 def get_php_temporary_directory_list(remove_duplicates = true)
55 # Return a list of all PHP "temporary" directories.
56 # I.e. upload_tmp_dir, session.save_path
57 temp_dirs = []
58 temp_dirs += self.get_php_admin_values('upload_tmp_dir')
59 temp_dirs += self.get_php_admin_values('session.save_path')
60
61 # We expect these to have been strip()ed already
62 if remove_duplicates
63 return temp_dirs.uniq
64 else
65 return temp_dirs
66 end
67 end
68
69 end