]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/EmailReminderPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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      * Register our queue handlers
66      *
67      * @param QueueManager $qm Current queue manager
68      *
69      * @return boolean hook value
70      */
71     function onEndInitializeQueueManager(QueueManager $qm)
72     {
73         $qm->connect('siterem', 'SiteConfirmReminderHandler');
74         $qm->connect('uregrem', 'UserConfirmRegReminderHandler');
75         $qm->connect('uinvrem', 'UserInviteReminderHandler');
76
77         return true;
78     }
79
80     function onEndDocFileForTitle($title, array $paths, &$filename)
81     {
82         if (empty($filename)) {
83             $filename = dirname(__FILE__) . '/mail-src/' . $title;
84             return false;
85         }
86
87         return true;
88     }
89
90     /**
91      * Send a reminder and record doing so
92      *
93      * @param string $type      type of reminder
94      * @param mixed  $object    Confirm_address or Invitation object
95      * @param string $subject   subjct of the email reminder
96      * @param int    $day       number of days
97      */
98     static function sendReminder($type, $object, $subject, $day)
99     {
100         // XXX: -1 is a for the special one-time reminder (maybe 30) would be
101         // better?  Like >= 30 days?
102         if ($day == -1) {
103             $title = "{$type}-onetime";
104         } else {
105             $title = "{$type}-{$day}";
106         }
107
108         // Record the fact that we sent a reminder
109         if (self::sendReminderEmail($type, $object, $subject, $title)) {
110             try {
111                 Email_reminder::recordReminder($type, $object, $day);
112                 common_log(
113                     LOG_INFO,
114                     "Sent {$type} reminder to {$object->address}.",
115                     __FILE__
116                 );
117             } catch (Exception $e) {
118                 // oh noez
119                 common_log(LOG_ERR, $e->getMessage(), __FILE__);
120             }
121         }
122
123         return true;
124     }
125
126     /**
127      * Send a real live email reminder
128      *
129      * @todo This would probably be better as two or more sep functions
130      * @todo Add language support?
131      *
132      * @param string $type      type of reminder
133      * @param mixed  $object    Confirm_address or Invitation object
134      * @param string $subject   subjct of the email reminder
135      * @param string $title     title of the email reminder
136      * @return boolean true if the email subsystem doesn't explode
137      */
138     static function sendReminderEmail($type, $object, $subject, $title = null) {
139
140         $sitename   = common_config('site', 'name');
141         $recipients = array($object->address);
142         $inviter    = null;
143         $inviterurl = null;
144
145         if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
146             $user = User::getKV($object->user_id);
147             if (!empty($user)) {
148                 $profile    = $user->getProfile();
149                 $inviter    = $profile->getBestName();
150                 $inviterUrl = $profile->profileurl;
151             }
152         }
153
154         $headers['From'] = mail_notify_from();
155         $headers['To']   = trim($object->address);
156         // TRANS: Subject for confirmation e-mail.
157         // TRANS: %s is the StatusNet sitename.
158         $headers['Subject']      = $subject;
159         $headers['Content-Type'] = 'text/html; charset=UTF-8';
160
161         $confirmUrl = common_local_url('register', array('code' => $object->code));
162
163         $template = DocFile::forTitle($title, DocFile::mailPaths());
164
165         $blankfillers = array('confirmurl' => $confirmUrl);
166
167         if ($type == UserInviteReminderHandler::INVITE_REMINDER) {
168             $blankfillers['inviter'] = $inviter;
169             $blankfillers['inviterurl'] = $inviterUrl;
170             // @todo private invitation message?
171         }
172
173         $body = $template->toHTML($blankfillers);
174
175         return mail_send($recipients, $headers, $body);
176     }
177
178     /**
179      *
180      * @param type $versions
181      * @return type
182      */
183     function onPluginVersion(array &$versions)
184     {
185         $versions[] = array(
186             'name'           => 'EmailReminder',
187             'version'        => GNUSOCIAL_VERSION,
188             'author'         => 'Zach Copley',
189             'homepage'       => 'http://status.net/wiki/Plugin:EmailReminder',
190             // TRANS: Plugin description.
191             'rawdescription' => _m('Send email reminders for various things.')
192         );
193         return true;
194     }
195
196 }