class.swpm-form.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. class SwpmForm {
  3. protected $fields;
  4. protected $op;
  5. protected $errors;
  6. protected $sanitized;
  7. public function __construct($fields) {
  8. $this->fields = $fields;
  9. $this->sanitized = array();
  10. $this->errors = array();
  11. $this->validate_wp_user_email();
  12. if ($this->is_valid()){
  13. foreach ($fields as $key => $value){
  14. $this->$key();
  15. }
  16. }
  17. }
  18. protected function validate_wp_user_email(){
  19. $user_name = filter_input(INPUT_POST, 'user_name',FILTER_SANITIZE_STRING);
  20. $email = filter_input(INPUT_POST, 'email', FILTER_UNSAFE_RAW);
  21. if (empty($user_name)) {
  22. return;
  23. }
  24. $user = get_user_by('login', $user_name);
  25. if ($user && ($user->user_email != $email)){
  26. $error_msg = SwpmUtils::_("Wordpress account exists with given username. But given email doesn't match.");
  27. $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.");
  28. $this->errors['wp_email'] = $error_msg;
  29. return;
  30. }
  31. $user = get_user_by('email', $email);
  32. if($user && ($user_name != $user->user_login)){
  33. $error_msg = SwpmUtils::_("Wordpress account exists with given email. But given username doesn't match.");
  34. $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.");
  35. $this->errors['wp_user'] = $error_msg;
  36. }
  37. }
  38. protected function user_name() {
  39. global $wpdb;
  40. if (!empty($this->fields['user_name'])){return;}
  41. $user_name = filter_input(INPUT_POST, 'user_name',FILTER_SANITIZE_STRING);
  42. if (empty($user_name)) {
  43. $this->errors['user_name'] = SwpmUtils::_('Username is required');
  44. return;
  45. }
  46. if (!SwpmMemberUtils::is_valid_user_name($user_name)) {
  47. $this->errors['user_name'] = SwpmUtils::_('Username contains invalid character');
  48. return;
  49. }
  50. $saned = sanitize_text_field($user_name);
  51. $query = "SELECT count(member_id) FROM {$wpdb->prefix}swpm_members_tbl WHERE user_name= %s";
  52. $result = $wpdb->get_var($wpdb->prepare($query, strip_tags($saned)));
  53. if ($result > 0) {
  54. if ($saned != $this->fields['user_name']) {
  55. $this->errors['user_name'] = SwpmUtils::_('Username already exists.');
  56. return;
  57. }
  58. }
  59. $this->sanitized['user_name'] = $saned;
  60. }
  61. protected function first_name() {
  62. $first_name = filter_input(INPUT_POST, 'first_name', FILTER_SANITIZE_STRING);
  63. $this->sanitized['first_name'] = sanitize_text_field($first_name);
  64. }
  65. protected function last_name() {
  66. $last_name = filter_input(INPUT_POST, 'last_name', FILTER_SANITIZE_STRING);
  67. $this->sanitized['last_name'] = sanitize_text_field($last_name);
  68. }
  69. protected function password() {
  70. $password = filter_input(INPUT_POST, 'password',FILTER_UNSAFE_RAW);
  71. $password_re = filter_input(INPUT_POST, 'password_re',FILTER_UNSAFE_RAW);
  72. if (empty($this->fields['password']) && empty($password)) {
  73. $this->errors['password'] = SwpmUtils::_('Password is required');
  74. return;
  75. }
  76. if (!empty($password)) {
  77. $saned = sanitize_text_field($password);
  78. $saned_re = sanitize_text_field($password_re);
  79. if ($saned != $saned_re){
  80. $this->errors['password'] = SwpmUtils::_('Password mismatch');
  81. }
  82. $this->sanitized['plain_password'] = $password;
  83. $this->sanitized['password'] = SwpmUtils::encrypt_password(trim($password)); //should use $saned??;
  84. }
  85. }
  86. protected function email() {
  87. global $wpdb;
  88. $email = filter_input(INPUT_POST, 'email', FILTER_UNSAFE_RAW);
  89. if (empty($email)) {
  90. $this->errors['email'] = SwpmUtils::_('Email is required');
  91. return;
  92. }
  93. if (!is_email($email)) {
  94. $this->errors['email'] = SwpmUtils::_('Email is invalid') . " (".$email.")";
  95. return;
  96. }
  97. $saned = sanitize_email($email);
  98. $query = "SELECT count(member_id) FROM {$wpdb->prefix}swpm_members_tbl WHERE email= %s";
  99. $member_id = filter_input(INPUT_GET, 'member_id', FILTER_SANITIZE_NUMBER_INT);
  100. if (!empty($member_id)) {
  101. $query .= ' AND member_id !=%d';
  102. $result = $wpdb->get_var($wpdb->prepare($query, strip_tags($saned), $member_id));
  103. }
  104. else{
  105. $result = $wpdb->get_var($wpdb->prepare($query, strip_tags($saned)));
  106. }
  107. if ($result > 0) {
  108. if ($saned != $this->fields['email']) {
  109. $error_msg = SwpmUtils::_('Email is already used.') . " (" . $saned .")";
  110. $this->errors['email'] = $error_msg;
  111. return;
  112. }
  113. }
  114. $this->sanitized['email'] = $saned;
  115. }
  116. protected function phone() {
  117. $phone = filter_input(INPUT_POST, 'phone', FILTER_UNSAFE_RAW);
  118. $saned = wp_kses($phone, array());
  119. $this->sanitized['phone'] = $saned;
  120. return;
  121. }
  122. protected function address_street() {
  123. $address_street = filter_input(INPUT_POST, 'address_street', FILTER_SANITIZE_STRING);
  124. $this->sanitized['address_street'] = wp_kses($address_street, array());
  125. }
  126. protected function address_city() {
  127. $address_city = filter_input(INPUT_POST, 'address_city', FILTER_SANITIZE_STRING);
  128. $this->sanitized['address_city'] = wp_kses($address_city, array());
  129. }
  130. protected function address_state() {
  131. $address_state = filter_input(INPUT_POST, 'address_state', FILTER_SANITIZE_STRING);
  132. $this->sanitized['address_state'] = wp_kses($address_state, array());
  133. }
  134. protected function address_zipcode() {
  135. $address_zipcode = filter_input(INPUT_POST, 'address_zipcode', FILTER_UNSAFE_RAW);
  136. $this->sanitized['address_zipcode'] = wp_kses($address_zipcode, array());
  137. }
  138. protected function country() {
  139. $country = filter_input(INPUT_POST, 'country', FILTER_SANITIZE_STRING);
  140. $this->sanitized['country'] = wp_kses($country, array());
  141. }
  142. protected function company_name() {
  143. $company_name = filter_input(INPUT_POST, 'company_name', FILTER_SANITIZE_STRING);
  144. $this->sanitized['company_name'] = $company_name;
  145. }
  146. protected function member_since() {
  147. $member_since = filter_input(INPUT_POST, 'member_since', FILTER_UNSAFE_RAW);
  148. if (empty($member_since)) {return;}
  149. if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $member_since)){
  150. $this->sanitized['member_since'] = sanitize_text_field($member_since);
  151. return;
  152. }
  153. $this->errors['member_since'] = SwpmUtils::_('Member since field is invalid');
  154. }
  155. protected function subscription_starts() {
  156. $subscription_starts = filter_input(INPUT_POST, 'subscription_starts', FILTER_SANITIZE_STRING);
  157. if(empty($subscription_starts)) {return ;}
  158. if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $subscription_starts)){
  159. $this->sanitized['subscription_starts'] = sanitize_text_field($subscription_starts);
  160. return;
  161. }
  162. $this->errors['subscription_starts'] = SwpmUtils::_('Access starts field is invalid');
  163. }
  164. protected function gender() {
  165. $gender = filter_input(INPUT_POST, 'gender', FILTER_SANITIZE_STRING);
  166. if(empty($gender)) {return;}
  167. if (in_array($gender, array('male', 'female', 'not specified'))){
  168. $this->sanitized['gender'] = $gender;
  169. }
  170. else{
  171. $this->errors['gender'] = SwpmUtils::_('Gender field is invalid');
  172. }
  173. }
  174. protected function account_state() {
  175. $account_state = filter_input(INPUT_POST, 'account_state', FILTER_SANITIZE_STRING);
  176. if(empty($account_state)) {return;}
  177. if (in_array($account_state, array('active', 'pending', 'activation_required', 'inactive', 'expired'))){
  178. $this->sanitized['account_state'] = $account_state;
  179. }
  180. else{
  181. $this->errors['account_state'] = SwpmUtils::_('Account state field is invalid');
  182. }
  183. }
  184. protected function membership_level() {
  185. $membership_level = filter_input(INPUT_POST, 'membership_level', FILTER_SANITIZE_NUMBER_INT);
  186. if ($membership_level == 1){
  187. $this->errors['membership_level'] = SwpmUtils::_('Invalid membership level');
  188. return;
  189. }
  190. if (empty($membership_level)) {return;}
  191. $this->sanitized['membership_level'] = $membership_level;
  192. }
  193. protected function birth_place() {
  194. $birth_place = filter_input(INPUT_POST, 'birth_place', FILTER_SANITIZE_STRING);
  195. $this->sanitized['birth_place'] = sanitize_text_field($birth_place);
  196. return;
  197. }
  198. protected function birth_date() {
  199. $birth_date = filter_input(INPUT_POST, 'birth_date', FILTER_UNSAFE_RAW);
  200. if (empty($birth_date)) {return;}
  201. if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $birth_date)){
  202. $this->sanitized['birth_date'] = sanitize_text_field($birth_date);
  203. return;
  204. }
  205. $this->errors['birth_date'] = SwpmUtils::_('Birth Date field is invalid');
  206. }
  207. protected function password_re() {
  208. }
  209. protected function last_accessed() {
  210. }
  211. protected function last_accessed_from_ip() {
  212. }
  213. protected function referrer() {
  214. }
  215. protected function extra_info() {
  216. }
  217. protected function reg_code() {
  218. }
  219. protected function txn_id() {
  220. }
  221. protected function subscr_id() {
  222. $subscr_id = filter_input(INPUT_POST, 'subscr_id', FILTER_SANITIZE_STRING);
  223. $this->sanitized['subscr_id'] = $subscr_id;
  224. }
  225. protected function flags() {
  226. }
  227. protected function more_membership_levels() {
  228. }
  229. protected function initial_membership_level() {
  230. }
  231. protected function home_page() {
  232. }
  233. protected function notes() {
  234. }
  235. protected function profile_image() {
  236. }
  237. protected function expiry_1st() {
  238. }
  239. protected function expiry_2nd() {
  240. }
  241. protected function member_id() {
  242. }
  243. public function is_valid() {
  244. if (!isset($this->errors)){
  245. //Errors are not set at all. Return true.
  246. return true;
  247. }
  248. return count($this->errors) < 1;
  249. }
  250. public function get_sanitized_member_form_data() {
  251. return $this->sanitized;
  252. }
  253. public function get_errors() {
  254. return $this->errors;
  255. }
  256. }