summaryrefslogtreecommitdiffstats
path: root/lib/installer.php
blob: 9416a42c972ee8b4a5e2f2b06549527b61ab55bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
 * ownCloud
 *
 * @author Robin Appelman
 * @copyright 2010 Frank Karlitschek karlitschek@kde.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * This class provides the functionality needed to install, update and remove plugins/apps
 */
class OC_Installer{
	/**
	 * @brief Installs an app
	 * @param $data array with all information
	 * @returns integer
	 *
	 * This function installs an app. All information needed are passed in the
	 * associative array $data.
	 * The following keys are required:
	 *   - source: string, can be "path" or "http"
	 *
	 * One of the following keys is required:
	 *   - path: path to the file containing the app
	 *   - href: link to the downloadable file containing the app
	 *
	 * The following keys are optional:
	 *   - pretend: boolean, if set true the system won't do anything
	 *   - noinstall: boolean, if true appinfo/install.php won't be loaded
	 *   - inactive: boolean, if set true the appconfig/app.sample.php won't be
	 *     renamed
	 *
	 * This function works as follows
	 *   -# fetching the file
	 *   -# unzipping it
	 *   -# installing the database at appinfo/database.xml
	 *   -# including appinfo/install.php
	 *   -# setting the installed version
	 *
	 * It is the task of oc_app_install to create the tables and do whatever is
	 * needed to get the app working.
	 */
	public static function installApp( $data = array()){
		if(!isset($data['source'])){
			if(defined("DEBUG") && DEBUG) {error_log("No source specified when installing app");}
			return false;
		}
		
		//download the file if necesary
		if($data['source']=='http'){
			$path=tempnam(sys_get_temp_dir(),'oc_installer_');
			if(!isset($data['href'])){
				if(defined("DEBUG") && DEBUG) {error_log("No href specified when installing app from http");}
				return false;
			}
			copy($data['href'],$path);
		}else{
			if(!isset($data['path'])){
				if(defined("DEBUG") && DEBUG) {error_log("No path specified when installing app from local file");}
				return false;
			}
			$path=$data['path'];
		}
		
		//extract the archive in a temporary folder
		$extractDir=tempnam(sys_get_temp_dir(),'oc_installer_uncompressed_');
		unlink($extractDir);
		mkdir($extractDir);
		$zip = new ZipArchive;
		if($zip->open($path)===true){
			$zip->extractTo($extractDir);
			$zip->close();
		} else {
			if(defined("DEBUG") && DEBUG) {error_log("Failed to open archive when installing app");}
			OC_Helper::rmdirr($extractDir);
			if($data['source']=='http'){
				unlink($path);
			}
			return false;
		}
		
		//load the info.xml file of the app
		if(!is_file($extractDir.'/appinfo/info.xml')){
			if(defined("DEBUG") && DEBUG) {error_log("App does not provide an info.xml file");}
			OC_Helper::rmdirr($extractDir);
			if($data['source']=='http'){
				unlink($path);
			}
			return false;
		}
		$info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml');
		$basedir=OC::$SERVERROOT.'/apps/'.$info['id'];
		
		//check if an app with the same id is already installed
		if(self::isInstalled( $info['id'] )){
			if(defined("DEBUG") && DEBUG) {error_log("App already installed");}
			OC_Helper::rmdirr($extractDir);
			if($data['source']=='http'){
				unlink($path);
			}
			return false;
		}

		//check if the destination directory already exists
		if(is_dir($basedir)){
			if(defined("DEBUG") && DEBUG) {error_log("App's directory already exists");}
			OC_Helper::rmdirr($extractDir);
			if($data['source']=='http'){
				unlink($path);
			}
			return false;
		}
		
		if(isset($data['pretent']) and $data['pretent']==true){
			return false;
		}
		
		//copy the app to the correct place
		if(!mkdir($basedir)){
			if(defined("DEBUG") && DEBUG) {error_log('Can\'t create app folder ('.$basedir.')');}
			OC_Helper::rmdirr($extractDir);
			if($data['source']=='http'){
				unlink($path);
			}
			return false;
		}
		OC_Helper::copyr($extractDir,$basedir);
		
		//remove temporary files
		OC_Helper::rmdirr($extractDir);
		if($data['source']=='http'){
			unlink($path);
		}
		
		//install the database
		if(is_file($basedir.'/appinfo/database.xml')){
			OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
		}
		
		//run appinfo/install.php
		if(!isset($data['noinstall']) or $data['noinstall']==false and is_file($basedir.'/appinfo/install.php')){
			include($basedir.'/appinfo/install.php');
		}
		
		//set the installed version
		OC_Appconfig::setValue($info['id'],'installed_version',$info['version']);
		OC_Appconfig::setValue($info['id'],'enabled','no');
		return true;
	}

	/**
	 * @brief checks whether or not an app is installed
	 * @param $app app
	 * @returns true/false
	 *
	 * Checks whether or not an app is installed, i.e. registered in apps table.
	 */
	public static function isInstalled( $app ){

		if( null == OC_Appconfig::getValue( $app, "installed_version" )){
			return false;
		}

		return true;
	}

	/**
	 * @brief Update an application
	 * @param $data array with all information
	 * @returns integer
	 *
	 * This function installs an app. All information needed are passed in the
	 * associative array $data.
	 * The following keys are required:
	 *   - source: string, can be "path" or "http"
	 *
	 * One of the following keys is required:
	 *   - path: path to the file containing the app
	 *   - href: link to the downloadable file containing the app
	 *
	 * The following keys are optional:
	 *   - pretend: boolean, if set true the system won't do anything
	 *   - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded
	 *
	 * This function works as follows
	 *   -# fetching the file
	 *   -# removing the old files
	 *   -# unzipping new file
	 *   -# including appinfo/upgrade.php
	 *   -# setting the installed version
	 *
	 * upgrade.php can determine the current installed version of the app using "OC_Appconfig::getValue($appid,'installed_version')"
	 */
	public static function upgradeApp( $data = array()){
		// TODO: write function
		return true;
	}

	/**
	 * @brief Removes an app
	 * @param $name name of the application to remove
	 * @param $options array with options
	 * @returns true/false
	 *
	 * This function removes an app. $options is an associative array. The
	 * following keys are optional:ja
	 *   - keeppreferences: boolean, if true the user preferences won't be deleted
	 *   - keepappconfig: boolean, if true the config will be kept
	 *   - keeptables: boolean, if true the database will be kept
	 *   - keepfiles: boolean, if true the user files will be kept
	 *
	 * This function works as follows
	 *   -# including appinfo/remove.php
	 *   -# removing the files
	 *
	 * The function will not delete preferences, tables and the configuration,
	 * this has to be done by the function oc_app_uninstall().
	 */
	public static function removeApp( $name, $options = array()){
		// TODO: write function
		return true;
	}

	/**
	 * @brief Installs shipped apps
	 * @param $enabled
	 *
	 * This function installs all apps found in the 'apps' directory;
	 * If $enabled is true, apps are installed as enabled.
	 * If $enabled is false, apps are installed as disabled.
	 */
	public static function installShippedApps( $enabled ){
		$dir = opendir( OC::$SERVERROOT."/apps" );
		while( false !== ( $filename = readdir( $dir ))){
			if( substr( $filename, 0, 1 ) != '.' and is_dir(OC::$SERVERROOT."/apps/$filename") ){
				if( file_exists( OC::$SERVERROOT."/apps/$filename/appinfo/app.php" )){
					if(!OC_Installer::isInstalled($filename)){
						OC_Installer::installShippedApp($filename);
						if( $enabled ){
							OC_Appconfig::setValue($filename,'enabled','yes');
						}else{
							OC_Appconfig::setValue($filename,'enabled','no');
						}
					}
				}
			}
		}
		closedir( $dir );
	}

	/**
	 * install an app already placed in the app folder
	 * @param string $app id of the app to install
	 * @return bool
	 */
	public static function installShippedApp($app){
		//install the database
		if(is_file(OC::$SERVERROOT."/apps/$app/appinfo/database.xml")){
			OC_DB::createDbFromStructure(OC::$SERVERROOT."/apps/$app/appinfo/database.xml");
		}

		//run appinfo/install.php
		if(is_file(OC::$SERVERROOT."/apps/$app/appinfo/install.php")){
			include(OC::$SERVERROOT."/apps/$app/appinfo/install.php");
		}
		$info=OC_App::getAppInfo(OC::$SERVERROOT."/apps/$app/appinfo/info.xml");
		OC_Appconfig::setValue($app,'installed_version',$info['version']);
	}
}