thomascube
2011-03-01 79cd6cefd5996031ebb93ffdac609d503553afb0
commit | author | age
d48470 1 #!/usr/bin/env php
fee8c6 2 <?php
e6bb83 3 /*
T 4  +-----------------------------------------------------------------------+
5  | bin/update.sh                                                         |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2010-2011, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Check local configuration and database schema after upgrading       |
13  |   to a new version                                                    |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
d48470 22 if (php_sapi_name() != 'cli') {
T 23     die('Not on the "shell" (php-cli).');
24 }
fee8c6 25 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
T 26
e6bb83 27 require_once INSTALL_PATH . 'program/include/clisetup.php';
fee8c6 28 require_once INSTALL_PATH . 'installer/rcube_install.php';
e6bb83 29
T 30 // get arguments
31 $opts = get_opt(array('v' => 'version'));
32
33 // ask user if no version is specified
34 if (!$opts['version']) {
35   echo "What version are you upgrading from? Type '?' if you don't know.\n";
36   if (($input = trim(fgets(STDIN))) && preg_match('/^[0-9.]+[a-z-]*$/', $input))
37     $opts['version'] = $input;
38 }
39
40 if ($opts['version'] && version_compare($opts['version'], RCMAIL_VERSION, '>'))
41   die("Nothing to be done here. Bye!\n");
42
fee8c6 43
T 44 $RCI = rcube_install::get_instance();
45 $RCI->load_config();
46
47 if ($RCI->configured) {
2491c6 48   $success = true;
T 49   
fee8c6 50   if ($messages = $RCI->check_config()) {
2491c6 51     $success = false;
fee8c6 52     $err = 0;
T 53
54     // list missing config options
55     if (is_array($messages['missing'])) {
56       echo "WARNING: Missing config options:\n";
57       echo "(These config options should be present in the current configuration)\n";
58
59       foreach ($messages['missing'] as $msg) {
cbffc2 60         echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
fee8c6 61         $err++;
T 62       }
63       echo "\n";
64     }
65
66     // list old/replaced config options
67     if (is_array($messages['replaced'])) {
68       echo "WARNING: Replaced config options:\n";
69       echo "(These config options have been replaced or renamed)\n";
70
71       foreach ($messages['replaced'] as $msg) {
cbffc2 72         echo "- '" . $msg['prop'] . "' was replaced by '" . $msg['replacement'] . "'\n";
fee8c6 73         $err++;
T 74       }
75       echo "\n";
76     }
77
78     // list obsolete config options (just a notice)
79     if (is_array($messages['obsolete'])) {
80       echo "NOTICE: Obsolete config options:\n";
81       echo "(You still have some obsolete or inexistent properties set. This isn't a problem but should be noticed)\n";
82
83       foreach ($messages['obsolete'] as $msg) {
cbffc2 84         echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
fee8c6 85         $err++;
T 86       }
87       echo "\n";
88     }
89
90     // ask user to update config files
91     if ($err) {
92       echo "Do you want me to fix your local configuration? (y/N)\n";
93       $input = trim(fgets(STDIN));
94
95       // positive: let's merge the local config with the defaults
96       if (strtolower($input) == 'y') {
97         $copy1 = $copy2 = $write1 = $write2 = false;
98         
99         // backup current config
100         echo ". backing up the current config files...\n";
101         $copy1 = copy(RCMAIL_CONFIG_DIR . '/main.inc.php', RCMAIL_CONFIG_DIR . '/main.old.php');
102         $copy2 = copy(RCMAIL_CONFIG_DIR . '/db.inc.php', RCMAIL_CONFIG_DIR . '/db.old.php');
103         
104         if ($copy1 && $copy2) {
105           $RCI->merge_config();
106         
107           echo ". writing " . RCMAIL_CONFIG_DIR . "/main.inc.php...\n";
108           $write1 = file_put_contents(RCMAIL_CONFIG_DIR . '/main.inc.php', $RCI->create_config('main', true));
109           echo ". writing " . RCMAIL_CONFIG_DIR . "/main.db.php...\n";
110           $write2 = file_put_contents(RCMAIL_CONFIG_DIR . '/db.inc.php', $RCI->create_config('db', true));
111         }
112         
113         // Success!
114         if ($write1 && $write2) {
115           echo "Done.\n";
0da902 116           echo "Your configuration files are now up-to-date!\n";
fee8c6 117         }
T 118         else {
119           echo "Failed to write config files!\n";
120           echo "Grant write privileges to the current user or update the files manually according to the above messages.\n";
121         }
122       }
123       else {
e6bb83 124         echo "Please update your config files manually according to the above messages.\n\n";
fee8c6 125       }
T 126     }
127
128     // check dependencies based on the current configuration
129     if (is_array($messages['dependencies'])) {
130       echo "WARNING: Dependency check failed!\n";
131       echo "(Some of your configuration settings require other options to be configured or additional PHP modules to be installed)\n";
132
133       foreach ($messages['dependencies'] as $msg) {
134         echo "- " . $msg['prop'] . ': ' . $msg['explain'] . "\n";
135       }
136       echo "Please fix your config files and run this script again!\n";
137       echo "See ya.\n";
138     }
139   }
2491c6 140
T 141   // check database schema
142   if ($RCI->config['db_dsnw']) {
143     $DB = new rcube_mdb2($RCI->config['db_dsnw'], '', false);
144     $DB->db_connect('w');
145     if ($db_error_msg = $DB->is_error()) {
146       echo "Error connecting to database: $db_error_msg\n";
147       $success = false;
148     }
e6bb83 149     else if ($err = $RCI->db_schema_check($DB, false)) {
T 150       $updatefile = INSTALL_PATH . 'SQL/' . (isset($RCI->db_map[$DB->db_provider]) ? $RCI->db_map[$DB->db_provider] : $DB->db_provider) . '.update.sql';
2491c6 151       echo "WARNING: Database schema needs to be updated!\n";
e6bb83 152       echo join("\n", $err) . "\n\n";
2491c6 153       $success = false;
e6bb83 154       
T 155       if ($opts['version']) {
156         echo "Do you want to run the update queries to get the schmea fixed? (y/N)\n";
157         $input = trim(fgets(STDIN));
158         if (strtolower($input) == 'y') {
159           $success = $RCI->update_db($DB, $opts['version']);
160         }
161       }
162       
163       if (!$success)
164         echo "Open $updatefile and execute all queries below the comment with the currently installed version number.\n";
2491c6 165     }
T 166   }
167   
168   
169   if ($success) {
e019f2 170     echo "This instance of Roundcube is up-to-date.\n";
fee8c6 171     echo "Have fun!\n";
T 172   }
173 }
174 else {
e019f2 175   echo "This instance of Roundcube is not yet configured!\n";
fee8c6 176   echo "Open http://url-to-roundcube/installer/ in your browser and follow the instuctions.\n";
T 177 }
178
179 echo "\n";
180
d48470 181 ?>