forked from craigk5n/webcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_remotes_handler.php
211 lines (192 loc) · 7.13 KB
/
edit_remotes_handler.php
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
<?php
require_once 'includes/init.php';
require_once 'includes/xcal.php';
// Only available in php 5.x Used for hCalendar parsing.
if ( function_exists ( 'simplexml_load_string' ) )
require_once 'includes/classes/hKit/hkit.class.php';
$error = '';
$layer_found = false;
$save = getPostValue ( 'Save' );
$add = getPostValue ( 'Add' );
$delete = getPostValue ( 'delete' );
$reload = getPostValue ( 'reload' );
$nid = getPostValue ( 'nid' );
$nfirstname = getPostValue ( 'nfirstname' );
$nlastname = getPostValue ( 'nlastname' );
$nadmin = getPostValue ( 'nadmin' );
$nurl = getPostValue ( 'nurl' );
$reload = getPostValue ( 'reload' );
$nlayer = getPostValue ( 'nlayer' );
$nlayercolor = getPostValue ( 'layercolor' );
if ( ! empty ( $delete ) ) {
// Delete events from this remote calendar.
delete_events ( $nid );
// Delete any layers other users may have that point to this user.
dbi_execute ( 'DELETE FROM webcal_user_layers WHERE cal_layeruser = ?',
[$nid] );
// Delete any UAC calendar access entries for this user.
dbi_execute ( 'DELETE FROM webcal_access_user WHERE cal_login = ?
OR cal_other_user = ?', [$nid, $nid] );
// Delete any UAC function access entries for this user.
dbi_execute ( 'DELETE FROM webcal_access_function WHERE cal_login = ?',
[$nid] );
// Delete user.
if ( ! dbi_execute ( 'DELETE FROM webcal_nonuser_cals WHERE cal_login = ?',
[$nid] ) )
$error = db_error();
} else {
if ( ! empty ( $nid ) && ! empty ( $save ) ) {
// Updating
$query_params = [];
$sql = 'UPDATE webcal_nonuser_cals SET ';
if ( $nlastname ) {
$sql .= ' cal_lastname = ?, ';
$query_params[] = $nlastname;
}
if ( $nfirstname ) {
$sql .= ' cal_firstname = ?, ';
$query_params[] = $nfirstname;
}
$sql .= ' cal_url = ?, cal_is_public = ?, cal_admin = ?
WHERE cal_login = ?';
$query_params[] = $nurl;
$query_params[] = 'N';
$query_params[] = $nadmin;
$query_params[] = $nid;
if ( ! dbi_execute ( $sql, $query_params ) )
$error = db_error();
} else
if ( ! empty ( $add ) ) {
// Adding
if ( preg_match ( '/^[\w]+$/', $nid ) ) {
$nid = $NONUSER_PREFIX . $nid;
if ( ! dbi_execute ( 'INSERT INTO webcal_nonuser_cals ( cal_login,
cal_firstname, cal_lastname, cal_admin, cal_is_public, cal_url )
VALUES ( ?, ?, ?, ?, ?, ? )',
[$nid, $nfirstname, $nlastname, $nadmin, 'N', $nurl] ) )
$error = db_error();
} else
$error = translate( 'Calendar ID' )
. translate( 'word characters only' );
// Add new layer if requested.
if ( ! empty ( $nlayer ) && $nlayer == 'Y' ) {
$res = dbi_execute ( 'SELECT MAX( cal_layerid ) FROM webcal_user_layers' );
$layerid = 1;
if ( $res ) {
$row = dbi_fetch_row ( $res );
$layerid += $row[0];
}
dbi_execute ( 'INSERT INTO webcal_user_layers ( cal_layerid, cal_login,
cal_layeruser, cal_color, cal_dups ) VALUES ( ?, ?, ?, ?, ? )',
[$layerid, $login, $nid, $layercolor, 'N'] );
$layer_found = true;
}
}
// Add entry in UAC access table for new admin and remove for old admin.
// First delete any record for this user/nuc combo.
dbi_execute ( 'DELETE FROM webcal_access_user WHERE cal_login = ?
AND cal_other_user = ?', [$nadmin, $nid] );
if ( ! dbi_execute ( 'INSERT INTO webcal_access_user ( cal_login,
cal_other_user, cal_can_view, cal_can_edit, cal_can_approve, cal_can_invite,
cal_can_email, cal_see_time_only ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )',
[$nadmin, $nid, 511, 511, 511, 'Y', 'Y', 'N'] ) )
die_miserable_death ( translate ( 'Database error' ) . ': '
. dbi_error() );
}
if ( ! empty ( $reload ) ) {
$data = [];
$calUser = $nid;
$overwrite = true;
$type = 'remoteics';
// We will check ics first.
$data = parse_ical ( $nurl, $type );
// TODO it may be a vcs file.
// if ( count ( $data ) == 0 ) {
// $data = parse_vcal ( $nurl );
// }
// We may be processing an hCalendar.
// $data sometimes has a count of 1 but is not a valid array.
if ( ( count ( $data ) == 0 || ! isset ( $data[0] ) ) &&
function_exists ( 'simplexml_load_string' ) ) {
$h = new hKit;
$h->tidy_mode = 'proxy';
$result = $h->getByURL ( 'hcal', $nurl );
$type = 'hcal';
$data = parse_hcal ( $result, $type );
}
$errorStr = '<br><br>
<b>' . translate ( 'Error' ) . ':</b> ';
print_header ( [], '', '', true, false, true );
if ( count ( $data ) && empty ( $errormsg ) ) {
// Delete existing events.
delete_events ( $nid );
// Import new events.
import_data ( $data, $overwrite, $type );
echo '
<p>' . translate ( 'Import Results' ) . '</p><br><br>
' . translate ( 'Events successfully imported' ) . ': ' . $count_suc
. '<br>';
if ( $layer_found == false ) { // We may have just added layer.
load_user_layers();
foreach ( $layers as $layer ) {
if ( $layer['cal_layeruser'] == $nid )
$layer_found = true;
}
}
if ( $layer_found == false )
echo '
<p>' . translate( 'Create a new layer to view this calendar.' ) . '</p>';
} elseif ( ! empty ( $errormsg ) ) {
echo '
' . translate ( 'Errors' ) . ': ' . $error_num . '<br><br>
' . $errorStr . $errormsg . '<br>';
} else {
echo $errorStr .
translate( 'There was an error parsing the import file or no events were returned.' )
. '<br>';
}
echo print_trailer ( false, true, true );
}
function delete_events ( $nid ) {
// Get event ids for all events this user is a participant.
$events = get_users_event_ids ( $nid );
// Now count number of participants in each event...
// If just 1, then save id to be deleted.
$delete_em = [];
for ( $i = 0, $cnt = count ( $events ); $i < $cnt; $i++ ) {
$res = dbi_execute ( 'SELECT COUNT( * ) FROM webcal_entry_user
WHERE cal_id = ?', [$events[$i]] );
if ( $res ) {
$row = dbi_fetch_row ( $res );
if ( ! empty ( $row ) && $row[0] == 1 )
$delete_em[] = $events[$i];
dbi_free_result ( $res );
}
}
// Now delete events that were just for this user.
for ( $i = 0, $cnt = count ( $delete_em ); $i < $cnt; $i++ ) {
dbi_execute ( 'DELETE FROM webcal_entry_repeats WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_repeats_not WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_log WHERE cal_entry_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_import_data WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_site_extras WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry_ext_user WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_reminders WHERE cal_id =? ',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_blob WHERE cal_id = ?',
[$delete_em[$i]] );
dbi_execute ( 'DELETE FROM webcal_entry WHERE cal_id = ?',
[$delete_em[$i]] );
}
// Delete user participation from events.
dbi_execute ( 'DELETE FROM webcal_entry_user WHERE cal_login = ?',
[$nid] );
}
echo error_check ( 'users.php?tab=remotes', false );
?>