]> git.wh0rd.org Git - tt-rss.git/blob - plugins/mail/init.php
mail plugin: cleanup disabled completion, add configurable destination email list
[tt-rss.git] / plugins / mail / init.php
1 <?php
2 class Mail extends Plugin {
3
4         private $host;
5
6         function about() {
7                 return array(1.0,
8                         "Share article via email",
9                         "fox");
10         }
11
12         function init($host) {
13                 $this->host = $host;
14
15                 $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
16                 $host->add_hook($host::HOOK_PREFS_TAB, $this);
17         }
18
19         function get_js() {
20                 return file_get_contents(dirname(__FILE__) . "/mail.js");
21         }
22
23         function save() {
24                 $addresslist = db_escape_string($_POST["addresslist"]);
25
26                 $this->host->set($this, "addresslist", $addresslist);
27
28                 echo __("Mail addresses saved.");
29         }
30
31         function hook_prefs_tab($args) {
32                 if ($args != "prefPrefs") return;
33
34                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mail plugin')."\">";
35
36                 print "<p>" . __("You can set predefined email addressed here (comma-separated list):") . "</p>";
37
38                 print "<form dojoType=\"dijit.form.Form\">";
39
40                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
41                         evt.preventDefault();
42                         if (this.validate()) {
43                                 console.log(dojo.objectToQuery(this.getValues()));
44                                 new Ajax.Request('backend.php', {
45                                         parameters: dojo.objectToQuery(this.getValues()),
46                                         onComplete: function(transport) {
47                                                 notify_info(transport.responseText);
48                                         }
49                                 });
50                                 //this.reset();
51                         }
52                         </script>";
53
54                         print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
55                         print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
56                         print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"mail\">";
57
58                         $addresslist = $this->host->get($this, "addresslist");
59
60                         print "<textarea dojoType=\"dijit.form.SimpleTextarea\" style='font-size : 12px; width : 50%' rows=\"3\"
61                                 name='addresslist'>$addresslist</textarea>";
62
63                         print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
64                                 __("Save")."</button>";
65
66                         print "</form>";
67
68                 print "</div>";
69         }
70
71         function hook_article_button($line) {
72                 return "<img src=\"plugins/mail/mail.png\"
73                                         class='tagsPic' style=\"cursor : pointer\"
74                                         onclick=\"emailArticle(".$line["id"].")\"
75                                         alt='Zoom' title='".__('Forward by email')."'>";
76         }
77
78         function emailArticle() {
79
80                 $param = db_escape_string($_REQUEST['param']);
81
82                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
83                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"mail\">";
84                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"sendEmail\">";
85
86                 $result = db_query("SELECT email, full_name FROM ttrss_users WHERE
87                         id = " . $_SESSION["uid"]);
88
89                 $user_email = htmlspecialchars(db_fetch_result($result, 0, "email"));
90                 $user_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
91
92                 if (!$user_name) $user_name = $_SESSION['name'];
93
94                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"from_email\" value=\"$user_email\">";
95                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"from_name\" value=\"$user_name\">";
96
97                 require_once "lib/MiniTemplator.class.php";
98
99                 $tpl = new MiniTemplator;
100
101                 $tpl->readTemplateFromFile("templates/email_article_template.txt");
102
103                 $tpl->setVariable('USER_NAME', $_SESSION["name"], true);
104                 $tpl->setVariable('USER_EMAIL', $user_email, true);
105                 $tpl->setVariable('TTRSS_HOST', $_SERVER["HTTP_HOST"], true);
106
107                 $result = db_query("SELECT DISTINCT link, content, title, note
108                         FROM ttrss_user_entries, ttrss_entries WHERE id = ref_id AND
109                         id IN ($param) AND owner_uid = " . $_SESSION["uid"]);
110
111                 if (db_num_rows($result) > 1) {
112                         $subject = __("[Forwarded]") . " " . __("Multiple articles");
113                 }
114
115                 while ($line = db_fetch_assoc($result)) {
116
117                         if (!$subject)
118                                 $subject = __("[Forwarded]") . " " . htmlspecialchars($line["title"]);
119
120                         $tpl->setVariable('ARTICLE_TITLE', strip_tags($line["title"]));
121                         $tnote = strip_tags($line["note"]);
122                         if( $tnote != ''){
123                                 $tpl->setVariable('ARTICLE_NOTE', $tnote, true);
124                                 $tpl->addBlock('note');
125                         }
126                         $tpl->setVariable('ARTICLE_URL', strip_tags($line["link"]));
127
128                         $tpl->addBlock('article');
129                 }
130
131                 $tpl->addBlock('email');
132
133                 $content = "";
134                 $tpl->generateOutputToString($content);
135
136                 print "<table width='100%'><tr><td>";
137
138                 print __('From:');
139
140                 print "</td><td>";
141
142                 print "<input dojoType=\"dijit.form.TextBox\" disabled=\"1\" style=\"width : 30em;\"
143                                 value=\"$user_name <$user_email>\">";
144
145                 print "</td></tr><tr><td>";
146
147                 $addresslist = explode(",", $this->host->get($this, "addresslist"));
148
149                 print __('To:');
150
151                 print "</td><td>";
152
153 /*              print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
154                                 style=\"width : 30em;\"
155                                 name=\"destination\" id=\"emailArticleDlg_destination\">"; */
156
157                 print_select("destination", "", $addresslist, 'dojoType="dijit.form.FilteringSelect"');
158
159 /*              print "<div class=\"autocomplete\" id=\"emailArticleDlg_dst_choices\"
160         style=\"z-index: 30; display : none\"></div>"; */
161
162                 print "</td></tr><tr><td>";
163
164                 print __('Subject:');
165
166                 print "</td><td>";
167
168                 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
169                                 style=\"width : 30em;\"
170                                 name=\"subject\" value=\"$subject\" id=\"subject\">";
171
172                 print "</td></tr>";
173
174                 print "<tr><td colspan='2'><textarea dojoType=\"dijit.form.SimpleTextarea\" style='font-size : 12px; width : 100%' rows=\"20\"
175                         name='content'>$content</textarea>";
176
177                 print "</td></tr></table>";
178
179                 print "<div class='dlgButtons'>";
180                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').execute()\">".__('Send e-mail')."</button> ";
181                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').hide()\">".__('Cancel')."</button>";
182                 print "</div>";
183
184                 //return;
185         }
186
187         function sendEmail() {
188                 require_once 'classes/ttrssmailer.php';
189
190                 $reply = array();
191
192                 $mail = new ttrssMailer();
193
194                 $mail->From = strip_tags($_REQUEST['from_email']);
195                 $mail->FromName = strip_tags($_REQUEST['from_name']);
196                 //$mail->AddAddress($_REQUEST['destination']);
197                 $addresses = explode(';', $_REQUEST['destination']);
198                 foreach($addresses as $nextaddr)
199                         $mail->AddAddress($nextaddr);
200
201                 $mail->IsHTML(false);
202                 $mail->Subject = $_REQUEST['subject'];
203                 $mail->Body = $_REQUEST['content'];
204
205                 $rc = $mail->Send();
206
207                 if (!$rc) {
208                         $reply['error'] =  $mail->ErrorInfo;
209                 } else {
210                         //save_email_address(db_escape_string($destination));
211                         $reply['message'] = "UPDATE_COUNTERS";
212                 }
213
214                 print json_encode($reply);
215         }
216
217         /* function completeEmails() {
218                 $search = db_escape_string($_REQUEST["search"]);
219
220                 print "<ul>";
221
222                 foreach ($_SESSION['stored_emails'] as $email) {
223                         if (strpos($email, $search) !== false) {
224                                 print "<li>$email</li>";
225                         }
226                 }
227
228                 print "</ul>";
229         } */
230
231         function api_version() {
232                 return 2;
233         }
234
235 }
236 ?>