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