Merge pull request #9632 from yashodha/CRM-19795
[civicrm-core.git] / CRM / Utils / Address / USPS.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2017
31 */
32
33 /**
34 * Address utilities.
35 */
36 class CRM_Utils_Address_USPS {
37
38 /**
39 * Whether USPS validation should be disabled during import.
40 *
41 * @var bool
42 */
43 protected static $_disabled = FALSE;
44
45 /**
46 * @param $disabled
47 *
48 * @return void
49 */
50 public static function disable($disable = TRUE) {
51 self::$_disabled = $disable;
52 }
53
54 /**
55 * @param $values
56 *
57 * @return bool
58 */
59 public static function checkAddress(&$values) {
60 if (self::$_disabled) {
61 return FALSE;
62 }
63 if (!isset($values['street_address']) ||
64 (!isset($values['city']) &&
65 !isset($values['state_province']) &&
66 !isset($values['postal_code'])
67 )
68 ) {
69 return FALSE;
70 }
71
72 $userID = Civi::settings()->get('address_standardization_userid');
73 $url = Civi::settings()->get('address_standardization_url');
74
75 if (empty($userID) ||
76 empty($url)
77 ) {
78 return FALSE;
79 }
80
81 $address2 = str_replace(',', '', $values['street_address']);
82
83 $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>';
84
85 require_once 'HTTP/Request.php';
86 $request = new HTTP_Request();
87
88 $request->setURL($url);
89
90 $request->addQueryString('API', 'Verify');
91 $request->addQueryString('XML', $XMLQuery);
92
93 $response = $request->sendRequest();
94
95 $session = CRM_Core_Session::singleton();
96
97 $code = $request->getResponseCode();
98 if ($code != 200) {
99 $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1',
100 array(1 => $code)
101 ));
102 return FALSE;
103 }
104
105 $responseBody = $request->getResponseBody();
106
107 $xml = simplexml_load_string($responseBody);
108
109 if (is_null($xml) || is_null($xml->Address)) {
110 $session->setStatus(ts('Your USPS API Lookup has Failed.'));
111 return FALSE;
112 }
113
114 if ($xml->Number == '80040b1a') {
115 $session->setStatus(ts('Your USPS API Authorization has Failed.'));
116 return FALSE;
117 }
118
119 if (array_key_exists('Error', $xml->Address)) {
120 $session->setStatus(ts('Address not found in USPS database.'));
121 return FALSE;
122 }
123
124 $values['street_address'] = (string) $xml->Address->Address2;
125 $values['city'] = (string) $xml->Address->City;
126 $values['state_province'] = (string) $xml->Address->State;
127 $values['postal_code'] = (string) $xml->Address->Zip5;
128 $values['postal_code_suffix'] = (string) $xml->Address->Zip4;
129
130 if (array_key_exists('Address1', $xml->Address)) {
131 $values['supplemental_address_1'] = (string) $xml->Address->Address1;
132 }
133
134 return TRUE;
135 }
136
137 }