PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/app.php

https://github.com/sezuan/core
PHP | 979 lines | 775 code | 62 blank | 142 comment | 58 complexity | 790e88dd08826bf7d2430190169d97cb MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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 apps menu
  376. $settings[] = array(
  377. "id" => "core_apps",
  378. "order" => 3,
  379. "href" => OC_Helper::linkToRoute( "settings_apps" ).'?installed',
  380. "name" => $l->t("Apps"),
  381. "icon" => OC_Helper::imagePath( "settings", "apps.svg" )
  382. );
  383. $settings[]=array(
  384. "id" => "admin",
  385. "order" => 1000,
  386. "href" => OC_Helper::linkToRoute( "settings_admin" ),
  387. "name" => $l->t("Admin"),
  388. "icon" => OC_Helper::imagePath( "settings", "admin.svg" )
  389. );
  390. }
  391. }
  392. $navigation = self::proceedNavigation($settings);
  393. return $navigation;
  394. }
  395. // This is private as well. It simply works, so don't ask for more details
  396. private static function proceedNavigation( $list ) {
  397. foreach( $list as &$naventry ) {
  398. if( $naventry['id'] == self::$activeapp ) {
  399. $naventry['active'] = true;
  400. }
  401. else{
  402. $naventry['active'] = false;
  403. }
  404. } unset( $naventry );
  405. usort( $list, create_function( '$a, $b', 'if( $a["order"] == $b["order"] ) {return 0;}elseif( $a["order"] < $b["order"] ) {return -1;}else{return 1;}' ));
  406. return $list;
  407. }
  408. /**
  409. * Get the path where to install apps
  410. */
  411. public static function getInstallPath() {
  412. if(OC_Config::getValue('appstoreenabled', true)==false) {
  413. return false;
  414. }
  415. foreach(OC::$APPSROOTS as $dir) {
  416. if(isset($dir['writable']) && $dir['writable']===true) {
  417. return $dir['path'];
  418. }
  419. }
  420. OC_Log::write('core', 'No application directories are marked as writable.', OC_Log::ERROR);
  421. return null;
  422. }
  423. protected static function findAppInDirectories($appid) {
  424. static $app_dir = array();
  425. if (isset($app_dir[$appid])) {
  426. return $app_dir[$appid];
  427. }
  428. foreach(OC::$APPSROOTS as $dir) {
  429. if(file_exists($dir['path'].'/'.$appid)) {
  430. return $app_dir[$appid]=$dir;
  431. }
  432. }
  433. return false;
  434. }
  435. /**
  436. * Get the directory for the given app.
  437. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  438. */
  439. public static function getAppPath($appid) {
  440. if( ($dir = self::findAppInDirectories($appid)) != false) {
  441. return $dir['path'].'/'.$appid;
  442. }
  443. return false;
  444. }
  445. /**
  446. * Get the path for the given app on the access
  447. * If the app is defined in multiple directories, the first one is taken. (false if not found)
  448. */
  449. public static function getAppWebPath($appid) {
  450. if( ($dir = self::findAppInDirectories($appid)) != false) {
  451. return OC::$WEBROOT.$dir['url'].'/'.$appid;
  452. }
  453. return false;
  454. }
  455. /**
  456. * get the last version of the app, either from appinfo/version or from appinfo/info.xml
  457. */
  458. public static function getAppVersion($appid) {
  459. $file= self::getAppPath($appid).'/appinfo/version';
  460. if(is_file($file) && $version = trim(file_get_contents($file))) {
  461. return $version;
  462. }else{
  463. $appData=self::getAppInfo($appid);
  464. return isset($appData['version'])? $appData['version'] : '';
  465. }
  466. }
  467. /**
  468. * @brief Read all app metadata from the info.xml file
  469. * @param string $appid id of the app or the path of the info.xml file
  470. * @param boolean $path (optional)
  471. * @return array
  472. * @note all data is read from info.xml, not just pre-defined fields
  473. */
  474. public static function getAppInfo($appid, $path=false) {
  475. if($path) {
  476. $file=$appid;
  477. }else{
  478. if(isset(self::$appInfo[$appid])) {
  479. return self::$appInfo[$appid];
  480. }
  481. $file= self::getAppPath($appid).'/appinfo/info.xml';
  482. }
  483. $data=array();
  484. $content=@file_get_contents($file);
  485. if(!$content) {
  486. return null;
  487. }
  488. $xml = new SimpleXMLElement($content);
  489. $data['info']=array();
  490. $data['remote']=array();
  491. $data['public']=array();
  492. foreach($xml->children() as $child) {
  493. /**
  494. * @var $child SimpleXMLElement
  495. */
  496. if($child->getName()=='remote') {
  497. foreach($child->children() as $remote) {
  498. /**
  499. * @var $remote SimpleXMLElement
  500. */
  501. $data['remote'][$remote->getName()]=(string)$remote;
  502. }
  503. }elseif($child->getName()=='public') {
  504. foreach($child->children() as $public) {
  505. /**
  506. * @var $public SimpleXMLElement
  507. */
  508. $data['public'][$public->getName()]=(string)$public;
  509. }
  510. }elseif($child->getName()=='types') {
  511. $data['types']=array();
  512. foreach($child->children() as $type) {
  513. /**
  514. * @var $type SimpleXMLElement
  515. */
  516. $data['types'][]=$type->getName();
  517. }
  518. }elseif($child->getName()=='description') {
  519. $xml=(string)$child->asXML();
  520. $data[$child->getName()]=substr($xml, 13, -14);//script <description> tags
  521. }else{
  522. $data[$child->getName()]=(string)$child;
  523. }
  524. }
  525. self::$appInfo[$appid]=$data;
  526. return $data;
  527. }
  528. /**
  529. * @brief Returns the navigation
  530. * @return array
  531. *
  532. * This function returns an array containing all entries added. The
  533. * entries are sorted by the key 'order' ascending. Additional to the keys
  534. * given for each app the following keys exist:
  535. * - active: boolean, signals if the user is on this navigation entry
  536. */
  537. public static function getNavigation() {
  538. $navigation = self::proceedNavigation( self::$navigation );
  539. return $navigation;
  540. }
  541. /**
  542. * get the id of loaded app
  543. * @return string
  544. */
  545. public static function getCurrentApp() {
  546. $script=substr(OC_Request::scriptName(), strlen(OC::$WEBROOT)+1);
  547. $topFolder=substr($script, 0, strpos($script, '/'));
  548. if (empty($topFolder)) {
  549. $path_info = OC_Request::getPathInfo();
  550. if ($path_info) {
  551. $topFolder=substr($path_info, 1, strpos($path_info, '/', 1)-1);
  552. }
  553. }
  554. if($topFolder=='apps') {
  555. $length=strlen($topFolder);
  556. return substr($script, $length+1, strpos($script, '/', $length+1)-$length-1);
  557. }else{
  558. return $topFolder;
  559. }
  560. }
  561. /**
  562. * get the forms for either settings, admin or personal
  563. */
  564. public static function getForms($type) {
  565. $forms=array();
  566. switch($type) {
  567. case 'settings':
  568. $source=self::$settingsForms;
  569. break;
  570. case 'admin':
  571. $source=self::$adminForms;
  572. break;
  573. case 'personal':
  574. $source=self::$personalForms;
  575. break;
  576. default:
  577. return array();
  578. }
  579. foreach($source as $form) {
  580. $forms[]=include $form;
  581. }
  582. return $forms;
  583. }
  584. /**
  585. * register a settings form to be shown
  586. */
  587. public static function registerSettings($app, $page) {
  588. self::$settingsForms[]= $app.'/'.$page.'.php';
  589. }
  590. /**
  591. * register an admin form to be shown
  592. */
  593. public static function registerAdmin($app, $page) {
  594. self::$adminForms[]= $app.'/'.$page.'.php';
  595. }
  596. /**
  597. * register a personal form to be shown
  598. */
  599. public static function registerPersonal($app, $page) {
  600. self::$personalForms[]= $app.'/'.$page.'.php';
  601. }
  602. public static function registerLogIn($entry) {
  603. self::$altLogin[] = $entry;
  604. }
  605. public static function getAlternativeLogIns() {
  606. return self::$altLogin;
  607. }
  608. /**
  609. * @brief: get a list of all apps in the apps folder
  610. * @return array or app names (string IDs)
  611. * @todo: change the name of this method to getInstalledApps, which is more accurate
  612. */
  613. public static function getAllApps() {
  614. $apps=array();
  615. foreach ( OC::$APPSROOTS as $apps_dir ) {
  616. if(! is_readable($apps_dir['path'])) {
  617. OC_Log::write('core', 'unable to read app folder : ' .$apps_dir['path'], OC_Log::WARN);
  618. continue;
  619. }
  620. $dh = opendir( $apps_dir['path'] );
  621. while( $file = readdir( $dh ) ) {
  622. if ($file[0] != '.' and is_file($apps_dir['path'].'/'.$file.'/appinfo/app.php')) {
  623. $apps[] = $file;
  624. }
  625. }
  626. }
  627. return $apps;
  628. }
  629. /**
  630. * @brief: Lists all apps, this is used in apps.php
  631. * @return array
  632. */
  633. public static function listAllApps() {
  634. $installedApps = OC_App::getAllApps();
  635. //TODO which apps do we want to blacklist and how do we integrate
  636. // blacklisting with the multi apps folder feature?
  637. $blacklist = array('files');//we dont want to show configuration for these
  638. $appList = array();
  639. foreach ( $installedApps as $app ) {
  640. if ( array_search( $app, $blacklist ) === false ) {
  641. $info=OC_App::getAppInfo($app);
  642. if (!isset($info['name'])) {
  643. OC_Log::write('core', 'App id "'.$app.'" has no name in appinfo', OC_Log::ERROR);
  644. continue;
  645. }
  646. if ( OC_Appconfig::getValue( $app, 'enabled', 'no') == 'yes' ) {
  647. $active = true;
  648. } else {
  649. $active = false;
  650. }
  651. $info['active'] = $active;
  652. if(isset($info['shipped']) and ($info['shipped']=='true')) {
  653. $info['internal']=true;
  654. $info['internallabel']='Internal App';
  655. $info['internalclass']='';
  656. $info['update']=false;
  657. } else {
  658. $info['internal']=false;
  659. $info['internallabel']='3rd Party';
  660. $info['internalclass']='externalapp';
  661. $info['update']=OC_Installer::isUpdateAvailable($app);
  662. }
  663. $info['preview'] = OC_Helper::imagePath('settings', 'trans.png');
  664. $info['version'] = OC_App::getAppVersion($app);
  665. $appList[] = $info;
  666. }
  667. }
  668. $remoteApps = OC_App::getAppstoreApps();
  669. if ( $remoteApps ) {
  670. // Remove duplicates
  671. foreach ( $appList as $app ) {
  672. foreach ( $remoteApps AS $key => $remote ) {
  673. if (
  674. $app['name'] == $remote['name']
  675. // To set duplicate detection to use OCS ID instead of string name,
  676. // enable this code, remove the line of code above,
  677. // and add <ocs_id>[ID]</ocs_id> to info.xml of each 3rd party app:
  678. // OR $app['ocs_id'] == $remote['ocs_id']
  679. ) {
  680. unset( $remoteApps[$key]);
  681. }
  682. }
  683. }
  684. $combinedApps = array_merge( $appList, $remoteApps );
  685. } else {
  686. $combinedApps = $appList;
  687. }
  688. return $combinedApps;
  689. }
  690. /**
  691. * @brief: get a list of all apps on apps.owncloud.com
  692. * @return array, multi-dimensional array of apps.
  693. * Keys: id, name, type, typename, personid, license, detailpage, preview, changed, description
  694. */
  695. public static function getAppstoreApps( $filter = 'approved' ) {
  696. $categoryNames = OC_OCSClient::getCategories();
  697. if ( is_array( $categoryNames ) ) {
  698. // Check that categories of apps were retrieved correctly
  699. if ( ! $categories = array_keys( $categoryNames ) ) {
  700. return false;
  701. }
  702. $page = 0;
  703. $remoteApps = OC_OCSClient::getApplications( $categories, $page, $filter );
  704. $app1 = array();
  705. $i = 0;
  706. foreach ( $remoteApps as $app ) {
  707. $app1[$i] = $app;
  708. $app1[$i]['author'] = $app['personid'];
  709. $app1[$i]['ocs_id'] = $app['id'];
  710. $app1[$i]['internal'] = $app1[$i]['active'] = 0;
  711. $app1[$i]['update'] = false;
  712. if($app['label']=='recommended') {
  713. $app1[$i]['internallabel'] = 'Recommended';
  714. $app1[$i]['internalclass'] = 'recommendedapp';
  715. }else{
  716. $app1[$i]['internallabel'] = '3rd Party';
  717. $app1[$i]['internalclass'] = 'externalapp';
  718. }
  719. // rating img
  720. if($app['score']>=0 and $app['score']<5) $img=OC_Helper::imagePath( "core", "rating/s1.png" );
  721. elseif($app['score']>=5 and $app['score']<15) $img=OC_Helper::imagePath( "core", "rating/s2.png" );
  722. elseif($app['score']>=15 and $app['score']<25) $img=OC_Helper::imagePath( "core", "rating/s3.png" );
  723. elseif($app['score']>=25 and $app['score']<35) $img=OC_Helper::imagePath( "core", "rating/s4.png" );
  724. elseif($app['score']>=35 and $app['score']<45) $img=OC_Helper::imagePath( "core", "rating/s5.png" );
  725. elseif($app['score']>=45 and $app['score']<55) $img=OC_Helper::imagePath( "core", "rating/s6.png" );
  726. elseif($app['score']>=55 and $app['score']<65) $img=OC_Helper::imagePath( "core", "rating/s7.png" );
  727. elseif($app['score']>=65 and $app['score']<75) $img=OC_Helper::imagePath( "core", "rating/s8.png" );
  728. elseif($app['score']>=75 and $app['score']<85) $img=OC_Helper::imagePath( "core", "rating/s9.png" );
  729. elseif($app['score']>=85 and $app['score']<95) $img=OC_Helper::imagePath( "core", "rating/s10.png" );
  730. elseif($app['score']>=95 and $app['score']<100) $img=OC_Helper::imagePath( "core", "rating/s11.png" );
  731. $app1[$i]['score'] = '<img src="'.$img.'"> Score: '.$app['score'].'%';
  732. $i++;
  733. }
  734. }
  735. if ( empty( $app1 ) ) {
  736. return false;
  737. } else {
  738. return $app1;
  739. }
  740. }
  741. /**
  742. * check if the app needs updating and update when needed
  743. */
  744. public static function checkUpgrade($app) {
  745. if (in_array($app, self::$checkedApps)) {
  746. return;
  747. }
  748. self::$checkedApps[] = $app;
  749. $versions = self::getAppVersions();
  750. $currentVersion=OC_App::getAppVersion($app);
  751. if ($currentVersion) {
  752. $installedVersion = $versions[$app];
  753. if (version_compare($currentVersion, $installedVersion, '>')) {
  754. $info = self::getAppInfo($app);
  755. OC_Log::write($app,
  756. 'starting app upgrade from '.$installedVersion.' to '.$currentVersion,
  757. OC_Log::DEBUG);
  758. try {
  759. OC_App::updateApp($app);
  760. OC_Hook::emit('update', 'success', 'Updated '.$info['name'].' app');
  761. }
  762. catch (Exception $e) {
  763. echo 'Failed to upgrade "'.$app.'". Exception="'.$e->getMessage().'"';
  764. OC_Hook::emit('update', 'failure', 'Failed to update '.$info['name'].' app: '.$e->getMessage());
  765. die;
  766. }
  767. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  768. }
  769. }
  770. }
  771. /**
  772. * check if the current enabled apps are compatible with the current
  773. * ownCloud version. disable them if not.
  774. * This is important if you upgrade ownCloud and have non ported 3rd
  775. * party apps installed.
  776. */
  777. public static function checkAppsRequirements($apps = array()) {
  778. if (empty($apps)) {
  779. $apps = OC_App::getEnabledApps();
  780. }
  781. $version = OC_Util::getVersion();
  782. foreach($apps as $app) {
  783. // check if the app is compatible with this version of ownCloud
  784. $info = OC_App::getAppInfo($app);
  785. if(!isset($info['require']) or !self::isAppVersionCompatible($version, $info['require'])) {
  786. OC_Log::write('core',
  787. 'App "'.$info['name'].'" ('.$app.') can\'t be used because it is'
  788. .' not compatible with this version of ownCloud',
  789. OC_Log::ERROR);
  790. OC_App::disable( $app );
  791. OC_Hook::emit('update', 'success', 'Disabled '.$info['name'].' app because it is not compatible');
  792. }
  793. }
  794. }
  795. /**
  796. * Compares the app version with the owncloud version to see if the app
  797. * requires a newer version than the currently active one
  798. * @param array $owncloudVersions array with 3 entries: major minor bugfix
  799. * @param string $appRequired the required version from the xml
  800. * major.minor.bugfix
  801. * @return boolean true if compatible, otherwise false
  802. */
  803. public static function isAppVersionCompatible($owncloudVersions, $appRequired){
  804. $appVersions = explode('.', $appRequired);
  805. for($i=0; $i<count($appVersions); $i++){
  806. $appVersion = (int) $appVersions[$i];
  807. if(isset($owncloudVersions[$i])){
  808. $owncloudVersion = $owncloudVersions[$i];
  809. } else {
  810. $owncloudVersion = 0;
  811. }
  812. if($owncloudVersion < $appVersion){
  813. return false;
  814. } elseif ($owncloudVersion > $appVersion) {
  815. return true;
  816. }
  817. }
  818. return true;
  819. }
  820. /**
  821. * get the installed version of all apps
  822. */
  823. public static function getAppVersions() {
  824. static $versions;
  825. if (isset($versions)) { // simple cache, needs to be fixed
  826. return $versions; // when function is used besides in checkUpgrade
  827. }
  828. $versions=array();
  829. $query = OC_DB::prepare( 'SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig`'
  830. .' WHERE `configkey` = \'installed_version\'' );
  831. $result = $query->execute();
  832. while($row = $result->fetchRow()) {
  833. $versions[$row['appid']]=$row['configvalue'];
  834. }
  835. return $versions;
  836. }
  837. /**
  838. * update the database for the app and call the update script
  839. * @param string $appid
  840. */
  841. public static function updateApp($appid) {
  842. if(file_exists(self::getAppPath($appid).'/appinfo/preupdate.php')) {
  843. self::loadApp($appid);
  844. include self::getAppPath($appid).'/appinfo/preupdate.php';
  845. }
  846. if(file_exists(self::getAppPath($appid).'/appinfo/database.xml')) {
  847. OC_DB::updateDbFromStructure(self::getAppPath($appid).'/appinfo/database.xml');
  848. }
  849. if(!self::isEnabled($appid)) {
  850. return;
  851. }
  852. if(file_exists(self::getAppPath($appid).'/appinfo/update.php')) {
  853. self::loadApp($appid);
  854. include self::getAppPath($appid).'/appinfo/update.php';
  855. }
  856. //set remote/public handlers
  857. $appData=self::getAppInfo($appid);
  858. foreach($appData['remote'] as $name=>$path) {
  859. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $appid.'/'.$path);
  860. }
  861. foreach($appData['public'] as $name=>$path) {
  862. OCP\CONFIG::setAppValue('core', 'public_'.$name, $appid.'/'.$path);
  863. }
  864. self::setAppTypes($appid);
  865. }
  866. /**
  867. * @param string $appid
  868. * @return \OC\Files\View
  869. */
  870. public static function getStorage($appid) {
  871. if(OC_App::isEnabled($appid)) {//sanity check
  872. if(OC_User::isLoggedIn()) {
  873. $view = new \OC\Files\View('/'.OC_User::getUser());
  874. if(!$view->file_exists($appid)) {
  875. $view->mkdir($appid);
  876. }
  877. return new \OC\Files\View('/'.OC_User::getUser().'/'.$appid);
  878. }else{
  879. OC_Log::write('core', 'Can\'t get app storage, app '.$appid.', user not logged in', OC_Log::ERROR);
  880. return false;
  881. }
  882. }else{
  883. OC_Log::write('core', 'Can\'t get app storage, app '.$appid.' not enabled', OC_Log::ERROR);
  884. return false;
  885. }
  886. }
  887. }