/var/www/hkosl.com/aga/wp-content/plugins/simple-membership/classes/class.swpm-form.php


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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php

class SwpmForm {

    protected 
$fields;
    protected 
$op;
    protected 
$errors;
    protected 
$sanitized;

    public function 
__construct($fields) {
        
$this->fields $fields;
        
$this->sanitized = array();
        
$this->errors = array();
        
$this->validate_wp_user_email();
        if (
$this->is_valid()){
            foreach (
$fields as $key => $value){
                
$this->$key();
            }
        }
    }
    protected function 
validate_wp_user_email(){
        
$user_name filter_input(INPUT_POST'user_name',FILTER_SANITIZE_STRING);
        
$email filter_input(INPUT_POST'email'FILTER_UNSAFE_RAW);
        if (empty(
$user_name)) {
            return;
        }
        
        
$user get_user_by('login'$user_name);
        if (
$user && ($user->user_email != $email)){
            
$error_msg SwpmUtils::_("Wordpress account exists with given username. But given email doesn't match.");
            
$error_msg .= SwpmUtils::_(" Use a different username to complete the registration. If you want to use that username then you must enter the correct email address associated with the existing WP user to connect with that account.");
            
$this->errors['wp_email'] = $error_msg;
            return;
        }
        
$user get_user_by('email'$email);
        if(
$user && ($user_name != $user->user_login)){
            
$error_msg SwpmUtils::_("Wordpress account exists with given email. But given username doesn't match.");
            
$error_msg .= SwpmUtils::_(" Use a different email address to complete the registration. If you want to use that email then you must enter the correct username associated with the existing WP user to connect with that account.");
            
$this->errors['wp_user'] = $error_msg;
        }
    }
    
    protected function 
user_name() {
        global 
$wpdb;
        if (!empty(
$this->fields['user_name'])){return;}
        
$user_name filter_input(INPUT_POST'user_name',FILTER_SANITIZE_STRING);
        if (empty(
$user_name)) {
            
$this->errors['user_name'] = SwpmUtils::_('Username is required');
            return;
        }
        if (!
SwpmMemberUtils::is_valid_user_name($user_name)) {
            
$this->errors['user_name'] = SwpmUtils::_('Username contains invalid character');
            return;
        }
        
$saned sanitize_text_field($user_name);
        
$query "SELECT count(member_id) FROM {$wpdb->prefix}swpm_members_tbl WHERE user_name= %s";
        
$result $wpdb->get_var($wpdb->prepare($querystrip_tags($saned)));
        if (
$result 0) {
            if (
$saned != $this->fields['user_name']) {
                
$this->errors['user_name'] = SwpmUtils::_('Username already exists.');
                return;
            }
        }
        
$this->sanitized['user_name'] = $saned;
    }

    protected function 
first_name() {
        
$first_name filter_input(INPUT_POST'first_name'FILTER_SANITIZE_STRING);
        if (empty(
$first_name)) {return;}
        
$this->sanitized['first_name'] = sanitize_text_field($first_name);
    }

    protected function 
last_name() {
        
$last_name filter_input(INPUT_POST'last_name'FILTER_SANITIZE_STRING);
        if (empty(
$last_name)) {return;}
        
$this->sanitized['last_name'] = sanitize_text_field($last_name);
    }

    protected function 
password() {
        
$password filter_input(INPUT_POST'password',FILTER_UNSAFE_RAW);
        
$password_re filter_input(INPUT_POST'password_re',FILTER_UNSAFE_RAW);
        if (empty(
$this->fields['password']) && empty($password)) {
            
$this->errors['password'] = SwpmUtils::_('Password is required');
            return;
        }
        if (!empty(
$password)) {
            
$saned sanitize_text_field($password);
            
$saned_re sanitize_text_field($password_re);
            if (
$saned != $saned_re){
                
$this->errors['password'] = SwpmUtils::_('Password mismatch');
            }
            
$this->sanitized['plain_password'] = $password;
            
$this->sanitized['password'] = SwpmUtils::encrypt_password(trim($password)); //should use $saned??;
        
}
    }

    protected function 
email() {
        global 
$wpdb;
        
$email filter_input(INPUT_POST'email'FILTER_UNSAFE_RAW);
        if (empty(
$email)) {
            
$this->errors['email'] = SwpmUtils::_('Email is required');
            return;
        }
        if (!
is_email($email)) {
            
$this->errors['email'] = SwpmUtils::_('Email is invalid') . " (".$email.")";
            return;
        }
        
$saned sanitize_email($email);
        
$query "SELECT count(member_id) FROM {$wpdb->prefix}swpm_members_tbl WHERE email= %s";
        
$member_id filter_input(INPUT_GET'member_id'FILTER_SANITIZE_NUMBER_INT); 
        if (!empty(
$member_id)) {
            
$query .= ' AND member_id !=%d';
            
$result $wpdb->get_var($wpdb->prepare($querystrip_tags($saned), $member_id));
        }
        else{            
            
$result $wpdb->get_var($wpdb->prepare($querystrip_tags($saned)));
        }
        
        if (
$result 0) {            
            if (
$saned != $this->fields['email']) {
                
$error_msg SwpmUtils::_('Email is already used.') . " (" $saned .")";
                
$this->errors['email'] = $error_msg;
                return;
            }
        }
        
$this->sanitized['email'] = $saned;
    }

    protected function 
phone() {
        
$phone filter_input(INPUT_POST'phone'FILTER_UNSAFE_RAW);
        if (empty(
$phone)) {return;}
        
$saned wp_kses($phone, array());
        
$this->sanitized['phone'] = $saned;        
        return;
    }

    protected function 
address_street() {
        
$address_street filter_input(INPUT_POST'address_street'FILTER_SANITIZE_STRING);
        if (empty(
$address_street)) { return;}
        
$this->sanitized['address_street'] = wp_kses($address_street, array());
    }

    protected function 
address_city() {
        
$address_city filter_input(INPUT_POST'address_city'FILTER_SANITIZE_STRING);
        if (empty(
$address_city)){ return; }
        
$this->sanitized['address_city'] = wp_kses($address_city, array());
    }

    protected function 
address_state() {
        
$address_state filter_input(INPUT_POST'address_state'FILTER_SANITIZE_STRING);
        if (empty(
$address_state))  {return;}
        
$this->sanitized['address_state'] = wp_kses($address_state, array());
    }

    protected function 
address_zipcode() {
        
$address_zipcode filter_input(INPUT_POST'address_zipcode'FILTER_UNSAFE_RAW);
        if (empty(
$address_zipcode)){ return;}
        
$this->sanitized['address_zipcode'] = wp_kses($address_zipcode, array());
    }

    protected function 
country() {
        
$country filter_input(INPUT_POST'country'FILTER_SANITIZE_STRING);
        if (empty(
$country)){ return;}
        
$this->sanitized['country'] = wp_kses($country, array());
    }

    protected function 
company_name() {
        
$company_name filter_input(INPUT_POST'company_name'FILTER_SANITIZE_STRING);
        
$this->sanitized['company_name'] = $company_name;
    }

    protected function 
member_since() {
        
$member_since filter_input(INPUT_POST'member_since'FILTER_UNSAFE_RAW);
        if (empty(
$member_since)) {return;}
        if (
preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'$member_since)){
            
$this->sanitized['member_since'] =  sanitize_text_field($member_since);
            return;
        }
        
$this->errors['member_since'] = SwpmUtils::_('Member since field is invalid');

    }

    protected function 
subscription_starts() {
        
$subscription_starts filter_input(INPUT_POST'subscription_starts'FILTER_SANITIZE_STRING);
        if(empty(
$subscription_starts)) {return ;}
        if (
preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'$subscription_starts)){
            
$this->sanitized['subscription_starts'] =  sanitize_text_field($subscription_starts);
            return;
        }
        
$this->errors['subscription_starts'] = SwpmUtils::_('Access starts field is invalid');
    }

    protected function 
gender() {
        
$gender filter_input(INPUT_POST'gender'FILTER_SANITIZE_STRING);
        if(empty(
$gender)) {return;}
        if (
in_array($gender, array('male''female''not specified'))){
            
$this->sanitized['gender'] = $gender;
        }
        else{
            
$this->errors['gender'] = SwpmUtils::_('Gender field is invalid');
        }
    }

    protected function 
account_state() {
        
$account_state filter_input(INPUT_POST'account_state'FILTER_SANITIZE_STRING);
        if(empty(
$account_state)) {return;}
        if (
in_array($account_state, array('active''pending''activation_required''inactive''expired'))){
            
$this->sanitized['account_state'] = $account_state;
        }
        else{
            
$this->errors['account_state'] = SwpmUtils::_('Account state field is invalid');
        }
    }

    protected function 
membership_level() {
        
$membership_level filter_input(INPUT_POST'membership_level'FILTER_SANITIZE_NUMBER_INT);
        if (
$membership_level == 1){
            
$this->errors['membership_level'] = SwpmUtils::_('Invalid membership level');
            return;
        }
        
        if (empty(
$membership_level)) {return;}
        
$this->sanitized['membership_level'] = $membership_level;
    }

    protected function 
password_re() {

    }

    protected function 
last_accessed() {

    }

    protected function 
last_accessed_from_ip() {

    }

    protected function 
referrer() {

    }

    protected function 
extra_info() {

    }

    protected function 
reg_code() {

    }

    protected function 
txn_id() {

    }

    protected function 
subscr_id() {
        
$subscr_id filter_input(INPUT_POST'subscr_id'FILTER_SANITIZE_STRING);
        
$this->sanitized['subscr_id'] = $subscr_id;
    }

    protected function 
flags() {

    }

    protected function 
more_membership_levels() {

    }

    protected function 
initial_membership_level() {

    }

    protected function 
home_page() {

    }

    protected function 
notes() {

    }

    protected function 
profile_image() {

    }

    protected function 
expiry_1st() {

    }

    protected function 
expiry_2nd() {

    }

    protected function 
member_id() {

    }

    public function 
is_valid() {
        
        if (!isset(
$this->errors)){
            
//Errors are not set at all. Return true.
            
return true;
        }
        
        return 
count($this->errors) < 1;
    }

    public function 
get_sanitized_member_form_data() {
        return 
$this->sanitized;
    }

    public function 
get_errors() {
        return 
$this->errors;
    }

}