d17b9051 by Marty Penner

Format and translate various methods/classes

1 parent 212a1099
...@@ -59,11 +59,11 @@ function login($username, $password, $remember = false) { ...@@ -59,11 +59,11 @@ function login($username, $password, $remember = false) {
59 throw new LogicException('Unable to login because headers have been sent'); 59 throw new LogicException('Unable to login because headers have been sent');
60 } 60 }
61 61
62 $auth = _signon(Array( 62 $auth = _signon([
63 'user_login' => esc_sql($username) 63 'user_login' => esc_sql($username)
64 , 'user_password' => esc_sql($password) 64 , 'user_password' => esc_sql($password)
65 , 'remember' => $remember 65 , 'remember' => $remember
66 )); 66 ]);
67 67
68 if (get_class($auth) == 'WP_User') { 68 if (get_class($auth) == 'WP_User') {
69 // This is done to ensure the auth'd user is logged in; cookie is not yet readable, redirect needed 69 // This is done to ensure the auth'd user is logged in; cookie is not yet readable, redirect needed
...@@ -124,7 +124,7 @@ function logout() { ...@@ -124,7 +124,7 @@ function logout() {
124 * @global $wpdb 124 * @global $wpdb
125 * @see wpmu_signup_user 125 * @see wpmu_signup_user
126 */ 126 */
127 function register($username, $email, $password, $meta = Array()) { 127 function register($username, $email, $password, $meta = []) {
128 global $wpdb; 128 global $wpdb;
129 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); 129 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
130 130
...@@ -138,11 +138,11 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -138,11 +138,11 @@ function register($username, $email, $password, $meta = Array()) {
138 */ 138 */
139 $wpdb->query("DELETE FROM {$wpdb->signups} WHERE user_email='$email' AND active=0"); 139 $wpdb->query("DELETE FROM {$wpdb->signups} WHERE user_email='$email' AND active=0");
140 140
141 $user_data = Array( 141 $user_data = [
142 'username' => $username 142 'username' => $username
143 , 'password' => $password 143 , 'password' => $password
144 , 'email' => $email 144 , 'email' => $email
145 ); 145 ];
146 $meta['password'] = $password; 146 $meta['password'] = $password;
147 // array_filter($user_data, 'esc_sql'); 147 // array_filter($user_data, 'esc_sql');
148 148
...@@ -158,7 +158,7 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -158,7 +158,7 @@ function register($username, $email, $password, $meta = Array()) {
158 $key = substr(md5(time() . rand() . $email ), 0, 16); 158 $key = substr(md5(time() . rand() . $email ), 0, 16);
159 $meta = serialize($meta); 159 $meta = serialize($meta);
160 160
161 $wpdb->insert($wpdb->signups, Array( 161 $wpdb->insert($wpdb->signups, [
162 'domain' => '', 162 'domain' => '',
163 'path' => '', 163 'path' => '',
164 'title' => '', 164 'title' => '',
...@@ -167,7 +167,7 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -167,7 +167,7 @@ function register($username, $email, $password, $meta = Array()) {
167 'registered' => current_time('mysql', true), 167 'registered' => current_time('mysql', true),
168 'activation_key' => $key, 168 'activation_key' => $key,
169 'meta' => $meta 169 'meta' => $meta
170 )); 170 ]);
171 171
172 // do_action('ACTION_REGISTER'); ??? 172 // do_action('ACTION_REGISTER'); ???
173 173
...@@ -209,7 +209,7 @@ function activate($key, $password_validation = null) { ...@@ -209,7 +209,7 @@ function activate($key, $password_validation = null) {
209 throw new Exception('Unable to create user'); 209 throw new Exception('Unable to create user');
210 } 210 }
211 211
212 $wpdb->update($wpdb->signups, Array('active' => 1, 'activated' => current_time('mysql', true)), Array('activation_key' => $key)); 212 $wpdb->update($wpdb->signups, ['active' => 1, 'activated' => current_time('mysql', true)], ['activation_key' => $key]);
213 // $user_site = get_site_option('dashboard_blog', $current_site->blog_id); 213 // $user_site = get_site_option('dashboard_blog', $current_site->blog_id);
214 214
215 if (function_exists('add_user_to_blog')) { 215 if (function_exists('add_user_to_blog')) {
...@@ -240,16 +240,18 @@ class Validation extends Common\Validation { ...@@ -240,16 +240,18 @@ class Validation extends Common\Validation {
240 */ 240 */
241 protected function username($val) { 241 protected function username($val) {
242 if (empty($val)) { 242 if (empty($val)) {
243 throw new Exception('<li>Username is blank</li>'); 243 throw new Exception('<li>'.__('Username is blank', CBV_DOMAIN).'</li>');
244 } 244 }
245 245
246 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); 246 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
247 if (!validate_username($val)) { 247 if (!validate_username($val)) {
248 throw new Exception('<li>Username must be at least 4 characters, letters and numbers only</li>'); 248 throw new Exception(
249 '<li>'.__('Username must be at least 4 characters, letters and numbers only', CBV_DOMAIN).'</li>'
250 );
249 } 251 }
250 252
251 if (username_exists($val)) { 253 if (username_exists($val)) {
252 throw new Exception('<li>Username already exists</li>'); 254 throw new Exception('<li>'.__('Username already exists', CBV_DOMAIN).'</li>');
253 } 255 }
254 } 256 }
255 257
...@@ -260,15 +262,19 @@ class Validation extends Common\Validation { ...@@ -260,15 +262,19 @@ class Validation extends Common\Validation {
260 */ 262 */
261 protected function password($val) { 263 protected function password($val) {
262 if (empty($val)) { 264 if (empty($val)) {
263 throw new Exception('<li>Password can not be blank</li>'); 265 throw new Exception('<li>'.__('Password can not be blank', CBV_DOMAIN).'</li>');
264 } 266 }
265 267
266 if (isset($val[PASS_MAX_LEN])) { 268 if (isset($val[PASS_MAX_LEN])) {
267 throw new Exception('<li>Password can not be longer than ' . PASS_MAX_LEN . ' characters.</li>'); 269 throw new Exception(
270 '<li>'.sprintf(__('Password can not be longer than %d characters.', CBV_DOMAIN), PASS_MAX_LEN).'</li>'
271 );
268 } 272 }
269 273
270 if (preg_match('/(\\\\|\\\'|"| )+/i', $val)) { 274 if (preg_match('/(\\\\|\\\'|"| )+/i', $val)) {
271 throw new Exception('<li>Password can not contain spaces, backslashes (\) or quotes</li>'); 275 throw new Exception(
276 '<li>'.__('Password can not contain spaces, backslashes (\) or quotes', CBV_DOMAIN).'</li>'
277 );
272 } 278 }
273 } 279 }
274 280
...@@ -278,11 +284,11 @@ class Validation extends Common\Validation { ...@@ -278,11 +284,11 @@ class Validation extends Common\Validation {
278 */ 284 */
279 protected function email($val) { 285 protected function email($val) {
280 if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) { 286 if (!(boolean)filter_var($val, FILTER_VALIDATE_EMAIL)) {
281 throw new Exception('<li>Invalid email address</li>'); 287 throw new Exception('<li>'.__('Invalid email address', CBV_DOMAIN).'</li>');
282 } 288 }
283 289
284 if (false !== email_exists($val)) { 290 if (false !== email_exists($val)) {
285 throw new Exception('<li>Email address already registered</li>'); 291 throw new Exception('<li>'.__('Email address already registered', CBV_DOMAIN).' </li > ');
286 } 292 }
287 } 293 }
288 } 294 }
...@@ -294,4 +300,3 @@ class Vars { ...@@ -294,4 +300,3 @@ class Vars {
294 */ 300 */
295 public static $options; 301 public static $options;
296 } 302 }
297 ?>
...\ No newline at end of file ...\ No newline at end of file
......
1 <?php 1 <?php
2 namespace Tz\WordPress\Tools\HTML; 2 namespace Tz\WordPress\Tools\HTML;
3 3
4 function select_opts(Array $options, $selected = null, $echo = true) 4 /**
5 * @param array $options
6 * @param null $selected
7 * @param bool $echo
8 *
9 * @return string
10 */
11 function selectOpts(array $options, $selected = null, $echo = true)
5 { 12 {
6 $return = ''; 13 $return = '';
7 14
...@@ -23,40 +30,82 @@ function select_opts(Array $options, $selected = null, $echo = true) ...@@ -23,40 +30,82 @@ function select_opts(Array $options, $selected = null, $echo = true)
23 } 30 }
24 } 31 }
25 32
26 function select_opts_provinces($selected = null, $echo = true) 33 /**
34 * @param null $selected
35 *
36 * @return string
37 */
38 function selectOptsProvinces($selected = null)
27 { 39 {
28 $return = "<option>Select a State/Province</option>"; 40 $return = '<option>'.__('Select a State/Province', CBV_DOMAIN).'</option>';
29 41
30 $return .= '<optgroup label="Canada">'; 42 $return .= '<optgroup label="Canada">';
31 $return .= select_opts(Vars::$provinces['CA'], $selected, false); 43 $return .= selectOpts(Vars::$provinces['CA'], $selected, false);
32 $return .= '</optgroup>'; 44 $return .= '</optgroup>';
33 45
34 $return .= '<optgroup label="United States">'; 46 $return .= '<optgroup label="United States">';
35 $return .= select_opts(Vars::$provinces['US'], $selected, false); 47 $return .= selectOpts(Vars::$provinces['US'], $selected, false);
36 $return .= '</optgroup>'; 48 $return .= '</optgroup>';
37 49
38 $return .= '<optgroup label="Other">'; 50 $return .= '<optgroup label="Other">';
39 $return .= '<option value="other">Outside Canada/USA</option>'; 51 $return .= '<option value="other">'.__('Outside Canada/USA', CBV_DOMAIN).'</option>';
40 $return .= '</optgroup>'; 52 $return .= '</optgroup>';
41 53
42 return $return; 54 return $return;
43 } 55 }
44 56
45 function select_opts_cards($selected = null, $echo = true) 57 /**
58 * @param null $selected
59 * @param bool $echo
60 *
61 * @return string
62 */
63 function selectOptsCards($selected = null, $echo = true)
46 { 64 {
47 return select_opts(Vars::$creditcards, $selected, $echo); 65 $cards = array_map(
66 function ($card) {
67 return tz_($card);
68 },
69 Vars::$creditcards
70 );
71
72 return selectOpts($cards, $selected, $echo);
48 } 73 }
49 74
50 function select_opts_states($selected = null, $echo = true) 75 /**
76 * @param null $selected
77 * @param bool $echo
78 *
79 * @return string
80 */
81 function selectOptsStates($selected = null, $echo = true)
51 { 82 {
52 return select_opts(Vars::$states, $selected, $echo); 83 return selectOpts(Vars::$states, $selected, $echo);
53 } 84 }
54 85
55 function select_opts_prefixes($selected = null, $echo = true) 86 /**
87 * @param null $selected
88 * @param bool $echo
89 *
90 * @return string
91 */
92 function selectOptsPrefixes($selected = null, $echo = true)
56 { 93 {
57 return select_opts(Vars::$prefixes, $selected, $echo); 94 $prefixes = array_map(
95 function ($prefix) {
96 return tz_($prefix);
97 },
98 Vars::$prefixes
99 );
100
101 return selectOpts($prefixes, $selected, $echo);
58 } 102 }
59 103
104 /**
105 * @param null $selected
106 *
107 * @return string
108 */
60 function selectOptsPrefixesNoBlank($selected = null) 109 function selectOptsPrefixesNoBlank($selected = null)
61 { 110 {
62 $return = ''; 111 $return = '';
...@@ -77,16 +126,42 @@ function selectOptsPrefixesNoBlank($selected = null) ...@@ -77,16 +126,42 @@ function selectOptsPrefixesNoBlank($selected = null)
77 return $return; 126 return $return;
78 } 127 }
79 128
80 function select_opts_countries($selected = null, $echo = true) 129 /**
130 * @param null $selected
131 * @param bool $echo
132 *
133 * @return string
134 */
135 function selectOptsCountries($selected = null, $echo = true)
81 { 136 {
82 return select_opts(Vars::$countries, $selected, $echo); 137 return selectOpts(Vars::$countries, $selected, $echo);
83 } 138 }
84 139
85 function select_opts_securities($selected = null, $echo = true) 140 /**
141 * @param null $selected
142 * @param bool $echo
143 *
144 * @return string
145 */
146 function selectOptsSecurities($selected = null, $echo = true)
86 { 147 {
87 return select_opts(Vars::$securities, $selected, $echo); 148 $securities = array_map(
149 function ($security) {
150 return tz_($security);
151 },
152 Vars::$securities
153 );
154
155 return selectOpts($securities, $selected, $echo);
88 } 156 }
89 157
158 /**
159 * @param $date
160 * @param string $format
161 *
162 * @return bool
163 * @throws \Exception
164 */
90 function validateDate($date, $format = 'YYYY-MM-DD') 165 function validateDate($date, $format = 'YYYY-MM-DD')
91 { 166 {
92 switch ($format) { 167 switch ($format) {
...@@ -123,34 +198,32 @@ function validateDate($date, $format = 'YYYY-MM-DD') ...@@ -123,34 +198,32 @@ function validateDate($date, $format = 'YYYY-MM-DD')
123 break; 198 break;
124 199
125 default: 200 default:
126 throw new Exception("Invalid Date Format"); 201 throw new \Exception('Invalid Date Format');
127 } 202 }
128 203
129 return checkdate($m, $d, $y); 204 return checkdate($m, $d, $y);
130
131
132 } 205 }
133 206
134 function validate_creditcard($cc_num, $type) 207 function validateCreditcard($ccNum, $type)
135 { 208 {
136 209
137 if ($type == "amex") { 210 if ($type == "amex") {
138 $pattern = "/^([34|37]{2})([0-9]{13})$/"; //American Express 211 $pattern = "/^([34|37]{2})([0-9]{13})$/"; //American Express
139 if (preg_match($pattern, $cc_num)) { 212 if (preg_match($pattern, $ccNum)) {
140 $verified = true; 213 $verified = true;
141 } else { 214 } else {
142 $verified = false; 215 $verified = false;
143 } 216 }
144 } elseif ($type == "mc") { 217 } elseif ($type == "mc") {
145 $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard 218 $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard
146 if (preg_match($pattern, $cc_num)) { 219 if (preg_match($pattern, $ccNum)) {
147 $verified = true; 220 $verified = true;
148 } else { 221 } else {
149 $verified = false; 222 $verified = false;
150 } 223 }
151 } elseif ($type == "visa") { 224 } elseif ($type == "visa") {
152 $pattern = "/^([4]{1})([0-9]{12,15})$/"; //Visa 225 $pattern = "/^([4]{1})([0-9]{12,15})$/"; //Visa
153 if (preg_match($pattern, $cc_num)) { 226 if (preg_match($pattern, $ccNum)) {
154 $verified = true; 227 $verified = true;
155 } else { 228 } else {
156 $verified = false; 229 $verified = false;
...@@ -248,7 +321,7 @@ function form_linked_dropdown( ...@@ -248,7 +321,7 @@ function form_linked_dropdown(
248 jQuery("#'.$name.'").show(); 321 jQuery("#'.$name.'").show();
249 322
250 323
251 var options = new Array(); 324 var options = new [];
252 var select = document.getElementById("'.$name.'"); 325 var select = document.getElementById("'.$name.'");
253 select.disabled = false; 326 select.disabled = false;
254 while (select.firstChild) 327 while (select.firstChild)
...@@ -268,7 +341,7 @@ function form_linked_dropdown( ...@@ -268,7 +341,7 @@ function form_linked_dropdown(
268 select.disabled = true; 341 select.disabled = true;
269 342
270 jQuery("#'.$name.'").after("<input type=\'text\' name=\''.$name.'\' id=\''.$name 343 jQuery("#'.$name.'").after("<input type=\'text\' name=\''.$name.'\' id=\''.$name
271 .'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' />").removeAttr("disabled").show(); 344 .'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' />").removeAttr("disabled").show();
272 jQuery("#'.$name.'").hide(); 345 jQuery("#'.$name.'").hide();
273 //options = new Array("other","Other"); 346 //options = new Array("other","Other");
274 return; 347 return;
...@@ -349,7 +422,7 @@ if (!function_exists('formLinkedDropdownWithExtra')) { ...@@ -349,7 +422,7 @@ if (!function_exists('formLinkedDropdownWithExtra')) {
349 jQuery("#'.$name.'_custom").attr("disabled", "disabled").hide(); 422 jQuery("#'.$name.'_custom").attr("disabled", "disabled").hide();
350 jQuery("#'.$name.'").show(); 423 jQuery("#'.$name.'").show();
351 424
352 var options = new Array(); 425 var options =[]ray();
353 var select = document.getElementById("'.$name.'"); 426 var select = document.getElementById("'.$name.'");
354 select.disabled = false; 427 select.disabled = false;
355 while (select.firstChild) 428 while (select.firstChild)
...@@ -396,7 +469,7 @@ if (!function_exists('formLinkedDropdownWithExtra')) { ...@@ -396,7 +469,7 @@ if (!function_exists('formLinkedDropdownWithExtra')) {
396 } 469 }
397 470
398 var elem = $("<input type=\'text\' name=\''.$name.'\' id=\''.$name 471 var elem = $("<input type=\'text\' name=\''.$name.'\' id=\''.$name
399 .'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' '.$inputExtra.'/>").hide(); 472 .'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' '.$inputExtra.'/>").hide();
400 elem.insertAfter(jQuery("#'.$name.'")); 473 elem.insertAfter(jQuery("#'.$name.'"));
401 update_'.$name.'(document.getElementById("'.$parent.'").value); 474 update_'.$name.'(document.getElementById("'.$parent.'").value);
402 document.getElementById("'.$parent.'").onchange = function() { update_'.$name.'(this.value); } 475 document.getElementById("'.$parent.'").onchange = function() { update_'.$name.'(this.value); }
...@@ -451,420 +524,3 @@ if (!function_exists('url_title')) { ...@@ -451,420 +524,3 @@ if (!function_exists('url_title')) {
451 return trim(stripslashes($str)); 524 return trim(stripslashes($str));
452 } 525 }
453 } 526 }
454
455 class Vars
456 {
457 public static $prefixes = [
458 '' => '-- select --',
459 'Mr.' => 'Mr.',
460 'Mrs.' => 'Mrs.',
461 'Ms.' => 'Ms.',
462 'Miss' => 'Miss',
463 'Master' => 'Master',
464 'Dr.' => 'Dr.',
465 'Prof.' => 'Prof.'
466 ];
467
468 public static $company_types = [
469 'advisory_firm' => "Advisory Firm",
470 'litigation_firm' => "Litigation Firm",
471 'venture_capital_firm' => "Venture Capital Firm",
472 'public_practice' => "Public Practice",
473 'government' => "Government",
474 '' => "None",
475 'accounting_firm' => "Accounting Firm",
476 'special_valuation' => "Special Valuation",
477 'investment_bank' => "Investment Bank",
478 'academia' => "Academia",
479 'financial_institution' => "Financial Institution",
480 'pension_fund_co' => "Pension Fund Management Company",
481 'mutual_fund_co' => "Mutual Fund Management Company",
482 'merchant_bank' => "Merchant Bank"
483 ];
484
485 public static $countries = [
486 'CA' => 'Canada',
487 'US' => 'United States',
488 'MX' => 'Mexico',
489 'AF' => 'Afghanistan',
490 'AR' => 'Argentina',
491 'AX' => 'Åland Islands',
492 'AL' => 'Albania',
493 'DZ' => 'Algeria',
494 'AS' => 'American Samoa',
495 'AD' => 'Andorra',
496 'AO' => 'Angola',
497 'AI' => 'Anguilla',
498 'AQ' => 'Antarctica',
499 'AG' => 'Antigua and Barbuda',
500 'AM' => 'Armenia',
501 'AW' => 'Aruba',
502 'AU' => 'Australia',
503 'AT' => 'Austria',
504 'AZ' => 'Azerbaijan',
505 'BS' => 'Bahamas',
506 'BH' => 'Bahrain',
507 'BD' => 'Bangladesh',
508 'BB' => 'Barbados',
509 'BY' => 'Belarus',
510 'BE' => 'Belgium',
511 'BZ' => 'Belize',
512 'BJ' => 'Benin',
513 'BM' => 'Bermuda',
514 'BT' => 'Bhutan',
515 'BO' => 'Bolivia',
516 'BA' => 'Bosnia and Herzegovina',
517 'BW' => 'Botswana',
518 'BV' => 'Bouvet Island',
519 'BR' => 'Brazil',
520 'IO' => 'British Indian Ocean Territory',
521 'BN' => 'Brunei Darussalam',
522 'BG' => 'Bulgaria',
523 'BF' => 'Burkina Faso',
524 'BI' => 'Burundi',
525 'KH' => 'Cambodia',
526 'CM' => 'Cameroon',
527 'CV' => 'Cape Verde',
528 'KY' => 'Cayman Islands',
529 'CF' => 'Central African Republic',
530 'TD' => 'Chad',
531 'CL' => 'Chile',
532 'CN' => 'China',
533 'CX' => 'Christmas Island',
534 'CC' => 'Cocos (Keeling) Islands',
535 'CO' => 'Columbia',
536 'KM' => 'Comoros',
537 'CG' => 'Congo',
538 'CD' => 'Congo, The Democratic Republic',
539 'CK' => 'Cook Islands',
540 'CR' => 'Costa Rica',
541 'CI' => 'Cote d’Ivoire – Really Ivory Coast',
542 'HR' => 'Croatia',
543 'CU' => 'Cuba',
544 'CY' => 'Cyprus',
545 'CZ' => 'Czech Republic',
546 'DK' => 'Denmark',
547 'DJ' => 'Djibouti',
548 'DM' => 'Dominica',
549 'DO' => 'Dominican Republic',
550 'TL' => 'East Timor',
551 'EC' => 'Ecuador',
552 'EG' => 'Egypt',
553 'SV' => 'El Salvador',
554 'GQ' => 'Equatorial Guinea',
555 'ER' => 'Eritrea',
556 'EE' => 'Estonia',
557 'ET' => 'Ethiopia',
558 'FK' => 'Falkland Islands',
559 'FO' => 'Faroe Islands',
560 'FJ' => 'Fiji',
561 'FI' => 'Finland',
562 'FR' => 'France',
563 'GF' => 'French Guiana',
564 'PF' => 'French Polynesia',
565 'TF' => 'French Southern Territories',
566 'GA' => 'Gabon',
567 'GM' => 'Gambia',
568 'GE' => 'Georgia',
569 'DE' => 'Germany',
570 'GH' => 'Ghana',
571 'GI' => 'Gibraltar',
572 'GB' => 'Great Britain',
573 'GR' => 'Greece',
574 'GL' => 'Greenland',
575 'GD' => 'Grenada',
576 'GP' => 'Guadeloupe',
577 'GU' => 'Guam',
578 'GT' => 'Guatemala',
579 'GN' => 'Guinea',
580 'GW' => 'Guinea Bissau',
581 'GY' => 'Guyana',
582 'HT' => 'Haiti',
583 'HM' => 'Heard and McDonald Islands',
584 'HN' => 'Honduras',
585 'HK' => 'Hong Kong',
586 'HU' => 'Hungary',
587 'IS' => 'Iceland',
588 'IN' => 'India',
589 'ID' => 'Indonesia',
590 'IR' => 'Iran, Islamic Republic of',
591 'IQ' => 'Iraq',
592 'IE' => 'Ireland',
593 'IL' => 'Israel',
594 'IT' => 'Italy',
595 'JM' => 'Jamaica',
596 'JP' => 'Japan',
597 'JO' => 'Jordan',
598 'KZ' => 'Kazakhstan',
599 'KE' => 'Kenya',
600 'KI' => 'Kiribati',
601 'KP' => "Korea, Democratic People's Republic",
602 'KR' => 'Korea, Republic of',
603 'KW' => 'Kuwait',
604 'KG' => 'Kyrgyzstan',
605 'LA' => "Lao People's Democratic Republic",
606 'LV' => 'Latvia',
607 'LB' => 'Lebanon',
608 'LI' => 'Liechtenstein',
609 'LS' => 'Lesotho',
610 'LR' => 'Liberia',
611 'LY' => 'Libyan Arab Jamahiriya',
612 'LT' => 'Lithuania',
613 'LU' => 'Luxembourg',
614 'MO' => 'Macau',
615 'MK' => 'Macedonia',
616 'MG' => 'Madagascar',
617 'MW' => 'Malawi',
618 'MY' => 'Malaysia',
619 'MV' => 'Maldives',
620 'ML' => 'Mali',
621 'MT' => 'Malta',
622 'MH' => 'Marshall Islands',
623 'MQ' => 'Martinique',
624 'MR' => 'Mauritania',
625 'MU' => 'Mauritius',
626 'YT' => 'Mayotte',
627 'FM' => "Micronesia, Federated States of",
628 'MD' => 'Moldova, Republic of',
629 'MC' => 'Monaco',
630 'MN' => 'Mongolia',
631 'MS' => 'Montserrat',
632 'MA' => 'Morocco',
633 'MZ' => "Mozambique",
634 'MM' => 'Myanmar',
635 'NA' => 'Namibia',
636 'NR' => 'Nauru',
637 'NP' => 'Nepal',
638 'NL' => 'Netherlands',
639 'AN' => 'Netherlands Antilles',
640 'NC' => 'New Caledonia',
641 'NZ' => 'New Zealand',
642 'NI' => 'Nicaragua',
643 'NE' => 'Niger',
644 'NG' => 'Nigeria',
645 'NU' => 'Niue',
646 'NF' => 'Norfolk Island',
647 'MP' => 'Northern Mariana Islands',
648 'NO' => "Norway",
649 'OM' => 'Oman',
650 'PK' => 'Pakistan',
651 'PW' => 'Palau',
652 'PS' => 'Palestinian Territory, Occupied',
653 'PA' => 'Panama',
654 'PG' => 'Papua New Guinea',
655 'PY' => 'Paraguay',
656 'PE' => 'Peru',
657 'PH' => 'Philippines',
658 'PN' => 'Pitcairn',
659 'PL' => 'Poland',
660 'PT' => 'Portugal',
661 'PR' => 'Puerto Rico',
662 'QA' => 'Qatar',
663 'RE' => 'Reunion',
664 'RO' => 'Romania',
665 'RU' => 'Russian Federation',
666 'RW' => 'Rwanda',
667 'KN' => 'Saint Kitts and Nevis',
668 'LC' => 'Saint Lucia',
669 'VC' => 'Saint Vincent and the Grenadines',
670 'WS' => 'Samoa',
671 'SM' => 'San Marino',
672 'ST' => 'Sao Tome and Principe',
673 'SA' => 'Saudi Arabia',
674 'SN' => 'Senegal',
675 'CS' => 'Serbia and Montenegro',
676 'SC' => 'Seychelles',
677 'SL' => 'Sierra Leone',
678 'SG' => 'Singapore',
679 'SK' => 'Slovakia',
680 'SI' => 'Slovenia',
681 'SB' => 'Solomon Islands',
682 'SO' => 'Somalia',
683 'ZA' => 'South Africa',
684 'GS' => 'South Georgia – South Sandwich Islands',
685 'ES' => 'Spain',
686 'LK' => 'Sri Lanka',
687 'SH' => 'St. Helena',
688 'PM' => 'St. Pierre and Miquelon',
689 'SD' => 'Sudan',
690 'SR' => 'Suriname',
691 'SJ' => 'Svalbard and Jan Mayen',
692 'SZ' => 'Swaziland',
693 'SE' => 'Sweden',
694 'CH' => 'Switzerland',
695 'SY' => 'Syrian Arab Republic',
696 'TW' => 'Taiwan',
697 'TJ' => 'Tajikistan',
698 'TZ' => 'Tanzania, United Republic of',
699 'TH' => 'Thailand',
700 'TG' => 'Togo',
701 'TK' => 'Tokelau',
702 'TO' => 'Tonga',
703 'TT' => 'Trinidad and Tobago',
704 'TN' => 'Tunisia',
705 'TR' => 'Turkey',
706 'TM' => 'Turkmenistan',
707 'TC' => 'Turks and Caicos Islands',
708 'TV' => 'Tuvalu',
709 'UG' => 'Uganda',
710 'UA' => 'Ukraine',
711 'AE' => 'United Arab Emirates',
712 'UM' => 'United States Minor Outlying Islands',
713 'UY' => 'Uruguay',
714 'UZ' => 'Uzbekistan',
715 'VU' => 'Vanuatu',
716 'VA' => 'Vatican City state',
717 'VE' => 'Venezuela',
718 'VN' => 'Viet Nam',
719 'VG' => 'Virgin Islands (British)',
720 'VI' => 'Virgin Islands (US)',
721 'WF' => 'Wallis and Futuna',
722 'EH' => 'Western Sahara',
723 'YE' => 'Yemen',
724 'ZM' => 'Zambia',
725 'ZW' => 'Zimbabwe'
726 ];
727
728 public static $provinces = [
729 'CA' => [
730 'AB' => 'Alberta',
731 'BC' => 'British Columbia',
732 'MB' => 'Manitoba',
733 'NB' => 'New Brunswick',
734 'NL' => 'Newfoundland and Labrador',
735 'NT' => 'Northwest Territories',
736 'NS' => 'Nova Scotia',
737 'NU' => 'Nunavut',
738 'PE' => 'Prince Edward Island',
739 'SK' => 'Saskatchewan',
740 'ON' => 'Ontario',
741 'QC' => 'Quebec',
742 'YT' => 'Yukon'
743 ],
744 'US' => [
745 'AL' => 'Alabama',
746 'AK' => 'Alaska',
747 'AZ' => 'Arizona',
748 'AR' => 'Arkansas',
749 'CA' => 'California',
750 'CO' => 'Colorado',
751 'CT' => 'Connecticut',
752 'DE' => 'Delaware',
753 'DC' => 'District of Columbia',
754 'FL' => 'Florida',
755 'GA' => 'Georgia',
756 'GU' => 'Guam',
757 'HI' => 'Hawaii',
758 'ID' => 'Idaho',
759 'IL' => 'Illinois',
760 'IN' => 'Indiana',
761 'IA' => 'Iowa',
762 'KS' => 'Kansas',
763 'KY' => 'Kentucky',
764 'LA' => 'Louisiana',
765 'ME' => 'Maine',
766 'MD' => 'Maryland',
767 'MA' => 'Massachusetts',
768 'MI' => 'Michigan',
769 'MN' => 'Minnesota',
770 'MS' => 'Mississippi',
771 'MO' => 'Missouri',
772 'MT' => 'Montana',
773 'NE' => 'Nebraska',
774 'NV' => 'Nevada',
775 'NH' => 'New Hampshire',
776 'NJ' => 'New Jersey',
777 'NM' => 'New Mexico',
778 'NY' => 'New York',
779 'NC' => 'North Carolina',
780 'ND' => 'North Dakota',
781 'OH' => 'Ohio',
782 'OK' => 'Oklahoma',
783 'OR' => 'Oregon',
784 'PA' => 'Pennsylvania',
785 'PR' => 'Puerto Rico',
786 'RI' => 'Rhode Island',
787 'SC' => 'South Carolina',
788 'SD' => 'South Dakota',
789 'TN' => 'Tennessee',
790 'TX' => 'Texas',
791 'UT' => 'Utah',
792 'VT' => 'Vermont',
793 'VA' => 'Virginia',
794 'VI' => 'Virgin Islands',
795 'WA' => 'Washington',
796 'WV' => 'West Virginia',
797 'WI' => 'Wisconsin',
798 'WY' => 'Wyoming'
799 ]
800 ];
801
802 public static $securities = [
803 'S01' => "What is your Mother's maiden name?",
804 'S02' => "Who was you childhood hero?",
805 'S03' => "What is/was the name of your first pet?"
806 ];
807
808 public static $creditcards = [
809 'visa' => "Visa",
810 'mc' => "MasterCard",
811 'amex' => "American Express"
812 ];
813
814 public static $states = [
815 'AL' => 'Alabama',
816 'AK' => 'Alaska',
817 'AZ' => 'Arizona',
818 'AR' => 'Arkansas',
819 'CA' => 'California',
820 'CO' => 'Colorado',
821 'CT' => 'Connecticut',
822 'DE' => 'Delaware',
823 'DC' => 'District of Columbia',
824 'FL' => 'Florida',
825 'GA' => 'Georgia',
826 'GU' => 'Guam',
827 'HI' => 'Hawaii',
828 'ID' => 'Idaho',
829 'IL' => 'Illinois',
830 'IN' => 'Indiana',
831 'IA' => 'Iowa',
832 'KS' => 'Kansas',
833 'KY' => 'Kentucky',
834 'LA' => 'Louisiana',
835 'ME' => 'Maine',
836 'MD' => 'Maryland',
837 'MA' => 'Massachusetts',
838 'MI' => 'Michigan',
839 'MN' => 'Minnesota',
840 'MS' => 'Mississippi',
841 'MO' => 'Missouri',
842 'MT' => 'Montana',
843 'NE' => 'Nebraska',
844 'NV' => 'Nevada',
845 'NH' => 'New Hampshire',
846 'NJ' => 'New Jersey',
847 'NM' => 'New Mexico',
848 'NY' => 'New York',
849 'NC' => 'North Carolina',
850 'ND' => 'North Dakota',
851 'OH' => 'Ohio',
852 'OK' => 'Oklahoma',
853 'OR' => 'Oregon',
854 'PA' => 'Pennyslvania',
855 'PR' => 'Puerto Rico',
856 'RI' => 'Rhode Island',
857 'SC' => 'South Carolina',
858 'SD' => 'South Dakota',
859 'TN' => 'Tennessee',
860 'TX' => 'Texas',
861 'UT' => 'Utah',
862 'VT' => 'Vermont',
863 'VA' => 'Virginia',
864 'VI' => 'Virgin Islands',
865 'WA' => 'Washington',
866 'WV' => 'West Virginia',
867 'WI' => 'Wisconsin',
868 'WY' => 'Wyoming'
869 ];
870 }
......
1 <?php 1 <?php
2 namespace Tz\WordPress\Tools\Notifications; 2 namespace Tz\WordPress\Tools\Notifications;
3 error_reporting(E_ALL^E_DEPRECATED); 3 error_reporting(E_ALL ^ E_DEPRECATED);
4 4
5 use Tz\WordPress\Tools;
6 use Tz\Common; 5 use Tz\Common;
6 use Tz\WordPress\Tools;
7 use Tz\WordPress\Tools\Notifications; 7 use Tz\WordPress\Tools\Notifications;
8 8
9 class Validation
10 {
11
12 protected $_field_data = [];
13 protected $_config_rules = [];
14 protected $_error_array = [];
15 protected $_error_messages = [];
16 protected $_error_prefix = '<div class="lblError">';
17 protected $_error_suffix = '</div>';
18 protected $error_string = '';
19 protected $_safe_form_data = false;
20
21 function __construct($rules = [])
22 {
23 // Validation rules can be stored in a config file.
24 $this->_config_rules = $rules;
25 }
26
27 public function get_error_message($key)
28 {
29 $lang = [];
30 $lang['required'] = "The %s field is required.";
31 $lang['isset'] = "The %s field must have a value.";
32 $lang['valid_email'] = "The %s field must contain a valid email address.";
33 $lang['valid_emails'] = "The %s field must contain all valid email addresses.";
34 $lang['valid_url'] = "The %s field must contain a valid URL.";
35 $lang['valid_ip'] = "The %s field must contain a valid IP.";
36 $lang['min_length'] = "The %s field must be at least %s characters in length.";
37 $lang['max_length'] = "The %s field can not exceed %s characters in length.";
38 $lang['exact_length'] = "The %s field must be exactly %s characters in length.";
39 $lang['alpha'] = "The %s field may only contain alphabetical characters.";
40 $lang['alpha_numeric'] = "The %s field may only contain alpha-numeric characters.";
41 $lang['alpha_dash'] = "The %s field may only contain alpha-numeric characters, underscores, and dashes.";
42 $lang['numeric'] = "The %s field must contain only numbers.";
43 $lang['is_numeric'] = "The %s field must contain only numeric characters.";
44 $lang['integer'] = "The %s field must contain an integer.";
45 $lang['matches'] = "The %s field does not match the %s field.";
46 $lang['is_natural'] = "The %s field must contain only positive numbers.";
47 $lang['is_natural_no_zero'] = "The %s field must contain a number greater than zero.";
48
49 if (isset($lang[$key])) {
50 return $lang[$key];
51 } else {
52 return false;
53 }
54 }
55
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Set Rules
61 *
62 * This function takes an array of field names and validation
63 * rules as input, validates the info, and stores it
64 *
65 * @access public
66 *
67 * @param mixed
68 * @param string
69 *
70 * @return void
71 */
72 function set_rules($field, $label = '', $rules = '')
73 {
74 // No reason to set rules if we have no POST data
75 if (count($_POST) == 0) {
76 return;
77 }
78
79 // If an array was passed via the first parameter instead of indidual string
80 // values we cycle through it and recursively call this function.
81 if (is_array($field)) {
82 foreach ($field as $row) {
83 // Houston, we have a problem...
84 if (!isset($row['field']) OR !isset($row['rules'])) {
85 continue;
86 }
87
88 // If the field label wasn't passed we use the field name
89 $label = (!isset($row['label'])) ? $row['field'] : $row['label'];
90
91 // Here we go!
92 $this->set_rules($row['field'], $label, $row['rules']);
93 }
94
95 return;
96 }
97
98 // No fields? Nothing to do...
99 if (!is_string($field) OR !is_string($rules) OR $field == '') {
100 return;
101 }
102
103 // If the field label wasn't passed we use the field name
104 $label = ($label == '') ? $field : $label;
105
106 // Is the field name an array? We test for the existence of a bracket "[" in
107 // the field name to determine this. If it is an array, we break it apart
108 // into its components so that we can fetch the corresponding POST data later
109 if (strpos($field, '[') !== false AND preg_match_all('/\[(.*?)\]/', $field, $matches)) {
110 // Note: Due to a bug in current() that affects some versions
111 // of PHP we can not pass function call directly into it
112 $x = explode('[', $field);
113 $indexes[] = current($x);
114
115 for ($i = 0; $i < count($matches['0']); $i++) {
116 if ($matches['1'][$i] != '') {
117 $indexes[] = $matches['1'][$i];
118 }
119 }
120
121 $is_array = true;
122 } else {
123 $indexes = [];
124 $is_array = false;
125 }
126
127 // Build our master array
128 $this->_field_data[$field] = [
129 'field' => $field,
130 'label' => $label,
131 'rules' => $rules,
132 'is_array' => $is_array,
133 'keys' => $indexes,
134 'postdata' => null,
135 'error' => ''
136 ];
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Set Error Message
143 *
144 * Lets users set their own error messages on the fly. Note: The key
145 * name has to match the function name that it corresponds to.
146 *
147 * @access public
148 *
149 * @param string
150 * @param string
151 *
152 * @return string
153 */
154 function set_message($lang, $val = '')
155 {
156 if (!is_array($lang)) {
157 $lang = [$lang => $val];
158 }
159
160 $this->_error_messages = array_merge($this->_error_messages, $lang);
161 }
9 162
10 class Validation { 163 // --------------------------------------------------------------------
11 164
12 protected $_field_data = array(); 165 /**
13 protected $_config_rules = array(); 166 * Set The Error Delimiter
14 protected $_error_array = array(); 167 *
15 protected $_error_messages = array(); 168 * Permits a prefix/suffix to be added to each error message
16 protected $_error_prefix = '<div class="lblError">'; 169 *
17 protected $_error_suffix = '</div>'; 170 * @access public
18 protected $error_string = ''; 171 *
19 protected $_safe_form_data = FALSE; 172 * @param string
20 173 * @param string
21 function __construct($rules = array()) 174 *
22 { 175 * @return void
23 // Validation rules can be stored in a config file. 176 */
24 $this->_config_rules = $rules; 177 function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
25 } 178 {
26 179 $this->_error_prefix = $prefix;
27 public function get_error_message($key) { 180 $this->_error_suffix = $suffix;
28 $lang = array(); 181 }
29 $lang['required'] = "The %s field is required."; 182
30 $lang['isset'] = "The %s field must have a value."; 183 // --------------------------------------------------------------------
31 $lang['valid_email'] = "The %s field must contain a valid email address."; 184
32 $lang['valid_emails'] = "The %s field must contain all valid email addresses."; 185 /**
33 $lang['valid_url'] = "The %s field must contain a valid URL."; 186 * Get Error Message
34 $lang['valid_ip'] = "The %s field must contain a valid IP."; 187 *
35 $lang['min_length'] = "The %s field must be at least %s characters in length."; 188 * Gets the error message associated with a particular field
36 $lang['max_length'] = "The %s field can not exceed %s characters in length."; 189 *
37 $lang['exact_length'] = "The %s field must be exactly %s characters in length."; 190 * @access public
38 $lang['alpha'] = "The %s field may only contain alphabetical characters."; 191 *
39 $lang['alpha_numeric'] = "The %s field may only contain alpha-numeric characters."; 192 * @param string the field name
40 $lang['alpha_dash'] = "The %s field may only contain alpha-numeric characters, underscores, and dashes."; 193 *
41 $lang['numeric'] = "The %s field must contain only numbers."; 194 * @return void
42 $lang['is_numeric'] = "The %s field must contain only numeric characters."; 195 */
43 $lang['integer'] = "The %s field must contain an integer."; 196 function error($field = '', $prefix = '', $suffix = '')
44 $lang['matches'] = "The %s field does not match the %s field."; 197 {
45 $lang['is_natural'] = "The %s field must contain only positive numbers."; 198 if (!isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '') {
46 $lang['is_natural_no_zero'] = "The %s field must contain a number greater than zero."; 199 return '';
47 200 }
48 201
49 if (isset($lang[$key])) { 202 if ($prefix == '') {
50 return $lang[$key]; 203 $prefix = $this->_error_prefix;
51 } else { 204 }
52 return FALSE; 205
53 } 206 if ($suffix == '') {
54 207 $suffix = $this->_error_suffix;
55 } 208 }
56 209
57 210 return $prefix.$this->_field_data[$field]['error'].$suffix;
58 // -------------------------------------------------------------------- 211 }
59 212
60 /** 213 // --------------------------------------------------------------------
61 * Set Rules 214
62 * 215 /**
63 * This function takes an array of field names and validation 216 * Error String
64 * rules as input, validates the info, and stores it 217 *
65 * 218 * Returns the error messages as a string, wrapped in the error delimiters
66 * @access public 219 *
67 * @param mixed 220 * @access public
68 * @param string 221 *
69 * @return void 222 * @param string
70 */ 223 * @param string
71 function set_rules($field, $label = '', $rules = '') 224 *
72 { 225 * @return str
73 // No reason to set rules if we have no POST data 226 */
74 if (count($_POST) == 0) 227 function error_string($prefix = '', $suffix = '')
75 { 228 {
76 return; 229 // No errrors, validation passes!
77 } 230 if (count($this->_error_array) === 0) {
78 231 return '';
79 // If an array was passed via the first parameter instead of indidual string 232 }
80 // values we cycle through it and recursively call this function. 233
81 if (is_array($field)) 234 if ($prefix == '') {
82 { 235 $prefix = $this->_error_prefix;
83 foreach ($field as $row) 236 }
84 { 237
85 // Houston, we have a problem... 238 if ($suffix == '') {
86 if ( ! isset($row['field']) OR ! isset($row['rules'])) 239 $suffix = $this->_error_suffix;
87 { 240 }
88 continue; 241
89 } 242 // Generate the error string
90 243 $str = '';
91 // If the field label wasn't passed we use the field name 244 foreach ($this->_error_array as $val) {
92 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label']; 245 if ($val != '') {
93 246 $str .= $prefix.$val.$suffix."\n";
94 // Here we go! 247 }
95 $this->set_rules($row['field'], $label, $row['rules']); 248 }
96 } 249
97 return; 250 return $str;
98 } 251 }
99 252
100 // No fields? Nothing to do... 253 // --------------------------------------------------------------------
101 if ( ! is_string($field) OR ! is_string($rules) OR $field == '') 254
102 { 255 /**
103 return; 256 * Run the Validator
104 } 257 *
105 258 * This function does all the work.
106 // If the field label wasn't passed we use the field name 259 *
107 $label = ($label == '') ? $field : $label; 260 * @access public
108 261 * @return bool
109 // Is the field name an array? We test for the existence of a bracket "[" in 262 */
110 // the field name to determine this. If it is an array, we break it apart 263 function run($group = '')
111 // into its components so that we can fetch the corresponding POST data later 264 {
112 if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches)) 265 // Do we even have any data to process? Mm?
113 { 266 if (count($_POST) == 0) {
114 // Note: Due to a bug in current() that affects some versions 267 return false;
115 // of PHP we can not pass function call directly into it 268 }
116 $x = explode('[', $field); 269
117 $indexes[] = current($x); 270 // Does the _field_data array containing the validation rules exist?
118 271 // If not, we look to see if they were assigned via a config file
119 for ($i = 0; $i < count($matches['0']); $i++) 272 if (count($this->_field_data) == 0) {
120 { 273 // No validation rules? We're done...
121 if ($matches['1'][$i] != '') 274 if (count($this->_config_rules) == 0) {
122 { 275 return false;
123 $indexes[] = $matches['1'][$i]; 276 }
124 } 277
125 } 278 // Is there a validation rule for the particular URI being accessed?
126 279 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
127 $is_array = TRUE; 280
128 } 281 if ($uri != '' AND isset($this->_config_rules[$uri])) {
129 else 282 $this->set_rules($this->_config_rules[$uri]);
130 { 283 } else {
131 $indexes = array(); 284 $this->set_rules($this->_config_rules);
132 $is_array = FALSE; 285 }
133 } 286
134 287 // We're we able to set the rules correctly?
135 // Build our master array 288 if (count($this->_field_data) == 0) {
136 $this->_field_data[$field] = array( 289 return false;
137 'field' => $field, 290 }
138 'label' => $label, 291 }
139 'rules' => $rules, 292
140 'is_array' => $is_array, 293 // Cycle through the rules for each field, match the
141 'keys' => $indexes, 294 // corresponding $_POST item and test for errors
142 'postdata' => NULL, 295 foreach ($this->_field_data as $field => $row) {
143 'error' => '' 296 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
144 ); 297 // Depending on whether the field name is an array or a string will determine where we get it from.
145 } 298
146 299 if ($row['is_array'] == true) {
147 // -------------------------------------------------------------------- 300 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
148 301 } else {
149 /** 302 if (isset($_POST[$field]) AND $_POST[$field] != "") {
150 * Set Error Message 303 $this->_field_data[$field]['postdata'] = $_POST[$field];
151 * 304 }
152 * Lets users set their own error messages on the fly. Note: The key 305 }
153 * name has to match the function name that it corresponds to. 306
154 * 307 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
155 * @access public 308 }
156 * @param string 309
157 * @param string 310 // Did we end up with any errors?
158 * @return string 311 $total_errors = count($this->_error_array);
159 */ 312
160 function set_message($lang, $val = '') 313 if ($total_errors > 0) {
161 { 314 $this->_safe_form_data = true;
162 if ( ! is_array($lang)) 315 }
163 { 316
164 $lang = array($lang => $val); 317 // Now we need to re-set the POST data with the new, processed data
165 } 318 $this->_reset_post_array();
166 319
167 $this->_error_messages = array_merge($this->_error_messages, $lang); 320 // No errors, validation passes!
168 } 321 if ($total_errors == 0) {
169 322 return true;
170 // -------------------------------------------------------------------- 323 }
171 324
172 /** 325 // Validation fails
173 * Set The Error Delimiter 326 return false;
174 * 327 }
175 * Permits a prefix/suffix to be added to each error message 328
176 * 329 // --------------------------------------------------------------------
177 * @access public 330
178 * @param string 331 /**
179 * @param string 332 * Traverse a multidimensional $_POST array index until the data is found
180 * @return void 333 *
181 */ 334 * @access private
182 function set_error_delimiters($prefix = '<p>', $suffix = '</p>') 335 *
183 { 336 * @param array
184 $this->_error_prefix = $prefix; 337 * @param array
185 $this->_error_suffix = $suffix; 338 * @param integer
186 } 339 *
187 340 * @return mixed
188 // -------------------------------------------------------------------- 341 */
189 342 function _reduce_array($array, $keys, $i = 0)
190 /** 343 {
191 * Get Error Message 344 if (is_array($array)) {
192 * 345 if (isset($keys[$i])) {
193 * Gets the error message associated with a particular field 346 if (isset($array[$keys[$i]])) {
194 * 347 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i + 1));
195 * @access public 348 } else {
196 * @param string the field name 349 return null;
197 * @return void 350 }
198 */ 351 } else {
199 function error($field = '', $prefix = '', $suffix = '') 352 return $array;
200 { 353 }
201 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '') 354 }
202 { 355
203 return ''; 356 return $array;
204 } 357 }
205 358
206 if ($prefix == '') 359 // --------------------------------------------------------------------
207 { 360
208 $prefix = $this->_error_prefix; 361 /**
209 } 362 * Re-populate the _POST array with our finalized and processed data
210 363 *
211 if ($suffix == '') 364 * @access private
212 { 365 * @return null
213 $suffix = $this->_error_suffix; 366 */
214 } 367 function _reset_post_array()
215 368 {
216 return $prefix.$this->_field_data[$field]['error'].$suffix; 369 foreach ($this->_field_data as $field => $row) {
217 } 370 if (!is_null($row['postdata'])) {
218 371 if ($row['is_array'] == false) {
219 // -------------------------------------------------------------------- 372 if (isset($_POST[$row['field']])) {
220 373 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
221 /** 374 }
222 * Error String 375 } else {
223 * 376 // start with a reference
224 * Returns the error messages as a string, wrapped in the error delimiters 377 $post_ref =& $_POST;
225 * 378
226 * @access public 379 // before we assign values, make a reference to the right POST key
227 * @param string 380 if (count($row['keys']) == 1) {
228 * @param string 381 $post_ref =& $post_ref[current($row['keys'])];
229 * @return str 382 } else {
230 */ 383 foreach ($row['keys'] as $val) {
231 function error_string($prefix = '', $suffix = '') 384 $post_ref =& $post_ref[$val];
232 { 385 }
233 // No errrors, validation passes! 386 }
234 if (count($this->_error_array) === 0) 387
235 { 388 if (is_array($row['postdata'])) {
236 return ''; 389 $array = [];
237 } 390 foreach ($row['postdata'] as $k => $v) {
238 391 $array[$k] = $this->prep_for_form($v);
239 if ($prefix == '') 392 }
240 { 393
241 $prefix = $this->_error_prefix; 394 $post_ref = $array;
242 } 395 } else {
243 396 $post_ref = $this->prep_for_form($row['postdata']);
244 if ($suffix == '') 397 }
245 { 398 }
246 $suffix = $this->_error_suffix; 399 }
247 } 400 }
248 401 }
249 // Generate the error string 402
250 $str = ''; 403 // --------------------------------------------------------------------
251 foreach ($this->_error_array as $val) 404
252 { 405 /**
253 if ($val != '') 406 * Executes the Validation routines
254 { 407 *
255 $str .= $prefix.$val.$suffix."\n"; 408 * @access private
256 } 409 *
257 } 410 * @param array
258 411 * @param array
259 return $str; 412 * @param mixed
260 } 413 * @param integer
261 414 *
262 // -------------------------------------------------------------------- 415 * @return mixed
263 416 */
264 /** 417 function _execute($row, $rules, $postdata = null, $cycles = 0)
265 * Run the Validator 418 {
266 * 419 // If the $_POST data is an array we will run a recursive call
267 * This function does all the work. 420 if (is_array($postdata)) {
268 * 421 foreach ($postdata as $key => $val) {
269 * @access public 422 $this->_execute($row, $rules, $val, $cycles);
270 * @return bool 423 $cycles++;
271 */ 424 }
272 function run($group = '') 425
273 { 426 return;
274 // Do we even have any data to process? Mm? 427 }
275 if (count($_POST) == 0) 428
276 { 429 // --------------------------------------------------------------------
277 return FALSE; 430
278 } 431 // If the field is blank, but NOT required, no further tests are necessary
279 432 $callback = false;
280 // Does the _field_data array containing the validation rules exist? 433 if (!in_array('required', $rules) AND is_null($postdata)) {
281 // If not, we look to see if they were assigned via a config file 434 // Before we bail out, does the rule contain a callback?
282 if (count($this->_field_data) == 0) 435 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match)) {
283 { 436 $callback = true;
284 // No validation rules? We're done... 437 $rules = (['1' => $match[1]]);
285 if (count($this->_config_rules) == 0) 438 } else {
286 { 439 return;
287 return FALSE; 440 }
288 } 441 }
289 442
290 // Is there a validation rule for the particular URI being accessed? 443 // --------------------------------------------------------------------
291 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group; 444
292 445 // Isset Test. Typically this rule will only apply to checkboxes.
293 if ($uri != '' AND isset($this->_config_rules[$uri])) 446 if (is_null($postdata) AND $callback == false) {
294 { 447 if (in_array('isset', $rules, true) OR in_array('required', $rules)) {
295 $this->set_rules($this->_config_rules[$uri]); 448 // Set the message type
296 } 449 $type = (in_array('required', $rules)) ? 'required' : 'isset';
297 else 450
298 { 451 if (!isset($this->_error_messages[$type])) {
299 $this->set_rules($this->_config_rules); 452 if (false === ($line = $this->get_error_message($type))) {
300 } 453 $line = 'The field was not set';
301 454 }
302 // We're we able to set the rules correctly? 455 } else {
303 if (count($this->_field_data) == 0) 456 $line = $this->_error_messages[$type];
304 { 457 }
305 return FALSE; 458
306 } 459 // Build the error message
307 } 460 $message = sprintf($line, $this->_translate_fieldname($row['label']));
308 461
309 // Cycle through the rules for each field, match the 462 // Save the error message
310 // corresponding $_POST item and test for errors 463 $this->_field_data[$row['field']]['error'] = $message;
311 foreach ($this->_field_data as $field => $row) 464
312 { 465 if (!isset($this->_error_array[$row['field']])) {
313 // Fetch the data from the corresponding $_POST array and cache it in the _field_data array. 466 $this->_error_array[$row['field']] = $message;
314 // Depending on whether the field name is an array or a string will determine where we get it from. 467 }
315 468 }
316 if ($row['is_array'] == TRUE) 469
317 { 470 return;
318 $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']); 471 }
319 } 472
320 else 473 // --------------------------------------------------------------------
321 { 474
322 if (isset($_POST[$field]) AND $_POST[$field] != "") 475 // Cycle through each rule and run it
323 { 476 foreach ($rules As $rule) {
324 $this->_field_data[$field]['postdata'] = $_POST[$field]; 477 $_in_array = false;
325 } 478
326 } 479 // We set the $postdata variable with the current data in our master array so that
327 480 // each cycle of the loop is dealing with the processed data from the last cycle
328 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']); 481 if ($row['is_array'] == true AND is_array($this->_field_data[$row['field']]['postdata'])) {
329 } 482 // We shouldn't need this safety, but just in case there isn't an array index
330 483 // associated with this cycle we'll bail out
331 // Did we end up with any errors? 484 if (!isset($this->_field_data[$row['field']]['postdata'][$cycles])) {
332 $total_errors = count($this->_error_array); 485 continue;
333 486 }
334 if ($total_errors > 0) 487
335 { 488 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
336 $this->_safe_form_data = TRUE; 489 $_in_array = true;
337 } 490 } else {
338 491 $postdata = $this->_field_data[$row['field']]['postdata'];
339 // Now we need to re-set the POST data with the new, processed data 492 }
340 $this->_reset_post_array(); 493
341 494 // --------------------------------------------------------------------
342 // No errors, validation passes! 495
343 if ($total_errors == 0) 496 // Is the rule a callback?
344 { 497 $callback = false;
345 return TRUE; 498 if (substr($rule, 0, 9) == 'callback_') {
346 } 499 $rule = substr($rule, 9);
347 500 $callback = true;
348 // Validation fails 501 }
349 return FALSE; 502
350 } 503 // Strip the parameter (if exists) from the rule
351 504 // Rules can contain a parameter: max_length[5]
352 // -------------------------------------------------------------------- 505 $param = false;
353 506 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match)) {
354 /** 507 $rule = $match[1];
355 * Traverse a multidimensional $_POST array index until the data is found 508 $param = $match[2];
356 * 509 }
357 * @access private 510
358 * @param array 511 // Call the function that corresponds to the rule
359 * @param array 512 if ($callback === true) {
360 * @param integer 513 if (!method_exists($this->CI, $rule)) {
361 * @return mixed 514 continue;
362 */ 515 }
363 function _reduce_array($array, $keys, $i = 0) 516
364 { 517 // Run the function and grab the result
365 if (is_array($array)) 518 $result = $this->CI->$rule($postdata, $param);
366 { 519
367 if (isset($keys[$i])) 520 // Re-assign the result to the master data array
368 { 521 if ($_in_array == true) {
369 if (isset($array[$keys[$i]])) 522 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
370 { 523 } else {
371 $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)); 524 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
372 } 525 }
373 else 526
374 { 527 // If the field isn't required and we just processed a callback we'll move on...
375 return NULL; 528 if (!in_array('required', $rules, true) AND $result !== false) {
376 } 529 continue;
377 } 530 }
378 else 531 } else {
379 { 532 if (!method_exists($this, $rule)) {
380 return $array; 533 // If our own wrapper function doesn't exist we see if a native PHP function does.
381 } 534 // Users can use any native PHP function call that has one param.
382 } 535 if (function_exists($rule)) {
383 536 $result = $rule($postdata);
384 return $array; 537
385 } 538 if ($_in_array == true) {
386 539 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata
387 // -------------------------------------------------------------------- 540 : $result;
388 541 } else {
389 /** 542 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
390 * Re-populate the _POST array with our finalized and processed data 543 }
391 * 544 }
392 * @access private 545
393 * @return null 546 continue;
394 */ 547 }
395 function _reset_post_array() 548
396 { 549 $result = $this->$rule($postdata, $param);
397 foreach ($this->_field_data as $field => $row) 550
398 { 551 if ($_in_array == true) {
399 if ( ! is_null($row['postdata'])) 552 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
400 { 553 } else {
401 if ($row['is_array'] == FALSE) 554 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
402 { 555 }
403 if (isset($_POST[$row['field']])) 556 }
404 { 557
405 $_POST[$row['field']] = $this->prep_for_form($row['postdata']); 558 // Did the rule test negatively? If so, grab the error.
406 } 559 if ($result === false) {
407 } 560 if (!isset($this->_error_messages[$rule])) {
408 else 561 if (false === ($line = $this->get_error_message($rule))) {
409 { 562 $line = 'Unable to access an error message corresponding to your field name.';
410 // start with a reference 563 }
411 $post_ref =& $_POST; 564 } else {
412 565 $line = $this->_error_messages[$rule];
413 // before we assign values, make a reference to the right POST key 566 }
414 if (count($row['keys']) == 1) 567
415 { 568 // Is the parameter we are inserting into the error message the name
416 $post_ref =& $post_ref[current($row['keys'])]; 569 // of another field? If so we need to grab its "field label"
417 } 570 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label'])) {
418 else 571 $param = $this->_field_data[$param]['label'];
419 { 572 }
420 foreach ($row['keys'] as $val) 573
421 { 574 // Build the error message
422 $post_ref =& $post_ref[$val]; 575 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
423 } 576
424 } 577 // Save the error message
425 578 $this->_field_data[$row['field']]['error'] = $message;
426 if (is_array($row['postdata'])) 579
427 { 580 if (!isset($this->_error_array[$row['field']])) {
428 $array = array(); 581 $this->_error_array[$row['field']] = $message;
429 foreach ($row['postdata'] as $k => $v) 582 }
430 { 583
431 $array[$k] = $this->prep_for_form($v); 584 return;
432 } 585 }
433 586 }
434 $post_ref = $array; 587 }
435 }
436 else
437 {
438 $post_ref = $this->prep_for_form($row['postdata']);
439 }
440 }
441 }
442 }
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Executes the Validation routines
449 *
450 * @access private
451 * @param array
452 * @param array
453 * @param mixed
454 * @param integer
455 * @return mixed
456 */
457 function _execute($row, $rules, $postdata = NULL, $cycles = 0)
458 {
459 // If the $_POST data is an array we will run a recursive call
460 if (is_array($postdata))
461 {
462 foreach ($postdata as $key => $val)
463 {
464 $this->_execute($row, $rules, $val, $cycles);
465 $cycles++;
466 }
467
468 return;
469 }
470
471 // --------------------------------------------------------------------
472
473 // If the field is blank, but NOT required, no further tests are necessary
474 $callback = FALSE;
475 if ( ! in_array('required', $rules) AND is_null($postdata))
476 {
477 // Before we bail out, does the rule contain a callback?
478 if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
479 {
480 $callback = TRUE;
481 $rules = (array('1' => $match[1]));
482 }
483 else
484 {
485 return;
486 }
487 }
488
489 // --------------------------------------------------------------------
490
491 // Isset Test. Typically this rule will only apply to checkboxes.
492 if (is_null($postdata) AND $callback == FALSE)
493 {
494 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
495 {
496 // Set the message type
497 $type = (in_array('required', $rules)) ? 'required' : 'isset';
498
499 if ( ! isset($this->_error_messages[$type]))
500 {
501 if (FALSE === ($line = $this->get_error_message($type)))
502 {
503 $line = 'The field was not set';
504 }
505 }
506 else
507 {
508 $line = $this->_error_messages[$type];
509 }
510
511 // Build the error message
512 $message = sprintf($line, $this->_translate_fieldname($row['label']));
513
514 // Save the error message
515 $this->_field_data[$row['field']]['error'] = $message;
516
517 if ( ! isset($this->_error_array[$row['field']]))
518 {
519 $this->_error_array[$row['field']] = $message;
520 }
521 }
522
523 return;
524 }
525
526 // --------------------------------------------------------------------
527
528 // Cycle through each rule and run it
529 foreach ($rules As $rule)
530 {
531 $_in_array = FALSE;
532
533 // We set the $postdata variable with the current data in our master array so that
534 // each cycle of the loop is dealing with the processed data from the last cycle
535 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
536 {
537 // We shouldn't need this safety, but just in case there isn't an array index
538 // associated with this cycle we'll bail out
539 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
540 {
541 continue;
542 }
543
544 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
545 $_in_array = TRUE;
546 }
547 else
548 {
549 $postdata = $this->_field_data[$row['field']]['postdata'];
550 }
551
552 // --------------------------------------------------------------------
553
554 // Is the rule a callback?
555 $callback = FALSE;
556 if (substr($rule, 0, 9) == 'callback_')
557 {
558 $rule = substr($rule, 9);
559 $callback = TRUE;
560 }
561
562 // Strip the parameter (if exists) from the rule
563 // Rules can contain a parameter: max_length[5]
564 $param = FALSE;
565 if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
566 {
567 $rule = $match[1];
568 $param = $match[2];
569 }
570
571 // Call the function that corresponds to the rule
572 if ($callback === TRUE)
573 {
574 if ( ! method_exists($this->CI, $rule))
575 {
576 continue;
577 }
578
579 // Run the function and grab the result
580 $result = $this->CI->$rule($postdata, $param);
581
582 // Re-assign the result to the master data array
583 if ($_in_array == TRUE)
584 {
585 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
586 }
587 else
588 {
589 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
590 }
591
592 // If the field isn't required and we just processed a callback we'll move on...
593 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
594 {
595 continue;
596 }
597 }
598 else
599 {
600 if ( ! method_exists($this, $rule))
601 {
602 // If our own wrapper function doesn't exist we see if a native PHP function does.
603 // Users can use any native PHP function call that has one param.
604 if (function_exists($rule))
605 {
606 $result = $rule($postdata);
607
608 if ($_in_array == TRUE)
609 {
610 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
611 }
612 else
613 {
614 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
615 }
616 }
617
618 continue;
619 }
620
621 $result = $this->$rule($postdata, $param);
622
623 if ($_in_array == TRUE)
624 {
625 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
626 }
627 else
628 {
629 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
630 }
631 }
632
633 // Did the rule test negatively? If so, grab the error.
634 if ($result === FALSE)
635 {
636 if ( ! isset($this->_error_messages[$rule]))
637 {
638 if (FALSE === ($line = $this->get_error_message($rule)))
639 {
640 $line = 'Unable to access an error message corresponding to your field name.';
641 }
642 }
643 else
644 {
645 $line = $this->_error_messages[$rule];
646 }
647
648 // Is the parameter we are inserting into the error message the name
649 // of another field? If so we need to grab its "field label"
650 if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
651 {
652 $param = $this->_field_data[$param]['label'];
653 }
654
655 // Build the error message
656 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
657
658 // Save the error message
659 $this->_field_data[$row['field']]['error'] = $message;
660
661 if ( ! isset($this->_error_array[$row['field']]))
662 {
663 $this->_error_array[$row['field']] = $message;
664 }
665
666 return;
667 }
668 }
669 }
670 588
671 function form_error($field = '', $prefix = '', $suffix = '') 589 function form_error($field = '', $prefix = '', $suffix = '')
672 { 590 {
673 591
674 592 return $this->error($field, $prefix, $suffix);
675 return $this->error($field, $prefix, $suffix); 593 }
676 } 594
677 595 function validation_errors($prefix = '', $suffix = '')
678 function validation_errors($prefix = '', $suffix = '') 596 {
679 { 597 return $this->error_string($prefix, $suffix);
680 return $this->error_string($prefix, $suffix); 598 }
681 } 599
682 600 // --------------------------------------------------------------------
683 // -------------------------------------------------------------------- 601
684 602 /**
685 /** 603 * Translate a field name
686 * Translate a field name 604 *
687 * 605 * @access private
688 * @access private 606 *
689 * @param string the field name 607 * @param string the field name
690 * @return string 608 *
691 */ 609 * @return string
692 function _translate_fieldname($fieldname) 610 */
693 { 611 function _translate_fieldname($fieldname)
694 // Do we need to translate the field name? 612 {
695 // We look for the prefix lang: to determine this 613 // Do we need to translate the field name?
696 if (substr($fieldname, 0, 5) == 'lang:') 614 // We look for the prefix lang: to determine this
697 { 615 if (substr($fieldname, 0, 5) == 'lang:') {
698 // Grab the variable 616 // Grab the variable
699 $line = substr($fieldname, 5); 617 $line = substr($fieldname, 5);
700 618
701 // Were we able to translate the field name? If not we use $line 619 // Were we able to translate the field name? If not we use $line
702 if (FALSE === ($fieldname = $this->get_error_message($line))) 620 if (false === ($fieldname = $this->get_error_message($line))) {
703 { 621 return $line;
704 return $line; 622 }
705 } 623 }
706 } 624
707 625 return $fieldname;
708 return $fieldname; 626 }
709 } 627
710 628 // --------------------------------------------------------------------
711 // -------------------------------------------------------------------- 629
712 630 /**
713 /** 631 * Get the value from a form
714 * Get the value from a form 632 *
715 * 633 * Permits you to repopulate a form field with the value it was submitted
716 * Permits you to repopulate a form field with the value it was submitted 634 * with, or, if that value doesn't exist, with the default
717 * with, or, if that value doesn't exist, with the default 635 *
718 * 636 * @access public
719 * @access public 637 *
720 * @param string the field name 638 * @param string the field name
721 * @param string 639 * @param string
722 * @return mixed 640 *
723 */ 641 * @return mixed
724 function set_value($field = '', $default = '') 642 */
725 { 643 function set_value($field = '', $default = '')
726 if ( ! isset($this->_field_data[$field])) 644 {
727 { 645 if (!isset($this->_field_data[$field])) {
728 return $default; 646 return $default;
729 } 647 }
730 648
731 return Tools\tzClean($this->_field_data[$field]['postdata']); 649 return Tools\tzClean($this->_field_data[$field]['postdata']);
732 } 650 }
733 651
734 // -------------------------------------------------------------------- 652 // --------------------------------------------------------------------
735 653
736 /** 654 /**
737 * Set Select 655 * Set Select
738 * 656 *
739 * Enables pull-down lists to be set to the value the user 657 * Enables pull-down lists to be set to the value the user
740 * selected in the event of an error 658 * selected in the event of an error
741 * 659 *
742 * @access public 660 * @access public
743 * @param string 661 *
744 * @param string 662 * @param string
745 * @return string 663 * @param string
746 */ 664 *
747 function set_select($field = '', $value = '', $default = FALSE) 665 * @return string
748 { 666 */
749 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) 667 function set_select($field = '', $value = '', $default = false)
750 { 668 {
751 if ($default === TRUE AND count($this->_field_data) === 0) 669 if (!isset($this->_field_data[$field]) OR !isset($this->_field_data[$field]['postdata'])) {
752 { 670 if ($default === true AND count($this->_field_data) === 0) {
753 return ' selected="selected"'; 671 return ' selected="selected"';
754 } 672 }
755 return ''; 673
756 } 674 return '';
757 675 }
758 $field = $this->_field_data[$field]['postdata']; 676
759 677 $field = $this->_field_data[$field]['postdata'];
760 if (is_array($field)) 678
761 { 679 if (is_array($field)) {
762 if ( ! in_array($value, $field)) 680 if (!in_array($value, $field)) {
763 { 681 return '';
764 return ''; 682 }
765 } 683 } else {
766 } 684 if (($field == '' OR $value == '') OR ($field != $value)) {
767 else 685 return '';
768 { 686 }
769 if (($field == '' OR $value == '') OR ($field != $value)) 687 }
770 { 688
771 return ''; 689 return ' selected="selected"';
772 } 690 }
773 } 691
774 692 // --------------------------------------------------------------------
775 return ' selected="selected"'; 693
776 } 694 /**
777 695 * Set Radio
778 // -------------------------------------------------------------------- 696 *
779 697 * Enables radio buttons to be set to the value the user
780 /** 698 * selected in the event of an error
781 * Set Radio 699 *
782 * 700 * @access public
783 * Enables radio buttons to be set to the value the user 701 *
784 * selected in the event of an error 702 * @param string
785 * 703 * @param string
786 * @access public 704 *
787 * @param string 705 * @return string
788 * @param string 706 */
789 * @return string 707 function set_radio($field = '', $value = '', $default = false)
790 */ 708 {
791 function set_radio($field = '', $value = '', $default = FALSE) 709 if (!isset($this->_field_data[$field]) OR !isset($this->_field_data[$field]['postdata'])) {
792 { 710 if ($default === true AND count($this->_field_data) === 0) {
793 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) 711 return ' checked="checked"';
794 { 712 }
795 if ($default === TRUE AND count($this->_field_data) === 0) 713
796 { 714 return '';
797 return ' checked="checked"'; 715 }
798 } 716
799 return ''; 717 $field = $this->_field_data[$field]['postdata'];
800 } 718
801 719 if (is_array($field)) {
802 $field = $this->_field_data[$field]['postdata']; 720 if (!in_array($value, $field)) {
803 721 return '';
804 if (is_array($field)) 722 }
805 { 723 } else {
806 if ( ! in_array($value, $field)) 724 if (($field == '' OR $value == '') OR ($field != $value)) {
807 { 725 return '';
808 return ''; 726 }
809 } 727 }
810 } 728
811 else 729 return ' checked="checked"';
812 { 730 }
813 if (($field == '' OR $value == '') OR ($field != $value)) 731
814 { 732 // --------------------------------------------------------------------
815 return ''; 733
816 } 734 /**
817 } 735 * Set Checkbox
818 736 *
819 return ' checked="checked"'; 737 * Enables checkboxes to be set to the value the user
820 } 738 * selected in the event of an error
821 739 *
822 // -------------------------------------------------------------------- 740 * @access public
823 741 *
824 /** 742 * @param string
825 * Set Checkbox 743 * @param string
826 * 744 *
827 * Enables checkboxes to be set to the value the user 745 * @return string
828 * selected in the event of an error 746 */
829 * 747 function set_checkbox($field = '', $value = '', $default = false)
830 * @access public 748 {
831 * @param string 749 if (!isset($this->_field_data[$field]) OR !isset($this->_field_data[$field]['postdata'])) {
832 * @param string 750 if ($default === true AND count($this->_field_data) === 0) {
833 * @return string 751 return ' checked="checked"';
834 */ 752 }
835 function set_checkbox($field = '', $value = '', $default = FALSE) 753
836 { 754 return '';
837 if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata'])) 755 }
838 { 756
839 if ($default === TRUE AND count($this->_field_data) === 0) 757 $field = $this->_field_data[$field]['postdata'];
840 { 758
841 return ' checked="checked"'; 759 if (is_array($field)) {
842 } 760 if (!in_array($value, $field)) {
843 return ''; 761 return '';
844 } 762 }
845 763 } else {
846 $field = $this->_field_data[$field]['postdata']; 764 if (($field == '' OR $value == '') OR ($field != $value)) {
847 765 return '';
848 if (is_array($field)) 766 }
849 { 767 }
850 if ( ! in_array($value, $field)) 768
851 { 769 return ' checked="checked"';
852 return ''; 770 }
853 } 771
854 } 772 // --------------------------------------------------------------------
855 else 773
856 { 774 /**
857 if (($field == '' OR $value == '') OR ($field != $value)) 775 * Required
858 { 776 *
859 return ''; 777 * @access public
860 } 778 *
861 } 779 * @param string
862 780 *
863 return ' checked="checked"'; 781 * @return bool
864 } 782 */
865 783 function required($str)
866 // -------------------------------------------------------------------- 784 {
867 785 if (!is_array($str)) {
868 /** 786 return (trim($str) == '') ? false : true;
869 * Required 787 } else {
870 * 788 return (!empty($str));
871 * @access public 789 }
872 * @param string 790 }
873 * @return bool 791
874 */ 792 // --------------------------------------------------------------------
875 function required($str) 793
876 { 794 /**
877 if ( ! is_array($str)) 795 * Match one field to another
878 { 796 *
879 return (trim($str) == '') ? FALSE : TRUE; 797 * @access public
880 } 798 *
881 else 799 * @param string
882 { 800 * @param field
883 return ( ! empty($str)); 801 *
884 } 802 * @return bool
885 } 803 */
886 804 function matches($str, $field)
887 // -------------------------------------------------------------------- 805 {
888 806 if (!isset($_POST[$field])) {
889 /** 807 return false;
890 * Match one field to another 808 }
891 * 809
892 * @access public 810 $field = $_POST[$field];
893 * @param string 811
894 * @param field 812 return ($str !== $field) ? false : true;
895 * @return bool 813 }
896 */ 814
897 function matches($str, $field) 815 // --------------------------------------------------------------------
898 { 816
899 if ( ! isset($_POST[$field])) 817 /**
900 { 818 * Minimum Length
901 return FALSE; 819 *
902 } 820 * @access public
903 821 *
904 $field = $_POST[$field]; 822 * @param string
905 823 * @param value
906 return ($str !== $field) ? FALSE : TRUE; 824 *
907 } 825 * @return bool
908 826 */
909 // -------------------------------------------------------------------- 827 function min_length($str, $val)
910 828 {
911 /** 829 if (preg_match("/[^0-9]/", $val)) {
912 * Minimum Length 830 return false;
913 * 831 }
914 * @access public 832
915 * @param string 833 if (function_exists('mb_strlen')) {
916 * @param value 834 return (mb_strlen($str) < $val) ? false : true;
917 * @return bool 835 }
918 */ 836
919 function min_length($str, $val) 837 return (strlen($str) < $val) ? false : true;
920 { 838 }
921 if (preg_match("/[^0-9]/", $val)) 839
922 { 840 // --------------------------------------------------------------------
923 return FALSE; 841
924 } 842 /**
925 843 * Max Length
926 if (function_exists('mb_strlen')) 844 *
927 { 845 * @access public
928 return (mb_strlen($str) < $val) ? FALSE : TRUE; 846 *
929 } 847 * @param string
930 848 * @param value
931 return (strlen($str) < $val) ? FALSE : TRUE; 849 *
932 } 850 * @return bool
933 851 */
934 // -------------------------------------------------------------------- 852 function max_length($str, $val)
935 853 {
936 /** 854 if (preg_match("/[^0-9]/", $val)) {
937 * Max Length 855 return false;
938 * 856 }
939 * @access public 857
940 * @param string 858 if (function_exists('mb_strlen')) {
941 * @param value 859 return (mb_strlen($str) > $val) ? false : true;
942 * @return bool 860 }
943 */ 861
944 function max_length($str, $val) 862 return (strlen($str) > $val) ? false : true;
945 { 863 }
946 if (preg_match("/[^0-9]/", $val)) 864
947 { 865 // --------------------------------------------------------------------
948 return FALSE; 866
949 } 867 /**
950 868 * Exact Length
951 if (function_exists('mb_strlen')) 869 *
952 { 870 * @access public
953 return (mb_strlen($str) > $val) ? FALSE : TRUE; 871 *
954 } 872 * @param string
955 873 * @param value
956 return (strlen($str) > $val) ? FALSE : TRUE; 874 *
957 } 875 * @return bool
958 876 */
959 // -------------------------------------------------------------------- 877 function exact_length($str, $val)
960 878 {
961 /** 879 if (preg_match("/[^0-9]/", $val)) {
962 * Exact Length 880 return false;
963 * 881 }
964 * @access public 882
965 * @param string 883 if (function_exists('mb_strlen')) {
966 * @param value 884 return (mb_strlen($str) != $val) ? false : true;
967 * @return bool 885 }
968 */ 886
969 function exact_length($str, $val) 887 return (strlen($str) != $val) ? false : true;
970 { 888 }
971 if (preg_match("/[^0-9]/", $val)) 889
972 { 890 // --------------------------------------------------------------------
973 return FALSE; 891
974 } 892 /**
975 893 * Valid Email
976 if (function_exists('mb_strlen')) 894 *
977 { 895 * @access public
978 return (mb_strlen($str) != $val) ? FALSE : TRUE; 896 *
979 } 897 * @param string
980 898 *
981 return (strlen($str) != $val) ? FALSE : TRUE; 899 * @return bool
982 } 900 */
983 901 function valid_email($str)
984 // -------------------------------------------------------------------- 902 {
985 903 return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? false
986 /** 904 : true;
987 * Valid Email 905 }
988 * 906
989 * @access public 907 // --------------------------------------------------------------------
990 * @param string 908
991 * @return bool 909 /**
992 */ 910 * Valid Emails
993 function valid_email($str) 911 *
994 { 912 * @access public
995 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; 913 *
996 } 914 * @param string
997 915 *
998 // -------------------------------------------------------------------- 916 * @return bool
999 917 */
1000 /** 918 function valid_emails($str)
1001 * Valid Emails 919 {
1002 * 920 if (strpos($str, ',') === false) {
1003 * @access public 921 return $this->valid_email(trim($str));
1004 * @param string 922 }
1005 * @return bool 923
1006 */ 924 foreach (explode(',', $str) as $email) {
1007 function valid_emails($str) 925 if (trim($email) != '' && $this->valid_email(trim($email)) === false) {
1008 { 926 return false;
1009 if (strpos($str, ',') === FALSE) 927 }
1010 { 928 }
1011 return $this->valid_email(trim($str)); 929
1012 } 930 return true;
1013 931 }
1014 foreach(explode(',', $str) as $email) 932
1015 { 933 // --------------------------------------------------------------------
1016 if (trim($email) != '' && $this->valid_email(trim($email)) === FALSE) 934
1017 { 935 /**
1018 return FALSE; 936 * Validate IP Address
1019 } 937 *
1020 } 938 * @access public
1021 939 *
1022 return TRUE; 940 * @param string
1023 } 941 *
1024 942 * @return string
1025 // -------------------------------------------------------------------- 943 */
1026 944 function valid_ip($ip)
1027 /** 945 {
1028 * Validate IP Address 946 return $this->CI->input->valid_ip($ip);
1029 * 947 }
1030 * @access public 948
1031 * @param string 949 // --------------------------------------------------------------------
1032 * @return string 950
1033 */ 951 /**
1034 function valid_ip($ip) 952 * Alpha
1035 { 953 *
1036 return $this->CI->input->valid_ip($ip); 954 * @access public
1037 } 955 *
1038 956 * @param string
1039 // -------------------------------------------------------------------- 957 *
1040 958 * @return bool
1041 /** 959 */
1042 * Alpha 960 function alpha($str)
1043 * 961 {
1044 * @access public 962 return (!preg_match("/^([a-z])+$/i", $str)) ? false : true;
1045 * @param string 963 }
1046 * @return bool 964
1047 */ 965 // --------------------------------------------------------------------
1048 function alpha($str) 966
1049 { 967 /**
1050 return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE; 968 * Alpha-numeric
1051 } 969 *
1052 970 * @access public
1053 // -------------------------------------------------------------------- 971 *
1054 972 * @param string
1055 /** 973 *
1056 * Alpha-numeric 974 * @return bool
1057 * 975 */
1058 * @access public 976 function alpha_numeric($str)
1059 * @param string 977 {
1060 * @return bool 978 return (!preg_match("/^([a-z0-9])+$/i", $str)) ? false : true;
1061 */ 979 }
1062 function alpha_numeric($str) 980
1063 { 981 // --------------------------------------------------------------------
1064 return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE; 982
1065 } 983 /**
1066 984 * Alpha-numeric with underscores and dashes
1067 // -------------------------------------------------------------------- 985 *
1068 986 * @access public
1069 /** 987 *
1070 * Alpha-numeric with underscores and dashes 988 * @param string
1071 * 989 *
1072 * @access public 990 * @return bool
1073 * @param string 991 */
1074 * @return bool 992 function alpha_dash($str)
1075 */ 993 {
1076 function alpha_dash($str) 994 return (!preg_match("/^([-a-z0-9_-])+$/i", $str)) ? false : true;
1077 { 995 }
1078 return ( ! preg_match("/^([-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE; 996
1079 } 997 // --------------------------------------------------------------------
1080 998
1081 // -------------------------------------------------------------------- 999 /**
1082 1000 * Numeric
1083 /** 1001 *
1084 * Numeric 1002 * @access public
1085 * 1003 *
1086 * @access public 1004 * @param string
1087 * @param string 1005 *
1088 * @return bool 1006 * @return bool
1089 */ 1007 */
1090 function numeric($str) 1008 function numeric($str)
1091 { 1009 {
1092 return (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $str); 1010 return (bool)preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
1093 1011 }
1094 } 1012
1095 1013 // --------------------------------------------------------------------
1096 // --------------------------------------------------------------------
1097 1014
1098 /** 1015 /**
1099 * Is Numeric 1016 * Is Numeric
1100 * 1017 *
1101 * @access public 1018 * @access public
1019 *
1102 * @param string 1020 * @param string
1021 *
1103 * @return bool 1022 * @return bool
1104 */ 1023 */
1105 function is_numeric($str) 1024 function is_numeric($str)
1106 { 1025 {
1107 return ( ! is_numeric($str)) ? FALSE : TRUE; 1026 return (!is_numeric($str)) ? false : true;
1108 } 1027 }
1109 1028
1110 // -------------------------------------------------------------------- 1029 // --------------------------------------------------------------------
1111 1030
1112 /** 1031 /**
1113 * Integer 1032 * Integer
1114 * 1033 *
1115 * @access public 1034 * @access public
1116 * @param string 1035 *
1117 * @return bool 1036 * @param string
1118 */ 1037 *
1119 function integer($str) 1038 * @return bool
1120 { 1039 */
1121 return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str); 1040 function integer($str)
1122 } 1041 {
1042 return (bool)preg_match('/^[\-+]?[0-9]+$/', $str);
1043 }
1123 1044
1124 // -------------------------------------------------------------------- 1045 // --------------------------------------------------------------------
1125 1046
1126 /** 1047 /**
1127 * Is a Natural number (0,1,2,3, etc.) 1048 * Is a Natural number (0,1,2,3, etc.)
1128 * 1049 *
1129 * @access public 1050 * @access public
1130 * @param string 1051 *
1131 * @return bool 1052 * @param string
1053 *
1054 * @return bool
1132 */ 1055 */
1133 function is_natural($str) 1056 function is_natural($str)
1134 { 1057 {
1135 return (bool)preg_match( '/^[0-9]+$/', $str); 1058 return (bool)preg_match('/^[0-9]+$/', $str);
1136 } 1059 }
1137 1060
1138 // -------------------------------------------------------------------- 1061 // --------------------------------------------------------------------
1139 1062
1140 /** 1063 /**
1141 * Is a Natural number, but not a zero (1,2,3, etc.) 1064 * Is a Natural number, but not a zero (1,2,3, etc.)
1142 * 1065 *
1143 * @access public 1066 * @access public
1144 * @param string 1067 *
1145 * @return bool 1068 * @param string
1069 *
1070 * @return bool
1071 */
1072 function is_natural_no_zero($str)
1073 {
1074 if (!preg_match('/^[0-9]+$/', $str)) {
1075 return false;
1076 }
1077
1078 if ($str == 0) {
1079 return false;
1080 }
1081
1082 return true;
1083 }
1084
1085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Valid Base64
1089 *
1090 * Tests a string for characters outside of the Base64 alphabet
1091 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1092 *
1093 * @access public
1094 *
1095 * @param string
1096 *
1097 * @return bool
1098 */
1099 function valid_base64($str)
1100 {
1101 return (bool)!preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1102 }
1103
1104 // --------------------------------------------------------------------
1105
1106 /**
1107 * Prep data for form
1108 *
1109 * This function allows HTML to be safely shown in a form.
1110 * Special characters are converted.
1111 *
1112 * @access public
1113 *
1114 * @param string
1115 *
1116 * @return string
1146 */ 1117 */
1147 function is_natural_no_zero($str) 1118 function prep_for_form($data = '')
1148 { 1119 {
1149 if ( ! preg_match( '/^[0-9]+$/', $str)) 1120 if (is_array($data)) {
1150 { 1121 foreach ($data as $key => $val) {
1151 return FALSE; 1122 $data[$key] = $this->prep_for_form($val);
1152 } 1123 }
1124
1125 return $data;
1126 }
1153 1127
1154 if ($str == 0) 1128 if ($this->_safe_form_data == false OR $data === '') {
1155 { 1129 return $data;
1156 return FALSE; 1130 }
1157 }
1158 1131
1159 return TRUE; 1132 return str_replace(["'", '"', '<', '>'], ["&#39;", "&quot;", '&lt;', '&gt;'], stripslashes($data));
1160 } 1133 }
1161 1134
1162 // -------------------------------------------------------------------- 1135 // --------------------------------------------------------------------
1163
1164 /**
1165 * Valid Base64
1166 *
1167 * Tests a string for characters outside of the Base64 alphabet
1168 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1169 *
1170 * @access public
1171 * @param string
1172 * @return bool
1173 */
1174 function valid_base64($str)
1175 {
1176 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1177 }
1178
1179 // --------------------------------------------------------------------
1180
1181 /**
1182 * Prep data for form
1183 *
1184 * This function allows HTML to be safely shown in a form.
1185 * Special characters are converted.
1186 *
1187 * @access public
1188 * @param string
1189 * @return string
1190 */
1191 function prep_for_form($data = '')
1192 {
1193 if (is_array($data))
1194 {
1195 foreach ($data as $key => $val)
1196 {
1197 $data[$key] = $this->prep_for_form($val);
1198 }
1199
1200 return $data;
1201 }
1202
1203 if ($this->_safe_form_data == FALSE OR $data === '')
1204 {
1205 return $data;
1206 }
1207
1208 return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '&lt;', '&gt;'), stripslashes($data));
1209 }
1210
1211 // --------------------------------------------------------------------
1212
1213 /**
1214 * Prep URL
1215 *
1216 * @access public
1217 * @param string
1218 * @return string
1219 */
1220 function prep_url($str = '')
1221 {
1222 if ($str == 'http://' OR $str == '')
1223 {
1224 return '';
1225 }
1226
1227 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
1228 {
1229 $str = 'http://'.$str;
1230 }
1231
1232 return $str;
1233 }
1234
1235 // --------------------------------------------------------------------
1236
1237 /**
1238 * Strip Image Tags
1239 *
1240 * @access public
1241 * @param string
1242 * @return string
1243 */
1244 function strip_image_tags($str)
1245 {
1246 return $this->CI->input->strip_image_tags($str);
1247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
1252 * XSS Clean
1253 *
1254 * @access public
1255 * @param string
1256 * @return string
1257 */
1258 function xss_clean($str)
1259 {
1260 return $this->CI->input->xss_clean($str);
1261 }
1262
1263 // --------------------------------------------------------------------
1264
1265 /**
1266 * Convert PHP tags to entities
1267 *
1268 * @access public
1269 * @param string
1270 * @return string
1271 */
1272 function encode_php_tags($str)
1273 {
1274 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
1275 }
1276 1136
1137 /**
1138 * Prep URL
1139 *
1140 * @access public
1141 *
1142 * @param string
1143 *
1144 * @return string
1145 */
1146 function prep_url($str = '')
1147 {
1148 if ($str == 'http://' OR $str == '') {
1149 return '';
1150 }
1151
1152 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') {
1153 $str = 'http://'.$str;
1154 }
1155
1156 return $str;
1157 }
1158
1159 // --------------------------------------------------------------------
1160
1161 /**
1162 * Strip Image Tags
1163 *
1164 * @access public
1165 *
1166 * @param string
1167 *
1168 * @return string
1169 */
1170 function strip_image_tags($str)
1171 {
1172 return $this->CI->input->strip_image_tags($str);
1173 }
1174
1175 // --------------------------------------------------------------------
1176
1177 /**
1178 * XSS Clean
1179 *
1180 * @access public
1181 *
1182 * @param string
1183 *
1184 * @return string
1185 */
1186 function xss_clean($str)
1187 {
1188 return $this->CI->input->xss_clean($str);
1189 }
1190
1191 // --------------------------------------------------------------------
1192
1193 /**
1194 * Convert PHP tags to entities
1195 *
1196 * @access public
1197 *
1198 * @param string
1199 *
1200 * @return string
1201 */
1202 function encode_php_tags($str)
1203 {
1204 return str_replace(['<?php', '<?PHP', '<?', '?>'], ['&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'], $str);
1205 }
1277 } 1206 }
1278 // END Form Validation Class 1207 // END Form Validation Class
1279 1208
1280 /* End of file Form_validation.php */ 1209 /* End of file Form_validation.php */
1281 /* Location: ./system/libraries/Form_validation.php */
...\ No newline at end of file ...\ No newline at end of file
1210 /* Location: ./system/libraries/Form_validation.php */
......