app.php 29 KB

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