installer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. $basedir=OC::$APPSROOT.'/apps/'.$info['id'];
  125. // check the code for not allowed calls
  126. if(!OC_Installer::checkCode($info['id'],$extractDir)){
  127. OC_Log::write('core','App can\'t be installed because of not allowed code in the App',OC_Log::ERROR);
  128. OC_Helper::rmdirr($extractDir);
  129. return false;
  130. }
  131. // check if the app is compatible with this version of ownCloud
  132. $version=OC_Util::getVersion();
  133. if(!isset($info['require']) or ($version[0]>$info['require'])){
  134. OC_Log::write('core','App can\'t be installed because it is not compatible with this version of ownCloud',OC_Log::ERROR);
  135. OC_Helper::rmdirr($extractDir);
  136. return false;
  137. }
  138. //check if an app with the same id is already installed
  139. if(self::isInstalled( $info['id'] )){
  140. OC_Log::write('core','App already installed',OC_Log::WARN);
  141. OC_Helper::rmdirr($extractDir);
  142. if($data['source']=='http'){
  143. unlink($path);
  144. }
  145. return false;
  146. }
  147. //check if the destination directory already exists
  148. if(is_dir($basedir)){
  149. OC_Log::write('core','App directory already exists',OC_Log::WARN);
  150. OC_Helper::rmdirr($extractDir);
  151. if($data['source']=='http'){
  152. unlink($path);
  153. }
  154. return false;
  155. }
  156. if(isset($data['pretent']) and $data['pretent']==true){
  157. return false;
  158. }
  159. //copy the app to the correct place
  160. if(@!mkdir($basedir)){
  161. OC_Log::write('core','Can\'t create app folder. Please fix permissions. ('.$basedir.')',OC_Log::ERROR);
  162. OC_Helper::rmdirr($extractDir);
  163. if($data['source']=='http'){
  164. unlink($path);
  165. }
  166. return false;
  167. }
  168. OC_Helper::copyr($extractDir,$basedir);
  169. //remove temporary files
  170. OC_Helper::rmdirr($extractDir);
  171. //install the database
  172. if(is_file($basedir.'/appinfo/database.xml')){
  173. OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
  174. }
  175. //run appinfo/install.php
  176. if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')){
  177. include($basedir.'/appinfo/install.php');
  178. }
  179. //set the installed version
  180. OC_Appconfig::setValue($info['id'],'installed_version',OC_App::getAppVersion($info['id']));
  181. OC_Appconfig::setValue($info['id'],'enabled','no');
  182. //set remote/public handelers
  183. foreach($info['remote'] as $name=>$path){
  184. OCP\CONFIG::setAppValue('core', 'remote_'.$name, '/apps/'.$info['id'].'/'.$path);
  185. }
  186. foreach($info['public'] as $name=>$path){
  187. OCP\CONFIG::setAppValue('core', 'public_'.$name, '/apps/'.$info['id'].'/'.$path);
  188. }
  189. OC_App::setAppTypes($info['id']);
  190. return $info['id'];
  191. }
  192. /**
  193. * @brief checks whether or not an app is installed
  194. * @param $app app
  195. * @returns true/false
  196. *
  197. * Checks whether or not an app is installed, i.e. registered in apps table.
  198. */
  199. public static function isInstalled( $app ){
  200. if( null == OC_Appconfig::getValue( $app, "installed_version" )){
  201. return false;
  202. }
  203. return true;
  204. }
  205. /**
  206. * @brief Update an application
  207. * @param $data array with all information
  208. * @returns integer
  209. *
  210. * This function installs an app. All information needed are passed in the
  211. * associative array $data.
  212. * The following keys are required:
  213. * - source: string, can be "path" or "http"
  214. *
  215. * One of the following keys is required:
  216. * - path: path to the file containing the app
  217. * - href: link to the downloadable file containing the app
  218. *
  219. * The following keys are optional:
  220. * - pretend: boolean, if set true the system won't do anything
  221. * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded
  222. *
  223. * This function works as follows
  224. * -# fetching the file
  225. * -# removing the old files
  226. * -# unzipping new file
  227. * -# including appinfo/upgrade.php
  228. * -# setting the installed version
  229. *
  230. * upgrade.php can determine the current installed version of the app using "OC_Appconfig::getValue($appid,'installed_version')"
  231. */
  232. public static function upgradeApp( $data = array()){
  233. // TODO: write function
  234. return true;
  235. }
  236. /**
  237. * @brief Removes an app
  238. * @param $name name of the application to remove
  239. * @param $options array with options
  240. * @returns true/false
  241. *
  242. * This function removes an app. $options is an associative array. The
  243. * following keys are optional:ja
  244. * - keeppreferences: boolean, if true the user preferences won't be deleted
  245. * - keepappconfig: boolean, if true the config will be kept
  246. * - keeptables: boolean, if true the database will be kept
  247. * - keepfiles: boolean, if true the user files will be kept
  248. *
  249. * This function works as follows
  250. * -# including appinfo/remove.php
  251. * -# removing the files
  252. *
  253. * The function will not delete preferences, tables and the configuration,
  254. * this has to be done by the function oc_app_uninstall().
  255. */
  256. public static function removeApp( $name, $options = array()){
  257. // TODO: write function
  258. return true;
  259. }
  260. /**
  261. * @brief Installs shipped apps
  262. *
  263. * This function installs all apps found in the 'apps' directory that should be enabled by default;
  264. */
  265. public static function installShippedApps(){
  266. $dir = opendir( OC::$APPSROOT."/apps" );
  267. while( false !== ( $filename = readdir( $dir ))){
  268. if( substr( $filename, 0, 1 ) != '.' and is_dir(OC::$APPSROOT."/apps/$filename") ){
  269. if( file_exists( OC::$APPSROOT."/apps/$filename/appinfo/app.php" )){
  270. if(!OC_Installer::isInstalled($filename)){
  271. $info=OC_App::getAppInfo($filename);
  272. $enabled = isset($info['default_enable']);
  273. if( $enabled ){
  274. OC_Installer::installShippedApp($filename);
  275. OC_Appconfig::setValue($filename,'enabled','yes');
  276. }
  277. }
  278. }
  279. }
  280. }
  281. closedir( $dir );
  282. }
  283. /**
  284. * install an app already placed in the app folder
  285. * @param string $app id of the app to install
  286. * @returns array see OC_App::getAppInfo
  287. */
  288. public static function installShippedApp($app){
  289. //install the database
  290. if(is_file(OC::$APPSROOT."/apps/$app/appinfo/database.xml")){
  291. OC_DB::createDbFromStructure(OC::$APPSROOT."/apps/$app/appinfo/database.xml");
  292. }
  293. //run appinfo/install.php
  294. if(is_file(OC::$APPSROOT."/apps/$app/appinfo/install.php")){
  295. include(OC::$APPSROOT."/apps/$app/appinfo/install.php");
  296. }
  297. $info=OC_App::getAppInfo($app);
  298. OC_Appconfig::setValue($app,'installed_version',OC_App::getAppVersion($app));
  299. //set remote/public handelers
  300. foreach($info['remote'] as $name=>$path){
  301. OCP\CONFIG::setAppValue('core', 'remote_'.$name, '/apps/'.$app.'/'.$path);
  302. }
  303. foreach($info['public'] as $name=>$path){
  304. OCP\CONFIG::setAppValue('core', 'public_'.$name, '/apps/'.$app.'/'.$path);
  305. }
  306. OC_App::setAppTypes($info['id']);
  307. return $info;
  308. }
  309. /**
  310. * check the code of an app with some static code checks
  311. * @param string $folder the folder of the app to check
  312. * @returns true for app is o.k. and false for app is not o.k.
  313. */
  314. public static function checkCode($appname,$folder){
  315. $blacklist=array(
  316. 'exec(',
  317. 'eval('
  318. // more evil pattern will go here later
  319. // will will also check if an app is using private api once the public api is in place
  320. );
  321. // is the code checker enabled?
  322. if(OC_Config::getValue('appcodechecker', false)){
  323. // check if grep is installed
  324. $grep = exec('which grep');
  325. if($grep=='') {
  326. OC_Log::write('core','grep not installed. So checking the code of the app "'.$appname.'" was not possible',OC_Log::ERROR);
  327. return true;
  328. }
  329. // iterate the bad patterns
  330. foreach($blacklist as $bl) {
  331. $cmd = 'grep -ri '.escapeshellarg($bl).' '.$folder.'';
  332. $result = exec($cmd);
  333. // bad pattern found
  334. if($result<>'') {
  335. OC_Log::write('core','App "'.$appname.'" is using a not allowed call "'.$bl.'". Installation refused.',OC_Log::ERROR);
  336. return false;
  337. }
  338. }
  339. return true;
  340. }else{
  341. return true;
  342. }
  343. }
  344. }