calendar_data.php 6.4 KB

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