]> git.wh0rd.org - tt-rss.git/blob - classes/digest.php
remove PHPMailer and related directives from config.php-dist; add pluggable Mailer...
[tt-rss.git] / classes / digest.php
1 <?php
2 class Digest
3 {
4
5 /**
6 * Send by mail a digest of last articles.
7 *
8 * @param mixed $link The database connection.
9 * @param integer $limit The maximum number of articles by digest.
10 * @return boolean Return false if digests are not enabled.
11 */
12 static function send_headlines_digests($debug = false) {
13
14 $user_limit = 15; // amount of users to process (e.g. emails to send out)
15 $limit = 1000; // maximum amount of headlines to include
16
17 if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
18
19 if (DB_TYPE == "pgsql") {
20 $interval_qpart = "last_digest_sent < NOW() - INTERVAL '1 days'";
21 } else if (DB_TYPE == "mysql") {
22 $interval_qpart = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
23 }
24
25 $pdo = Db::pdo();
26
27 $res = $pdo->query("SELECT id,email FROM ttrss_users
28 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_qpart)");
29
30 while ($line = $res->fetch()) {
31
32 if (@get_pref('DIGEST_ENABLE', $line['id'], false)) {
33 $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
34
35 // try to send digests within 2 hours of preferred time
36 if ($preferred_ts && time() >= $preferred_ts &&
37 time() - $preferred_ts <= 7200
38 ) {
39
40 if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
41
42 $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
43
44 global $tz_offset;
45
46 // reset tz_offset global to prevent tz cache clash between users
47 $tz_offset = -1;
48
49 $tuple = Digest::prepare_headlines_digest($line["id"], 1, $limit);
50 $digest = $tuple[0];
51 $headlines_count = $tuple[1];
52 $affected_ids = $tuple[2];
53 $digest_text = $tuple[3];
54
55 if ($headlines_count > 0) {
56
57 $mailer = new Mailer();
58
59 //$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text);
60
61 $rc = $mailer->mail(["to" => $line["login"] . " <" . $line["email"] . ">",
62 "subject" => DIGEST_SUBJECT,
63 "message" => $digest_text,
64 "message_html" => $digest]);
65
66 //if (!$rc && $debug) _debug("ERROR: " . $mailer->lastError());
67
68 if ($debug) _debug("RC=$rc");
69
70 if ($rc && $do_catchup) {
71 if ($debug) _debug("Marking affected articles as read...");
72 Article::catchupArticlesById($affected_ids, 0, $line["id"]);
73 }
74 } else {
75 if ($debug) _debug("No headlines");
76 }
77
78 $sth = $pdo->prepare("UPDATE ttrss_users SET last_digest_sent = NOW()
79 WHERE id = ?");
80 $sth->execute([$line["id"]]);
81
82 }
83 }
84 }
85
86 if ($debug) _debug("All done.");
87
88 }
89
90 static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
91
92 require_once "lib/MiniTemplator.class.php";
93
94 $tpl = new MiniTemplator;
95 $tpl_t = new MiniTemplator;
96
97 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
98 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
99
100 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
101 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
102
103 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
104 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
105
106 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
107 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
108
109 $affected_ids = array();
110
111 $days = (int) $days;
112
113 if (DB_TYPE == "pgsql") {
114 $interval_qpart = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
115 } else if (DB_TYPE == "mysql") {
116 $interval_qpart = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
117 }
118
119 $pdo = Db::pdo();
120
121 $sth = $pdo->prepare("SELECT ttrss_entries.title,
122 ttrss_feeds.title AS feed_title,
123 COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
124 date_updated,
125 ttrss_user_entries.ref_id,
126 link,
127 score,
128 content,
129 " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
130 FROM
131 ttrss_user_entries,ttrss_entries,ttrss_feeds
132 LEFT JOIN
133 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
134 WHERE
135 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
136 AND include_in_digest = true
137 AND $interval_qpart
138 AND ttrss_user_entries.owner_uid = :user_id
139 AND unread = true
140 AND score >= 0
141 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
142 LIMIT :limit");
143 $sth->bindParam(':user_id', intval($user_id, 10), \PDO::PARAM_INT);
144 $sth->bindParam(':limit', intval($limit, 10), \PDO::PARAM_INT);
145 $sth->execute();
146
147 $headlines_count = 0;
148 $headlines = array();
149
150 while ($line = $sth->fetch()) {
151 array_push($headlines, $line);
152 $headlines_count++;
153 }
154
155 for ($i = 0; $i < sizeof($headlines); $i++) {
156
157 $line = $headlines[$i];
158
159 array_push($affected_ids, $line["ref_id"]);
160
161 $updated = make_local_datetime($line['last_updated'], false,
162 $user_id);
163
164 if (get_pref('ENABLE_FEED_CATS', $user_id)) {
165 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
166 }
167
168 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
169 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
170 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
171 $tpl->setVariable('ARTICLE_UPDATED', $updated);
172 $tpl->setVariable('ARTICLE_EXCERPT',
173 truncate_string(strip_tags($line["content"]), 300));
174 // $tpl->setVariable('ARTICLE_CONTENT',
175 // strip_tags($article_content));
176
177 $tpl->addBlock('article');
178
179 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
180 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
181 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
182 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
183 // $tpl_t->setVariable('ARTICLE_EXCERPT',
184 // truncate_string(strip_tags($line["excerpt"]), 100));
185
186 $tpl_t->addBlock('article');
187
188 if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
189 $tpl->addBlock('feed');
190 $tpl_t->addBlock('feed');
191 }
192
193 }
194
195 $tpl->addBlock('digest');
196 $tpl->generateOutputToString($tmp);
197
198 $tpl_t->addBlock('digest');
199 $tpl_t->generateOutputToString($tmp_t);
200
201 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
202 }
203
204 }