init.php 5.5 KB

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