Merge branch 'bug-1389' into testserver-stable
[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
28 function sendmail($to, $subject, $message, $from, $replyto = "", $toname = "", $fromname = "", $errorsto = "returns@cacert.org", $use_utf8 = true)
29 {
30 $lines = explode("\n", $message);
31 $message = "";
32 foreach($lines as $line)
33 {
34 $line = trim($line);
35 if($line == ".")
36 $message .= " .\n";
37 else
38 $message .= $line."\n";
39 }
40
41 if($fromname == "")
42 $fromname = $from;
43
44 $bits = explode(",", $from);
45 $from = addslashes($bits['0']);
46 $fromname = addslashes($fromname);
47
48 $smtp = fsockopen("localhost", 25);
49 if(!$smtp)
50 {
51 echo("Could not connect to mailserver at localhost:25\n");
52 return;
53 }
54 $InputBuffer = fgets($smtp, 1024);
55 fputs($smtp, "HELO www.cacert.org\r\n");
56 $InputBuffer = fgets($smtp, 1024);
57 fputs($smtp, "MAIL FROM:<returns@cacert.org>\r\n");
58 $InputBuffer = fgets($smtp, 1024);
59 $bits = explode(",", $to);
60 foreach($bits as $user)
61 fputs($smtp, "RCPT TO:<".trim($user).">\r\n");
62 $InputBuffer = fgets($smtp, 1024);
63 fputs($smtp, "DATA\r\n");
64 $InputBuffer = fgets($smtp, 1024);
65 fputs($smtp, "X-Mailer: CAcert.org Website\r\n");
66 if (array_key_exists("REMOTE_ADDR", $_SERVER))
67 fputs($smtp, "X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
68 fputs($smtp, "Sender: $errorsto\r\n");
69 fputs($smtp, "Errors-To: $errorsto\r\n");
70 if($replyto != "")
71 fputs($smtp, "Reply-To: $replyto\r\n");
72 else
73 fputs($smtp, "Reply-To: $from\r\n");
74 fputs($smtp, "From: $from\r\n");
75 fputs($smtp, "To: $to\r\n");
76 if(preg_match("/[^a-zA-Z0-9 .-\[\]!_@]/",$subject))
77 {
78 fputs($smtp, "Subject: =?utf-8?B?".base64_encode(recode("html..utf-8", $subject))."?=\r\n");
79 }
80 else
81 {
82 fputs($smtp, "Subject: $subject\r\n");
83 }
84 fputs($smtp, "Mime-Version: 1.0\r\n");
85 if($use_utf8)
86 {
87 fputs($smtp, "Content-Type: text/plain; charset=\"utf-8\"\r\n");
88 }
89 else
90 {
91 fputs($smtp, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
92 }
93 fputs($smtp, "Content-Transfer-Encoding: quoted-printable\r\n");
94 fputs($smtp, "Content-Disposition: inline\r\n");
95
96 // fputs($smtp, "Content-Transfer-Encoding: BASE64\r\n");
97 fputs($smtp, "\r\n");
98 // fputs($smtp, chunk_split(base64_encode(recode("html..utf-8", $message)))."\r\n.\r\n");
99 $encoded_lines = explode( "\n", str_replace("\r", "", $message) );
100 array_walk( $encoded_lines,
101 function (&$a) {
102 $a = quoted_printable_encode(recode("html..utf-8", $a));
103 });
104 $encoded_message = implode("\n", $encoded_lines);
105
106 $encoded_message = str_replace("\r.", "\r=2E", $encoded_message);
107 $encoded_message = str_replace("\n.", "\n=2E", $encoded_message);
108 fputs($smtp, $encoded_message);
109 fputs($smtp, "\r\n.\r\n");
110 fputs($smtp, "QUIT\n");
111 $InputBuffer = fgets($smtp, 1024);
112 fclose($smtp);
113 }
114
115 ?>