#1291081. Undo encoding of line feeds in calendar data.
[squirrelmail.git] / plugins / calendar / calendar_data.php
CommitLineData
2c85de8f 1<?php
7c67a5e8 2
3/**
4 * calendar_data.php
5 *
7c67a5e8 6 * Originally contrubuted by Michal Szczotka <michal@tuxy.org>
d61a01d4 7 *
7c67a5e8 8 * functions to operate on calendar data files.
d61a01d4 9 *
4b4abf93 10 * @copyright &copy; 2002-2005 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
ea5f4b8e 13 * @package plugins
14 * @subpackage calendar
d61a01d4 15 */
16
ea5f4b8e 17/** this is array that contains all events
18 * it is three dimensional array with fallowing structure
19 * $calendardata[date][time] = array(length,priority,title,message); */
d61a01d4 20$calendardata = array();
21
607428ac 22/**
23 * Reads multilined calendar data
24 *
25 * Plugin stores multiline texts converted to single line with PHP nl2br().
26 * Function undoes nl2br() conversion and sanitizes data with htmlspecialchars().
27 * @param string $string calendar string
28 * @return string calendar string converted to multiline text
29 * @since 1.5.1
30 */
31function calendar_readmultiline($string) {
32 // replace html line breaks with ASCII line feeds
33 $string = str_replace(array('<br />','<br>'),array("\n","\n"),$string);
34 // FIXME: don't sanitize data. Storage backend should not care about html data safety
35 $string = htmlspecialchars($string,ENT_NOQUOTES);
36 return $string;
37}
38
39/**
40 * Callback function used to sanitize calendar data before saving it to file
41 * @param string $sValue array value
42 * @param string $sKey array key
43 * @since 1.5.1
44 */
45function calendar_encodedata(&$sValue, $sKey) {
46 // add html line breaks and remove original ASCII line feeds and carriage returns
47 $sValue = str_replace(array("\n","\r"),array('',''),nl2br($sValue));
48}
49
ea5f4b8e 50/**
51 * read events into array
52 *
53 * data is | delimited, just like addressbook
54 * files are structured like this:
55 * date|time|length|priority|title|message
56 * files are divided by year for performance increase */
d61a01d4 57function readcalendardata() {
58 global $calendardata, $username, $data_dir, $year;
59
60 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
61
62 if (file_exists($filename)){
63 $fp = fopen ($filename,'r');
88cb1b4d 64
65 if ($fp){
66 while ($fdata = fgetcsv ($fp, 4096, '|')) {
67 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
68 'priority' => $fdata[3],
e842b215 69 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
607428ac 70 'message' => calendar_readmultiline($fdata[5]),
88cb1b4d 71 'reminder' => $fdata[6] );
72 }
73 fclose ($fp);
5d01bcce 74 // this is to sort the events within a day on starttime
bdb21d65 75 $new_calendardata = array();
91e0dccc 76 foreach($calendardata as $day => $data) {
5d01bcce 77 ksort($data, SORT_NUMERIC);
bdb21d65 78 $new_calendardata[$day] = $data;
5d01bcce 79 }
91e0dccc 80 $calendardata = $new_calendardata;
d61a01d4 81 }
d61a01d4 82 }
83}
84
85//makes events persistant
86function writecalendardata() {
3ecad5e6 87 global $calendardata, $username, $data_dir, $year, $color;
d61a01d4 88
89 $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
90 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
91 $fp = fopen ($filetmp,"w");
92 if ($fp) {
93 while ( $calfoo = each ($calendardata)) {
88cb1b4d 94 while ( $calbar = each ($calfoo['value'])) {
95 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
607428ac 96 array_walk($calfoobar,'calendar_encodedata');
d61a01d4 97 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
3ecad5e6 98 if(sq_fwrite($fp, $calstr, 4096) === FALSE) {
91e0dccc 99 error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"), $color);
100 }
d61a01d4 101 }
102
103 }
104 fclose ($fp);
1d461a18 105 @unlink($filename);
d61a01d4 106 rename($filetmp,$filename);
107 }
108}
109
110//deletes event from file
111function delete_event($date, $time) {
112 global $calendardata, $username, $data_dir, $year;
113
114 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
115 $fp = fopen ($filename,'r');
116 if ($fp){
117 while ($fdata = fgetcsv ($fp, 4096, "|")) {
118 if (($fdata[0]==$date) && ($fdata[1]==$time)){
119 // do nothing
120 } else {
121 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
122 'priority' => $fdata[3],
123 'title' => $fdata[4],
124 'message' => $fdata[5],
125 'reminder' => $fdata[6] );
126 }
127 }
128 fclose ($fp);
129 }
130 writecalendardata();
131
132}
133
134// same as delete but not saves calendar
135// saving is done inside event_edit.php
136function update_event($date, $time) {
137 global $calendardata, $username, $data_dir, $year;
138
139 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
140 $fp = fopen ($filename,'r');
141 if ($fp){
142 while ($fdata = fgetcsv ($fp, 4096, '|')) {
143 if (($fdata[0]==$date) && ($fdata[1]==$time)){
144 // do nothing
145 } else {
146 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
147 'priority' => $fdata[3],
148 'title' => $fdata[4],
149 'message' => $fdata[5],
150 'reminder' => $fdata[6] );
151 }
152 }
153 fclose ($fp);
154 }
155}
156
91e0dccc 157?>