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.

installer.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This class provides the functionality needed to install, update and remove plugins/apps
  24. */
  25. class OC_Installer{
  26. /**
  27. *
  28. * This function installs an app. All information needed are passed in the
  29. * associative array $data.
  30. * The following keys are required:
  31. * - source: string, can be "path" or "http"
  32. *
  33. * One of the following keys is required:
  34. * - path: path to the file containing the app
  35. * - href: link to the downloadable file containing the app
  36. *
  37. * The following keys are optional:
  38. * - pretend: boolean, if set true the system won't do anything
  39. * - noinstall: boolean, if true appinfo/install.php won't be loaded
  40. * - inactive: boolean, if set true the appconfig/app.sample.php won't be
  41. * renamed
  42. *
  43. * This function works as follows
  44. * -# fetching the file
  45. * -# unzipping it
  46. * -# check the code
  47. * -# installing the database at appinfo/database.xml
  48. * -# including appinfo/install.php
  49. * -# setting the installed version
  50. *
  51. * It is the task of oc_app_install to create the tables and do whatever is
  52. * needed to get the app working.
  53. *
  54. * @brief Installs an app
  55. * @param array $data with all information
  56. * @throws \Exception
  57. * @return integer
  58. */
  59. public static function installApp( $data = array()) {
  60. $l = \OC_L10N::get('lib');
  61. if(!isset($data['source'])) {
  62. throw new \Exception($l->t("No source specified when installing app"));
  63. }
  64. //download the file if necesary
  65. if($data['source']=='http') {
  66. $path=OC_Helper::tmpFile();
  67. if(!isset($data['href'])) {
  68. throw new \Exception($l->t("No href specified when installing app from http"));
  69. }
  70. copy($data['href'], $path);
  71. }else{
  72. if(!isset($data['path'])) {
  73. throw new \Exception($l->t("No path specified when installing app from local file"));
  74. }
  75. $path=$data['path'];
  76. }
  77. //detect the archive type
  78. $mime=OC_Helper::getMimeType($path);
  79. if($mime=='application/zip') {
  80. rename($path, $path.'.zip');
  81. $path.='.zip';
  82. }elseif($mime=='application/x-gzip') {
  83. rename($path, $path.'.tgz');
  84. $path.='.tgz';
  85. }else{
  86. throw new \Exception($l->t("Archives of type %s are not supported", array($mime)));
  87. }
  88. //extract the archive in a temporary folder
  89. $extractDir=OC_Helper::tmpFolder();
  90. OC_Helper::rmdirr($extractDir);
  91. mkdir($extractDir);
  92. if($archive=OC_Archive::open($path)) {
  93. $archive->extract($extractDir);
  94. } else {
  95. OC_Helper::rmdirr($extractDir);
  96. if($data['source']=='http') {
  97. unlink($path);
  98. }
  99. throw new \Exception($l->t("Failed to open archive when installing app"));
  100. }
  101. //load the info.xml file of the app
  102. if(!is_file($extractDir.'/appinfo/info.xml')) {
  103. //try to find it in a subdir
  104. $dh=opendir($extractDir);
  105. if(is_resource($dh)) {
  106. while (($folder = readdir($dh)) !== false) {
  107. if($folder[0]!='.' and is_dir($extractDir.'/'.$folder)) {
  108. if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')) {
  109. $extractDir.='/'.$folder;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. if(!is_file($extractDir.'/appinfo/info.xml')) {
  116. OC_Helper::rmdirr($extractDir);
  117. if($data['source']=='http') {
  118. unlink($path);
  119. }
  120. throw new \Exception($l->t("App does not provide an info.xml file"));
  121. }
  122. $info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml', true);
  123. // check the code for not allowed calls
  124. if(!OC_Installer::checkCode($info['id'], $extractDir)) {
  125. OC_Helper::rmdirr($extractDir);
  126. throw new \Exception($l->t("App can't be installed because of not allowed code in the App"));
  127. }
  128. // check if the app is compatible with this version of ownCloud
  129. if(
  130. !isset($info['require'])
  131. or !OC_App::isAppVersionCompatible(OC_Util::getVersion(), $info['require'])
  132. ) {
  133. OC_Helper::rmdirr($extractDir);
  134. throw new \Exception($l->t("App can't be installed because it is not compatible with this version of ownCloud"));
  135. }
  136. // check if shipped tag is set which is only allowed for apps that are shipped with ownCloud
  137. if(isset($info['shipped']) and ($info['shipped']=='true')) {
  138. OC_Helper::rmdirr($extractDir);
  139. 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"));
  140. }
  141. // check if the ocs version is the same as the version in info.xml/version
  142. $versionFile= $extractDir.'/appinfo/version';
  143. if(is_file($versionFile)) {
  144. $version = trim(file_get_contents($versionFile));
  145. }else{
  146. $version = trim($info['version']);
  147. }
  148. if($version<>trim($data['appdata']['version'])) {
  149. OC_Helper::rmdirr($extractDir);
  150. 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"));
  151. }
  152. $basedir=OC_App::getInstallPath().'/'.$info['id'];
  153. //check if the destination directory already exists
  154. if(is_dir($basedir)) {
  155. OC_Helper::rmdirr($extractDir);
  156. if($data['source']=='http') {
  157. unlink($path);
  158. }
  159. throw new \Exception($l->t("App directory already exists"));
  160. }
  161. if(isset($data['pretent']) and $data['pretent']==true) {
  162. return false;
  163. }
  164. //copy the app to the correct place
  165. if(@!mkdir($basedir)) {
  166. OC_Helper::rmdirr($extractDir);
  167. if($data['source']=='http') {
  168. unlink($path);
  169. }
  170. throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir)));
  171. }
  172. OC_Helper::copyr($extractDir, $basedir);
  173. //remove temporary files
  174. OC_Helper::rmdirr($extractDir);
  175. //install the database
  176. if(is_file($basedir.'/appinfo/database.xml')) {
  177. if (OC_Appconfig::getValue($info['id'], 'installed_version') === null) {
  178. OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
  179. } else {
  180. OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
  181. }
  182. }
  183. //run appinfo/install.php
  184. if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')) {
  185. include $basedir.'/appinfo/install.php';
  186. }
  187. //set the installed version
  188. OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id']));
  189. OC_Appconfig::setValue($info['id'], 'enabled', 'no');
  190. //set remote/public handelers
  191. foreach($info['remote'] as $name=>$path) {
  192. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
  193. }
  194. foreach($info['public'] as $name=>$path) {
  195. OCP\CONFIG::setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
  196. }
  197. OC_App::setAppTypes($info['id']);
  198. return $info['id'];
  199. }
  200. /**
  201. * @brief checks whether or not an app is installed
  202. * @param string $app app
  203. * @returns true/false
  204. *
  205. * Checks whether or not an app is installed, i.e. registered in apps table.
  206. */
  207. public static function isInstalled( $app ) {
  208. if( null == OC_Appconfig::getValue( $app, "installed_version" )) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. /**
  214. * @brief Update an application
  215. *
  216. * This function installs an app. All information needed are passed in the
  217. * associative array $data.
  218. * The following keys are required:
  219. * - source: string, can be "path" or "http"
  220. *
  221. * One of the following keys is required:
  222. * - path: path to the file containing the app
  223. * - href: link to the downloadable file containing the app
  224. *
  225. * The following keys are optional:
  226. * - pretend: boolean, if set true the system won't do anything
  227. * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded
  228. *
  229. * This function works as follows
  230. * -# fetching the file
  231. * -# removing the old files
  232. * -# unzipping new file
  233. * -# including appinfo/upgrade.php
  234. * -# setting the installed version
  235. *
  236. * upgrade.php can determine the current installed version of the app using
  237. * "OC_Appconfig::getValue($appid, 'installed_version')"
  238. */
  239. public static function updateApp( $app ) {
  240. $ocsid=OC_Appconfig::getValue( $app, 'ocsid');
  241. OC_App::disable($app);
  242. OC_App::enable($ocsid);
  243. return(true);
  244. }
  245. /**
  246. * @brief Check if an update for the app is available
  247. * @return string|false false or the version number of the update
  248. *
  249. * The function will check if an update for a version is available
  250. */
  251. public static function isUpdateAvailable( $app ) {
  252. $ocsid=OC_Appconfig::getValue( $app, 'ocsid', '');
  253. if($ocsid<>'') {
  254. $ocsdata=OC_OCSClient::getApplication($ocsid);
  255. $ocsversion= (string) $ocsdata['version'];
  256. $currentversion=OC_App::getAppVersion($app);
  257. if($ocsversion<>$currentversion) {
  258. return($ocsversion);
  259. }else{
  260. return false;
  261. }
  262. }else{
  263. return false;
  264. }
  265. }
  266. /**
  267. * @brief Check if app is already downloaded
  268. * @param string $name name of the application to remove
  269. * @return boolean
  270. *
  271. * The function will check if the app is already downloaded in the apps repository
  272. */
  273. public static function isDownloaded( $name ) {
  274. $downloaded=false;
  275. foreach(OC::$APPSROOTS as $dir) {
  276. if(is_dir($dir['path'].'/'.$name)) $downloaded=true;
  277. }
  278. return($downloaded);
  279. }
  280. /**
  281. * @brief Removes an app
  282. * @param string $name name of the application to remove
  283. * @param $options array with options
  284. * @return boolean|null
  285. *
  286. * This function removes an app. $options is an associative array. The
  287. * following keys are optional:ja
  288. * - keeppreferences: boolean, if true the user preferences won't be deleted
  289. * - keepappconfig: boolean, if true the config will be kept
  290. * - keeptables: boolean, if true the database will be kept
  291. * - keepfiles: boolean, if true the user files will be kept
  292. *
  293. * This function works as follows
  294. * -# including appinfo/remove.php
  295. * -# removing the files
  296. *
  297. * The function will not delete preferences, tables and the configuration,
  298. * this has to be done by the function oc_app_uninstall().
  299. */
  300. public static function removeApp( $name, $options = array()) {
  301. if(isset($options['keeppreferences']) and $options['keeppreferences']==false ) {
  302. // todo
  303. // remove preferences
  304. }
  305. if(isset($options['keepappconfig']) and $options['keepappconfig']==false ) {
  306. // todo
  307. // remove app config
  308. }
  309. if(isset($options['keeptables']) and $options['keeptables']==false ) {
  310. // todo
  311. // remove app database tables
  312. }
  313. if(isset($options['keepfiles']) and $options['keepfiles']==false ) {
  314. // todo
  315. // remove user files
  316. }
  317. if(OC_Installer::isDownloaded( $name )) {
  318. $appdir=OC_App::getInstallPath().'/'.$name;
  319. OC_Helper::rmdirr($appdir);
  320. }else{
  321. OC_Log::write('core', 'can\'t remove app '.$name.'. It is not installed.', OC_Log::ERROR);
  322. }
  323. }
  324. /**
  325. * @brief Installs shipped apps
  326. *
  327. * This function installs all apps found in the 'apps' directory that should be enabled by default;
  328. */
  329. public static function installShippedApps() {
  330. foreach(OC::$APPSROOTS as $app_dir) {
  331. if($dir = opendir( $app_dir['path'] )) {
  332. while( false !== ( $filename = readdir( $dir ))) {
  333. if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) {
  334. if( file_exists( $app_dir['path']."/$filename/appinfo/app.php" )) {
  335. if(!OC_Installer::isInstalled($filename)) {
  336. $info=OC_App::getAppInfo($filename);
  337. $enabled = isset($info['default_enable']);
  338. if( $enabled ) {
  339. OC_Installer::installShippedApp($filename);
  340. OC_Appconfig::setValue($filename, 'enabled', 'yes');
  341. }
  342. }
  343. }
  344. }
  345. }
  346. closedir( $dir );
  347. }
  348. }
  349. }
  350. /**
  351. * install an app already placed in the app folder
  352. * @param string $app id of the app to install
  353. * @return integer
  354. */
  355. public static function installShippedApp($app) {
  356. //install the database
  357. if(is_file(OC_App::getAppPath($app)."/appinfo/database.xml")) {
  358. OC_DB::createDbFromStructure(OC_App::getAppPath($app)."/appinfo/database.xml");
  359. }
  360. //run appinfo/install.php
  361. if(is_file(OC_App::getAppPath($app)."/appinfo/install.php")) {
  362. include OC_App::getAppPath($app)."/appinfo/install.php";
  363. }
  364. $info=OC_App::getAppInfo($app);
  365. if (is_null($info)) {
  366. return false;
  367. }
  368. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  369. //set remote/public handelers
  370. foreach($info['remote'] as $name=>$path) {
  371. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $app.'/'.$path);
  372. }
  373. foreach($info['public'] as $name=>$path) {
  374. OCP\CONFIG::setAppValue('core', 'public_'.$name, $app.'/'.$path);
  375. }
  376. OC_App::setAppTypes($info['id']);
  377. return $info['id'];
  378. }
  379. /**
  380. * check the code of an app with some static code checks
  381. * @param string $folder the folder of the app to check
  382. * @return boolean true for app is o.k. and false for app is not o.k.
  383. */
  384. public static function checkCode($appname, $folder) {
  385. $blacklist=array(
  386. 'exec(',
  387. 'eval(',
  388. // more evil pattern will go here later
  389. // classes replaced by the public api
  390. 'OC_API::',
  391. 'OC_App::',
  392. 'OC_AppConfig::',
  393. 'OC_Avatar',
  394. 'OC_BackgroundJob::',
  395. 'OC_Config::',
  396. 'OC_DB::',
  397. 'OC_Files::',
  398. 'OC_Helper::',
  399. 'OC_Hook::',
  400. 'OC_Image::',
  401. 'OC_JSON::',
  402. 'OC_L10N::',
  403. 'OC_Log::',
  404. 'OC_Mail::',
  405. 'OC_Preferences::',
  406. 'OC_Request::',
  407. 'OC_Response::',
  408. 'OC_Template::',
  409. 'OC_User::',
  410. 'OC_Util::',
  411. );
  412. // is the code checker enabled?
  413. if(OC_Config::getValue('appcodechecker', true)) {
  414. // check if grep is installed
  415. $grep = exec('command -v grep');
  416. if($grep=='') {
  417. OC_Log::write('core',
  418. 'grep not installed. So checking the code of the app "'.$appname.'" was not possible',
  419. OC_Log::ERROR);
  420. return true;
  421. }
  422. // iterate the bad patterns
  423. foreach($blacklist as $bl) {
  424. $cmd = 'grep -ri '.escapeshellarg($bl).' '.$folder.'';
  425. $result = exec($cmd);
  426. // bad pattern found
  427. if($result<>'') {
  428. OC_Log::write('core',
  429. 'App "'.$appname.'" is using a not allowed call "'.$bl.'". Installation refused.',
  430. OC_Log::ERROR);
  431. return false;
  432. }
  433. }
  434. return true;
  435. }else{
  436. return true;
  437. }
  438. }
  439. }