app.php 27 KB

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