app.php 22 KB

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