digest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Send by mail a digest of last articles.
  4. *
  5. * @param mixed $link The database connection.
  6. * @param integer $limit The maximum number of articles by digest.
  7. * @return boolean Return false if digests are not enabled.
  8. */
  9. function send_headlines_digests($link, $debug = false) {
  10. require_once 'lib/phpmailer/class.phpmailer.php';
  11. $user_limit = 15; // amount of users to process (e.g. emails to send out)
  12. $limit = 1000; // maximum amount of headlines to include
  13. if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
  14. if (DB_TYPE == "pgsql") {
  15. $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
  16. } else if (DB_TYPE == "mysql") {
  17. $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
  18. }
  19. $result = db_query($link, "SELECT id,email FROM ttrss_users
  20. WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
  21. while ($line = db_fetch_assoc($result)) {
  22. if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
  23. $preferred_ts = strtotime(get_pref($link, 'DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
  24. // try to send digests within 2 hours of preferred time
  25. if ($preferred_ts && time() >= $preferred_ts &&
  26. time() - $preferred_ts <= 7200) {
  27. if ($debug) print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
  28. $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
  29. global $tz_offset;
  30. // reset tz_offset global to prevent tz cache clash between users
  31. $tz_offset = -1;
  32. $tuple = prepare_headlines_digest($link, $line["id"], 1, $limit);
  33. $digest = $tuple[0];
  34. $headlines_count = $tuple[1];
  35. $affected_ids = $tuple[2];
  36. $digest_text = $tuple[3];
  37. if ($headlines_count > 0) {
  38. $mail = new PHPMailer();
  39. $mail->PluginDir = "lib/phpmailer/";
  40. $mail->SetLanguage("en", "lib/phpmailer/language/");
  41. $mail->CharSet = "UTF-8";
  42. $mail->From = SMTP_FROM_ADDRESS;
  43. $mail->FromName = SMTP_FROM_NAME;
  44. $mail->AddAddress($line["email"], $line["login"]);
  45. if (SMTP_HOST) {
  46. $mail->Host = SMTP_HOST;
  47. $mail->Mailer = "smtp";
  48. $mail->SMTPAuth = SMTP_LOGIN != '';
  49. $mail->Username = SMTP_LOGIN;
  50. $mail->Password = SMTP_PASSWORD;
  51. }
  52. $mail->IsHTML(true);
  53. $mail->Subject = DIGEST_SUBJECT;
  54. $mail->Body = $digest;
  55. $mail->AltBody = $digest_text;
  56. $rc = $mail->Send();
  57. if (!$rc && $debug) print "ERROR: " . $mail->ErrorInfo;
  58. if ($debug) print "RC=$rc\n";
  59. if ($rc && $do_catchup) {
  60. if ($debug) print "Marking affected articles as read...\n";
  61. catchupArticlesById($link, $affected_ids, 0, $line["id"]);
  62. }
  63. } else {
  64. if ($debug) print "No headlines\n";
  65. }
  66. db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
  67. WHERE id = " . $line["id"]);
  68. }
  69. }
  70. }
  71. if ($debug) _debug("All done.");
  72. }
  73. function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 1000) {
  74. require_once "lib/MiniTemplator.class.php";
  75. $tpl = new MiniTemplator;
  76. $tpl_t = new MiniTemplator;
  77. $tpl->readTemplateFromFile("templates/digest_template_html.txt");
  78. $tpl_t->readTemplateFromFile("templates/digest_template.txt");
  79. $user_tz_string = get_pref($link, 'USER_TIMEZONE', $user_id);
  80. $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
  81. $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
  82. $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
  83. $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
  84. $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
  85. $affected_ids = array();
  86. if (DB_TYPE == "pgsql") {
  87. $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
  88. } else if (DB_TYPE == "mysql") {
  89. $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
  90. }
  91. $result = db_query($link, "SELECT ttrss_entries.title,
  92. ttrss_feeds.title AS feed_title,
  93. COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title,
  94. date_updated,
  95. ttrss_user_entries.ref_id,
  96. link,
  97. score,
  98. content,
  99. ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  100. FROM
  101. ttrss_user_entries,ttrss_entries,ttrss_feeds
  102. LEFT JOIN
  103. ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
  104. WHERE
  105. ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
  106. AND include_in_digest = true
  107. AND $interval_query
  108. AND ttrss_user_entries.owner_uid = $user_id
  109. AND unread = true
  110. AND score >= 0
  111. ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
  112. LIMIT $limit");
  113. $cur_feed_title = "";
  114. $headlines_count = db_num_rows($result);
  115. $headlines = array();
  116. while ($line = db_fetch_assoc($result)) {
  117. array_push($headlines, $line);
  118. }
  119. for ($i = 0; $i < sizeof($headlines); $i++) {
  120. $line = $headlines[$i];
  121. array_push($affected_ids, $line["ref_id"]);
  122. $updated = make_local_datetime($link, $line['last_updated'], false,
  123. $user_id);
  124. /* if ($line["score"] != 0) {
  125. if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
  126. $line["title"] .= " (".$line['score'].")";
  127. } */
  128. if (get_pref($link, 'ENABLE_FEED_CATS', $user_id)) {
  129. $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
  130. }
  131. $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
  132. $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
  133. $tpl->setVariable('ARTICLE_LINK', $line["link"]);
  134. $tpl->setVariable('ARTICLE_UPDATED', $updated);
  135. $tpl->setVariable('ARTICLE_EXCERPT',
  136. truncate_string(strip_tags($line["content"]), 300));
  137. // $tpl->setVariable('ARTICLE_CONTENT',
  138. // strip_tags($article_content));
  139. $tpl->addBlock('article');
  140. $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
  141. $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
  142. $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
  143. $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
  144. // $tpl_t->setVariable('ARTICLE_EXCERPT',
  145. // truncate_string(strip_tags($line["excerpt"]), 100));
  146. $tpl_t->addBlock('article');
  147. if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
  148. $tpl->addBlock('feed');
  149. $tpl_t->addBlock('feed');
  150. }
  151. }
  152. $tpl->addBlock('digest');
  153. $tpl->generateOutputToString($tmp);
  154. $tpl_t->addBlock('digest');
  155. $tpl_t->generateOutputToString($tmp_t);
  156. return array($tmp, $headlines_count, $affected_ids, $tmp_t);
  157. }
  158. ?>