Merge branch 'bug-1459' into release (Emergency bugfix)
[cacert-devel.git] / includes / mysql.php.sample
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 $_SESSION['mconn'] = mysql_connect("127.0.0.1", "username", "password");
20 if ($_SESSION['mconn'] != FALSE)
21 {
22 mysql_select_db("database");
23 $_SESSION['mconn'] = TRUE;
24 }
25 $_SESSION['_config']['normalhostname'] = "www.cacert.org";
26 $_SESSION['_config']['securehostname'] = "secure.cacert.org";
27 $_SESSION['_config']['tverify'] = "tverify.cacert.org";
28
29 function sendmail($to, $subject, $message, $from, $replyto = "", $toname = "", $fromname = "", $errorsto = "returns@cacert.org", $use_utf8 = true)
30 {
31 $lines = explode("\n", $message);
32 $message = "";
33 foreach($lines as $line)
34 {
35 $line = trim($line);
36 if($line == ".")
37 $message .= " .\n";
38 else
39 $message .= $line."\n";
40 }
41
42 if($fromname == "")
43 $fromname = $from;
44
45 $bits = explode(",", $from);
46 $from = addslashes($bits['0']);
47 $fromname = addslashes($fromname);
48
49 $smtp = fsockopen("localhost", 25);
50 if(!$smtp)
51 {
52 echo("Could not connect to mailserver at localhost:25\n");
53 return;
54 }
55 $InputBuffer = fgets($smtp, 1024);
56 fputs($smtp, "HELO www.cacert.org\r\n");
57 $InputBuffer = fgets($smtp, 1024);
58 fputs($smtp, "MAIL FROM:<returns@cacert.org>\r\n");
59 $InputBuffer = fgets($smtp, 1024);
60 $bits = explode(",", $to);
61 foreach($bits as $user)
62 fputs($smtp, "RCPT TO:<".trim($user).">\r\n");
63 $InputBuffer = fgets($smtp, 1024);
64 fputs($smtp, "DATA\r\n");
65 $InputBuffer = fgets($smtp, 1024);
66 fputs($smtp, "X-Mailer: CAcert.org Website\r\n");
67 if (array_key_exists("REMOTE_ADDR", $_SERVER))
68 fputs($smtp, "X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
69 fputs($smtp, "Sender: $errorsto\r\n");
70 fputs($smtp, "Errors-To: $errorsto\r\n");
71 if($replyto != "")
72 fputs($smtp, "Reply-To: $replyto\r\n");
73 else
74 fputs($smtp, "Reply-To: $from\r\n");
75 fputs($smtp, "From: $from\r\n");
76 fputs($smtp, "To: $to\r\n");
77 if(preg_match("/[^a-zA-Z0-9 .-\[\]!_@]/",$subject))
78 {
79 fputs($smtp, "Subject: =?utf-8?B?".base64_encode(recode("html..utf-8", $subject))."?=\r\n");
80 }
81 else
82 {
83 fputs($smtp, "Subject: $subject\r\n");
84 }
85 fputs($smtp, "Mime-Version: 1.0\r\n");
86 if($use_utf8)
87 {
88 fputs($smtp, "Content-Type: text/plain; charset=\"utf-8\"\r\n");
89 }
90 else
91 {
92 fputs($smtp, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
93 }
94 fputs($smtp, "Content-Transfer-Encoding: quoted-printable\r\n");
95 fputs($smtp, "Content-Disposition: inline\r\n");
96
97 // fputs($smtp, "Content-Transfer-Encoding: BASE64\r\n");
98 fputs($smtp, "\r\n");
99 // fputs($smtp, chunk_split(base64_encode(recode("html..utf-8", $message)))."\r\n.\r\n");
100 $encoded_lines = explode( "\n", str_replace("\r", "", $message) );
101 array_walk( $encoded_lines,
102 function (&$a) {
103 $a = quoted_printable_encode(recode("html..utf-8", $a));
104 });
105 $encoded_message = implode("\n", $encoded_lines);
106
107 $encoded_message = str_replace("\r.", "\r=2E", $encoded_message);
108 $encoded_message = str_replace("\n.", "\n=2E", $encoded_message);
109 fputs($smtp, $encoded_message);
110 fputs($smtp, "\r\n.\r\n");
111 fputs($smtp, "QUIT\n");
112 $InputBuffer = fgets($smtp, 1024);
113 fclose($smtp);
114 }
115
116 ?>