EmailReminderPlugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. const PLUGIN_VERSION = '2.0.0';
  48. /**
  49. * Set up email_reminder table
  50. *
  51. * @see Schema
  52. * @see ColumnDef
  53. *
  54. * @return boolean hook value; true means continue processing, false means stop.
  55. */
  56. function onCheckSchema()
  57. {
  58. $schema = Schema::get();
  59. $schema->ensureTable('email_reminder', Email_reminder::schemaDef());
  60. return true;
  61. }
  62. /**
  63. * Register our queue handlers
  64. *
  65. * @param QueueManager $qm Current queue manager
  66. *
  67. * @return boolean hook value
  68. */
  69. function onEndInitializeQueueManager($qm)
  70. {
  71. $qm->connect('siterem', 'SiteConfirmReminderHandler');
  72. $qm->connect('uregrem', 'UserConfirmRegReminderHandler');
  73. $qm->connect('uinvrem', 'UserInviteReminderHandler');
  74. return true;
  75. }
  76. function onEndDocFileForTitle($title, $paths, &$filename)
  77. {
  78. if (empty($filename)) {
  79. $filename = dirname(__FILE__) . '/mail-src/' . $title;
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Send a reminder and record doing so
  86. *
  87. * @param string $type type of reminder
  88. * @param mixed $object Confirm_address or Invitation object
  89. * @param string $subject subjct of the email reminder
  90. * @param int $day number of days
  91. */
  92. static function sendReminder($type, $object, $subject, $day)
  93. {
  94. // XXX: -1 is a for the special one-time reminder (maybe 30) would be
  95. // better? Like >= 30 days?
  96. if ($day == -1) {
  97. $title = "{$type}-onetime";
  98. } else {
  99. $title = "{$type}-{$day}";
  100. }
  101. // Record the fact that we sent a reminder
  102. if (self::sendReminderEmail($type, $object, $subject, $title)) {
  103. try {
  104. Email_reminder::recordReminder($type, $object, $day);
  105. common_log(
  106. LOG_INFO,
  107. "Sent {$type} reminder to {$object->address}.",
  108. __FILE__
  109. );
  110. } catch (Exception $e) {
  111. // oh noez
  112. common_log(LOG_ERR, $e->getMessage(), __FILE__);
  113. }
  114. }
  115. return true;
  116. }
  117. /**
  118. * Send a real live email reminder
  119. *
  120. * @todo This would probably be better as two or more sep functions
  121. *
  122. * @param string $type type of reminder
  123. * @param mixed $object Confirm_address or Invitation object
  124. * @param string $subject subjct of the email reminder
  125. * @param string $title title of the email reminder
  126. * @return boolean true if the email subsystem doesn't explode
  127. */
  128. static function sendReminderEmail($type, $object, $subject, $title = null) {
  129. $sitename = common_config('site', 'name');
  130. $recipients = array($object->address);
  131. $inviter = null;
  132. $inviterurl = null;
  133. if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
  134. $user = User::getKV($object->user_id);
  135. if (!empty($user)) {
  136. $profile = $user->getProfile();
  137. $inviter = $profile->getBestName();
  138. $inviterUrl = $profile->profileurl;
  139. }
  140. }
  141. $headers['From'] = mail_notify_from();
  142. $headers['To'] = trim($object->address);
  143. // TRANS: Subject for confirmation e-mail.
  144. // TRANS: %s is the StatusNet sitename.
  145. $headers['Subject'] = $subject;
  146. $headers['Content-Type'] = 'text/html; charset=UTF-8';
  147. $confirmUrl = common_local_url('register', array('code' => $object->code));
  148. $template = DocFile::forTitle($title, DocFile::mailPaths());
  149. $blankfillers = array('confirmurl' => $confirmUrl);
  150. if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
  151. $blankfillers['inviter'] = $inviter;
  152. $blankfillers['inviterurl'] = $inviterUrl;
  153. // @todo private invitation message?
  154. }
  155. $body = $template->toHTML($blankfillers);
  156. return mail_send($recipients, $headers, $body);
  157. }
  158. /**
  159. *
  160. * @param type $versions
  161. * @return type
  162. */
  163. public function onPluginVersion(array &$versions): bool
  164. {
  165. $versions[] = array(
  166. 'name' => 'EmailReminder',
  167. 'version' => self::PLUGIN_VERSION,
  168. 'author' => 'Zach Copley',
  169. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/EmailReminder',
  170. // TRANS: Plugin description.
  171. 'rawdescription' => _m('Send email reminders for various things.')
  172. );
  173. return true;
  174. }
  175. }