| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * calendar_data.php |
| 5 | * |
| 6 | * Copyright (c) 2002-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Originally contrubuted by Michal Szczotka <michal@tuxy.org> |
| 10 | * |
| 11 | * functions to operate on calendar data files. |
| 12 | * |
| 13 | * $Id$ |
| 14 | */ |
| 15 | |
| 16 | // this is array that contains all events |
| 17 | // it is three dimensional array with fallowing structure |
| 18 | // $calendardata[date][time] = array(length,priority,title,message); |
| 19 | $calendardata = array(); |
| 20 | |
| 21 | //read events into array |
| 22 | //data is | delimited, just like addresbook |
| 23 | //files are structured like this: |
| 24 | //date|time|length|priority|title|message); |
| 25 | //files are divide by year for performance increase |
| 26 | function readcalendardata() { |
| 27 | global $calendardata, $username, $data_dir, $year; |
| 28 | |
| 29 | $filename = getHashedFile($username, $data_dir, "$username.$year.cal"); |
| 30 | |
| 31 | if (file_exists($filename)){ |
| 32 | $fp = fopen ($filename,'r'); |
| 33 | |
| 34 | if ($fp){ |
| 35 | while ($fdata = fgetcsv ($fp, 4096, '|')) { |
| 36 | $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2], |
| 37 | 'priority' => $fdata[3], |
| 38 | 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES), |
| 39 | 'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES), |
| 40 | 'reminder' => $fdata[6] ); |
| 41 | } |
| 42 | fclose ($fp); |
| 43 | // this is to sort the events within a day on starttime |
| 44 | $new_calendardata = array(); |
| 45 | foreach($calendardata as $day => $data) { |
| 46 | ksort($data, SORT_NUMERIC); |
| 47 | $new_calendardata[$day] = $data; |
| 48 | } |
| 49 | $calendardata = $new_calendardata; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | //makes events persistant |
| 55 | function writecalendardata() { |
| 56 | global $calendardata, $username, $data_dir, $year; |
| 57 | |
| 58 | $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp"); |
| 59 | $filename = getHashedFile($username, $data_dir, "$username.$year.cal"); |
| 60 | $fp = fopen ($filetmp,"w"); |
| 61 | if ($fp) { |
| 62 | while ( $calfoo = each ($calendardata)) { |
| 63 | while ( $calbar = each ($calfoo['value'])) { |
| 64 | $calfoobar = $calendardata[$calfoo['key']][$calbar['key']]; |
| 65 | $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n"; |
| 66 | fwrite($fp, $calstr, 4096); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | fclose ($fp); |
| 71 | @unlink($filename); |
| 72 | rename($filetmp,$filename); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | //deletes event from file |
| 77 | function delete_event($date, $time) { |
| 78 | global $calendardata, $username, $data_dir, $year; |
| 79 | |
| 80 | $filename = getHashedFile($username, $data_dir, "$username.$year.cal"); |
| 81 | $fp = fopen ($filename,'r'); |
| 82 | if ($fp){ |
| 83 | while ($fdata = fgetcsv ($fp, 4096, "|")) { |
| 84 | if (($fdata[0]==$date) && ($fdata[1]==$time)){ |
| 85 | // do nothing |
| 86 | } else { |
| 87 | $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2], |
| 88 | 'priority' => $fdata[3], |
| 89 | 'title' => $fdata[4], |
| 90 | 'message' => $fdata[5], |
| 91 | 'reminder' => $fdata[6] ); |
| 92 | } |
| 93 | } |
| 94 | fclose ($fp); |
| 95 | } |
| 96 | writecalendardata(); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | // same as delete but not saves calendar |
| 101 | // saving is done inside event_edit.php |
| 102 | function update_event($date, $time) { |
| 103 | global $calendardata, $username, $data_dir, $year; |
| 104 | |
| 105 | $filename = getHashedFile($username, $data_dir, "$username.$year.cal"); |
| 106 | $fp = fopen ($filename,'r'); |
| 107 | if ($fp){ |
| 108 | while ($fdata = fgetcsv ($fp, 4096, '|')) { |
| 109 | if (($fdata[0]==$date) && ($fdata[1]==$time)){ |
| 110 | // do nothing |
| 111 | } else { |
| 112 | $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2], |
| 113 | 'priority' => $fdata[3], |
| 114 | 'title' => $fdata[4], |
| 115 | 'message' => $fdata[5], |
| 116 | 'reminder' => $fdata[6] ); |
| 117 | } |
| 118 | } |
| 119 | fclose ($fp); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | ?> |