class.recurrence.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. <?php
  2. /*
  3. * Copyright 2005 - 2016 Zarafa and its licensors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License, version 3,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. ?>
  19. <?php
  20. include_once('class.baserecurrence.php');
  21. /**
  22. * Recurrence
  23. * @author Steve Hardy <steve@zarafa.com>
  24. * @author Michel de Ron <michel@zarafa.com>
  25. */
  26. class Recurrence extends BaseRecurrence {
  27. /*
  28. * ABOUT TIMEZONES
  29. *
  30. * Timezones are rather complicated here so here are some rules to think about:
  31. *
  32. * - Timestamps in mapi-like properties (so in PT_SYSTIME properties) are always in GMT (including
  33. * the 'basedate' property in exceptions !!)
  34. * - Timestamps for recurrence (so start/end of recurrence, and basedates for exceptions (everything
  35. * outside the 'basedate' property in the exception !!), and start/endtimes for exceptions) are
  36. * always in LOCAL time.
  37. */
  38. // All properties for a recipient that are interesting
  39. var $recipprops = Array(
  40. PR_ENTRYID,
  41. PR_SEARCH_KEY,
  42. PR_DISPLAY_NAME,
  43. PR_EMAIL_ADDRESS,
  44. PR_RECIPIENT_ENTRYID,
  45. PR_RECIPIENT_TYPE,
  46. PR_SEND_INTERNET_ENCODING,
  47. PR_SEND_RICH_INFO,
  48. PR_RECIPIENT_DISPLAY_NAME,
  49. PR_ADDRTYPE,
  50. PR_DISPLAY_TYPE,
  51. PR_DISPLAY_TYPE_EX,
  52. PR_RECIPIENT_TRACKSTATUS,
  53. PR_RECIPIENT_TRACKSTATUS_TIME,
  54. PR_RECIPIENT_FLAGS,
  55. PR_ROWID
  56. );
  57. /**
  58. * @param resource $store MAPI Message Store Object
  59. * @param resource $message the MAPI (appointment) message
  60. */
  61. function __construct($store, $message)
  62. {
  63. $properties = array();
  64. $properties["entryid"] = PR_ENTRYID;
  65. $properties["parent_entryid"] = PR_PARENT_ENTRYID;
  66. $properties["message_class"] = PR_MESSAGE_CLASS;
  67. $properties["icon_index"] = PR_ICON_INDEX;
  68. $properties["subject"] = PR_SUBJECT;
  69. $properties["display_to"] = PR_DISPLAY_TO;
  70. $properties["importance"] = PR_IMPORTANCE;
  71. $properties["sensitivity"] = PR_SENSITIVITY;
  72. $properties["startdate"] = "PT_SYSTIME:PSETID_Appointment:0x820d";
  73. $properties["duedate"] = "PT_SYSTIME:PSETID_Appointment:0x820e";
  74. $properties["recurring"] = "PT_BOOLEAN:PSETID_Appointment:0x8223";
  75. $properties["recurring_data"] = "PT_BINARY:PSETID_Appointment:0x8216";
  76. $properties["busystatus"] = "PT_LONG:PSETID_Appointment:0x8205";
  77. $properties["label"] = "PT_LONG:PSETID_Appointment:0x8214";
  78. $properties["alldayevent"] = "PT_BOOLEAN:PSETID_Appointment:0x8215";
  79. $properties["private"] = "PT_BOOLEAN:PSETID_Common:0x8506";
  80. $properties["meeting"] = "PT_LONG:PSETID_Appointment:0x8217";
  81. $properties["startdate_recurring"] = "PT_SYSTIME:PSETID_Appointment:0x8235";
  82. $properties["enddate_recurring"] = "PT_SYSTIME:PSETID_Appointment:0x8236";
  83. $properties["recurring_pattern"] = "PT_STRING8:PSETID_Appointment:0x8232";
  84. $properties["location"] = "PT_STRING8:PSETID_Appointment:0x8208";
  85. $properties["duration"] = "PT_LONG:PSETID_Appointment:0x8213";
  86. $properties["responsestatus"] = "PT_LONG:PSETID_Appointment:0x8218";
  87. $properties["reminder"] = "PT_BOOLEAN:PSETID_Common:0x8503";
  88. $properties["reminder_minutes"] = "PT_LONG:PSETID_Common:0x8501";
  89. $properties["recurrencetype"] = "PT_LONG:PSETID_Appointment:0x8231";
  90. $properties["contacts"] = "PT_MV_STRING8:PSETID_Common:0x853a";
  91. $properties["contacts_string"] = "PT_STRING8:PSETID_Common:0x8586";
  92. $properties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords";
  93. $properties["reminder_time"] = "PT_SYSTIME:PSETID_Common:0x8502";
  94. $properties["commonstart"] = "PT_SYSTIME:PSETID_Common:0x8516";
  95. $properties["commonend"] = "PT_SYSTIME:PSETID_Common:0x8517";
  96. $properties["basedate"] = "PT_SYSTIME:PSETID_Appointment:0x8228";
  97. $properties["timezone_data"] = "PT_BINARY:PSETID_Appointment:0x8233";
  98. $properties["timezone"] = "PT_STRING8:PSETID_Appointment:0x8234";
  99. $properties["flagdueby"] = "PT_SYSTIME:PSETID_Common:0x8560";
  100. $properties["side_effects"] = "PT_LONG:PSETID_Common:0x8510";
  101. $properties["hideattachments"] = "PT_BOOLEAN:PSETID_Common:0x8514";
  102. $this->proptags = getPropIdsFromStrings($store, $properties);
  103. parent::__construct($store, $message);
  104. }
  105. /**
  106. * Create an exception
  107. * @param array $exception_props the exception properties (same properties as normal recurring items)
  108. * @param date $base_date the base date of the exception (LOCAL time of non-exception occurrence)
  109. * @param boolean $delete true - delete occurrence, false - create new exception or modify existing
  110. * @param array $exception_recips true - delete occurrence, false - create new exception or modify existing
  111. * @param mapi_message $copy_attach_from mapi message from which attachments should be copied
  112. */
  113. function createException($exception_props, $base_date, $delete = false, $exception_recips = array(), $copy_attach_from = false)
  114. {
  115. $baseday = $this->dayStartOf($base_date);
  116. $basetime = $baseday + $this->recur["startocc"] * 60;
  117. // Remove any pre-existing exception on this base date
  118. if ($this->isException($baseday))
  119. $this->deleteException($baseday); // note that deleting an exception is different from creating a deleted exception (deleting an occurrence).
  120. if(!$delete) {
  121. if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]])))
  122. return false;
  123. // Properties in the attachment are the properties of the base object, plus $exception_props plus the base date
  124. foreach (array("subject", "location", "label", "reminder", "reminder_minutes", "alldayevent", "busystatus") as $propname)
  125. if(isset($this->messageprops[$this->proptags[$propname]]))
  126. $props[$this->proptags[$propname]] = $this->messageprops[$this->proptags[$propname]];
  127. $props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}";
  128. unset($exception_props[PR_MESSAGE_CLASS]);
  129. unset($exception_props[PR_ICON_INDEX]);
  130. $props = $exception_props + $props;
  131. // Basedate in the exception attachment is the GMT time at which the original occurrence would have been
  132. $props[$this->proptags["basedate"]] = $this->toGMT($this->tz, $basetime);
  133. if (!isset($exception_props[$this->proptags["startdate"]]))
  134. $props[$this->proptags["startdate"]] = $this->getOccurrenceStart($base_date);
  135. if (!isset($exception_props[$this->proptags["duedate"]]))
  136. $props[$this->proptags["duedate"]] = $this->getOccurrenceEnd($base_date);
  137. // synchronize commonstart/commonend with startdate/duedate
  138. if (isset($props[$this->proptags["startdate"]]))
  139. $props[$this->proptags["commonstart"]] = $props[$this->proptags["startdate"]];
  140. if (isset($props[$this->proptags["duedate"]]))
  141. $props[$this->proptags["commonend"]] = $props[$this->proptags["duedate"]];
  142. // Save the data into an attachment
  143. $this->createExceptionAttachment($props, $exception_recips, $copy_attach_from);
  144. $changed_item = array();
  145. $changed_item["basedate"] = $baseday;
  146. $changed_item["start"] = $this->fromGMT($this->tz, $props[$this->proptags["startdate"]]);
  147. $changed_item["end"] = $this->fromGMT($this->tz, $props[$this->proptags["duedate"]]);
  148. if (array_key_exists($this->proptags["subject"], $exception_props))
  149. $changed_item["subject"] = $exception_props[$this->proptags["subject"]];
  150. if (array_key_exists($this->proptags["location"], $exception_props))
  151. $changed_item["location"] = $exception_props[$this->proptags["location"]];
  152. if (array_key_exists($this->proptags["label"], $exception_props))
  153. $changed_item["label"] = $exception_props[$this->proptags["label"]];
  154. if (array_key_exists($this->proptags["reminder"], $exception_props))
  155. $changed_item["reminder_set"] = $exception_props[$this->proptags["reminder"]];
  156. if (array_key_exists($this->proptags["reminder_minutes"], $exception_props))
  157. $changed_item["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]];
  158. if (array_key_exists($this->proptags["alldayevent"], $exception_props))
  159. $changed_item["alldayevent"] = $exception_props[$this->proptags["alldayevent"]];
  160. if (array_key_exists($this->proptags["busystatus"], $exception_props))
  161. $changed_item["busystatus"] = $exception_props[$this->proptags["busystatus"]];
  162. // Add the changed occurrence to the list
  163. array_push($this->recur["changed_occurences"], $changed_item);
  164. } else {
  165. // Delete the occurrence by placing it in the deleted occurrences list
  166. array_push($this->recur["deleted_occurences"], $baseday);
  167. }
  168. // Turn on hideattachments, because the attachments in this item are the exceptions
  169. mapi_setprops($this->message, array ( $this->proptags["hideattachments"] => true ));
  170. // Save recurrence data to message
  171. $this->saveRecurrence();
  172. return true;
  173. }
  174. /**
  175. * Modifies an existing exception, but only updates the given properties
  176. * NOTE: You can't remove properites from an exception, only add new ones
  177. */
  178. function modifyException($exception_props, $base_date, $exception_recips = array(), $copy_attach_from = false)
  179. {
  180. if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]])))
  181. return false;
  182. $baseday = $this->dayStartOf($base_date);
  183. $basetime = $baseday + $this->recur["startocc"] * 60;
  184. $extomodify = false;
  185. for($i = 0, $len = count($this->recur["changed_occurences"]); $i < $len; $i++)
  186. if($this->isSameDay($this->recur["changed_occurences"][$i]["basedate"], $baseday))
  187. $extomodify = &$this->recur["changed_occurences"][$i];
  188. if(!$extomodify)
  189. return false;
  190. // remove basedate property as we want to preserve the old value
  191. // client will send basedate with time part as zero, so discard that value
  192. unset($exception_props[$this->proptags["basedate"]]);
  193. if (array_key_exists($this->proptags["startdate"], $exception_props))
  194. $extomodify["start"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
  195. if (array_key_exists($this->proptags["duedate"], $exception_props))
  196. $extomodify["end"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
  197. if (array_key_exists($this->proptags["subject"], $exception_props))
  198. $extomodify["subject"] = $exception_props[$this->proptags["subject"]];
  199. if (array_key_exists($this->proptags["location"], $exception_props))
  200. $extomodify["location"] = $exception_props[$this->proptags["location"]];
  201. if (array_key_exists($this->proptags["label"], $exception_props))
  202. $extomodify["label"] = $exception_props[$this->proptags["label"]];
  203. if (array_key_exists($this->proptags["reminder"], $exception_props))
  204. $extomodify["reminder_set"] = $exception_props[$this->proptags["reminder"]];
  205. if (array_key_exists($this->proptags["reminder_minutes"], $exception_props))
  206. $extomodify["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]];
  207. if (array_key_exists($this->proptags["alldayevent"], $exception_props))
  208. $extomodify["alldayevent"] = $exception_props[$this->proptags["alldayevent"]];
  209. if (array_key_exists($this->proptags["busystatus"], $exception_props))
  210. $extomodify["busystatus"] = $exception_props[$this->proptags["busystatus"]];
  211. $exception_props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}";
  212. // synchronize commonstart/commonend with startdate/duedate
  213. if (isset($exception_props[$this->proptags["startdate"]]))
  214. $exception_props[$this->proptags["commonstart"]] = $exception_props[$this->proptags["startdate"]];
  215. if (isset($exception_props[$this->proptags["duedate"]]))
  216. $exception_props[$this->proptags["commonend"]] = $exception_props[$this->proptags["duedate"]];
  217. $attach = $this->getExceptionAttachment($baseday);
  218. if(!$attach) {
  219. if ($copy_attach_from) {
  220. $this->deleteExceptionAttachment($base_date);
  221. $this->createException($exception_props, $base_date, false, $exception_recips, $copy_attach_from);
  222. } else {
  223. $this->createExceptionAttachment($exception_props, $exception_recips, $copy_attach_from);
  224. }
  225. } else {
  226. $message = mapi_attach_openobj($attach, MAPI_MODIFY);
  227. // Set exception properties on embedded message and save
  228. mapi_setprops($message, $exception_props);
  229. $this->setExceptionRecipients($message, $exception_recips, false);
  230. mapi_savechanges($message);
  231. // If a new start or duedate is provided, we update the properties 'PR_EXCEPTION_STARTTIME' and 'PR_EXCEPTION_ENDTIME'
  232. // on the attachment which holds the embedded msg and save everything.
  233. $props = array();
  234. if (isset($exception_props[$this->proptags["startdate"]]))
  235. $props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
  236. if (isset($exception_props[$this->proptags["duedate"]]))
  237. $props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
  238. if (!empty($props))
  239. mapi_setprops($attach, $props);
  240. mapi_savechanges($attach);
  241. }
  242. // Save recurrence data to message
  243. $this->saveRecurrence();
  244. return true;
  245. }
  246. // Checks to see if the following is true:
  247. // 1) The exception to be created doesn't create two exceptions starting on one day (however, they can END on the same day by modifying duration)
  248. // 2) The exception to be created doesn't 'jump' over another occurrence (which may be an exception itself!)
  249. //
  250. // Both $basedate and $start are in LOCAL time
  251. function isValidExceptionDate($basedate, $start)
  252. {
  253. // The way we do this is to look at the days that we're 'moving' the item in the exception. Each
  254. // of these days may only contain the item that we're modifying. Any other item violates the rules.
  255. if($this->isException($basedate)) {
  256. // If we're modifying an exception, we want to look at the days that we're 'moving' compared to where
  257. // the exception used to be.
  258. $oldexception = $this->getChangeException($basedate);
  259. $prevday = $this->dayStartOf($oldexception["start"]);
  260. } else {
  261. // If its a new exception, we want to look at the original placement of this item.
  262. $prevday = $basedate;
  263. }
  264. $startday = $this->dayStartOf($start);
  265. // Get all the occurrences on the days between the basedate (may be reversed)
  266. if($prevday < $startday)
  267. $items = $this->getItems($this->toGMT($this->tz, $prevday), $this->toGMT($this->tz, $startday + 24 * 60 * 60));
  268. else
  269. $items = $this->getItems($this->toGMT($this->tz, $startday), $this->toGMT($this->tz, $prevday + 24 * 60 * 60));
  270. // There should now be exactly one item, namely the item that we are modifying. If there are any other items in the range,
  271. // then we abort the change, since one of the rules has been violated.
  272. return count($items) == 1;
  273. }
  274. /**
  275. * Check to see if the exception proposed at a certain basedate is allowed concerning reminder times:
  276. *
  277. * Both must be true:
  278. * - reminder time of this item is not before the starttime of the previous recurring item
  279. * - reminder time of the next item is not before the starttime of this item
  280. *
  281. * @param date $basedate the base date of the exception (LOCAL time of non-exception occurrence)
  282. * @param string $reminderminutes reminder minutes which is set of the item
  283. * @param date $startdate the startdate of the selected item
  284. * @returns boolean if the reminder minutes value valid (FALSE if either of the rules above are FALSE)
  285. */
  286. function isValidReminderTime($basedate, $reminderminutes, $startdate)
  287. {
  288. $isreminderrangeset = false;
  289. // get all occurence items before the seleceted items occurence starttime
  290. $occitems = $this->getItems($this->messageprops[$this->proptags["startdate"]], $this->toGMT($this->tz, $basedate));
  291. if(!empty($occitems)) {
  292. // as occitems array is sorted in ascending order of startdate, to get the previous occurence we take the last items in occitems .
  293. $previousitem_startdate = $occitems[count($occitems) - 1][$this->proptags["startdate"]];
  294. // if our reminder is set before or equal to the beginning of the previous occurrence, then that's not allowed
  295. if($startdate - ($reminderminutes*60) <= $previousitem_startdate)
  296. return false;
  297. }
  298. // Get the endtime of the current occurrence and find the next two occurrences (including the current occurrence)
  299. $currentOcc = $this->getItems($this->toGMT($this->tz, $basedate), 0x7ff00000, 2, true);
  300. // If there are another two occurrences, then the first is the current occurrence, and the one after that
  301. // is the next occurrence.
  302. if(count($currentOcc) > 1) {
  303. $next = $currentOcc[1];
  304. // Get reminder time of the next occurrence.
  305. $nextOccReminderTime = $next[$this->proptags["startdate"]] - ($next[$this->proptags["reminder_minutes"]] * 60);
  306. // If the reminder time of the next item is before the start of this item, then that's not allowed
  307. if($nextOccReminderTime <= $startdate)
  308. return false;
  309. }
  310. // All was ok
  311. return true;
  312. }
  313. function setRecurrence($tz, $recur)
  314. {
  315. // only reset timezone if specified
  316. if($tz)
  317. $this->tz = $tz;
  318. $this->recur = $recur;
  319. if(!isset($this->recur["changed_occurences"]))
  320. $this->recur["changed_occurences"] = Array();
  321. if(!isset($this->recur["deleted_occurences"]))
  322. $this->recur["deleted_occurences"] = Array();
  323. $this->deleteAttachments();
  324. $this->saveRecurrence();
  325. // if client has not set the recurring_pattern then we should generate it and save it
  326. $messageProps = mapi_getprops($this->message, Array($this->proptags["recurring_pattern"]));
  327. if (empty($messageProps[$this->proptags["recurring_pattern"]]))
  328. $this->saveRecurrencePattern();
  329. }
  330. // Returns the start or end time of the occurrence on the given base date.
  331. // This assumes that the basedate you supply is in LOCAL time
  332. function getOccurrenceStart($basedate) {
  333. $daystart = $this->dayStartOf($basedate);
  334. return $this->toGMT($this->tz, $daystart + $this->recur["startocc"] * 60);
  335. }
  336. function getOccurrenceEnd($basedate) {
  337. $daystart = $this->dayStartOf($basedate);
  338. return $this->toGMT($this->tz, $daystart + $this->recur["endocc"] * 60);
  339. }
  340. // Backwards compatible code
  341. function getOccurenceStart($basedate) {
  342. return $this->getOccurrenceStart($basedate);
  343. }
  344. function getOccurenceEnd($basedate) {
  345. return $this->getOccurrenceEnd($basedate);
  346. }
  347. /**
  348. * This function returns the next remindertime starting from $timestamp
  349. * When no next reminder exists, false is returned.
  350. *
  351. * Note: Before saving this new reminder time (when snoozing), you must check for
  352. * yourself if this reminder time is earlier than your snooze time, else
  353. * use your snooze time and not this reminder time.
  354. */
  355. function getNextReminderTime($timestamp)
  356. {
  357. /**
  358. * Get next item from now until forever, but max 1 item with reminder set
  359. * Note 0x7ff00000 instead of 0x7fffffff because of possible overflow failures when converting to GMT....
  360. * Here for getting next 10 occurences assuming that next here we will be able to find
  361. * nextreminder occurence in 10 occureneces
  362. */
  363. $items = $this->getItems($timestamp, 0x7ff00000, 10, true);
  364. // Initially setting nextreminder to false so when no next reminder exists, false is returned.
  365. $nextreminder = false;
  366. /**
  367. * Loop through all reminder which we get in items variable
  368. * and check whether the remindertime is greater than timestamp.
  369. * On the first occurence of greater nextreminder break the loop
  370. * and return the value to calling function.
  371. */
  372. for($i = 0, $len = count($items); $i < $len; $i++)
  373. {
  374. $item = $items[$i];
  375. $tempnextreminder = $item[$this->proptags["startdate"]] - ( $item[$this->proptags["reminder_minutes"]] * 60 );
  376. // If tempnextreminder is greater than timestamp then save it in nextreminder and break from the loop.
  377. if($tempnextreminder > $timestamp)
  378. {
  379. $nextreminder = $tempnextreminder;
  380. break;
  381. }
  382. }
  383. return $nextreminder;
  384. }
  385. /**
  386. * Note: Static function, more like a utility function.
  387. *
  388. * Gets all the items (including recurring items) in the specified calendar in the given timeframe. Items are
  389. * included as a whole if they overlap the interval <$start, $end> (non-inclusive). This means that if the interval
  390. * is <08:00 - 14:00>, the item [6:00 - 8:00> is NOT included, nor is the item [14:00 - 16:00>. However, the item
  391. * [7:00 - 9:00> is included as a whole, and is NOT capped to [8:00 - 9:00>.
  392. *
  393. * @param $store resource The store in which the calendar resides
  394. * @param $calendar resource The calendar to get the items from
  395. * @param $viewstart int Timestamp of beginning of view window
  396. * @param $viewend int Timestamp of end of view window
  397. * @param $propsrequested array Array of properties to return
  398. * @param $rows array Array of rowdata as if they were returned directly from mapi_table_queryrows. Each recurring item is
  399. * expanded so that it seems that there are only many single appointments in the table.
  400. */
  401. static function getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested)
  402. {
  403. return getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested);
  404. }
  405. /*****************************************************************************************************************
  406. * CODE BELOW THIS LINE IS FOR INTERNAL USE ONLY
  407. *****************************************************************************************************************
  408. */
  409. /**
  410. * Generates and stores recurrence pattern string to recurring_pattern property.
  411. */
  412. function saveRecurrencePattern()
  413. {
  414. // Start formatting the properties in such a way we can apply
  415. // them directly into the recurrence pattern.
  416. $type = $this->recur['type'];
  417. $everyn = $this->recur['everyn'];
  418. $start = $this->recur['start'];
  419. $end = $this->recur['end'];
  420. $term = $this->recur['term'];
  421. $numocc = isset($this->recur['numoccur']) ? $this->recur['numoccur'] : false;
  422. $startocc = $this->recur['startocc'];
  423. $endocc = $this->recur['endocc'];
  424. $pattern = '';
  425. $occSingleDayRank = false;
  426. $occTimeRange = ($startocc != 0 && $endocc != 0);
  427. switch ($type) {
  428. // Daily
  429. case 0x0A:
  430. if ($everyn == 1) {
  431. $type = dgettext("kopano", "workday");
  432. $occSingleDayRank = true;
  433. } else if ($everyn == (24 * 60)) {
  434. $type = dgettext("kopano", "day");
  435. $occSingleDayRank = true;
  436. } else {
  437. $everyn /= (24 * 60);
  438. $type = dgettext("kopano", "days");
  439. $occSingleDayRank = false;
  440. }
  441. break;
  442. // Weekly
  443. case 0x0B:
  444. if ($everyn == 1) {
  445. $type = dgettext("kopano", "week");
  446. $occSingleDayRank = true;
  447. } else {
  448. $type = dgettext("kopano", "weeks");
  449. $occSingleDayRank = false;
  450. }
  451. break;
  452. // Monthly
  453. case 0x0C:
  454. if ($everyn == 1) {
  455. $type = dgettext("kopano", "month");
  456. $occSingleDayRank = true;
  457. } else {
  458. $type = dgettext("kopano", "months");
  459. $occSingleDayRank = false;
  460. }
  461. break;
  462. // Yearly
  463. case 0x0D:
  464. if ($everyn <= 12) {
  465. $everyn = 1;
  466. $type = dgettext("kopano", "year");
  467. $occSingleDayRank = true;
  468. } else {
  469. $everyn = $everyn / 12;
  470. $type = dgettext("kopano", "years");
  471. $occSingleDayRank = false;
  472. }
  473. break;
  474. }
  475. // get timings of the first occurence
  476. $firstoccstartdate = isset($startocc) ? $start + (((int) $startocc) * 60) : $start;
  477. $firstoccenddate = isset($endocc) ? $end + (((int) $endocc) * 60) : $end;
  478. $start = gmdate(dgettext("kopano","d-m-Y"), $firstoccstartdate);
  479. $end = gmdate(dgettext("kopano","d-m-Y"), $firstoccenddate);
  480. $startocc = gmdate(dgettext("kopano","G:i"), $firstoccstartdate);
  481. $endocc = gmdate(dgettext("kopano","G:i"), $firstoccenddate);
  482. // Based on the properties, we need to generate the recurrence pattern string.
  483. // This is obviously very easy since we can simply concatenate a bunch of strings,
  484. // however this messes up translations for languages which order their words
  485. // differently.
  486. // To improve translation quality we create a series of default strings, in which
  487. // we only have to fill in the correct variables. The base string is thus selected
  488. // based on the available properties.
  489. if ($term == 0x23) {
  490. // Never ends
  491. if ($occTimeRange) {
  492. if ($occSingleDayRank)
  493. $pattern = sprintf(dgettext("kopano","Occurs every %s effective %s from %s to %s."), $type, $start, $startocc, $endocc);
  494. else
  495. $pattern = sprintf(dgettext("kopano","Occurs every %s %s effective %s from %s to %s."), $everyn, $type, $start, $startocc, $endocc);
  496. } else {
  497. if ($occSingleDayRank)
  498. $pattern = sprintf(dgettext("kopano","Occurs every %s effective %s."), $type, $start);
  499. else
  500. $pattern = sprintf(dgettext("kopano","Occurs every %s %s effective %s."), $everyn, $type, $start);
  501. }
  502. } else if ($term == 0x22) {
  503. // After a number of times
  504. if ($occTimeRange) {
  505. if ($occSingleDayRank)
  506. $pattern = sprintf(dngettext("kopano","Occurs every %s effective %s for %s occurence from %s to %s.",
  507. "Occurs every %s effective %s for %s occurences from %s to %s.", $numocc), $type, $start, $numocc, $startocc, $endocc);
  508. else
  509. $pattern = sprintf(dngettext("kopano","Occurs every %s %s effective %s for %s occurence from %s to %s.",
  510. "Occurs every %s %s effective %s for %s occurences %s to %s.", $numocc), $everyn, $type, $start, $numocc, $startocc, $endocc);
  511. } else {
  512. if ($occSingleDayRank)
  513. $pattern = sprintf(dngettext("kopano","Occurs every %s effective %s for %s occurence.",
  514. "Occurs every %s effective %s for %s occurences.", $numocc), $type, $start, $numocc);
  515. else
  516. $pattern = sprintf(dngettext("kopano","Occurs every %s %s effective %s for %s occurence.",
  517. "Occurs every %s %s effective %s for %s occurences.", $numocc), $everyn, $type, $start, $numocc);
  518. }
  519. } else if ($term == 0x21) {
  520. // After the given enddate
  521. if ($occTimeRange) {
  522. if ($occSingleDayRank)
  523. $pattern = sprintf(dgettext("kopano","Occurs every %s effective %s until %s from %s to %s."), $type, $start, $end, $startocc, $endocc);
  524. else
  525. $pattern = sprintf(dgettext("kopano","Occurs every %s %s effective %s until %s from %s to %s."), $everyn, $type, $start, $end, $startocc, $endocc);
  526. } else {
  527. if ($occSingleDayRank)
  528. $pattern = sprintf(dgettext("kopano","Occurs every %s effective %s until %s."), $type, $start, $end);
  529. else
  530. $pattern = sprintf(dgettext("kopano","Occurs every %s %s effective %s until %s."), $everyn, $type, $start, $end);
  531. }
  532. }
  533. if (!empty($pattern))
  534. mapi_setprops($this->message, Array($this->proptags["recurring_pattern"] => $pattern ));
  535. }
  536. /*
  537. * Remove an exception by base_date. This is the base date in local daystart time
  538. */
  539. function deleteException($base_date)
  540. {
  541. // Remove all exceptions on $base_date from the deleted and changed occurrences lists
  542. // Remove all items in $todelete from deleted_occurences
  543. $new = Array();
  544. foreach($this->recur["deleted_occurences"] as $entry)
  545. if($entry != $base_date)
  546. $new[] = $entry;
  547. $this->recur["deleted_occurences"] = $new;
  548. $new = Array();
  549. foreach($this->recur["changed_occurences"] as $entry) {
  550. if(!$this->isSameDay($entry["basedate"], $base_date))
  551. $new[] = $entry;
  552. else
  553. $this->deleteExceptionAttachment($this->toGMT($this->tz, $base_date + $this->recur["startocc"] * 60));
  554. }
  555. $this->recur["changed_occurences"] = $new;
  556. }
  557. /**
  558. * Function which saves the exception data in an attachment.
  559. * @param array $exception_props the exception data (like any other MAPI appointment)
  560. * @param array $exception_recips list of recipients
  561. * @param mapi_message $copy_attach_from mapi message from which attachments should be copied
  562. * @return array properties of the exception
  563. */
  564. function createExceptionAttachment($exception_props, $exception_recips = array(), $copy_attach_from = false)
  565. {
  566. // Create new attachment.
  567. $attachment = mapi_message_createattach($this->message);
  568. $props = array();
  569. $props[PR_ATTACHMENT_FLAGS] = 2;
  570. $props[PR_ATTACHMENT_HIDDEN] = true;
  571. $props[PR_ATTACHMENT_LINKID] = 0;
  572. $props[PR_ATTACH_FLAGS] = 0;
  573. $props[PR_ATTACH_METHOD] = 5;
  574. $props[PR_DISPLAY_NAME] = "Exception";
  575. $props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
  576. $props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
  577. mapi_message_setprops($attachment, $props);
  578. $imessage = mapi_attach_openobj($attachment, MAPI_CREATE | MAPI_MODIFY);
  579. if ($copy_attach_from) {
  580. $attachmentTable = mapi_message_getattachmenttable($copy_attach_from);
  581. if($attachmentTable) {
  582. $attachments = mapi_table_queryallrows($attachmentTable, array(PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME, PR_ATTACHMENT_HIDDEN, PR_DISPLAY_NAME, PR_ATTACH_METHOD));
  583. foreach($attachments as $attach_props){
  584. $attach_old = mapi_message_openattach($copy_attach_from, (int) $attach_props[PR_ATTACH_NUM]);
  585. $attach_newResourceMsg = mapi_message_createattach($imessage);
  586. mapi_copyto($attach_old, array(), array(), $attach_newResourceMsg, 0);
  587. mapi_savechanges($attach_newResourceMsg);
  588. }
  589. }
  590. }
  591. $props = $props + $exception_props;
  592. // FIXME: the following piece of code is written to fix the creation
  593. // of an exception. This is only a quickfix as it is not yet possible
  594. // to change an existing exception.
  595. // remove mv properties when needed
  596. foreach ($props as $propTag=>$propVal)
  597. if (mapi_prop_type($propTag) & MV_FLAG && is_null($propVal))
  598. unset($props[$propTag]);
  599. mapi_message_setprops($imessage, $props);
  600. $this->setExceptionRecipients($imessage, $exception_recips, true);
  601. mapi_message_savechanges($imessage);
  602. mapi_message_savechanges($attachment);
  603. }
  604. /**
  605. * Function which deletes the attachment of an exception.
  606. * @param date $base_date base date of the attachment. Should be in GMT. The attachment
  607. * actually saves the real time of the original date, so we have
  608. * to check whether it's on the same day.
  609. */
  610. function deleteExceptionAttachment($base_date)
  611. {
  612. $attachments = mapi_message_getattachmenttable($this->message);
  613. $attachTable = mapi_table_queryallrows($attachments, Array(PR_ATTACH_NUM));
  614. foreach($attachTable as $attachRow)
  615. {
  616. $tempattach = mapi_message_openattach($this->message, $attachRow[PR_ATTACH_NUM]);
  617. $exception = mapi_attach_openobj($tempattach);
  618. $data = mapi_message_getprops($exception, array($this->proptags["basedate"]));
  619. if ($this->dayStartOf($this->fromGMT($this->tz, $data[$this->proptags["basedate"]])) == $this->dayStartOf($base_date))
  620. mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]);
  621. }
  622. }
  623. /**
  624. * Function which deletes all attachments of a message.
  625. */
  626. function deleteAttachments()
  627. {
  628. $attachments = mapi_message_getattachmenttable($this->message);
  629. $attachTable = mapi_table_queryallrows($attachments, Array(PR_ATTACH_NUM, PR_ATTACHMENT_HIDDEN));
  630. foreach($attachTable as $attachRow)
  631. if (isset($attachRow[PR_ATTACHMENT_HIDDEN]) && $attachRow[PR_ATTACHMENT_HIDDEN])
  632. mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]);
  633. }
  634. /**
  635. * Get an exception attachment based on its basedate
  636. */
  637. function getExceptionAttachment($base_date)
  638. {
  639. // Retrieve only embedded messages
  640. $attach_res = Array(RES_AND,
  641. Array(
  642. Array(RES_PROPERTY,
  643. Array(RELOP => RELOP_EQ,
  644. ULPROPTAG => PR_ATTACH_METHOD,
  645. VALUE => array(PR_ATTACH_METHOD => ATTACH_EMBEDDED_MSG)
  646. )
  647. )
  648. )
  649. );
  650. $attachments = mapi_message_getattachmenttable($this->message);
  651. $attachRows = mapi_table_queryallrows($attachments, Array(PR_ATTACH_NUM), $attach_res);
  652. if(is_array($attachRows)) {
  653. foreach($attachRows as $attachRow)
  654. {
  655. $tempattach = mapi_message_openattach($this->message, $attachRow[PR_ATTACH_NUM]);
  656. $exception = mapi_attach_openobj($tempattach);
  657. $data = mapi_message_getprops($exception, array($this->proptags["basedate"]));
  658. if ($this->isSameDay($this->fromGMT($this->tz,$data[$this->proptags["basedate"]]), $base_date))
  659. return $tempattach;
  660. }
  661. }
  662. return false;
  663. }
  664. /**
  665. * processOccurrenceItem, adds an item to a list of occurrences, but only if the following criteria are met:
  666. * - The resulting occurrence (or exception) starts or ends in the interval <$start, $end>
  667. * - The ocurrence isn't specified as a deleted occurrence
  668. * @param array $items reference to the array to be added to
  669. * @param date $start start of timeframe in GMT TIME
  670. * @param date $end end of timeframe in GMT TIME
  671. * @param date $basedate (hour/sec/min assumed to be 00:00:00) in LOCAL TIME OF THE OCCURRENCE
  672. * @param int $startocc start of occurrence since beginning of day in minutes
  673. * @param int $endocc end of occurrence since beginning of day in minutes
  674. * @param int $tz the timezone info for this occurrence ( applied to $basedate / $startocc / $endocc )
  675. * @param bool $reminderonly If TRUE, only add the item if the reminder is set
  676. */
  677. function processOccurrenceItem(&$items, $start, $end, $basedate, $startocc, $endocc, $tz, $reminderonly)
  678. {
  679. $exception = $this->isException($basedate);
  680. if($exception){
  681. return false;
  682. }else{
  683. $occstart = $basedate + $startocc * 60;
  684. $occend = $basedate + $endocc * 60;
  685. // Convert to GMT
  686. $occstart = $this->toGMT($tz, $occstart);
  687. $occend = $this->toGMT($tz, $occend);
  688. /**
  689. * FIRST PART : Check range criterium. Exact matches (eg when $occstart == $end), do NOT match since you cannot
  690. * see any part of the appointment. Partial overlaps DO match.
  691. *
  692. * SECOND PART : check if occurence is not a zero duration occurrence which
  693. * starts at 00:00 and ends on 00:00. if it is so, then process
  694. * the occurrence and send it in response.
  695. */
  696. if(($occstart >= $end || $occend <= $start) && !($occstart == $occend && $occstart == $start))
  697. return;
  698. // Properties for this occurrence are the same as the main object,
  699. // With these properties overridden
  700. $newitem = $this->messageprops;
  701. $newitem[$this->proptags["startdate"]] = $occstart;
  702. $newitem[$this->proptags["duedate"]] = $occend;
  703. $newitem[$this->proptags["commonstart"]] = $occstart;
  704. $newitem[$this->proptags["commonend"]] = $occend;
  705. $newitem["basedate"] = $basedate;
  706. }
  707. // If reminderonly is set, only add reminders
  708. if($reminderonly && (!isset($newitem[$this->proptags["reminder"]]) || $newitem[$this->proptags["reminder"]] == false))
  709. return;
  710. $items[] = $newitem;
  711. }
  712. /**
  713. * processExceptionItem, adds an all exception item to a list of occurrences, without any constraint on timeframe
  714. * @param array $items reference to the array to be added to
  715. * @param date $start start of timeframe in GMT TIME
  716. * @param date $end end of timeframe in GMT TIME
  717. */
  718. function processExceptionItems(&$items, $start, $end)
  719. {
  720. $limit = 0;
  721. foreach($this->recur["changed_occurences"] as $exception) {
  722. // Convert to GMT
  723. $occstart = $this->toGMT($this->tz, $exception["start"]);
  724. $occend = $this->toGMT($this->tz, $exception["end"]);
  725. // Check range criterium. Exact matches (eg when $occstart == $end), do NOT match since you cannot
  726. // see any part of the appointment. Partial overlaps DO match.
  727. if($occstart >= $end || $occend <= $start)
  728. continue;
  729. array_push($items, $this->getExceptionProperties($exception));
  730. if($limit && (count($items) == $limit))
  731. break;
  732. }
  733. }
  734. /**
  735. * Function which verifies if on the given date an exception, delete or change, occurs.
  736. * @param date $date the date
  737. * @return array the exception, true - if an occurrence is deleted on the given date, false - no exception occurs on the given date
  738. */
  739. function isException($basedate)
  740. {
  741. if($this->isDeleteException($basedate))
  742. return true;
  743. if($this->getChangeException($basedate) != false)
  744. return true;
  745. return false;
  746. }
  747. /**
  748. * Returns TRUE if there is a DELETE exception on the given base date
  749. */
  750. function isDeleteException($basedate)
  751. {
  752. // Check if the occurrence is deleted on the specified date
  753. foreach($this->recur["deleted_occurences"] as $deleted)
  754. if($this->isSameDay($deleted, $basedate))
  755. return true;
  756. return false;
  757. }
  758. /**
  759. * Returns the exception if there is a CHANGE exception on the given base date, or FALSE otherwise
  760. */
  761. function getChangeException($basedate)
  762. {
  763. // Check if the occurrence is modified on the specified date
  764. foreach($this->recur["changed_occurences"] as $changed)
  765. if($this->isSameDay($changed["basedate"], $basedate))
  766. return $changed;
  767. return false;
  768. }
  769. /**
  770. * Function to see if two dates are on the same day
  771. * @param date $time1 date 1
  772. * @param date $time2 date 2
  773. * @return boolean Returns TRUE when both dates are on the same day
  774. */
  775. function isSameDay($date1, $date2)
  776. {
  777. $time1 = $this->gmtime($date1);
  778. $time2 = $this->gmtime($date2);
  779. return $time1["tm_mon"] == $time2["tm_mon"] && $time1["tm_year"] == $time2["tm_year"] && $time1["tm_mday"] == $time2["tm_mday"];
  780. }
  781. /**
  782. * Function to get all properties of a single changed exception.
  783. * @param date $date base date of exception
  784. * @return array associative array of properties for the exception, compatible with
  785. */
  786. function getExceptionProperties($exception)
  787. {
  788. // Exception has same properties as main object, with some properties overridden:
  789. $item = $this->messageprops;
  790. // Special properties
  791. $item["exception"] = true;
  792. $item["basedate"] = $exception["basedate"]; // note that the basedate is always in local time !
  793. // MAPI-compatible properties (you can handle an exception as a normal calendar item like this)
  794. $item[$this->proptags["startdate"]] = $this->toGMT($this->tz, $exception["start"]);
  795. $item[$this->proptags["duedate"]] = $this->toGMT($this->tz, $exception["end"]);
  796. $item[$this->proptags["commonstart"]] = $item[$this->proptags["startdate"]];
  797. $item[$this->proptags["commonend"]] = $item[$this->proptags["duedate"]];
  798. if (isset($exception["subject"]))
  799. $item[$this->proptags["subject"]] = $exception["subject"];
  800. if (isset($exception["label"]))
  801. $item[$this->proptags["label"]] = $exception["label"];
  802. if (isset($exception["alldayevent"]))
  803. $item[$this->proptags["alldayevent"]] = $exception["alldayevent"];
  804. if (isset($exception["location"]))
  805. $item[$this->proptags["location"]] = $exception["location"];
  806. if (isset($exception["remind_before"]))
  807. $item[$this->proptags["reminder_minutes"]] = $exception["remind_before"];
  808. if (isset($exception["reminder_set"]))
  809. $item[$this->proptags["reminder"]] = $exception["reminder_set"];
  810. if (isset($exception["busystatus"]))
  811. $item[$this->proptags["busystatus"]] = $exception["busystatus"];
  812. return $item;
  813. }
  814. /**
  815. * Function which sets recipients for an exception.
  816. *
  817. * The $exception_recips can be provided in 2 ways:
  818. * - A delta which indicates which recipients must be added, removed or deleted.
  819. * - A complete array of the recipients which should be applied to the message.
  820. *
  821. * The first option is preferred as it will require less work to be executed.
  822. *
  823. * @param resource $message exception attachment of recurring item
  824. * @param array $exception_recips list of recipients
  825. * @param boolean $copy_orig_recips True to copy all recipients which are on the original
  826. * message to the attachment by default. False if only the $exception_recips changes should
  827. * be applied.
  828. */
  829. function setExceptionRecipients($message, $exception_recips, $copy_orig_recips = true)
  830. {
  831. if (isset($exception_recips['add']) || isset($exception_recips['remove']) || isset($exception_recips['modify']))
  832. $this->setDeltaExceptionRecipients($message, $exception_recips, $copy_orig_recips);
  833. else
  834. $this->setAllExceptionRecipients($message, $exception_recips);
  835. }
  836. /**
  837. * Function which applies the provided delta for recipients changes to the exception.
  838. *
  839. * The $exception_recips should be an array containing the following keys:
  840. * - "add": this contains an array of recipients which must be added
  841. * - "remove": This contains an array of recipients which must be removed
  842. * - "modify": This contains an array of recipients which must be modified
  843. *
  844. * @param resource $message exception attachment of recurring item
  845. * @param array $exception_recips list of recipients
  846. * @param boolean $copy_orig_recips True to copy all recipients which are on the original
  847. * message to the attachment by default. False if only the $exception_recips changes should
  848. * be applied.
  849. */
  850. function setDeltaExceptionRecipients($exception, $exception_recips, $copy_orig_recips)
  851. {
  852. // Check if the recipients from the original message should be copied,
  853. // if so, open the recipient table of the parent message and apply all
  854. // rows on the target recipient.
  855. if ($copy_orig_recips === true) {
  856. $origTable = mapi_message_getrecipienttable($this->message);
  857. $recipientRows = mapi_table_queryallrows($origTable, $this->recipprops);
  858. mapi_message_modifyrecipients($exception, MODRECIP_ADD, $recipientRows);
  859. }
  860. // Add organizer to meeting only if it is not organized.
  861. $msgprops = mapi_getprops($exception, array(PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_ADDRTYPE, PR_SENT_REPRESENTING_SEARCH_KEY, $this->proptags['responsestatus']));
  862. if (isset($msgprops[$this->proptags['responsestatus']]) && $msgprops[$this->proptags['responsestatus']] != olResponseOrganized)
  863. $this->addOrganizer($msgprops, $exception_recips['add']);
  864. // Remove all deleted recipients
  865. if (isset($exception_recips['remove'])) {
  866. foreach ($exception_recips['remove'] as &$recip) {
  867. if (!isset($recipient[PR_RECIPIENT_FLAGS]) || $recip[PR_RECIPIENT_FLAGS] != (recipReserved | recipExceptionalDeleted | recipSendable))
  868. $recip[PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalDeleted;
  869. else
  870. $recip[PR_RECIPIENT_FLAGS] = recipReserved | recipExceptionalDeleted | recipSendable;
  871. $recip[PR_RECIPIENT_TRACKSTATUS] = olResponseNone; // No Response required
  872. }
  873. unset($recip);
  874. mapi_message_modifyrecipients($exception, MODRECIP_MODIFY, $exception_recips['remove']);
  875. }
  876. // Add all new recipients
  877. if (isset($exception_recips['add']))
  878. mapi_message_modifyrecipients($exception, MODRECIP_ADD, $exception_recips['add']);
  879. // Modify the existing recipients
  880. if (isset($exception_recips['modify']))
  881. mapi_message_modifyrecipients($exception, MODRECIP_MODIFY, $exception_recips['modify']);
  882. }
  883. /**
  884. * Function which applies the provided recipients to the exception, also checks for deleted recipients.
  885. *
  886. * The $exception_recips should be an array containing all recipients which must be applied
  887. * to the exception. This will copy all recipients from the original message and then start filter
  888. * out all recipients which are not provided by the $exception_recips list.
  889. *
  890. * @param resource $message exception attachment of recurring item
  891. * @param array $exception_recips list of recipients
  892. */
  893. function setAllExceptionRecipients($message, $exception_recips)
  894. {
  895. $deletedRecipients = array();
  896. $useMessageRecipients = false;
  897. $recipientTable = mapi_message_getrecipienttable($message);
  898. $recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops);
  899. if (empty($recipientRows)) {
  900. $useMessageRecipients = true;
  901. $recipientTable = mapi_message_getrecipienttable($this->message);
  902. $recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops);
  903. }
  904. // Add organizer to meeting only if it is not organized.
  905. $msgprops = mapi_getprops($message, array(PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_ADDRTYPE, PR_SENT_REPRESENTING_SEARCH_KEY, $this->proptags['responsestatus']));
  906. if (isset($msgprops[$this->proptags['responsestatus']]) && $msgprops[$this->proptags['responsestatus']] != olResponseOrganized)
  907. $this->addOrganizer($msgprops, $exception_recips);
  908. if (!empty($exception_recips)) {
  909. foreach($recipientRows as $key => $recipient) {
  910. $found = false;
  911. foreach($exception_recips as $excep_recip)
  912. if ($recipient[PR_SEARCH_KEY] == $excep_recip[PR_SEARCH_KEY])
  913. $found = true;
  914. if (!$found) {
  915. $foundInDeletedRecipients = false;
  916. // Look if the $recipient is in the list of deleted recipients
  917. if (!empty($deletedRecipients)) {
  918. foreach($deletedRecipients as $recip) {
  919. if ($recip[PR_SEARCH_KEY] == $recipient[PR_SEARCH_KEY]){
  920. $foundInDeletedRecipients = true;
  921. break;
  922. }
  923. }
  924. }
  925. // If recipient is not in list of deleted recipient, add him
  926. if (!$foundInDeletedRecipients) {
  927. if (!isset($recipient[PR_RECIPIENT_FLAGS]) || $recipient[PR_RECIPIENT_FLAGS] != (recipReserved | recipExceptionalDeleted | recipSendable))
  928. $recipient[PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalDeleted;
  929. else
  930. $recipient[PR_RECIPIENT_FLAGS] = recipReserved | recipExceptionalDeleted | recipSendable;
  931. $recipient[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone; // No Response required
  932. $deletedRecipients[] = $recipient;
  933. }
  934. }
  935. // When $message contains a non-empty recipienttable, we must delete the recipients
  936. // before re-adding them. However, when $message is doesn't contain any recipients,
  937. // we are using the recipient table of the original message ($this->message)
  938. // rather then $message. In that case, we don't need to remove the recipients
  939. // from the $message, as the recipient table is already empty, and
  940. // mapi_message_modifyrecipients() will throw an error.
  941. if ($useMessageRecipients === false)
  942. mapi_message_modifyrecipients($message, MODRECIP_REMOVE, array($recipient));
  943. }
  944. $exception_recips = array_merge($exception_recips, $deletedRecipients);
  945. } else {
  946. $exception_recips = $recipientRows;
  947. }
  948. if (!empty($exception_recips))
  949. // Set the new list of recipients on the exception message, this also removes the existing recipients
  950. mapi_message_modifyrecipients($message, 0, $exception_recips);
  951. }
  952. /**
  953. * Function returns basedates of all changed occurrences
  954. *@return array array(
  955. 0 => 123459321
  956. )
  957. */
  958. function getAllExceptions()
  959. {
  960. $result = false;
  961. if (!empty($this->recur["changed_occurences"])) {
  962. $result = array();
  963. foreach($this->recur["changed_occurences"] as $exception)
  964. $result[] = $exception["basedate"];
  965. return $result;
  966. }
  967. return $result;
  968. }
  969. /**
  970. * Function which adds organizer to recipient list which is passed.
  971. * This function also checks if it has organizer.
  972. *
  973. * @param array $messageProps message properties
  974. * @param array $recipients recipients list of message.
  975. * @param boolean $isException true if we are processing recipient of exception
  976. */
  977. function addOrganizer($messageProps, &$recipients, $isException = false)
  978. {
  979. $hasOrganizer = false;
  980. // Check if meeting already has an organizer.
  981. foreach ($recipients as $key => $recipient){
  982. if (isset($recipient[PR_RECIPIENT_FLAGS]) && $recipient[PR_RECIPIENT_FLAGS] == (recipSendable | recipOrganizer))
  983. $hasOrganizer = true;
  984. else if ($isException && !isset($recipient[PR_RECIPIENT_FLAGS]))
  985. // Recipients for an occurrence
  986. $recipients[$key][PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalResponse;
  987. }
  988. if (!$hasOrganizer){
  989. // Create organizer.
  990. $organizer = array();
  991. $organizer[PR_ENTRYID] = $messageProps[PR_SENT_REPRESENTING_ENTRYID];
  992. $organizer[PR_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
  993. $organizer[PR_EMAIL_ADDRESS] = $messageProps[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
  994. $organizer[PR_RECIPIENT_TYPE] = MAPI_TO;
  995. $organizer[PR_RECIPIENT_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
  996. $organizer[PR_ADDRTYPE] = empty($messageProps[PR_SENT_REPRESENTING_ADDRTYPE]) ? 'SMTP' : $messageProps[PR_SENT_REPRESENTING_ADDRTYPE];
  997. $organizer[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;
  998. $organizer[PR_RECIPIENT_FLAGS] = recipSendable | recipOrganizer;
  999. $organizer[PR_SEARCH_KEY] = $messageProps[PR_SENT_REPRESENTING_SEARCH_KEY];
  1000. // Add organizer to recipients list.
  1001. array_unshift($recipients, $organizer);
  1002. }
  1003. }
  1004. }
  1005. /*
  1006. From http://www.ohelp-one.com/new-6765483-3268.html:
  1007. Recurrence Data Structure Offset Type Value
  1008. 0 ULONG (?) Constant : { 0x04, 0x30, 0x04, 0x30}
  1009. 4 UCHAR 0x0A + recurrence type: 0x0A for daily, 0x0B for weekly, 0x0C for
  1010. monthly, 0x0D for yearly
  1011. 5 UCHAR Constant: { 0x20}
  1012. 6 ULONG Seems to be a variant of the recurrence type: 1 for daily every n
  1013. days, 2 for daily every weekday and weekly, 3 for monthly or yearly. The
  1014. special exception is regenerating tasks that regenerate on a weekly basis: 0
  1015. is used in that case (I have no idea why).
  1016. Here's the recurrence-type-specific data. Because the daily every N days
  1017. data are 4 bytes shorter than the data for the other types, the offsets for
  1018. the rest of the data will be 4 bytes off depending on the recurrence type.
  1019. Daily every N days:
  1020. 10 ULONG ( N - 1) * ( 24 * 60). I'm not sure what this is used for, but it's consistent.
  1021. 14 ULONG N * 24 * 60: minutes between recurrences
  1022. 18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
  1023. regenerating tasks.
  1024. Daily every weekday (this is essentially a subtype of weekly recurrence):
  1025. 10 ULONG 6 * 24 * 60: minutes between recurrences ( a week... sort of)
  1026. 14 ULONG 1: recur every week (corresponds to the second parameter for weekly
  1027. recurrence)
  1028. 18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
  1029. regenerating tasks.
  1030. 22 ULONG 0x3E: bitmask for recurring every weekday (corresponds to fourth
  1031. parameter for weekly recurrence)
  1032. Weekly every N weeks for all events and non-regenerating tasks:
  1033. 10 ULONG 6 * 24 * 60: minutes between recurrences (a week... sort of)
  1034. 14 ULONG N: recurrence interval
  1035. 18 ULONG Constant: 0
  1036. 22 ULONG Bitmask for determining which days of the week the event recurs on
  1037. ( 1 << dayOfWeek, where Sunday is 0).
  1038. Weekly every N weeks for regenerating tasks: 10 ULONG Constant: 0
  1039. 14 ULONG N * 7 * 24 * 60: recurrence interval in minutes between occurrences
  1040. 18 ULONG Constant: 1
  1041. Monthly every N months on day D:
  1042. 10 ULONG This is the most complicated value
  1043. in the entire mess. It's basically a very complicated way of stating the
  1044. recurrence interval. I tweaked fbs' basic algorithm. DateTime::MonthInDays
  1045. simply returns the number of days in a given month, e.g. 31 for July for 28
  1046. for February (the algorithm doesn't take into account leap years, but it
  1047. doesn't seem to matter). My DateTime object, like Microsoft's COleDateTime,
  1048. uses 1-based months (i.e. January is 1, not 0). With that in mind, this
  1049. works:
  1050. long monthIndex = ( ( ( ( 12 % schedule-=GetInterval()) *
  1051. ( ( schedule-=GetStartDate().GetYear() - 1601) %
  1052. schedule-=GetInterval())) % schedule-=GetInterval()) +
  1053. ( schedule-=GetStartDate().GetMonth() - 1)) % schedule-=GetInterval();
  1054. for( int i = 0; i < monthIndex; i++)
  1055. {
  1056. value += DateTime::GetDaysInMonth( ( i % 12) + 1) * 24 * 60;
  1057. }
  1058. This should work for any recurrence interval, including those greater than
  1059. 12.
  1060. 14 ULONG N: recurrence interval
  1061. 18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
  1062. regenerating tasks.
  1063. 22 ULONG D: day of month the event recurs on (if this value is greater than
  1064. the number of days in a given month [e.g. 31 for and recurs in June], then
  1065. the event will recur on the last day of the month)
  1066. Monthly every N months on the Xth Y (e.g. "2nd Tuesday"):
  1067. 10 ULONG See above: same as for monthly every N months on day D
  1068. 14 ULONG N: recurrence interval
  1069. 18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
  1070. regenerating tasks.
  1071. 22 ULONG Y: bitmask for determining which day of the week the event recurs
  1072. on (see weekly every N weeks). Some useful values are 0x7F for any day, 0x3E
  1073. for a weekday, or 0x41 for a weekend day.
  1074. 26 ULONG X: 1 for first occurrence, 2 for second, etc. 5 for last
  1075. occurrence. E.g. for "2nd Tuesday", you should have values of 0x04 for the
  1076. prior value and 2 for this one.
  1077. Yearly on day D of month M:
  1078. 10 ULONG M (sort of): This is another messy
  1079. value. It's the number of minute since the startning of the year to the
  1080. given month. For an explanation of GetDaysInMonth, see monthly every N
  1081. months. This will work:
  1082. ULONG monthOfYearInMinutes = 0;
  1083. for( int i = DateTime::cJanuary; i < schedule-=GetMonth(); i++)
  1084. {
  1085. monthOfYearInMinutes += DateTime::GetDaysInMonth( i) * 24 * 60;
  1086. }
  1087. 14 ULONG 12: recurrence interval in months. Naturally, 12.
  1088. 18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
  1089. regenerating tasks.
  1090. 22 ULONG D: day of month the event recurs on. See monthly every N months on
  1091. day D.
  1092. Yearly on the Xth Y of month M: 10 ULONG M (sort of): See yearly on day D of
  1093. month M.
  1094. 14 ULONG 12: recurrence interval in months. Naturally, 12.
  1095. 18 ULONG Constant: 0
  1096. 22 ULONG Y: see monthly every N months on the Xth Y.
  1097. 26 ULONG X: see monthly every N months on the Xth Y.
  1098. After these recurrence-type-specific values, the offsets will change
  1099. depending on the type. For every type except daily every N days, the offsets
  1100. will grow by at least 4. For those types using the Xth Y, the offsets will
  1101. grow by an additional 4, for a total of 8. The offsets for the rest of these
  1102. values will be given for the most basic case, daily every N days, i.e.
  1103. without any growth. Adjust as necessary. Also, the presence of exceptions
  1104. will change the offsets following the exception data by a variable number of
  1105. bytes, so the offsets given in the table are accurate only for those
  1106. recurrence patterns without any exceptions.
  1107. 22 UCHAR Type of pattern termination: 0x21 for terminating on a given date, 0x22 for terminating
  1108. after a given number of recurrences, or 0x23 for never terminating
  1109. (recurring infinitely)
  1110. 23 UCHARx3 Constant: { 0x20, 0x00, 0x00}
  1111. 26 ULONG Number of occurrences in pattern: 0 for infinite recurrence,
  1112. otherwise supply the value, even if it terminates on a given date, not after
  1113. a given number
  1114. 30 ULONG Constant: 0
  1115. 34 ULONG Number of exceptions to pattern (i.e. deleted or changed
  1116. occurrences)
  1117. .... ULONGxN Base date of each exception, given in hundreds of nanoseconds
  1118. since 1601, so see below to turn them into a comprehensible format. The base
  1119. date of an exception is the date (and only the date-- not the time) the
  1120. exception would have occurred on in the pattern. They must occur in
  1121. ascending order.
  1122. 38 ULONG Number of changed exceptions (i.e. total number of exceptions -
  1123. number of deleted exceptions): if there are changed exceptions, again, more
  1124. data will be needed, but that will wait
  1125. .... ULONGxN Start date (and only the date-- not the time) of each changed
  1126. exception, i.e. the exceptions which aren't deleted. These must also occur
  1127. in ascending order. If all of the exceptions are deleted, this data will be
  1128. absent. If present, they will be in the format above. Any dates that are in
  1129. the first list but not in the second are exceptions that have been deleted
  1130. (i.e. the difference between the two sets). Note that this is the start date
  1131. (including time), not the base date. Given that the values are unordered and
  1132. that they can't be matched up against the previous list in this iteration of
  1133. the recurrence data (they could in previous ones), it is very difficult to
  1134. tell which exceptions are deleted and which are changed. Fortunately, for
  1135. this new format, the base dates are given on the attachment representing the
  1136. changed exception (described below), so you can simply ignore this list of
  1137. changed exceptions. Just create a list of exceptions from the previous list
  1138. and assume they're all deleted unless you encounter an attachment with a
  1139. matching base date later on.
  1140. 42 ULONG Start date of pattern given in hundreds of nanoseconds since 1601;
  1141. see below for an explanation.
  1142. 46 ULONG End date of pattern: see start date of pattern
  1143. 50 ULONG Constant: { 0x06, 0x30, 0x00, 0x00}
  1144. NOTE: I find the following 8-byte sequence of bytes to be very useful for
  1145. orienting myself when looking at the raw data. If you can find { 0x06, 0x30,
  1146. 0x00, 0x00, 0x08, 0x30, 0x00, 0x00}, you can use these tables to work either
  1147. forwards or backwards to find the data you need. The sequence sort of
  1148. delineates certain critical exception-related data and delineates the
  1149. exceptions themselves from the rest of the data and is relatively easy to
  1150. find. If you're going to be meddling in here a lot, I suggest making a
  1151. friend of ol' 0x00003006.
  1152. 54 UCHAR This number is some kind of version indicator. Use 0x08 for Outlook
  1153. 2003. I believe 0x06 is Outlook 2000 and possibly 98, while 0x07 is Outlook
  1154. XP. This number must be consistent with the features of the data structure
  1155. generated by the version of Outlook indicated thereby-- there are subtle
  1156. differences between the structures, and, if the version doesn't match the
  1157. data, Outlook will sometimes failto read the structure.
  1158. 55 UCHARx3 Constant: { 0x30, 0x00, 0x00}
  1159. 58 ULONG Start time of occurrence in minutes: e.g. 0 for midnight or 720 for
  1160. 12 PM
  1161. 62 ULONG End time of occurrence in minutes: i.e. start time + duration, e.g.
  1162. 900 for an event that starts at 12 PM and ends at 3PM
  1163. Exception Data 66 USHORT Number of changed exceptions: essentially a check
  1164. on the prior occurrence of this value; should be equivalent.
  1165. NOTE: The following structure will occur N many times (where N = number of
  1166. changed exceptions), and each structure can be of variable length.
  1167. .... ULONG Start date of changed exception given in hundreds of nanoseconds
  1168. since 1601
  1169. .... ULONG End date of changed exception given in hundreds of nanoseconds
  1170. since 1601
  1171. .... ULONG This is a value I don't clearly understand. It seems to be some
  1172. kind of archival value that matches the start time most of the time, but
  1173. will lag behind when the start time is changed and then match up again under
  1174. certain conditions later. In any case, setting to the same value as the
  1175. start time seems to work just fine (more information on this value would be
  1176. appreciated).
  1177. .... USHORT Bitmask of changes to the exception (see below). This will be 0
  1178. if the only changes to the exception were to its start or end time.
  1179. .... ULONGxN Numeric values (e.g. label or minutes to remind before the
  1180. event) changed in the exception. These will occur in the order of their
  1181. corresponding bits (see below). If no numeric values were changed, then
  1182. these values will be absent.
  1183. NOTE: The following three values constitute a single sub-structure that will
  1184. occur N many times, where N is the number of strings that are changed in the
  1185. exception. Since there are at most 2 string values that can be excepted
  1186. (i.e. subject [or description], and location), there can at most be two of
  1187. these, but there may be none.
  1188. .... USHORT Length of changed string value with NULL character
  1189. .... USHORT Length of changed string value without NULL character (i.e.
  1190. previous value - 1)
  1191. .... CHARxN Changed string value (without NULL terminator)
  1192. Unicode Data NOTE: If a string value was changed on an exception, those
  1193. changed string values will reappear here in Unicode format after 8 bytes of
  1194. NULL padding (possibly a Unicode terminator?). For each exception with a
  1195. changed string value, there will be an identifier, followed by the changed
  1196. strings in Unicode. The strings will occur in the order of their
  1197. corresponding bits (see below). E.g., if both subject and location were
  1198. changed in the exception, there would be the 3-ULONG identifier, then the
  1199. length of the subject, then the subject, then the length of the location,
  1200. then the location.
  1201. 70 ULONGx2 Constant: { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}. This
  1202. padding serves as a barrier between the older data structure and the
  1203. appended Unicode data. This is the same sequence as the Unicode terminator,
  1204. but I'm not sure whether that's its identity or not.
  1205. .... ULONGx3 These are the three times used to identify the exception above:
  1206. start date, end date, and repeated start date. These should be the same as
  1207. they were above.
  1208. .... USHORT Length of changed string value without NULL character. This is
  1209. given as count of WCHARs, so it should be identical to the value above.
  1210. .... WCHARxN Changed string value in Unicode (without NULL terminator)
  1211. Terminator ... ULONGxN Constant: { 0x00, 0x00, 0x00, 0x00}. 4 bytes of NULL
  1212. padding per changed exception. If there were no changed exceptions, all
  1213. you'll need is the final terminator below.
  1214. .... ULONGx2 Constant: { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}.
  1215. */
  1216. ?>