#1291081. Undo encoding of line feeds in calendar data.
[squirrelmail.git] / plugins / calendar / calendar_data.php
index 8e724547e59486462fd12c08173a15e076d4b234..7128841db11b234a776179dd51b949acc6dbdd70 100644 (file)
@@ -3,26 +3,57 @@
 /**
  * calendar_data.php
  *
- * Copyright (c) 2002-2003 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * Originally contrubuted by Michal Szczotka <michal@tuxy.org>
  *
  * functions to operate on calendar data files.
  *
- * $Id$
+ * @copyright &copy; 2002-2005 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @package plugins
+ * @subpackage calendar
  */
 
-// this is array that contains all events
-// it is three dimensional array with fallowing structure
-// $calendardata[date][time] = array(length,priority,title,message);
+/** this is array that contains all events
+ *  it is three dimensional array with fallowing structure
+ *  $calendardata[date][time] = array(length,priority,title,message); */
 $calendardata = array();
 
-//read events into array
-//data is | delimited, just like addresbook
-//files are structured like this:
-//date|time|length|priority|title|message);
-//files are divide by year for performance increase
+/**
+ * Reads multilined calendar data
+ * 
+ * Plugin stores multiline texts converted to single line with PHP nl2br().
+ * Function undoes nl2br() conversion and sanitizes data with htmlspecialchars().
+ * @param string $string calendar string
+ * @return string calendar string converted to multiline text
+ * @since 1.5.1
+ */
+function calendar_readmultiline($string) {
+    // replace html line breaks with ASCII line feeds
+    $string = str_replace(array('<br />','<br>'),array("\n","\n"),$string);
+    // FIXME: don't sanitize data. Storage backend should not care about html data safety
+    $string = htmlspecialchars($string,ENT_NOQUOTES);
+    return $string;
+}
+
+/**
+ * Callback function used to sanitize calendar data before saving it to file
+ * @param string $sValue array value 
+ * @param string $sKey array key
+ * @since 1.5.1
+ */
+function calendar_encodedata(&$sValue, $sKey) {
+    // add html line breaks and remove original ASCII line feeds and carriage returns
+    $sValue = str_replace(array("\n","\r"),array('',''),nl2br($sValue));
+}
+
+/**
+ * read events into array
+ *
+ * data is | delimited, just like addressbook
+ * files are structured like this:
+ * date|time|length|priority|title|message
+ * files are divided by year for performance increase */
 function readcalendardata() {
     global $calendardata, $username, $data_dir, $year;
 
@@ -36,17 +67,24 @@ function readcalendardata() {
                 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
                                                             'priority' => $fdata[3],
                                                             'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
-                                                            'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES),
+                                                            'message' => calendar_readmultiline($fdata[5]),
                                                             'reminder' => $fdata[6] );
             }
             fclose ($fp);
+            // this is to sort the events within a day on starttime
+            $new_calendardata = array();
+            foreach($calendardata as $day => $data) {
+                ksort($data, SORT_NUMERIC);
+                $new_calendardata[$day] = $data;
+            }
+            $calendardata = $new_calendardata;
         }
     }
 }
 
 //makes events persistant
 function writecalendardata() {
-    global $calendardata, $username, $data_dir, $year;
+    global $calendardata, $username, $data_dir, $year, $color;
 
     $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
     $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
@@ -55,12 +93,16 @@ function writecalendardata() {
         while ( $calfoo = each ($calendardata)) {
             while ( $calbar = each ($calfoo['value'])) {
                 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
+                array_walk($calfoobar,'calendar_encodedata');
                 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
-                fwrite($fp, $calstr, 4096);
+                if(sq_fwrite($fp, $calstr, 4096) === FALSE) {
+                        error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"), $color);
+                }
             }
 
         }
         fclose ($fp);
+        @unlink($filename);
         rename($filetmp,$filename);
     }
 }
@@ -112,5 +154,4 @@ function update_event($date, $time) {
     }
 }
 
-
-?>
+?>
\ No newline at end of file