fsf changes, meant to be rebased on upstream
[squirrelmail.git] / plugins / calendar / calendar_data.php
CommitLineData
2c85de8f 1<?php
7c67a5e8 2
3/**
7c67a5e8 4 * functions to operate on calendar data files.
d61a01d4 5 *
33aab559 6 * @copyright 2002-2024 The SquirrelMail Project Team
4b4abf93 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * @version $Id$
ea5f4b8e 9 * @package plugins
10 * @subpackage calendar
d61a01d4 11 */
12
1c7143ad 13/**
14 * this is array that contains all events
15 * it is three dimensional array with fallowing structure
16 * $calendardata[date][time] = array(length,priority,title,message,reminder);
17 */
d61a01d4 18$calendardata = array();
19
607428ac 20/**
21 * Reads multilined calendar data
22 *
23 * Plugin stores multiline texts converted to single line with PHP nl2br().
1c7143ad 24 * Function undoes nl2br() conversion and html encoding of ASCII vertical bar.
25 *
3047e291 26 * Older plugin versions sanitized data with sm_encode_html_special_chars. Since 1.5.1 calendar
1c7143ad 27 * data is not sanitized. Output functions must make sure that data is correctly
28 * encoded and sanitized.
607428ac 29 * @param string $string calendar string
30 * @return string calendar string converted to multiline text
1c7143ad 31 * @access private
607428ac 32 * @since 1.5.1
33 */
34function calendar_readmultiline($string) {
1c7143ad 35 /**
36 * replace html line breaks with ASCII line feeds
37 * replace htmlencoded | with ASCII vertical bar
38 */
39 $string = str_replace(array('<br />','<br>','&#124;'),array("\n","\n",'|'),$string);
607428ac 40 return $string;
41}
42
43/**
44 * Callback function used to sanitize calendar data before saving it to file
45 * @param string $sValue array value
46 * @param string $sKey array key
1c7143ad 47 * @access private
607428ac 48 * @since 1.5.1
49 */
50function calendar_encodedata(&$sValue, $sKey) {
1c7143ad 51 /**
52 * add html line breaks
53 * remove original ASCII line feeds and carriage returns
54 * replace ASCII vertical bar with html code in order to sanitize field delimiter
55 */
56 $sValue = str_replace(array("\n","\r",'|'),array('','','&#124;'),nl2br($sValue));
607428ac 57}
58
ea5f4b8e 59/**
60 * read events into array
61 *
62 * data is | delimited, just like addressbook
63 * files are structured like this:
64 * date|time|length|priority|title|message
1c7143ad 65 * files are divided by year for performance increase
66 */
d61a01d4 67function readcalendardata() {
68 global $calendardata, $username, $data_dir, $year;
69
70 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
71
72 if (file_exists($filename)){
73 $fp = fopen ($filename,'r');
88cb1b4d 74
75 if ($fp){
76 while ($fdata = fgetcsv ($fp, 4096, '|')) {
1c7143ad 77 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
78 'priority' => $fdata[3],
f1d482ae 79 'title' => str_replace("\n",' ',calendar_readmultiline($fdata[4])),
1c7143ad 80 'message' => calendar_readmultiline($fdata[5]),
81 'reminder' => $fdata[6] );
88cb1b4d 82 }
83 fclose ($fp);
5d01bcce 84 // this is to sort the events within a day on starttime
bdb21d65 85 $new_calendardata = array();
91e0dccc 86 foreach($calendardata as $day => $data) {
5d01bcce 87 ksort($data, SORT_NUMERIC);
bdb21d65 88 $new_calendardata[$day] = $data;
5d01bcce 89 }
91e0dccc 90 $calendardata = $new_calendardata;
d61a01d4 91 }
d61a01d4 92 }
93}
94
1c7143ad 95/**
96 * Saves calendar data
97 * @return void
98 * @access private
99 */
d61a01d4 100function writecalendardata() {
3ecad5e6 101 global $calendardata, $username, $data_dir, $year, $color;
d61a01d4 102
103 $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
104 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
105 $fp = fopen ($filetmp,"w");
106 if ($fp) {
1dbeefcc 107 foreach ( $calendardata as $datetime => $events ) {
108 foreach ( $events as $time_of_day => $event ) {
109 array_walk($event,'calendar_encodedata');
1c7143ad 110 /**
111 * Make sure that reminder field is set. Calendar forms don't implement it,
112 * but it is still used for calendar data. Backwards compatibility.
113 */
1dbeefcc 114 if (!isset($event['reminder'])) $event['reminder']='';
1c7143ad 115
1dbeefcc 116 $calstr = "$datetime|$time_of_day|$event[length]|$event[priority]|$event[title]|$event[message]|$event[reminder]\n";
3ecad5e6 117 if(sq_fwrite($fp, $calstr, 4096) === FALSE) {
1b858d86 118 error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"));
91e0dccc 119 }
d61a01d4 120 }
d61a01d4 121 }
122 fclose ($fp);
1d461a18 123 @unlink($filename);
d61a01d4 124 rename($filetmp,$filename);
125 }
126}
127
1c7143ad 128/**
129 * deletes event from file
130 * @return void
131 * @access private
132 */
d61a01d4 133function delete_event($date, $time) {
134 global $calendardata, $username, $data_dir, $year;
135
136 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
137 $fp = fopen ($filename,'r');
138 if ($fp){
139 while ($fdata = fgetcsv ($fp, 4096, "|")) {
140 if (($fdata[0]==$date) && ($fdata[1]==$time)){
1c7143ad 141 // do nothing
d61a01d4 142 } else {
1c7143ad 143 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
d61a01d4 144 'priority' => $fdata[3],
1c7143ad 145 'title' => $fdata[4],
146 'message' => $fdata[5],
d61a01d4 147 'reminder' => $fdata[6] );
148 }
149 }
150 fclose ($fp);
151 }
152 writecalendardata();
d61a01d4 153}
154
1c7143ad 155/**
f1d482ae 156 * same as delete but does not save calendar
1c7143ad 157 * saving is done inside event_edit.php
158 * @return void
159 * @access private
160 * @todo code reuse
161 */
d61a01d4 162function update_event($date, $time) {
163 global $calendardata, $username, $data_dir, $year;
164
165 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
166 $fp = fopen ($filename,'r');
167 if ($fp){
168 while ($fdata = fgetcsv ($fp, 4096, '|')) {
169 if (($fdata[0]==$date) && ($fdata[1]==$time)){
1c7143ad 170 // do nothing
d61a01d4 171 } else {
1c7143ad 172 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
d61a01d4 173 'priority' => $fdata[3],
1c7143ad 174 'title' => $fdata[4],
175 'message' => $fdata[5],
d61a01d4 176 'reminder' => $fdata[6] );
177 }
178 }
179 fclose ($fp);
180 }
181}