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