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