-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitelity-api.inc
313 lines (276 loc) · 9.64 KB
/
vitelity-api.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Vitelity API PHP Class
* Based on: http://api.vitelity.net/apihelp.html
* Written by Peter Beckman <[email protected]>
* August 8, 2006
*
* This work is licensed under a
* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
* http://creativecommons.org/licenses/by-nc-sa/3.0/us/
*
* For Commercial Use, please see the README file under DONATIONS:
* http://github.com/ooglek/PHP-Vitelity-API/tree/master
*
* Requires PEAR HTTP_Request Library, http://pear.php.net/package/HTTP_Request
*
* All functions return FALSE on failure, or on success, either:
*
* 1 the command returned "success"
* array an array containing the list returned from the command
*
* If a failure occurs, $this->errormsg will be set with the reason it failed.
*
* USAGE:
*
* require_once('vitelity-api.inc');
* $v = new vitelityapi;
* $ratecenters = $v->listratecenters('VA');
*
*/
require_once("HTTP/Request.php");
class vitelityapi {
/**
* If testmode is on, returns success for API functions that would
* charge your account or change your account in any way. The functions
* that will not actually execute but return success are:
* gettollfree, getlocaldid, removedid, requestvanity, localbackorder,
* reroute, routeall
*/
var $testmode = 0;
var $debug = 0;
var $login = '';
var $pass = '';
var $url = 'http://api.vitelity.net/api.php';
var $r = '';
var $params = '';
var $maxretries = 3; // Number of times to retry the HTTP request if it fails
function listratecenters($state) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'state' => $state
));
}
function listavailratecenters($state) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'state' => $state
));
}
function getlocaldid($did, $type='perminute') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did,
'type' => $type
));
}
function removedid($did) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did
));
}
function listlocal($state, $ratecenter='', $withrates='no', $type='perminute') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'state' => $state,
'ratecenter' => $ratecenter,
'withrates' => $withrates,
'type' => $type
));
}
function localbackorder($state, $ratecenter, $type='perminute') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'state' => $state,
'ratecenter' => $ratecenter,
'type' => $type
));
}
function listtollfree() {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__
));
}
function gettollfree($did) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did
));
}
function searchtoll($did) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did
));
}
function requestvanity($did) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did
));
}
function listavailstates($type='perminute') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'type' => $type
));
}
function liststates($type='perminute') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'type' => $type
));
}
function cnam($did) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'did' => $did
));
}
function reroute($did, $routesip) {
return $this->_handleQuery(array(
'cmd' =>__FUNCTION__,
'did' =>$did,
'routesip' =>$routesip
));
}
function routeall($routesip) {
return $this->_handleQuery(array(
'cmd' =>__FUNCTION__,
'routesip' =>$routesip
));
}
function balance() {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__
));
}
function listdids($extra='yes') {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'extra' => $extra
));
}
function getrate($number) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'number' => $number
));
}
function subaccounts($do) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'do' => $do
));
}
function callfromclick($callbacknumber, $calltonumber) {
return $this->_handleQuery(array(
'cmd' => __FUNCTION__,
'number' => $callbacknumber,
'servicenumber' => $calltonumber
));
}
/***************************************
* These functions are private; you *
* can access them, but you shouldn't. *
***************************************/
function _handleQuery($params) {
$this->params = $params;
$testfunctions = array('gettollfree', 'getlocaldid', 'removedid', 'requestvanity', 'localbackorder', 'reroute',
'routeall');
if ($this->testmode and in_array($params['cmd'], $testfunctions)) {
$this->_log("Testmode Active, returning 1 for {$params['cmd']}");
return 1;
}
$r =& new HTTP_Request($this->url);
$r->addHeader('User-Agent', 'PHP-Vitelity-API class http://github.com/ooglek/PHP-Vitelity-API');
$r->setMethod(HTTP_REQUEST_METHOD_POST);
$r->addPostData('login',$this->login);
$r->addPostData('pass',$this->pass);
foreach($params as $k => $v) {
$r->addPostData($k, $v);
}
for ($i=0;$i<$this->maxretries;$i++) {
$reqReturn = $r->sendRequest();
if (!PEAR::isError($reqReturn)) {
if ($this->debug) { $this->_log("Vitelity Parameters: " . print_r($params,1)); }
$response = $this->_parseResponse($r->getResponseBody());
if ($this->debug) { $this->_log("Got Response from Vitelity: " . print_r($response,1)); }
return $response;
}
}
/**
* If we get here, the request has failed $this->maxretries times.
*/
$this->errorstr = "HTTP Request failed: ".$reqReturn->getMessage() . "\nParams:" . print_r($params,1);
$this->_log($this->errorstr, 'error');
return FALSE;
}
function _parseResponse($data) {
if ($this->debug) { $this->_log("Got HTTP Response: $data"); }
$errors = array(
'missingdata' => 'Missing login, pass or cmd parameters in URL',
'invalidauth' => 'Invalid username or password',
'invalid' => 'Invalid parameters?',
'missingrc' => 'Missing or invalid state or ratecenter',
'unavailable' => 'Requested Number Unavailable',
'none' => 'No Toll Free Numbers Available',
'missingdid' => 'Missing did parameter',
);
$ret = explode("[[", $data);
if (!isset($ret[1])) {
$this->errorstr = "Error in return Data: $data";
$this->_log($this->errorstr, 'error');
return FALSE;
}
$res = trim($ret[1]);
if (isset($errors[$res])) {
$this->errorstr = $errors[$res];
$this->_log("Got Non-Success Response: {$this->errorstr} params: " . print_r($this->params, 1), 'info');
return FALSE;
} else {
if ($res == 'success') {
if ($this->debug) { $this->_log("Success!"); }
return 1;
} else {
if ($this->debug) { $this->_log("Returned: $res"); }
$numlist = explode("\n", $res);
/*
* Sometimes AT&T returns letters instead of numbers. We normalize.
*/
$patterns = array('/[a-cA-C]/','/[d-fD-F]/','/[g-iG-I]/','/[j-lJ-L]/',
'/[m-oM-O]/','/[p-sP-S]/','/[t-vT-V]/','/[w-zW-Z]/');
$replacements = array(2,3,4,5,6,7,8,9);
foreach($numlist as $k => $v) {
if (strlen($v) == 10 and is_numeric(substr($v,0,3))) {
$numlist[$k] = preg_replace($patterns, $replacements, $v);
}
}
return $numlist;
}
}
}
/**
* Pseudo logging functionality. Override this in your own implementation to get logging.
* For example:
* class vitelityapiextended extends vitelityapi {
* function _log($msg, $level='debug') {
* echo $msg."\n";
* // If you want to get the default logging too, keep this line
* parent::_log($msg, $level);
* }
* }
*
* @param string $msg The log message
* @param string $level Syslog log levels:
* debug, info, notice, warning,
* err, error, crit, critical,
* alert, emerg, emergency
* @param boolean Returns TRUE
*/
function _log($msg, $level='debug') {
error_log($level . ': ' . $msg);
return TRUE;
}
}