Aleksander Machniak
2012-09-07 00f34700632f7ff78df0c532d2605397820d0c79
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                       |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
e6bb83 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Check local configuration and database schema after upgrading       |
16  |   to a new version                                                    |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
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";
471d55 114
TB 115           if ($messages['missing']) {
116             echo "But you still need to add the following missing options:\n";
117             foreach ($messages['missing'] as $msg)
118               echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
119           }
fee8c6 120         }
T 121         else {
122           echo "Failed to write config files!\n";
123           echo "Grant write privileges to the current user or update the files manually according to the above messages.\n";
124         }
125       }
126       else {
e6bb83 127         echo "Please update your config files manually according to the above messages.\n\n";
fee8c6 128       }
T 129     }
130
131     // check dependencies based on the current configuration
132     if (is_array($messages['dependencies'])) {
133       echo "WARNING: Dependency check failed!\n";
134       echo "(Some of your configuration settings require other options to be configured or additional PHP modules to be installed)\n";
135
136       foreach ($messages['dependencies'] as $msg) {
137         echo "- " . $msg['prop'] . ': ' . $msg['explain'] . "\n";
138       }
139       echo "Please fix your config files and run this script again!\n";
140       echo "See ya.\n";
141     }
142   }
2491c6 143
T 144   // check database schema
145   if ($RCI->config['db_dsnw']) {
91f227 146     $DB = rcube_db::factory($RCI->config['db_dsnw'], '', false);
2491c6 147     $DB->db_connect('w');
T 148     if ($db_error_msg = $DB->is_error()) {
149       echo "Error connecting to database: $db_error_msg\n";
150       $success = false;
151     }
e6bb83 152     else if ($err = $RCI->db_schema_check($DB, false)) {
T 153       $updatefile = INSTALL_PATH . 'SQL/' . (isset($RCI->db_map[$DB->db_provider]) ? $RCI->db_map[$DB->db_provider] : $DB->db_provider) . '.update.sql';
2491c6 154       echo "WARNING: Database schema needs to be updated!\n";
e6bb83 155       echo join("\n", $err) . "\n\n";
2491c6 156       $success = false;
e6bb83 157       
T 158       if ($opts['version']) {
159         echo "Do you want to run the update queries to get the schmea fixed? (y/N)\n";
160         $input = trim(fgets(STDIN));
161         if (strtolower($input) == 'y') {
162           $success = $RCI->update_db($DB, $opts['version']);
163         }
164       }
165       
166       if (!$success)
167         echo "Open $updatefile and execute all queries below the comment with the currently installed version number.\n";
2491c6 168     }
T 169   }
170   
faf10e 171   // index contacts for fulltext searching
T 172   if (version_compare($opts['version'], '0.6', '<')) {
173     system(INSTALL_PATH . 'bin/indexcontacts.sh');
174   }
2491c6 175   
T 176   if ($success) {
e019f2 177     echo "This instance of Roundcube is up-to-date.\n";
fee8c6 178     echo "Have fun!\n";
T 179   }
180 }
181 else {
e019f2 182   echo "This instance of Roundcube is not yet configured!\n";
fee8c6 183   echo "Open http://url-to-roundcube/installer/ in your browser and follow the instuctions.\n";
T 184 }
185
186 echo "\n";
187
d48470 188 ?>