app.php 27 KB

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