aboutsummaryrefslogtreecommitdiffstats
path: root/functions/admin_functions.php
blob: d3ba2d592a53af41754472b679bb55cb6725d1b1 (plain) (blame)
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
<?php
// Is the user logged in
//
// returns boolean is the user logged in
function is_loggedin () {
	global $_SESSION;
	if (!isset($_SESSION['phpical_loggedin']) || $_SESSION['phpical_loggedin'] == FALSE) {
		return FALSE;
	}
	else
		return TRUE;
}

// Attempt to login. If login is valid, set the session variable 'phpical_loggedin' to TRUE and store the username and password in the session
//
// arg0: string username
// arg1: string password
// returns boolean was the login successful
function login ($username, $password) {
	global $_SESSION;
	global $phpiCal_config;
	
	switch ($phpiCal_config->auth_method) {
		case 'ftp':
			$loggedin = login_ftp($username, $password);
			break;
		case 'internal':
			$loggedin = login_internal($username, $password);
			break;
		default:
			$loggedin = FALSE;
	}
	
	$_SESSION['phpical_loggedin'] = $loggedin;
	if ($loggedin) {
		$_SESSION['phpical_username'] = $username;
		$_SESSION['phpical_password'] = $password;
	}
	
	return $loggedin;
}


// Attempt to login to the ftp server
//
// arg0: string username
// arg1: string password
// returns boolean was login successful
function login_ftp ($username, $password) {
	global $phpiCal_config;
	
	// set up basic connection
	$conn_id = @ftp_connect($phpiCal_config->ftp_server, $phpiCal_config->ftp_port); 
	if (!$conn_id) exit(error('Cannot connect to FTP server', $filename));
	// login with username and password
	$login_result = @ftp_login($conn_id, $username, $password); 
	
	// check connection
	if ((!$conn_id) || (!$login_result)) { 
		return FALSE;
	}
	
	// close the FTP stream 
	@ftp_close($conn_id);
	
	return TRUE;
}

// Attempt to login using username and password defined in config.inc.php
//
// arg0: string username
// arg1: string password
// returns boolean was login successful
function login_internal ($username, $password) {
	global $phpiCal_config;
	
	if ($phpiCal_config->auth_internal_username == $username && $phpiCal_config->auth_internal_password == $password)
		return TRUE;
	else
		return FALSE;
}

// Delete a calendar. If using ftp for authentication, use ftp to delete. Otherwise, use file system functions.
//
// arg0: string calendar file - not the full path
// returns boolean was delete successful
function delete_cal ($filename) {
	global $_SESSION;
	global $phpiCal_config;
	global $calendar_path;
	echo $filename;
	
	if ($phpiCal_config->auth_method == 'ftp') {
		$filename = get_ftp_calendar_path() . "/" . $filename;
		
		// set up basic connection
		$conn_id = @ftp_connect($phpiCal_config->ftp_server); 
		
		// login with username and password
		$login_result = @ftp_login($conn_id, $_SESSION['phpical_username'], $_SESSION['phpical_password']); 
		
		// check connection
		if ((!$conn_id) || (!$login_result))
			return FALSE;
		
		// delete the file
		$delete = @ftp_delete($conn_id, $filename); 
		
		// check delete status
		if (!$delete)
			return FALSE;
		
		// close the FTP stream 
		@ftp_close($conn_id);
		
		return TRUE;
	} else {
		#$filename = $calendar_path . "/" . $filename;
		$delete = unlink($filename); 
		clearstatcache();
		if (@file_exists($filename)) { 
			$filesys = preg_match("/\//","\\", $filename); 
			$delete = system("rm -f $filesys");
			clearstatcache();
			if (@file_exists($filename)) { 
				$delete = chmod ($filename, 0775); 
				$delete = unlink($filename); 
				$delete = system("del $filesys");
			}
		}
		clearstatcache();
		if (@file_exists($filename)) {
			return FALSE;
		}
		else {
			return TRUE;
		}
		
		return TRUE;
	}
}

// Copy the uploaded calendar. If using ftp for authentication, use ftp to copy. Otherwise, use file system functions.
//
// arg0: string full path to calendar file
// arg1: string destination filename
// returns boolean was copy successful
function copy_cal ($source, $destination) {
	global $_SESSION;
	global $phpiCal_config;
	global $calendar_path;
	
	if ($phpiCal_config->auth_method == 'ftp') {
		$destination = get_ftp_calendar_path() . "/" . basename($destination);
		$destination = str_replace ("\\", "/", realpath($destination));
		
		// set up basic connection
		$conn_id = ftp_connect($phpiCal_config->ftp_server); 
		
		// login with username and password
		$login_result = ftp_login($conn_id, $_SESSION['phpical_username'], $_SESSION['phpical_password']); 
		
		// check connection
		if ((!$conn_id) || (!$login_result))
			return FALSE;
		
		// upload the file
		$upload = ftp_put($conn_id, $destination, $source, FTP_ASCII); 
		
		// check upload status
		if (!$upload)
			return FALSE;
		
		// close the FTP stream 
		ftp_close($conn_id);
		
		return TRUE;
	}
	else {
		$destination = $calendar_path . "/" . basename($destination);
		
		if (check_php_version('4.0.3')) {
			return move_uploaded_file($source, $destination);
		}
		else {
			return copy($source, $destination);
		}
	}
}

// Find the full path to the caledar directory for use with ftp
//  if $ftp_calendar_path == '', sends back the full path to the $calendar_path - this may not work depending 
//  on ftp server config, but would be a best guess
//
// return string path to calendar directory for ftp operations
function get_ftp_calendar_path() {
	global $phpiCal_config;
	global $calendar_path;
	
	if ($phpiCal_config->ftp_calendar_path != '')
		return $phpiCal_config->ftp_calendar_path;
	else {
		return str_replace ("\\", "/", realpath($calendar_path));
	}
}

// Check to see if the current version of php is >= to the arguement
//
// arg0: string version of php to check against
// return boolean true if $version is >= current php version
function check_php_version($version) {
	// intval used for version like "4.0.4pl1"
	$testVer=intval(str_replace(".", "",$version));
	$curVer=intval(str_replace(".", "",phpversion()));
	if( $curVer < $testVer )
		return FALSE;
	return TRUE;
}

// Is the file uploaded truly a file via HTTP POST - used to thwart a user from trying to trick the script from working on other files
//
// arg0: string filename
// returns boolean is the uploaded a file
function is_uploaded_file_v4 ($filename) {
    if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
        $tmp_file = dirname(tempnam('', ''));
    }
    $tmp_file .= '/' . basename($filename);
    // For Windows compat
    $filename = str_replace ("\\", "/", $filename);
    $tmp_file = str_replace ("\\", "/", $tmp_file);
    // User might have trailing slash in php.ini... 
    return (preg_replace('/\/+/', '/', $tmp_file) == $filename);
}

// return the appropriate error message if the file upload had an error
//
// arg0: array error number from $_FILES[file]['error']
// returns string error message
function get_upload_error ($upload_error) {
	global $php_error_lang;
	global $upload_error_lang;
	global $upload_error_gen_lang;
	
	if (isset($upload_error)) {
		// This is only available in PHP >= 4.2.0
		$error = $php_error_lang . " ";
		switch($upload_error) {
			case 0: //no error; possible file attack!
			case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
			case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
			case 3: //uploaded file was only partially uploaded
			case 4: //no file was uploaded
				$error = $error . $upload_error . ": " . $upload_error_lang[$upload_error];
				break;
			default: //a default error, just in case!  :)
				$error = $error . $upload_error . ": " . $upload_error_gen_lang;
				break;
		}
	}
	else {
		$error = $upload_error_gen_lang;
	}
	
	return $error;
}

// Check to see that the file has an .ics extension
//
// arg0: string filename
// returns booloean does the filename end in .ics
function is_uploaded_ics ($filename) {
	// Check the file extension for .ics. Can also check the the mime type, but it's not reliable so why bother...
	if(preg_match("/.ics$/i", $filename)) {
		return TRUE;
	}
	else {
		return FALSE;
	}
}

?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy