You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.php 27KB

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