app.php 33 KB

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