Merge pull request #20115 from larssandergreen/fix-internal-anchor-URLs-in-mailings
[civicrm-core.git] / CRM / Utils / Address / USPS.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6a488035 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
d4620d57 18 * Address utilities.
6a488035
TO
19 */
20class CRM_Utils_Address_USPS {
21
5bc392e6 22 /**
fd830836 23 * Whether USPS validation should be disabled during import.
d4620d57 24 *
fd830836
DRJ
25 * @var bool
26 */
27 protected static $_disabled = FALSE;
28
29 /**
bf48aa29 30 * Disable the USPS validation.
fd830836 31 *
bf48aa29 32 * @param bool $disable
fd830836
DRJ
33 */
34 public static function disable($disable = TRUE) {
35 self::$_disabled = $disable;
36 }
37
38 /**
bf48aa29 39 * Check address against USPS.
40 *
41 * @param array $values
5bc392e6
EM
42 *
43 * @return bool
44 */
00be9182 45 public static function checkAddress(&$values) {
fd830836
DRJ
46 if (self::$_disabled) {
47 return FALSE;
48 }
6a488035
TO
49 if (!isset($values['street_address']) ||
50 (!isset($values['city']) &&
51 !isset($values['state_province']) &&
52 !isset($values['postal_code'])
53 )
54 ) {
55 return FALSE;
56 }
57
aaffa79f 58 $userID = Civi::settings()->get('address_standardization_userid');
59 $url = Civi::settings()->get('address_standardization_url');
6a488035
TO
60
61 if (empty($userID) ||
62 empty($url)
63 ) {
64 return FALSE;
65 }
66
67 $address2 = str_replace(',', '', $values['street_address']);
68
69 $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>';
70
80d51c38
SL
71 $client = new GuzzleHttp\Client();
72 $request = $client->request('GET', $url, [
73 'query' => [
74 'API' => 'Verify',
75 'XML' => $XMLQuery,
76 ],
0b181297 77 'timeout' => \Civi::settings()->get('http_timeout'),
80d51c38 78 ]);
6a488035
TO
79
80 $session = CRM_Core_Session::singleton();
81
80d51c38 82 $code = $request->getStatusCode();
6a488035
TO
83 if ($code != 200) {
84 $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1',
be2fb01f 85 [1 => $code]
353ffa53 86 ));
6a488035
TO
87 return FALSE;
88 }
89
80d51c38 90 $responseBody = $request->getBody();
6a488035
TO
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}