app.php 27 KB

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