Linted files
[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
95 global $dnxcf_options;
96 $dnxcf_options = get_option( 'dnxcf_options' );
97 if ( ! empty( $dnxcf_options['dnxcf_apikey'] ) ) {
98 add_action( 'wp_footer', 'dnxcf_gmap_enqueue' );
99 }
100
101
102 /*
103 * The actual contact form displayed using a shortcode
104 * @since 0.1
105 */
106 add_shortcode( 'dnx_contactform', 'dnxcf_return_form' );
107
108 /*
109 * we use output buffer so that the form will stay at the bottom of content
110 * allowing the user to write some text in the body of the page before
111 * displaying the form with the shortcode
112 * @since 0.5
113 */
114 function dnxcf_return_form() {
115 ob_start();
116 dnxcf_display_form();
117 $output = ob_get_contents();
118 ob_end_clean();
119 return $output;
120 }
121
122 /*
123 * Here we handle the input/output flow
124 * @since 0.2
125 */
126 function dnxcf_display_form() {
127 global $dnxcf_form_version, $dnxcf_options;
128 $dnxcf_options = get_option( 'dnxcf_options' );
129
130 $dnxcf_pid = md5( $dnxcf_options['dnxcf_pid_key'] );
131 $dnxcf_vers_id = md5( $dnxcf_form_version );
132 $dnxcf_date_id = md5( date( 'TOZ' ) );
133 $dnxcf_eml_id = md5( $dnxcf_options['dnxcf_recv_email'] );
134 $dnxcf_form_id = $dnxcf_pid . $dnxcf_vers_id . $dnxcf_eml_id . $dnxcf_date_id;
135 $dnxcf_form_id = strtoupper( md5( $dnxcf_form_id ) );
136 $dnxcf_form_id = 'ID' . $dnxcf_form_id . 'DNX';
137 $dnxcf_send_value = trim( strtolower( 'submit_' . md5( $dnxcf_form_id ) ) );
138
139 if ( isset( $_POST[ $dnxcf_send_value ] ) ) { // the form has been submitted
140 $dnxcf_email_output = ( '1' == $dnxcf_options['dnxcf_content_type'] ) ? 'text/plain' : 'text/html';
141 $dnxcf_form_name = 'dnxcf_form_' . $dnxcf_pid;
142 $dnxcf_form_action = 'dnxcf_submit_' . $dnxcf_form_id;
143 // valid html used to validate the comment content.
144 $valid_html = array(
145 'a' => array(
146 'href' => array(),
147 'title' => array(),
148 ),
149 'br' => array(),
150 'em' => array(),
151 'strong' => array(),
152 'p' => array(),
153 'pre' => array(),
154 'code' => array(),
155 );
156 // security checks before submitting the form
157 if ( $_SERVER['REQUEST_URI'] == $_POST['_wp_http_referer'] && wp_verify_nonce( $_POST[ $dnxcf_form_name ], $dnxcf_form_action ) ) {
158
159 $dnxcf_posted = array();
160 // let's gather some data about the user submitting the form
161 $dnxcf_ltd = trim( strip_tags( stripslashes( current_time( 'mysql' ) ) ) );
162 $dnxcf_hst = trim( strip_tags( stripslashes( getenv( 'REMOTE_ADDR' ) ) ) );
163 $dnxcf_ua = trim( strip_tags( stripslashes( $_SERVER['HTTP_USER_AGENT'] ) ) );
164 // our posted options, arranged in one nice array
165 $dnxcf_posted['dnxcf_name'] = sanitize_text_field( $_POST['dnxcf_name'] );
166 $dnxcf_posted['dnxcf_email'] = sanitize_email( $_POST['dnxcf_email'] );
167 $dnxcf_posted['dnxcf_website'] = esc_url( $_POST['dnxcf_website'] );
168 $dnxcf_posted['dnxcf_subject'] = sanitize_text_field( $_POST['dnxcf_subject'] );
169 $dnxcf_posted['dnxcf_message'] = wp_kses( $_POST['dnxcf_message'], $valid_html );
170 // let's begin with our email data, like receiver email, subject ecc.
171 $dnxcf_to = $dnxcf_options['dnxcf_recv_email'];
172 $dnxcf_headers = 'Reply-To: ' . $dnxcf_posted['dnxcf_email'];
173 $dnxcf_subject = __( 'Contact from "', 'dnxcf' ) . get_bloginfo( 'name' ) . '" - ' . $dnxcf_posted['dnxcf_subject'];
174
175 // check for our content type and arrange our info accordingly
176 if ( 'text/html' == $dnxcf_email_output ) {
177 require( apply_filters( 'dnxcf_template_file', dirname( __FILE__ ) . '/include/dnxcf_mail_template_danixland.php' ) );
178 $dnxcf_email_data = array(
179 'ownname' => $dnxcf_options['dnxcf_recv_name'],
180 'site' => get_bloginfo( 'name' ),
181 'time' => $dnxcf_ltd,
182 'host' => $dnxcf_hst,
183 'ua' => $dnxcf_ua,
184 );
185 $dnxcf_message = dnxcf_email_content( $dnxcf_email_data, $dnxcf_posted );
186 } else { // content_type is set to text/plain
187 $dnxcf_message = sprintf(
188 __( "Hello \"%1\$s\",\nyou are being contacted by %2\$s on %3\$s.\n%4\$s has provided the following informations:\n\tEmail:\t\t%5\$s\n\tWebsite:\t%6\$s\n\tMessage:\n\n%7\$s", 'dnxcf' ),
189 $dnxcf_options['dnxcf_recv_name'],
190 $dnxcf_posted['dnxcf_name'],
191 get_bloginfo( 'name' ),
192 $dnxcf_posted['dnxcf_name'],
193 $dnxcf_posted['dnxcf_email'],
194 $dnxcf_posted['dnxcf_website'],
195 $dnxcf_posted['dnxcf_message']
196 );
197 $dnxcf_message .= "\n\n##-----------#-----------#-----------##\n\n";
198 $dnxcf_message .= sprintf(
199 __( "We have also collected the following informations:\n\tBrowser:\t%1\$s\n\tTime:\t\t%2\$s\n\tIP Address:\t%3\$s\n", 'dnxcf' ),
200 $dnxcf_ua,
201 $dnxcf_ltd,
202 $dnxcf_hst
203 );
204 } // end check for mail_content_type
205 $dnxcf_mailed = wp_mail( $dnxcf_to, $dnxcf_subject, $dnxcf_message, $dnxcf_headers );
206 if ( $dnxcf_mailed ) {
207 ?>
208 <p id="dnxcf_success"><?php _e( 'your email was sent successfully. Here\'s the data you submitted via our form.', 'dnxcf' ); ?></p>
209 <p>
210 <dl>
211 <dt><?php _e( 'Your Name:', 'dnxcf' ); ?></dt>
212 <dd><?php echo $dnxcf_posted['dnxcf_name']; ?></dd>
213 <dt><?php _e( 'Your e-mail:', 'dnxcf' ); ?></dt>
214 <dd><?php echo $dnxcf_posted['dnxcf_email']; ?></dd>
215 <dt><?php _e( 'Subject of your message:', 'dnxcf' ); ?></dt>
216 <dd><?php echo $dnxcf_posted['dnxcf_subject']; ?></dd>
217 <dt><?php _e( 'Text of your Message:', 'dnxcf' ); ?></dt>
218 <dd><blockquote><?php echo wptexturize( wpautop( $dnxcf_posted['dnxcf_message'] ) ); ?></blockquote></dd>
219 </dl>
220 </p>
221 <p><?php _e( 'we also collected some data for tecnical reasons.', 'dnxcf' ); ?></p>
222 <p>
223 <dl>
224 <dt><?php _e( 'Your IP address:', 'dnxcf' ); ?></dt>
225 <dd><?php echo $dnxcf_hst; ?></dd>
226 <dt><?php _e( 'Your Browser:', 'dnxcf' ); ?></dt>
227 <dd><?php echo $dnxcf_ua; ?></dd>
228 </dl>
229 </p>
230 <?php } else { ?>
231 <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>
232 <?php } ?>
233
234 <?php
235 } else { // Houston we have a problem, spammer detected
236 // we might want to use the data we have and send a mail to the admin just in case...
237 ?>
238 <p id="dnxcf_spammer"><?php _e( 'looks like you don\'t belong here. We don\'t like spammers. Go away now!', 'dnxcf' ); ?></p>
239 <?php
240 } // end check for wp_nonce
241 } else { // the post hasn't been submitted. Let's show the form
242 global $dnxcf_options;
243 $dnxcf_options = get_option( 'dnxcf_options' );
244 ?>
245 <!-- begin #dnxcf_form -->
246 <form id="dnxcf_form" method="post" action="<?php echo htmlentities( $_SERVER['REQUEST_URI'] ); ?>">
247 <?php
248 wp_nonce_field( 'dnxcf_submit_' . $dnxcf_form_id, 'dnxcf_form_' . $dnxcf_pid, true, true );
249 if ( ! empty( $dnxcf_options['dnxcf_apikey'] ) ) :
250 ?>
251 <h4><?php _e( 'Our location', 'dnxcf' ); ?></h4>
252 <div id="dnxcf_gmap"></div>
253 <?php endif; ?>
254 <p class="comment-notes"><?php _e( 'Required fields are marked ', 'dnxcf' ); ?><span class="required">*</span></p>
255 <fieldset id="dnxcf_formwrap">
256 <?php if ( $dnxcf_options['dnxcf_privacy'] ) : ?>
257 <fieldset id="dnxcf_privacy">
258 <h4><?php _e( 'Privacy:', 'dnxcf' ); ?></h4>
259 <div class="policy"><?php echo wpautop( wptexturize( $dnxcf_options['dnxcf_privacy'] ) ); ?></div>
260 </fieldset>
261 <?php endif; ?>
262 <fieldset id="dnxcf_personal">
263 <h4><?php _e( 'Personal Informations:', 'dnxcf' ); ?></h4>
264 <p class="dnxcf-name">
265 <label class="genericon genericon-user" for="name"><span class="screen-reader-text"><?php _e( 'Full Name', 'dnxcf' ); ?></span><span class="required">*</span></label>
266 <input name="dnxcf_name" id="name" size="35" maxlength="40" type="text" placeholder="<?php _e( 'name', 'dnxcf' ); ?>" required autocomplete="name">
267 </p>
268 <p class="dnxcf-email">
269 <label class="genericon genericon-mail" for="email"><span class="screen-reader-text"><?php _e( 'email address', 'dnxcf' ); ?></span><span class="required">*</span></label>
270 <input name="dnxcf_email" id="email" size="35" maxlength="50" type="email" placeholder="<?php _e( 'e-mail', 'dnxcf' ); ?>" required autocomplete="email">
271 </p>
272 <p class="dnxcf-website">
273 <label for="website" class="genericon genericon-website"><span class="screen-reader-text"><?php _e( 'website', 'dnxcf' ); ?></span></label>
274 <input name="dnxcf_website" id="website" size="35" maxlength="50" type="url" placeholder="<?php _e( 'website', 'dnxcf' ); ?>" autocomplete="url">
275 </p>
276 </fieldset>
277 <fieldset id="dnxcf_message">
278 <h4><?php _e( 'your message:', 'dnxcf' ); ?></h4>
279 <p class="dnxcf-subject">
280 <label class="genericon genericon-tag" for="subject"><span class="screen-reader-text"><?php _e( 'subject', 'dnxcf' ); ?></span><span class="required">*</span></label>
281 <select name="dnxcf_subject" id="subject" required>
282 <?php
283 global $dnxcf_options;
284 $dnxcf_options = get_option( 'dnxcf_options' );
285 $subject_options = $dnxcf_options['dnxcf_subject'];
286 foreach ( $subject_options as $option ) {
287 echo '<option value="' . $option . '">' . $option . '</option>';
288 }
289 ?>
290 </select>
291 </p>
292 <p class="dnxcf-message">
293 <label class="genericon genericon-comment" for="message"><span class="screen-reader-text"><?php _e( 'message', 'dnxcf' ); ?></span><span class="required">*</span></label>
294 <textarea name="dnxcf_message" id="message" cols="60" rows="12" placeholder="<?php _e( 'Comment Here', 'dnxcf' ); ?>" required></textarea>
295 </p>
296 </fieldset>
297 <fieldset id="dnxcf_send">
298 <input type="submit" name="<?php echo $dnxcf_send_value; ?>" id="<?php echo $dnxcf_send_value; ?>" value="<?php _e( 'send message', 'dnxcf' ); ?>">
299 </fieldset>
300 </fieldset><!-- #formwrap -->
301 </form>
302 <!-- end #dnxcf_form -->
303 <?php
304
305 } // end check for post submission
306 } // end dnxcf_display_form()
307 ?>