Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 |
e7112fa7 | 30 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
31 | */ |
32 | ||
33 | /** | |
34 | * Address utilties | |
35 | */ | |
36 | class CRM_Utils_Address_USPS { | |
37 | ||
5bc392e6 EM |
38 | /** |
39 | * @param $values | |
40 | * | |
41 | * @return bool | |
42 | */ | |
00be9182 | 43 | public static function checkAddress(&$values) { |
6a488035 TO |
44 | if (!isset($values['street_address']) || |
45 | (!isset($values['city']) && | |
46 | !isset($values['state_province']) && | |
47 | !isset($values['postal_code']) | |
48 | ) | |
49 | ) { | |
50 | return FALSE; | |
51 | } | |
52 | ||
6a488035 TO |
53 | $userID = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME, |
54 | 'address_standardization_userid' | |
55 | ); | |
56 | $url = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME, | |
57 | 'address_standardization_url' | |
58 | ); | |
59 | ||
60 | if (empty($userID) || | |
61 | empty($url) | |
62 | ) { | |
63 | return FALSE; | |
64 | } | |
65 | ||
66 | $address2 = str_replace(',', '', $values['street_address']); | |
67 | ||
68 | $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>'; | |
69 | ||
70 | require_once 'HTTP/Request.php'; | |
71 | $request = new HTTP_Request(); | |
72 | ||
73 | $request->setURL($url); | |
74 | ||
75 | $request->addQueryString('API', 'Verify'); | |
76 | $request->addQueryString('XML', $XMLQuery); | |
77 | ||
78 | $response = $request->sendRequest(); | |
79 | ||
80 | $session = CRM_Core_Session::singleton(); | |
81 | ||
82 | $code = $request->getResponseCode(); | |
83 | if ($code != 200) { | |
84 | $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1', | |
353ffa53 TO |
85 | array(1 => $code) |
86 | )); | |
6a488035 TO |
87 | return FALSE; |
88 | } | |
89 | ||
90 | $responseBody = $request->getResponseBody(); | |
91 | ||
92 | $xml = simplexml_load_string($responseBody); | |
93 | ||
94 | if (is_null($xml) || is_null($xml->Address)) { | |
95 | $session->setStatus(ts('Your USPS API Lookup has Failed.')); | |
96 | return FALSE; | |
97 | } | |
98 | ||
99 | if ($xml->Number == '80040b1a') { | |
100 | $session->setStatus(ts('Your USPS API Authorization has Failed.')); | |
101 | return FALSE; | |
102 | } | |
103 | ||
104 | if (array_key_exists('Error', $xml->Address)) { | |
105 | $session->setStatus(ts('Address not found in USPS database.')); | |
106 | return FALSE; | |
107 | } | |
108 | ||
e7292422 TO |
109 | $values['street_address'] = (string) $xml->Address->Address2; |
110 | $values['city'] = (string) $xml->Address->City; | |
111 | $values['state_province'] = (string) $xml->Address->State; | |
112 | $values['postal_code'] = (string) $xml->Address->Zip5; | |
113 | $values['postal_code_suffix'] = (string) $xml->Address->Zip4; | |
6a488035 TO |
114 | |
115 | if (array_key_exists('Address1', $xml->Address)) { | |
e7292422 | 116 | $values['supplemental_address_1'] = (string) $xml->Address->Address1; |
6a488035 TO |
117 | } |
118 | ||
119 | return TRUE; | |
120 | } | |
96025800 | 121 | |
6a488035 | 122 | } |