Blame view

sources/lib/private/updater.php 6.93 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
  <?php
  /**
   * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
31b7f2792   Kload   Upgrade to ownclo...
8
  namespace OC;
03e52840d   Kload   Init
9
10
11
12
13
14
15
16
17
  use OC\Hooks\BasicEmitter;
  
  /**
   * Class that handles autoupdating of ownCloud
   *
   * Hooks provided in scope \OC\Updater
   *  - maintenanceStart()
   *  - maintenanceEnd()
   *  - dbUpgrade()
03e52840d   Kload   Init
18
19
   *  - failure(string $message)
   */
31b7f2792   Kload   Upgrade to ownclo...
20
21
22
23
24
25
  class Updater extends BasicEmitter {
  
  	/**
  	 * @var \OC\Log $log
  	 */
  	private $log;
6d9380f96   Cédric Dupont   Update sources OC...
26
27
28
  	private $simulateStepEnabled;
  
  	private $updateStepEnabled;
31b7f2792   Kload   Upgrade to ownclo...
29
30
31
32
33
  	/**
  	 * @param \OC\Log $log
  	 */
  	public function __construct($log = null) {
  		$this->log = $log;
6d9380f96   Cédric Dupont   Update sources OC...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  		$this->simulateStepEnabled = true;
  		$this->updateStepEnabled = true;
  	}
  
  	/**
  	 * Sets whether the database migration simulation must
  	 * be enabled.
  	 * This can be set to false to skip this test.
  	 *
  	 * @param bool $flag true to enable simulation, false otherwise
  	 */
  	public function setSimulateStepEnabled($flag) {
  		$this->simulateStepEnabled = $flag;
  	}
  
  	/**
  	 * Sets whether the update must be performed.
  	 * This can be set to false to skip the actual update.
  	 *
  	 * @param bool $flag true to enable update, false otherwise
  	 */
  	public function setUpdateStepEnabled($flag) {
  		$this->updateStepEnabled = $flag;
31b7f2792   Kload   Upgrade to ownclo...
57
  	}
03e52840d   Kload   Init
58
59
  	/**
  	 * Check if a new version is available
31b7f2792   Kload   Upgrade to ownclo...
60
  	 * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
6d9380f96   Cédric Dupont   Update sources OC...
61
  	 * @return array|bool
03e52840d   Kload   Init
62
  	 */
31b7f2792   Kload   Upgrade to ownclo...
63
64
65
  	public function check($updaterUrl) {
  
  		// Look up the cache - it is invalidated all 30 minutes
03e52840d   Kload   Init
66
67
68
  		if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) {
  			return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true);
  		}
31b7f2792   Kload   Upgrade to ownclo...
69

03e52840d   Kload   Init
70
  		\OC_Appconfig::setValue('core', 'lastupdatedat', time());
31b7f2792   Kload   Upgrade to ownclo...
71

03e52840d   Kload   Init
72
73
74
75
76
77
78
  		if (\OC_Appconfig::getValue('core', 'installedat', '') == '') {
  			\OC_Appconfig::setValue('core', 'installedat', microtime(true));
  		}
  
  		$version = \OC_Util::getVersion();
  		$version['installed'] = \OC_Appconfig::getValue('core', 'installedat');
  		$version['updated'] = \OC_Appconfig::getValue('core', 'lastupdatedat');
6d9380f96   Cédric Dupont   Update sources OC...
79
  		$version['updatechannel'] = \OC_Util::getChannel();
03e52840d   Kload   Init
80
  		$version['edition'] = \OC_Util::getEditionString();
31b7f2792   Kload   Upgrade to ownclo...
81
  		$version['build'] = \OC_Util::getBuild();
03e52840d   Kload   Init
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  		$versionString = implode('x', $version);
  
  		//fetch xml data from updater
  		$url = $updaterUrl . '?version=' . $versionString;
  
  		// set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  		$ctx = stream_context_create(
  			array(
  				'http' => array(
  					'timeout' => 10
  				)
  			)
  		);
  		$xml = @file_get_contents($url, 0, $ctx);
  		if ($xml == false) {
  			return array();
  		}
837968727   Kload   [enh] Upgrade to ...
99
  		$loadEntities = libxml_disable_entity_loader(true);
03e52840d   Kload   Init
100
  		$data = @simplexml_load_string($xml);
837968727   Kload   [enh] Upgrade to ...
101
  		libxml_disable_entity_loader($loadEntities);
03e52840d   Kload   Init
102
103
104
105
106
107
  
  		$tmp = array();
  		$tmp['version'] = $data->version;
  		$tmp['versionstring'] = $data->versionstring;
  		$tmp['url'] = $data->url;
  		$tmp['web'] = $data->web;
31b7f2792   Kload   Upgrade to ownclo...
108
  		// Cache the result
03e52840d   Kload   Init
109
  		\OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data));
03e52840d   Kload   Init
110

31b7f2792   Kload   Upgrade to ownclo...
111
  		return $tmp;
03e52840d   Kload   Init
112
113
114
115
  	}
  
  	/**
  	 * runs the update actions in maintenance mode, does not upgrade the source files
6d9380f96   Cédric Dupont   Update sources OC...
116
117
118
  	 * except the main .htaccess file
  	 *
  	 * @return bool true if the operation succeeded, false otherwise
03e52840d   Kload   Init
119
120
121
122
  	 */
  	public function upgrade() {
  		\OC_DB::enableCaching(false);
  		\OC_Config::setValue('maintenance', true);
6d9380f96   Cédric Dupont   Update sources OC...
123

03e52840d   Kload   Init
124
125
  		$installedVersion = \OC_Config::getValue('version', '0.0.0');
  		$currentVersion = implode('.', \OC_Util::getVersion());
31b7f2792   Kload   Upgrade to ownclo...
126
127
128
129
  		if ($this->log) {
  			$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  		}
  		$this->emit('\OC\Updater', 'maintenanceStart');
a293d369c   Kload   Update sources to...
130

6d9380f96   Cédric Dupont   Update sources OC...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  		try {
  			$this->doUpgrade($currentVersion, $installedVersion);
  		} catch (\Exception $exception) {
  			$this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
  		}
  
  		\OC_Config::setValue('maintenance', false);
  		$this->emit('\OC\Updater', 'maintenanceEnd');
  	}
  
  	/**
  	 * runs the update actions in maintenance mode, does not upgrade the source files
  	 * except the main .htaccess file
  	 *
  	 * @param string $currentVersion current version to upgrade to
  	 * @param string $installedVersion previous version from which to upgrade from
  	 *
  	 * @return bool true if the operation succeeded, false otherwise
  	 */
  	private function doUpgrade($currentVersion, $installedVersion) {
  		// Update htaccess files for apache hosts
  		if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  			\OC_Setup::updateHtaccess();
  		}
837968727   Kload   [enh] Upgrade to ...
155
156
157
158
  		// create empty file in data dir, so we can later find
  		// out that this is indeed an ownCloud data directory
  		// (in case it didn't exist before)
  		file_put_contents(\OC_Config::getValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', '');
a293d369c   Kload   Update sources to...
159
160
161
  		/*
  		 * START CONFIG CHANGES FOR OLDER VERSIONS
  		 */
6d9380f96   Cédric Dupont   Update sources OC...
162
  		if (!\OC::$CLI && version_compare($installedVersion, '6.90.1', '<')) {
837968727   Kload   [enh] Upgrade to ...
163
  			// Add the trusted_domains config if it is not existant
a293d369c   Kload   Update sources to...
164
  			// This is added to prevent host header poisoning
6d9380f96   Cédric Dupont   Update sources OC...
165
  			\OC_Config::setValue('trusted_domains', \OC_Config::getValue('trusted_domains', array(\OC_Request::serverHost())));
a293d369c   Kload   Update sources to...
166
  		}
6d9380f96   Cédric Dupont   Update sources OC...
167

a293d369c   Kload   Update sources to...
168
169
170
  		/*
  		 * STOP CONFIG CHANGES FOR OLDER VERSIONS
  		 */
6d9380f96   Cédric Dupont   Update sources OC...
171
172
173
  		// pre-upgrade repairs
  		$repair = new \OC\Repair(\OC\Repair::getBeforeUpgradeRepairSteps());
  		$repair->run();
a293d369c   Kload   Update sources to...
174

6d9380f96   Cédric Dupont   Update sources OC...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  		// simulate DB upgrade
  		if ($this->simulateStepEnabled) {
  			// simulate core DB upgrade
  			\OC_DB::simulateUpdateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  
  			// simulate apps DB upgrade
  			$version = \OC_Util::getVersion();
  			$apps = \OC_App::getEnabledApps();
  			foreach ($apps as $appId) {
  				$info = \OC_App::getAppInfo($appId);
  				if (\OC_App::isAppCompatible($version, $info) && \OC_App::shouldUpgrade($appId)) {
  					if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
  						\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
  					}
  				}
  			}
03e52840d   Kload   Init
191

6d9380f96   Cédric Dupont   Update sources OC...
192
  			$this->emit('\OC\Updater', 'dbSimulateUpgrade');
03e52840d   Kload   Init
193
  		}
31b7f2792   Kload   Upgrade to ownclo...
194

6d9380f96   Cédric Dupont   Update sources OC...
195
196
197
198
199
200
  		// upgrade from OC6 to OC7
  		// TODO removed it again for OC8
  		$sharePolicy = \OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
  		if ($sharePolicy === 'groups_only') {
  			\OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', 'yes');
  		}
31b7f2792   Kload   Upgrade to ownclo...
201

6d9380f96   Cédric Dupont   Update sources OC...
202
203
204
205
  		if ($this->updateStepEnabled) {
  			// do the real upgrade
  			\OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  			$this->emit('\OC\Updater', 'dbUpgrade');
03e52840d   Kload   Init
206

6d9380f96   Cédric Dupont   Update sources OC...
207
208
209
  			$disabledApps = \OC_App::checkAppsRequirements();
  			if (!empty($disabledApps)) {
  				$this->emit('\OC\Updater', 'disabledApps', array($disabledApps));
03e52840d   Kload   Init
210
  			}
6d9380f96   Cédric Dupont   Update sources OC...
211
212
213
214
215
216
217
218
219
220
221
222
  			// load all apps to also upgrade enabled apps
  			\OC_App::loadApps();
  
  			// post-upgrade repairs
  			$repair = new \OC\Repair(\OC\Repair::getRepairSteps());
  			$repair->run();
  
  			//Invalidate update feed
  			\OC_Appconfig::setValue('core', 'lastupdatedat', 0);
  
  			// only set the final version if everything went well
  			\OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
03e52840d   Kload   Init
223
  		}
03e52840d   Kload   Init
224
  	}
31b7f2792   Kload   Upgrade to ownclo...
225
  }
a293d369c   Kload   Update sources to...
226