]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/EmailReminderPlugin.php
Add ability to send special one-time reminders
[quix0rs-gnu-social.git] / plugins / EmailReminder / EmailReminderPlugin.php
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
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Email reminder plugin
39  *
40  * @category  Plugin
41  * @package   StatusNet
42  * @author    Zach Copley <zach@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class EmailReminderPlugin extends Plugin
48 {
49     /**
50      * Set up email_reminder table
51      *
52      * @see Schema
53      * @see ColumnDef
54      *
55      * @return boolean hook value; true means continue processing, false means stop.
56      */
57     function onCheckSchema()
58     {
59         $schema = Schema::get();
60         $schema->ensureTable('email_reminder', Email_reminder::schemaDef());
61         return true;
62     }
63
64     /**
65      * Load related modules when needed
66      *
67      * @param string $cls Name of the class to be loaded
68      *
69      * @return boolean hook value; true means continue processing, false
70      *         means stop.
71      */
72     function onAutoload($cls) {
73         $base = dirname(__FILE__);
74         $lower = strtolower($cls);
75
76         $files = array("$base/classes/$cls.php",
77             "$base/lib/$lower.php");
78         if (substr($lower, -6) == 'action') {
79             $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
80         }
81         foreach ($files as $file) {
82             if (file_exists($file)) {
83                 include_once $file;
84                 return false;
85             }
86         }
87         return true;
88     }
89
90     /**
91      * Register our queue handlers
92      *
93      * @param QueueManager $qm Current queue manager
94      *
95      * @return boolean hook value
96      */
97     function onEndInitializeQueueManager($qm)
98     {
99         $qm->connect('siterem', 'SiteConfirmReminderHandler');
100         $qm->connect('uregrem', 'UserConfirmRegReminderHandler');
101         $qm->connect('uinvrem', 'UserInviteReminderHandler');
102
103         return true;
104     }
105
106     function onEndDocFileForTitle($title, $paths, &$filename)
107     {
108         if (empty($filename)) {
109             $filename = dirname(__FILE__) . '/mail-src/' . $title;
110             return false;
111         }
112
113         return true;
114     }
115
116     /**
117      * Send a reminder and record doing so
118      *
119      * @param string $type      type of reminder
120      * @param mixed  $object    Confirm_address or Invitation object
121      * @param string $subject   subjct of the email reminder
122      * @param int    $day       number of days
123      */
124     static function sendReminder($type, $object, $subject, $day)
125     {
126         // XXX: -1 is a for the special one-time reminder (maybe 30) would be
127         // better?  Like >= 30 days?
128         if ($day == -1) {
129             $title = "{$type}-onetime";
130         } else {
131             $title = "{$type}-{$day}";
132         }
133
134         // Record the fact that we sent a reminder
135         if (self::sendReminderEmail($type, $object, $subject, $title)) {
136             try {
137                 Email_reminder::recordReminder($type, $object, $day);
138             } catch (Exception $e) {
139                 // oh noez
140                 common_log(LOG_ERR, $e->getMessage(), __FILE__);
141             }
142         }
143
144         return true;
145     }
146
147     /**
148      * Send a real live email reminder
149      *
150      * @todo This would probably be better as two or more sep functions
151      *
152      * @param string $type      type of reminder
153      * @param mixed  $object    Confirm_address or Invitation object
154      * @param string $subject   subjct of the email reminder
155      * @param string $title     title of the email reminder
156      * @return boolean true if the email subsystem doesn't explode
157      */
158     static function sendReminderEmail($type, $object, $subject, $title = null) {
159
160         $sitename   = common_config('site', 'name');
161         $recipients = array($object->address);
162         $inviter    = null;
163         $inviterurl = null;
164
165         if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
166             $user = User::staticGet($object->user_id);
167             if (!empty($user)) {
168                 $profile    = $user->getProfile();
169                 $inviter    = $profile->getBestName();
170                 $inviterUrl = $profile->profileurl;
171             }
172         }
173
174         $headers['From'] = mail_notify_from();
175         $headers['To']   = trim($object->address);
176         // TRANS: Subject for confirmation e-mail.
177         // TRANS: %s is the StatusNet sitename.
178         $headers['Subject']      = $subject;
179         $headers['Content-Type'] = 'text/html; charset=UTF-8';
180
181         $confirmUrl = common_local_url('register', array('code' => $object->code));
182
183         $template = DocFile::forTitle($title, DocFile::mailPaths());
184
185         $blankfillers = array('confirmurl' => $confirmUrl);
186
187         if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
188             $blankfillers['inviter'] = $inviter;
189             $blankfillers['inviterurl'] = $inviterUrl;
190             // @todo private invitation message?
191         }
192
193         $body = $template->toHTML($blankfillers);
194
195         return mail_send($recipients, $headers, $body);
196     }
197
198     /**
199      *
200      * @param type $versions
201      * @return type
202      */
203     function onPluginVersion(&$versions)
204     {
205         $versions[] = array(
206             'name'           => 'EmailReminder',
207             'version'        => STATUSNET_VERSION,
208             'author'         => 'Zach Copley',
209             'homepage'       => 'http://status.net/wiki/Plugin:EmailReminder',
210             'rawdescription' => _m('Send email reminders for various things.')
211         );
212         return true;
213     }
214
215 }