AppManager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Schaefer "christophł@wolkesicher.de"
  8. * @author Christoph Wurst <christoph@owncloud.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Haertl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\App;
  33. use OC\AppConfig;
  34. use OCP\App\AppPathNotFoundException;
  35. use OCP\App\IAppManager;
  36. use OCP\App\ManagerEvent;
  37. use OCP\ICacheFactory;
  38. use OCP\IGroupManager;
  39. use OCP\IUser;
  40. use OCP\IUserSession;
  41. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  42. class AppManager implements IAppManager {
  43. /**
  44. * Apps with these types can not be enabled for certain groups only
  45. * @var string[]
  46. */
  47. protected $protectedAppTypes = [
  48. 'filesystem',
  49. 'prelogin',
  50. 'authentication',
  51. 'logging',
  52. 'prevent_group_restriction',
  53. ];
  54. /** @var IUserSession */
  55. private $userSession;
  56. /** @var AppConfig */
  57. private $appConfig;
  58. /** @var IGroupManager */
  59. private $groupManager;
  60. /** @var ICacheFactory */
  61. private $memCacheFactory;
  62. /** @var EventDispatcherInterface */
  63. private $dispatcher;
  64. /** @var string[] $appId => $enabled */
  65. private $installedAppsCache;
  66. /** @var string[] */
  67. private $shippedApps;
  68. /** @var string[] */
  69. private $alwaysEnabled;
  70. /** @var array */
  71. private $appInfos = [];
  72. /** @var array */
  73. private $appVersions = [];
  74. /**
  75. * @param IUserSession $userSession
  76. * @param AppConfig $appConfig
  77. * @param IGroupManager $groupManager
  78. * @param ICacheFactory $memCacheFactory
  79. * @param EventDispatcherInterface $dispatcher
  80. */
  81. public function __construct(IUserSession $userSession,
  82. AppConfig $appConfig,
  83. IGroupManager $groupManager,
  84. ICacheFactory $memCacheFactory,
  85. EventDispatcherInterface $dispatcher) {
  86. $this->userSession = $userSession;
  87. $this->appConfig = $appConfig;
  88. $this->groupManager = $groupManager;
  89. $this->memCacheFactory = $memCacheFactory;
  90. $this->dispatcher = $dispatcher;
  91. }
  92. /**
  93. * @return string[] $appId => $enabled
  94. */
  95. private function getInstalledAppsValues() {
  96. if (!$this->installedAppsCache) {
  97. $values = $this->appConfig->getValues(false, 'enabled');
  98. $alwaysEnabledApps = $this->getAlwaysEnabledApps();
  99. foreach($alwaysEnabledApps as $appId) {
  100. $values[$appId] = 'yes';
  101. }
  102. $this->installedAppsCache = array_filter($values, function ($value) {
  103. return $value !== 'no';
  104. });
  105. ksort($this->installedAppsCache);
  106. }
  107. return $this->installedAppsCache;
  108. }
  109. /**
  110. * List all installed apps
  111. *
  112. * @return string[]
  113. */
  114. public function getInstalledApps() {
  115. return array_keys($this->getInstalledAppsValues());
  116. }
  117. /**
  118. * List all apps enabled for a user
  119. *
  120. * @param \OCP\IUser $user
  121. * @return string[]
  122. */
  123. public function getEnabledAppsForUser(IUser $user) {
  124. $apps = $this->getInstalledAppsValues();
  125. $appsForUser = array_filter($apps, function ($enabled) use ($user) {
  126. return $this->checkAppForUser($enabled, $user);
  127. });
  128. return array_keys($appsForUser);
  129. }
  130. /**
  131. * Check if an app is enabled for user
  132. *
  133. * @param string $appId
  134. * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
  135. * @return bool
  136. */
  137. public function isEnabledForUser($appId, $user = null) {
  138. if ($this->isAlwaysEnabled($appId)) {
  139. return true;
  140. }
  141. if ($user === null) {
  142. $user = $this->userSession->getUser();
  143. }
  144. $installedApps = $this->getInstalledAppsValues();
  145. if (isset($installedApps[$appId])) {
  146. return $this->checkAppForUser($installedApps[$appId], $user);
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * @param string $enabled
  153. * @param IUser $user
  154. * @return bool
  155. */
  156. private function checkAppForUser($enabled, $user) {
  157. if ($enabled === 'yes') {
  158. return true;
  159. } elseif ($user === null) {
  160. return false;
  161. } else {
  162. if(empty($enabled)){
  163. return false;
  164. }
  165. $groupIds = json_decode($enabled);
  166. if (!is_array($groupIds)) {
  167. $jsonError = json_last_error();
  168. \OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
  169. return false;
  170. }
  171. $userGroups = $this->groupManager->getUserGroupIds($user);
  172. foreach ($userGroups as $groupId) {
  173. if (in_array($groupId, $groupIds, true)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. }
  180. /**
  181. * Check if an app is enabled in the instance
  182. *
  183. * Notice: This actually checks if the app is enabled and not only if it is installed.
  184. *
  185. * @param string $appId
  186. * @return bool
  187. */
  188. public function isInstalled($appId) {
  189. $installedApps = $this->getInstalledAppsValues();
  190. return isset($installedApps[$appId]);
  191. }
  192. /**
  193. * Enable an app for every user
  194. *
  195. * @param string $appId
  196. * @throws AppPathNotFoundException
  197. */
  198. public function enableApp($appId) {
  199. // Check if app exists
  200. $this->getAppPath($appId);
  201. $this->installedAppsCache[$appId] = 'yes';
  202. $this->appConfig->setValue($appId, 'enabled', 'yes');
  203. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
  204. ManagerEvent::EVENT_APP_ENABLE, $appId
  205. ));
  206. $this->clearAppsCache();
  207. }
  208. /**
  209. * Whether a list of types contains a protected app type
  210. *
  211. * @param string[] $types
  212. * @return bool
  213. */
  214. public function hasProtectedAppType($types) {
  215. if (empty($types)) {
  216. return false;
  217. }
  218. $protectedTypes = array_intersect($this->protectedAppTypes, $types);
  219. return !empty($protectedTypes);
  220. }
  221. /**
  222. * Enable an app only for specific groups
  223. *
  224. * @param string $appId
  225. * @param \OCP\IGroup[] $groups
  226. * @throws \Exception if app can't be enabled for groups
  227. */
  228. public function enableAppForGroups($appId, $groups) {
  229. $info = $this->getAppInfo($appId);
  230. if (!empty($info['types'])) {
  231. $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
  232. if (!empty($protectedTypes)) {
  233. throw new \Exception("$appId can't be enabled for groups.");
  234. }
  235. }
  236. $groupIds = array_map(function ($group) {
  237. /** @var \OCP\IGroup $group */
  238. return $group->getGID();
  239. }, $groups);
  240. $this->installedAppsCache[$appId] = json_encode($groupIds);
  241. $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
  242. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
  243. ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
  244. ));
  245. $this->clearAppsCache();
  246. }
  247. /**
  248. * Disable an app for every user
  249. *
  250. * @param string $appId
  251. * @throws \Exception if app can't be disabled
  252. */
  253. public function disableApp($appId) {
  254. if ($this->isAlwaysEnabled($appId)) {
  255. throw new \Exception("$appId can't be disabled.");
  256. }
  257. unset($this->installedAppsCache[$appId]);
  258. $this->appConfig->setValue($appId, 'enabled', 'no');
  259. // run uninstall steps
  260. $appData = $this->getAppInfo($appId);
  261. if (!is_null($appData)) {
  262. \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
  263. }
  264. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
  265. ManagerEvent::EVENT_APP_DISABLE, $appId
  266. ));
  267. $this->clearAppsCache();
  268. }
  269. /**
  270. * Get the directory for the given app.
  271. *
  272. * @param string $appId
  273. * @return string
  274. * @throws AppPathNotFoundException if app folder can't be found
  275. */
  276. public function getAppPath($appId) {
  277. $appPath = \OC_App::getAppPath($appId);
  278. if($appPath === false) {
  279. throw new AppPathNotFoundException('Could not find path for ' . $appId);
  280. }
  281. return $appPath;
  282. }
  283. /**
  284. * Clear the cached list of apps when enabling/disabling an app
  285. */
  286. public function clearAppsCache() {
  287. $settingsMemCache = $this->memCacheFactory->createDistributed('settings');
  288. $settingsMemCache->clear('listApps');
  289. $this->appInfos = [];
  290. }
  291. /**
  292. * Returns a list of apps that need upgrade
  293. *
  294. * @param string $version Nextcloud version as array of version components
  295. * @return array list of app info from apps that need an upgrade
  296. *
  297. * @internal
  298. */
  299. public function getAppsNeedingUpgrade($version) {
  300. $appsToUpgrade = [];
  301. $apps = $this->getInstalledApps();
  302. foreach ($apps as $appId) {
  303. $appInfo = $this->getAppInfo($appId);
  304. $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
  305. if ($appDbVersion
  306. && isset($appInfo['version'])
  307. && version_compare($appInfo['version'], $appDbVersion, '>')
  308. && \OC_App::isAppCompatible($version, $appInfo)
  309. ) {
  310. $appsToUpgrade[] = $appInfo;
  311. }
  312. }
  313. return $appsToUpgrade;
  314. }
  315. /**
  316. * Returns the app information from "appinfo/info.xml".
  317. *
  318. * @param string $appId app id
  319. *
  320. * @param bool $path
  321. * @param null $lang
  322. * @return array|null app info
  323. */
  324. public function getAppInfo(string $appId, bool $path = false, $lang = null) {
  325. if ($path) {
  326. $file = $appId;
  327. } else {
  328. if ($lang === null && isset($this->appInfos[$appId])) {
  329. return $this->appInfos[$appId];
  330. }
  331. try {
  332. $appPath = $this->getAppPath($appId);
  333. } catch (AppPathNotFoundException $e) {
  334. return null;
  335. }
  336. $file = $appPath . '/appinfo/info.xml';
  337. }
  338. $parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
  339. $data = $parser->parse($file);
  340. if (is_array($data)) {
  341. $data = \OC_App::parseAppInfo($data, $lang);
  342. }
  343. if ($lang === null) {
  344. $this->appInfos[$appId] = $data;
  345. }
  346. return $data;
  347. }
  348. public function getAppVersion(string $appId, bool $useCache = true): string {
  349. if(!$useCache || !isset($this->appVersions[$appId])) {
  350. $appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
  351. $this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
  352. }
  353. return $this->appVersions[$appId];
  354. }
  355. /**
  356. * Returns a list of apps incompatible with the given version
  357. *
  358. * @param string $version Nextcloud version as array of version components
  359. *
  360. * @return array list of app info from incompatible apps
  361. *
  362. * @internal
  363. */
  364. public function getIncompatibleApps(string $version): array {
  365. $apps = $this->getInstalledApps();
  366. $incompatibleApps = array();
  367. foreach ($apps as $appId) {
  368. $info = $this->getAppInfo($appId);
  369. if ($info === null) {
  370. $incompatibleApps[] = ['id' => $appId];
  371. } else if (!\OC_App::isAppCompatible($version, $info)) {
  372. $incompatibleApps[] = $info;
  373. }
  374. }
  375. return $incompatibleApps;
  376. }
  377. /**
  378. * @inheritdoc
  379. * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped()
  380. */
  381. public function isShipped($appId) {
  382. $this->loadShippedJson();
  383. return in_array($appId, $this->shippedApps, true);
  384. }
  385. private function isAlwaysEnabled($appId) {
  386. $alwaysEnabled = $this->getAlwaysEnabledApps();
  387. return in_array($appId, $alwaysEnabled, true);
  388. }
  389. /**
  390. * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson()
  391. * @throws \Exception
  392. */
  393. private function loadShippedJson() {
  394. if ($this->shippedApps === null) {
  395. $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
  396. if (!file_exists($shippedJson)) {
  397. throw new \Exception("File not found: $shippedJson");
  398. }
  399. $content = json_decode(file_get_contents($shippedJson), true);
  400. $this->shippedApps = $content['shippedApps'];
  401. $this->alwaysEnabled = $content['alwaysEnabled'];
  402. }
  403. }
  404. /**
  405. * @inheritdoc
  406. */
  407. public function getAlwaysEnabledApps() {
  408. $this->loadShippedJson();
  409. return $this->alwaysEnabled;
  410. }
  411. }