You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updater.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. use OC\Hooks\BasicEmitter;
  10. /**
  11. * Class that handles autoupdating of ownCloud
  12. *
  13. * Hooks provided in scope \OC\Updater
  14. * - maintenanceStart()
  15. * - maintenanceEnd()
  16. * - dbUpgrade()
  17. * - failure(string $message)
  18. */
  19. class Updater extends BasicEmitter {
  20. /**
  21. * @var \OC\Log $log
  22. */
  23. private $log;
  24. /**
  25. * @var \OC\HTTPHelper $helper;
  26. */
  27. private $httpHelper;
  28. /**
  29. * @var \OCP\IAppConfig;
  30. */
  31. private $config;
  32. private $simulateStepEnabled;
  33. private $updateStepEnabled;
  34. /**
  35. * @param \OC\Log $log
  36. */
  37. public function __construct($httpHelper, $config, $log = null) {
  38. $this->httpHelper = $httpHelper;
  39. $this->log = $log;
  40. $this->config = $config;
  41. $this->simulateStepEnabled = true;
  42. $this->updateStepEnabled = true;
  43. }
  44. /**
  45. * Sets whether the database migration simulation must
  46. * be enabled.
  47. * This can be set to false to skip this test.
  48. *
  49. * @param bool $flag true to enable simulation, false otherwise
  50. */
  51. public function setSimulateStepEnabled($flag) {
  52. $this->simulateStepEnabled = $flag;
  53. }
  54. /**
  55. * Sets whether the update must be performed.
  56. * This can be set to false to skip the actual update.
  57. *
  58. * @param bool $flag true to enable update, false otherwise
  59. */
  60. public function setUpdateStepEnabled($flag) {
  61. $this->updateStepEnabled = $flag;
  62. }
  63. /**
  64. * Check if a new version is available
  65. *
  66. * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
  67. * @return array|bool
  68. */
  69. public function check($updaterUrl = null) {
  70. // Look up the cache - it is invalidated all 30 minutes
  71. if (($this->config->getValue('core', 'lastupdatedat') + 1800) > time()) {
  72. return json_decode($this->config->getValue('core', 'lastupdateResult'), true);
  73. }
  74. if (is_null($updaterUrl)) {
  75. $updaterUrl = 'https://apps.owncloud.com/updater.php';
  76. }
  77. $this->config->setValue('core', 'lastupdatedat', time());
  78. if ($this->config->getValue('core', 'installedat', '') == '') {
  79. $this->config->setValue('core', 'installedat', microtime(true));
  80. }
  81. $version = \OC_Util::getVersion();
  82. $version['installed'] = $this->config->getValue('core', 'installedat');
  83. $version['updated'] = $this->config->getValue('core', 'lastupdatedat');
  84. $version['updatechannel'] = \OC_Util::getChannel();
  85. $version['edition'] = \OC_Util::getEditionString();
  86. $version['build'] = \OC_Util::getBuild();
  87. $versionString = implode('x', $version);
  88. //fetch xml data from updater
  89. $url = $updaterUrl . '?version=' . $versionString;
  90. // set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  91. $tmp = array();
  92. $xml = $this->httpHelper->getUrlContent($url);
  93. if ($xml) {
  94. $loadEntities = libxml_disable_entity_loader(true);
  95. $data = @simplexml_load_string($xml);
  96. libxml_disable_entity_loader($loadEntities);
  97. if ($data !== false) {
  98. $tmp['version'] = $data->version;
  99. $tmp['versionstring'] = $data->versionstring;
  100. $tmp['url'] = $data->url;
  101. $tmp['web'] = $data->web;
  102. }
  103. } else {
  104. $data = array();
  105. }
  106. // Cache the result
  107. $this->config->setValue('core', 'lastupdateResult', json_encode($data));
  108. return $tmp;
  109. }
  110. /**
  111. * runs the update actions in maintenance mode, does not upgrade the source files
  112. * except the main .htaccess file
  113. *
  114. * @return bool true if the operation succeeded, false otherwise
  115. */
  116. public function upgrade() {
  117. \OC_Config::setValue('maintenance', true);
  118. $installedVersion = \OC_Config::getValue('version', '0.0.0');
  119. $currentVersion = implode('.', \OC_Util::getVersion());
  120. if ($this->log) {
  121. $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  122. }
  123. $this->emit('\OC\Updater', 'maintenanceStart');
  124. try {
  125. $this->doUpgrade($currentVersion, $installedVersion);
  126. } catch (\Exception $exception) {
  127. $this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
  128. }
  129. \OC_Config::setValue('maintenance', false);
  130. $this->emit('\OC\Updater', 'maintenanceEnd');
  131. }
  132. /**
  133. * Whether an upgrade to a specified version is possible
  134. * @param string $oldVersion
  135. * @param string $newVersion
  136. * @return bool
  137. */
  138. public function isUpgradePossible($oldVersion, $newVersion) {
  139. $oldVersion = explode('.', $oldVersion);
  140. $newVersion = explode('.', $newVersion);
  141. if($newVersion[0] > ($oldVersion[0] + 1) || $oldVersion[0] > $newVersion[0]) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. /**
  147. * runs the update actions in maintenance mode, does not upgrade the source files
  148. * except the main .htaccess file
  149. *
  150. * @param string $currentVersion current version to upgrade to
  151. * @param string $installedVersion previous version from which to upgrade from
  152. *
  153. * @throws \Exception
  154. * @return bool true if the operation succeeded, false otherwise
  155. */
  156. private function doUpgrade($currentVersion, $installedVersion) {
  157. // Stop update if the update is over several major versions
  158. if (!self::isUpgradePossible($installedVersion, $currentVersion)) {
  159. throw new \Exception('Updates between multiple major versions are unsupported.');
  160. }
  161. // Update htaccess files for apache hosts
  162. if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  163. \OC_Setup::updateHtaccess();
  164. }
  165. // create empty file in data dir, so we can later find
  166. // out that this is indeed an ownCloud data directory
  167. // (in case it didn't exist before)
  168. file_put_contents(\OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
  169. // pre-upgrade repairs
  170. $repair = new \OC\Repair(\OC\Repair::getBeforeUpgradeRepairSteps());
  171. $repair->run();
  172. // simulate DB upgrade
  173. if ($this->simulateStepEnabled) {
  174. $this->checkCoreUpgrade();
  175. // simulate apps DB upgrade
  176. $this->checkAppUpgrade($currentVersion);
  177. }
  178. if ($this->updateStepEnabled) {
  179. $this->doCoreUpgrade();
  180. $disabledApps = \OC_App::checkAppsRequirements();
  181. if (!empty($disabledApps)) {
  182. $this->emit('\OC\Updater', 'disabledApps', array($disabledApps));
  183. }
  184. $this->doAppUpgrade();
  185. // post-upgrade repairs
  186. $repair = new \OC\Repair(\OC\Repair::getRepairSteps());
  187. $repair->run();
  188. //Invalidate update feed
  189. $this->config->setValue('core', 'lastupdatedat', 0);
  190. // only set the final version if everything went well
  191. \OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
  192. }
  193. }
  194. protected function checkCoreUpgrade() {
  195. // simulate core DB upgrade
  196. \OC_DB::simulateUpdateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  197. $this->emit('\OC\Updater', 'dbSimulateUpgrade');
  198. }
  199. protected function doCoreUpgrade() {
  200. // do the real upgrade
  201. \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  202. $this->emit('\OC\Updater', 'dbUpgrade');
  203. }
  204. /**
  205. * @param string $version the oc version to check app compatibility with
  206. */
  207. protected function checkAppUpgrade($version) {
  208. $apps = \OC_App::getEnabledApps();
  209. foreach ($apps as $appId) {
  210. if ($version) {
  211. $info = \OC_App::getAppInfo($appId);
  212. $compatible = \OC_App::isAppCompatible($version, $info);
  213. } else {
  214. $compatible = true;
  215. }
  216. if ($compatible && \OC_App::shouldUpgrade($appId)) {
  217. /**
  218. * FIXME: The preupdate check is performed before the database migration, otherwise database changes
  219. * are not possible anymore within it. - Consider this when touching the code.
  220. * @link https://github.com/owncloud/core/issues/10980
  221. * @see \OC_App::updateApp
  222. */
  223. if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
  224. $this->includePreUpdate($appId);
  225. }
  226. if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
  227. \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
  228. }
  229. }
  230. }
  231. $this->emit('\OC\Updater', 'appUpgradeCheck');
  232. }
  233. /**
  234. * Includes the pre-update file. Done here to prevent namespace mixups.
  235. * @param string $appId
  236. */
  237. private function includePreUpdate($appId) {
  238. include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
  239. }
  240. protected function doAppUpgrade() {
  241. $apps = \OC_App::getEnabledApps();
  242. foreach ($apps as $appId) {
  243. if (\OC_App::shouldUpgrade($appId)) {
  244. \OC_App::updateApp($appId);
  245. $this->emit('\OC\Updater', 'appUpgrade', array($appId, \OC_App::getAppVersion($appId)));
  246. }
  247. }
  248. }
  249. }