installer.php 14 KB

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