installer.php 14 KB

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