]> git.wh0rd.org Git - tt-rss.git/blob - plugins/mail/init.php
Merge branch 'master' of github.com:gothfox/Tiny-Tiny-RSS
[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                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
36                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"mail\">";
37                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"sendEmail\">";
38
39                 $result = db_query($this->link, "SELECT email, full_name FROM ttrss_users WHERE
40                         id = " . $_SESSION["uid"]);
41
42                 $user_email = htmlspecialchars(db_fetch_result($result, 0, "email"));
43                 $user_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
44
45                 if (!$user_name) $user_name = $_SESSION['name'];
46
47                 $_SESSION['email_replyto'] = $user_email;
48                 $_SESSION['email_fromname'] = $user_name;
49
50                 require_once "lib/MiniTemplator.class.php";
51
52                 $tpl = new MiniTemplator;
53                 $tpl_t = new MiniTemplator;
54
55                 $tpl->readTemplateFromFile("templates/email_article_template.txt");
56
57                 $tpl->setVariable('USER_NAME', $_SESSION["name"], true);
58                 $tpl->setVariable('USER_EMAIL', $user_email, true);
59                 $tpl->setVariable('TTRSS_HOST', $_SERVER["HTTP_HOST"], true);
60
61                 $result = db_query($this->link, "SELECT link, content, title
62                         FROM ttrss_user_entries, ttrss_entries WHERE id = ref_id AND
63                         id IN ($param) AND owner_uid = " . $_SESSION["uid"]);
64
65                 if (db_num_rows($result) > 1) {
66                         $subject = __("[Forwarded]") . " " . __("Multiple articles");
67                 }
68
69                 while ($line = db_fetch_assoc($result)) {
70
71                         if (!$subject)
72                                 $subject = __("[Forwarded]") . " " . htmlspecialchars($line["title"]);
73
74                         $tpl->setVariable('ARTICLE_TITLE', strip_tags($line["title"]));
75                         $tpl->setVariable('ARTICLE_URL', strip_tags($line["link"]));
76
77                         $tpl->addBlock('article');
78                 }
79
80                 $tpl->addBlock('email');
81
82                 $content = "";
83                 $tpl->generateOutputToString($content);
84
85                 print "<table width='100%'><tr><td>";
86
87                 print __('From:');
88
89                 print "</td><td>";
90
91                 print "<input dojoType=\"dijit.form.TextBox\" disabled=\"1\" style=\"width : 30em;\"
92                                 value=\"$user_name <$user_email>\">";
93
94                 print "</td></tr><tr><td>";
95
96                 print __('To:');
97
98                 print "</td><td>";
99
100                 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
101                                 style=\"width : 30em;\"
102                                 name=\"destination\" id=\"emailArticleDlg_destination\">";
103
104                 print "<div class=\"autocomplete\" id=\"emailArticleDlg_dst_choices\"
105                                 style=\"z-index: 30; display : none\"></div>";
106
107                 print "</td></tr><tr><td>";
108
109                 print __('Subject:');
110
111                 print "</td><td>";
112
113                 print "<input dojoType=\"dijit.form.ValidationTextBox\" required=\"true\"
114                                 style=\"width : 30em;\"
115                                 name=\"subject\" value=\"$subject\" id=\"subject\">";
116
117                 print "</td></tr>";
118
119                 print "<tr><td colspan='2'><textarea dojoType=\"dijit.form.SimpleTextarea\" style='font-size : 12px; width : 100%' rows=\"20\"
120                         name='content'>$content</textarea>";
121
122                 print "</td></tr></table>";
123
124                 print "<div class='dlgButtons'>";
125                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').execute()\">".__('Send e-mail')."</button> ";
126                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('emailArticleDlg').hide()\">".__('Cancel')."</button>";
127                 print "</div>";
128
129                 //return;
130         }
131
132         function sendEmail() {
133                 require_once 'classes/ttrssmailer.php';
134
135                 $reply = array();
136
137                 $_SESSION['email_secretkey'] = '';
138
139                 $replyto = strip_tags($_SESSION['email_replyto']);
140                 $fromname = strip_tags($_SESSION['email_fromname']);
141
142                 $mail = new ttrssMailer();
143
144                 $mail->From = $replyto;
145                 $mail->FromName = $fromname;
146                 $mail->AddAddress($_REQUEST['destination']);
147
148                 $mail->IsHTML(false);
149                 $mail->Subject = $_REQUEST['subject'];
150                 $mail->Body = $_REQUEST['content'];
151
152                 $rc = $mail->Send();
153
154                 if (!$rc) {
155                         $reply['error'] =  $mail->ErrorInfo;
156                 } else {
157                         save_email_address($this->link, db_escape_string($this->link, $destination));
158                         $reply['message'] = "UPDATE_COUNTERS";
159                 }
160
161                 print json_encode($reply);
162         }
163
164         function completeEmails() {
165                 $search = db_escape_string($this->link, $_REQUEST["search"]);
166
167                 print "<ul>";
168
169                 foreach ($_SESSION['stored_emails'] as $email) {
170                         if (strpos($email, $search) !== false) {
171                                 print "<li>$email</li>";
172                         }
173                 }
174
175                 print "</ul>";
176         }
177
178
179 }
180 ?>