installer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Brice Maron <brice@bmaron.net>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Felix Moeller <mail@felixmoeller.de>
  8. * @author Frank Karlitschek <frank@owncloud.org>
  9. * @author Georg Ehrke <georg@owncloud.com>
  10. * @author Jakob Sack <mail@jakobsack.de>
  11. * @author Joas Schilling <nickvergessen@owncloud.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author Kamil Domanski <kdomanski@kdemail.net>
  14. * @author Lukas Reschke <lukas@owncloud.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <icewind@owncloud.com>
  17. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  18. * @author root <root@oc.(none)>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Thomas Tanghus <thomas@tanghus.net>
  21. * @author Vincent Petry <pvince81@owncloud.com>
  22. *
  23. * @copyright Copyright (c) 2015, ownCloud, Inc.
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. use OC\OCSClient;
  40. /**
  41. * This class provides the functionality needed to install, update and remove plugins/apps
  42. */
  43. class OC_Installer{
  44. /**
  45. *
  46. * This function installs an app. All information needed are passed in the
  47. * associative array $data.
  48. * The following keys are required:
  49. * - source: string, can be "path" or "http"
  50. *
  51. * One of the following keys is required:
  52. * - path: path to the file containing the app
  53. * - href: link to the downloadable file containing the app
  54. *
  55. * The following keys are optional:
  56. * - pretend: boolean, if set true the system won't do anything
  57. * - noinstall: boolean, if true appinfo/install.php won't be loaded
  58. * - inactive: boolean, if set true the appconfig/app.sample.php won't be
  59. * renamed
  60. *
  61. * This function works as follows
  62. * -# fetching the file
  63. * -# unzipping it
  64. * -# check the code
  65. * -# installing the database at appinfo/database.xml
  66. * -# including appinfo/install.php
  67. * -# setting the installed version
  68. *
  69. * It is the task of oc_app_install to create the tables and do whatever is
  70. * needed to get the app working.
  71. *
  72. * Installs an app
  73. * @param array $data with all information
  74. * @throws \Exception
  75. * @return integer
  76. */
  77. public static function installApp( $data = array()) {
  78. $l = \OC::$server->getL10N('lib');
  79. list($extractDir, $path) = self::downloadApp($data);
  80. $info = self::checkAppsIntegrity($data, $extractDir, $path);
  81. $basedir=OC_App::getInstallPath().'/'.$info['id'];
  82. //check if the destination directory already exists
  83. if(is_dir($basedir)) {
  84. OC_Helper::rmdirr($extractDir);
  85. if($data['source']=='http') {
  86. unlink($path);
  87. }
  88. throw new \Exception($l->t("App directory already exists"));
  89. }
  90. if(!empty($data['pretent'])) {
  91. return false;
  92. }
  93. //copy the app to the correct place
  94. if(@!mkdir($basedir)) {
  95. OC_Helper::rmdirr($extractDir);
  96. if($data['source']=='http') {
  97. unlink($path);
  98. }
  99. throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir)));
  100. }
  101. $extractDir .= '/' . $info['id'];
  102. OC_Helper::copyr($extractDir, $basedir);
  103. //remove temporary files
  104. OC_Helper::rmdirr($extractDir);
  105. //install the database
  106. if(is_file($basedir.'/appinfo/database.xml')) {
  107. if (OC_Appconfig::getValue($info['id'], 'installed_version') === null) {
  108. OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
  109. } else {
  110. OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
  111. }
  112. }
  113. //run appinfo/install.php
  114. if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')) {
  115. include $basedir.'/appinfo/install.php';
  116. }
  117. //set the installed version
  118. OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id']));
  119. OC_Appconfig::setValue($info['id'], 'enabled', 'no');
  120. //set remote/public handelers
  121. foreach($info['remote'] as $name=>$path) {
  122. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
  123. }
  124. foreach($info['public'] as $name=>$path) {
  125. OCP\CONFIG::setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
  126. }
  127. OC_App::setAppTypes($info['id']);
  128. return $info['id'];
  129. }
  130. /**
  131. * @brief checks whether or not an app is installed
  132. * @param string $app app
  133. * @returns bool
  134. *
  135. * Checks whether or not an app is installed, i.e. registered in apps table.
  136. */
  137. public static function isInstalled( $app ) {
  138. return (OC_Appconfig::getValue($app, "installed_version") !== null);
  139. }
  140. /**
  141. * @brief Update an application
  142. * @param array $info
  143. * @param bool $isShipped
  144. *
  145. * This function could work like described below, but currently it disables and then
  146. * enables the app again. This does result in an updated app.
  147. *
  148. *
  149. * This function installs an app. All information needed are passed in the
  150. * associative array $info.
  151. * The following keys are required:
  152. * - source: string, can be "path" or "http"
  153. *
  154. * One of the following keys is required:
  155. * - path: path to the file containing the app
  156. * - href: link to the downloadable file containing the app
  157. *
  158. * The following keys are optional:
  159. * - pretend: boolean, if set true the system won't do anything
  160. * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded
  161. *
  162. * This function works as follows
  163. * -# fetching the file
  164. * -# removing the old files
  165. * -# unzipping new file
  166. * -# including appinfo/upgrade.php
  167. * -# setting the installed version
  168. *
  169. * upgrade.php can determine the current installed version of the app using
  170. * "OC_Appconfig::getValue($appid, 'installed_version')"
  171. */
  172. public static function updateApp( $info=array(), $isShipped=false) {
  173. list($extractDir, $path) = self::downloadApp($info);
  174. $info = self::checkAppsIntegrity($info, $extractDir, $path, $isShipped);
  175. $currentDir = OC_App::getAppPath($info['id']);
  176. $basedir = OC_App::getInstallPath();
  177. $basedir .= '/';
  178. $basedir .= $info['id'];
  179. if($currentDir !== false && is_writable($currentDir)) {
  180. $basedir = $currentDir;
  181. }
  182. if(is_dir($basedir)) {
  183. OC_Helper::rmdirr($basedir);
  184. }
  185. $appInExtractDir = $extractDir;
  186. if (substr($extractDir, -1) !== '/') {
  187. $appInExtractDir .= '/';
  188. }
  189. $appInExtractDir .= $info['id'];
  190. OC_Helper::copyr($appInExtractDir, $basedir);
  191. OC_Helper::rmdirr($extractDir);
  192. return OC_App::updateApp($info['id']);
  193. }
  194. /**
  195. * update an app by it's id
  196. *
  197. * @param integer $ocsId
  198. * @return bool
  199. * @throws Exception
  200. */
  201. public static function updateAppByOCSId($ocsId) {
  202. $ocsClient = new OCSClient(
  203. \OC::$server->getHTTPClientService(),
  204. \OC::$server->getConfig(),
  205. \OC::$server->getLogger()
  206. );
  207. $appData = $ocsClient->getApplication($ocsId);
  208. $download = $ocsClient->getApplicationDownload($ocsId);
  209. if (isset($download['downloadlink']) && trim($download['downloadlink']) !== '') {
  210. $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']);
  211. $info = array(
  212. 'source' => 'http',
  213. 'href' => $download['downloadlink'],
  214. 'appdata' => $appData
  215. );
  216. } else {
  217. throw new \Exception('Could not fetch app info!');
  218. }
  219. return self::updateApp($info);
  220. }
  221. /**
  222. * @param array $data
  223. * @return array
  224. * @throws Exception
  225. */
  226. public static function downloadApp($data = array()) {
  227. $l = \OC::$server->getL10N('lib');
  228. if(!isset($data['source'])) {
  229. throw new \Exception($l->t("No source specified when installing app"));
  230. }
  231. //download the file if necessary
  232. if($data['source']=='http') {
  233. $pathInfo = pathinfo($data['href']);
  234. $path=OC_Helper::tmpFile('.' . $pathInfo['extension']);
  235. if(!isset($data['href'])) {
  236. throw new \Exception($l->t("No href specified when installing app from http"));
  237. }
  238. $client = \OC::$server->getHTTPClientService()->newClient();
  239. $client->get($data['href'], ['save_to' => $path]);
  240. } else {
  241. if(!isset($data['path'])) {
  242. throw new \Exception($l->t("No path specified when installing app from local file"));
  243. }
  244. $path=$data['path'];
  245. }
  246. //detect the archive type
  247. $mime=OC_Helper::getMimeType($path);
  248. if ($mime !=='application/zip' && $mime !== 'application/x-gzip') {
  249. throw new \Exception($l->t("Archives of type %s are not supported", array($mime)));
  250. }
  251. //extract the archive in a temporary folder
  252. $extractDir=OC_Helper::tmpFolder();
  253. OC_Helper::rmdirr($extractDir);
  254. mkdir($extractDir);
  255. if($archive=OC_Archive::open($path)) {
  256. $archive->extract($extractDir);
  257. } else {
  258. OC_Helper::rmdirr($extractDir);
  259. if($data['source']=='http') {
  260. unlink($path);
  261. }
  262. throw new \Exception($l->t("Failed to open archive when installing app"));
  263. }
  264. return array(
  265. $extractDir,
  266. $path
  267. );
  268. }
  269. /**
  270. * check an app's integrity
  271. * @param array $data
  272. * @param string $extractDir
  273. * @param bool $isShipped
  274. * @return array
  275. * @throws \Exception
  276. */
  277. public static function checkAppsIntegrity($data, $extractDir, $path, $isShipped=false) {
  278. $l = \OC::$server->getL10N('lib');
  279. //load the info.xml file of the app
  280. if(!is_file($extractDir.'/appinfo/info.xml')) {
  281. //try to find it in a subdir
  282. $dh=opendir($extractDir);
  283. if(is_resource($dh)) {
  284. while (($folder = readdir($dh)) !== false) {
  285. if($folder[0]!='.' and is_dir($extractDir.'/'.$folder)) {
  286. if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')) {
  287. $extractDir.='/'.$folder;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. if(!is_file($extractDir.'/appinfo/info.xml')) {
  294. OC_Helper::rmdirr($extractDir);
  295. if($data['source']=='http') {
  296. unlink($path);
  297. }
  298. throw new \Exception($l->t("App does not provide an info.xml file"));
  299. }
  300. $info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml', true);
  301. // check the code for not allowed calls
  302. if(!$isShipped && !OC_Installer::checkCode($extractDir)) {
  303. OC_Helper::rmdirr($extractDir);
  304. throw new \Exception($l->t("App can't be installed because of not allowed code in the App"));
  305. }
  306. // check if the app is compatible with this version of ownCloud
  307. if(!OC_App::isAppCompatible(OC_Util::getVersion(), $info)) {
  308. OC_Helper::rmdirr($extractDir);
  309. throw new \Exception($l->t("App can't be installed because it is not compatible with this version of ownCloud"));
  310. }
  311. // check if shipped tag is set which is only allowed for apps that are shipped with ownCloud
  312. if(!$isShipped && isset($info['shipped']) && ($info['shipped']=='true')) {
  313. OC_Helper::rmdirr($extractDir);
  314. throw new \Exception($l->t("App can't be installed because it contains the <shipped>true</shipped> tag which is not allowed for non shipped apps"));
  315. }
  316. // check if the ocs version is the same as the version in info.xml/version
  317. $versionFile= $extractDir.'/appinfo/version';
  318. if(is_file($versionFile)) {
  319. $version = trim(file_get_contents($versionFile));
  320. }else{
  321. $version = trim($info['version']);
  322. }
  323. if(isset($data['appdata']['version']) && $version<>trim($data['appdata']['version'])) {
  324. OC_Helper::rmdirr($extractDir);
  325. throw new \Exception($l->t("App can't be installed because the version in info.xml/version is not the same as the version reported from the app store"));
  326. }
  327. return $info;
  328. }
  329. /**
  330. * Check if an update for the app is available
  331. * @param string $app
  332. * @return string|false false or the version number of the update
  333. *
  334. * The function will check if an update for a version is available
  335. */
  336. public static function isUpdateAvailable( $app ) {
  337. static $isInstanceReadyForUpdates = null;
  338. if ($isInstanceReadyForUpdates === null) {
  339. $installPath = OC_App::getInstallPath();
  340. if ($installPath === false || $installPath === null) {
  341. $isInstanceReadyForUpdates = false;
  342. } else {
  343. $isInstanceReadyForUpdates = true;
  344. }
  345. }
  346. if ($isInstanceReadyForUpdates === false) {
  347. return false;
  348. }
  349. $ocsid=OC_Appconfig::getValue( $app, 'ocsid', '');
  350. if($ocsid<>'') {
  351. $ocsClient = new OCSClient(
  352. \OC::$server->getHTTPClientService(),
  353. \OC::$server->getConfig(),
  354. \OC::$server->getLogger()
  355. );
  356. $ocsdata = $ocsClient->getApplication($ocsid);
  357. $ocsversion= (string) $ocsdata['version'];
  358. $currentversion=OC_App::getAppVersion($app);
  359. if (version_compare($ocsversion, $currentversion, '>')) {
  360. return($ocsversion);
  361. }else{
  362. return false;
  363. }
  364. }else{
  365. return false;
  366. }
  367. }
  368. /**
  369. * Check if app is already downloaded
  370. * @param string $name name of the application to remove
  371. * @return boolean
  372. *
  373. * The function will check if the app is already downloaded in the apps repository
  374. */
  375. public static function isDownloaded( $name ) {
  376. foreach(OC::$APPSROOTS as $dir) {
  377. $dirToTest = $dir['path'];
  378. $dirToTest .= '/';
  379. $dirToTest .= $name;
  380. $dirToTest .= '/';
  381. if (is_dir($dirToTest)) {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. /**
  388. * Removes an app
  389. * @param string $name name of the application to remove
  390. * @param array $options options
  391. * @return boolean
  392. *
  393. * This function removes an app. $options is an associative array. The
  394. * following keys are optional:ja
  395. * - keeppreferences: boolean, if true the user preferences won't be deleted
  396. * - keepappconfig: boolean, if true the config will be kept
  397. * - keeptables: boolean, if true the database will be kept
  398. * - keepfiles: boolean, if true the user files will be kept
  399. *
  400. * This function works as follows
  401. * -# including appinfo/remove.php
  402. * -# removing the files
  403. *
  404. * The function will not delete preferences, tables and the configuration,
  405. * this has to be done by the function oc_app_uninstall().
  406. */
  407. public static function removeApp( $name, $options = array()) {
  408. if(isset($options['keeppreferences']) and $options['keeppreferences']==false ) {
  409. // todo
  410. // remove preferences
  411. }
  412. if(isset($options['keepappconfig']) and $options['keepappconfig']==false ) {
  413. // todo
  414. // remove app config
  415. }
  416. if(isset($options['keeptables']) and $options['keeptables']==false ) {
  417. // todo
  418. // remove app database tables
  419. }
  420. if(isset($options['keepfiles']) and $options['keepfiles']==false ) {
  421. // todo
  422. // remove user files
  423. }
  424. if(OC_Installer::isDownloaded( $name )) {
  425. $appdir=OC_App::getInstallPath().'/'.$name;
  426. OC_Helper::rmdirr($appdir);
  427. return true;
  428. }else{
  429. OC_Log::write('core', 'can\'t remove app '.$name.'. It is not installed.', OC_Log::ERROR);
  430. return false;
  431. }
  432. }
  433. /**
  434. * Installs shipped apps
  435. *
  436. * This function installs all apps found in the 'apps' directory that should be enabled by default;
  437. */
  438. public static function installShippedApps() {
  439. foreach(OC::$APPSROOTS as $app_dir) {
  440. if($dir = opendir( $app_dir['path'] )) {
  441. while( false !== ( $filename = readdir( $dir ))) {
  442. if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) {
  443. if( file_exists( $app_dir['path']."/$filename/appinfo/app.php" )) {
  444. if(!OC_Installer::isInstalled($filename)) {
  445. $info=OC_App::getAppInfo($filename);
  446. $enabled = isset($info['default_enable']);
  447. if( $enabled ) {
  448. OC_Installer::installShippedApp($filename);
  449. OC_Appconfig::setValue($filename, 'enabled', 'yes');
  450. }
  451. }
  452. }
  453. }
  454. }
  455. closedir( $dir );
  456. }
  457. }
  458. }
  459. /**
  460. * install an app already placed in the app folder
  461. * @param string $app id of the app to install
  462. * @return integer
  463. */
  464. public static function installShippedApp($app) {
  465. //install the database
  466. if(is_file(OC_App::getAppPath($app)."/appinfo/database.xml")) {
  467. OC_DB::createDbFromStructure(OC_App::getAppPath($app)."/appinfo/database.xml");
  468. }
  469. //run appinfo/install.php
  470. if(is_file(OC_App::getAppPath($app)."/appinfo/install.php")) {
  471. include OC_App::getAppPath($app)."/appinfo/install.php";
  472. }
  473. $info=OC_App::getAppInfo($app);
  474. if (is_null($info)) {
  475. return false;
  476. }
  477. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  478. if (array_key_exists('ocsid', $info)) {
  479. OC_Appconfig::setValue($app, 'ocsid', $info['ocsid']);
  480. }
  481. //set remote/public handlers
  482. foreach($info['remote'] as $name=>$path) {
  483. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $app.'/'.$path);
  484. }
  485. foreach($info['public'] as $name=>$path) {
  486. OCP\CONFIG::setAppValue('core', 'public_'.$name, $app.'/'.$path);
  487. }
  488. OC_App::setAppTypes($info['id']);
  489. return $info['id'];
  490. }
  491. /**
  492. * check the code of an app with some static code checks
  493. * @param string $folder the folder of the app to check
  494. * @return boolean true for app is o.k. and false for app is not o.k.
  495. */
  496. public static function checkCode($folder) {
  497. // is the code checker enabled?
  498. if(!OC_Config::getValue('appcodechecker', false)) {
  499. return true;
  500. }
  501. $codeChecker = new \OC\App\CodeChecker();
  502. $errors = $codeChecker->analyseFolder($folder);
  503. return empty($errors);
  504. }
  505. }