EmailReminderPlugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Plugin for sending email reminders about various things
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category OnDemand
  24. * @package StatusNet
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Email reminder plugin
  37. *
  38. * @category Plugin
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class EmailReminderPlugin extends Plugin
  46. {
  47. /**
  48. * Set up email_reminder table
  49. *
  50. * @see Schema
  51. * @see ColumnDef
  52. *
  53. * @return boolean hook value; true means continue processing, false means stop.
  54. */
  55. function onCheckSchema()
  56. {
  57. $schema = Schema::get();
  58. $schema->ensureTable('email_reminder', Email_reminder::schemaDef());
  59. return true;
  60. }
  61. /**
  62. * Register our queue handlers
  63. *
  64. * @param QueueManager $qm Current queue manager
  65. *
  66. * @return boolean hook value
  67. */
  68. function onEndInitializeQueueManager($qm)
  69. {
  70. $qm->connect('siterem', 'SiteConfirmReminderHandler');
  71. $qm->connect('uregrem', 'UserConfirmRegReminderHandler');
  72. $qm->connect('uinvrem', 'UserInviteReminderHandler');
  73. return true;
  74. }
  75. function onEndDocFileForTitle($title, $paths, &$filename)
  76. {
  77. if (empty($filename)) {
  78. $filename = dirname(__FILE__) . '/mail-src/' . $title;
  79. return false;
  80. }
  81. return true;
  82. }
  83. /**
  84. * Send a reminder and record doing so
  85. *
  86. * @param string $type type of reminder
  87. * @param mixed $object Confirm_address or Invitation object
  88. * @param string $subject subjct of the email reminder
  89. * @param int $day number of days
  90. */
  91. static function sendReminder($type, $object, $subject, $day)
  92. {
  93. // XXX: -1 is a for the special one-time reminder (maybe 30) would be
  94. // better? Like >= 30 days?
  95. if ($day == -1) {
  96. $title = "{$type}-onetime";
  97. } else {
  98. $title = "{$type}-{$day}";
  99. }
  100. // Record the fact that we sent a reminder
  101. if (self::sendReminderEmail($type, $object, $subject, $title)) {
  102. try {
  103. Email_reminder::recordReminder($type, $object, $day);
  104. common_log(
  105. LOG_INFO,
  106. "Sent {$type} reminder to {$object->address}.",
  107. __FILE__
  108. );
  109. } catch (Exception $e) {
  110. // oh noez
  111. common_log(LOG_ERR, $e->getMessage(), __FILE__);
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * Send a real live email reminder
  118. *
  119. * @todo This would probably be better as two or more sep functions
  120. *
  121. * @param string $type type of reminder
  122. * @param mixed $object Confirm_address or Invitation object
  123. * @param string $subject subjct of the email reminder
  124. * @param string $title title of the email reminder
  125. * @return boolean true if the email subsystem doesn't explode
  126. */
  127. static function sendReminderEmail($type, $object, $subject, $title = null) {
  128. $sitename = common_config('site', 'name');
  129. $recipients = array($object->address);
  130. $inviter = null;
  131. $inviterurl = null;
  132. if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
  133. $user = User::getKV($object->user_id);
  134. if (!empty($user)) {
  135. $profile = $user->getProfile();
  136. $inviter = $profile->getBestName();
  137. $inviterUrl = $profile->profileurl;
  138. }
  139. }
  140. $headers['From'] = mail_notify_from();
  141. $headers['To'] = trim($object->address);
  142. // TRANS: Subject for confirmation e-mail.
  143. // TRANS: %s is the StatusNet sitename.
  144. $headers['Subject'] = $subject;
  145. $headers['Content-Type'] = 'text/html; charset=UTF-8';
  146. $confirmUrl = common_local_url('register', array('code' => $object->code));
  147. $template = DocFile::forTitle($title, DocFile::mailPaths());
  148. $blankfillers = array('confirmurl' => $confirmUrl);
  149. if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
  150. $blankfillers['inviter'] = $inviter;
  151. $blankfillers['inviterurl'] = $inviterUrl;
  152. // @todo private invitation message?
  153. }
  154. $body = $template->toHTML($blankfillers);
  155. return mail_send($recipients, $headers, $body);
  156. }
  157. /**
  158. *
  159. * @param type $versions
  160. * @return type
  161. */
  162. function onPluginVersion(&$versions)
  163. {
  164. $versions[] = array(
  165. 'name' => 'EmailReminder',
  166. 'version' => GNUSOCIAL_VERSION,
  167. 'author' => 'Zach Copley',
  168. 'homepage' => 'http://status.net/wiki/Plugin:EmailReminder',
  169. // TRANS: Plugin description.
  170. 'rawdescription' => _m('Send email reminders for various things.')
  171. );
  172. return true;
  173. }
  174. }