File: import_handler.php

package info (click to toggle)
webcalendar 0.9.45-4sarge7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,888 kB
  • ctags: 4,775
  • sloc: php: 16,990; sql: 1,272; perl: 777; sh: 120; makefile: 45
file content (529 lines) | stat: -rw-r--r-- 20,655 bytes parent folder | download
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php

include_once 'includes/init.php';
include_once 'includes/site_extras.php';
$error = '';
print_header();

$overwrite = getValue("overwrite");
$doOverwrite = ( empty ( $overwrite ) || $overwrite != 'Y' ) ? false : true;
$numDeleted = 0;

$sqlLog = '';

if ( ! empty ( $_FILES['FileName'] ) )
  $file = $_FILES['FileName'];
else if ( ! empty ( $HTTP_POST_FILES['FileName'] ) )
  $file = $HTTP_POST_FILES['FileName'];

if ( empty ( $file ) )
  echo "No file!<br />";

// Handle user
$calUser = getValue ( "calUser" );
if ( ! empty ( $calUser ) ) {
  if ( $single_user == "N" && ! $is_admin ) $calUser = $login;
} else {
  $calUser = $login;
}

if ($file['size'] > 0) {
  switch ($ImportType) {

// ADD New modules here:

//    case 'MODULE':
//      include "import_module.php";
//      $data = parse_module($HTTP_POST_FILES['FileName']['tmp_name']);
//      break;
//
    case 'PALMDESKTOP':
      include "import_palmdesktop.php";
      if (delete_palm_events($login) != 1) $errormsg = "Error deleting palm events from webcalendar.";
      $data = parse_palmdesktop($file['tmp_name'], $exc_private);
      $type = 'palm';
      break;

    case 'VCAL':
      include "import_vcal.php";
      $data = parse_vcal($file['tmp_name']);
      $type = 'vcal';
      break;

    case 'ICAL':
      include "import_ical.php";
      $data = parse_ical($file['tmp_name']);
      $type = 'ical';
      break;
  }

  $count_con = $count_suc = $error_num = 0;
  if (! empty ($data) && empty ($errormsg) ) {
    import_data ( $data, $doOverwrite, $type );
    echo "<p>" . translate("Import Results") . "</p>\n<br /><br />\n" .
      translate("Events successfully imported") . " : $count_suc<br />\n";
    echo translate("Events from prior import marked as deleted") .
      ": $numDeleted <br />\n";
    if ( empty ( $allow_conflicts ) ) {
      echo translate("Conflicting events") . ": " . $count_con . "<br />\n";
    }
    echo translate ( "Errors" ) . ": $error_num<br>\n<br>\n";
  } elseif ($errormsg) {
    echo "<br /><br />\n<b>" . translate("Error") . ":</b> $errormsg<br />\n";
  } else {
    echo "<br /><br />\n<b>" . translate("Error") . ":</b> " .
      translate("There was an error parsing the import file or no events were returned") .
      ".<br />\n";
  }
} else {
	echo "<br /><br />\n<b>" . translate("Error") . ":</b> " .
    translate("The import file contained no data") . ".<br />\n";
}


//echo "<hr />$sqlLog\n";

print_trailer ();
echo "</body>\n</html>";

/* Import the data structure
$Entry[RecordID]           =  Record ID (in the Palm) ** only required for palm desktop
$Entry[StartTime]          =  In seconds since 1970 (Unix Epoch)
$Entry[EndTime]            =  In seconds since 1970 (Unix Epoch)
$Entry[Summary]            =  Summary of event (string)
$Entry[Duration]           =  How long the event lasts (in minutes)
$Entry[Description]        =  Full Description (string)
$Entry[Untimed]            =  1 = true  0 = false
$Entry[Private]            =  1 = true  0 = false
$Entry[Category]           =  useless for Palm (not supported yet)
$Entry[AlarmSet]           =  1 = true  0 = false
$Entry[AlarmAdvanceAmount] =  How many units in AlarmAdvanceType (-1 means not set)
$Entry[AlarmAdvanceType]   =  Units: (0=minutes, 1=hours, 2=days)
$Entry[Repeat]             =  Array containing repeat information (if repeat)
$Entry[Repeat][Interval]   =  1=daily,2=weekly,3=MonthlyByDay,4=MonthlyByDate,5=Yearly,6=monthlyByDayR
$Entry[Repeat][Frequency]  =  How often event occurs. (1=every, 2=every other,etc.)
$Entry[Repeat][EndTime]    =  When the repeat ends (In seconds since 1970 (Unix Epoch))
$Entry[Repeat][Exceptions] =  Exceptions to the repeat (In seconds since 1970 (Unix Epoch))
$Entry[Repeat][RepeatDays] =  For Weekly: What days to repeat on (7 characters...y or n for each day)
*/
//
// TODO: Figure out category from $Entry[Category] or have a drop-down asking which
//       category to import into.
//
function import_data ( $data, $overwrite, $type ) {
  global $login, $count_con, $count_suc, $error_num, $ImportType, $LOG_CREATE;
  global $single_user, $single_user_login, $allow_conflicts;
  global $numDeleted, $errormsg;
  global $calUser, $H2COLOR, $sqlLog;

  $oldUIDs = array ();
  $oldIds = array ();
  $firstEventId = 0;
  $importId = 1;

  // Generate a unique import id
  $res = dbi_query ( "SELECT MAX(cal_import_id) FROM webcal_import" );
  if ( $res ) {
    if ( $row = dbi_fetch_row ( $res ) ) {
      $importId = $row[0] + 1;
    }
    dbi_free_result ( $res );
  }
  $sql = "INSERT INTO webcal_import ( cal_import_id, cal_name, " .
    "cal_date, cal_type, cal_login ) VALUES ( $importId, NULL, " .
    date("Ymd") . ", '$type', '$login' )";
  if ( ! dbi_query ( $sql ) ) {
    $errormsg = translate("Database error") . ": " . dbi_error ();
    return;
  }

  foreach ( $data as $Entry ){

    $priority = 2;
    $participants[0] = $calUser;

    // Some additional date/time info
    $START = $Entry['StartTime'] > 0 ? localtime($Entry['StartTime']) : 0;
    $END   = $Entry['EndTime'] > 0 ? localtime($Entry['EndTime']) : 0;
    $Entry['StartMinute']        = sprintf ("%02d",$START[1]);
    $Entry['StartHour']          = sprintf ("%02d",$START[2]);
    $Entry['StartDay']           = sprintf ("%02d",$START[3]);
    $Entry['StartMonth']         = sprintf ("%02d",$START[4] + 1);
    $Entry['StartYear']          = sprintf ("%04d",$START[5] + 1900);
    $Entry['EndMinute']          = sprintf ("%02d",$END[1]);
    $Entry['EndHour']            = sprintf ("%02d",$END[2]);
    $Entry['EndDay']             = sprintf ("%02d",$END[3]);
    $Entry['EndMonth']           = sprintf ("%02d",$END[4] + 1);
    $Entry['EndYear']            = sprintf ("%04d",$END[5] + 1900);
    if ( $overwrite && ! empty ( $Entry['UID'] ) ) {
      $oldUIDs[$Entry['UID']]++;
    }

    // Check for untimed
    if ($Entry['Untimed'] == 1) {
      $Entry['StartMinute'] = '';
      $Entry['StartHour'] = '';
      $Entry['EndMinute'] = '';
      $Entry['EndHour'] = '';
    }

    // first check for any schedule conflicts
    if ( empty ( $allow_conflicts )  &&  ( $Entry['Duration'] != 0 )) {
      $date = mktime (0,0,0,$Entry['StartMonth'],
        $Entry['StartDay'],$Entry['StartYear']);
      $endt =  (! empty ( $Entry['Repeat']['EndTime'] ) ) ? $Entry['Repeat']['EndTime'] : 'NULL';
      $dayst =  (! empty ( $Entry['Repeat']['RepeatDays'] ) ) ? $Entry['Repeat']['RepeatDays'] : "nnnnnnn";

      $ex_days = array ();
      if ( ! empty ( $Entry['Repeat']['Exceptions'] ) ) {
        foreach ($Entry['Repeat']['Exceptions'] as $ex_date) {
          $ex_days[] = date("Ymd",$ex_date);
        }
      }

      $dates = get_all_dates($date, RepeatType($Entry['Repeat']['Interval']), $endt, $dayst, $ex_days, $Entry['Repeat']['Frequency']);
      $overlap = overlap ( $dates, $Entry['Duration'], $Entry['StartHour'], $Entry['StartMinute'], $participants, $login, 0 );
    }

    if ( empty ( $error ) && ! empty ( $overlap ) ) {
      $error = translate("The following conflicts with the suggested time").":<ul>$overlap</ul>\n";
    }

    if ( empty ( $error ) ) {

      $updateMode = false;

      // See if event already is there from prior import.
      // The same UID is used for all events imported at once with iCal.
      // So, we still don't have enough info to find the exact
      // event we want to replace.  We could just delete all
      // existing events that correspond to the UID.
/************************************************************************
  Not sure what to do with this code since I don't know how Palm and vCal
  use the UID stuff yet...
  
      if ( ! empty ( $Entry['UID'] ) ) {
        $res = dbi_query ( "SELECT webcal_import_data.cal_id " .
          "FROM webcal_import_data, webcal_entry_user " .
          "WHERE cal_import_type = 'ical' AND " .
          "webcal_import_data.cal_id = webcal_entry_user.cal_id AND " .
          "webcal_entry_user.cal_login = '$login' AND " .
          "cal_external_id = '$Entry[UID]'" );
        if ( $res ) {
          if ( $row = dbi_fetch_row ( $res ) ) {
            if ( ! empty ( $row[0] ) ) {
              $id = $row[0];
              $updateMode = true;
              // update rather than add a new event
            }
          }
        }
      }
************************************************************************/

      // Add the Event
      $res = dbi_query ( "SELECT MAX(cal_id) FROM webcal_entry" );
      if ( $res ) {
        $row = dbi_fetch_row ( $res );
        $id = $row[0] + 1;
        dbi_free_result ( $res );
      } else {
        $id = 1;
        //$error = "Unable to select MAX cal_id: " . dbi_error () . "<br /><br />\n<b>SQL:</b> $sql";
        //break;
      }
      if ( $firstEventId == 0 )
        $firstEventId = $id;

      $names = array ();
      $values = array ();
      $names[] = 'cal_id';
      $values[] = "$id";
      if ( ! $updateMode ) {
        $names[] = 'cal_create_by';
        $values[] = "'$login'";
      }
      $names[] = 'cal_date';
      $values[] = sprintf ( "%04d%02d%02d",
        $Entry['StartYear'],$Entry['StartMonth'],$Entry['StartDay']);
      $names[] = 'cal_time';
      $values[] = ($Entry['Untimed'] == 1) ? "-1" :
        sprintf ( "%02d%02d00", $Entry['StartHour'],$Entry['StartMinute']);
      $names[] = 'cal_mod_date';
      $values[] = date("Ymd");
      $names[] = 'cal_mod_time';
      $values[] = date("Gis");
      $names[] = 'cal_duration';
      $values[] = sprintf ( "%d", $Entry['Duration'] );
      $names[] = 'cal_priority';
      $values[] = $priority;
      $names[] = 'cal_access';
      $values[] = ($Entry['Private'] == 1) ? "'R'" : "'P'";
      $names[] = 'cal_type';
      $values[] = ($Entry['Repeat']) ? "'M'" : "'E'";

      if ( strlen ( $Entry['Summary'] ) == 0 )
        $Entry['Summary'] = translate("Unnamed Event");
      if ( strlen ( $Entry['Description'] ) == 0 )
        $Entry['Description'] = $Entry['Summary'];
      $Entry['Summary'] = str_replace ( "\\n", "\n", $Entry['Summary'] );
      $Entry['Summary'] = str_replace ( "\\'", "'", $Entry['Summary'] );
      $Entry['Summary'] = str_replace ( "\\\"", "\"", $Entry['Summary'] );
      $Entry['Summary'] = str_replace ( "'", "\\'", $Entry['Summary'] );
      $names[] = 'cal_name';
      $values[] = "'" . $Entry['Summary'] .  "'";
      $Entry['Description'] = str_replace ( "\\n", "\n", $Entry['Description'] );
      $Entry['Description'] = str_replace ( "\\'", "'", $Entry['Description'] );
      $Entry['Description'] = str_replace ( "\\\"", "\"", $Entry['Description'] );
      $Entry['Description'] = str_replace ( "'", "\\'", $Entry['Description'] );
      // limit length to 1024 chars since we setup tables that way
      if ( strlen ( $Entry['Description'] ) >= 1024 )
        $Entry['Description'] = substr ( $Entry['Description'], 0, 1019 ) . "...";
      $names[] = 'cal_description';
      $values[] = "'" . $Entry['Description'] .  "'";
      //echo "Summary:<p>" . nl2br ( htmlspecialchars ( $Entry['Summary'] ) ) . "</p>";
      //echo "Description:<p>" . nl2br ( htmlspecialchars ( $Entry['Description'] ) ) . "</p>"; exit;
      if ( $updateMode ) {
        $sql = "UPDATE webcal_entry SET ";
        for ( $f = 0; $f < count ( $names ); $f++ ) {
          if ( $f > 0 )
            $sql .= ", ";
          $sql .= $names[$f] . " = " . $values[$f];
        }
        $sql .= " WHERE cal_id = $id";
      } else {
        $sql = "INSERT INTO webcal_entry ( " . implode ( ", ", $names ) .
          " ) VALUES ( " . implode ( ", ", $values ) . " )";
      }

      if ( empty ( $error ) ) {
        $sqlLog .= $sql . "<br />\n";
        //echo "SQL: $sql <br />\n";
        if ( ! dbi_query ( $sql ) ) {
          $error .= "<p>" . translate("Database error") . ": " . dbi_error () .
            "</p>\n";
          break;
        }
      }

      // log add/update
      activity_log ( $id, $login, $login,
        $updateMode ? $LOG_UPDATE : $LOG_CREATE, "Import from $ImportType" );

      if ( $single_user == "Y" ) {
        $participants[0] = $single_user_login;
      }

      // Now add to webcal_import_data
      if ( ! $updateMode ) {
        if ($ImportType == "PALMDESKTOP") {
          $sql = "INSERT INTO webcal_import_data ( cal_import_id, cal_id, " .
            "cal_login, cal_import_type, cal_external_id ) VALUES ( " .
            "$importId, $id, '$calUser', 'palm', '$Entry[RecordID]' )";
          $sqlLog .= $sql . "<br />\n";
          if ( ! dbi_query ( $sql ) ) {
            $error = translate("Database error") . ": " . dbi_error ();
            break;
          }
        }
        else if ($ImportType == "VCAL") {
          $uid = empty ( $Entry['UID'] ) ? "null" : "'$Entry[UID]'";
          if ( strlen ( $uid ) > 200 )
            $uid = "NULL";
          $sql = "INSERT INTO webcal_import_data ( cal_import_id, cal_id, " .
            "cal_login, cal_import_type, cal_external_id ) VALUES ( " .
            "$importId, $id, '$calUser', 'vcal', $uid )";
          $sqlLog .= $sql . "<br />\n";
          if ( ! dbi_query ( $sql ) ) {
            $error = translate("Database error") . ": " . dbi_error ();
            break;
          }
        }
        else if ($ImportType == "ICAL") {
          $uid = empty ( $Entry['UID'] ) ? "null" : "'$Entry[UID]'";
          if ( strlen ( $uid ) > 200 )
            $uid = "NULL";
          $sql = "INSERT INTO webcal_import_data ( cal_import_id, cal_id, " .
            "cal_login, cal_import_type, cal_external_id ) VALUES ( " .
            "$importId, $id, '$calUser', 'ical', $uid )";
          $sqlLog .= $sql . "<br />\n";
          if ( ! dbi_query ( $sql ) ) {
            $error = translate("Database error") . ": " . dbi_error ();
            break;
          }
        }
      }

      // Now add participants
      if ( ! $updateMode ) {
        $status = ( $login == "__public__" ) ? 'W' : 'A';
        if ( empty ( $cat_id ) ) $cat_id = 'NULL';
        $sql = "INSERT INTO webcal_entry_user " .
          "( cal_id, cal_login, cal_status, cal_category ) VALUES ( $id, '" .
          $participants[0] . "', '$status', $cat_id )";
        $sqlLog .= $sql . "<br />\n";
        if ( ! dbi_query ( $sql ) ) {
          $error = translate("Database error") . ": " . dbi_error ();
          break;
        }
      }

      // Add repeating info
      if ( $updateMode ) {
        // remove old repeating info
        dbi_query ( "DELETE FROM webcal_entry_repeats WHERE cal_id = $id" );
        dbi_query ( "DELETE FROM webcal_entry_repeats_not WHERE cal_id = $id" );
      }
      if (! empty ($Entry['Repeat']['Interval'])) {
        //while ( list($k,$v) = each ( $Entry['Repeat'] ) ) {
        //  echo "$k: $v <br />\n";
        //}
        $rpt_type = RepeatType($Entry['Repeat']['Interval']);
        $freq = ( $Entry['Repeat']['Frequency'] ? $Entry['Repeat']['Frequency'] : 1 );
        if ( strlen ( $Entry['Repeat']['EndTime'] ) ) {
          $REND   = localtime($Entry['Repeat']['EndTime']);
	  $end = sprintf ( "%04d%02d%02d",$REND[5] + 1900,$REND[4] + 1,$REND[3]);
        } else {
          $end = 'NULL';
        }
        $days = (! empty ($Entry['Repeat']['RepeatDays'])) ? "'".$Entry['Repeat']['RepeatDays']."'" : 'NULL';
        $sql = "INSERT INTO webcal_entry_repeats ( cal_id, " .
          "cal_type, cal_end, cal_days, cal_frequency ) VALUES " .
          "( $id, '$rpt_type', $end, $days, $freq )";
        $sqlLog .= $sql . "<br />\n";
        if ( ! dbi_query ( $sql ) ) {
            $error = "Unable to add to webcal_entry_repeats: ".dbi_error ()."<br /><br />\n<b>SQL:</b> $sql";
            break;
        }

        // Repeating Exceptions...
        if ( ! empty ( $Entry['Repeat']['Exceptions'] ) ) {
          foreach ($Entry['Repeat']['Exceptions'] as $ex_date) {
            $ex_date = date("Ymd",$ex_date);
            $sql = "INSERT INTO webcal_entry_repeats_not ( cal_id, cal_date ) VALUES ( $id, $ex_date )";
            $sqlLog .= $sql . "<br />\n";
            if ( ! dbi_query ( $sql ) ) {
              $error = "Unable to add to webcal_entry_repeats_not: ".dbi_error ()."<br /><br />\n<b>SQL:</b> $sql";
              break;
            }
          }
        }
      } // End Repeat

      // Add Alarm info -> site_extras
      if ( $updateMode ) {
        dbi_query ( "DELETE FROM webcal_site_extras WHERE cal_id = $id" );
      }
      if ($Entry['AlarmSet'] == 1) {
        $RM = $Entry['AlarmAdvanceAmount'];
        if ($Entry['AlarmAdvanceType'] == 1){ $RM = $RM * 60; }
        if ($Entry['AlarmAdvanceType'] == 2){ $RM = $RM * 60 * 24; }
        $sql = "INSERT INTO webcal_site_extras ( cal_id, " .
          "cal_name, cal_type, cal_remind, cal_data ) VALUES " .
          "( $id, 'Reminder', 7, 1, $RM )";
        $sqlLog .= $sql . "<br />\n";
        if ( ! dbi_query ( $sql ) ) {
          $error = translate("Database error") . ": " . dbi_error ();
        }
      }
    }

    if ( ! empty ($error) && empty ($overlap))  {
      $error_num++;
      echo "<h2>". translate("Error") .
        "</h2>\n<blockquote>\n";
      echo $error . "</blockquote>\n<br />\n";
    }

    // Conflicting
    if ( ! empty ( $overlap ) ) {
      echo "<b><h2>" .
        translate("Scheduling Conflict") . ": ";
      $count_con++;
      echo "</h2></b>";

      if ( $Entry['Duration'] > 0 ) {
        $time = display_time ( $Entry['StartHour'].$Entry['StartMinute']."00" ) .
          " - " . display_time ( $Entry['EndHour'].$Entry['EndMinute']."00" );
      }
      $dd = $Entry['StartMonth'] . "-" .  $Entry['StartDay'] . "-" . $Entry['StartYear'];
      echo "<a class=\"entry\" href=\"view_entry.php?id=$id";
      echo "\" onmouseover=\"window.status='" . translate("View this entry") ."'; return true;\" onmouseout=\"window.status=''; return true;\">";
      $Entry['Summary'] = str_replace ( "''", "'", $Entry['Summary'] );
      $Entry['Summary'] = str_replace ( "'", "\\'", $Entry['Summary'] );
      echo htmlspecialchars ( $Entry['Summary'] );
      echo "</a> (" . $dd;
      $time = trim ( $time );
      if ( ! empty ( $time ) )
        echo "&nbsp; " . $time;
      echo ")<br />\n";
      etranslate("conflicts with the following existing calendar entries");
      echo ":<ul>\n" . $overlap . "</ul>\n";
    } else {

    // No Conflict
      echo "<b><h2>" .
        translate("Event Imported") . ":</h2></b>\n";
      $count_suc++;
      if ( $Entry['Duration'] > 0 ) {
        $time = display_time ( $Entry['StartHour'].$Entry['StartMinute']."00" ) .
          " - " . display_time ( $Entry['EndHour'].$Entry['EndMinute']."00" );
      }
      $dateYmd = sprintf ( "%04d%02d%02d", $Entry['StartYear'],
        $Entry['StartMonth'], $Entry['StartDay'] );
      $dd = date_to_str ( $dateYmd );
      echo "<a class=\"entry\" href=\"view_entry.php?id=$id";
      echo "\" onmouseover=\"window.status='" . translate("View this entry") ."'; return true;\" onmouseout=\"window.status=''; return true;\">";
      $Entry['Summary'] = str_replace( "''", "'", $Entry['Summary']);
      $Entry['Summary'] = str_replace( "\\", "", $Entry['Summary']);
      echo htmlspecialchars ( $Entry['Summary'] );
      echo "</a> (" . $dd;
      if ( ! empty ( $time ) )
        echo "&nbsp; " . $time;
      echo ")<br />\n";
    }

    // Reset Variables
    $overlap = $error = $dd = $time = '';
  }

  // Mark old events from prior import as deleted.
  if ( $overwrite && count ( $oldUIDs ) > 0 ) {
    // We could do this with a single SQL using sub-select, but
    // I'm pretty sure MySQL does not support it.
    $old = array_keys ( $oldUIDs );
    for ( $i = 0; $i < count ( $old ); $i++ ) {
      $sql = "SELECT cal_id FROM webcal_import_data WHERE " .
        "cal_import_type = '$type' AND " .
        "cal_external_id = '$old[$i]' AND " .
        "cal_login = '$calUser' AND " .
        "cal_id < $firstEventId";
      $res = dbi_query ( $sql );
      if ( $res ) {
        while ( $row = dbi_fetch_row ( $res ) ) {
          $oldIds[] = $row[0];
        }
        dbi_free_result ( $res );
      } else {
        echo translate("Database error") . ": " . dbi_error () . "<br />\n";
      }
    }
    for ( $i = 0; $i < count ( $oldIds ); $i++ ) {
      $sql = "UPDATE webcal_entry_user SET cal_status = 'D' " .
        "WHERE cal_id = $oldIds[$i]";
      $sqlLog .= $sql . "<br />\n";
      dbi_query ( $sql );
      $numDeleted++;
    }
  }

  //echo "<b>SQL:</b><br />\n$sqlLog\n";
}

// Convert interval to webcal repeat type
function RepeatType ($type) {
  $Repeat = array (0,'daily','weekly','monthlyByDay','monthlyByDate','yearly','monthlyByDayR');
  return $Repeat[$type];
}

?>