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