Minor fix for people who saw fatal errors trying to overwrite with the rename command.
[squirrelmail.git] / plugins / calendar / calendar_data.php
CommitLineData
2c85de8f 1<?php
7c67a5e8 2
3/**
4 * calendar_data.php
5 *
76911253 6 * Copyright (c) 2002-2003 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$
d61a01d4 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
26function 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');
88cb1b4d 33
34 if ($fp){
35 while ($fdata = fgetcsv ($fp, 4096, '|')) {
36 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
37 'priority' => $fdata[3],
e842b215 38 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
39 'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES),
88cb1b4d 40 'reminder' => $fdata[6] );
41 }
42 fclose ($fp);
d61a01d4 43 }
d61a01d4 44 }
45}
46
47//makes events persistant
48function writecalendardata() {
49 global $calendardata, $username, $data_dir, $year;
50
51 $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
52 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
53 $fp = fopen ($filetmp,"w");
54 if ($fp) {
55 while ( $calfoo = each ($calendardata)) {
88cb1b4d 56 while ( $calbar = each ($calfoo['value'])) {
57 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
d61a01d4 58 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
59 fwrite($fp, $calstr, 4096);
60 }
61
62 }
63 fclose ($fp);
1d461a18 64 @unlink($filename);
d61a01d4 65 rename($filetmp,$filename);
66 }
67}
68
69//deletes event from file
70function delete_event($date, $time) {
71 global $calendardata, $username, $data_dir, $year;
72
73 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
74 $fp = fopen ($filename,'r');
75 if ($fp){
76 while ($fdata = fgetcsv ($fp, 4096, "|")) {
77 if (($fdata[0]==$date) && ($fdata[1]==$time)){
78 // do nothing
79 } else {
80 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
81 'priority' => $fdata[3],
82 'title' => $fdata[4],
83 'message' => $fdata[5],
84 'reminder' => $fdata[6] );
85 }
86 }
87 fclose ($fp);
88 }
89 writecalendardata();
90
91}
92
93// same as delete but not saves calendar
94// saving is done inside event_edit.php
95function update_event($date, $time) {
96 global $calendardata, $username, $data_dir, $year;
97
98 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
99 $fp = fopen ($filename,'r');
100 if ($fp){
101 while ($fdata = fgetcsv ($fp, 4096, '|')) {
102 if (($fdata[0]==$date) && ($fdata[1]==$time)){
103 // do nothing
104 } else {
105 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
106 'priority' => $fdata[3],
107 'title' => $fdata[4],
108 'message' => $fdata[5],
109 'reminder' => $fdata[6] );
110 }
111 }
112 fclose ($fp);
113 }
114}
115
116
7c67a5e8 117?>