Make writing to prefs/abook/calendar files more reliable by handling it
[squirrelmail.git] / plugins / calendar / calendar_data.php
1 <?php
2
3 /**
4 * calendar_data.php
5 *
6 * Copyright (c) 2002-2004 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 * @package plugins
15 * @subpackage calendar
16 */
17
18 /** this is array that contains all events
19 * it is three dimensional array with fallowing structure
20 * $calendardata[date][time] = array(length,priority,title,message); */
21 $calendardata = array();
22
23 /**
24 * read events into array
25 *
26 * data is | delimited, just like addressbook
27 * files are structured like this:
28 * date|time|length|priority|title|message
29 * files are divided by year for performance increase */
30 function readcalendardata() {
31 global $calendardata, $username, $data_dir, $year;
32
33 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
34
35 if (file_exists($filename)){
36 $fp = fopen ($filename,'r');
37
38 if ($fp){
39 while ($fdata = fgetcsv ($fp, 4096, '|')) {
40 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
41 'priority' => $fdata[3],
42 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
43 'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES),
44 'reminder' => $fdata[6] );
45 }
46 fclose ($fp);
47 // this is to sort the events within a day on starttime
48 $new_calendardata = array();
49 foreach($calendardata as $day => $data) {
50 ksort($data, SORT_NUMERIC);
51 $new_calendardata[$day] = $data;
52 }
53 $calendardata = $new_calendardata;
54 }
55 }
56 }
57
58 //makes events persistant
59 function writecalendardata() {
60 global $calendardata, $username, $data_dir, $year, $color;
61
62 $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
63 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
64 $fp = fopen ($filetmp,"w");
65 if ($fp) {
66 while ( $calfoo = each ($calendardata)) {
67 while ( $calbar = each ($calfoo['value'])) {
68 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
69 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
70 if(sq_fwrite($fp, $calstr, 4096) === FALSE) {
71 error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"), $color);
72 }
73 }
74
75 }
76 fclose ($fp);
77 @unlink($filename);
78 rename($filetmp,$filename);
79 }
80 }
81
82 //deletes event from file
83 function delete_event($date, $time) {
84 global $calendardata, $username, $data_dir, $year;
85
86 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
87 $fp = fopen ($filename,'r');
88 if ($fp){
89 while ($fdata = fgetcsv ($fp, 4096, "|")) {
90 if (($fdata[0]==$date) && ($fdata[1]==$time)){
91 // do nothing
92 } else {
93 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
94 'priority' => $fdata[3],
95 'title' => $fdata[4],
96 'message' => $fdata[5],
97 'reminder' => $fdata[6] );
98 }
99 }
100 fclose ($fp);
101 }
102 writecalendardata();
103
104 }
105
106 // same as delete but not saves calendar
107 // saving is done inside event_edit.php
108 function update_event($date, $time) {
109 global $calendardata, $username, $data_dir, $year;
110
111 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
112 $fp = fopen ($filename,'r');
113 if ($fp){
114 while ($fdata = fgetcsv ($fp, 4096, '|')) {
115 if (($fdata[0]==$date) && ($fdata[1]==$time)){
116 // do nothing
117 } else {
118 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
119 'priority' => $fdata[3],
120 'title' => $fdata[4],
121 'message' => $fdata[5],
122 'reminder' => $fdata[6] );
123 }
124 }
125 fclose ($fp);
126 }
127 }
128
129
130 ?>