app.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. /**
  39. * @brief loads all apps
  40. * @param array $types
  41. * @return bool
  42. *
  43. * This function walks through the owncloud directory and loads all apps
  44. * it can find. A directory contains an app if the file /appinfo/app.php
  45. * exists.
  46. *
  47. * if $types is set, only apps of those types will be loaded
  48. */
  49. public static function loadApps($types=null) {
  50. // Load the enabled apps here
  51. $apps = self::getEnabledApps();
  52. // prevent app.php from printing output
  53. ob_start();
  54. foreach( $apps as $app ) {
  55. if((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
  56. self::loadApp($app);
  57. self::$loadedApps[] = $app;
  58. }
  59. }
  60. ob_end_clean();
  61. if (!defined('DEBUG') || !DEBUG) {
  62. if (is_null($types)
  63. && empty(OC_Util::$core_scripts)
  64. && empty(OC_Util::$core_styles)) {
  65. OC_Util::$core_scripts = OC_Util::$scripts;
  66. OC_Util::$scripts = array();
  67. OC_Util::$core_styles = OC_Util::$styles;
  68. OC_Util::$styles = array();
  69. }
  70. }
  71. // return
  72. return true;
  73. }
  74. /**
  75. * load a single app
  76. * @param string $app
  77. */
  78. public static function loadApp($app) {
  79. if(is_file(self::getAppPath($app).'/appinfo/app.php')) {
  80. self::checkUpgrade($app);
  81. require_once $app.'/appinfo/app.php';
  82. }
  83. }
  84. /**
  85. * check if an app is of a specific type
  86. * @param string $app
  87. * @param string/array $types
  88. * @return bool
  89. */
  90. public static function isType($app, $types) {
  91. if(is_string($types)) {
  92. $types=array($types);
  93. }
  94. $appTypes=self::getAppTypes($app);
  95. foreach($types as $type) {
  96. if(array_search($type, $appTypes)!==false) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * get the types of an app
  104. * @param string $app
  105. * @return array
  106. */
  107. private static function getAppTypes($app) {
  108. //load the cache
  109. if(count(self::$appTypes)==0) {
  110. self::$appTypes=OC_Appconfig::getValues(false, 'types');
  111. }
  112. if(isset(self::$appTypes[$app])) {
  113. return explode(',', self::$appTypes[$app]);
  114. }else{
  115. return array();
  116. }
  117. }
  118. /**
  119. * read app types from info.xml and cache them in the database
  120. */
  121. public static function setAppTypes($app) {
  122. $appData=self::getAppInfo($app);
  123. if(isset($appData['types'])) {
  124. $appTypes=implode(',', $appData['types']);
  125. }else{
  126. $appTypes='';
  127. }
  128. OC_Appconfig::setValue($app, 'types', $appTypes);
  129. }
  130. /**
  131. * get all enabled apps
  132. */
  133. public static function getEnabledApps() {
  134. if(!OC_Config::getValue('installed', false))
  135. return array();
  136. $apps=array('files');
  137. $query = OC_DB::prepare( 'SELECT `appid` FROM `*PREFIX*appconfig` WHERE `configkey` = \'enabled\' AND `configvalue`=\'yes\'' );
  138. $result=$query->execute();
  139. while($row=$result->fetchRow()) {
  140. if(array_search($row['appid'], $apps)===false) {
  141. $apps[]=$row['appid'];
  142. }
  143. }
  144. return $apps;
  145. }
  146. /**
  147. * @brief checks whether or not an app is enabled
  148. * @param string $app app
  149. * @return bool
  150. *
  151. * This function checks whether or not an app is enabled.
  152. */
  153. public static function isEnabled( $app ) {
  154. if( 'files'==$app or 'yes' == OC_Appconfig::getValue( $app, 'enabled' )) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * @brief enables an app
  161. * @param mixed $app app
  162. * @return bool
  163. *
  164. * This function set an app as enabled in appconfig.
  165. */
  166. public static function enable( $app ) {
  167. if(!OC_Installer::isInstalled($app)) {
  168. // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string
  169. if(!is_numeric($app)) {
  170. $app = OC_Installer::installShippedApp($app);
  171. }else{
  172. $download=OC_OCSClient::getApplicationDownload($app, 1);
  173. if(isset($download['downloadlink']) and $download['downloadlink']!='') {
  174. $app=OC_Installer::installApp(array('source'=>'http', 'href'=>$download['downloadlink']));
  175. }
  176. }
  177. }
  178. if($app!==false) {
  179. // check if the app is compatible with this version of ownCloud
  180. $info=OC_App::getAppInfo($app);
  181. $version=OC_Util::getVersion();
  182. if(!isset($info['require']) or ($version[0]>$info['require'])) {
  183. OC_Log::write('core', 'App "'.$info['name'].'" can\'t be installed because it is not compatible with this version of ownCloud', OC_Log::ERROR);
  184. return false;
  185. }else{
  186. OC_Appconfig::setValue( $app, 'enabled', 'yes' );
  187. return true;
  188. }
  189. }else{
  190. return false;
  191. }
  192. }
  193. /**
  194. * @brief disables an app
  195. * @param string $app app
  196. * @return bool
  197. *
  198. * This function set an app as disabled in appconfig.
  199. */
  200. public static function disable( $app ) {
  201. // check if app is a shiped app or not. if not delete
  202. OC_Appconfig::setValue( $app, 'enabled', 'no' );
  203. }
  204. /**
  205. * @brief adds an entry to the navigation
  206. * @param string $data array containing the data
  207. * @return bool
  208. *
  209. * This function adds a new entry to the navigation visible to users. $data
  210. * is an associative array.
  211. * The following keys are required:
  212. * - id: unique id for this entry ('addressbook_index')
  213. * - href: link to the page
  214. * - name: Human readable name ('Addressbook')
  215. *
  216. * The following keys are optional:
  217. * - icon: path to the icon of the app
  218. * - order: integer, that influences the position of your application in
  219. * the navigation. Lower values come first.
  220. */
  221. public static function addNavigationEntry( $data ) {
  222. $data['active']=false;
  223. if(!isset($data['icon'])) {
  224. $data['icon']='';
  225. }
  226. OC_App::$navigation[] = $data;
  227. return true;
  228. }
  229. /**
  230. * @brief marks a navigation entry as active
  231. * @param string $id id of the entry
  232. * @return bool
  233. *
  234. * This function sets a navigation entry as active and removes the 'active'
  235. * property from all other entries. The templates can use this for
  236. * highlighting the current position of the user.
  237. */
  238. public static function setActiveNavigationEntry( $id ) {
  239. // load all the apps, to make sure we have all the navigation entries
  240. self::loadApps();
  241. self::$activeapp = $id;
  242. return true;
  243. }
  244. /**
  245. * @brief gets the active Menu entry
  246. * @return string id or empty string
  247. *
  248. * This function returns the id of the active navigation entry (set by
  249. * setActiveNavigationEntry
  250. */
  251. public static function getActiveNavigationEntry() {
  252. return self::$activeapp;
  253. }
  254. /**
  255. * @brief Returns the Settings Navigation
  256. * @return array
  257. *
  258. * This function returns an array containing all settings pages added. The
  259. * entries are sorted by the key 'order' ascending.
  260. */
  261. public static function getSettingsNavigation() {
  262. $l=OC_L10N::get('lib');
  263. $settings = array();
  264. // by default, settings only contain the help menu
  265. if(OC_Config::getValue('knowledgebaseenabled', true)==true) {
  266. $settings = array(
  267. array( "id" => "help", "order" => 1000, "href" => OC_Helper::linkToRoute( "settings_help" ), "name" => $l->t("Help"), "icon" => OC_Helper::imagePath( "settings", "help.svg" ))
  268. );
  269. }
  270. // if the user is logged-in
  271. if (OC_User::isLoggedIn()) {
  272. // personal menu
  273. $settings[] = array( "id" => "personal", "order" => 1, "href" => OC_Helper::linkToRoute( "settings_personal" ), "name" => $l->t("Personal"), "icon" => OC_Helper::imagePath( "settings", "personal.svg" ));
  274. // if there are some settings forms
  275. if(!empty(self::$settingsForms))
  276. // settings menu
  277. $settings[]=array( "id" => "settings", "order" => 1000, "href" => OC_Helper::linkToRoute( "settings_settings" ), "name" => $l->t("Settings"), "icon" => OC_Helper::imagePath( "settings", "settings.svg" ));
  278. //SubAdmins are also allowed to access user management
  279. if(OC_SubAdmin::isSubAdmin($_SESSION["user_id"]) || OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
  280. // admin users menu
  281. $settings[] = array( "id" => "core_users", "order" => 2, "href" => OC_Helper::linkToRoute( "settings_users" ), "name" => $l->t("Users"), "icon" => OC_Helper::imagePath( "settings", "users.svg" ));
  282. }
  283. // if the user is an admin
  284. if(OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
  285. // admin apps menu
  286. $settings[] = array( "id" => "core_apps", "order" => 3, "href" => OC_Helper::linkToRoute( "settings_apps" ).'?installed', "name" => $l->t("Apps"), "icon" => OC_Helper::imagePath( "settings", "apps.svg" ));
  287. $settings[]=array( "id" => "admin", "order" => 1000, "href" => OC_Helper::linkToRoute( "settings_admin" ), "name" => $l->t("Admin"), "icon" => OC_Helper::imagePath( "settings", "admin.svg" ));
  288. }
  289. }
  290. $navigation = self::proceedNavigation($settings);
  291. return $navigation;
  292. }
  293. /// This is private as well. It simply works, so don't ask for more details
  294. private static function proceedNavigation( $list ) {
  295. foreach( $list as &$naventry ) {
  296. if( $naventry['id'] == self::$activeapp ) {
  297. $naventry['active'] = true;
  298. }
  299. else{
  300. $naventry['active'] = false;
  301. }
  302. } unset( $naventry );
  303. usort( $list, create_function( '$a, $b', 'if( $a["order"] == $b["order"] ) {return 0;}elseif( $a["order"] < $b["order"] ) {return -1;}else{return 1;}' ));
  304. return $list;
  305. }
  306. /**
  307. * Get the path where to install apps
  308. */
  309. public static function getInstallPath() {
  310. if(OC_Config::getValue('appstoreenabled', true)==false) {
  311. return false;
  312. }
  313. foreach(OC::$APPSROOTS as $dir) {
  314. if(isset($dir['writable']) && $dir['writable']===true)
  315. return $dir['path'];
  316. }
  317. OC_Log::write('core', 'No application directories are marked as writable.', OC_Log::ERROR);
  318. return null;
  319. }
  320. protected static function findAppInDirectories($appid) {
  321. static $app_dir = array();
  322. if (isset($app_dir[$appid])) {
  323. return $app_dir[$appid];
  324. }
  325. foreach(OC::$APPSROOTS as $dir) {
  326. if(file_exists($dir['path'].'/'.$appid)) {
  327. return $app_dir[$appid]=$dir;
  328. }
  329. }
  330. return false;
  331. }
  332. /**
  333. * Get the directory for the given app.
  334. * If the app is defined in multiple directory, the first one is taken. (false if not found)
  335. */
  336. public static function getAppPath($appid) {
  337. if( ($dir = self::findAppInDirectories($appid)) != false) {
  338. return $dir['path'].'/'.$appid;
  339. }
  340. return false;
  341. }
  342. /**
  343. * Get the path for the given app on the access
  344. * If the app is defined in multiple directory, the first one is taken. (false if not found)
  345. */
  346. public static function getAppWebPath($appid) {
  347. if( ($dir = self::findAppInDirectories($appid)) != false) {
  348. return OC::$WEBROOT.$dir['url'].'/'.$appid;
  349. }
  350. return false;
  351. }
  352. /**
  353. * get the last version of the app, either from appinfo/version or from appinfo/info.xml
  354. */
  355. public static function getAppVersion($appid) {
  356. $file= self::getAppPath($appid).'/appinfo/version';
  357. if(is_file($file) && $version = trim(file_get_contents($file))) {
  358. return $version;
  359. }else{
  360. $appData=self::getAppInfo($appid);
  361. return isset($appData['version'])? $appData['version'] : '';
  362. }
  363. }
  364. /**
  365. * @brief Read all app metadata from the info.xml file
  366. * @param string $appid id of the app or the path of the info.xml file
  367. * @param boolean $path (optional)
  368. * @return array
  369. * @note all data is read from info.xml, not just pre-defined fields
  370. */
  371. public static function getAppInfo($appid, $path=false) {
  372. if($path) {
  373. $file=$appid;
  374. }else{
  375. if(isset(self::$appInfo[$appid])) {
  376. return self::$appInfo[$appid];
  377. }
  378. $file= self::getAppPath($appid).'/appinfo/info.xml';
  379. }
  380. $data=array();
  381. $content=@file_get_contents($file);
  382. if(!$content) {
  383. return null;
  384. }
  385. $xml = new SimpleXMLElement($content);
  386. $data['info']=array();
  387. $data['remote']=array();
  388. $data['public']=array();
  389. foreach($xml->children() as $child) {
  390. /**
  391. * @var $child SimpleXMLElement
  392. */
  393. if($child->getName()=='remote') {
  394. foreach($child->children() as $remote) {
  395. /**
  396. * @var $remote SimpleXMLElement
  397. */
  398. $data['remote'][$remote->getName()]=(string)$remote;
  399. }
  400. }elseif($child->getName()=='public') {
  401. foreach($child->children() as $public) {
  402. /**
  403. * @var $public SimpleXMLElement
  404. */
  405. $data['public'][$public->getName()]=(string)$public;
  406. }
  407. }elseif($child->getName()=='types') {
  408. $data['types']=array();
  409. foreach($child->children() as $type) {
  410. /**
  411. * @var $type SimpleXMLElement
  412. */
  413. $data['types'][]=$type->getName();
  414. }
  415. }elseif($child->getName()=='description') {
  416. $xml=(string)$child->asXML();
  417. $data[$child->getName()]=substr($xml, 13, -14);//script <description> tags
  418. }else{
  419. $data[$child->getName()]=(string)$child;
  420. }
  421. }
  422. self::$appInfo[$appid]=$data;
  423. return $data;
  424. }
  425. /**
  426. * @brief Returns the navigation
  427. * @return array
  428. *
  429. * This function returns an array containing all entries added. The
  430. * entries are sorted by the key 'order' ascending. Additional to the keys
  431. * given for each app the following keys exist:
  432. * - active: boolean, signals if the user is on this navigation entry
  433. */
  434. public static function getNavigation() {
  435. $navigation = self::proceedNavigation( self::$navigation );
  436. return $navigation;
  437. }
  438. /**
  439. * get the id of loaded app
  440. * @return string
  441. */
  442. public static function getCurrentApp() {
  443. $script=substr($_SERVER["SCRIPT_NAME"], strlen(OC::$WEBROOT)+1);
  444. $topFolder=substr($script, 0, strpos($script, '/'));
  445. if (empty($topFolder)) {
  446. $path_info = OC_Request::getPathInfo();
  447. if ($path_info) {
  448. $topFolder=substr($path_info, 1, strpos($path_info, '/', 1)-1);
  449. }
  450. }
  451. if($topFolder=='apps') {
  452. $length=strlen($topFolder);
  453. return substr($script, $length+1, strpos($script, '/', $length+1)-$length-1);
  454. }else{
  455. return $topFolder;
  456. }
  457. }
  458. /**
  459. * get the forms for either settings, admin or personal
  460. */
  461. public static function getForms($type) {
  462. $forms=array();
  463. switch($type) {
  464. case 'settings':
  465. $source=self::$settingsForms;
  466. break;
  467. case 'admin':
  468. $source=self::$adminForms;
  469. break;
  470. case 'personal':
  471. $source=self::$personalForms;
  472. break;
  473. default:
  474. return array();
  475. }
  476. foreach($source as $form) {
  477. $forms[]=include $form;
  478. }
  479. return $forms;
  480. }
  481. /**
  482. * register a settings form to be shown
  483. */
  484. public static function registerSettings($app, $page) {
  485. self::$settingsForms[]= $app.'/'.$page.'.php';
  486. }
  487. /**
  488. * register an admin form to be shown
  489. */
  490. public static function registerAdmin($app, $page) {
  491. self::$adminForms[]= $app.'/'.$page.'.php';
  492. }
  493. /**
  494. * register a personal form to be shown
  495. */
  496. public static function registerPersonal($app, $page) {
  497. self::$personalForms[]= $app.'/'.$page.'.php';
  498. }
  499. /**
  500. * @brief: get a list of all apps in the apps folder
  501. * @return array or app names (string IDs)
  502. * @todo: change the name of this method to getInstalledApps, which is more accurate
  503. */
  504. public static function getAllApps() {
  505. $apps=array();
  506. foreach ( OC::$APPSROOTS as $apps_dir ) {
  507. if(! is_readable($apps_dir['path'])) {
  508. OC_Log::write('core', 'unable to read app folder : ' .$apps_dir['path'], OC_Log::WARN);
  509. continue;
  510. }
  511. $dh = opendir( $apps_dir['path'] );
  512. while( $file = readdir( $dh ) ) {
  513. if ($file[0] != '.' and is_file($apps_dir['path'].'/'.$file.'/appinfo/app.php')) {
  514. $apps[] = $file;
  515. }
  516. }
  517. }
  518. return $apps;
  519. }
  520. /**
  521. * @brief: get a list of all apps on apps.owncloud.com
  522. * @return array, multi-dimensional array of apps. Keys: id, name, type, typename, personid, license, detailpage, preview, changed, description
  523. */
  524. public static function getAppstoreApps( $filter = 'approved' ) {
  525. $catagoryNames = OC_OCSClient::getCategories();
  526. if ( is_array( $catagoryNames ) ) {
  527. // Check that categories of apps were retrieved correctly
  528. if ( ! $categories = array_keys( $catagoryNames ) ) {
  529. return false;
  530. }
  531. $page = 0;
  532. $remoteApps = OC_OCSClient::getApplications( $categories, $page, $filter );
  533. $app1 = array();
  534. $i = 0;
  535. foreach ( $remoteApps as $app ) {
  536. $app1[$i] = $app;
  537. $app1[$i]['author'] = $app['personid'];
  538. $app1[$i]['ocs_id'] = $app['id'];
  539. $app1[$i]['internal'] = $app1[$i]['active'] = 0;
  540. // rating img
  541. if($app['score']>=0 and $app['score']<5) $img=OC_Helper::imagePath( "core", "rating/s1.png" );
  542. elseif($app['score']>=5 and $app['score']<15) $img=OC_Helper::imagePath( "core", "rating/s2.png" );
  543. elseif($app['score']>=15 and $app['score']<25) $img=OC_Helper::imagePath( "core", "rating/s3.png" );
  544. elseif($app['score']>=25 and $app['score']<35) $img=OC_Helper::imagePath( "core", "rating/s4.png" );
  545. elseif($app['score']>=35 and $app['score']<45) $img=OC_Helper::imagePath( "core", "rating/s5.png" );
  546. elseif($app['score']>=45 and $app['score']<55) $img=OC_Helper::imagePath( "core", "rating/s6.png" );
  547. elseif($app['score']>=55 and $app['score']<65) $img=OC_Helper::imagePath( "core", "rating/s7.png" );
  548. elseif($app['score']>=65 and $app['score']<75) $img=OC_Helper::imagePath( "core", "rating/s8.png" );
  549. elseif($app['score']>=75 and $app['score']<85) $img=OC_Helper::imagePath( "core", "rating/s9.png" );
  550. elseif($app['score']>=85 and $app['score']<95) $img=OC_Helper::imagePath( "core", "rating/s10.png" );
  551. elseif($app['score']>=95 and $app['score']<100) $img=OC_Helper::imagePath( "core", "rating/s11.png" );
  552. $app1[$i]['score'] = '<img src="'.$img.'"> Score: '.$app['score'].'%';
  553. $i++;
  554. }
  555. }
  556. if ( empty( $app1 ) ) {
  557. return false;
  558. } else {
  559. return $app1;
  560. }
  561. }
  562. /**
  563. * check if the app need updating and update when needed
  564. */
  565. public static function checkUpgrade($app) {
  566. if (in_array($app, self::$checkedApps)) {
  567. return;
  568. }
  569. self::$checkedApps[] = $app;
  570. $versions = self::getAppVersions();
  571. $currentVersion=OC_App::getAppVersion($app);
  572. if ($currentVersion) {
  573. $installedVersion = $versions[$app];
  574. if (version_compare($currentVersion, $installedVersion, '>')) {
  575. OC_Log::write($app, 'starting app upgrade from '.$installedVersion.' to '.$currentVersion, OC_Log::DEBUG);
  576. try {
  577. OC_App::updateApp($app);
  578. }
  579. catch (Exception $e) {
  580. echo 'Failed to upgrade "'.$app.'". Exception="'.$e->getMessage().'"';
  581. die;
  582. }
  583. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  584. }
  585. }
  586. }
  587. /**
  588. * check if the current enabled apps are compatible with the current
  589. * ownCloud version. disable them if not.
  590. * This is important if you upgrade ownCloud and have non ported 3rd
  591. * party apps installed.
  592. */
  593. public static function checkAppsRequirements($apps = array()) {
  594. if (empty($apps)) {
  595. $apps = OC_App::getEnabledApps();
  596. }
  597. $version = OC_Util::getVersion();
  598. foreach($apps as $app) {
  599. // check if the app is compatible with this version of ownCloud
  600. $info = OC_App::getAppInfo($app);
  601. if(!isset($info['require']) or (($version[0].'.'.$version[1])>$info['require'])) {
  602. OC_Log::write('core', 'App "'.$info['name'].'" ('.$app.') can\'t be used because it is not compatible with this version of ownCloud', OC_Log::ERROR);
  603. OC_App::disable( $app );
  604. }
  605. }
  606. }
  607. /**
  608. * get the installed version of all apps
  609. */
  610. public static function getAppVersions() {
  611. static $versions;
  612. if (isset($versions)) { // simple cache, needs to be fixed
  613. return $versions; // when function is used besides in checkUpgrade
  614. }
  615. $versions=array();
  616. $query = OC_DB::prepare( 'SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = \'installed_version\'' );
  617. $result = $query->execute();
  618. while($row = $result->fetchRow()) {
  619. $versions[$row['appid']]=$row['configvalue'];
  620. }
  621. return $versions;
  622. }
  623. /**
  624. * update the database for the app and call the update script
  625. * @param string $appid
  626. */
  627. public static function updateApp($appid) {
  628. if(file_exists(self::getAppPath($appid).'/appinfo/preupdate.php')) {
  629. self::loadApp($appid);
  630. include self::getAppPath($appid).'/appinfo/preupdate.php';
  631. }
  632. if(file_exists(self::getAppPath($appid).'/appinfo/database.xml')) {
  633. OC_DB::updateDbFromStructure(self::getAppPath($appid).'/appinfo/database.xml');
  634. }
  635. if(!self::isEnabled($appid)) {
  636. return;
  637. }
  638. if(file_exists(self::getAppPath($appid).'/appinfo/update.php')) {
  639. self::loadApp($appid);
  640. include self::getAppPath($appid).'/appinfo/update.php';
  641. }
  642. //set remote/public handlers
  643. $appData=self::getAppInfo($appid);
  644. foreach($appData['remote'] as $name=>$path) {
  645. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $appid.'/'.$path);
  646. }
  647. foreach($appData['public'] as $name=>$path) {
  648. OCP\CONFIG::setAppValue('core', 'public_'.$name, $appid.'/'.$path);
  649. }
  650. self::setAppTypes($appid);
  651. }
  652. /**
  653. * @param string $appid
  654. * @return OC_FilesystemView
  655. */
  656. public static function getStorage($appid) {
  657. if(OC_App::isEnabled($appid)) {//sanity check
  658. if(OC_User::isLoggedIn()) {
  659. $view = new OC_FilesystemView('/'.OC_User::getUser());
  660. if(!$view->file_exists($appid)) {
  661. $view->mkdir($appid);
  662. }
  663. return new OC_FilesystemView('/'.OC_User::getUser().'/'.$appid);
  664. }else{
  665. OC_Log::write('core', 'Can\'t get app storage, app, user not logged in', OC_Log::ERROR);
  666. return false;
  667. }
  668. }else{
  669. OC_Log::write('core', 'Can\'t get app storage, app '.$appid.' not enabled', OC_Log::ERROR);
  670. return false;
  671. }
  672. }
  673. }