We're living in 2004 now... perl is your friend for these kinds of things :)
[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() {
60 global $calendardata, $username, $data_dir, $year;
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";
70 fwrite($fp, $calstr, 4096);
71 }
72
73 }
74 fclose ($fp);
1d461a18 75 @unlink($filename);
d61a01d4 76 rename($filetmp,$filename);
77 }
78}
79
80//deletes event from file
81function delete_event($date, $time) {
82 global $calendardata, $username, $data_dir, $year;
83
84 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
85 $fp = fopen ($filename,'r');
86 if ($fp){
87 while ($fdata = fgetcsv ($fp, 4096, "|")) {
88 if (($fdata[0]==$date) && ($fdata[1]==$time)){
89 // do nothing
90 } else {
91 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
92 'priority' => $fdata[3],
93 'title' => $fdata[4],
94 'message' => $fdata[5],
95 'reminder' => $fdata[6] );
96 }
97 }
98 fclose ($fp);
99 }
100 writecalendardata();
101
102}
103
104// same as delete but not saves calendar
105// saving is done inside event_edit.php
106function update_event($date, $time) {
107 global $calendardata, $username, $data_dir, $year;
108
109 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
110 $fp = fopen ($filename,'r');
111 if ($fp){
112 while ($fdata = fgetcsv ($fp, 4096, '|')) {
113 if (($fdata[0]==$date) && ($fdata[1]==$time)){
114 // do nothing
115 } else {
116 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
117 'priority' => $fdata[3],
118 'title' => $fdata[4],
119 'message' => $fdata[5],
120 'reminder' => $fdata[6] );
121 }
122 }
123 fclose ($fp);
124 }
125}
126
127
7c67a5e8 128?>