app.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  9. * @author Björn Schießle <bjoern@schiessle.org>
  10. * @author Borjan Tchakaloff <borjan@tchakaloff.fr>
  11. * @author Brice Maron <brice@bmaron.net>
  12. * @author Christopher Schäpers <kondou@ts.unde.re>
  13. * @author Felix Moeller <mail@felixmoeller.de>
  14. * @author Frank Karlitschek <frank@karlitschek.de>
  15. * @author Georg Ehrke <georg@owncloud.com>
  16. * @author Jakob Sack <mail@jakobsack.de>
  17. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  18. * @author Joas Schilling <coding@schilljs.com>
  19. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  20. * @author Kamil Domanski <kdomanski@kdemail.net>
  21. * @author Klaas Freitag <freitag@owncloud.com>
  22. * @author Lukas Reschke <lukas@statuscode.ch>
  23. * @author Markus Goetz <markus@woboq.com>
  24. * @author Morris Jobke <hey@morrisjobke.de>
  25. * @author RealRancor <Fisch.666@gmx.de>
  26. * @author Robin Appelman <robin@icewind.nl>
  27. * @author Robin McCorkell <robin@mccorkell.me.uk>
  28. * @author Roeland Jago Douma <roeland@famdouma.nl>
  29. * @author Sam Tuke <mail@samtuke.com>
  30. * @author Thomas Müller <thomas.mueller@tmit.eu>
  31. * @author Thomas Tanghus <thomas@tanghus.net>
  32. * @author Tom Needham <tom@owncloud.com>
  33. * @author Vincent Petry <pvince81@owncloud.com>
  34. *
  35. * @license AGPL-3.0
  36. *
  37. * This code is free software: you can redistribute it and/or modify
  38. * it under the terms of the GNU Affero General Public License, version 3,
  39. * as published by the Free Software Foundation.
  40. *
  41. * This program is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. * GNU Affero General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU Affero General Public License, version 3,
  47. * along with this program. If not, see <http://www.gnu.org/licenses/>
  48. *
  49. */
  50. use OC\App\DependencyAnalyzer;
  51. use OC\App\InfoParser;
  52. use OC\App\Platform;
  53. use OC\Installer;
  54. use OC\OCSClient;
  55. use OC\Repair;
  56. use OCP\App\ManagerEvent;
  57. /**
  58. * This class manages the apps. It allows them to register and integrate in the
  59. * ownCloud ecosystem. Furthermore, this class is responsible for installing,
  60. * upgrading and removing apps.
  61. */
  62. class OC_App {
  63. static private $appVersion = [];
  64. static private $adminForms = array();
  65. static private $personalForms = array();
  66. static private $appInfo = array();
  67. static private $appTypes = array();
  68. static private $loadedApps = array();
  69. static private $altLogin = array();
  70. static private $alreadyRegistered = [];
  71. const officialApp = 200;
  72. /**
  73. * clean the appId
  74. *
  75. * @param string|boolean $app AppId that needs to be cleaned
  76. * @return string
  77. */
  78. public static function cleanAppId($app) {
  79. return str_replace(array('\0', '/', '\\', '..'), '', $app);
  80. }
  81. /**
  82. * Check if an app is loaded
  83. *
  84. * @param string $app
  85. * @return bool
  86. */
  87. public static function isAppLoaded($app) {
  88. return in_array($app, self::$loadedApps, true);
  89. }
  90. /**
  91. * loads all apps
  92. *
  93. * @param string[] | string | null $types
  94. * @return bool
  95. *
  96. * This function walks through the ownCloud directory and loads all apps
  97. * it can find. A directory contains an app if the file /appinfo/info.xml
  98. * exists.
  99. *
  100. * if $types is set, only apps of those types will be loaded
  101. */
  102. public static function loadApps($types = null) {
  103. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  104. return false;
  105. }
  106. // Load the enabled apps here
  107. $apps = self::getEnabledApps();
  108. // Add each apps' folder as allowed class path
  109. foreach($apps as $app) {
  110. $path = self::getAppPath($app);
  111. if($path !== false) {
  112. self::registerAutoloading($app, $path);
  113. }
  114. }
  115. // prevent app.php from printing output
  116. ob_start();
  117. foreach ($apps as $app) {
  118. if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
  119. self::loadApp($app);
  120. }
  121. }
  122. ob_end_clean();
  123. return true;
  124. }
  125. /**
  126. * load a single app
  127. *
  128. * @param string $app
  129. */
  130. public static function loadApp($app) {
  131. self::$loadedApps[] = $app;
  132. $appPath = self::getAppPath($app);
  133. if($appPath === false) {
  134. return;
  135. }
  136. // in case someone calls loadApp() directly
  137. self::registerAutoloading($app, $appPath);
  138. if (is_file($appPath . '/appinfo/app.php')) {
  139. \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
  140. self::requireAppFile($app);
  141. if (self::isType($app, array('authentication'))) {
  142. // since authentication apps affect the "is app enabled for group" check,
  143. // the enabled apps cache needs to be cleared to make sure that the
  144. // next time getEnableApps() is called it will also include apps that were
  145. // enabled for groups
  146. self::$enabledAppsCache = array();
  147. }
  148. \OC::$server->getEventLogger()->end('load_app_' . $app);
  149. }
  150. $info = self::getAppInfo($app);
  151. if (!empty($info['activity']['filters'])) {
  152. foreach ($info['activity']['filters'] as $filter) {
  153. \OC::$server->getActivityManager()->registerFilter($filter);
  154. }
  155. }
  156. if (!empty($info['activity']['settings'])) {
  157. foreach ($info['activity']['settings'] as $setting) {
  158. \OC::$server->getActivityManager()->registerSetting($setting);
  159. }
  160. }
  161. if (!empty($info['activity']['providers'])) {
  162. foreach ($info['activity']['providers'] as $provider) {
  163. \OC::$server->getActivityManager()->registerProvider($provider);
  164. }
  165. }
  166. }
  167. /**
  168. * @internal
  169. * @param string $app
  170. * @param string $path
  171. */
  172. public static function registerAutoloading($app, $path) {
  173. $key = $app . '-' . $path;
  174. if(isset(self::$alreadyRegistered[$key])) {
  175. return;
  176. }
  177. self::$alreadyRegistered[$key] = true;
  178. // Register on PSR-4 composer autoloader
  179. $appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
  180. \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
  181. if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
  182. \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
  183. }
  184. // Register on legacy autoloader
  185. \OC::$loader->addValidRoot($path);
  186. }
  187. /**
  188. * Load app.php from the given app
  189. *
  190. * @param string $app app name
  191. */
  192. private static function requireAppFile($app) {
  193. try {
  194. // encapsulated here to avoid variable scope conflicts
  195. require_once $app . '/appinfo/app.php';
  196. } catch (Error $ex) {
  197. \OC::$server->getLogger()->logException($ex);
  198. $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
  199. if (!in_array($app, $blacklist)) {
  200. self::disable($app);
  201. }
  202. }
  203. }
  204. /**
  205. * check if an app is of a specific type
  206. *
  207. * @param string $app
  208. * @param string|array $types
  209. * @return bool
  210. */
  211. public static function isType($app, $types) {
  212. if (is_string($types)) {
  213. $types = array($types);
  214. }
  215. $appTypes = self::getAppTypes($app);
  216. foreach ($types as $type) {
  217. if (array_search($type, $appTypes) !== false) {
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. /**
  224. * get the types of an app
  225. *
  226. * @param string $app
  227. * @return array
  228. */
  229. private static function getAppTypes($app) {
  230. //load the cache
  231. if (count(self::$appTypes) == 0) {
  232. self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
  233. }
  234. if (isset(self::$appTypes[$app])) {
  235. return explode(',', self::$appTypes[$app]);
  236. } else {
  237. return array();
  238. }
  239. }
  240. /**
  241. * read app types from info.xml and cache them in the database
  242. */
  243. public static function setAppTypes($app) {
  244. $appData = self::getAppInfo($app);
  245. if(!is_array($appData)) {
  246. return;
  247. }
  248. if (isset($appData['types'])) {
  249. $appTypes = implode(',', $appData['types']);
  250. } else {
  251. $appTypes = '';
  252. $appData['types'] = [];
  253. }
  254. \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes);
  255. if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) {
  256. $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes');
  257. if ($enabled !== 'yes' && $enabled !== 'no') {
  258. \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes');
  259. }
  260. }
  261. }
  262. /**
  263. * check if app is shipped
  264. *
  265. * @param string $appId the id of the app to check
  266. * @return bool
  267. *
  268. * Check if an app that is installed is a shipped app or installed from the appstore.
  269. */
  270. public static function isShipped($appId) {
  271. return \OC::$server->getAppManager()->isShipped($appId);
  272. }
  273. /**
  274. * get all enabled apps
  275. */
  276. protected static $enabledAppsCache = array();
  277. /**
  278. * Returns apps enabled for the current user.
  279. *
  280. * @param bool $forceRefresh whether to refresh the cache
  281. * @param bool $all whether to return apps for all users, not only the
  282. * currently logged in one
  283. * @return string[]
  284. */
  285. public static function getEnabledApps($forceRefresh = false, $all = false) {
  286. if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
  287. return array();
  288. }
  289. // in incognito mode or when logged out, $user will be false,
  290. // which is also the case during an upgrade
  291. $appManager = \OC::$server->getAppManager();
  292. if ($all) {
  293. $user = null;
  294. } else {
  295. $user = \OC::$server->getUserSession()->getUser();
  296. }
  297. if (is_null($user)) {
  298. $apps = $appManager->getInstalledApps();
  299. } else {
  300. $apps = $appManager->getEnabledAppsForUser($user);
  301. }
  302. $apps = array_filter($apps, function ($app) {
  303. return $app !== 'files';//we add this manually
  304. });
  305. sort($apps);
  306. array_unshift($apps, 'files');
  307. return $apps;
  308. }
  309. /**
  310. * checks whether or not an app is enabled
  311. *
  312. * @param string $app app
  313. * @return bool
  314. *
  315. * This function checks whether or not an app is enabled.
  316. */
  317. public static function isEnabled($app) {
  318. return \OC::$server->getAppManager()->isEnabledForUser($app);
  319. }
  320. /**
  321. * enables an app
  322. *
  323. * @param string $appId
  324. * @param array $groups (optional) when set, only these groups will have access to the app
  325. * @throws \Exception
  326. * @return void
  327. *
  328. * This function set an app as enabled in appconfig.
  329. */
  330. public function enable($appId,
  331. $groups = null) {
  332. self::$enabledAppsCache = []; // flush
  333. $l = \OC::$server->getL10N('core');
  334. $config = \OC::$server->getConfig();
  335. // Check if app is already downloaded
  336. $installer = new Installer(
  337. \OC::$server->getAppFetcher(),
  338. \OC::$server->getHTTPClientService(),
  339. \OC::$server->getTempManager(),
  340. \OC::$server->getLogger()
  341. );
  342. $isDownloaded = $installer->isDownloaded($appId);
  343. if(!$isDownloaded) {
  344. $installer->downloadApp($appId);
  345. }
  346. if (!Installer::isInstalled($appId)) {
  347. $appId = self::installApp(
  348. $appId,
  349. $config,
  350. $l
  351. );
  352. $appPath = self::getAppPath($appId);
  353. self::registerAutoloading($appId, $appPath);
  354. $installer->installApp($appId);
  355. } else {
  356. // check for required dependencies
  357. $info = self::getAppInfo($appId);
  358. self::checkAppDependencies($config, $l, $info);
  359. $appPath = self::getAppPath($appId);
  360. self::registerAutoloading($appId, $appPath);
  361. $installer->installApp($appId);
  362. }
  363. $appManager = \OC::$server->getAppManager();
  364. if (!is_null($groups)) {
  365. $groupManager = \OC::$server->getGroupManager();
  366. $groupsList = [];
  367. foreach ($groups as $group) {
  368. $groupItem = $groupManager->get($group);
  369. if ($groupItem instanceof \OCP\IGroup) {
  370. $groupsList[] = $groupManager->get($group);
  371. }
  372. }
  373. $appManager->enableAppForGroups($appId, $groupsList);
  374. } else {
  375. $appManager->enableApp($appId);
  376. }
  377. $info = self::getAppInfo($appId);
  378. if(isset($info['settings']) && is_array($info['settings'])) {
  379. $appPath = self::getAppPath($appId);
  380. self::registerAutoloading($appId, $appPath);
  381. \OC::$server->getSettingsManager()->setupSettings($info['settings']);
  382. }
  383. }
  384. /**
  385. * @param string $app
  386. * @return bool
  387. */
  388. public static function removeApp($app) {
  389. if (self::isShipped($app)) {
  390. return false;
  391. }
  392. $installer = new Installer(
  393. \OC::$server->getAppFetcher(),
  394. \OC::$server->getHTTPClientService(),
  395. \OC::$server->getTempManager(),
  396. \OC::$server->getLogger()
  397. );
  398. return $installer->removeApp($app);
  399. }
  400. /**
  401. * This function set an app as disabled in appconfig.
  402. *
  403. * @param string $app app
  404. * @throws Exception
  405. */
  406. public static function disable($app) {
  407. // flush
  408. self::$enabledAppsCache = array();
  409. // run uninstall steps
  410. $appData = OC_App::getAppInfo($app);
  411. if (!is_null($appData)) {
  412. OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']);
  413. }
  414. // emit disable hook - needed anymore ?
  415. \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
  416. // finally disable it
  417. $appManager = \OC::$server->getAppManager();
  418. $appManager->disableApp($app);
  419. }
  420. /**
  421. * Returns the Settings Navigation
  422. *
  423. * @return string[]
  424. *
  425. * This function returns an array containing all settings pages added. The
  426. * entries are sorted by the key 'order' ascending.
  427. */
  428. public static function getSettingsNavigation() {
  429. $l = \OC::$server->getL10N('lib');
  430. $urlGenerator = \OC::$server->getURLGenerator();
  431. $settings = array();
  432. // by default, settings only contain the help menu
  433. if (\OC::$server->getSystemConfig()->getValue('knowledgebaseenabled', true)) {
  434. $settings = array(
  435. array(
  436. "id" => "help",
  437. "order" => 4,
  438. "href" => $urlGenerator->linkToRoute('settings_help'),
  439. "name" => $l->t("Help"),
  440. "icon" => $urlGenerator->imagePath("settings", "help.svg")
  441. )
  442. );
  443. }
  444. // if the user is logged-in
  445. if (\OC::$server->getUserSession()->isLoggedIn()) {
  446. // personal menu
  447. $settings[] = array(
  448. "id" => "personal",
  449. "order" => 1,
  450. "href" => $urlGenerator->linkToRoute('settings_personal'),
  451. "name" => $l->t("Personal"),
  452. "icon" => $urlGenerator->imagePath("settings", "personal.svg")
  453. );
  454. //SubAdmins are also allowed to access user management
  455. $userObject = \OC::$server->getUserSession()->getUser();
  456. $isSubAdmin = false;
  457. if($userObject !== null) {
  458. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  459. }
  460. if ($isSubAdmin) {
  461. // admin users menu
  462. $settings[] = array(
  463. "id" => "core_users",
  464. "order" => 3,
  465. "href" => $urlGenerator->linkToRoute('settings_users'),
  466. "name" => $l->t("Users"),
  467. "icon" => $urlGenerator->imagePath("settings", "users.svg")
  468. );
  469. }
  470. // if the user is an admin
  471. if (OC_User::isAdminUser(OC_User::getUser())) {
  472. // admin settings
  473. $settings[] = array(
  474. "id" => "admin",
  475. "order" => 2,
  476. "href" => $urlGenerator->linkToRoute('settings.AdminSettings.index'),
  477. "name" => $l->t("Admin"),
  478. "icon" => $urlGenerator->imagePath("settings", "admin.svg")
  479. );
  480. }
  481. }
  482. $navigation = self::proceedNavigation($settings);
  483. return $navigation;
  484. }
  485. // This is private as well. It simply works, so don't ask for more details
  486. private static function proceedNavigation($list) {
  487. $headerIconCount = 8;
  488. if(OC_User::isAdminUser(OC_User::getUser())) {
  489. $headerIconCount--;
  490. }
  491. usort($list, function($a, $b) {
  492. if (isset($a['order']) && isset($b['order'])) {
  493. return ($a['order'] < $b['order']) ? -1 : 1;
  494. } else if (isset($a['order']) || isset($b['order'])) {
  495. return isset($a['order']) ? -1 : 1;
  496. } else {
  497. return ($a['name'] < $b['name']) ? -1 : 1;
  498. }
  499. });
  500. $activeAppIndex = -1;
  501. $activeApp = OC::$server->getNavigationManager()->getActiveEntry();
  502. foreach ($list as $index => &$navEntry) {
  503. if ($navEntry['id'] == $activeApp) {
  504. $navEntry['active'] = true;
  505. $activeAppIndex = $index;
  506. } else {
  507. $navEntry['active'] = false;
  508. }
  509. }
  510. unset($navEntry);
  511. if($activeAppIndex > ($headerIconCount-1)) {
  512. $active = $list[$activeAppIndex];
  513. $lastInHeader = $list[$headerIconCount-1];
  514. $list[$headerIconCount-1] = $active;
  515. $list[$activeAppIndex] = $lastInHeader;
  516. }
  517. foreach ($list as $index => &$navEntry) {
  518. $navEntry['showInHeader'] = false;
  519. if($index < $headerIconCount) {
  520. $navEntry['showInHeader'] = true;
  521. }
  522. }
  523. return $list;
  524. }
  525. public static function proceedAppNavigation($entries) {
  526. $headerIconCount = 8;
  527. if(OC_User::isAdminUser(OC_User::getUser())) {
  528. $headerIconCount--;
  529. }
  530. $activeAppIndex = -1;
  531. $list = self::proceedNavigation($entries);
  532. $activeApp = OC::$server->getNavigationManager()->getActiveEntry();
  533. foreach ($list as $index => &$navEntry) {
  534. if ($navEntry['id'] == $activeApp) {
  535. $navEntry['active'] = true;
  536. $activeAppIndex = $index;
  537. } else {
  538. $navEntry['active'] = false;
  539. }
  540. }
  541. // move active item to last position
  542. if($activeAppIndex > ($headerIconCount-1)) {
  543. $active = $list[$activeAppIndex];
  544. $lastInHeader = $list[$headerIconCount-1];
  545. $list[$headerIconCount-1] = $active;
  546. $list[$activeAppIndex] = $lastInHeader;
  547. }
  548. $list = array_slice($list, 0, $headerIconCount);
  549. return $list;
  550. }
  551. /**
  552. * Get the path where to install apps
  553. *
  554. * @return string|false
  555. */
  556. public static function getInstallPath() {
  557. if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
  558. return false;
  559. }
  560. foreach (OC::$APPSROOTS as $dir) {
  561. if (isset($dir['writable']) && $dir['writable'] === true) {
  562. return $dir['path'];
  563. }
  564. }
  565. \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
  566. return null;
  567. }
  568. /**
  569. * search for an app in all app-directories
  570. *
  571. * @param string $appId
  572. * @return false|string
  573. */
  574. public static function findAppInDirectories($appId) {
  575. $sanitizedAppId = self::cleanAppId($appId);
  576. if($sanitizedAppId !== $appId) {
  577. return false;
  578. }
  579. static $app_dir = array();
  580. if (isset($app_dir[$appId])) {
  581. return $app_dir[$appId];
  582. }
  583. $possibleApps = array();
  584. foreach (OC::$APPSROOTS as $dir) {
  585. if (file_exists($dir['path'] . '/' . $appId)) {
  586. $possibleApps[] = $dir;
  587. }
  588. }
  589. if (empty($possibleApps)) {
  590. return false;
  591. } elseif (count($possibleApps) === 1) {
  592. $dir = array_shift($possibleApps);
  593. $app_dir[$appId] = $dir;
  594. return $dir;
  595. } else {
  596. $versionToLoad = array();
  597. foreach ($possibleApps as $possibleApp) {
  598. $version = self::getAppVersionByPath($possibleApp['path']);
  599. if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
  600. $versionToLoad = array(
  601. 'dir' => $possibleApp,
  602. 'version' => $version,
  603. );
  604. }
  605. }
  606. $app_dir[$appId] = $versionToLoad['dir'];
  607. return $versionToLoad['dir'];
  608. //TODO - write test
  609. }
  610. }
  611. /**
  612. * Get the directory for the given app.
  613. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  614. *
  615. * @param string $appId
  616. * @return string|false
  617. */
  618. public static function getAppPath($appId) {
  619. if ($appId === null || trim($appId) === '') {
  620. return false;
  621. }
  622. if (($dir = self::findAppInDirectories($appId)) != false) {
  623. return $dir['path'] . '/' . $appId;
  624. }
  625. return false;
  626. }
  627. /**
  628. * Get the path for the given app on the access
  629. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  630. *
  631. * @param string $appId
  632. * @return string|false
  633. */
  634. public static function getAppWebPath($appId) {
  635. if (($dir = self::findAppInDirectories($appId)) != false) {
  636. return OC::$WEBROOT . $dir['url'] . '/' . $appId;
  637. }
  638. return false;
  639. }
  640. /**
  641. * get the last version of the app from appinfo/info.xml
  642. *
  643. * @param string $appId
  644. * @param bool $useCache
  645. * @return string
  646. */
  647. public static function getAppVersion($appId, $useCache = true) {
  648. if($useCache && isset(self::$appVersion[$appId])) {
  649. return self::$appVersion[$appId];
  650. }
  651. $file = self::getAppPath($appId);
  652. self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0';
  653. return self::$appVersion[$appId];
  654. }
  655. /**
  656. * get app's version based on it's path
  657. *
  658. * @param string $path
  659. * @return string
  660. */
  661. public static function getAppVersionByPath($path) {
  662. $infoFile = $path . '/appinfo/info.xml';
  663. $appData = self::getAppInfo($infoFile, true);
  664. return isset($appData['version']) ? $appData['version'] : '';
  665. }
  666. /**
  667. * Read all app metadata from the info.xml file
  668. *
  669. * @param string $appId id of the app or the path of the info.xml file
  670. * @param bool $path
  671. * @param string $lang
  672. * @return array|null
  673. * @note all data is read from info.xml, not just pre-defined fields
  674. */
  675. public static function getAppInfo($appId, $path = false, $lang = null) {
  676. if ($path) {
  677. $file = $appId;
  678. } else {
  679. if ($lang === null && isset(self::$appInfo[$appId])) {
  680. return self::$appInfo[$appId];
  681. }
  682. $appPath = self::getAppPath($appId);
  683. if($appPath === false) {
  684. return null;
  685. }
  686. $file = $appPath . '/appinfo/info.xml';
  687. }
  688. $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo'));
  689. $data = $parser->parse($file);
  690. if (is_array($data)) {
  691. $data = OC_App::parseAppInfo($data, $lang);
  692. }
  693. if(isset($data['ocsid'])) {
  694. $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid');
  695. if($storedId !== '' && $storedId !== $data['ocsid']) {
  696. $data['ocsid'] = $storedId;
  697. }
  698. }
  699. if ($lang === null) {
  700. self::$appInfo[$appId] = $data;
  701. }
  702. return $data;
  703. }
  704. /**
  705. * Returns the navigation
  706. *
  707. * @return array
  708. *
  709. * This function returns an array containing all entries added. The
  710. * entries are sorted by the key 'order' ascending. Additional to the keys
  711. * given for each app the following keys exist:
  712. * - active: boolean, signals if the user is on this navigation entry
  713. */
  714. public static function getNavigation() {
  715. $entries = OC::$server->getNavigationManager()->getAll();
  716. $navigation = self::proceedNavigation($entries);
  717. return $navigation;
  718. }
  719. /**
  720. * Returns the navigation inside the header bar
  721. *
  722. * @return array
  723. *
  724. * This function returns an array containing all entries added. The
  725. * entries are sorted by the key 'order' ascending. Additional to the keys
  726. * given for each app the following keys exist:
  727. * - active: boolean, signals if the user is on this navigation entry
  728. */
  729. public static function getHeaderNavigation() {
  730. $entries = OC::$server->getNavigationManager()->getAll();
  731. $navigation = self::proceedAppNavigation($entries);
  732. return $navigation;
  733. }
  734. /**
  735. * get the id of loaded app
  736. *
  737. * @return string
  738. */
  739. public static function getCurrentApp() {
  740. $request = \OC::$server->getRequest();
  741. $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
  742. $topFolder = substr($script, 0, strpos($script, '/'));
  743. if (empty($topFolder)) {
  744. $path_info = $request->getPathInfo();
  745. if ($path_info) {
  746. $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
  747. }
  748. }
  749. if ($topFolder == 'apps') {
  750. $length = strlen($topFolder);
  751. return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
  752. } else {
  753. return $topFolder;
  754. }
  755. }
  756. /**
  757. * @param string $type
  758. * @return array
  759. */
  760. public static function getForms($type) {
  761. $forms = array();
  762. switch ($type) {
  763. case 'admin':
  764. $source = self::$adminForms;
  765. break;
  766. case 'personal':
  767. $source = self::$personalForms;
  768. break;
  769. default:
  770. return array();
  771. }
  772. foreach ($source as $form) {
  773. $forms[] = include $form;
  774. }
  775. return $forms;
  776. }
  777. /**
  778. * register an admin form to be shown
  779. *
  780. * @param string $app
  781. * @param string $page
  782. */
  783. public static function registerAdmin($app, $page) {
  784. self::$adminForms[] = $app . '/' . $page . '.php';
  785. }
  786. /**
  787. * register a personal form to be shown
  788. * @param string $app
  789. * @param string $page
  790. */
  791. public static function registerPersonal($app, $page) {
  792. self::$personalForms[] = $app . '/' . $page . '.php';
  793. }
  794. /**
  795. * @param array $entry
  796. */
  797. public static function registerLogIn(array $entry) {
  798. self::$altLogin[] = $entry;
  799. }
  800. /**
  801. * @return array
  802. */
  803. public static function getAlternativeLogIns() {
  804. return self::$altLogin;
  805. }
  806. /**
  807. * get a list of all apps in the apps folder
  808. *
  809. * @return array an array of app names (string IDs)
  810. * @todo: change the name of this method to getInstalledApps, which is more accurate
  811. */
  812. public static function getAllApps() {
  813. $apps = array();
  814. foreach (OC::$APPSROOTS as $apps_dir) {
  815. if (!is_readable($apps_dir['path'])) {
  816. \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
  817. continue;
  818. }
  819. $dh = opendir($apps_dir['path']);
  820. if (is_resource($dh)) {
  821. while (($file = readdir($dh)) !== false) {
  822. if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
  823. $apps[] = $file;
  824. }
  825. }
  826. }
  827. }
  828. return $apps;
  829. }
  830. /**
  831. * List all apps, this is used in apps.php
  832. *
  833. * @return array
  834. */
  835. public function listAllApps() {
  836. $installedApps = OC_App::getAllApps();
  837. //we don't want to show configuration for these
  838. $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
  839. $appList = array();
  840. $langCode = \OC::$server->getL10N('core')->getLanguageCode();
  841. $urlGenerator = \OC::$server->getURLGenerator();
  842. foreach ($installedApps as $app) {
  843. if (array_search($app, $blacklist) === false) {
  844. $info = OC_App::getAppInfo($app, false, $langCode);
  845. if (!is_array($info)) {
  846. \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
  847. continue;
  848. }
  849. if (!isset($info['name'])) {
  850. \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
  851. continue;
  852. }
  853. $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no');
  854. $info['groups'] = null;
  855. if ($enabled === 'yes') {
  856. $active = true;
  857. } else if ($enabled === 'no') {
  858. $active = false;
  859. } else {
  860. $active = true;
  861. $info['groups'] = $enabled;
  862. }
  863. $info['active'] = $active;
  864. if (self::isShipped($app)) {
  865. $info['internal'] = true;
  866. $info['level'] = self::officialApp;
  867. $info['removable'] = false;
  868. } else {
  869. $info['internal'] = false;
  870. $info['removable'] = true;
  871. }
  872. $appPath = self::getAppPath($app);
  873. if($appPath !== false) {
  874. $appIcon = $appPath . '/img/' . $app . '.svg';
  875. if (file_exists($appIcon)) {
  876. $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
  877. $info['previewAsIcon'] = true;
  878. } else {
  879. $appIcon = $appPath . '/img/app.svg';
  880. if (file_exists($appIcon)) {
  881. $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
  882. $info['previewAsIcon'] = true;
  883. }
  884. }
  885. }
  886. // fix documentation
  887. if (isset($info['documentation']) && is_array($info['documentation'])) {
  888. foreach ($info['documentation'] as $key => $url) {
  889. // If it is not an absolute URL we assume it is a key
  890. // i.e. admin-ldap will get converted to go.php?to=admin-ldap
  891. if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
  892. $url = $urlGenerator->linkToDocs($url);
  893. }
  894. $info['documentation'][$key] = $url;
  895. }
  896. }
  897. $info['version'] = OC_App::getAppVersion($app);
  898. $appList[] = $info;
  899. }
  900. }
  901. return $appList;
  902. }
  903. /**
  904. * Returns the internal app ID or false
  905. * @param string $ocsID
  906. * @return string|false
  907. */
  908. public static function getInternalAppIdByOcs($ocsID) {
  909. if(is_numeric($ocsID)) {
  910. $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
  911. if(array_search($ocsID, $idArray)) {
  912. return array_search($ocsID, $idArray);
  913. }
  914. }
  915. return false;
  916. }
  917. public static function shouldUpgrade($app) {
  918. $versions = self::getAppVersions();
  919. $currentVersion = OC_App::getAppVersion($app);
  920. if ($currentVersion && isset($versions[$app])) {
  921. $installedVersion = $versions[$app];
  922. if (!version_compare($currentVersion, $installedVersion, '=')) {
  923. return true;
  924. }
  925. }
  926. return false;
  927. }
  928. /**
  929. * Adjust the number of version parts of $version1 to match
  930. * the number of version parts of $version2.
  931. *
  932. * @param string $version1 version to adjust
  933. * @param string $version2 version to take the number of parts from
  934. * @return string shortened $version1
  935. */
  936. private static function adjustVersionParts($version1, $version2) {
  937. $version1 = explode('.', $version1);
  938. $version2 = explode('.', $version2);
  939. // reduce $version1 to match the number of parts in $version2
  940. while (count($version1) > count($version2)) {
  941. array_pop($version1);
  942. }
  943. // if $version1 does not have enough parts, add some
  944. while (count($version1) < count($version2)) {
  945. $version1[] = '0';
  946. }
  947. return implode('.', $version1);
  948. }
  949. /**
  950. * Check whether the current ownCloud version matches the given
  951. * application's version requirements.
  952. *
  953. * The comparison is made based on the number of parts that the
  954. * app info version has. For example for ownCloud 6.0.3 if the
  955. * app info version is expecting version 6.0, the comparison is
  956. * made on the first two parts of the ownCloud version.
  957. * This means that it's possible to specify "requiremin" => 6
  958. * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
  959. *
  960. * @param string $ocVersion ownCloud version to check against
  961. * @param array $appInfo app info (from xml)
  962. *
  963. * @return boolean true if compatible, otherwise false
  964. */
  965. public static function isAppCompatible($ocVersion, $appInfo) {
  966. $requireMin = '';
  967. $requireMax = '';
  968. if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
  969. $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
  970. } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
  971. $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
  972. } else if (isset($appInfo['requiremin'])) {
  973. $requireMin = $appInfo['requiremin'];
  974. } else if (isset($appInfo['require'])) {
  975. $requireMin = $appInfo['require'];
  976. }
  977. if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
  978. $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
  979. } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
  980. $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
  981. } else if (isset($appInfo['requiremax'])) {
  982. $requireMax = $appInfo['requiremax'];
  983. }
  984. if (is_array($ocVersion)) {
  985. $ocVersion = implode('.', $ocVersion);
  986. }
  987. if (!empty($requireMin)
  988. && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
  989. ) {
  990. return false;
  991. }
  992. if (!empty($requireMax)
  993. && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
  994. ) {
  995. return false;
  996. }
  997. return true;
  998. }
  999. /**
  1000. * get the installed version of all apps
  1001. */
  1002. public static function getAppVersions() {
  1003. static $versions;
  1004. if(!$versions) {
  1005. $appConfig = \OC::$server->getAppConfig();
  1006. $versions = $appConfig->getValues(false, 'installed_version');
  1007. }
  1008. return $versions;
  1009. }
  1010. /**
  1011. * @param string $app
  1012. * @param \OCP\IConfig $config
  1013. * @param \OCP\IL10N $l
  1014. * @return bool
  1015. *
  1016. * @throws Exception if app is not compatible with this version of ownCloud
  1017. * @throws Exception if no app-name was specified
  1018. */
  1019. public function installApp($app,
  1020. \OCP\IConfig $config,
  1021. \OCP\IL10N $l) {
  1022. if ($app !== false) {
  1023. // check if the app is compatible with this version of ownCloud
  1024. $info = self::getAppInfo($app);
  1025. if(!is_array($info)) {
  1026. throw new \Exception(
  1027. $l->t('App "%s" cannot be installed because appinfo file cannot be read.',
  1028. [$info['name']]
  1029. )
  1030. );
  1031. }
  1032. $version = \OCP\Util::getVersion();
  1033. if (!self::isAppCompatible($version, $info)) {
  1034. throw new \Exception(
  1035. $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
  1036. array($info['name'])
  1037. )
  1038. );
  1039. }
  1040. // check for required dependencies
  1041. self::checkAppDependencies($config, $l, $info);
  1042. $config->setAppValue($app, 'enabled', 'yes');
  1043. if (isset($appData['id'])) {
  1044. $config->setAppValue($app, 'ocsid', $appData['id']);
  1045. }
  1046. if(isset($info['settings']) && is_array($info['settings'])) {
  1047. $appPath = self::getAppPath($app);
  1048. self::registerAutoloading($app, $appPath);
  1049. \OC::$server->getSettingsManager()->setupSettings($info['settings']);
  1050. }
  1051. \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
  1052. } else {
  1053. if(empty($appName) ) {
  1054. throw new \Exception($l->t("No app name specified"));
  1055. } else {
  1056. throw new \Exception($l->t("App '%s' could not be installed!", $appName));
  1057. }
  1058. }
  1059. return $app;
  1060. }
  1061. /**
  1062. * update the database for the app and call the update script
  1063. *
  1064. * @param string $appId
  1065. * @return bool
  1066. */
  1067. public static function updateApp($appId) {
  1068. $appPath = self::getAppPath($appId);
  1069. if($appPath === false) {
  1070. return false;
  1071. }
  1072. $appData = self::getAppInfo($appId);
  1073. self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
  1074. if (file_exists($appPath . '/appinfo/database.xml')) {
  1075. OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
  1076. }
  1077. self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
  1078. self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
  1079. unset(self::$appVersion[$appId]);
  1080. // run upgrade code
  1081. if (file_exists($appPath . '/appinfo/update.php')) {
  1082. self::loadApp($appId);
  1083. include $appPath . '/appinfo/update.php';
  1084. }
  1085. self::setupBackgroundJobs($appData['background-jobs']);
  1086. if(isset($appData['settings']) && is_array($appData['settings'])) {
  1087. $appPath = self::getAppPath($appId);
  1088. self::registerAutoloading($appId, $appPath);
  1089. \OC::$server->getSettingsManager()->setupSettings($appData['settings']);
  1090. }
  1091. //set remote/public handlers
  1092. if (array_key_exists('ocsid', $appData)) {
  1093. \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
  1094. } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
  1095. \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
  1096. }
  1097. foreach ($appData['remote'] as $name => $path) {
  1098. \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
  1099. }
  1100. foreach ($appData['public'] as $name => $path) {
  1101. \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
  1102. }
  1103. self::setAppTypes($appId);
  1104. $version = \OC_App::getAppVersion($appId);
  1105. \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version);
  1106. \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
  1107. ManagerEvent::EVENT_APP_UPDATE, $appId
  1108. ));
  1109. return true;
  1110. }
  1111. /**
  1112. * @param string $appId
  1113. * @param string[] $steps
  1114. * @throws \OC\NeedsUpdateException
  1115. */
  1116. public static function executeRepairSteps($appId, array $steps) {
  1117. if (empty($steps)) {
  1118. return;
  1119. }
  1120. // load the app
  1121. self::loadApp($appId);
  1122. $dispatcher = OC::$server->getEventDispatcher();
  1123. // load the steps
  1124. $r = new Repair([], $dispatcher);
  1125. foreach ($steps as $step) {
  1126. try {
  1127. $r->addStep($step);
  1128. } catch (Exception $ex) {
  1129. $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
  1130. \OC::$server->getLogger()->logException($ex);
  1131. }
  1132. }
  1133. // run the steps
  1134. $r->run();
  1135. }
  1136. public static function setupBackgroundJobs(array $jobs) {
  1137. $queue = \OC::$server->getJobList();
  1138. foreach ($jobs as $job) {
  1139. $queue->add($job);
  1140. }
  1141. }
  1142. /**
  1143. * @param string $appId
  1144. * @param string[] $steps
  1145. */
  1146. private static function setupLiveMigrations($appId, array $steps) {
  1147. $queue = \OC::$server->getJobList();
  1148. foreach ($steps as $step) {
  1149. $queue->add('OC\Migration\BackgroundRepair', [
  1150. 'app' => $appId,
  1151. 'step' => $step]);
  1152. }
  1153. }
  1154. /**
  1155. * @param string $appId
  1156. * @return \OC\Files\View|false
  1157. */
  1158. public static function getStorage($appId) {
  1159. if (OC_App::isEnabled($appId)) { //sanity check
  1160. if (\OC::$server->getUserSession()->isLoggedIn()) {
  1161. $view = new \OC\Files\View('/' . OC_User::getUser());
  1162. if (!$view->file_exists($appId)) {
  1163. $view->mkdir($appId);
  1164. }
  1165. return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
  1166. } else {
  1167. \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
  1168. return false;
  1169. }
  1170. } else {
  1171. \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
  1172. return false;
  1173. }
  1174. }
  1175. protected static function findBestL10NOption($options, $lang) {
  1176. $fallback = $similarLangFallback = $englishFallback = false;
  1177. $lang = strtolower($lang);
  1178. $similarLang = $lang;
  1179. if (strpos($similarLang, '_')) {
  1180. // For "de_DE" we want to find "de" and the other way around
  1181. $similarLang = substr($lang, 0, strpos($lang, '_'));
  1182. }
  1183. foreach ($options as $option) {
  1184. if (is_array($option)) {
  1185. if ($fallback === false) {
  1186. $fallback = $option['@value'];
  1187. }
  1188. if (!isset($option['@attributes']['lang'])) {
  1189. continue;
  1190. }
  1191. $attributeLang = strtolower($option['@attributes']['lang']);
  1192. if ($attributeLang === $lang) {
  1193. return $option['@value'];
  1194. }
  1195. if ($attributeLang === $similarLang) {
  1196. $similarLangFallback = $option['@value'];
  1197. } else if (strpos($attributeLang, $similarLang . '_') === 0) {
  1198. if ($similarLangFallback === false) {
  1199. $similarLangFallback = $option['@value'];
  1200. }
  1201. }
  1202. } else {
  1203. $englishFallback = $option;
  1204. }
  1205. }
  1206. if ($similarLangFallback !== false) {
  1207. return $similarLangFallback;
  1208. } else if ($englishFallback !== false) {
  1209. return $englishFallback;
  1210. }
  1211. return (string) $fallback;
  1212. }
  1213. /**
  1214. * parses the app data array and enhanced the 'description' value
  1215. *
  1216. * @param array $data the app data
  1217. * @param string $lang
  1218. * @return array improved app data
  1219. */
  1220. public static function parseAppInfo(array $data, $lang = null) {
  1221. if ($lang && isset($data['name']) && is_array($data['name'])) {
  1222. $data['name'] = self::findBestL10NOption($data['name'], $lang);
  1223. }
  1224. if ($lang && isset($data['summary']) && is_array($data['summary'])) {
  1225. $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
  1226. }
  1227. if ($lang && isset($data['description']) && is_array($data['description'])) {
  1228. $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
  1229. } else if (isset($data['description']) && is_string($data['description'])) {
  1230. $data['description'] = trim($data['description']);
  1231. } else {
  1232. $data['description'] = '';
  1233. }
  1234. return $data;
  1235. }
  1236. /**
  1237. * @param \OCP\IConfig $config
  1238. * @param \OCP\IL10N $l
  1239. * @param array $info
  1240. * @throws \Exception
  1241. */
  1242. protected static function checkAppDependencies($config, $l, $info) {
  1243. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
  1244. $missing = $dependencyAnalyzer->analyze($info);
  1245. if (!empty($missing)) {
  1246. $missingMsg = join(PHP_EOL, $missing);
  1247. throw new \Exception(
  1248. $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
  1249. [$info['name'], $missingMsg]
  1250. )
  1251. );
  1252. }
  1253. }
  1254. }