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