Blame view

sources/lib/private/updater.php 5.42 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
18
19
20
21
22
  use OC\Hooks\BasicEmitter;
  
  /**
   * Class that handles autoupdating of ownCloud
   *
   * Hooks provided in scope \OC\Updater
   *  - maintenanceStart()
   *  - maintenanceEnd()
   *  - dbUpgrade()
   *  - filecacheStart()
   *  - filecacheProgress(int $percentage)
   *  - filecacheDone()
   *  - failure(string $message)
   */
31b7f2792   Kload   Upgrade to ownclo...
23
24
25
26
27
28
29
30
31
32
33
34
35
  class Updater extends BasicEmitter {
  
  	/**
  	 * @var \OC\Log $log
  	 */
  	private $log;
  
  	/**
  	 * @param \OC\Log $log
  	 */
  	public function __construct($log = null) {
  		$this->log = $log;
  	}
03e52840d   Kload   Init
36
37
  	/**
  	 * Check if a new version is available
31b7f2792   Kload   Upgrade to ownclo...
38
  	 * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
03e52840d   Kload   Init
39
40
  	 * @return array | bool
  	 */
31b7f2792   Kload   Upgrade to ownclo...
41
42
43
  	public function check($updaterUrl) {
  
  		// Look up the cache - it is invalidated all 30 minutes
03e52840d   Kload   Init
44
45
46
  		if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) {
  			return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true);
  		}
31b7f2792   Kload   Upgrade to ownclo...
47

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

03e52840d   Kload   Init
50
51
52
53
54
55
56
  		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');
31b7f2792   Kload   Upgrade to ownclo...
57
  		$version['updatechannel'] = \OC_Util::getChannel(); 
03e52840d   Kload   Init
58
  		$version['edition'] = \OC_Util::getEditionString();
31b7f2792   Kload   Upgrade to ownclo...
59
  		$version['build'] = \OC_Util::getBuild();
03e52840d   Kload   Init
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  		$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 ...
77
  		$loadEntities = libxml_disable_entity_loader(true);
03e52840d   Kload   Init
78
  		$data = @simplexml_load_string($xml);
837968727   Kload   [enh] Upgrade to ...
79
  		libxml_disable_entity_loader($loadEntities);
03e52840d   Kload   Init
80
81
82
83
84
85
  
  		$tmp = array();
  		$tmp['version'] = $data->version;
  		$tmp['versionstring'] = $data->versionstring;
  		$tmp['url'] = $data->url;
  		$tmp['web'] = $data->web;
31b7f2792   Kload   Upgrade to ownclo...
86
  		// Cache the result
03e52840d   Kload   Init
87
  		\OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data));
03e52840d   Kload   Init
88

31b7f2792   Kload   Upgrade to ownclo...
89
  		return $tmp;
03e52840d   Kload   Init
90
91
92
93
94
95
96
97
98
99
  	}
  
  	/**
  	 * runs the update actions in maintenance mode, does not upgrade the source files
  	 */
  	public function upgrade() {
  		\OC_DB::enableCaching(false);
  		\OC_Config::setValue('maintenance', true);
  		$installedVersion = \OC_Config::getValue('version', '0.0.0');
  		$currentVersion = implode('.', \OC_Util::getVersion());
31b7f2792   Kload   Upgrade to ownclo...
100
101
102
103
  		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...
104

837968727   Kload   [enh] Upgrade to ...
105
106
107
108
  		// 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...
109
110
111
  		/*
  		 * START CONFIG CHANGES FOR OLDER VERSIONS
  		 */
837968727   Kload   [enh] Upgrade to ...
112
113
  		if (!\OC::$CLI && version_compare($installedVersion, '6.00.4', '<')) {
  			// Add the trusted_domains config if it is not existant
a293d369c   Kload   Update sources to...
114
115
116
117
118
119
  			// This is added to prevent host header poisoning
  			\OC_Config::setValue('trusted_domains', \OC_Config::getValue('trusted_domains', array(\OC_Request::serverHost()))); 
  		}
  		/*
  		 * STOP CONFIG CHANGES FOR OLDER VERSIONS
  		 */
03e52840d   Kload   Init
120
121
  		try {
  			\OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
31b7f2792   Kload   Upgrade to ownclo...
122
  			$this->emit('\OC\Updater', 'dbUpgrade');
03e52840d   Kload   Init
123
124
125
126
127
  
  			// do a file cache upgrade for users with files
  			// this can take loooooooooooooooooooooooong
  			$this->upgradeFileCache();
  		} catch (\Exception $exception) {
31b7f2792   Kload   Upgrade to ownclo...
128
  			$this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
03e52840d   Kload   Init
129
130
131
132
133
  		}
  		\OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
  		\OC_App::checkAppsRequirements();
  		// load all apps to also upgrade enabled apps
  		\OC_App::loadApps();
31b7f2792   Kload   Upgrade to ownclo...
134
135
136
  
  		$repair = new Repair();
  		$repair->run();
837968727   Kload   [enh] Upgrade to ...
137
138
  		//Invalidate update feed
  		\OC_Appconfig::setValue('core', 'lastupdatedat', 0);
03e52840d   Kload   Init
139
  		\OC_Config::setValue('maintenance', false);
31b7f2792   Kload   Upgrade to ownclo...
140
  		$this->emit('\OC\Updater', 'maintenanceEnd');
03e52840d   Kload   Init
141
142
143
144
145
146
147
148
149
  	}
  
  	private function upgradeFileCache() {
  		try {
  			$query = \OC_DB::prepare('
  				SELECT DISTINCT `user`
  				FROM `*PREFIX*fscache`
  			');
  			$result = $query->execute();
03e52840d   Kload   Init
150
  		} catch (\Exception $e) {
03e52840d   Kload   Init
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  			return;
  		}
  		$users = $result->fetchAll();
  		if (count($users) == 0) {
  			return;
  		}
  		$step = 100 / count($users);
  		$percentCompleted = 0;
  		$lastPercentCompletedOutput = 0;
  		$startInfoShown = false;
  		foreach ($users as $userRow) {
  			$user = $userRow['user'];
  			\OC\Files\Filesystem::initMountPoints($user);
  			\OC\Files\Cache\Upgrade::doSilentUpgrade($user);
  			if (!$startInfoShown) {
  				//We show it only now, because otherwise Info about upgraded apps
  				//will appear between this and progress info
31b7f2792   Kload   Upgrade to ownclo...
168
  				$this->emit('\OC\Updater', 'filecacheStart');
03e52840d   Kload   Init
169
170
171
172
173
  				$startInfoShown = true;
  			}
  			$percentCompleted += $step;
  			$out = floor($percentCompleted);
  			if ($out != $lastPercentCompletedOutput) {
31b7f2792   Kload   Upgrade to ownclo...
174
  				$this->emit('\OC\Updater', 'filecacheProgress', array($out));
03e52840d   Kload   Init
175
176
177
  				$lastPercentCompletedOutput = $out;
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
178
  		$this->emit('\OC\Updater', 'filecacheDone');
03e52840d   Kload   Init
179
  	}
31b7f2792   Kload   Upgrade to ownclo...
180
  }
a293d369c   Kload   Update sources to...
181