· 6 years ago · Apr 02, 2020, 01:18 PM
1<?php
2
3require('inc/MailChimp.php');
4
5use \DrewM\MailChimp\MailChimp;
6
7class Crumina_Submit {
8
9 private $ip = '';
10 private $message = '';
11 private $email_subject = '';
12 private $email = '';
13 private $message_success = '';
14 private $nonce = null;
15 private $type = null;
16 private $config = null;
17 private $inputs_allowed = null;
18 private $inputs_required = null;
19 private $inputs = array();
20
21 public function __construct() {
22 $this->config = require('inc/config.php');
23
24 $this->getData();
25 $this->validateData();
26
27 if ( $this->type === 'standard' ) {
28 $this->prepareStandardMessage();
29 $this->sendStandardMessage();
30 }
31
32 if ( $this->type === 'mailchimp' ) {
33 $this->sendMailchimpMessage();
34 }
35 }
36
37 private function getData() {
38 parse_str( filter_input( INPUT_POST, 'inputs' ), $this->inputs );
39 $this->nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING );
40 $this->type = filter_input( INPUT_POST, 'type', FILTER_SANITIZE_STRING );
41 }
42
43 private function validateData() {
44 if ( $this->nonce !== 'crumina-submit-form-nonce' ) {
45 throw new Exception( 'No direct eccess!' );
46 }
47
48 $this->inputs_required = isset( $this->config[ 'forms' ][ $this->type ][ 'inputs_required' ] ) ? (array) $this->config[ 'forms' ][ $this->type ][ 'inputs_required' ] : array();
49 $this->inputs_allowed = isset( $this->config[ 'forms' ][ $this->type ][ 'inputs_allowed' ] ) ? (array) $this->config[ 'forms' ][ $this->type ][ 'inputs_allowed' ] : array();
50 $this->message_success = isset( $this->config[ 'forms' ][ $this->type ][ 'message_success' ] ) ? $this->config[ 'forms' ][ $this->type ][ 'message_success' ] : false;
51
52 if ( !$this->inputs_allowed ) {
53 throw new Exception( 'No allowed fields!' );
54 }
55
56 if ( !$this->message_success ) {
57 throw new Exception( 'No success message!' );
58 }
59
60 if ( !$this->inputs ) {
61 throw new Exception( 'No fields for submit!' );
62 }
63
64 foreach ( $this->inputs as $key => $input ) {
65 switch ( $key ) {
66 case 'email':
67 $filtered = filter_var( $input, FILTER_VALIDATE_EMAIL );
68 break;
69 case 'website':
70 $filtered = filter_var( $input, FILTER_VALIDATE_URL );
71 break;
72 default:
73 $filtered = filter_var( $input, FILTER_SANITIZE_STRING );
74 }
75
76 if ( !$filtered && in_array( $key, $this->inputs_required ) ) {
77 throw new Exception( ucfirst( $key ) . ' field is empty!' );
78 }
79
80 if ( $filtered && in_array( $key, $this->inputs_allowed ) ) {
81 $this->inputs[ $key ] = $filtered;
82 } else {
83 unset( $this->inputs[ $key ] );
84 }
85 }
86 }
87
88 private function prepareStandardMessage() {
89 $this->email_subject = isset( $this->config[ 'forms' ][ $this->type ][ 'email_subject' ] ) ? $this->config[ 'forms' ][ $this->type ][ 'email_subject' ] : false;
90 $this->email = isset( $this->config[ 'forms' ][ $this->type ][ 'email' ] ) ? $this->config[ 'forms' ][ $this->type ][ 'email' ] : false;
91
92 if ( !$this->email ) {
93 throw new Exception( 'No config email!' );
94 }
95
96 if ( !$this->email_subject ) {
97 throw new Exception( 'No message subject!' );
98 }
99
100 if ( isset( $this->inputs[ 'email_subject' ] ) ) {
101 $this->email_subject = $this->inputs[ 'email_subject' ] ? $this->inputs[ 'email_subject' ] : $this->email_subject;
102 }
103
104 // Récupérer la véritable adresse IP d'un visiteur
105 if (!empty($_SERVER['HTTP_CLIENT_IP'])) // consultez l'ip de partage internet
106 {
107 $ip=$_SERVER['HTTP_CLIENT_IP'];
108 }
109 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) // pour vérifier que l'IP est passée de la procuration
110 {
111 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
112 }
113 else
114 {
115 $ip=$_SERVER['REMOTE_ADDR'];
116 }
117
118 foreach ( $this->inputs as $key => $field ) {
119 $this->message .= '<p><strong>' . ucfirst( $key ) . ':</strong> ' . $field . '</p>';
120
121 $this->message .= '<p><strong>IP:</strong> ' . $ip . '</p>';
122 }
123 }
124
125 private function sendStandardMessage() {
126 $headers = "MIME-Version: 1.0\r\n";
127 $headers .= "Content-type: text/html; charset=UTF-8\r\n";
128 $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
129 $headers .= "From: {$this->inputs[ 'name' ]} <{$this->inputs[ 'email' ]}>\r\n";
130 $headers .= "Reply-To: <{$this->inputs[ 'email' ]}>\r\n";
131
132 $submit = mail( $this->email, $this->email_subject, $this->message, $headers, $ip );
133
134 if ( $submit ) {
135 echo json_encode( array(
136 'success' => true,
137 'message' => $this->message_success
138 ) );
139 } else {
140 throw new Exception( 'Have errors during submit!' );
141 }
142 }
143
144 private function sendMailchimpMessage() {
145 $api_key = isset( $this->config[ 'forms' ][ $this->type ][ 'api_key' ] ) ? $this->config[ 'forms' ][ $this->type ][ 'api_key' ] : false;
146 $list_id = isset( $this->config[ 'forms' ][ $this->type ][ 'list_id' ] ) ? $this->config[ 'forms' ][ $this->type ][ 'list_id' ] : false;
147
148 if ( !$api_key ) {
149 throw new Exception( 'Api key is missing!' );
150 }
151
152 if ( !$list_id ) {
153 throw new Exception( 'List id is missing!' );
154 }
155
156 $MailChimp = new MailChimp( $api_key );
157 $subscribe = $MailChimp->post( "lists/{$list_id}/members", [
158 'email_address' => $this->inputs[ 'email' ],
159 'status' => 'subscribed',
160 ] );
161
162 switch ( $subscribe[ 'status' ] ) {
163 case 'subscribed':
164 echo json_encode( array(
165 'success' => true,
166 'message' => $this->message_success
167 ) );
168 break;
169 case 400:
170 echo json_encode( array(
171 'success' => false,
172 'message' => $subscribe[ 'detail' ]
173 ) );
174 break;
175 default:
176 echo json_encode( array(
177 'success' => false,
178 'message' => 'Something went wrong!'
179 ) );
180 }
181 }
182
183}
184
185try {
186 new Crumina_Submit();
187} catch ( Exception $e ) {
188 echo json_encode( array(
189 'success' => false,
190 'message' => $e->getMessage()
191 ) );
192}