installer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. if(!isset($info['version']) or ($info['version']<>$data['appdata']['version'])) {
  142. OC_Helper::rmdirr($extractDir);
  143. 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"));
  144. }
  145. $basedir=OC_App::getInstallPath().'/'.$info['id'];
  146. //check if the destination directory already exists
  147. if(is_dir($basedir)) {
  148. OC_Helper::rmdirr($extractDir);
  149. if($data['source']=='http') {
  150. unlink($path);
  151. }
  152. throw new \Exception($l->t("App directory already exists"));
  153. }
  154. if(isset($data['pretent']) and $data['pretent']==true) {
  155. return false;
  156. }
  157. //copy the app to the correct place
  158. if(@!mkdir($basedir)) {
  159. OC_Helper::rmdirr($extractDir);
  160. if($data['source']=='http') {
  161. unlink($path);
  162. }
  163. throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir)));
  164. }
  165. OC_Helper::copyr($extractDir, $basedir);
  166. //remove temporary files
  167. OC_Helper::rmdirr($extractDir);
  168. //install the database
  169. if(is_file($basedir.'/appinfo/database.xml')) {
  170. OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
  171. }
  172. //run appinfo/install.php
  173. if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')) {
  174. include $basedir.'/appinfo/install.php';
  175. }
  176. //set the installed version
  177. OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id']));
  178. OC_Appconfig::setValue($info['id'], 'enabled', 'no');
  179. //set remote/public handelers
  180. foreach($info['remote'] as $name=>$path) {
  181. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
  182. }
  183. foreach($info['public'] as $name=>$path) {
  184. OCP\CONFIG::setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
  185. }
  186. OC_App::setAppTypes($info['id']);
  187. return $info['id'];
  188. }
  189. /**
  190. * @brief checks whether or not an app is installed
  191. * @param $app app
  192. * @returns true/false
  193. *
  194. * Checks whether or not an app is installed, i.e. registered in apps table.
  195. */
  196. public static function isInstalled( $app ) {
  197. if( null == OC_Appconfig::getValue( $app, "installed_version" )) {
  198. return false;
  199. }
  200. return true;
  201. }
  202. /**
  203. * @brief Update an application
  204. * @param $data array with all information
  205. *
  206. * This function installs an app. All information needed are passed in the
  207. * associative array $data.
  208. * The following keys are required:
  209. * - source: string, can be "path" or "http"
  210. *
  211. * One of the following keys is required:
  212. * - path: path to the file containing the app
  213. * - href: link to the downloadable file containing the app
  214. *
  215. * The following keys are optional:
  216. * - pretend: boolean, if set true the system won't do anything
  217. * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded
  218. *
  219. * This function works as follows
  220. * -# fetching the file
  221. * -# removing the old files
  222. * -# unzipping new file
  223. * -# including appinfo/upgrade.php
  224. * -# setting the installed version
  225. *
  226. * upgrade.php can determine the current installed version of the app using
  227. * "OC_Appconfig::getValue($appid, 'installed_version')"
  228. */
  229. public static function updateApp( $app ) {
  230. $ocsid=OC_Appconfig::getValue( $app, 'ocsid');
  231. OC_App::disable($app);
  232. OC_App::enable($ocsid);
  233. return(true);
  234. }
  235. /**
  236. * @brief Check if an update for the app is available
  237. * @param $name name of the application
  238. * @returns empty string is no update available or the version number of the update
  239. *
  240. * The function will check if an update for a version is available
  241. */
  242. public static function isUpdateAvailable( $app ) {
  243. $ocsid=OC_Appconfig::getValue( $app, 'ocsid', '');
  244. if($ocsid<>'') {
  245. $ocsdata=OC_OCSClient::getApplication($ocsid);
  246. $ocsversion= (string) $ocsdata['version'];
  247. $currentversion=OC_App::getAppVersion($app);
  248. if($ocsversion<>$currentversion) {
  249. return($ocsversion);
  250. }else{
  251. return('');
  252. }
  253. }else{
  254. return('');
  255. }
  256. }
  257. /**
  258. * @brief Check if app is already downloaded
  259. * @param $name name of the application to remove
  260. * @returns true/false
  261. *
  262. * The function will check if the app is already downloaded in the apps repository
  263. */
  264. public static function isDownloaded( $name ) {
  265. $downloaded=false;
  266. foreach(OC::$APPSROOTS as $dir) {
  267. if(is_dir($dir['path'].'/'.$name)) $downloaded=true;
  268. }
  269. return($downloaded);
  270. }
  271. /**
  272. * @brief Removes an app
  273. * @param $name name of the application to remove
  274. * @param $options array with options
  275. * @returns true/false
  276. *
  277. * This function removes an app. $options is an associative array. The
  278. * following keys are optional:ja
  279. * - keeppreferences: boolean, if true the user preferences won't be deleted
  280. * - keepappconfig: boolean, if true the config will be kept
  281. * - keeptables: boolean, if true the database will be kept
  282. * - keepfiles: boolean, if true the user files will be kept
  283. *
  284. * This function works as follows
  285. * -# including appinfo/remove.php
  286. * -# removing the files
  287. *
  288. * The function will not delete preferences, tables and the configuration,
  289. * this has to be done by the function oc_app_uninstall().
  290. */
  291. public static function removeApp( $name, $options = array()) {
  292. if(isset($options['keeppreferences']) and $options['keeppreferences']==false ) {
  293. // todo
  294. // remove preferences
  295. }
  296. if(isset($options['keepappconfig']) and $options['keepappconfig']==false ) {
  297. // todo
  298. // remove app config
  299. }
  300. if(isset($options['keeptables']) and $options['keeptables']==false ) {
  301. // todo
  302. // remove app database tables
  303. }
  304. if(isset($options['keepfiles']) and $options['keepfiles']==false ) {
  305. // todo
  306. // remove user files
  307. }
  308. if(OC_Installer::isDownloaded( $name )) {
  309. $appdir=OC_App::getInstallPath().'/'.$name;
  310. OC_Helper::rmdirr($appdir);
  311. }else{
  312. OC_Log::write('core', 'can\'t remove app '.$name.'. It is not installed.', OC_Log::ERROR);
  313. }
  314. }
  315. /**
  316. * @brief Installs shipped apps
  317. *
  318. * This function installs all apps found in the 'apps' directory that should be enabled by default;
  319. */
  320. public static function installShippedApps() {
  321. foreach(OC::$APPSROOTS as $app_dir) {
  322. if($dir = opendir( $app_dir['path'] )) {
  323. while( false !== ( $filename = readdir( $dir ))) {
  324. if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) {
  325. if( file_exists( $app_dir['path']."/$filename/appinfo/app.php" )) {
  326. if(!OC_Installer::isInstalled($filename)) {
  327. $info=OC_App::getAppInfo($filename);
  328. $enabled = isset($info['default_enable']);
  329. if( $enabled ) {
  330. OC_Installer::installShippedApp($filename);
  331. OC_Appconfig::setValue($filename, 'enabled', 'yes');
  332. }
  333. }
  334. }
  335. }
  336. }
  337. closedir( $dir );
  338. }
  339. }
  340. }
  341. /**
  342. * install an app already placed in the app folder
  343. * @param string $app id of the app to install
  344. * @returns array see OC_App::getAppInfo
  345. */
  346. public static function installShippedApp($app) {
  347. //install the database
  348. if(is_file(OC_App::getAppPath($app)."/appinfo/database.xml")) {
  349. OC_DB::createDbFromStructure(OC_App::getAppPath($app)."/appinfo/database.xml");
  350. }
  351. //run appinfo/install.php
  352. if(is_file(OC_App::getAppPath($app)."/appinfo/install.php")) {
  353. include OC_App::getAppPath($app)."/appinfo/install.php";
  354. }
  355. $info=OC_App::getAppInfo($app);
  356. OC_Appconfig::setValue($app, 'installed_version', OC_App::getAppVersion($app));
  357. //set remote/public handelers
  358. foreach($info['remote'] as $name=>$path) {
  359. OCP\CONFIG::setAppValue('core', 'remote_'.$name, $app.'/'.$path);
  360. }
  361. foreach($info['public'] as $name=>$path) {
  362. OCP\CONFIG::setAppValue('core', 'public_'.$name, $app.'/'.$path);
  363. }
  364. OC_App::setAppTypes($info['id']);
  365. return $info['id'];
  366. }
  367. /**
  368. * check the code of an app with some static code checks
  369. * @param string $folder the folder of the app to check
  370. * @returns true for app is o.k. and false for app is not o.k.
  371. */
  372. public static function checkCode($appname, $folder) {
  373. $blacklist=array(
  374. 'exec(',
  375. 'eval(',
  376. // more evil pattern will go here later
  377. // classes replaced by the public api
  378. 'OC_API::',
  379. 'OC_App::',
  380. 'OC_AppConfig::',
  381. 'OC_Avatar',
  382. 'OC_BackgroundJob::',
  383. 'OC_Config::',
  384. 'OC_DB::',
  385. 'OC_Files::',
  386. 'OC_Helper::',
  387. 'OC_Hook::',
  388. 'OC_Image::',
  389. 'OC_JSON::',
  390. 'OC_L10N::',
  391. 'OC_Log::',
  392. 'OC_Mail::',
  393. 'OC_Preferences::',
  394. 'OC_Request::',
  395. 'OC_Response::',
  396. 'OC_Template::',
  397. 'OC_User::',
  398. 'OC_Util::',
  399. );
  400. // is the code checker enabled?
  401. if(OC_Config::getValue('appcodechecker', false)) {
  402. // check if grep is installed
  403. $grep = exec('which grep');
  404. if($grep=='') {
  405. OC_Log::write('core',
  406. 'grep not installed. So checking the code of the app "'.$appname.'" was not possible',
  407. OC_Log::ERROR);
  408. return true;
  409. }
  410. // iterate the bad patterns
  411. foreach($blacklist as $bl) {
  412. $cmd = 'grep -ri '.escapeshellarg($bl).' '.$folder.'';
  413. $result = exec($cmd);
  414. // bad pattern found
  415. if($result<>'') {
  416. OC_Log::write('core',
  417. 'App "'.$appname.'" is using a not allowed call "'.$bl.'". Installation refused.',
  418. OC_Log::ERROR);
  419. return false;
  420. }
  421. }
  422. return true;
  423. }else{
  424. return true;
  425. }
  426. }
  427. }