1 |
<?php |
2 |
|
3 |
$dir = "/data/corp_html"; |
4 |
|
5 |
include_once("DB.php"); // PEAR |
6 |
include_once("$dir/inc/Smarty.class.php"); |
7 |
include_once("$dir/inc/quoted-printable.inc"); |
8 |
include_once("$dir/inc/news_type.inc"); |
9 |
|
10 |
function CheckError($sth) { |
11 |
if (DB::isError($sth)) die($sth->getMessage()); |
12 |
} |
13 |
|
14 |
$user = "dpavlin"; |
15 |
$pwd = ""; |
16 |
$host = "localhost"; |
17 |
$db = "corp"; |
18 |
|
19 |
$smarty = new Smarty; |
20 |
$smarty->template_dir = "$dir/templates"; |
21 |
$smarty->compile_dir = "$dir/templates_c"; |
22 |
|
23 |
$dbh = DB::connect("pgsql://$user:$pwd@$host/$db"); |
24 |
// $dbh->setFetchmode(DB_FETCHMODE_ORDERED); |
25 |
$dbh->setFetchmode(DB_FETCHMODE_ASSOC); |
26 |
$dbh->query("SET DATESTYLE='ISO'"); |
27 |
|
28 |
|
29 |
$sth = $dbh->query("select max(id) as max_id from news_sent"); |
30 |
CheckError($sth); |
31 |
|
32 |
while ($row = $sth->fetchRow()) { |
33 |
if ($row[max_id]) { |
34 |
$max_id = $row[max_id]; |
35 |
} else { |
36 |
$max_id = 0; |
37 |
} |
38 |
} |
39 |
$sql=" |
40 |
select |
41 |
id,upper(title) as title,town_date,body,more,pdf, |
42 |
(date-now()::date) as comming,type,title as title_lc |
43 |
from news where newsletter is true and id > $max_id |
44 |
order by priority desc,date desc"; |
45 |
|
46 |
$sth = $dbh->query($sql); |
47 |
CheckError($sth); |
48 |
|
49 |
|
50 |
if ($sth->numRows() > 0) { |
51 |
|
52 |
$new_news=0; |
53 |
|
54 |
$max_id = 0; |
55 |
while ($row = $sth->fetchRow()) { |
56 |
$row[news_type] = $news_type[$row[type]]; |
57 |
$row[body] = wordwrap(strip_tags($row[body]), 75, "\n"); |
58 |
$data[] = $row; |
59 |
if ($row[id] > $max_id) $max_id = $row[id]; |
60 |
$new_news++; |
61 |
} |
62 |
|
63 |
$sth->free(); |
64 |
|
65 |
$subscribers = 0; |
66 |
|
67 |
$sql = "select email from news_sub where confirmed is true"; |
68 |
$sth = $dbh->query($sql); |
69 |
CheckError($sth); |
70 |
|
71 |
while ($row = $sth->fetchRow()) { |
72 |
$smarty->assign(array( |
73 |
"email"=>$row[email], |
74 |
"data"=>$data |
75 |
)); |
76 |
iso_sendmail_t($smarty->fetch("newsletter_outgoing.tpl")); |
77 |
# print "<pre>".$smarty->fetch("newsletter_outgoing.tpl")."</pre><hr>"; |
78 |
$subscribers++; |
79 |
} |
80 |
|
81 |
// insert last news sent |
82 |
$sth = $dbh->query("insert into news_sent (id) values ($max_id)"); |
83 |
CheckError($sth); |
84 |
|
85 |
|
86 |
print "sent $new_news news to $subscribers subscribers."; |
87 |
} else { |
88 |
print "no news for newsletter to send!"; |
89 |
} |
90 |
|
91 |
$dbh->disconnect(); |
92 |
$dbh = null; |
93 |
|
94 |
?> |