app.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Björn Schießle <schiessle@owncloud.com>
  7. * @author Borjan Tchakaloff <borjan@tchakaloff.fr>
  8. * @author Brice Maron <brice@bmaron.net>
  9. * @author Christopher Schäpers <kondou@ts.unde.re>
  10. * @author Felix Moeller <mail@felixmoeller.de>
  11. * @author Frank Karlitschek <frank@owncloud.org>
  12. * @author Georg Ehrke <georg@owncloud.com>
  13. * @author Jakob Sack <mail@jakobsack.de>
  14. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  15. * @author Joas Schilling <nickvergessen@owncloud.com>
  16. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  17. * @author Kamil Domanski <kdomanski@kdemail.net>
  18. * @author Lukas Reschke <lukas@owncloud.com>
  19. * @author Markus Goetz <markus@woboq.com>
  20. * @author Morris Jobke <hey@morrisjobke.de>
  21. * @author Robin Appelman <icewind@owncloud.com>
  22. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  23. * @author Sam Tuke <mail@samtuke.com>
  24. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  25. * @author Thomas Müller <thomas.mueller@tmit.eu>
  26. * @author Thomas Tanghus <thomas@tanghus.net>
  27. * @author Tom Needham <tom@owncloud.com>
  28. * @author Vincent Petry <pvince81@owncloud.com>
  29. *
  30. * @copyright Copyright (c) 2015, ownCloud, Inc.
  31. * @license AGPL-3.0
  32. *
  33. * This code is free software: you can redistribute it and/or modify
  34. * it under the terms of the GNU Affero General Public License, version 3,
  35. * as published by the Free Software Foundation.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU Affero General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU Affero General Public License, version 3,
  43. * along with this program. If not, see <http://www.gnu.org/licenses/>
  44. *
  45. */
  46. use OC\App\DependencyAnalyzer;
  47. use OC\App\Platform;
  48. use OC\OCSClient;
  49. /**
  50. * This class manages the apps. It allows them to register and integrate in the
  51. * ownCloud ecosystem. Furthermore, this class is responsible for installing,
  52. * upgrading and removing apps.
  53. */
  54. class OC_App {
  55. static private $appVersion = [];
  56. static private $adminForms = array();
  57. static private $personalForms = array();
  58. static private $appInfo = array();
  59. static private $appTypes = array();
  60. static private $loadedApps = array();
  61. static private $altLogin = array();
  62. private static $shippedApps = null;
  63. const officialApp = 200;
  64. /**
  65. * clean the appId
  66. *
  67. * @param string|boolean $app AppId that needs to be cleaned
  68. * @return string
  69. */
  70. public static function cleanAppId($app) {
  71. return str_replace(array('\0', '/', '\\', '..'), '', $app);
  72. }
  73. /**
  74. * loads all apps
  75. *
  76. * @param array $types
  77. * @return bool
  78. *
  79. * This function walks through the ownCloud directory and loads all apps
  80. * it can find. A directory contains an app if the file /appinfo/info.xml
  81. * exists.
  82. *
  83. * if $types is set, only apps of those types will be loaded
  84. */
  85. public static function loadApps($types = null) {
  86. if (OC_Config::getValue('maintenance', false)) {
  87. return false;
  88. }
  89. // Load the enabled apps here
  90. $apps = self::getEnabledApps();
  91. // prevent app.php from printing output
  92. ob_start();
  93. foreach ($apps as $app) {
  94. if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
  95. self::$loadedApps[] = $app;
  96. self::loadApp($app);
  97. }
  98. }
  99. ob_end_clean();
  100. return true;
  101. }
  102. /**
  103. * load a single app
  104. *
  105. * @param string $app
  106. * @param bool $checkUpgrade whether an upgrade check should be done
  107. * @throws \OC\NeedsUpdateException
  108. */
  109. public static function loadApp($app, $checkUpgrade = true) {
  110. if (is_file(self::getAppPath($app) . '/appinfo/app.php')) {
  111. \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
  112. if ($checkUpgrade and self::shouldUpgrade($app)) {
  113. throw new \OC\NeedsUpdateException();
  114. }
  115. self::requireAppFile($app);
  116. if (self::isType($app, array('authentication'))) {
  117. // since authentication apps affect the "is app enabled for group" check,
  118. // the enabled apps cache needs to be cleared to make sure that the
  119. // next time getEnableApps() is called it will also include apps that were
  120. // enabled for groups
  121. self::$enabledAppsCache = array();
  122. }
  123. \OC::$server->getEventLogger()->end('load_app_' . $app);
  124. }
  125. }
  126. /**
  127. * Load app.php from the given app
  128. *
  129. * @param string $app app name
  130. */
  131. private static function requireAppFile($app) {
  132. // encapsulated here to avoid variable scope conflicts
  133. require_once $app . '/appinfo/app.php';
  134. }
  135. /**
  136. * check if an app is of a specific type
  137. *
  138. * @param string $app
  139. * @param string|array $types
  140. * @return bool
  141. */
  142. public static function isType($app, $types) {
  143. if (is_string($types)) {
  144. $types = array($types);
  145. }
  146. $appTypes = self::getAppTypes($app);
  147. foreach ($types as $type) {
  148. if (array_search($type, $appTypes) !== false) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * get the types of an app
  156. *
  157. * @param string $app
  158. * @return array
  159. */
  160. private static function getAppTypes($app) {
  161. //load the cache
  162. if (count(self::$appTypes) == 0) {
  163. self::$appTypes = OC_Appconfig::getValues(false, 'types');
  164. }
  165. if (isset(self::$appTypes[$app])) {
  166. return explode(',', self::$appTypes[$app]);
  167. } else {
  168. return array();
  169. }
  170. }
  171. /**
  172. * read app types from info.xml and cache them in the database
  173. */
  174. public static function setAppTypes($app) {
  175. $appData = self::getAppInfo($app);
  176. if (isset($appData['types'])) {
  177. $appTypes = implode(',', $appData['types']);
  178. } else {
  179. $appTypes = '';
  180. }
  181. OC_Appconfig::setValue($app, 'types', $appTypes);
  182. }
  183. /**
  184. * check if app is shipped
  185. *
  186. * @param string $appId the id of the app to check
  187. * @return bool
  188. *
  189. * Check if an app that is installed is a shipped app or installed from the appstore.
  190. */
  191. public static function isShipped($appId) {
  192. if (is_null(self::$shippedApps)) {
  193. $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
  194. if (file_exists($shippedJson)) {
  195. self::$shippedApps = json_decode(file_get_contents($shippedJson), true);
  196. self::$shippedApps = self::$shippedApps['shippedApps'];
  197. } else {
  198. self::$shippedApps = ['files', 'encryption', 'files_external',
  199. 'files_sharing', 'files_trashbin', 'files_versions', 'provisioning_api',
  200. 'user_ldap', 'user_webdavauth'];
  201. }
  202. }
  203. return in_array($appId, self::$shippedApps);
  204. }
  205. /**
  206. * get all enabled apps
  207. */
  208. protected static $enabledAppsCache = array();
  209. /**
  210. * Returns apps enabled for the current user.
  211. *
  212. * @param bool $forceRefresh whether to refresh the cache
  213. * @param bool $all whether to return apps for all users, not only the
  214. * currently logged in one
  215. * @return string[]
  216. */
  217. public static function getEnabledApps($forceRefresh = false, $all = false) {
  218. if (!OC_Config::getValue('installed', false)) {
  219. return array();
  220. }
  221. // in incognito mode or when logged out, $user will be false,
  222. // which is also the case during an upgrade
  223. $appManager = \OC::$server->getAppManager();
  224. if ($all) {
  225. $user = null;
  226. } else {
  227. $user = \OC::$server->getUserSession()->getUser();
  228. }
  229. if (is_null($user)) {
  230. $apps = $appManager->getInstalledApps();
  231. } else {
  232. $apps = $appManager->getEnabledAppsForUser($user);
  233. }
  234. $apps = array_filter($apps, function ($app) {
  235. return $app !== 'files';//we add this manually
  236. });
  237. sort($apps);
  238. array_unshift($apps, 'files');
  239. return $apps;
  240. }
  241. /**
  242. * checks whether or not an app is enabled
  243. *
  244. * @param string $app app
  245. * @return bool
  246. *
  247. * This function checks whether or not an app is enabled.
  248. */
  249. public static function isEnabled($app) {
  250. if ('files' == $app) {
  251. return true;
  252. }
  253. return \OC::$server->getAppManager()->isEnabledForUser($app);
  254. }
  255. /**
  256. * enables an app
  257. *
  258. * @param mixed $app app
  259. * @param array $groups (optional) when set, only these groups will have access to the app
  260. * @throws \Exception
  261. * @return void
  262. *
  263. * This function set an app as enabled in appconfig.
  264. */
  265. public static function enable($app, $groups = null) {
  266. self::$enabledAppsCache = array(); // flush
  267. if (!OC_Installer::isInstalled($app)) {
  268. $app = self::installApp($app);
  269. }
  270. $appManager = \OC::$server->getAppManager();
  271. if (!is_null($groups)) {
  272. $groupManager = \OC::$server->getGroupManager();
  273. $groupsList = [];
  274. foreach ($groups as $group) {
  275. $groupItem = $groupManager->get($group);
  276. if ($groupItem instanceof \OCP\IGroup) {
  277. $groupsList[] = $groupManager->get($group);
  278. }
  279. }
  280. $appManager->enableAppForGroups($app, $groupsList);
  281. } else {
  282. $appManager->enableApp($app);
  283. }
  284. }
  285. /**
  286. * @param string $app
  287. * @return int
  288. */
  289. public static function downloadApp($app) {
  290. $ocsClient = new OCSClient(
  291. \OC::$server->getHTTPClientService(),
  292. \OC::$server->getConfig(),
  293. \OC::$server->getLogger()
  294. );
  295. $appData = $ocsClient->getApplication($app);
  296. $download= $ocsClient->getApplicationDownload($app);
  297. if(isset($download['downloadlink']) and $download['downloadlink']!='') {
  298. // Replace spaces in download link without encoding entire URL
  299. $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']);
  300. $info = array('source' => 'http', 'href' => $download['downloadlink'], 'appdata' => $appData);
  301. $app = OC_Installer::installApp($info);
  302. }
  303. return $app;
  304. }
  305. /**
  306. * @param string $app
  307. * @return bool
  308. */
  309. public static function removeApp($app) {
  310. if (self::isShipped($app)) {
  311. return false;
  312. }
  313. return OC_Installer::removeApp($app);
  314. }
  315. /**
  316. * This function set an app as disabled in appconfig.
  317. *
  318. * @param string $app app
  319. * @throws Exception
  320. */
  321. public static function disable($app) {
  322. // Convert OCS ID to regular application identifier
  323. if(self::getInternalAppIdByOcs($app) !== false) {
  324. $app = self::getInternalAppIdByOcs($app);
  325. }
  326. if($app === 'files') {
  327. throw new \Exception("files can't be disabled.");
  328. }
  329. self::$enabledAppsCache = array(); // flush
  330. // check if app is a shipped app or not. if not delete
  331. \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
  332. $appManager = \OC::$server->getAppManager();
  333. $appManager->disableApp($app);
  334. }
  335. /**
  336. * marks a navigation entry as active
  337. *
  338. * @param string $id id of the entry
  339. * @return bool
  340. *
  341. * This function sets a navigation entry as active and removes the 'active'
  342. * property from all other entries. The templates can use this for
  343. * highlighting the current position of the user.
  344. *
  345. * @deprecated Use \OC::$server->getNavigationManager()->setActiveEntry() instead
  346. */
  347. public static function setActiveNavigationEntry($id) {
  348. OC::$server->getNavigationManager()->setActiveEntry($id);
  349. return true;
  350. }
  351. /**
  352. * Get the navigation entries for the $app
  353. *
  354. * @param string $app app
  355. * @return array an array of the $data added with addNavigationEntry
  356. *
  357. * Warning: destroys the existing entries
  358. */
  359. public static function getAppNavigationEntries($app) {
  360. if (is_file(self::getAppPath($app) . '/appinfo/app.php')) {
  361. OC::$server->getNavigationManager()->clear();
  362. try {
  363. require $app . '/appinfo/app.php';
  364. } catch (\OC\Encryption\Exceptions\ModuleAlreadyExistsException $e) {
  365. // FIXME we should avoid getting this exception in first place,
  366. // For now we just catch it, since we don't care about encryption modules
  367. // when trying to find out, whether the app has a navigation entry.
  368. }
  369. return OC::$server->getNavigationManager()->getAll();
  370. }
  371. return array();
  372. }
  373. /**
  374. * gets the active Menu entry
  375. *
  376. * @return string id or empty string
  377. *
  378. * This function returns the id of the active navigation entry (set by
  379. * setActiveNavigationEntry
  380. *
  381. * @deprecated Use \OC::$server->getNavigationManager()->getActiveEntry() instead
  382. */
  383. public static function getActiveNavigationEntry() {
  384. return OC::$server->getNavigationManager()->getActiveEntry();
  385. }
  386. /**
  387. * Returns the Settings Navigation
  388. *
  389. * @return string
  390. *
  391. * This function returns an array containing all settings pages added. The
  392. * entries are sorted by the key 'order' ascending.
  393. */
  394. public static function getSettingsNavigation() {
  395. $l = \OC::$server->getL10N('lib');
  396. $settings = array();
  397. // by default, settings only contain the help menu
  398. if (OC_Util::getEditionString() === '' &&
  399. OC_Config::getValue('knowledgebaseenabled', true) == true
  400. ) {
  401. $settings = array(
  402. array(
  403. "id" => "help",
  404. "order" => 1000,
  405. "href" => OC_Helper::linkToRoute("settings_help"),
  406. "name" => $l->t("Help"),
  407. "icon" => OC_Helper::imagePath("settings", "help.svg")
  408. )
  409. );
  410. }
  411. // if the user is logged-in
  412. if (OC_User::isLoggedIn()) {
  413. // personal menu
  414. $settings[] = array(
  415. "id" => "personal",
  416. "order" => 1,
  417. "href" => OC_Helper::linkToRoute("settings_personal"),
  418. "name" => $l->t("Personal"),
  419. "icon" => OC_Helper::imagePath("settings", "personal.svg")
  420. );
  421. //SubAdmins are also allowed to access user management
  422. if (OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  423. // admin users menu
  424. $settings[] = array(
  425. "id" => "core_users",
  426. "order" => 2,
  427. "href" => OC_Helper::linkToRoute("settings_users"),
  428. "name" => $l->t("Users"),
  429. "icon" => OC_Helper::imagePath("settings", "users.svg")
  430. );
  431. }
  432. // if the user is an admin
  433. if (OC_User::isAdminUser(OC_User::getUser())) {
  434. // admin settings
  435. $settings[] = array(
  436. "id" => "admin",
  437. "order" => 1000,
  438. "href" => OC_Helper::linkToRoute("settings_admin"),
  439. "name" => $l->t("Admin"),
  440. "icon" => OC_Helper::imagePath("settings", "admin.svg")
  441. );
  442. }
  443. }
  444. $navigation = self::proceedNavigation($settings);
  445. return $navigation;
  446. }
  447. // This is private as well. It simply works, so don't ask for more details
  448. private static function proceedNavigation($list) {
  449. $activeApp = OC::$server->getNavigationManager()->getActiveEntry();
  450. foreach ($list as &$navEntry) {
  451. if ($navEntry['id'] == $activeApp) {
  452. $navEntry['active'] = true;
  453. } else {
  454. $navEntry['active'] = false;
  455. }
  456. }
  457. unset($navEntry);
  458. usort($list, create_function('$a, $b', 'if( $a["order"] == $b["order"] ) {return 0;}elseif( $a["order"] < $b["order"] ) {return -1;}else{return 1;}'));
  459. return $list;
  460. }
  461. /**
  462. * Get the path where to install apps
  463. *
  464. * @return string|false
  465. */
  466. public static function getInstallPath() {
  467. if (OC_Config::getValue('appstoreenabled', true) == false) {
  468. return false;
  469. }
  470. foreach (OC::$APPSROOTS as $dir) {
  471. if (isset($dir['writable']) && $dir['writable'] === true) {
  472. return $dir['path'];
  473. }
  474. }
  475. OC_Log::write('core', 'No application directories are marked as writable.', OC_Log::ERROR);
  476. return null;
  477. }
  478. /**
  479. * search for an app in all app-directories
  480. *
  481. * @param string $appId
  482. * @return mixed (bool|string)
  483. */
  484. protected static function findAppInDirectories($appId) {
  485. static $app_dir = array();
  486. if (isset($app_dir[$appId])) {
  487. return $app_dir[$appId];
  488. }
  489. $possibleApps = array();
  490. foreach (OC::$APPSROOTS as $dir) {
  491. if (file_exists($dir['path'] . '/' . $appId)) {
  492. $possibleApps[] = $dir;
  493. }
  494. }
  495. if (empty($possibleApps)) {
  496. return false;
  497. } elseif (count($possibleApps) === 1) {
  498. $dir = array_shift($possibleApps);
  499. $app_dir[$appId] = $dir;
  500. return $dir;
  501. } else {
  502. $versionToLoad = array();
  503. foreach ($possibleApps as $possibleApp) {
  504. $version = self::getAppVersionByPath($possibleApp['path']);
  505. if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
  506. $versionToLoad = array(
  507. 'dir' => $possibleApp,
  508. 'version' => $version,
  509. );
  510. }
  511. }
  512. $app_dir[$appId] = $versionToLoad['dir'];
  513. return $versionToLoad['dir'];
  514. //TODO - write test
  515. }
  516. }
  517. /**
  518. * Get the directory for the given app.
  519. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  520. *
  521. * @param string $appId
  522. * @return string|false
  523. */
  524. public static function getAppPath($appId) {
  525. if ($appId === null || trim($appId) === '') {
  526. return false;
  527. }
  528. if (($dir = self::findAppInDirectories($appId)) != false) {
  529. return $dir['path'] . '/' . $appId;
  530. }
  531. return false;
  532. }
  533. /**
  534. * check if an app's directory is writable
  535. *
  536. * @param string $appId
  537. * @return bool
  538. */
  539. public static function isAppDirWritable($appId) {
  540. $path = self::getAppPath($appId);
  541. return ($path !== false) ? is_writable($path) : false;
  542. }
  543. /**
  544. * Get the path for the given app on the access
  545. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  546. *
  547. * @param string $appId
  548. * @return string|false
  549. */
  550. public static function getAppWebPath($appId) {
  551. if (($dir = self::findAppInDirectories($appId)) != false) {
  552. return OC::$WEBROOT . $dir['url'] . '/' . $appId;
  553. }
  554. return false;
  555. }
  556. /**
  557. * get the last version of the app, either from appinfo/version or from appinfo/info.xml
  558. *
  559. * @param string $appId
  560. * @return string
  561. */
  562. public static function getAppVersion($appId) {
  563. if (!isset(self::$appVersion[$appId])) {
  564. $file = self::getAppPath($appId);
  565. self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0';
  566. }
  567. return self::$appVersion[$appId];
  568. }
  569. /**
  570. * get app's version based on it's path
  571. *
  572. * @param string $path
  573. * @return string
  574. */
  575. public static function getAppVersionByPath($path) {
  576. $versionFile = $path . '/appinfo/version';
  577. $infoFile = $path . '/appinfo/info.xml';
  578. if (is_file($versionFile)) {
  579. return trim(file_get_contents($versionFile));
  580. } else {
  581. $appData = self::getAppInfo($infoFile, true);
  582. return isset($appData['version']) ? $appData['version'] : '';
  583. }
  584. }
  585. /**
  586. * Read all app metadata from the info.xml file
  587. *
  588. * @param string $appId id of the app or the path of the info.xml file
  589. * @param boolean $path (optional)
  590. * @return array|null
  591. * @note all data is read from info.xml, not just pre-defined fields
  592. */
  593. public static function getAppInfo($appId, $path = false) {
  594. if ($path) {
  595. $file = $appId;
  596. } else {
  597. if (isset(self::$appInfo[$appId])) {
  598. return self::$appInfo[$appId];
  599. }
  600. $file = self::getAppPath($appId) . '/appinfo/info.xml';
  601. }
  602. $parser = new \OC\App\InfoParser(\OC::$server->getHTTPHelper(), \OC::$server->getURLGenerator());
  603. $data = $parser->parse($file);
  604. if (is_array($data)) {
  605. $data = OC_App::parseAppInfo($data);
  606. }
  607. self::$appInfo[$appId] = $data;
  608. return $data;
  609. }
  610. /**
  611. * Returns the navigation
  612. *
  613. * @return array
  614. *
  615. * This function returns an array containing all entries added. The
  616. * entries are sorted by the key 'order' ascending. Additional to the keys
  617. * given for each app the following keys exist:
  618. * - active: boolean, signals if the user is on this navigation entry
  619. */
  620. public static function getNavigation() {
  621. $entries = OC::$server->getNavigationManager()->getAll();
  622. $navigation = self::proceedNavigation($entries);
  623. return $navigation;
  624. }
  625. /**
  626. * get the id of loaded app
  627. *
  628. * @return string
  629. */
  630. public static function getCurrentApp() {
  631. $request = \OC::$server->getRequest();
  632. $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
  633. $topFolder = substr($script, 0, strpos($script, '/'));
  634. if (empty($topFolder)) {
  635. $path_info = $request->getPathInfo();
  636. if ($path_info) {
  637. $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
  638. }
  639. }
  640. if ($topFolder == 'apps') {
  641. $length = strlen($topFolder);
  642. return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
  643. } else {
  644. return $topFolder;
  645. }
  646. }
  647. /**
  648. * @param string $type
  649. * @return array
  650. */
  651. public static function getForms($type) {
  652. $forms = array();
  653. switch ($type) {
  654. case 'admin':
  655. $source = self::$adminForms;
  656. break;
  657. case 'personal':
  658. $source = self::$personalForms;
  659. break;
  660. default:
  661. return array();
  662. }
  663. foreach ($source as $form) {
  664. $forms[] = include $form;
  665. }
  666. return $forms;
  667. }
  668. /**
  669. * register an admin form to be shown
  670. *
  671. * @param string $app
  672. * @param string $page
  673. */
  674. public static function registerAdmin($app, $page) {
  675. self::$adminForms[] = $app . '/' . $page . '.php';
  676. }
  677. /**
  678. * register a personal form to be shown
  679. * @param string $app
  680. * @param string $page
  681. */
  682. public static function registerPersonal($app, $page) {
  683. self::$personalForms[] = $app . '/' . $page . '.php';
  684. }
  685. /**
  686. * @param array $entry
  687. */
  688. public static function registerLogIn(array $entry) {
  689. self::$altLogin[] = $entry;
  690. }
  691. /**
  692. * @return array
  693. */
  694. public static function getAlternativeLogIns() {
  695. return self::$altLogin;
  696. }
  697. /**
  698. * get a list of all apps in the apps folder
  699. *
  700. * @return array an array of app names (string IDs)
  701. * @todo: change the name of this method to getInstalledApps, which is more accurate
  702. */
  703. public static function getAllApps() {
  704. $apps = array();
  705. foreach (OC::$APPSROOTS as $apps_dir) {
  706. if (!is_readable($apps_dir['path'])) {
  707. OC_Log::write('core', 'unable to read app folder : ' . $apps_dir['path'], OC_Log::WARN);
  708. continue;
  709. }
  710. $dh = opendir($apps_dir['path']);
  711. if (is_resource($dh)) {
  712. while (($file = readdir($dh)) !== false) {
  713. if ($file[0] != '.' and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
  714. $apps[] = $file;
  715. }
  716. }
  717. }
  718. }
  719. return $apps;
  720. }
  721. /**
  722. * List all apps, this is used in apps.php
  723. *
  724. * @param bool $onlyLocal
  725. * @return array
  726. */
  727. public static function listAllApps($onlyLocal = false) {
  728. $installedApps = OC_App::getAllApps();
  729. //TODO which apps do we want to blacklist and how do we integrate
  730. // blacklisting with the multi apps folder feature?
  731. $blacklist = array('files'); //we don't want to show configuration for these
  732. $appList = array();
  733. $l = \OC::$server->getL10N('core');
  734. foreach ($installedApps as $app) {
  735. if (array_search($app, $blacklist) === false) {
  736. $info = OC_App::getAppInfo($app);
  737. if (!isset($info['name'])) {
  738. OC_Log::write('core', 'App id "' . $app . '" has no name in appinfo', OC_Log::ERROR);
  739. continue;
  740. }
  741. $enabled = OC_Appconfig::getValue($app, 'enabled', 'no');
  742. $info['groups'] = null;
  743. if ($enabled === 'yes') {
  744. $active = true;
  745. } else if ($enabled === 'no') {
  746. $active = false;
  747. } else {
  748. $active = true;
  749. $info['groups'] = $enabled;
  750. }
  751. $info['active'] = $active;
  752. if (isset($info['shipped']) and ($info['shipped'] == 'true')) {
  753. $info['internal'] = true;
  754. $info['level'] = self::officialApp;
  755. $info['removable'] = false;
  756. } else {
  757. $info['internal'] = false;
  758. $info['removable'] = true;
  759. }
  760. $info['update'] = OC_Installer::isUpdateAvailable($app);
  761. $appIcon = self::getAppPath($app) . '/img/' . $app . '.svg';
  762. if (file_exists($appIcon)) {
  763. $info['preview'] = OC_Helper::imagePath($app, $app . '.svg');
  764. $info['previewAsIcon'] = true;
  765. } else {
  766. $appIcon = self::getAppPath($app) . '/img/app.svg';
  767. if (file_exists($appIcon)) {
  768. $info['preview'] = OC_Helper::imagePath($app, 'app.svg');
  769. $info['previewAsIcon'] = true;
  770. }
  771. }
  772. $info['version'] = OC_App::getAppVersion($app);
  773. $appList[] = $info;
  774. }
  775. }
  776. if ($onlyLocal) {
  777. $remoteApps = [];
  778. } else {
  779. $remoteApps = OC_App::getAppstoreApps();
  780. }
  781. if ($remoteApps) {
  782. // Remove duplicates
  783. foreach ($appList as $app) {
  784. foreach ($remoteApps AS $key => $remote) {
  785. if ($app['name'] === $remote['name'] ||
  786. (isset($app['ocsid']) &&
  787. $app['ocsid'] === $remote['id'])
  788. ) {
  789. unset($remoteApps[$key]);
  790. }
  791. }
  792. }
  793. $combinedApps = array_merge($appList, $remoteApps);
  794. } else {
  795. $combinedApps = $appList;
  796. }
  797. return $combinedApps;
  798. }
  799. /**
  800. * Returns the internal app ID or false
  801. * @param string $ocsID
  802. * @return string|false
  803. */
  804. protected static function getInternalAppIdByOcs($ocsID) {
  805. if(is_numeric($ocsID)) {
  806. $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
  807. if(array_search($ocsID, $idArray)) {
  808. return array_search($ocsID, $idArray);
  809. }
  810. }
  811. return false;
  812. }
  813. /**
  814. * Get a list of all apps on the appstore
  815. * @param string $filter
  816. * @param string $category
  817. * @return array|bool multi-dimensional array of apps.
  818. * Keys: id, name, type, typename, personid, license, detailpage, preview, changed, description
  819. */
  820. public static function getAppstoreApps($filter = 'approved', $category = null) {
  821. $categories = [$category];
  822. $ocsClient = new OCSClient(
  823. \OC::$server->getHTTPClientService(),
  824. \OC::$server->getConfig(),
  825. \OC::$server->getLogger()
  826. );
  827. if (is_null($category)) {
  828. $categoryNames = $ocsClient->getCategories();
  829. if (is_array($categoryNames)) {
  830. // Check that categories of apps were retrieved correctly
  831. if (!$categories = array_keys($categoryNames)) {
  832. return false;
  833. }
  834. } else {
  835. return false;
  836. }
  837. }
  838. $page = 0;
  839. $remoteApps = $ocsClient->getApplications($categories, $page, $filter);
  840. $apps = [];
  841. $i = 0;
  842. $l = \OC::$server->getL10N('core');
  843. foreach ($remoteApps as $app) {
  844. $potentialCleanId = self::getInternalAppIdByOcs($app['id']);
  845. // enhance app info (for example the description)
  846. $apps[$i] = OC_App::parseAppInfo($app);
  847. $apps[$i]['author'] = $app['personid'];
  848. $apps[$i]['ocs_id'] = $app['id'];
  849. $apps[$i]['internal'] = 0;
  850. $apps[$i]['active'] = ($potentialCleanId !== false) ? self::isEnabled($potentialCleanId) : false;
  851. $apps[$i]['update'] = false;
  852. $apps[$i]['groups'] = false;
  853. $apps[$i]['score'] = $app['score'];
  854. $apps[$i]['removable'] = false;
  855. if ($app['label'] == 'recommended') {
  856. $apps[$i]['internallabel'] = (string)$l->t('Recommended');
  857. $apps[$i]['internalclass'] = 'recommendedapp';
  858. }
  859. $i++;
  860. }
  861. if (empty($apps)) {
  862. return false;
  863. } else {
  864. return $apps;
  865. }
  866. }
  867. public static function shouldUpgrade($app) {
  868. $versions = self::getAppVersions();
  869. $currentVersion = OC_App::getAppVersion($app);
  870. if ($currentVersion && isset($versions[$app])) {
  871. $installedVersion = $versions[$app];
  872. if (version_compare($currentVersion, $installedVersion, '>')) {
  873. return true;
  874. }
  875. }
  876. return false;
  877. }
  878. /**
  879. * Adjust the number of version parts of $version1 to match
  880. * the number of version parts of $version2.
  881. *
  882. * @param string $version1 version to adjust
  883. * @param string $version2 version to take the number of parts from
  884. * @return string shortened $version1
  885. */
  886. private static function adjustVersionParts($version1, $version2) {
  887. $version1 = explode('.', $version1);
  888. $version2 = explode('.', $version2);
  889. // reduce $version1 to match the number of parts in $version2
  890. while (count($version1) > count($version2)) {
  891. array_pop($version1);
  892. }
  893. // if $version1 does not have enough parts, add some
  894. while (count($version1) < count($version2)) {
  895. $version1[] = '0';
  896. }
  897. return implode('.', $version1);
  898. }
  899. /**
  900. * Check whether the current ownCloud version matches the given
  901. * application's version requirements.
  902. *
  903. * The comparison is made based on the number of parts that the
  904. * app info version has. For example for ownCloud 6.0.3 if the
  905. * app info version is expecting version 6.0, the comparison is
  906. * made on the first two parts of the ownCloud version.
  907. * This means that it's possible to specify "requiremin" => 6
  908. * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
  909. *
  910. * @param string $ocVersion ownCloud version to check against
  911. * @param array $appInfo app info (from xml)
  912. *
  913. * @return boolean true if compatible, otherwise false
  914. */
  915. public static function isAppCompatible($ocVersion, $appInfo) {
  916. $requireMin = '';
  917. $requireMax = '';
  918. if (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
  919. $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
  920. } else if (isset($appInfo['requiremin'])) {
  921. $requireMin = $appInfo['requiremin'];
  922. } else if (isset($appInfo['require'])) {
  923. $requireMin = $appInfo['require'];
  924. }
  925. if (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
  926. $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
  927. } else if (isset($appInfo['requiremax'])) {
  928. $requireMax = $appInfo['requiremax'];
  929. }
  930. if (is_array($ocVersion)) {
  931. $ocVersion = implode('.', $ocVersion);
  932. }
  933. if (!empty($requireMin)
  934. && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
  935. ) {
  936. return false;
  937. }
  938. if (!empty($requireMax)
  939. && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
  940. ) {
  941. return false;
  942. }
  943. return true;
  944. }
  945. /**
  946. * get the installed version of all apps
  947. */
  948. public static function getAppVersions() {
  949. static $versions;
  950. if (isset($versions)) { // simple cache, needs to be fixed
  951. return $versions; // when function is used besides in checkUpgrade
  952. }
  953. $versions = array();
  954. try {
  955. $query = OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig`'
  956. . ' WHERE `configkey` = \'installed_version\'');
  957. $result = $query->execute();
  958. while ($row = $result->fetchRow()) {
  959. $versions[$row['appid']] = $row['configvalue'];
  960. }
  961. return $versions;
  962. } catch (\Exception $e) {
  963. return array();
  964. }
  965. }
  966. /**
  967. * @param mixed $app
  968. * @return bool
  969. * @throws Exception if app is not compatible with this version of ownCloud
  970. * @throws Exception if no app-name was specified
  971. */
  972. public static function installApp($app) {
  973. $l = \OC::$server->getL10N('core');
  974. $config = \OC::$server->getConfig();
  975. $ocsClient = new OCSClient(
  976. \OC::$server->getHTTPClientService(),
  977. $config,
  978. \OC::$server->getLogger()
  979. );
  980. $appData = $ocsClient->getApplication($app);
  981. // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string
  982. if (!is_numeric($app)) {
  983. $shippedVersion = self::getAppVersion($app);
  984. if ($appData && version_compare($shippedVersion, $appData['version'], '<')) {
  985. $app = self::downloadApp($app);
  986. } else {
  987. $app = OC_Installer::installShippedApp($app);
  988. }
  989. } else {
  990. // Maybe the app is already installed - compare the version in this
  991. // case and use the local already installed one.
  992. // FIXME: This is a horrible hack. I feel sad. The god of code cleanness may forgive me.
  993. $internalAppId = self::getInternalAppIdByOcs($app);
  994. if($internalAppId !== false) {
  995. if($appData && version_compare(\OC_App::getAppVersion($internalAppId), $appData['version'], '<')) {
  996. $app = self::downloadApp($app);
  997. } else {
  998. self::enable($internalAppId);
  999. $app = $internalAppId;
  1000. }
  1001. } else {
  1002. $app = self::downloadApp($app);
  1003. }
  1004. }
  1005. if ($app !== false) {
  1006. // check if the app is compatible with this version of ownCloud
  1007. $info = self::getAppInfo($app);
  1008. $version = OC_Util::getVersion();
  1009. if (!self::isAppCompatible($version, $info)) {
  1010. throw new \Exception(
  1011. $l->t('App "%s" cannot be installed because it is not compatible with this version of ownCloud.',
  1012. array($info['name'])
  1013. )
  1014. );
  1015. }
  1016. // check for required dependencies
  1017. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
  1018. $missing = $dependencyAnalyzer->analyze($app);
  1019. if (!empty($missing)) {
  1020. $missingMsg = join(PHP_EOL, $missing);
  1021. throw new \Exception(
  1022. $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
  1023. array($info['name'], $missingMsg)
  1024. )
  1025. );
  1026. }
  1027. $config->setAppValue($app, 'enabled', 'yes');
  1028. if (isset($appData['id'])) {
  1029. $config->setAppValue($app, 'ocsid', $appData['id']);
  1030. }
  1031. \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
  1032. } else {
  1033. throw new \Exception($l->t("No app name specified"));
  1034. }
  1035. return $app;
  1036. }
  1037. /**
  1038. * update the database for the app and call the update script
  1039. *
  1040. * @param string $appId
  1041. * @return bool
  1042. */
  1043. public static function updateApp($appId) {
  1044. if (file_exists(self::getAppPath($appId) . '/appinfo/database.xml')) {
  1045. OC_DB::updateDbFromStructure(self::getAppPath($appId) . '/appinfo/database.xml');
  1046. }
  1047. unset(self::$appVersion[$appId]);
  1048. if (!self::isEnabled($appId)) {
  1049. return false;
  1050. }
  1051. if (file_exists(self::getAppPath($appId) . '/appinfo/update.php')) {
  1052. self::loadApp($appId, false);
  1053. include self::getAppPath($appId) . '/appinfo/update.php';
  1054. }
  1055. //set remote/public handlers
  1056. $appData = self::getAppInfo($appId);
  1057. if (array_key_exists('ocsid', $appData)) {
  1058. OC_Appconfig::setValue($appId, 'ocsid', $appData['ocsid']);
  1059. }
  1060. foreach ($appData['remote'] as $name => $path) {
  1061. OCP\CONFIG::setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
  1062. }
  1063. foreach ($appData['public'] as $name => $path) {
  1064. OCP\CONFIG::setAppValue('core', 'public_' . $name, $appId . '/' . $path);
  1065. }
  1066. self::setAppTypes($appId);
  1067. $version = \OC_App::getAppVersion($appId);
  1068. \OC_Appconfig::setValue($appId, 'installed_version', $version);
  1069. return true;
  1070. }
  1071. /**
  1072. * @param string $appId
  1073. * @return \OC\Files\View|false
  1074. */
  1075. public static function getStorage($appId) {
  1076. if (OC_App::isEnabled($appId)) { //sanity check
  1077. if (OC_User::isLoggedIn()) {
  1078. $view = new \OC\Files\View('/' . OC_User::getUser());
  1079. if (!$view->file_exists($appId)) {
  1080. $view->mkdir($appId);
  1081. }
  1082. return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
  1083. } else {
  1084. OC_Log::write('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', OC_Log::ERROR);
  1085. return false;
  1086. }
  1087. } else {
  1088. OC_Log::write('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', OC_Log::ERROR);
  1089. return false;
  1090. }
  1091. }
  1092. /**
  1093. * parses the app data array and enhanced the 'description' value
  1094. *
  1095. * @param array $data the app data
  1096. * @return array improved app data
  1097. */
  1098. public static function parseAppInfo(array $data) {
  1099. // just modify the description if it is available
  1100. // otherwise this will create a $data element with an empty 'description'
  1101. if (isset($data['description'])) {
  1102. // sometimes the description contains line breaks and they are then also
  1103. // shown in this way in the app management which isn't wanted as HTML
  1104. // manages line breaks itself
  1105. // first of all we split on empty lines
  1106. $paragraphs = preg_split("!\n[[:space:]]*\n!m", $data['description']);
  1107. $result = [];
  1108. foreach ($paragraphs as $value) {
  1109. // replace multiple whitespace (tabs, space, newlines) inside a paragraph
  1110. // with a single space - also trims whitespace
  1111. $result[] = trim(preg_replace('![[:space:]]+!m', ' ', $value));
  1112. }
  1113. // join the single paragraphs with a empty line in between
  1114. $data['description'] = implode("\n\n", $result);
  1115. }
  1116. return $data;
  1117. }
  1118. }