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