Newer
Older
ubFramework / Portal / docroot / user / voip / devices_json.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 11 KB Cleaning Up Making It A Sub Module
<?php

/*
 * ****************************************************************************************
 * Copyright (c) 2013, 2014 Christopher W. Olsen <cwolsen@ubixtechnologies.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions, the following disclaimer and the list of authors. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions, the following
 * disclaimer and the list of authors in the documentation and/or other materials provided
 * with the distribution. Neither the name of the uBix Cube Project nor the names of its
 * contributors may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: devices_json.php 475 2016-03-30 03:08:32Z reddawg $
 *
 * ***************************************************************************************
 */

/* Main include info */
ini_set ( 'include_path', $_SERVER ['DOCUMENT_ROOT'] . "/include/" );
include ("config.php");

$ubF->session->Validate_JSON ( 'U_V_', 1 );

switch ($ubF->data ['req']) {
  case 'accounts' :
    $json_data ['data'] = $ubF->libs ['voip']->getAccounts ( 0, 1 );
    break;
  case 'accountCodes' :
    $json_data ['data'] = $ubF->libs ['voip']->getAccountCodes ( 0, 1, 1 );
    break;
  case 'accountInfo' :
    $query = "SELECT dialplan, dialplan_timeout FROM accounts WHERE account_number = '" . $ubF->data ['account_number'] . "'";
    $result = $ubF->DB ['main']->query ( $query );
    $json_data ['data'] = $result->fetch_assoc ();
    break;
  case 'peers' :
    if (isset($ubF->data['aL']) && $ubF->data ['aL'] == 0)
      $json_data ['data'] = $ubF->libs ['voip']->getExtensions ( '', '', true, true );
    else
      $json_data ['data'] = $ubF->libs ['voip']->getExtensions ( $ubF->session->account_number, '', false, true );
    break;
  case 'peerInfo' :
    $json_data ['data'] = $ubF->libs ['voip']->peerInfo ( $ubF->session->account_code . $ubF->data ['peer'], $ubF->data ['uas'], $ubF->session->account_code );
    break;
  case 'device' :
    $query = "SELECT vd.account_number, vd.mac, vd.extensions, vd.dialplan, vd.dialplan_timeout, a.dialplan as a_dp, a.dialplan_timeout as a_dpt, a.server_primary as a_server_primary, a.transport_primary as a_transport_primary, a.server_port_primary as a_server_port_primary, a.server_backup as a_server_backup, a.transport_backup as a_transport_backup, a.server_port_backup as a_server_port_backup, vd.server_primary, vd.server_backup, vd.last_config, vd.transport_primary, vd.transport_backup, vd.account_locked, vd.stocked_in, vd.vdtid, vd.stocked_in, vd.device_owner, vdt.manufacturer, vdt.model FROM voip_devices vd LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid LEFT JOIN accounts a ON vd.account_number = a.account_number WHERE vd.mac = '" . $ubF->data ['mac'] . "'";
    $result = $ubF->DB ['main']->query ( $query );
    $json_data ['data'] = $result->fetch_assoc ();
    $json_data ['data'] ['lines'] = $ubF->libs ['ubf']->voip_deviceLines ( $json_data ['data'] ['vdtid'] );
    $json_data ['data'] ['stocked_in'] = date ( "Y-m-d H:i:s", $json_data ['data'] ['stocked_in'] );
    $json_data ['data'] ['last_config'] = date ( "Y-m-d H:i", $json_data ['data'] ['last_config'] );
    $json_data ['data'] ['extensions'] = json_decode ( $json_data ['data'] ['extensions'] );
    
    if ($json_data ['data'] ['dialplan'] == "")
      $json_data ['data'] ['dialplan'] = $json_data ['data'] ['a_dp'];
    
    if ($json_data ['data'] ['dialplan_timeout'] == "")
      $json_data ['data'] ['dialplan_timeout'] = $json_data ['data'] ['a_dpt'];
    $result->free ();
    break;
  case 'deviceTypes' :
    $json_data ['data'] = $ubF->libs ['voip']->deviceList ( 0, 1, 1 );
    break;
  case 'deviceList' :
    $query = "SELECT * FROM voip_devices vd LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid WHERE vd.account_number = '" . $ubF->session->account_number . "'";
    
    $result = $ubF->DB ['main']->query ( $query );
    
    while ( $qData = $result->fetch_assoc () ) {
      $extension = json_decode ( $qData ['extensions'], true );
      
      $lexts = "";
      $lkeys = "";
      $lps = "";
      $lbs = "";
      $k = 1;
      
      foreach ( $extension as $ext => $keys ) {
        if ($keys ['extension'] != '') {
          $act = preg_replace ( '/[0-9]/', "", $keys ['extension'] );
          $ext = preg_replace ( '/\D/', '', $keys ['extension'] );
          $lexts .= '<div title="Account: ' . $act . '&#10;Peer: ' . $ext . '&#10;Label: ' . $keys ['label'] . '">' . $ext . '</div>';
          $lkeys .= $k . " - " . $keys ['line_keys'] . "<br />";
          $lps .= explode ( '.', $keys ['server_primary'] ) [0] . "<br />";
          $lbs .= explode ( '.', $keys ['server_backup'] ) [0] . "<br />";
          $k++;
        }
      }
      
      $rows [] = array (
        $qData['vdid'],
        "<a class=\"edit_device\" id=\"" . $qData ['mac'] . "\">" . $qData ['mac'] . "</a>",
        $qData ['manufacturer'] . " " . $qData ['model'],
        $lkeys,
        $lexts,
        date ( "m/d/Y H:i:s", $qData ['last_config'] ),
        "<a class=\"view_log\" id=\"" . $qData ['mac'] . "-app\">View</a>",
        "<a class=\"view_log\" id=\"" . $qData ['mac'] . "-boot\">View</a>" 
      );
    }
    
    $result->free ();
    $json_data ['data'] = $rows;
    break;
  case 'edit_device' :
    $json_data ['data'] = json_encode ( $ubF->data );
    
    $vdtid = explode ( ':', $ubF->data ['vdtid'] );
    
    for($i = 1; $i <= count ( $ubF->data ['keys'] ); $i++) {
      $eI = explode ( ":", $ubF->data ['keys'] [$i] ['extension'] );
      system ( 'echo "eI[0]: ' . $eI [0] . ', eI[1]: ' . $eI [1] . '" >> /tmp/JSON.log' );
      
      $ubF->data ['keys'] [$i] ['extension'] = $eI [0];
      $ubF->data ['keys'] [$i] ['voip_code'] = $eI [1];
      $ubF->data ['keys'] [$i] ['secret'] = $ubF->libs ['ubf']->voip_getSecret ( $eI [0] );
      
      if ($ubF->data ['keys'] [$i] ['uas'] == 1) {
        $query = "SELECT server_primary, server_backup, transport_primary, transport_backup, server_port_primary, server_port_backup FROM accounts WHERE voip_code = '" . $eI [1] . "'";
        $res = $ubF->DB ['main']->query ( $query );
        $qD = $res->fetch_row ();
        
        $ubF->data ['keys'] [$i] ['server_primary'] = $qD [0];
        $ubF->data ['keys'] [$i] ['server_backup'] = $qD [1];
        $ubF->data ['keys'] [$i] ['transport_primary'] = $qD [2];
        $ubF->data ['keys'] [$i] ['transport_backup'] = $qD [3];
        $ubF->data ['keys'] [$i] ['server_port_primary'] = $qD [4];
        $ubF->data ['keys'] [$i] ['server_port_backup'] = $qD [5];
        $res->free ();
        system ( 'echo "eI[0]: ' . $eI [0] . ', eI[1]: ' . $eI [1] . ', JSON: ' . json_encode ( $ubF->data ['keys'] ) . '" >> /tmp/JSON.log' );
      }
    }
    
    $query = "UPDATE voip_devices SET extensions = '" . json_encode ( $ubF->data ['keys'] ) . "', account_number = '" . $ubF->data ['account_number'] . "', account_locked = " . $ubF->data ['account_locked'] . ", vdtid = " . $vdtid [0] . ", dialplan = '" . $ubF->data ['dialplan'] . "', dialplan_timeout = '" . $ubF->data ['dialplan_timeout'] . "' WHERE mac = '" . $ubF->data ['mac'] . "'";
    
    if ($ubF->DB ['main']->query ( $query ) === true) {
      $jD ['msg'] = "Device Updated.";
      $jD ['code'] = 0;
    }
    else {
      $jD ['msg'] = "Error Updating Device.";
      $jD ['code'] = 0;
    }
    $json_data ['data'] = $jD;
    break;
  case 'view_log' :
    $query = "SELECT log FROM voip_device_logs WHERE mac = '" . $ubF->data ['mac'] . "' AND log_type = '" . $ubF->data ['log'] . "'";
    $result = $ubF->DB ['main']->query ( $query );
    if ($result->num_rows == 0)
      $json_data ['data'] = "No Log";
    else {
      $row = $result->fetch_row ();
      $json_data ['data'] = $row [0];
      $result->free ();
    }
    
    break;
  case 'add_device' :
    $json_data ['data'] = json_encode ( $ubF->data );
    
    $vdtid = explode ( ':', $ubF->data ['vdtid'] );
    
    for($i = 1; $i <= count ( $ubF->data ['keys'] ); $i++) {
      $eI = explode ( ":", $ubF->data ['keys'] [$i] ['extension'] );
      
      $ubF->data ['keys'] [$i] ['extension'] = $eI [0];
      $ubF->data ['keys'] [$i] ['voip_code'] = $eI [1];
      $ubF->data ['keys'] [$i] ['secret'] = $ubF->libs ['ubf']->voip_getSecret ( $eI [0] );
      
      if ($ubF->data ['keys'] [$i] ['uas'] == 1) {
        $query = "SELECT server_primary, server_backup, transport_primary, transport_backup, server_port_primary, server_port_backup FROM accounts WHERE voip_code = '" . $eI [1] . "'";
        $res = $ubF->DB ['main']->query ( $query );
        $qD = $res->fetch_row ();
        
        $ubF->data ['keys'] [$i] ['server_primary'] = $qD [0];
        $ubF->data ['keys'] [$i] ['server_backup'] = $qD [1];
        $ubF->data ['keys'] [$i] ['transport_primary'] = $qD [2];
        $ubF->data ['keys'] [$i] ['transport_backup'] = $qD [3];
        $ubF->data ['keys'] [$i] ['server_port_primary'] = $qD [4];
        $ubF->data ['keys'] [$i] ['server_port_backup'] = $qD [5];
        $res->free ();
      }
    }
    
    // $query = "UPDATE voip_devices SET extensions = '" . json_encode($ubF->data['keys']) . "', account_number = '" . $ubF->data['account_number'] . "', account_locked = " . $ubF->data['account_locked'] . ", vdtid = " . $vdtid[0] . ", dialplan = '" . $ubF->data['dialplan'] . "', dialplan_timeout = '" . $ubF->data['dialplan_timeout'] . "' WHERE mac = '" . $ubF->data['mac'] . "'";
    $query = "INSERT INTO voip_devices (extensions, account_number,account_locked,vdtid,dialplan,dialplan_timeout,mac) VALUES('" . json_encode ( $ubF->data ['keys'] ) . "', '" . $ubF->data ['account_number'] . "', " . $ubF->data ['account_locked'] . ", " . $vdtid [0] . ", '" . $ubF->data ['dialplan'] . "', '" . $ubF->data ['dialplan_timeout'] . "', '" . $ubF->data ['mac'] . "')";
    
    if ($ubF->DB ['main']->query ( $query ) === true) {
      $jD ['msg'] = "Device Added.";
      $jD ['code'] = 0;
    }
    else {
      $jD ['msg'] = "Error Addinging Device.";
      $jD ['code'] = 0;
    }
    $json_data ['data'] = $jD;
    break;
  default :
    $json_data ['data'] = array (
      0 
    );
    break;
}

print (json_encode ( $json_data )) ;

?>