]> git.wh0rd.org Git - tt-rss.git/blob - plugins/mail/init.php
Merge pull request #68 from gvmelle/master
[tt-rss.git] / plugins / mail / init.php
1 <?php
2 class Mail extends Plugin {
3
4         private $link;
5         private $host;
6
7         function about() {
8                 return array(1.0,
9                         "Share article via email",
10                         "fox");
11         }
12
13         function init($host) {
14                 $this->link = $host->get_link();
15                 $this->host = $host;
16
17                 $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
18         }
19
20         function get_js() {
21                 return file_get_contents(dirname(__FILE__) . "/mail.js");
22         }
23
24         function hook_article_button($line) {
25                 return "<img src=\"plugins/mail/mail.png\"
26                                         class='tagsPic' style=\"cursor : pointer\"
27                                         onclick=\"emailArticle(".$line["id"].")\"
28                                         alt='Zoom' title='".__('Forward by email')."'>";
29         }
30
31         function emailArticle() {
32
33                 $param = db_escape_string($this->link, $_REQUEST['param']);
34
35                 $secretkey = sha1(uniqid(rand(), true));
36
37                 $_SESSION['email_secretkey'] = $secretkey;
38
39                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"secretkey\" value=\"$secretkey\">";
40                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
41                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"mail\">";
42                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"sendEmail\">";
43
44                 $result = db_query($this->link, "SELECT email, full_name FROM ttrss_users WHERE
45                         id = " . $_SESSION["uid"]);
46
47                 $user_email = htmlspecialchars(db_fetch_result($result, 0, "email"));
48                 $user_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
49
50                 if (!$user_name) $user_name = $_SESSION['name'];
51
52                 $_SESSION['email_replyto'] = $user_email;
53                 $_SESSION['email_fromname'] = $user_name;
54
55                 require_once "lib/MiniTemplator.class.php";
56
57                 $tpl = new MiniTemplator;
58                 $tpl_t = new MiniTemplator;
59
60                 $tpl->readTemplateFromFile("templates/email_article_template.txt");
61
62                 $tpl->setVariable('USER_NAME', $_SESSION["name"], true);
63                 $tpl->setVariable('USER_EMAIL', $user_email, true);
64                 $tpl->setVariable('TTRSS_HOST', $_SERVER["HTTP_HOST"], true);
65
66                 $result = db_query($this->link, "SELECT link, content, title
67                         FROM ttrss_user_entries, ttrss_entries WHERE id = ref_id AND
68                         id IN ($param) AND owner_uid = " . $_SESSION["uid"]);
69
70                 if (db_num_rows($result) > 1) {
71                         $subject = __("[Forwarded]") . " " . __("Multiple articles");
72                 }
73
74                 while ($line = db_fetch_assoc($result)) {
75
76                         if (!$subject)
77                                 $subject = __("[Forwarded]") . " " . htmlspecialchars($line["title"]);
78
79                         $tpl->setVariable('ARTICLE_TITLE', strip_tags($line["title"]));
80                         $tpl->setVariable('ARTICLE_URL', strip_tags($line["link"]));
81
82                         $tpl->addBlock('article');
83                 }
84
85                 $tpl->addBlock('email');
86
87                 $content = "";
88                 $tpl->generateOutputToString($content);
89
90                 print "<table width='100%'><tr><td>";
91
92                 print __('From:');
93
94                 print "</td><td>";
95
96                 print "<input dojoType=\"dijit.form.TextBox\" disabled=\"1\" style=\"width : 30em;\"
97                                 value=\"$user_name <$user_email>\">";
98
99                 print "</td></tr><tr><td>";
100
101                 print __('To:');
102
103                 print "</td><td>";
104
105                 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
106                                 style=\"width : 30em;\"
107                                 name=\"destination\" id=\"emailArticleDlg_destination\">";
108
109                 print "<div class=\"autocomplete\" id=\"emailArticleDlg_dst_choices\"
110                                 style=\"z-index: 30; display : none\"></div>";
111
112                 print "</td></tr><tr><td>";
113
114                 print __('Subject:');
115
116                 print "</td><td>";
117
118                 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
119                                 style=\"width : 30em;\"
120                                 name=\"subject\" value=\"$subject\" id=\"subject\">";
121
122                 print "</td></tr>";
123
124                 print "<tr><td colspan='2'><textarea dojoType=\"dijit.form.SimpleTextarea\" style='font-size : 12px; width : 100%' rows=\"20\"
125                         name='content'>$content</textarea>";
126
127                 print "</td></tr></table>";
128
129                 print "<div class='dlgButtons'>";
130                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').execute()\">".__('Send e-mail')."</button> ";
131                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').hide()\">".__('Cancel')."</button>";
132                 print "</div>";
133
134                 //return;
135         }
136
137         function sendEmail() {
138                 $secretkey = $_REQUEST['secretkey'];
139
140                 require_once 'lib/phpmailer/class.phpmailer.php';
141
142                 $reply = array();
143
144                 if ($_SESSION['email_secretkey'] &&
145                 $secretkey == $_SESSION['email_secretkey']) {
146
147                         $_SESSION['email_secretkey'] = '';
148
149                         $destination = $_REQUEST['destination'];
150                         $subject = $_REQUEST['subject'];
151                         $content = $_REQUEST['content'];
152
153                         $replyto = strip_tags($_SESSION['email_replyto']);
154                         $fromname = strip_tags($_SESSION['email_fromname']);
155
156                         $mail = new PHPMailer();
157
158                         $mail->PluginDir = "lib/phpmailer/";
159                         $mail->SetLanguage("en", "lib/phpmailer/language/");
160
161                         $mail->CharSet = "UTF-8";
162
163                         $mail->From = $replyto;
164                         $mail->FromName = $fromname;
165                         $mail->AddAddress($destination);
166
167                         if (SMTP_HOST) {
168                                 $mail->Host = SMTP_HOST;
169                                 $mail->Mailer = "smtp";
170                                 $mail->SMTPAuth = SMTP_LOGIN != '';
171                                 $mail->Username = SMTP_LOGIN;
172                                 $mail->Password = SMTP_PASSWORD;
173                         }
174
175                         $mail->IsHTML(false);
176                         $mail->Subject = $subject;
177                         $mail->Body = $content;
178
179                         $rc = $mail->Send();
180
181                         if (!$rc) {
182                                 $reply['error'] =  $mail->ErrorInfo;
183                         } else {
184                                 save_email_address($this->link, db_escape_string($this->link, $destination));
185                                 $reply['message'] = "UPDATE_COUNTERS";
186                         }
187
188                 } else {
189                         $reply['error'] = "Not authorized.";
190                 }
191
192                 print json_encode($reply);
193         }
194
195         function completeEmails() {
196                 $search = db_escape_string($this->link, $_REQUEST["search"]);
197
198                 print "<ul>";
199
200                 foreach ($_SESSION['stored_emails'] as $email) {
201                         if (strpos($email, $search) !== false) {
202                                 print "<li>$email</li>";
203                         }
204                 }
205
206                 print "</ul>";
207         }
208
209
210 }
211 ?>