Merge remote-tracking branch 'origin/bug-1213' into release
[cacert-devel.git] / scripts / ate-hh-mail.php.txt
1 #!/usr/bin/php -q
2 <? /*
3 LibreSSL - CAcert web application
4 Copyright (C) 2004-2008 CAcert Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 include_once("../includes/mysql.php");
20
21 $lines = "";
22 $fp = fopen("ate-hh-email.txt", "r");
23 while(!feof($fp))
24 {
25 $line = trim(fgets($fp, 4096));
26 $lines .= wordwrap($line, 75, "\n")."\n";
27 }
28 fclose($fp);
29
30
31 // $locid = intval($_REQUEST['location']);
32 // $maxdist = intval($_REQUEST['maxdist']);
33 $maxdist = 200;
34
35
36 // location location.ID
37 // verified: 29.4.09 u.schroeter
38 // $locid = 7902857; // Paris
39 // $locid = 238568; // Bielefeld
40 // $locid = 715191; // Hamburg
41 // $locid = 1102495; // London
42 // $locid = 520340; // Duesseldorf
43 // $locid = 1260319; // Muenchen
44 // $locid = 606058; // Frankfurt
45 // $locid = 1775784; // Stuttgart
46 // $locid = 228950; // Berlin
47
48 $locid = 715191; // Hamburg
49 $city = "Hamburg 7.5.2009";
50
51
52 $query = "select * from `locations` where `id`='$locid'";
53 $loc = mysql_fetch_assoc(mysql_query($query));
54
55 $query = "SELECT ROUND(6378.137 * ACOS(0.9999999*((SIN(PI() * $loc[lat] / 180) * SIN(PI() * `locations`.`lat` / 180)) +
56 (COS(PI() * $loc[lat] / 180 ) * COS(PI() * `locations`.`lat` / 180) *
57 COS(PI() * `locations`.`long` / 180 - PI() * $loc[long] / 180)))), -1) AS `distance`, sum(`points`) as pts, `users`.*
58 FROM `locations`
59 inner join `users` on `users`.`locid` = `locations`.`id`
60 inner join `alerts` on `users`.`id`=`alerts`.`memid`
61 inner join `notary` on `users`.`id`=`notary`.`to`
62 WHERE (`alerts`.`general`=1 OR `alerts`.`country`=1 OR `alerts`.`regional`=1 OR `alerts`.`radius`=1)
63 GROUP BY `users`.`id`
64 HAVING `distance` <= '$maxdist'
65 ORDER BY `distance` ";
66 echo $query;
67
68 // comment next line when starting to send mail not only to me
69 // $query = "select * from `users` where `email` like 'cacerttest%'";
70
71 $res = mysql_query($query);
72 $xrows = mysql_num_rows($res);
73
74 while($row = mysql_fetch_assoc($res))
75 {
76 echo $row['pts']."..".$row['email']."...\n";
77 // uncomment next line to send mails ...
78 sendEmail($row['email'], "[CAcert.org] ATE-$city", $lines, "events@cacert.org", "", "", "CAcert Events Organisation", "returns@cacert.org", 1);
79 }
80 // 1x cc to events.cacert.org
81 sendEmail("events@cacert.org", "[CAcert.org] ATE-$city", $lines, "events@cacert.org", "", "", "CAcert Events Organisation", "returns@cacert.org", 1);
82 // 1x mailing report to events.cacert.org
83 sendEmail("events@cacert.org", "[CAcert.org] ATE-$city Report", "invitation sent to $xrows recipients.", "support@cacert.org", "", "", "CAcert Events Organisation", "returns@cacert.org", 1);
84
85
86
87 function sendEmail($to, $subject, $message, $from, $replyto = "", $toname = "", $fromname = "", $errorsto = "returns@cacert.org", $extra="")
88 {
89 $lines = explode('\n', $message);
90 $message = "";
91 foreach($lines as $line)
92 {
93 $line = trim($line);
94 if($line == ".")
95 $message .= " .\n";
96 else
97 $message .= $line."\n";
98 }
99
100 if($fromname == "")
101 $fromname = $from;
102
103 $bits = explode(",", $from);
104 $from = addslashes($bits['0']);
105 $fromname = addslashes($fromname);
106
107 $smtp = fsockopen("localhost", 25);
108 if(!$smtp)
109 {
110 echo("Could not connect to mailserver at localhost:25\n");
111 return;
112 }
113 $InputBuffer = fgets($smtp, 1024);
114 fputs($smtp, "HELO hlin.cacert.org\r\n");
115 $InputBuffer = fgets($smtp, 1024);
116 fputs($smtp, "MAIL FROM: <returns@cacert.org>\r\n");
117 $InputBuffer = fgets($smtp, 1024);
118 $bits = explode(",", $to);
119 foreach($bits as $user)
120 fputs($smtp, "RCPT TO: <".trim($user).">\r\n");
121 $InputBuffer = fgets($smtp, 1024);
122 fputs($smtp, "DATA\r\n");
123 $InputBuffer = fgets($smtp, 1024);
124 fputs($smtp, "X-Mailer: CAcert.org Website\r\n");
125 fputs($smtp, "X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
126 fputs($smtp, "Sender: $errorsto\r\n");
127 fputs($smtp, "Errors-To: $errorsto\r\n");
128 if($replyto != "")
129 fputs($smtp, "Reply-To: $replyto\r\n");
130 else
131 fputs($smtp, "Reply-To: $from\r\n");
132 fputs($smtp, "From: $from\r\n");
133 fputs($smtp, "To: $to\r\n");
134 if(preg_match("/[^a-zA-Z0-9 .-\[\]!_@]/",$subject))
135 {
136 fputs($smtp, "Subject: =?utf-8?B?".base64_encode( $subject)."?=\r\n");
137 // fputs($smtp, "Subject: =?utf-8?B?".base64_encode(recode("html..utf-8", $subject))."?=\r\n");
138 }
139 else
140 {
141 fputs($smtp, "Subject: $subject\r\n");
142 }
143 fputs($smtp, "Mime-Version: 1.0\r\n");
144 if($extra == "")
145 {
146 fputs($smtp, "Content-Type: text/plain; charset=\"utf-8\"\r\n");
147 // fputs($smtp, "Content-Transfer-Encoding: 8bit\r\n");
148 } else {
149 fputs($smtp, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
150 // fputs($smtp, "Content-Transfer-Encoding: quoted-printable\r\n");
151 fputs($smtp, "Content-Disposition: inline\r\n");
152 }
153 fputs($smtp, "Content-Transfer-Encoding: BASE64\r\n");
154 fputs($smtp, "\r\n");
155 fputs($smtp, chunk_split(base64_encode($message))."\r\n.\r\n");
156 // fputs($smtp, chunk_split(base64_encode(recode("html..utf-8", $message)))."\r\n.\r\n");
157 // fputs($smtp, recode("html..utf-8", $message)."\r\n.\r\n");
158 // fputs($smtp, $message."\r\n.\r\n");
159 fputs($smtp, "QUIT\n");
160 $InputBuffer = fgets($smtp, 1024);
161 fclose($smtp);
162 }
163
164 ?>