Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // Sipmody.php
- // Created by Brett Kokinadis (brett@attendme.io)
- // Git openfax/sipmodify
- // July 19, 2016
- //
- // This API talks to the OpenSips Core server's psqldb to activate and deactivate
- // IVR/Fax Removal Service Numbers and Inbound Fax Numbers.
- //
- // Security & Access Notes: This service uses the sip1.security table with group
- // type Outboundcoreserver. You will need to add
- // IPs of the application servers that contact this
- // webservice. Firewall updates take 5 minutes to sync.
- //
- // Published URL: https://smod.openfax.com/api/
- // Example call: https://smod.openfax.com/api/?apikey=1&action=c&type=f&did=5554443333
- //
- // Passing Params: api=apikey !api key as sepecified in this file
- // action=c !request to create/provision number
- // or action=d !request to delete/de-provision number
- // type=i !request pertains to an inbound ivr/fax removal number
- // or type=f !request pertains to an inbound fax DID
- // did=npanxxxxxxx !though we do sterialize this number, we are
- // expected 10 digits. Ie. 5206651945
- //
- //
- // Responses: json encoded {"Result":"##","Description":"Value"}
- //
- // Value Description
- // ----------------------------------------------------------------------------------
- // -1 Invalid API Key
- //
- // -2 DID Invalid - Insufficient digits to sterilize.
- // Input: NPANXXXXX Sertialized Output: +1NPANXXXXXX'
- // -3 Cannot add, DID already exists in table
- //
- // -4 Requested function not yet supported in this API Version
- //
- //
- //
- //Static Settings
- $dbcon = 'host=localhost port=5432 dbname=opensips user=XXXX password=XXXX!';
- $apikey='XXXXX';
- // Grabs information
- $api = $_GET['api']; // API key
- $action = $_GET['action']; // To c=create or d=delete entry
- $type = $_GET ['type']; // f = inbound fax did, i = ivr fax removal
- $phonenumber = $_GET['did']; // expecting NPANXXXXXX
- $action = strtoupper($action); // makes action paramater case insenstitive
- $type = strtoupper($type); // makes type paramater case insenstitive
- //Validate API key
- if ($api != $apikey) {
- $status = '-1';
- returnHandler($status);
- } else {
- //Sterilize DID for Opensip e.164 standard, this will work with North American numbers only
- $did = preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '+1$1$2$3', $phonenumber);
- // Make sure we got 12 characters after sterilization, expecting NPANXXXXX input, ouput +1NPANXXXXXX
- if (strlen($did) < 12) {
- $status = '-2';
- returnHandler($status);
- }
- //Determine CRUD action
- //Action to create entry
- if ($action == 'C') {
- if ($type == 'F') {
- //Create Fax DID Entry
- createEntry ($dbcon, $did, $type);
- } elseif ($type == 'I') {
- //Create IVR DID
- createEntry ($dbcon, $did, $type);
- } else {
- //Invalud TYPE value (F)ax or (I)vr
- $status = '-6';
- returnHandler($status);
- }
- //Action Delete DID
- } elseif ($action == 'D') {
- if ($type == 'F') {
- //Delete Fax DID
- deleteEntry ($dbcon, $did,$type);
- } elseif ($type == 'I') {
- //Delete IVR DID
- deleteEntry ($dbcon, $did,$type);
- } else {
- //Not a valid DID format
- $status = '-6';
- returnHandler($status);
- }
- } else {
- //Not a valid Action
- $status = '-7';
- returnHandler($status);
- }
- }
- function createEntry ($dbcon, $did,$type) {
- //connect to opensips psql db
- $conn = pg_connect($dbcon);
- //Complain about db connection error
- if (!$conn) {
- $status = '0';
- returnHandler($status);
- }
- //Search if entry already exists
- $result = pg_query($conn,"SELECT did FROM opensips_dids where did='".$did."'");
- echo $did;
- if (!$result) {
- pg_close($conn);
- $status = '0';
- returnHandler($status);
- }
- $row = pg_fetch_array($result);
- // Cannot add, DID already exists in table.
- if ($row[0] == $did) {
- pg_close($conn);
- $status = '-3';
- returnHandler($status);
- }
- //Adds entry for Fax DID
- if ($type = 'F') {
- $addFax = pg_query($conn, "insert into opensips_dids (did, status, setid) values ('".$did."',0,20)");
- pg_close($conn);
- $status = '1';
- returnHandler($status); //Added FaxDID successfully
- } elseif ($type = 'I') {
- $addIVR = pg_query($conn, "insert into opensips_dids (did, status, setid) values ('".$did."',0,30)");
- pg_close($conn);
- $status = '1';
- returnHandler($status); //Added IVR successfully
- }
- else {
- // Future ivr settings, opensip rules not yet defined
- pg_close($conn);
- $status = '-4';
- returnHandler($status); //Cannot add, feature unsupported at this time.
- }
- }
- function deleteEntry ($dbcon, $did,$type) {
- //connect to opensips psql db
- $conn = pg_connect($dbcon) or returnHandler('0');
- //Complain about db connection error
- if (!$conn) {
- pg_close($conn);
- $status = '0';
- returnHandler($status);
- }
- //Search if entry already exists
- $result = pg_query($conn,"SELECT did FROM opensips_dids where did='".$did."' and setid='".$settype.'"');
- if (!$result) {
- pg_close($conn);
- $status = '0';
- returnHandler($status);
- }
- $row = pg_fetch_array($result);
- // Cannot delete, DID does not exists in table.
- if ($row[0] != $did) {
- pg_close($conn);
- $status = '-5';
- returnHandler($status);
- }
- //Delete entry for Fax DID
- if ($type = 'F') {
- $delFax = pg_query($conn, "delete from opensips_dids where did = '".$did."'");
- pg_close($conn);
- $status = '1';
- returnHandler($status);
- } elseif ($type = 'I') {
- $delIVR = pg_query($conn, "delete from opensips_dids where did = '".$did."'");
- pg_close($conn);
- $status = '1';
- returnHandler($status);
- } else {
- // Future ivr settings, opensip rules not yet defined
- pg_close($conn);
- $status = '-4';
- returnHandler($status);
- }
- }
- function returnHandler ($status) {
- unset ($jsonResponse);
- //Define description responses for JSON
- if ($status == '-1' ) {
- $description ='Invalid API Key';
- }
- if ($status == '-2') {
- $description = 'DID Invalid - Insufficient digits to sterilize. Input: NPANXXXXX Sertialized Output: +1NPANXXXXXX';
- }
- if ($status == '-3') {
- $description = 'Cannot add, DID already exists in table';
- }
- if ($status == '-4') {
- $description = 'Requested function not yet supported in this api version';
- }
- if ($status == '-5') {
- $description = 'Cannot delete, DID does not exists in table';
- }
- if ($status == '-6') {
- $description = 'Invalid DID type, type value (F)ax or (I)vr.';
- }
- if ($status == '-7') {
- $description = 'Invalid Action type, type value (C)reate or (D)elete.';
- }
- if ($status == '0') {
- $description = 'An error occurred while working with the database';
- }
- if ($status == '1') {
- $description = 'Request processed successfully';
- }
- //Build & reply with json response
- $jsonResponse = array('Result' => $status, 'Description' => $description);
- echo json_encode($jsonResponse);
- die ();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement