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

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