You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

646 lines
19 KiB

14 years ago
  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. * @returns true/false
  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. */
  89. public static function isType($app,$types) {
  90. if(is_string($types)) {
  91. $types=array($types);
  92. }
  93. $appTypes=self::getAppTypes($app);
  94. foreach($types as $type) {
  95. if(array_search($type, $appTypes)!==false) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. * get the types of an app
  103. * @param string $app
  104. * @return array
  105. */
  106. private static function getAppTypes($app) {
  107. //load the cache
  108. if(count(self::$appTypes)==0) {
  109. self::$appTypes=OC_Appconfig::getValues(false, 'types');
  110. }
  111. if(isset(self::$appTypes[$app])) {
  112. return explode(',', self::$appTypes[$app]);
  113. }else{
  114. return array();
  115. }
  116. }
  117. /**
  118. * read app types from info.xml and cache them in the database
  119. */
  120. public static function setAppTypes($app) {
  121. $appData=self::getAppInfo($app);
  122. if(isset($appData['types'])) {
  123. $appTypes=implode(',', $appData['types']);
  124. }else{
  125. $appTypes='';
  126. }
  127. OC_Appconfig::setValue($app, 'types', $appTypes);
  128. }
  129. /**
  130. * get all enabled apps
  131. */
  132. public static function getEnabledApps() {
  133. if(!OC_Config::getValue('installed', false))
  134. return array();
  135. $apps=array('files');
  136. $query = OC_DB::prepare( 'SELECT `appid` FROM `*PREFIX*appconfig` WHERE `configkey` = \'enabled\' AND `configvalue`=\'yes\'' );
  137. $result=$query->execute();
  138. while($row=$result->fetchRow()) {
  139. if(array_search($row['appid'], $apps)===false) {
  140. $apps[]=$row['appid'];
  141. }
  142. }
  143. return $apps;
  144. }
  145. /**
  146. * @brief checks whether or not an app is enabled
  147. * @param $app app
  148. * @returns true/false
  149. *
  150. * This function checks whether or not an app is enabled.
  151. */
  152. public static function isEnabled( $app ) {
  153. if( 'files'==$app or 'yes' == OC_Appconfig::getValue( $app, 'enabled' )) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * @brief enables an app
  160. * @param $app app
  161. * @returns true/false
  162. *
  163. * This function set an app as enabled in appconfig.
  164. */
  165. public static function enable( $app ) {
  166. if(!OC_Installer::isInstalled($app)) {
  167. // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string
  168. if(!is_numeric($app)) {
  169. $app = OC_Installer::installShippedApp($app);
  170. }else{
  171. $download=OC_OCSClient::getApplicationDownload($app, 1);
  172. if(isset($download['downloadlink']) and $download['downloadlink']!='') {
  173. $app=OC_Installer::installApp(array('source'=>'http','href'=>$download['downloadlink']));
  174. }
  175. }
  176. }
  177. if($app!==false) {
  178. // check if the app is compatible with this version of ownCloud
  179. $info=OC_App::getAppInfo($app);
  180. $version=OC_Util::getVersion();
  181. if(!isset($info['require']) or ($version[0]>$info['require'])) {
  182. OC_Log::write('core', 'App "'.$info['name'].'" can\'t be installed because it is not compatible with this version of ownCloud', OC_Log::ERROR);
  183. return false;
  184. }else{
  185. OC_Appconfig::setValue( $app, 'enabled', 'yes' );
  186. return true;
  187. }
  188. }else{
  189. return false;
  190. }
  191. return $app;
  192. }
  193. /**
  194. * @brief disables an app
  195. * @param $app app
  196. * @returns true/false
  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 $data array containing the data
  207. * @returns true/false
  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 $id id of the entry
  232. * @returns true/false
  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. self::$activeapp = $id;
  240. return true;
  241. }
  242. /**
  243. * @brief gets the active Menu entry
  244. * @returns id or empty string
  245. *
  246. * This function returns the id of the active navigation entry (set by
  247. * setActiveNavigationEntry
  248. */
  249. public static function getActiveNavigationEntry() {
  250. return self::$activeapp;
  251. }
  252. /**
  253. * @brief Returns the Settings Navigation
  254. * @returns associative array
  255. *
  256. * This function returns an array containing all settings pages added. The
  257. * entries are sorted by the key 'order' ascending.
  258. */
  259. public static function getSettingsNavigation() {
  260. $l=OC_L10N::get('lib');
  261. $settings = array();
  262. // by default, settings only contain the help menu
  263. if(OC_Config::getValue('knowledgebaseenabled', true)==true) {
  264. $settings = array(
  265. array( "id" => "help", "order" => 1000, "href" => OC_Helper::linkTo( "settings", "help.php" ), "name" => $l->t("Help"), "icon" => OC_Helper::imagePath( "settings", "help.svg" ))
  266. );
  267. }
  268. // if the user is logged-in
  269. if (OC_User::isLoggedIn()) {
  270. // personal menu
  271. $settings[] = array( "id" => "personal", "order" => 1, "href" => OC_Helper::linkTo( "settings", "personal.php" ), "name" => $l->t("Personal"), "icon" => OC_Helper::imagePath( "settings", "personal.svg" ));
  272. // if there're some settings forms
  273. if(!empty(self::$settingsForms))
  274. // settings menu
  275. $settings[]=array( "id" => "settings", "order" => 1000, "href" => OC_Helper::linkTo( "settings", "settings.php" ), "name" => $l->t("Settings"), "icon" => OC_Helper::imagePath( "settings", "settings.svg" ));
  276. //SubAdmins are also allowed to access user management
  277. if(OC_SubAdmin::isSubAdmin($_SESSION["user_id"]) || OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
  278. // admin users menu
  279. $settings[] = array( "id" => "core_users", "order" => 2, "href" => OC_Helper::linkTo( "settings", "users.php" ), "name" => $l->t("Users"), "icon" => OC_Helper::imagePath( "settings", "users.svg" ));
  280. }
  281. // if the user is an admin
  282. if(OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
  283. // admin apps menu
  284. $settings[] = array( "id" => "core_apps", "order" => 3, "href" => OC_Helper::linkTo( "settings", "apps.php" ).'?installed', "name" => $l->t("Apps"), "icon" => OC_Helper::imagePath( "settings", "apps.svg" ));
  285. $settings[]=array( "id" => "admin", "order" => 1000, "href" => OC_Helper::linkTo( "settings", "admin.php" ), "name" => $l->t("Admin"), "icon" => OC_Helper::imagePath( "settings", "admin.svg" ));
  286. }
  287. }
  288. $navigation = self::proceedNavigation($settings);
  289. return $navigation;
  290. }
  291. /// This is private as well. It simply works, so don't ask for more details
  292. private static function proceedNavigation( $list ) {
  293. foreach( $list as &$naventry ) {
  294. $naventry['subnavigation'] = array();
  295. if( $naventry['id'] == self::$activeapp ) {
  296. $naventry['active'] = true;
  297. }
  298. else{
  299. $naventry['active'] = false;
  300. }
  301. } unset( $naventry );
  302. usort( $list, create_function( '$a, $b', 'if( $a["order"] == $b["order"] ) {return 0;}elseif( $a["order"] < $b["order"] ) {return -1;}else{return 1;}' ));
  303. return $list;
  304. }
  305. /**
  306. * Get the path where to install apps
  307. */
  308. public static function getInstallPath() {
  309. if(OC_Config::getValue('appstoreenabled', true)==false) {
  310. return false;
  311. }
  312. foreach(OC::$APPSROOTS as $dir) {
  313. if(isset($dir['writable']) && $dir['writable']===true)
  314. return $dir['path'];
  315. }
  316. OC_Log::write('core', 'No application directories are marked as writable.', OC_Log::ERROR);
  317. return null;
  318. }
  319. protected static function findAppInDirectories($appid) {
  320. static $app_dir = array();
  321. if (isset($app_dir[$appid])) {
  322. return $app_dir[$appid];
  323. }
  324. foreach(OC::$APPSROOTS as $dir) {
  325. if(file_exists($dir['path'].'/'.$appid)) {
  326. return $app_dir[$appid]=$dir;
  327. }
  328. }
  329. }
  330. /**
  331. * Get the directory for the given app.
  332. * If the app is defined in multiple directory, the first one is taken. (false if not found)
  333. */
  334. public static function getAppPath($appid) {
  335. if( ($dir = self::findAppInDirectories($appid)) != false) {
  336. return $dir['path'].'/'.$appid;
  337. }
  338. }
  339. /**
  340. * Get the path for the given app on the access
  341. * If the app is defined in multiple directory, the first one is taken. (false if not found)
  342. */
  343. public static function getAppWebPath($appid) {
  344. if( ($dir = self::findAppInDirectories($appid)) != false) {
  345. return OC::$WEBROOT.$dir['url'].'/'.$appid;
  346. }
  347. }
  348. /**
  349. * get the last version of the app, either from appinfo/version or from appinfo/info.xml
  350. */
  351. public static function getAppVersion($appid) {
  352. $file= self::getAppPath($appid).'/appinfo/version';
  353. $version=@file_get_contents($file);
  354. if($version) {
  355. return trim($version);
  356. }else{
  357. $appData=self::getAppInfo($appid);
  358. return isset($appData['version'])? $appData['version'] : '';
  359. }
  360. }
  361. /**
  362. * @brief Read app metadata from the info.xml file
  363. * @param string $appid id of the app or the path of the info.xml file
  364. * @param boolean path (optional)
  365. * @returns array
  366. */
  367. public static function getAppInfo($appid,$path=false) {
  368. if($path) {
  369. $file=$appid;
  370. }else{
  371. if(isset(self::$appInfo[$appid])) {
  372. return self::$appInfo[$appid];
  373. }
  374. $file= self::getAppPath($appid).'/appinfo/info.xml';
  375. }
  376. $data=array();
  377. $content=@file_get_contents($file);
  378. if(!$content) {
  379. return;
  380. }
  381. $xml = new SimpleXMLElement($content);
  382. $data['info']=array();
  383. $data['remote']=array();
  384. $data['public']=array();
  385. foreach($xml->children() as $child) {
  386. if($child->getName()=='remote') {
  387. foreach($child->children() as $remote) {
  388. $data['remote'][$remote->getName()]=(string)$remote;
  389. }
  390. }elseif($child->getName()=='public') {
  391. foreach($child->children() as $public) {
  392. $data['public'][$public->getName()]=(string)$public;
  393. }
  394. }elseif($child->getName()=='types') {
  395. $data['types']=array();
  396. foreach($child->children() as $type) {
  397. $data['types'][]=$type->getName();
  398. }
  399. }elseif($child->getName()=='description') {
  400. $xml=(string)$child->asXML();
  401. $data[$child->getName()]=substr($xml, 13, -14);//script <description> tags
  402. }else{
  403. $data[$child->getName()]=(string)$child;
  404. }
  405. }
  406. self::$appInfo[$appid]=$data;
  407. return $data;
  408. }
  409. /**
  410. * @brief Returns the navigation
  411. * @returns associative array
  412. *
  413. * This function returns an array containing all entries added. The
  414. * entries are sorted by the key 'order' ascending. Additional to the keys
  415. * given for each app the following keys exist:
  416. * - active: boolean, signals if the user is on this navigation entry
  417. * - children: array that is empty if the key 'active' is false or
  418. * contains the subentries if the key 'active' is true
  419. */
  420. public static function getNavigation() {
  421. $navigation = self::proceedNavigation( self::$navigation );
  422. return $navigation;
  423. }
  424. /**
  425. * get the id of loaded app
  426. * @return string
  427. */
  428. public static function getCurrentApp() {
  429. $script=substr($_SERVER["SCRIPT_NAME"], strlen(OC::$WEBROOT)+1);
  430. $topFolder=substr($script, 0, strpos($script, '/'));
  431. if($topFolder=='apps') {
  432. $length=strlen($topFolder);
  433. return substr($script, $length+1, strpos($script, '/', $length+1)-$length-1);
  434. }else{
  435. return $topFolder;
  436. }
  437. }
  438. /**
  439. * get the forms for either settings, admin or personal
  440. */
  441. public static function getForms($type) {
  442. $forms=array();
  443. switch($type) {
  444. case 'settings':
  445. $source=self::$settingsForms;
  446. break;
  447. case 'admin':
  448. $source=self::$adminForms;
  449. break;
  450. case 'personal':
  451. $source=self::$personalForms;
  452. break;
  453. }
  454. foreach($source as $form) {
  455. $forms[]=include $form;
  456. }
  457. return $forms;
  458. }
  459. /**
  460. * register a settings form to be shown
  461. */
  462. public static function registerSettings($app,$page) {
  463. self::$settingsForms[]= $app.'/'.$page.'.php';
  464. }
  465. /**
  466. * register an admin form to be shown
  467. */
  468. public static function registerAdmin($app,$page) {
  469. self::$adminForms[]= $app.'/'.$page.'.php';
  470. }
  471. /**
  472. * register a personal form to be shown
  473. */
  474. public static function registerPersonal($app,$page) {
  475. self::$personalForms[]= $app.'/'.$page.'.php';
  476. }
  477. /**
  478. * get a list of all apps in the apps folder
  479. */
  480. public static function getAllApps() {
  481. $apps=array();
  482. foreach(OC::$APPSROOTS as $apps_dir) {
  483. $dh=opendir($apps_dir['path']);
  484. while($file=readdir($dh)) {
  485. if($file[0]!='.' and is_file($apps_dir['path'].'/'.$file.'/appinfo/app.php')) {
  486. $apps[]=$file;
  487. }
  488. }
  489. }
  490. return $apps;
  491. }
  492. /**
  493. * check if the app need updating and update when needed
  494. */
  495. public static function checkUpgrade($app) {
  496. if (in_array($app, self::$checkedApps)) {
  497. return;
  498. }
  499. self::$checkedApps[] = $app;
  500. $versions = self::getAppVersions();
  501. $currentVersion=OC_App::getAppVersion($app);
  502. if ($currentVersion) {
  503. $installedVersion = $versions[$app];
  504. if (version_compare($currentVersion, $installedVersion, '>')) {
  505. OC_Log::write($app, 'starting app upgrade from '.$installedVersion.' to '.$currentVersion, OC_Log::DEBUG);
  506. OC_App::updateApp($app);
  507. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  508. }
  509. }
  510. }
  511. /**
  512. * check if the current enabled apps are compatible with the current
  513. * ownCloud version. disable them if not.
  514. * This is important if you upgrade ownCloud and have non ported 3rd
  515. * party apps installed.
  516. */
  517. public static function checkAppsRequirements($apps = array()) {
  518. if (empty($apps)) {
  519. $apps = OC_App::getEnabledApps();
  520. }
  521. $version = OC_Util::getVersion();
  522. foreach($apps as $app) {
  523. // check if the app is compatible with this version of ownCloud
  524. $info = OC_App::getAppInfo($app);
  525. if(!isset($info['require']) or ($version[0]>$info['require'])) {
  526. 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);
  527. OC_App::disable( $app );
  528. }
  529. }
  530. }
  531. /**
  532. * get the installed version of all apps
  533. */
  534. public static function getAppVersions() {
  535. static $versions;
  536. if (isset($versions)) { // simple cache, needs to be fixed
  537. return $versions; // when function is used besides in checkUpgrade
  538. }
  539. $versions=array();
  540. $query = OC_DB::prepare( 'SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = \'installed_version\'' );
  541. $result = $query->execute();
  542. while($row = $result->fetchRow()) {
  543. $versions[$row['appid']]=$row['configvalue'];
  544. }
  545. return $versions;
  546. }
  547. /**
  548. * update the database for the app and call the update script
  549. * @param string appid
  550. */
  551. public static function updateApp($appid) {
  552. if(file_exists(self::getAppPath($appid).'/appinfo/database.xml')) {
  553. OC_DB::updateDbFromStructure(self::getAppPath($appid).'/appinfo/database.xml');
  554. }
  555. if(!self::isEnabled($appid)) {
  556. return;
  557. }
  558. if(file_exists(self::getAppPath($appid).'/appinfo/update.php')) {
  559. self::loadApp($appid);
  560. include self::getAppPath($appid).'/appinfo/update.php';
  561. }
  562. //set remote/public handelers
  563. $appData=self::getAppInfo($appid);
  564. foreach($appData['remote'] as $name=>$path) {
  565. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $appid.'/'.$path);
  566. }
  567. foreach($appData['public'] as $name=>$path) {
  568. OCP\CONFIG::setAppValue('core', 'public_'.$name, $appid.'/'.$path);
  569. }
  570. self::setAppTypes($appid);
  571. }
  572. /**
  573. * @param string appid
  574. * @return OC_FilesystemView
  575. */
  576. public static function getStorage($appid) {
  577. if(OC_App::isEnabled($appid)) {//sanity check
  578. if(OC_User::isLoggedIn()) {
  579. $view = new OC_FilesystemView('/'.OC_User::getUser());
  580. if(!$view->file_exists($appid)) {
  581. $view->mkdir($appid);
  582. }
  583. return new OC_FilesystemView('/'.OC_User::getUser().'/'.$appid);
  584. }else{
  585. OC_Log::write('core', 'Can\'t get app storage, app, user not logged in', OC_Log::ERROR);
  586. return false;
  587. }
  588. }else{
  589. OC_Log::write('core', 'Can\'t get app storage, app '.$appid.' not enabled', OC_Log::ERROR);
  590. false;
  591. }
  592. }
  593. }