Dispatcher.php
8.31 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* Handles sending information to our api.ninjaforms.com endpoint.
*
* @since 3.2
*/
final class NF_Dispatcher
{
private $api_url = 'http://api.ninjaforms.com/';
/**
* Returns bool true if we are opted-in or have a premium add-on.
* If a premium add-on is installed, then users have opted into tracked via our terms and conditions.
* If no premium add-ons are installed, check to see if the user has opted in or out of anonymous usage tracking.
*
* @since version
* @return bool
*/
public function should_we_send() {
/**
* TODO:
* Prevent certain URLS or IPs from submitting. i.e. staging, 127.0.0.1, localhost, etc.
*/
if ( ! has_filter( 'ninja_forms_settings_licenses_addons' ) && ( ! Ninja_Forms()->tracking->is_opted_in() || Ninja_Forms()->tracking->is_opted_out() ) ) {
return false;
}
return true;
}
/**
* Package up our environment variables and send those to our API endpoint.
*
* @since 3.2
* @return void
*
* @updated 3.3.17
*/
public function update_environment_vars() {
global $wpdb;
// Plugins
$active_plugins = (array) get_option( 'active_plugins', array() );
//WP_DEBUG
if ( defined('WP_DEBUG') && WP_DEBUG ){
$debug = 1;
} else {
$debug = 0;
}
//WPLANG
if ( defined( 'WPLANG' ) && WPLANG ) {
$lang = WPLANG;
} else {
$lang = 'default';
}
$ip_address = '';
if ( array_key_exists( 'SERVER_ADDR', $_SERVER ) ) {
$ip_address = $_SERVER[ 'SERVER_ADDR' ];
} else if ( array_key_exists( 'LOCAL_ADDR', $_SERVER ) ) {
$ip_address = $_SERVER[ 'LOCAL_ADDR' ];
}
// If we have a valid IP Address...
if ( filter_var( $ip_address, FILTER_VALIDATE_IP ) ) {
// Get the hostname.
$host_name = gethostbyaddr( $ip_address );
} else {
$host_name = 'unknown';
}
if ( is_multisite() ) {
$multisite_enabled = 1;
} else {
$multisite_enabled = 0;
}
$environment = array(
'nf_version' => Ninja_Forms::VERSION,
'nf_db_version' => get_option( 'ninja_forms_db_version', '1.0' ),
'wp_version' => get_bloginfo('version'),
'multisite_enabled' => $multisite_enabled,
'server_type' => $_SERVER['SERVER_SOFTWARE'],
'php_version' => phpversion(),
'mysql_version' => $wpdb->db_version(),
'wp_memory_limit' => WP_MEMORY_LIMIT,
'wp_debug_mode' => $debug,
'wp_lang' => $lang,
'wp_max_upload_size' => size_format( wp_max_upload_size() ),
'php_max_post_size' => ini_get( 'post_max_size' ),
'hostname' => $host_name,
'smtp' => ini_get('SMTP'),
'smtp_port' => ini_get('smtp_port'),
'active_plugins' => $active_plugins,
);
$this->send( 'update_environment_vars', $environment );
}
/**
* Package up our form data and send it to our API endpoint.
*
* @since 3.2
* @return void
*/
public function form_data() {
global $wpdb;
// If we have not finished the process...
if ( ! get_option( 'nf_form_tel_sent' ) || 'false' == get_option( 'nf_form_tel_sent' ) ) {
// Get our list of already processed forms (if it exists).
$forms_ref = get_option( 'nf_form_tel_data' );
// Get a list of Forms on this site.
$sql = "SELECT id FROM `" . $wpdb->prefix . "nf3_forms`";
$forms = $wpdb->get_results( $sql, 'ARRAY_A' );
// If our list of processed forms already exists...
if ( ! empty( $forms_ref ) ) {
// Break those into an array.
$forms_ref = explode( ',', $forms_ref );
} // Otherwise...
else {
// Make sure we have an array.
$forms_ref = array();
}
$match_found = false;
// For each form...
foreach ( $forms as $form ) {
// If the current form is not in our list of sent values...
if ( ! in_array( $form[ 'id' ], $forms_ref ) ) {
// Set our target ID.
$id = $form[ 'id' ];
// Record that we found a match.
$match_found = true;
}
}
// If we didn't find a match.
if ( ! $match_found ) {
// Record that we're done.
update_option( 'nf_form_tel_sent', 'true', false );
// Exit.
return false;
}// Otherwise... (We did find a match.)
// Get our form.
$form_data = Ninja_Forms()->form( intval( $id ) )->get();
// Setup our data value.
$data = array();
// Set the form title.
$data[ 'title' ] = $form_data->get_setting( 'title' );
$sql = "SELECT COUNT(meta_id) AS total FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key = '_form_id' AND meta_value = '" . intval( $id ) . "'";
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// Set the number of submissions.
$data[ 'subs' ] = $result[ 0 ][ 'total' ];
// Get our fields.
$field_data = Ninja_Forms()->form( intval( $id ) )->get_fields();
$data[ 'fields' ] = array();
// For each field on the form...
foreach ( $field_data as $field ) {
// Add that data to our array.
$data[ 'fields' ][] = $field->get_setting( 'type' );
}
// Get our actions.
$action_data = Ninja_Forms()->form( intval( $id ) )->get_actions();
$data[ 'actions' ] = array();
// For each action on the form...
foreach ( $action_data as $action ) {
// Add that data to our array.
$data[ 'actions' ][] = $action->get_setting( 'type' );
}
// Add this form ID to our option.
$forms_ref[] = $id;
// Update our option.
update_option( 'nf_form_tel_data', implode( ',', $forms_ref ), false );
$this->send( 'form_data', $data );
}
}
/**
* Sends a campaign slug and data to our API endpoint.
* Checks to ensure that the user has 1) opted into tracking or 2) they have a premium add-on installed.
*
* @since 3.2
* @param string $slug Campaign slug
* @param array $data Array of data being sent. Should NOT already be a JSON string.
* @return void
*/
public function send( $slug, $data = array() ) {
if ( ! $this->should_we_send() ) {
return false;
}
/**
* Gather site data before we send.
*
* We send the following site data with our passed data:
* IP Address
* Email
* Site Url
*/
$ip_address = '';
if ( array_key_exists( 'SERVER_ADDR', $_SERVER ) ) {
$ip_address = $_SERVER[ 'SERVER_ADDR' ];
} else if ( array_key_exists( 'LOCAL_ADDR', $_SERVER ) ) {
$ip_address = $_SERVER[ 'LOCAL_ADDR' ];
}
/**
* Email address of the current user.
* (if one was provided)
*/
$email = isset( $data[ 'user_email' ] ) ? $data[ 'user_email' ] : '';
$site_data = array(
'url' => site_url(),
'ip_address' => $ip_address,
'email' => $email,
);
/*
* Send our data using wp_remote_post.
*/
$response = wp_remote_post(
$this->api_url,
array(
'body' => array(
'slug' => $slug,
'data' => wp_json_encode( $data ),
'site_data' => wp_json_encode( $site_data ),
),
)
);
}
}