· 10 years ago · Jul 19, 2016, 08:33 PM
1<?php
2//
3// Sipmody.php
4// Created by Brett Kokinadis (brett@attendme.io)
5// Git openfax/sipmodify
6// July 19, 2016
7//
8// This API talks to the OpenSips Core server's psqldb to activate and deactivate
9// IVR/Fax Removal Service Numbers and Inbound Fax Numbers.
10//
11// Security & Access Notes: This service uses the sip1.security table with group
12// type Outboundcoreserver. You will need to add
13// IPs of the application servers that contact this
14// webservice. Firewall updates take 5 minutes to sync.
15//
16// Published URL: https://smod.openfax.com/api/
17// Example call: https://smod.openfax.com/api/?apikey=1&action=c&type=f&did=5554443333
18//
19// Passing Params: api=apikey !api key as sepecified in this file
20// action=c !request to create/provision number
21// or action=d !request to delete/de-provision number
22// type=i !request pertains to an inbound ivr/fax removal number
23// or type=f !request pertains to an inbound fax DID
24// did=npanxxxxxxx !though we do sterialize this number, we are
25// expected 10 digits. Ie. 5206651945
26//
27//
28// Responses: json encoded {"Result":"##","Description":"Value"}
29//
30// Value Description
31// ----------------------------------------------------------------------------------
32// -1 Invalid API Key
33//
34// -2 DID Invalid - Insufficient digits to sterilize.
35// Input: NPANXXXXX Sertialized Output: +1NPANXXXXXX'
36// -3 Cannot add, DID already exists in table
37//
38// -4 Requested function not yet supported in this API Version
39//
40//
41//
42//Static Settings
43$dbcon = 'host=localhost port=5432 dbname=opensips user=XXXX password=XXXX!';
44$apikey='XXXXX';
45
46
47// Grabs information
48$api = $_GET['api']; // API key
49$action = $_GET['action']; // To c=create or d=delete entry
50$type = $_GET ['type']; // f = inbound fax did, i = ivr fax removal
51$phonenumber = $_GET['did']; // expecting NPANXXXXXX
52
53
54$action = strtoupper($action); // makes action paramater case insenstitive
55$type = strtoupper($type); // makes type paramater case insenstitive
56
57
58//Validate API key
59if ($api != $apikey) {
60 $status = '-1';
61 returnHandler($status);
62} else {
63
64//Sterilize DID for Opensip e.164 standard, this will work with North American numbers only
65$did = preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '+1$1$2$3', $phonenumber);
66
67// Make sure we got 12 characters after sterilization, expecting NPANXXXXX input, ouput +1NPANXXXXXX
68
69if (strlen($did) < 12) {
70 $status = '-2';
71 returnHandler($status);
72}
73
74//Determine CRUD action
75
76 //Action to create entry
77 if ($action == 'C') {
78
79 if ($type == 'F') {
80 //Create Fax DID Entry
81 createEntry ($dbcon, $did, $type);
82 } elseif ($type == 'I') {
83 //Create IVR DID
84 createEntry ($dbcon, $did, $type);
85 } else {
86 //Invalud TYPE value (F)ax or (I)vr
87 $status = '-6';
88 returnHandler($status);
89 }
90
91 //Action Delete DID
92 } elseif ($action == 'D') {
93
94 if ($type == 'F') {
95 //Delete Fax DID
96 deleteEntry ($dbcon, $did,$type);
97 } elseif ($type == 'I') {
98 //Delete IVR DID
99 deleteEntry ($dbcon, $did,$type);
100 } else {
101 //Not a valid DID format
102 $status = '-6';
103 returnHandler($status);
104 }
105
106 } else {
107 //Not a valid Action
108 $status = '-7';
109 returnHandler($status);
110 }
111 }
112
113
114
115function createEntry ($dbcon, $did,$type) {
116 //connect to opensips psql db
117 $conn = pg_connect($dbcon);
118
119 //Complain about db connection error
120 if (!$conn) {
121 $status = '0';
122 returnHandler($status);
123 }
124
125 //Search if entry already exists
126 $result = pg_query($conn,"SELECT did FROM opensips_dids where did='".$did."'");
127 echo $did;
128 if (!$result) {
129 pg_close($conn);
130 $status = '0';
131 returnHandler($status);
132 }
133
134 $row = pg_fetch_array($result);
135
136 // Cannot add, DID already exists in table.
137 if ($row[0] == $did) {
138 pg_close($conn);
139 $status = '-3';
140 returnHandler($status);
141 }
142
143 //Adds entry for Fax DID
144 if ($type = 'F') {
145 $addFax = pg_query($conn, "insert into opensips_dids (did, status, setid) values ('".$did."',0,20)");
146 pg_close($conn);
147 $status = '1';
148 returnHandler($status); //Added FaxDID successfully
149
150 } elseif ($type = 'I') {
151 $addIVR = pg_query($conn, "insert into opensips_dids (did, status, setid) values ('".$did."',0,30)");
152 pg_close($conn);
153 $status = '1';
154 returnHandler($status); //Added IVR successfully
155 }
156
157 else {
158 // Future ivr settings, opensip rules not yet defined
159 pg_close($conn);
160 $status = '-4';
161 returnHandler($status); //Cannot add, feature unsupported at this time.
162 }
163}
164
165function deleteEntry ($dbcon, $did,$type) {
166 //connect to opensips psql db
167 $conn = pg_connect($dbcon) or returnHandler('0');
168
169 //Complain about db connection error
170 if (!$conn) {
171 pg_close($conn);
172 $status = '0';
173 returnHandler($status);
174
175 }
176
177 //Search if entry already exists
178 $result = pg_query($conn,"SELECT did FROM opensips_dids where did='".$did."' and setid='".$settype.'"');
179
180 if (!$result) {
181 pg_close($conn);
182 $status = '0';
183 returnHandler($status);
184 }
185
186 $row = pg_fetch_array($result);
187
188 // Cannot delete, DID does not exists in table.
189
190
191
192 if ($row[0] != $did) {
193 pg_close($conn);
194 $status = '-5';
195 returnHandler($status);
196 }
197
198 //Delete entry for Fax DID
199 if ($type = 'F') {
200 $delFax = pg_query($conn, "delete from opensips_dids where did = '".$did."'");
201 pg_close($conn);
202 $status = '1';
203 returnHandler($status);
204
205 } elseif ($type = 'I') {
206 $delIVR = pg_query($conn, "delete from opensips_dids where did = '".$did."'");
207 pg_close($conn);
208 $status = '1';
209 returnHandler($status);
210
211 } else {
212
213 // Future ivr settings, opensip rules not yet defined
214 pg_close($conn);
215 $status = '-4';
216 returnHandler($status);
217 }
218}
219
220
221function returnHandler ($status) {
222 unset ($jsonResponse);
223 //Define description responses for JSON
224
225 if ($status == '-1' ) {
226 $description ='Invalid API Key';
227 }
228
229 if ($status == '-2') {
230 $description = 'DID Invalid - Insufficient digits to sterilize. Input: NPANXXXXX Sertialized Output: +1NPANXXXXXX';
231 }
232
233 if ($status == '-3') {
234 $description = 'Cannot add, DID already exists in table';
235 }
236
237 if ($status == '-4') {
238 $description = 'Requested function not yet supported in this api version';
239 }
240
241 if ($status == '-5') {
242 $description = 'Cannot delete, DID does not exists in table';
243 }
244
245 if ($status == '-6') {
246 $description = 'Invalid DID type, type value (F)ax or (I)vr.';
247 }
248
249 if ($status == '-7') {
250 $description = 'Invalid Action type, type value (C)reate or (D)elete.';
251 }
252
253 if ($status == '0') {
254 $description = 'An error occurred while working with the database';
255 }
256 if ($status == '1') {
257 $description = 'Request processed successfully';
258 }
259
260 //Build & reply with json response
261 $jsonResponse = array('Result' => $status, 'Description' => $description);
262 echo json_encode($jsonResponse);
263 die ();
264}
265?>