public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
Search Repo:
cmsmadesimple-2-0 / plugins / function.contact_form.php
100644 203 lines (177 sloc) 8.734 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
 
# CMSMS - CMS Made Simple
#
# (c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#
# This project's homepage is: http://cmsmadesimple.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
function smarty_cms_function_contact_form($params, &$smarty) {
 
    global $gCms;
 
    if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha']))
    {
        $captcha =& $gCms->modules['Captcha']['object'];
    }
 
  if (empty($params['email'])){
    echo '<div class="formError">An email address must be specified in order to use this plugin.</div>';
    return;
  }else{
    $to = $params['email'];
  }
  
  $style = true; // Use default styles
  if (FALSE == empty($params['style']) && $params['style'] === "false" ) $style = false; // Except if "false" given in params
  
  // Default styles
  $inputStyle = 'style="width:100%;border: 1px solid black; margin:0 0 1em 0;"'; // input boxes
  $taStyle = 'style="width:100%; border: 1px solid black; margin:0 0 1em 0;"'; // TextArea boxes
  $formStyle = 'style="width:30em; important; font-weight: bold;"'; // form
  $errorsStyle = 'style="color: white; background-color: red; font-weight: bold; border: 3px solid black; margin: 1em;"'; // Errors box (div)
        $labelStyle = 'style="display:block;"';
        $buttonStyle = 'style="float:left; width:50%;"';
        $fieldsetStyle = 'style="padding:1em;"';
        $captchaStyle = 'style="margin-bottom:1em; text-align: center;"';
 
  $errors=$name=$email=$subject=$message = '';
  if (FALSE == empty($params['subject_get_var']) && FALSE == empty($_GET[$params['subject_get_var']]))
   {
   $subject = $_GET[$params['subject_get_var']];
   }
  if($_SERVER['REQUEST_METHOD']=='POST'){
    if (!empty($_POST['name'])) $name = cfSanitize($_POST['name']);
    if (!empty($_POST['email'])) $email = cfSanitize($_POST['email']);
    if (!empty($_POST['subject'])) $subject = cfSanitize($_POST['subject']);
    if (!empty($_POST['message'])) $message = $_POST['message'];
 
    if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha']))
    {
     if (!empty($_POST['captcha_resp'])) { $captcha_resp = $_POST['captcha_resp']; }
    }
 
    //Mail headers
    $extra = "From: $name <$email>\r\n";
    $charset = isset($gCms->config['default_encoding']) && $gCms->config['default_encoding'] != '' ? $gCms->config['default_encoding'] : 'utf-8';
    $extra .= "Content-Type: text/plain; charset=" . $charset . "\r\n";
    
    if (empty($name)) $errors .= "\t\t<li>" . 'Please Enter Your Name' . "</li>\n";
    if (empty($email)) $errors .= "\t\t<li>" . 'Please Enter Your Email Address' . "</li>\n";
    elseif (!validEmail($email)) $errors .= "\t\t<li>" . 'Your Email Address is Not Valid' . "</li>\n";
    if (empty($subject)) $errors .= "\t\t<li>" . 'Please Enter a Subject' . "</li>\n";
    if (empty($message)) $errors .= "\t\t<li>" . 'Please Enter a Message' . "</li>\n";
    if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha']))
    {
     if (empty($captcha_resp)) $errors .= "\t\t<li>" . 'Please enter the text in the image' . "</li>\n";
     elseif (! ($captcha->checkCaptcha($captcha_resp))) $errors .= "\t\t<li>" . 'The text from the image was not entered correctly' . "</li>\n";
    }
    
    if (!empty($errors)) {
      echo '<div class="formError" ' . (($style) ? $errorsStyle:'') . '>' . "\n";
      echo '<p>Error(s) : </p>' . "\n";
      echo "\t<ul>\n";
      echo $errors;
      echo "\t</ul>\n";
      echo "</div>";
    }
    elseif (@mail($to, $subject, $message . "\n\nsender: ".$_SERVER["REMOTE_ADDR"] . ', ' . $_SERVER["HTTP_USER_AGENT"] , $extra)) {
      echo '<div class="formMessage">Your message was successfully sent.</div>' . "\n";
      return;
    }
    else {
      echo '<div class="formError" ' . (($style) ? $errorsStyle:'') . '>Sorry, the message was not sent. The server may be down!</div>' . "\n";
      return;
    }
  }
 
    if (isset($_SERVER['REQUEST_URI']))
    {
  $action = $_SERVER['REQUEST_URI'];
    }
    else
    {
  $action = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
  if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '')
  {
   $action .= '?'.$_SERVER['QUERY_STRING'];
  }
    }
?>
 
  <!-- CONTACT_FORM -->
  <form action="<?php echo $action ?>" method="post" <?php echo ($style) ? $formStyle:''; ?>>
<fieldset <?php echo ($style) ? $fieldsetStyle:''; ?>>
<legend>Contact</legend>
      <label for="name" <?php echo ($style) ? $labelStyle:''; ?> >Your name :</label>
      <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" <?php echo ($style) ? $inputStyle:''; ?>/>
 
      <label for="email" <?php echo ($style) ? $labelStyle:''; ?> >Your email address : </label>
      <input type="text" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" <?php echo ($style) ? $inputStyle:''; ?>/>
 
      <label for="subject" <?php echo ($style) ? $labelStyle:''; ?> >Subject : </label>
      <input type="text" id="subject" name="subject" value="<?php echo htmlspecialchars($subject); ?>" <?php echo ($style) ? $inputStyle:''; ?>/>
 
      <label for="message" <?php echo ($style) ? $labelStyle:''; ?> >Message : </label>
      <textarea id="message" name="message" rows="12" cols="48" <?php echo ($style) ? $taStyle:''; ?>><?php echo $message; ?></textarea>
 
<?php
if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha']))
{
?>
      <label for="captcha_resp" <?php echo ($style) ? $labelStyle:''; ?> >Enter the text from the image below : </label>
      <input type="text" id="captcha_resp" name="captcha_resp" value="" <?php echo ($style) ? $inputStyle:''; ?>/>
 
<?php
    echo "<div $captchaStyle>" . $captcha->getCaptcha() . '</div>';
}
?>
 
     <input type="submit" class="button" value="Submit" <?php echo ($style) ? $buttonStyle: ''; ?> />
<input type="reset" class="button" value="Clear" <?php echo ($style) ? $buttonStyle: ''; ?> />
</fieldset>
  </form>
  <!-- END of CONTACT_FORM -->
 
<?php
}
 
function smarty_cms_help_function_contact_form() {
  ?>
  <h3>What does this do?</h3>
  <p>Display's a contact form. This can be used to allow others to send an email message to the address specified.</p>
  <h3>How do I use it?</h3>
  <p>Just insert the tag into your template/page like: <code>{contact_form email="yourname@yourdomain.com"}</code><br>
  <br>
  If you would like to send an email to multiple adresses, seperate each address with a comma.</p>
  <h3>What parameters does it take?</h3>
  <ul>
    <li>email - The email address that the message will be sent to.</li>
    <li><em>(optional)</em>style - true/false, use the predefined styles. Default is true.</li>
    <li><em>(optional)</em>subject_get_var - string, allows you to specify which _GET var to use as the default value for subject.
<p>Example:</p>
<pre>{contact_form email="yourname@yourdomain.com" subject_get_var="subject"}</pre>
<p>Then call the page with the form on it like this: /index.php?page=contact&subject=test+subject</p>
<p>And the following will appear in the "Subject" box: "test subject"
</li>
    <li><em>(optional)</em>captcha - true/false, use Captcha response test (Captcha module must be installed). Default is false.</li>
  </ul>
  </p>
  <?php
}
 
function smarty_cms_about_function_contact_form() {
  ?>
  <p>Author: Brett Batie &lt;brett-cms@classicwebdevelopment.com&gt; &amp; Simon van der Linden &lt;ifmy@geekbox.be&gt;</p>
  <p>Version: 1.4 (20061010)</p>
  <p>
  Change History:<br/>
<ul>
<li>l.2 : various improvements (errors handling, etc.)</li>
<li>1.3 : added subject_get_var parameter (by elijahlofgren)</li>
<li>1.4 : added captcha module support (by Dick Ittmann)</li>
</ul>
  </p>
  <?php
}
 
function cfsanitize($content){
  return str_replace(array("\r", "\n"), "", trim($content));
}
 
function validEmail($email) {
  if (!preg_match("/^([\w|\.|\-|_]+)@([\w||\-|_]+)\.([\w|\.|\-|_]+)$/i", $email)) {
    return false;
    exit;
  }
  return true;
}
 
?>