]> git.donarmstrong.com Git - roundcube.git/blob - bin/update.sh
New upstream version.
[roundcube.git] / bin / update.sh
1 #!/usr/bin/env php
2 <?php
3 /*
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
22 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
23
24 require_once INSTALL_PATH . 'program/include/clisetup.php';
25 require_once INSTALL_PATH . 'installer/rcube_install.php';
26
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
40
41 $RCI = rcube_install::get_instance();
42 $RCI->load_config();
43
44 if ($RCI->configured) {
45   $success = true;
46   
47   if ($messages = $RCI->check_config()) {
48     $success = false;
49     $err = 0;
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) {
57         echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
58         $err++;
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) {
69         echo "- '" . $msg['prop'] . "' was replaced by '" . $msg['replacement'] . "'\n";
70         $err++;
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) {
81         echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
82         $err++;
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";
113           echo "Your configuration files are now up-to-date!\n";
114         }
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 {
121         echo "Please update your config files manually according to the above messages.\n\n";
122       }
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   }
137
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     }
146     else if ($err = $RCI->db_schema_check($DB, false)) {
147       $updatefile = INSTALL_PATH . 'SQL/' . (isset($RCI->db_map[$DB->db_provider]) ? $RCI->db_map[$DB->db_provider] : $DB->db_provider) . '.update.sql';
148       echo "WARNING: Database schema needs to be updated!\n";
149       echo join("\n", $err) . "\n\n";
150       $success = false;
151       
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";
162     }
163   }
164   
165   // index contacts for fulltext searching
166   if (version_compare($opts['version'], '0.6', '<')) {
167     system(INSTALL_PATH . 'bin/indexcontacts.sh');
168   }
169   
170   if ($success) {
171     echo "This instance of Roundcube is up-to-date.\n";
172     echo "Have fun!\n";
173   }
174 }
175 else {
176   echo "This instance of Roundcube is not yet configured!\n";
177   echo "Open http://url-to-roundcube/installer/ in your browser and follow the instuctions.\n";
178 }
179
180 echo "\n";
181
182 ?>