/carl_util/event_invite/EventInvite.php

https://github.com/luthercollege/reason_package · PHP · 254 lines · 150 code · 44 blank · 60 comment · 16 complexity · b56c38d680a3aa17d4243091dec1cf29 MD5 · raw file

  1. <?php
  2. /** Event Invite email class
  3. *
  4. * @package carl_util
  5. * @subpackage event_invite
  6. */
  7. /**
  8. * Allows you to send email to Zimbra (or elsewhere) with an attached ical event in such a way that the event will be offered
  9. * to the recipient for inclusion in their personal calendar.
  10. *
  11. * Usage:
  12. *
  13. * $invite = new EventInvite($recipient, $sender, $subject, $message, $ical_data, $debug_flag)
  14. * $invite->send(True); // True = send debug information to error_log
  15. *
  16. * $invite->setRecipient($new_recipient);
  17. * $invite->send(); // send a copy to someone else
  18. *
  19. * Functions:
  20. *
  21. * // Constructor
  22. * EventInvite ( $recipient = null , $sender = "wsg@carleton.edu" , $subject = "Incoming Event", $message = null , $ical_data = null )
  23. *
  24. * // send the message
  25. * send ( $debug = FALSE )
  26. *
  27. * // get/set the recipient
  28. * getRecipient ( )
  29. * setRecipient ( $new_recipient )
  30. *
  31. * // get/set the sender
  32. * getSender ( )
  33. * setSender ( $new_sender )
  34. *
  35. * // get/set the message subject
  36. * getSubject ( )
  37. * setSubject ( $new_subject )
  38. *
  39. * // get/set the message body
  40. * getMessage ( )
  41. * setMessage ( $new_message )
  42. *
  43. * // get/set the ical data -- see EventInviteExample.php for the appropriate set of ical fields
  44. * getICalData ( )
  45. * setICalData ( $new_ical_data )
  46. *
  47. * // used internally for building the message headers
  48. * mainHeader ( )
  49. * messageHeader ( )
  50. * icalHeader ( )
  51. * getBoundary ( )
  52. * setBoundary ( )
  53. * boundaryKey ($length=10)
  54. *
  55. * @author Matt Bockol
  56. * @version .001
  57. * @date 10.3.2007
  58. */
  59. class EventInvite
  60. {
  61. var $recipient = null ;
  62. var $sender = null ;
  63. var $subject = null ;
  64. var $ical_data = null ;
  65. var $message = null ;
  66. var $boundary = null ;
  67. var $debug = FALSE ;
  68. function EventInvite ( $recipient = null , $sender = "wsg@carleton.edu" , $subject = "Incoming Event", $message = null , $ical_data = null )
  69. {
  70. $this->recipient = $recipient ;
  71. $this->sender = $sender ;
  72. $this->subject = $subject ;
  73. $this->message = $message ;
  74. $this->ical_data = $ical_data ;
  75. $this->debug = $debug ;
  76. }
  77. function send ( $debug = FALSE )
  78. {
  79. if(is_null($this->recipient)){
  80. if($this->debug){ error_log("EventInvite: recipient null."); }
  81. return FALSE ;
  82. }
  83. if(is_null($this->sender)){
  84. if($this->debug){ error_log("EventInvite: sender null."); }
  85. return FALSE ;
  86. }
  87. if(is_null($this->ical_data)){
  88. if($this->debug){ error_log("EventInvite: ical_data null."); }
  89. return FALSE ;
  90. }
  91. if(is_null($this->message)){
  92. if($this->debug){ error_log("EventInvite: message null."); }
  93. }
  94. $fullheaders = $this->mainHeader() . $this->messageHeader() . $this->icalHeader() . "--" . $this->getBoundary() . "--\n";
  95. mail($this->recipient, $this->subject, "" , $fullheaders);
  96. }
  97. function getRecipient ( )
  98. {
  99. return $this->recipient ;
  100. }
  101. function setRecipient ( $new_recipient )
  102. {
  103. if(isset($new_recipient))
  104. {
  105. $this->recipient = $new_recipient ;
  106. }
  107. return $this->recipient ;
  108. }
  109. function getSender ( )
  110. {
  111. return $this->sender ;
  112. }
  113. function setSender ( $new_sender )
  114. {
  115. if(isset($new_sender))
  116. {
  117. $this->sender = $new_sender ;
  118. }
  119. return $this->sender ;
  120. }
  121. function getSubject ( )
  122. {
  123. return $this->subject ;
  124. }
  125. function setSubject ( $new_subject )
  126. {
  127. if(isset($new_subject))
  128. {
  129. $this->subject = $new_subject ;
  130. }
  131. return $this->subject ;
  132. }
  133. function getMessage ( )
  134. {
  135. return $this->message ;
  136. }
  137. function setMessage ( $new_message )
  138. {
  139. if(isset($new_message))
  140. {
  141. $this->message = $new_message ;
  142. }
  143. return $this->message ;
  144. }
  145. function getICalData ( )
  146. {
  147. return $this->ical_data ;
  148. }
  149. function setICalData ( $new_ical_data )
  150. {
  151. if(isset($new_ical_data))
  152. {
  153. $this->recipient = $new_ical_data ;
  154. }
  155. return $this->ical_data ;
  156. }
  157. function mainHeader ( )
  158. {
  159. $main_header = "";
  160. $main_header .= "From: " . $this->sender . "\n";
  161. $main_header .= "MIME-Version: 1.0\n";
  162. $main_header .= "Content-Type: multipart/alternative;\n boundary=\"" . $this->getBoundary() . "\"\n";
  163. $main_header .= "X-Originating-IP: [137.22.1.42]\n\n";
  164. return $main_header ;
  165. }
  166. function messageHeader ( )
  167. {
  168. // with no message, we return an empty header
  169. if(!isset( $this->message )){ return ; }
  170. $message_header = "";
  171. $message_header .= "--" . $this->getBoundary() . "\n";
  172. $message_header .= "Content-Type: text/plain; charset=utf-8\n";
  173. $message_header .= "Content-Transfer-Encoding: 7bit\n\n";
  174. $message_header .= $this->message . "\n";
  175. return $message_header ;
  176. }
  177. function icalHeader ( )
  178. {
  179. // with no ical data, we return an empty header
  180. if(!isset( $this->ical_data )){ return ; }
  181. $ical_header = "";
  182. $ical_header .= "--" . $this->getBoundary() . "\n";
  183. $ical_header .= "Content-Type: text/calendar; charset=utf-8; method=REQUEST; name=meeting.ics\n";
  184. $ical_header .= "Content-Transfer-Encoding: 7bit\n\n";
  185. $ical_header .= $this->ical_data ;
  186. return $ical_header ;
  187. }
  188. function getBoundary ( )
  189. {
  190. if(!isset($this->boundary)){ $this->setBoundary(); }
  191. return $this->boundary ;
  192. }
  193. function setBoundary ( )
  194. {
  195. $this->boundary = "----=_Part_" . $this->boundaryKey(7) . "_" . $this->boundaryKey(10) . "." . $this->boundaryKey(13) ;
  196. return $this->boundary ;
  197. }
  198. // generate a random boundary key
  199. function boundaryKey ($length=10)
  200. {
  201. $key = "";
  202. while($length > 0)
  203. {
  204. $key .= rand(0,9);
  205. $length-- ;
  206. }
  207. return $key ;
  208. }
  209. }
  210. ?>