Newer
Older
ubFramework / Portal / docroot / admin / 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 940 2017-09-07 20:12:42Z reddawg $
 *
 * ***************************************************************************************
 */

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

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

if ( !isset ( $form_data ['ddt'] ) )
  $form_data ['ddt'] = 0;

if ( isset ( $form_data ['req'] ) )
  $form_data ['ddt'] = $form_data ['req'];

switch ( $form_data ['ddt'] ) {
  case 'accounts' :
    $json_data ['data'] = $data->libs['voip']->getAccounts ( 0, 1 );
    break;
  case 'accountCodes' :
    $json_data ['data'] = $data->libs['voip']->getAccountCodes ( 0, 1, 1 );
    break;
  case 'accountInfo' :
    $query = "SELECT dialplan, dialplan_timeout FROM accounts WHERE account_number = '" . $form_data ['account_number'] . "'";
    $result = $ubF->DB['main']->query ( $query );
    $json_data ['data'] = $result->fetch_assoc ();
    break;
  case 'peers' :
    if ( $form_data ['aL'] == 0 )
      $json_data ['data'] = $data->libs['voip']->getExtensions ( '', '', true, true );
    else
      $json_data ['data'] = $data->libs['voip']->getExtensions ( $form_data ['account_number'], '', true, true );
    break;
  case 'peerInfo' :
    $json_data ['data'] = $data->libs['voip']->peerInfo ( $form_data ['peer'], $form_data ['uas'], $form_data ['voip_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 = '" . $form_data ['mac'] . "'";
    $result = $ubF->DB['main']->query ( $query );
    $json_data ['data'] = $result->fetch_assoc ();
    $json_data ['data'] ['lines'] = $data->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'] = $data->libs['voip']->deviceList ( 0, 1, 1 );
    break;
  case 'deviceList' :
    if ( isset ( $form_data ['account_number'] ) && ($form_data ['account_number'] != '' && $form_data ['account_number'] != "null") )
      $query = "SELECT * FROM voip_devices vd LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid WHERE vd.account_number = '" . $form_data ['account_number'] . "'";
    else
      $query = "SELECT * FROM voip_devices vd LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid";

    $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 (
          "<a class=\"edit_device\" id=\"" . $qData ['mac'] . "\">" . $qData ['mac'] . "</a>",
          $qData ['manufacturer'] . " " . $qData ['model'],
          $qData ['account_number'],
          $lkeys,
          $lexts,
          $lps,
          $lbs,
          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 ( $form_data );

    $vdtid = explode ( ':', $form_data ['vdtid'] );

    for ( $i = 1 ; $i <= count ( $form_data ['keys'] ) ; $i++ ) {
      $eI = explode ( ":", $form_data ['keys'] [$i] ['extension'] );
system('echo "eI[0]: ' . $eI[0] . ', eI[1]: ' . $eI[1] . '" >> /tmp/JSON.log');

      $form_data ['keys'] [$i] ['extension'] = $eI [0];
      $form_data ['keys'] [$i] ['voip_code'] = $eI [1];
      $form_data ['keys'] [$i] ['secret'] = $data->libs['ubf']->voip_getSecret ( $eI [0] );

      if ( $form_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 ();

        $form_data ['keys'] [$i] ['server_primary'] = $qD [0];
        $form_data ['keys'] [$i] ['server_backup'] = $qD [1];
        $form_data ['keys'] [$i] ['transport_primary'] = $qD [2];
        $form_data ['keys'] [$i] ['transport_backup'] = $qD [3];
        $form_data ['keys'] [$i] ['server_port_primary'] = $qD [4];
        $form_data ['keys'] [$i] ['server_port_backup'] = $qD [5];
        $res->free ();
system('echo "eI[0]: ' . $eI[0] . ', eI[1]: ' . $eI[1] . ', JSON: ' . json_encode($form_data['keys']) . '" >> /tmp/JSON.log');
      }
    }

    $query = "UPDATE voip_devices SET extensions = '" . json_encode ( $form_data ['keys'] ) . "', account_number = '" . $form_data ['account_number'] . "', account_locked = " . $form_data ['account_locked'] . ", vdtid = " . $vdtid [0] . ", dialplan = '" . $form_data ['dialplan'] . "', dialplan_timeout = '" . $form_data ['dialplan_timeout'] . "' WHERE mac = '" . $form_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 = '" . $form_data ['mac'] . "' AND log_type = '" . $form_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 ( $form_data );

    $vdtid = explode ( ':', $form_data ['vdtid'] );

    for ( $i = 1 ; $i <= count ( $form_data ['keys'] ) ; $i++ ) {
      $eI = explode ( ":", $form_data ['keys'] [$i] ['extension'] );

      $form_data ['keys'] [$i] ['extension'] = $eI [0];
      $form_data ['keys'] [$i] ['voip_code'] = $eI [1];
      $form_data ['keys'] [$i] ['secret'] = $data->libs['ubf']->voip_getSecret ( $eI [0] );

      if ( $form_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 ();

        $form_data ['keys'] [$i] ['server_primary'] = $qD [0];
        $form_data ['keys'] [$i] ['server_backup'] = $qD [1];
        $form_data ['keys'] [$i] ['transport_primary'] = $qD [2];
        $form_data ['keys'] [$i] ['transport_backup'] = $qD [3];
        $form_data ['keys'] [$i] ['server_port_primary'] = $qD [4];
        $form_data ['keys'] [$i] ['server_port_backup'] = $qD [5];
        $res->free ();
      }
    }

    // $query = "UPDATE voip_devices SET extensions = '" . json_encode($form_data['keys']) . "', account_number = '" . $form_data['account_number'] . "', account_locked = " . $form_data['account_locked'] . ", vdtid = " . $vdtid[0] . ", dialplan = '" . $form_data['dialplan'] . "', dialplan_timeout = '" . $form_data['dialplan_timeout'] . "' WHERE mac = '" . $form_data['mac'] . "'";
    $query = "INSERT INTO voip_devices (extensions, account_number,account_locked,vdtid,dialplan,dialplan_timeout,mac) VALUES('" . json_encode ( $form_data ['keys'] ) . "', '" . $form_data ['account_number'] . "', " . $form_data ['account_locked'] . ", " . $vdtid [0] . ", '" . $form_data ['dialplan'] . "', '" . $form_data ['dialplan_timeout'] . "', '" . $form_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 )) ;

?>