root / wp-frontend / trunk / plugins / ContactForm.php

Revision 767, 6.3 kB (checked in by klaas, 1 year ago)

Sync with weblogssl trunk for WP2.1 compatibility

Line 
1<?php
2
3// Author: Inaki Ecenarro / WeblogsSL
4
5require_once 'classes/PostGenericAction.php';
6
7class ContactForm extends PostGenericAction {
8
9    var $default_context = LP_CONTEXT_STATIC;
10   
11    var $description = 'Displays a contact form on a static page. Use the {PLUGIN_CONTACTFORM} template tag to show it in a page.';
12   
13    var $constructor_args = array(
14        'email_to'          => 'Email address to send the form to',
15        'mail_subject'      => 'Subject to use for the contact email',
16        // 'captcha'           =>  'Do you want to use a captcha to avoid spam?',
17        );
18
19    // options
20    var $email_to = 'emailto@domain.com';
21    var $mail_subject = 'mail from contact';
22    var $captcha = 1;
23   
24    var $active = true;
25   
26    var $hooks = array('parse_post');
27   
28    var $_actions = array('send_comment', 'notify_post');
29
30    function ContactForm(&$frontend, $args, $dummy_run=false) {
31        $this->PostGenericAction($frontend, $args, $dummy_run);
32    }
33   
34    function run($hook, &$post) {
35       
36        $tpl =& $this->_frontend->tpl;
37        $action =& $this->_action;
38       
39        if (isset($_GET['sent']) && $_GET['sent'] == 1 ) {
40           
41            $tpl->setFile('PLUGIN_CONTACTFORM', 'plugins/contact_form_sent.xml');
42            $tpl->parse('PLUGIN_CONTACTFORM', 'PLUGIN_CONTACTFORM');
43           
44        } else {
45           
46            $messages =& $this->_frontend->messages;
47            $options =& $this->_frontend->options;
48           
49            $this->_bootstrap($post, $messages['comment_form_message']);   // **IE** what is this?
50           
51            $message =& $this->_message;
52            $message_class =& $this->_message_class;
53            $user_data =& $this->_user_data;
54            $labels =& $this->_labels;
55            // check user IP
56            $ip = $_SERVER['REMOTE_ADDR'];
57
58            $rbls = array(
59                'bl.blbl.org', // blacklist Wordpress
60                'bsb.spamlookup.net', // blacklist MT
61                'opm.blitzed.org', // blacklist open proxies
62            );
63
64            $found = false;
65
66            if (preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/', $ip, $matches)) {
67                foreach ($rbls as $rbl) {
68                    $rblhost = $matches[4] . '.' . $matches[3] . '.' . $matches[2] . '.' . $matches[1] . '.' . $rbl.'.';
69                    $resolved = gethostbyname($rblhost);
70                    if ($resolved != $rblhost) {
71                        die;
72                    }
73                }
74            }
75           
76            // check user submitted data
77            $empty_fields = array();
78            foreach (array('name', 'email', 'text' ) as $key) {
79                if (empty($user_data["ud_$key"]))
80                    $empty_fields[] = $key;
81            }
82
83            // before we go ahead, lets do some checking for email injection
84            // we are just dying here, that's not very pretty, but a right punishment
85            $sender_name = urldecode($this->_user_data['ud_name']);
86            if (!empty($sender_name) && eregi("\r",$sender_name)){
87                die();
88            }
89
90            $sender_email = urldecode($this->_user_data['ud_email']);
91            if (!empty($sender_email) && !eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",$sender_email)) {
92                die();
93            }
94
95            // basic email regexp check
96            if (!empty($user_data['ud_email']) && !preg_match('/[^@]+@[^.]+\.[^.]+/', $user_data['ud_email']))
97                $empty_fields[] = 'email';
98           
99            if (count($empty_fields) == 0) {
100                // no errors, let's send message
101                $mh =& $this->_frontend->getMailHelper();
102                $shortname = $mh->htent2qp($options['shortname']);
103                $shortname_hdr = $mh->encoded2hdr($options['shortname']);
104                $tpl->setVar(array(
105                    'contact_name'          => $mh->htent2qp($user_data['ud_name']),
106                    'contact_email'         => $mh->htent2qp($user_data['ud_email']),
107                    'contact_message'       => $mh->htent2qp(wordwrap(strip_tags($user_data['ud_text']), 75, "\r\n")),
108                    'contact_date'          => date( "d-m-Y H:i:s" ),
109                    'contact_ip'            => $_SERVER['REMOTE_ADDR']
110                    ));
111                $headers = array(
112                    'Content-Type: text/plain; charset="' . $options['charset'] . '"',
113                    'MIME-Version: 1.0',
114                    'Content-Transfer-Encoding: quoted-printable',
115                    'From: "' . $mh->encoded2hdr($user_data['ud_name'], $options['charset']) . '" <' . $user_data['ud_email'] . '>');
116                $tpl->setFile('PLUGIN_CONTACTFORM', 'plugins/contact_form.txt');
117                $mh->send_mail(
118                    $this->email_to,
119                    "[$shortname_hdr] " . $mh->encoded2hdr($this->mail_subject),
120                    $tpl->parse('PLUGIN_CONTACTFORM', 'PLUGIN_CONTACTFORM'),
121                    implode("\r\n", $headers));
122                if (isset($_SERVER['HTTPS'])) {
123                    $location = "https://{$_SERVER['HTTP_HOST']}";
124                    $port = '443';
125                } else {
126                    $location = "http://{$_SERVER['HTTP_HOST']}";
127                    $port = 80;
128                }
129                if ($_SERVER['SERVER_PORT'] != $port)
130                    $location .= ":{$_SERVER['SERVER_PORT']}";
131                header("Location: $location{$_SERVER['REQUEST_URI']}?sent=1" );
132            } else {
133                if ($action == 'send_comment') {
134                    // we have errors, let's set warnings and hide comment stuff
135                    foreach ($empty_fields as $empty_field)
136                        $user_data["ud_label_$empty_field"] = "warning";
137                    $message_class = 'comment_form_warning';
138                }
139                $tpl->setVar($user_data);
140                $tpl->setVar(array(
141                    'comment_form_message'          => $message,
142                    'comment_form_message_class'    => $message_class));
143                $tpl->setFile('PLUGIN_CONTACTFORM', 'plugins/contact_form.xml');
144                $tpl->parse('PLUGIN_CONTACTFORM', 'PLUGIN_CONTACTFORM');
145            }
146        }
147    }
148}
149
150?>
Note: See TracBrowser for help on using the browser.