comparison bin/update.sh @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children bf99236cc5cd
comparison
equal deleted inserted replaced
-1:000000000000 0:4681f974d28b
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-2015, The Roundcube Dev Team |
9 | |
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. |
13 | |
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 if ($_SERVER['RCMAIL_CONFIG_DIR']) {
22 define('RCMAIL_CONFIG_DIR', $_SERVER['RCMAIL_CONFIG_DIR']);
23 }
24
25 if ($_SERVER['DEBIAN_PKG']) {
26 define('DEBIAN_PKG', TRUE);
27 }
28
29 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
30
31 require_once INSTALL_PATH . 'program/include/clisetup.php';
32
33 // get arguments
34 $opts = rcube_utils::get_opt(array('v' => 'version', 'y' => 'accept'));
35
36 // ask user if no version is specified
37 if (!$opts['version']) {
38 echo "What version are you upgrading from? Type '?' if you don't know.\n";
39 if (($input = trim(fgets(STDIN))) && preg_match('/^[0-9.]+[a-z-]*$/', $input))
40 $opts['version'] = $input;
41 else
42 $opts['version'] = RCMAIL_VERSION;
43 }
44
45 $RCI = rcmail_install::get_instance();
46 $RCI->load_config();
47
48 if ($RCI->configured) {
49 $success = true;
50
51 if (($messages = $RCI->check_config()) || $RCI->legacy_config) {
52 $success = false;
53 $err = 0;
54
55 // list old/replaced config options
56 if (is_array($messages['replaced'])) {
57 echo "WARNING: Replaced config options:\n";
58 echo "(These config options have been replaced or renamed)\n";
59
60 foreach ($messages['replaced'] as $msg) {
61 echo "- '" . $msg['prop'] . "' was replaced by '" . $msg['replacement'] . "'\n";
62 $err++;
63 }
64 echo "\n";
65 }
66
67 // list obsolete config options (just a notice)
68 if (is_array($messages['obsolete'])) {
69 echo "NOTICE: Obsolete config options:\n";
70 echo "(You still have some obsolete or inexistent properties set. This isn't a problem but should be noticed)\n";
71
72 foreach ($messages['obsolete'] as $msg) {
73 echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
74 $err++;
75 }
76 echo "\n";
77 }
78
79 if (!$err && $RCI->legacy_config) {
80 echo "WARNING: Your configuration needs to be migrated!\n";
81 echo "We changed the configuration files structure and your two config files main.inc.php and db.inc.php have to be merged into one single file.\n";
82 $err++;
83 }
84
85 // ask user to update config files
86 if ($err) {
87 if (!$opts['accept']) {
88 echo "Do you want me to fix your local configuration? (y/N)\n";
89 $input = trim(fgets(STDIN));
90 }
91
92 // positive: let's merge the local config with the defaults
93 if ($opts['accept'] || strtolower($input) == 'y') {
94 $error = $written = false;
95
96 if (!DEBIAN_PKG) {
97 // backup current config
98 echo ". backing up the current config file(s)...\n";
99
100 foreach (array('config', 'main', 'db') as $file) {
101 if (file_exists(RCMAIL_CONFIG_DIR . '/' . $file . '.inc.php')) {
102 if (!copy(RCMAIL_CONFIG_DIR . '/' . $file . '.inc.php', RCMAIL_CONFIG_DIR . '/' . $file . '.old.php')) {
103 $error = true;
104 }
105 }
106 }
107 }
108
109 if (!$error) {
110 $RCI->merge_config();
111 if (DEBIAN_PKG) {
112 echo ". writing " . RCMAIL_CONFIG_DIR . "/config.inc.php.dpkg-new...\n";
113 } else {
114 echo ". writing " . RCMAIL_CONFIG_DIR . "/config.inc.php...\n";
115 }
116 $written = $RCI->save_configfile($RCI->create_config());
117 }
118
119 // Success!
120 if ($written) {
121 echo "Done.\n";
122 echo "Your configuration files are now up-to-date!\n";
123
124 if ($messages['missing']) {
125 echo "But you still need to add the following missing options:\n";
126 foreach ($messages['missing'] as $msg)
127 echo "- '" . $msg['prop'] . ($msg['name'] ? "': " . $msg['name'] : "'") . "\n";
128 }
129
130 if (!DEBIAN_PKG) {
131 if ($RCI->legacy_config) {
132 foreach (array('main', 'db') as $file) {
133 @unlink(RCMAIL_CONFIG_DIR . '/' . $file . '.inc.php');
134 }
135 }
136 }
137 }
138 else {
139 echo "Failed to write config file(s)!\n";
140 echo "Grant write privileges to the current user or update the files manually according to the above messages.\n";
141 }
142 }
143 else {
144 echo "Please update your config files manually according to the above messages.\n";
145 }
146 }
147
148 // check dependencies based on the current configuration
149 if (is_array($messages['dependencies'])) {
150 echo "WARNING: Dependency check failed!\n";
151 echo "(Some of your configuration settings require other options to be configured or additional PHP modules to be installed)\n";
152
153 foreach ($messages['dependencies'] as $msg) {
154 echo "- " . $msg['prop'] . ': ' . $msg['explain'] . "\n";
155 }
156 echo "Please fix your config files and run this script again!\n";
157 echo "See ya.\n";
158 }
159 }
160
161 // check file type detection
162 if ($RCI->check_mime_detection()) {
163 echo "WARNING: File type detection doesn't work properly!\n";
164 echo "Please check the 'mime_magic' config option or the finfo functions of PHP and run this script again.\n";
165 }
166 if ($RCI->check_mime_extensions()) {
167 echo "WARNING: Mimetype to file extension mapping doesn't work properly!\n";
168 echo "Please check the 'mime_types' config option and run this script again.\n";
169 }
170
171 if (!DEBIAN_PKG) {
172 // check database schema
173 if ($RCI->config['db_dsnw']) {
174 echo "Executing database schema update.\n";
175 system("php " . INSTALL_PATH . "bin/updatedb.sh --package=roundcube --version=" . $opts['version']
176 . " --dir=" . INSTALL_PATH . "SQL", $res);
177
178 $success = !$res;
179 }
180 }
181
182 // update composer dependencies
183 if (is_file(INSTALL_PATH . 'composer.json') && is_readable(INSTALL_PATH . 'composer.json-dist')) {
184 $composer_data = json_decode(file_get_contents(INSTALL_PATH . 'composer.json'), true);
185 $composer_template = json_decode(file_get_contents(INSTALL_PATH . 'composer.json-dist'), true);
186 $comsposer_json = null;
187
188 // update the require section with the new dependencies
189 if (is_array($composer_data['require']) && is_array($composer_template['require'])) {
190 $composer_data['require'] = array_merge($composer_data['require'], $composer_template['require']);
191 /* TO BE ADDED LATER
192 $old_packages = array();
193 for ($old_packages as $pkg) {
194 if (array_key_exists($composer_data['require'], $pkg)) {
195 unset($composer_data['require'][$pkg]);
196 }
197 }
198 */
199 }
200
201 // update the repositories section with the new dependencies
202 if (is_array($composer_template['repositories'])) {
203 if (!is_array($composer_data['repositories'])) {
204 $composer_data['repositories'] = array();
205 }
206
207 foreach ($composer_template['repositories'] as $repo) {
208 $rkey = $repo['type'] . preg_replace('/^https?:/', '', $repo['url']) . $repo['package']['name'];
209 $existing = false;
210 foreach ($composer_data['repositories'] as $k => $_repo) {
211 if ($rkey == $_repo['type'] . preg_replace('/^https?:/', '', $_repo['url']) . $_repo['package']['name']) {
212 $existing = true;
213 break;
214 }
215 // remove old repos
216 else if (strpos($_repo['url'], 'git://git.kolab.org') === 0) {
217 unset($composer_data['repositories'][$k]);
218 }
219 }
220 if (!$existing) {
221 $composer_data['repositories'][] = $repo;
222 }
223 }
224
225 $composer_data['repositories'] = array_values($composer_data['repositories']);
226 }
227
228 // use the JSON encoder from the Composer package
229 if (is_file('composer.phar')) {
230 include 'phar://composer.phar/src/Composer/Json/JsonFile.php';
231 $comsposer_json = \Composer\Json\JsonFile::encode($composer_data);
232 }
233 // PHP 5.4's json_encode() does the job, too
234 else if (defined('JSON_PRETTY_PRINT')) {
235 $comsposer_json = json_encode($composer_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
236 }
237 else {
238 $success = false;
239 $comsposer_json = null;
240 }
241
242 // write updated composer.json back to disk
243 if ($comsposer_json && is_writeable(INSTALL_PATH . 'composer.json')) {
244 $success &= (bool)file_put_contents(INSTALL_PATH . 'composer.json', $comsposer_json);
245 }
246 else {
247 echo "WARNING: unable to update composer.json!\n";
248 echo "Please replace the 'require' section in your composer.json with the following:\n";
249
250 $require_json = '';
251 foreach ($composer_data['require'] as $pkg => $ver) {
252 $require_json .= sprintf(' "%s": "%s",'."\n", $pkg, $ver);
253 }
254
255 echo ' "require": {'."\n";
256 echo rtrim($require_json, ",\n");
257 echo "\n }\n\n";
258 }
259
260 echo "NOTE: Update dependencies by running `php composer.phar update --no-dev`\n";
261 }
262
263 // index contacts for fulltext searching
264 if ($opts['version'] && version_compare(version_parse($opts['version']), '0.6.0', '<')) {
265 system("php " . INSTALL_PATH . 'bin/indexcontacts.sh');
266 }
267
268 if ($success) {
269 echo "This instance of Roundcube is up-to-date.\n";
270 echo "Have fun!\n";
271 }
272 }
273 else {
274 echo "This instance of Roundcube is not yet configured!\n";
275 echo "Open http://url-to-roundcube/installer/ in your browser and follow the instuctions.\n";
276 }
277
278 ?>