Make writing to prefs/abook/calendar files more reliable by handling it
[squirrelmail.git] / plugins / calendar / calendar_data.php
CommitLineData
2c85de8f 1<?php
7c67a5e8 2
3/**
4 * calendar_data.php
5 *
82d304a0 6 * Copyright (c) 2002-2004 The SquirrelMail Project Team
7c67a5e8 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
d61a01d4 8 *
7c67a5e8 9 * Originally contrubuted by Michal Szczotka <michal@tuxy.org>
d61a01d4 10 *
7c67a5e8 11 * functions to operate on calendar data files.
d61a01d4 12 *
2c85de8f 13 * $Id$
ea5f4b8e 14 * @package plugins
15 * @subpackage calendar
d61a01d4 16 */
17
ea5f4b8e 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); */
d61a01d4 21$calendardata = array();
22
ea5f4b8e 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 */
d61a01d4 30function 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');
88cb1b4d 37
38 if ($fp){
39 while ($fdata = fgetcsv ($fp, 4096, '|')) {
40 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
41 'priority' => $fdata[3],
e842b215 42 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
43 'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES),
88cb1b4d 44 'reminder' => $fdata[6] );
45 }
46 fclose ($fp);
5d01bcce 47 // this is to sort the events within a day on starttime
bdb21d65 48 $new_calendardata = array();
49 foreach($calendardata as $day => $data) {
5d01bcce 50 ksort($data, SORT_NUMERIC);
bdb21d65 51 $new_calendardata[$day] = $data;
5d01bcce 52 }
bdb21d65 53 $calendardata = $new_calendardata;
d61a01d4 54 }
d61a01d4 55 }
56}
57
58//makes events persistant
59function writecalendardata() {
3ecad5e6 60 global $calendardata, $username, $data_dir, $year, $color;
d61a01d4 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)) {
88cb1b4d 67 while ( $calbar = each ($calfoo['value'])) {
68 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
d61a01d4 69 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
3ecad5e6 70 if(sq_fwrite($fp, $calstr, 4096) === FALSE) {
71 error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"), $color);
72 }
d61a01d4 73 }
74
75 }
76 fclose ($fp);
1d461a18 77 @unlink($filename);
d61a01d4 78 rename($filetmp,$filename);
79 }
80}
81
82//deletes event from file
83function 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
108function 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
7c67a5e8 130?>