Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
6a488035 | 29 | * @package CRM |
6b83d5bd | 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
31 | */ |
32 | ||
33 | /** | |
d4620d57 | 34 | * Address utilities. |
6a488035 TO |
35 | */ |
36 | class CRM_Utils_Address_USPS { | |
37 | ||
5bc392e6 | 38 | /** |
fd830836 | 39 | * Whether USPS validation should be disabled during import. |
d4620d57 | 40 | * |
fd830836 DRJ |
41 | * @var bool |
42 | */ | |
43 | protected static $_disabled = FALSE; | |
44 | ||
45 | /** | |
bf48aa29 | 46 | * Disable the USPS validation. |
fd830836 | 47 | * |
bf48aa29 | 48 | * @param bool $disable |
fd830836 DRJ |
49 | */ |
50 | public static function disable($disable = TRUE) { | |
51 | self::$_disabled = $disable; | |
52 | } | |
53 | ||
54 | /** | |
bf48aa29 | 55 | * Check address against USPS. |
56 | * | |
57 | * @param array $values | |
5bc392e6 EM |
58 | * |
59 | * @return bool | |
60 | */ | |
00be9182 | 61 | public static function checkAddress(&$values) { |
fd830836 DRJ |
62 | if (self::$_disabled) { |
63 | return FALSE; | |
64 | } | |
6a488035 TO |
65 | if (!isset($values['street_address']) || |
66 | (!isset($values['city']) && | |
67 | !isset($values['state_province']) && | |
68 | !isset($values['postal_code']) | |
69 | ) | |
70 | ) { | |
71 | return FALSE; | |
72 | } | |
73 | ||
aaffa79f | 74 | $userID = Civi::settings()->get('address_standardization_userid'); |
75 | $url = Civi::settings()->get('address_standardization_url'); | |
6a488035 TO |
76 | |
77 | if (empty($userID) || | |
78 | empty($url) | |
79 | ) { | |
80 | return FALSE; | |
81 | } | |
82 | ||
83 | $address2 = str_replace(',', '', $values['street_address']); | |
84 | ||
85 | $XMLQuery = '<AddressValidateRequest USERID="' . $userID . '"><Address ID="0"><Address1>' . CRM_Utils_Array::value('supplemental_address_1', $values, '') . '</Address1><Address2>' . $address2 . '</Address2><City>' . $values['city'] . '</City><State>' . $values['state_province'] . '</State><Zip5>' . $values['postal_code'] . '</Zip5><Zip4>' . CRM_Utils_Array::value('postal_code_suffix', $values, '') . '</Zip4></Address></AddressValidateRequest>'; | |
86 | ||
87 | require_once 'HTTP/Request.php'; | |
88 | $request = new HTTP_Request(); | |
89 | ||
90 | $request->setURL($url); | |
91 | ||
92 | $request->addQueryString('API', 'Verify'); | |
93 | $request->addQueryString('XML', $XMLQuery); | |
94 | ||
95 | $response = $request->sendRequest(); | |
96 | ||
97 | $session = CRM_Core_Session::singleton(); | |
98 | ||
99 | $code = $request->getResponseCode(); | |
100 | if ($code != 200) { | |
101 | $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1', | |
be2fb01f | 102 | [1 => $code] |
353ffa53 | 103 | )); |
6a488035 TO |
104 | return FALSE; |
105 | } | |
106 | ||
107 | $responseBody = $request->getResponseBody(); | |
108 | ||
109 | $xml = simplexml_load_string($responseBody); | |
110 | ||
111 | if (is_null($xml) || is_null($xml->Address)) { | |
112 | $session->setStatus(ts('Your USPS API Lookup has Failed.')); | |
113 | return FALSE; | |
114 | } | |
115 | ||
116 | if ($xml->Number == '80040b1a') { | |
117 | $session->setStatus(ts('Your USPS API Authorization has Failed.')); | |
118 | return FALSE; | |
119 | } | |
120 | ||
121 | if (array_key_exists('Error', $xml->Address)) { | |
122 | $session->setStatus(ts('Address not found in USPS database.')); | |
123 | return FALSE; | |
124 | } | |
125 | ||
e7292422 TO |
126 | $values['street_address'] = (string) $xml->Address->Address2; |
127 | $values['city'] = (string) $xml->Address->City; | |
128 | $values['state_province'] = (string) $xml->Address->State; | |
129 | $values['postal_code'] = (string) $xml->Address->Zip5; | |
130 | $values['postal_code_suffix'] = (string) $xml->Address->Zip4; | |
6a488035 TO |
131 | |
132 | if (array_key_exists('Address1', $xml->Address)) { | |
e7292422 | 133 | $values['supplemental_address_1'] = (string) $xml->Address->Address1; |
6a488035 TO |
134 | } |
135 | ||
136 | return TRUE; | |
137 | } | |
96025800 | 138 | |
6a488035 | 139 | } |