installer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. $appData = OCSClient::getApplication($ocsId);
  203. $download = OCSClient::getApplicationDownload($ocsId, 1);
  204. if (isset($download['downloadlink']) && trim($download['downloadlink']) !== '') {
  205. $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']);
  206. $info = array(
  207. 'source' => 'http',
  208. 'href' => $download['downloadlink'],
  209. 'appdata' => $appData
  210. );
  211. } else {
  212. throw new \Exception('Could not fetch app info!');
  213. }
  214. return self::updateApp($info);
  215. }
  216. /**
  217. * @param array $data
  218. * @return array
  219. * @throws Exception
  220. */
  221. public static function downloadApp($data = array()) {
  222. $l = \OC::$server->getL10N('lib');
  223. if(!isset($data['source'])) {
  224. throw new \Exception($l->t("No source specified when installing app"));
  225. }
  226. //download the file if necessary
  227. if($data['source']=='http') {
  228. $pathInfo = pathinfo($data['href']);
  229. $path=OC_Helper::tmpFile('.' . $pathInfo['extension']);
  230. if(!isset($data['href'])) {
  231. throw new \Exception($l->t("No href specified when installing app from http"));
  232. }
  233. $client = \OC::$server->getHTTPClientService()->newClient();
  234. $client->get($data['href'], ['save_to' => $path]);
  235. } else {
  236. if(!isset($data['path'])) {
  237. throw new \Exception($l->t("No path specified when installing app from local file"));
  238. }
  239. $path=$data['path'];
  240. }
  241. //detect the archive type
  242. $mime=OC_Helper::getMimeType($path);
  243. if ($mime !=='application/zip' && $mime !== 'application/x-gzip') {
  244. throw new \Exception($l->t("Archives of type %s are not supported", array($mime)));
  245. }
  246. //extract the archive in a temporary folder
  247. $extractDir=OC_Helper::tmpFolder();
  248. OC_Helper::rmdirr($extractDir);
  249. mkdir($extractDir);
  250. if($archive=OC_Archive::open($path)) {
  251. $archive->extract($extractDir);
  252. } else {
  253. OC_Helper::rmdirr($extractDir);
  254. if($data['source']=='http') {
  255. unlink($path);
  256. }
  257. throw new \Exception($l->t("Failed to open archive when installing app"));
  258. }
  259. return array(
  260. $extractDir,
  261. $path
  262. );
  263. }
  264. /**
  265. * check an app's integrity
  266. * @param array $data
  267. * @param string $extractDir
  268. * @param bool $isShipped
  269. * @return array
  270. * @throws \Exception
  271. */
  272. public static function checkAppsIntegrity($data, $extractDir, $path, $isShipped=false) {
  273. $l = \OC::$server->getL10N('lib');
  274. //load the info.xml file of the app
  275. if(!is_file($extractDir.'/appinfo/info.xml')) {
  276. //try to find it in a subdir
  277. $dh=opendir($extractDir);
  278. if(is_resource($dh)) {
  279. while (($folder = readdir($dh)) !== false) {
  280. if($folder[0]!='.' and is_dir($extractDir.'/'.$folder)) {
  281. if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')) {
  282. $extractDir.='/'.$folder;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. if(!is_file($extractDir.'/appinfo/info.xml')) {
  289. OC_Helper::rmdirr($extractDir);
  290. if($data['source']=='http') {
  291. unlink($path);
  292. }
  293. throw new \Exception($l->t("App does not provide an info.xml file"));
  294. }
  295. $info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml', true);
  296. // check the code for not allowed calls
  297. if(!$isShipped && !OC_Installer::checkCode($extractDir)) {
  298. OC_Helper::rmdirr($extractDir);
  299. throw new \Exception($l->t("App can't be installed because of not allowed code in the App"));
  300. }
  301. // check if the app is compatible with this version of ownCloud
  302. if(!OC_App::isAppCompatible(OC_Util::getVersion(), $info)) {
  303. OC_Helper::rmdirr($extractDir);
  304. throw new \Exception($l->t("App can't be installed because it is not compatible with this version of ownCloud"));
  305. }
  306. // check if shipped tag is set which is only allowed for apps that are shipped with ownCloud
  307. if(!$isShipped && isset($info['shipped']) && ($info['shipped']=='true')) {
  308. OC_Helper::rmdirr($extractDir);
  309. 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"));
  310. }
  311. // check if the ocs version is the same as the version in info.xml/version
  312. $versionFile= $extractDir.'/appinfo/version';
  313. if(is_file($versionFile)) {
  314. $version = trim(file_get_contents($versionFile));
  315. }else{
  316. $version = trim($info['version']);
  317. }
  318. if(isset($data['appdata']['version']) && $version<>trim($data['appdata']['version'])) {
  319. OC_Helper::rmdirr($extractDir);
  320. 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"));
  321. }
  322. return $info;
  323. }
  324. /**
  325. * Check if an update for the app is available
  326. * @param string $app
  327. * @return string|false false or the version number of the update
  328. *
  329. * The function will check if an update for a version is available
  330. */
  331. public static function isUpdateAvailable( $app ) {
  332. static $isInstanceReadyForUpdates = null;
  333. if ($isInstanceReadyForUpdates === null) {
  334. $installPath = OC_App::getInstallPath();
  335. if ($installPath === false || $installPath === null) {
  336. $isInstanceReadyForUpdates = false;
  337. } else {
  338. $isInstanceReadyForUpdates = true;
  339. }
  340. }
  341. if ($isInstanceReadyForUpdates === false) {
  342. return false;
  343. }
  344. $ocsid=OC_Appconfig::getValue( $app, 'ocsid', '');
  345. if($ocsid<>'') {
  346. $ocsdata=OCSClient::getApplication($ocsid);
  347. $ocsversion= (string) $ocsdata['version'];
  348. $currentversion=OC_App::getAppVersion($app);
  349. if (version_compare($ocsversion, $currentversion, '>')) {
  350. return($ocsversion);
  351. }else{
  352. return false;
  353. }
  354. }else{
  355. return false;
  356. }
  357. }
  358. /**
  359. * Check if app is already downloaded
  360. * @param string $name name of the application to remove
  361. * @return boolean
  362. *
  363. * The function will check if the app is already downloaded in the apps repository
  364. */
  365. public static function isDownloaded( $name ) {
  366. foreach(OC::$APPSROOTS as $dir) {
  367. $dirToTest = $dir['path'];
  368. $dirToTest .= '/';
  369. $dirToTest .= $name;
  370. $dirToTest .= '/';
  371. if (is_dir($dirToTest)) {
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. /**
  378. * Removes an app
  379. * @param string $name name of the application to remove
  380. * @param array $options options
  381. * @return boolean
  382. *
  383. * This function removes an app. $options is an associative array. The
  384. * following keys are optional:ja
  385. * - keeppreferences: boolean, if true the user preferences won't be deleted
  386. * - keepappconfig: boolean, if true the config will be kept
  387. * - keeptables: boolean, if true the database will be kept
  388. * - keepfiles: boolean, if true the user files will be kept
  389. *
  390. * This function works as follows
  391. * -# including appinfo/remove.php
  392. * -# removing the files
  393. *
  394. * The function will not delete preferences, tables and the configuration,
  395. * this has to be done by the function oc_app_uninstall().
  396. */
  397. public static function removeApp( $name, $options = array()) {
  398. if(isset($options['keeppreferences']) and $options['keeppreferences']==false ) {
  399. // todo
  400. // remove preferences
  401. }
  402. if(isset($options['keepappconfig']) and $options['keepappconfig']==false ) {
  403. // todo
  404. // remove app config
  405. }
  406. if(isset($options['keeptables']) and $options['keeptables']==false ) {
  407. // todo
  408. // remove app database tables
  409. }
  410. if(isset($options['keepfiles']) and $options['keepfiles']==false ) {
  411. // todo
  412. // remove user files
  413. }
  414. if(OC_Installer::isDownloaded( $name )) {
  415. $appdir=OC_App::getInstallPath().'/'.$name;
  416. OC_Helper::rmdirr($appdir);
  417. return true;
  418. }else{
  419. OC_Log::write('core', 'can\'t remove app '.$name.'. It is not installed.', OC_Log::ERROR);
  420. return false;
  421. }
  422. }
  423. /**
  424. * Installs shipped apps
  425. *
  426. * This function installs all apps found in the 'apps' directory that should be enabled by default;
  427. */
  428. public static function installShippedApps() {
  429. foreach(OC::$APPSROOTS as $app_dir) {
  430. if($dir = opendir( $app_dir['path'] )) {
  431. while( false !== ( $filename = readdir( $dir ))) {
  432. if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) {
  433. if( file_exists( $app_dir['path']."/$filename/appinfo/app.php" )) {
  434. if(!OC_Installer::isInstalled($filename)) {
  435. $info=OC_App::getAppInfo($filename);
  436. $enabled = isset($info['default_enable']);
  437. if( $enabled ) {
  438. OC_Installer::installShippedApp($filename);
  439. OC_Appconfig::setValue($filename, 'enabled', 'yes');
  440. }
  441. }
  442. }
  443. }
  444. }
  445. closedir( $dir );
  446. }
  447. }
  448. }
  449. /**
  450. * install an app already placed in the app folder
  451. * @param string $app id of the app to install
  452. * @return integer
  453. */
  454. public static function installShippedApp($app) {
  455. //install the database
  456. if(is_file(OC_App::getAppPath($app)."/appinfo/database.xml")) {
  457. OC_DB::createDbFromStructure(OC_App::getAppPath($app)."/appinfo/database.xml");
  458. }
  459. //run appinfo/install.php
  460. if(is_file(OC_App::getAppPath($app)."/appinfo/install.php")) {
  461. include OC_App::getAppPath($app)."/appinfo/install.php";
  462. }
  463. $info=OC_App::getAppInfo($app);
  464. if (is_null($info)) {
  465. return false;
  466. }
  467. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  468. if (array_key_exists('ocsid', $info)) {
  469. OC_Appconfig::setValue($app, 'ocsid', $info['ocsid']);
  470. }
  471. //set remote/public handlers
  472. foreach($info['remote'] as $name=>$path) {
  473. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $app.'/'.$path);
  474. }
  475. foreach($info['public'] as $name=>$path) {
  476. OCP\CONFIG::setAppValue('core', 'public_'.$name, $app.'/'.$path);
  477. }
  478. OC_App::setAppTypes($info['id']);
  479. return $info['id'];
  480. }
  481. /**
  482. * check the code of an app with some static code checks
  483. * @param string $folder the folder of the app to check
  484. * @return boolean true for app is o.k. and false for app is not o.k.
  485. */
  486. public static function checkCode($folder) {
  487. // is the code checker enabled?
  488. if(!OC_Config::getValue('appcodechecker', false)) {
  489. return true;
  490. }
  491. $codeChecker = new \OC\App\CodeChecker();
  492. $errors = $codeChecker->analyseFolder($folder);
  493. return empty($errors);
  494. }
  495. }