~george-edison55/charms/oneiric/thinkup/update-to-1-0-2

« back to all changes in this revision

Viewing changes to opt/update_thinkup_config

  • Committer: Nathan Osman
  • Date: 2011-11-21 23:09:06 UTC
  • Revision ID: admin@quickmediasolutions.com-20111121230906-mgwx2rbkz7dsla2k
Removed sed and switched to a PHP script for updating the config file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/php
 
2
<?php
 
3
 
 
4
//===============================
 
5
// This small PHP script updates
 
6
//    the configuration file
 
7
//===============================
 
8
 
 
9
// This is the path to the config file we are
 
10
// interested in.
 
11
define('CONFIG_FILE', '/var/www/config.inc.php');
 
12
 
 
13
// Get the current contents of the config file
 
14
$contents = file_get_contents(CONFIG_FILE);
 
15
 
 
16
// Read the configuration options to be changed from STDIN
 
17
while(TRUE)
 
18
{
 
19
    // Read a line from STDIN that contains '='
 
20
    $input_line = fgets(STDIN);
 
21
    
 
22
    if($input_line === FALSE)
 
23
        break;
 
24
    if(strpos($input_line, '=') === FALSE)
 
25
        continue;
 
26
    
 
27
    // Extract the key / value from the line
 
28
    list($key, $value) = explode('=', trim($input_line), 2);
 
29
    
 
30
    // Replace all occurrences of the key with
 
31
    // the key's new value.
 
32
    $contents = preg_replace('/^\$THINKUP_CFG\[\'' . preg_quote($key) . '\'\](\s*)=.*$/m',
 
33
                             "\$THINKUP_CFG['$key']\$1= $value;",
 
34
                             $contents);
 
35
}
 
36
 
 
37
// Open the file for writing and write the new contents
 
38
$file = fopen(CONFIG_FILE, 'w');
 
39
fwrite($file, $contents);
 
40
fclose($file);
 
41
 
 
42
?>