phone.au.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Australia phone numbers.
  5. */
  6. /**
  7. * Patterns generated from Telecommunications Numbering Plan 1997
  8. * http://www.comlaw.gov.au/comlaw/management.nsf/lookupindexpagesbyid/IP200506356?OpenDocument
  9. */
  10. function _cck_phone_au_number_patterns() {
  11. $valid_patterns = array(
  12. // Geographic numbers (10 digits) - Central East Region (NSW, ACT, Northern VIC).
  13. '/^02[4,6-9][0-9]{7}$/',
  14. '/^023[3,8][0-9]{6}$/',
  15. '/^025[0-3,5-9][0-9]{6}$/',
  16. // Geographic numbers (10 digits) - South East Region (TAS, Southern NSW, VIC).
  17. '/^03[5,7-9][0-9]{7}$/',
  18. '/^034[0-5,7-9][0-9]{6}$/',
  19. '/^036[1-5,7][0-9]{6}$/',
  20. // Geographic numbers (10 digits) - North East Region (QLD).
  21. '/^07[2-4][0-9]{7}$/',
  22. '/^075[2-7][0-9]{6}$/',
  23. '/^0776[0-9]{6}$/',
  24. // Geographic numbers (10 digits) - Central and West Region (WA, SA, NT and Western NSW).
  25. '/^08[7-9][0-9]{7}$/',
  26. '/^085[1-4][0-9]{6}$/',
  27. '/^086[0-8][0-9]{6}$/',
  28. // Mobile numbers (10 digits).
  29. '/^04[0-9]{8}$/',
  30. // Local rate special numbers (10 digits).
  31. '/^130[0-9]{7}$/',
  32. // Local rate special numbers (6 digits).
  33. '/^13[1-3,5-9][0-9]{3}$/',
  34. '/^134[0-4,6-9][0-9]{2}$/',
  35. // Free phone numbers (10 digits).
  36. '/^180[0-1][0-9]{6}$/',
  37. // Satelite telephone numbers (10 digits).
  38. '/^014[1-3,5,7][0-9]{6}$/',
  39. // Satelite telephone numbers (9 digits).
  40. '/^014[0,4,6,8-9][0-9]{5}$/',
  41. '/^015[0-9]{6}$/',
  42. '/^017[1,2,8,9][0-9]{5}$/',
  43. '/^018[0-9]{6}$/',
  44. );
  45. $invalid_patterns = array(
  46. // Invalid geographic phone numbers.
  47. '/^0[2,3,7]5550[0-9]{4}$/',
  48. );
  49. $format_search_patterns = array(
  50. // Geographic numbers (10 digits).
  51. '/^(0)([2,3,7,8])([0-9]{4})([0-9]{4})$/',
  52. // Mobile numbers (10 digits).
  53. '/^(0)(4[0-9]{2})([0-9]{3})([0-9]{3})$/',
  54. // 1300 numbers, 1800 numbers (10 digits).
  55. '/^(1[3,8][0-9]{2})([0-9]{3})([0-9]{3})$/',
  56. // 13 numbers (6 digits).
  57. '/^(13)([0-9]{2})([0-9]{2})$/',
  58. // Satelite telephone numbers (10 digits).
  59. '/^(0)(14)([0-9])([0-9]{3})([0-9]{3})$/',
  60. // Satelite telephone numbers (9 digits).
  61. '/^(0)(1[4,5,7,8])([0-9]{3})([0-9]{3})$/',
  62. );
  63. $format_replace_patterns = array(
  64. // Geographic numbers (10 digits).
  65. '($1$2) $3 $4',
  66. // Mobile numbers (10 digits).
  67. '$1$2 $3 $4',
  68. // 1300 numbers, 1800 numbers (10 digits).
  69. '$1 $2 $3',
  70. // 13 numbers (6 digits).
  71. '$1 $2 $3',
  72. // Satelite telephone numbers (10 digits).
  73. '$1$2 $3 $4 $5',
  74. // Satelite telephone numbers (9 digits).
  75. '$1$2 $3 $4',
  76. );
  77. $format_replace_patterns_international = array(
  78. // Geographic numbers (10 digits).
  79. '$2 $3 $4',
  80. // Mobile numbers (10 digits).
  81. '$2 $3 $4',
  82. // 1300 numbers, 1800 numbers (10 digits).
  83. '$1 $2 $3',
  84. // 13 numbers (6 digits).
  85. '$1 $2 $3',
  86. // Satelite telephone numbers (10 digits).
  87. '$2 $3 $4 $5',
  88. // Satelite telephone numbers (9 digits).
  89. '$2 $3 $4',
  90. );
  91. return array($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international);
  92. }
  93. /**
  94. * Verifies that $number is a valid Australian phone number.
  95. */
  96. function au_validate_number($number, $ext = '', &$error) {
  97. // Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
  98. // We don't want to worry about separators
  99. $number = cck_phone_clean_number($number);
  100. if (empty($number)) {
  101. return TRUE;
  102. }
  103. list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
  104. $invalid_number = preg_replace($invalid_patterns, '', $number);
  105. $correct_number = preg_replace($valid_patterns, '', $number);
  106. if (empty($invalid_number)) {
  107. $error = 'The phone number you have entered is classified as unusable by the Australian telecommunications authority.';
  108. return FALSE;
  109. }
  110. elseif (!empty($correct_number)) {
  111. $error = 'You have not entered a valid australian phone number. Please enter a 10 digit phone number including the area code, but not including the 61 international prefix. Valid 1800, 1300, and 13 numbers are accepted, as are satilite and AMPS numbers.';
  112. return FALSE;
  113. }
  114. return TRUE;
  115. }
  116. /**
  117. * Default formatter for Australian phone number.
  118. */
  119. function au_formatter_default($element) {
  120. // Display a global phone number with country code.
  121. $cc = cck_phone_countrycodes($element['country_codes']);
  122. list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
  123. return $cc['code'] .' '. preg_replace($format_search_patterns, $format_replace_patterns_international, $element['number']);
  124. }
  125. /**
  126. * Local formatter for local Australian phone number.
  127. */
  128. function au_formatter_local($element) {
  129. list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
  130. return preg_replace($format_search_patterns, $format_replace_patterns, $element['number']);
  131. }