initial commit
[danixland-contact-form.git] / danixland-contact-form.php
1 <?php
2 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
3
4 /*
5 Plugin Name: danixland contact form
6 Description: A simple yet powerful contact form plugin
7 Plugin URI: https://danix.xyz/?p=3577
8 Version: 1.2
9 Author: Danilo 'danix' Macr&igrave;
10 Author URI: http://danix.xyz
11 License: GPL2
12 License URI: https://www.gnu.org/licenses/gpl-2.0.html
13 Domain Path: /languages
14 Text Domain: dnxcf
15 */
16
17 global $dnxcf_form_version;
18 $dnxcf_form_version = '1.2';
19
20 /**
21 * Add plugin i18n domain: dnxcf
22 * @since 0.2
23 */
24 load_plugin_textdomain('dnxcf', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
25
26 /**
27 * Let's load our helper file ( DB initialization and stuff )
28 * @since 0.2
29 */
30 require( dirname( __FILE__ ) . '/include/dnxcf_helper.php' );
31
32 /**
33 * Check / create the database for the plugin data
34 * @since 1.1.3
35 */
36 register_activation_hook( __FILE__, 'dnxcf_set_db_version' );
37 register_activation_hook( __FILE__, 'dnxcf_db_check' );
38
39 if ( is_admin()) {
40 require_once( dirname(__FILE__) . '/include/dnxcf_settings.php' );
41 }
42
43 // Add Genericons, used in the contact form.
44 function dnxcf_scripts() {
45 wp_enqueue_style( 'dnxcf_genericons', plugins_url( '/style/genericons/genericons.css', __FILE__ ), array(), '3.4.1' );
46 wp_enqueue_style( 'dnxcf_helper_style', plugins_url( '/style/dnxcf_style.css', __FILE__ ), array(), '4.4.2' );
47 }
48 add_action( 'wp_enqueue_scripts', 'dnxcf_scripts' );
49
50 /*
51 * This function displays a google map by adding a js script to the wp_footer
52 * @since 1.0
53 */
54 function dnxcf_gmap_enqueue() {
55 global $dnxcf_options;
56 $dnxcf_options = get_option('dnxcf_options');
57 $latitude = $dnxcf_options['dnxcf_latitude'];
58 $longitude = $dnxcf_options['dnxcf_longitude'];
59 $apikey = $dnxcf_options['dnxcf_apikey'];
60 $sitename = get_bloginfo('name');
61 $siteurl = get_bloginfo('url');
62 $sitemsg = $dnxcf_options['dnxcf_gmap_message'];
63 $visitus = __('Come visit us', 'dnxcf');
64
65 $popup_content = <<< DNX6655788EOT
66 <div id="content">
67 <div id="siteNotice">
68 </div>
69 <h1 id="firstHeading" class="firstHeading">$sitename</h1>
70 <div id="bodyContent">
71 <p>$sitemsg</p>
72 <p><a href="$siteurl">$visitus</a></p>
73 </div>
74 </div>
75 DNX6655788EOT;
76 ?>
77 <script>
78 function initMap() {
79 var location = {lat: <?php echo $latitude; ?>, lng: <?php echo $longitude; ?> };
80 var map = new google.maps.Map( document.getElementById('dnxcf_gmap'), {zoom: 15, center: location} );
81
82 contentString = <?php echo json_encode($popup_content); ?>;
83 var infowindow = new google.maps.InfoWindow( {content: contentString} );
84
85 var marker = new google.maps.Marker( {position: location, map: map, animation: google.maps.Animation.DROP} );
86 marker.addListener('click', function() { infowindow.open(map, marker); });
87 }
88 </script>
89
90 <script src="https://maps.googleapis.com/maps/api/js?key=<?php echo $apikey; ?>&callback=initMap" async defer></script>
91
92 <?php }
93
94 global $dnxcf_options;
95 $dnxcf_options = get_option('dnxcf_options');
96 if ( ! empty($dnxcf_options['dnxcf_apikey']) ) {
97 add_action('wp_footer', 'dnxcf_gmap_enqueue');
98 }
99
100
101 /*
102 * The actual contact form displayed using a shortcode
103 * @since 0.1
104 */
105 add_shortcode( 'dnx_contactform', 'dnxcf_return_form');
106
107 /*
108 * we use output buffer so that the form will stay at the bottom of content
109 * allowing the user to write some text in the body of the page before
110 * displaying the form with the shortcode
111 * @since 0.5
112 */
113 function dnxcf_return_form() {
114 ob_start();
115 dnxcf_display_form();
116 $output = ob_get_contents();
117 ob_end_clean();
118 return $output;
119 }
120
121 /*
122 * Here we handle the input/output flow
123 * @since 0.2
124 */
125 function dnxcf_display_form() {
126 global $dnxcf_form_version, $dnxcf_options;
127 $dnxcf_options = get_option('dnxcf_options');
128
129 $dnxcf_pid = md5($dnxcf_options['dnxcf_pid_key']);
130 $dnxcf_vers_id = md5($dnxcf_form_version);
131 $dnxcf_date_id = md5(date('TOZ'));
132 $dnxcf_eml_id = md5($dnxcf_options['dnxcf_recv_email']);
133 $dnxcf_form_id = $dnxcf_pid . $dnxcf_vers_id . $dnxcf_eml_id . $dnxcf_date_id;
134 $dnxcf_form_id = strtoupper(md5($dnxcf_form_id));
135 $dnxcf_form_id = 'ID' . $dnxcf_form_id . 'DNX';
136 $dnxcf_send_value = trim( strtolower( 'submit_' . md5($dnxcf_form_id) ) );
137
138 if ( isset( $_POST[$dnxcf_send_value] ) ) { // the form has been submitted
139 $dnxcf_email_output = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html';
140 $dnxcf_form_name = 'dnxcf_form_' . $dnxcf_pid;
141 $dnxcf_form_action = 'dnxcf_submit_' . $dnxcf_form_id;
142 // valid html used to validate the comment content.
143 $valid_html = array(
144 'a' => array(
145 'href' => array(),
146 'title' => array()
147 ),
148 'br' => array(),
149 'em' => array(),
150 'strong' => array(),
151 'p' => array(),
152 'pre' => array(),
153 'code' => array()
154 );
155 // security checks before submitting the form
156 if ( $_SERVER['REQUEST_URI'] == $_POST['_wp_http_referer'] && wp_verify_nonce( $_POST[ $dnxcf_form_name], $dnxcf_form_action ) ) {
157
158 $dnxcf_posted = array();
159 // let's gather some data about the user submitting the form
160 $dnxcf_ltd = trim(strip_tags(stripslashes(current_time("mysql"))));
161 $dnxcf_hst = trim(strip_tags(stripslashes(getenv("REMOTE_ADDR"))));
162 $dnxcf_ua = trim(strip_tags(stripslashes($_SERVER['HTTP_USER_AGENT'])));
163 // our posted options, arranged in one nice array
164 $dnxcf_posted['dnxcf_name'] = sanitize_text_field($_POST['dnxcf_name']);
165 $dnxcf_posted['dnxcf_email'] = sanitize_email($_POST['dnxcf_email']);
166 $dnxcf_posted['dnxcf_website'] = esc_url($_POST['dnxcf_website']);
167 $dnxcf_posted['dnxcf_subject'] = sanitize_text_field($_POST['dnxcf_subject']);
168 $dnxcf_posted['dnxcf_message'] = wp_kses($_POST['dnxcf_message'], $valid_html);
169 // let's begin with our email data, like receiver email, subject ecc.
170 $dnxcf_to = $dnxcf_options['dnxcf_recv_email'];
171 $dnxcf_headers = "Reply-To: " . $dnxcf_posted['dnxcf_email'];
172 $dnxcf_subject = __('Contact from "', 'dnxcf') . get_bloginfo('name') . '" - ' . $dnxcf_posted['dnxcf_subject'];
173
174 // check for our content type and arrange our info accordingly
175 if ( 'text/html' == $dnxcf_email_output ) {
176 require( apply_filters( 'dnxcf_template_file', dirname( __FILE__ ) . '/include/dnxcf_mail_template_danixland.php') );
177 $dnxcf_email_data = array(
178 'ownname' => $dnxcf_options['dnxcf_recv_name'],
179 'site' => get_bloginfo('name'),
180 'time' => $dnxcf_ltd,
181 'host' => $dnxcf_hst,
182 'ua' => $dnxcf_ua,
183 );
184 $dnxcf_message = dnxcf_email_content( $dnxcf_email_data, $dnxcf_posted );
185 } else { // content_type is set to text/plain
186 $dnxcf_message = sprintf(
187 __("Hello \"%s\",\nyou are being contacted by %s on %s.\n%s has provided the following informations:\n\tEmail:\t\t%s\n\tWebsite:\t%s\n\tMessage:\n\n%s", 'dnxcf'),
188 $dnxcf_options['dnxcf_recv_name'],
189 $dnxcf_posted['dnxcf_name'],
190 get_bloginfo('name'),
191 $dnxcf_posted['dnxcf_name'],
192 $dnxcf_posted['dnxcf_email'],
193 $dnxcf_posted['dnxcf_website'],
194 $dnxcf_posted['dnxcf_message']
195 );
196 $dnxcf_message .= "\n\n##-----------#-----------#-----------##\n\n";
197 $dnxcf_message .= sprintf(
198 __("We have also collected the following informations:\n\tBrowser:\t%s\n\tTime:\t\t%s\n\tIP Address:\t%s\n", 'dnxcf'),
199 $dnxcf_ua,
200 $dnxcf_ltd,
201 $dnxcf_hst
202 );
203 } // end check for mail_content_type
204 $dnxcf_mailed = wp_mail( $dnxcf_to, $dnxcf_subject, $dnxcf_message, $dnxcf_headers );
205 if ( $dnxcf_mailed ) { ?>
206 <p id="dnxcf_success"><?php _e('your email was sent successfully. Here\'s the data you submitted via our form.', 'dnxcf' ); ?></p>
207 <p>
208 <dl>
209 <dt><?php _e('Your Name:', 'dnxcf'); ?></dt>
210 <dd><?php echo $dnxcf_posted['dnxcf_name']; ?></dd>
211 <dt><?php _e('Your e-mail:', 'dnxcf'); ?></dt>
212 <dd><?php echo $dnxcf_posted['dnxcf_email']; ?></dd>
213 <dt><?php _e('Subject of your message:', 'dnxcf'); ?></dt>
214 <dd><?php echo $dnxcf_posted['dnxcf_subject']; ?></dd>
215 <dt><?php _e('Text of your Message:', 'dnxcf'); ?></dt>
216 <dd><blockquote><?php echo wptexturize( wpautop( $dnxcf_posted['dnxcf_message'] ) ); ?></blockquote></dd>
217 </dl>
218 </p>
219 <p><?php _e('we also collected some data for tecnical reasons.', 'dnxcf'); ?></p>
220 <p>
221 <dl>
222 <dt><?php _e('Your IP address:', 'dnxcf'); ?></dt>
223 <dd><?php echo $dnxcf_hst; ?></dd>
224 <dt><?php _e('Your Browser:', 'dnxcf'); ?></dt>
225 <dd><?php echo $dnxcf_ua; ?></dd>
226 </dl>
227 </p>
228 <?php } else { ?>
229 <p id="dnxcf_result_failure"><?php printf( __('there was a problem processing your email. Please contact the <a href="mailto:%s">administrator</a>.', 'dnxcf'), get_bloginfo('admin_email') ); ?></p>
230 <?php } ?>
231
232 <?php
233 } else { // Houston we have a problem, spammer detected
234 // we might want to use the data we have and send a mail to the admin just in case...
235 ?>
236 <p id="dnxcf_spammer"><?php _e('looks like you don\'t belong here. We don\'t like spammers. Go away now!', 'dnxcf'); ?></p>
237 <?php
238 } // end check for wp_nonce
239
240 } else { // the post hasn't been submitted. Let's show the form
241 global $dnxcf_options;
242 $dnxcf_options = get_option('dnxcf_options');
243 ?>
244 <!-- begin #dnxcf_form -->
245 <form id="dnxcf_form" method="post" action="<?php echo htmlentities( $_SERVER['REQUEST_URI'] ); ?>">
246 <?php wp_nonce_field( 'dnxcf_submit_' . $dnxcf_form_id, 'dnxcf_form_' . $dnxcf_pid, true, true );
247 if ( ! empty($dnxcf_options['dnxcf_apikey']) ) : ?>
248 <h4><?php _e('Our location', 'dnxcf'); ?></h4>
249 <div id="dnxcf_gmap"></div>
250 <?php endif; ?>
251 <p class="comment-notes"><?php _e('Required fields are marked ', 'dnxcf'); ?><span class="required">*</span></p>
252 <fieldset id="dnxcf_formwrap">
253 <?php if ( $dnxcf_options['dnxcf_privacy'] ) : ?>
254 <fieldset id="dnxcf_privacy">
255 <h4><?php _e('Privacy:', 'dnxcf'); ?></h4>
256 <div class="policy"><?php echo wpautop( wptexturize( $dnxcf_options['dnxcf_privacy'] ) ); ?></div>
257 </fieldset>
258 <?php endif; ?>
259 <fieldset id="dnxcf_personal">
260 <h4><?php _e('Personal Informations:', 'dnxcf'); ?></h4>
261 <p class="dnxcf-name">
262 <label class="genericon genericon-user" for="name"><span class="screen-reader-text"><?php _e('Full Name', 'dnxcf'); ?></span><span class="required">*</span></label>
263 <input name="dnxcf_name" id="name" size="35" maxlength="40" type="text" placeholder="<?php _e('name', 'dnxcf'); ?>" required autocomplete="name">
264 </p>
265 <p class="dnxcf-email">
266 <label class="genericon genericon-mail" for="email"><span class="screen-reader-text"><?php _e('email address', 'dnxcf'); ?></span><span class="required">*</span></label>
267 <input name="dnxcf_email" id="email" size="35" maxlength="50" type="email" placeholder="<?php _e('e-mail', 'dnxcf'); ?>" required autocomplete="email">
268 </p>
269 <p class="dnxcf-website">
270 <label for="website" class="genericon genericon-website"><span class="screen-reader-text"><?php _e('website', 'dnxcf'); ?></span></label>
271 <input name="dnxcf_website" id="website" size="35" maxlength="50" type="url" placeholder="<?php _e('website', 'dnxcf'); ?>" autocomplete="url">
272 </p>
273 </fieldset>
274 <fieldset id="dnxcf_message">
275 <h4><?php _e('your message:', 'dnxcf'); ?></h4>
276 <p class="dnxcf-subject">
277 <label class="genericon genericon-tag" for="subject"><span class="screen-reader-text"><?php _e('subject', 'dnxcf'); ?></span><span class="required">*</span></label>
278 <select name="dnxcf_subject" id="subject" required>
279 <?php global $dnxcf_options;
280 $dnxcf_options = get_option('dnxcf_options');
281 $subject_options = $dnxcf_options['dnxcf_subject'];
282 foreach ($subject_options as $option) {
283 echo '<option value="' . $option . '">' . $option . '</option>';
284 } ?>
285 </select>
286 </p>
287 <p class="dnxcf-message">
288 <label class="genericon genericon-comment" for="message"><span class="screen-reader-text"><?php _e('message', 'dnxcf'); ?></span><span class="required">*</span></label>
289 <textarea name="dnxcf_message" id="message" cols="60" rows="12" placeholder="<?php _e('Comment Here', 'dnxcf'); ?>" required></textarea>
290 </p>
291 </fieldset>
292 <fieldset id="dnxcf_send">
293 <input type="submit" name="<?php echo $dnxcf_send_value; ?>" id="<?php echo $dnxcf_send_value; ?>" value="<?php _e('send message', 'dnxcf'); ?>">
294 </fieldset>
295 </fieldset><!-- #formwrap -->
296 </form>
297 <!-- end #dnxcf_form -->
298 <?php
299
300 } // end check for post submission
301 } // end dnxcf_display_form()
302 ?>