updater.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Frank Karlitschek <frank@owncloud.org>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC;
  32. use OC\Hooks\BasicEmitter;
  33. use OC_App;
  34. use OC_Installer;
  35. use OC_Util;
  36. use OCP\IConfig;
  37. use OC\Setup;
  38. use OCP\ILogger;
  39. /**
  40. * Class that handles autoupdating of ownCloud
  41. *
  42. * Hooks provided in scope \OC\Updater
  43. * - maintenanceStart()
  44. * - maintenanceEnd()
  45. * - dbUpgrade()
  46. * - failure(string $message)
  47. */
  48. class Updater extends BasicEmitter {
  49. /** @var ILogger $log */
  50. private $log;
  51. /** @var \OC\HTTPHelper $helper */
  52. private $httpHelper;
  53. /** @var IConfig */
  54. private $config;
  55. /** @var bool */
  56. private $simulateStepEnabled;
  57. /** @var bool */
  58. private $updateStepEnabled;
  59. /**
  60. * @param HTTPHelper $httpHelper
  61. * @param IConfig $config
  62. * @param ILogger $log
  63. */
  64. public function __construct(HTTPHelper $httpHelper, IConfig $config, ILogger $log = null) {
  65. $this->httpHelper = $httpHelper;
  66. $this->log = $log;
  67. $this->config = $config;
  68. $this->simulateStepEnabled = true;
  69. $this->updateStepEnabled = true;
  70. }
  71. /**
  72. * Sets whether the database migration simulation must
  73. * be enabled.
  74. * This can be set to false to skip this test.
  75. *
  76. * @param bool $flag true to enable simulation, false otherwise
  77. */
  78. public function setSimulateStepEnabled($flag) {
  79. $this->simulateStepEnabled = $flag;
  80. }
  81. /**
  82. * Sets whether the update must be performed.
  83. * This can be set to false to skip the actual update.
  84. *
  85. * @param bool $flag true to enable update, false otherwise
  86. */
  87. public function setUpdateStepEnabled($flag) {
  88. $this->updateStepEnabled = $flag;
  89. }
  90. /**
  91. * Check if a new version is available
  92. *
  93. * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
  94. * @return array|bool
  95. */
  96. public function check($updaterUrl = null) {
  97. // Look up the cache - it is invalidated all 30 minutes
  98. if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
  99. return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
  100. }
  101. if (is_null($updaterUrl)) {
  102. $updaterUrl = 'https://apps.owncloud.com/updater.php';
  103. }
  104. $this->config->setAppValue('core', 'lastupdatedat', time());
  105. if ($this->config->getAppValue('core', 'installedat', '') == '') {
  106. $this->config->setAppValue('core', 'installedat', microtime(true));
  107. }
  108. $version = \OC_Util::getVersion();
  109. $version['installed'] = $this->config->getAppValue('core', 'installedat');
  110. $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat');
  111. $version['updatechannel'] = \OC_Util::getChannel();
  112. $version['edition'] = \OC_Util::getEditionString();
  113. $version['build'] = \OC_Util::getBuild();
  114. $versionString = implode('x', $version);
  115. //fetch xml data from updater
  116. $url = $updaterUrl . '?version=' . $versionString;
  117. // set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  118. $tmp = array();
  119. $xml = $this->httpHelper->getUrlContent($url);
  120. if ($xml) {
  121. $loadEntities = libxml_disable_entity_loader(true);
  122. $data = @simplexml_load_string($xml);
  123. libxml_disable_entity_loader($loadEntities);
  124. if ($data !== false) {
  125. $tmp['version'] = $data->version;
  126. $tmp['versionstring'] = $data->versionstring;
  127. $tmp['url'] = $data->url;
  128. $tmp['web'] = $data->web;
  129. }
  130. } else {
  131. $data = array();
  132. }
  133. // Cache the result
  134. $this->config->setAppValue('core', 'lastupdateResult', json_encode($data));
  135. return $tmp;
  136. }
  137. /**
  138. * runs the update actions in maintenance mode, does not upgrade the source files
  139. * except the main .htaccess file
  140. *
  141. * @return bool true if the operation succeeded, false otherwise
  142. */
  143. public function upgrade() {
  144. $this->config->setSystemValue('maintenance', true);
  145. $installedVersion = $this->config->getSystemValue('version', '0.0.0');
  146. $currentVersion = implode('.', \OC_Util::getVersion());
  147. if ($this->log) {
  148. $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  149. }
  150. $this->emit('\OC\Updater', 'maintenanceStart');
  151. try {
  152. $this->doUpgrade($currentVersion, $installedVersion);
  153. } catch (\Exception $exception) {
  154. $this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
  155. }
  156. $this->config->setSystemValue('maintenance', false);
  157. $this->emit('\OC\Updater', 'maintenanceEnd');
  158. }
  159. /**
  160. * Whether an upgrade to a specified version is possible
  161. * @param string $oldVersion
  162. * @param string $newVersion
  163. * @return bool
  164. */
  165. public function isUpgradePossible($oldVersion, $newVersion) {
  166. $oldVersion = explode('.', $oldVersion);
  167. $newVersion = explode('.', $newVersion);
  168. if($newVersion[0] > ($oldVersion[0] + 1) || $oldVersion[0] > $newVersion[0]) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Forward messages emitted by the repair routine
  175. *
  176. * @param Repair $repair repair routine
  177. */
  178. private function emitRepairMessages(Repair $repair) {
  179. $repair->listen('\OC\Repair', 'warning', function ($description) {
  180. $this->emit('\OC\Updater', 'repairWarning', array($description));
  181. });
  182. $repair->listen('\OC\Repair', 'error', function ($description) {
  183. $this->emit('\OC\Updater', 'repairError', array($description));
  184. });
  185. }
  186. /**
  187. * runs the update actions in maintenance mode, does not upgrade the source files
  188. * except the main .htaccess file
  189. *
  190. * @param string $currentVersion current version to upgrade to
  191. * @param string $installedVersion previous version from which to upgrade from
  192. *
  193. * @throws \Exception
  194. * @return bool true if the operation succeeded, false otherwise
  195. */
  196. private function doUpgrade($currentVersion, $installedVersion) {
  197. // Stop update if the update is over several major versions
  198. if (!self::isUpgradePossible($installedVersion, $currentVersion)) {
  199. throw new \Exception('Updates between multiple major versions are unsupported.');
  200. }
  201. // Update .htaccess files
  202. try {
  203. Setup::updateHtaccess();
  204. Setup::protectDataDirectory();
  205. } catch (\Exception $e) {
  206. throw new \Exception($e->getMessage());
  207. }
  208. // create empty file in data dir, so we can later find
  209. // out that this is indeed an ownCloud data directory
  210. // (in case it didn't exist before)
  211. file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
  212. // pre-upgrade repairs
  213. $repair = new Repair(Repair::getBeforeUpgradeRepairSteps());
  214. $this->emitRepairMessages($repair);
  215. $repair->run();
  216. // simulate DB upgrade
  217. if ($this->simulateStepEnabled) {
  218. $this->checkCoreUpgrade();
  219. // simulate apps DB upgrade
  220. $this->checkAppUpgrade($currentVersion);
  221. }
  222. if ($this->updateStepEnabled) {
  223. $this->doCoreUpgrade();
  224. // update all shipped apps
  225. $disabledApps = $this->checkAppsRequirements();
  226. $this->doAppUpgrade();
  227. // upgrade appstore apps
  228. $this->upgradeAppStoreApps($disabledApps);
  229. // post-upgrade repairs
  230. $repair = new Repair(Repair::getRepairSteps());
  231. $this->emitRepairMessages($repair);
  232. $repair->run();
  233. //Invalidate update feed
  234. $this->config->setAppValue('core', 'lastupdatedat', 0);
  235. // only set the final version if everything went well
  236. $this->config->setSystemValue('version', implode('.', \OC_Util::getVersion()));
  237. }
  238. }
  239. protected function checkCoreUpgrade() {
  240. // simulate core DB upgrade
  241. \OC_DB::simulateUpdateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  242. $this->emit('\OC\Updater', 'dbSimulateUpgrade');
  243. }
  244. protected function doCoreUpgrade() {
  245. // do the real upgrade
  246. \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  247. $this->emit('\OC\Updater', 'dbUpgrade');
  248. }
  249. /**
  250. * @param string $version the oc version to check app compatibility with
  251. */
  252. protected function checkAppUpgrade($version) {
  253. $apps = \OC_App::getEnabledApps();
  254. foreach ($apps as $appId) {
  255. $info = \OC_App::getAppInfo($appId);
  256. $compatible = \OC_App::isAppCompatible($version, $info);
  257. $isShipped = \OC_App::isShipped($appId);
  258. if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) {
  259. /**
  260. * FIXME: The preupdate check is performed before the database migration, otherwise database changes
  261. * are not possible anymore within it. - Consider this when touching the code.
  262. * @link https://github.com/owncloud/core/issues/10980
  263. * @see \OC_App::updateApp
  264. */
  265. if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
  266. $this->includePreUpdate($appId);
  267. }
  268. if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
  269. \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
  270. }
  271. }
  272. }
  273. $this->emit('\OC\Updater', 'appUpgradeCheck');
  274. }
  275. /**
  276. * Includes the pre-update file. Done here to prevent namespace mixups.
  277. * @param string $appId
  278. */
  279. private function includePreUpdate($appId) {
  280. include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
  281. }
  282. /**
  283. * upgrades all apps within a major ownCloud upgrade. Also loads "priority"
  284. * (types authentication, filesystem, logging, in that order) afterwards.
  285. *
  286. * @throws NeedsUpdateException
  287. */
  288. protected function doAppUpgrade() {
  289. $apps = \OC_App::getEnabledApps();
  290. $priorityTypes = array('authentication', 'filesystem', 'logging');
  291. $pseudoOtherType = 'other';
  292. $stacks = array($pseudoOtherType => array());
  293. foreach ($apps as $appId) {
  294. $priorityType = false;
  295. foreach ($priorityTypes as $type) {
  296. if(!isset($stacks[$type])) {
  297. $stacks[$type] = array();
  298. }
  299. if (\OC_App::isType($appId, $type)) {
  300. $stacks[$type][] = $appId;
  301. $priorityType = true;
  302. break;
  303. }
  304. }
  305. if (!$priorityType) {
  306. $stacks[$pseudoOtherType][] = $appId;
  307. }
  308. }
  309. foreach ($stacks as $type => $stack) {
  310. foreach ($stack as $appId) {
  311. if (\OC_App::shouldUpgrade($appId)) {
  312. \OC_App::updateApp($appId);
  313. $this->emit('\OC\Updater', 'appUpgrade', array($appId, \OC_App::getAppVersion($appId)));
  314. }
  315. if($type !== $pseudoOtherType) {
  316. // load authentication, filesystem and logging apps after
  317. // upgrading them. Other apps my need to rely on modifying
  318. // user and/or filesystem aspects.
  319. \OC_App::loadApp($appId, false);
  320. }
  321. }
  322. }
  323. }
  324. /**
  325. * check if the current enabled apps are compatible with the current
  326. * ownCloud version. disable them if not.
  327. * This is important if you upgrade ownCloud and have non ported 3rd
  328. * party apps installed.
  329. */
  330. private function checkAppsRequirements() {
  331. $isCoreUpgrade = $this->isCodeUpgrade();
  332. $apps = OC_App::getEnabledApps();
  333. $version = OC_Util::getVersion();
  334. $disabledApps = [];
  335. foreach ($apps as $app) {
  336. // check if the app is compatible with this version of ownCloud
  337. $info = OC_App::getAppInfo($app);
  338. if(!OC_App::isAppCompatible($version, $info)) {
  339. OC_App::disable($app);
  340. $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
  341. }
  342. // no need to disable any app in case this is a non-core upgrade
  343. if (!$isCoreUpgrade) {
  344. continue;
  345. }
  346. // shipped apps will remain enabled
  347. if (OC_App::isShipped($app)) {
  348. continue;
  349. }
  350. // authentication and session apps will remain enabled as well
  351. if (OC_App::isType($app, ['session', 'authentication'])) {
  352. continue;
  353. }
  354. // disable any other 3rd party apps
  355. \OC_App::disable($app);
  356. $disabledApps[]= $app;
  357. $this->emit('\OC\Updater', 'thirdPartyAppDisabled', array($app));
  358. }
  359. return $disabledApps;
  360. }
  361. private function isCodeUpgrade() {
  362. $installedVersion = $this->config->getSystemValue('version', '0.0.0');
  363. $currentVersion = implode('.', OC_Util::getVersion());
  364. if (version_compare($currentVersion, $installedVersion, '>')) {
  365. return true;
  366. }
  367. return false;
  368. }
  369. private function upgradeAppStoreApps($disabledApps) {
  370. foreach($disabledApps as $app) {
  371. if (OC_Installer::isUpdateAvailable($app)) {
  372. $ocsId = \OC::$server->getConfig()->getAppValue($app, 'ocsid', '');
  373. $this->emit('\OC\Updater', 'upgradeAppStoreApp', array($app));
  374. OC_Installer::updateAppByOCSId($ocsId);
  375. }
  376. }
  377. }
  378. }