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

/**
 * Copyright (c) 2015, 2016 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:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by uBix Technologies.
 * 4. Neither the name of uBix Technologies 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 uBix Technologies ''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 uBix Technologies 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: account_settings_json.php 943 2017-09-07 20:16:43Z reddawg $
 *
 */

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

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

if ( !isset ( $form_data ['req'] ) )
  $form_data ['req'] = -1;

$_day = 60 * 60 * 24;

$rData ['authenticated'] = $ubF->session->auth_data ['authenticated'];

$aS = Array('Inactive','Active','Suspended');

switch ( $form_data ['req'] ) {
  case 'account_list' :
    $query = "SELECT account_number, company, telephone, server_primary, server_backup, ext_range, account_status FROM accounts  WHERE account_code != '' AND account_status = 1 ORDER BY voip_code";
    $query = "SELECT a.account_number, a.company, av.btn, av.endpoint_group, av.did_group, av.ext_base, voip_status FROM accounts a JOIN accounts_voip av ON a.account_number = av.account_number  WHERE voip_status >= 1 ORDER BY a.account_number";
    
    $result = $ubF->DB ['main']->query ( $query );
    
    while ( $qD = $result->fetch_assoc () ) {
      $rData ['data'] [] = array (
          "<a class=\"edit_account\" id=\"" . $qD ['account_number'] . "\">" . $qD ['account_number'] . "</a>",
          $qD ['company'],
          $qD ['btn'],
          $qD ['endpoint_group'], 
          $qD ['did_group'],
          $qD ['ext_base'], 
          $aS[$qD['voip_status']]
      );
    }
    break;
  case 'account_info' :
    $query = "SELECT * FROM accounts WHERE account_number = '" . $form_data ['account_number'] . "'";
    $result = $ubF->DB ['main']->query ( $query );
    $qData = $result->fetch_assoc ();
    $result->free ();
    
    $rData ['data'] = $qData;
    break;
  case 'account_update' :
    $query = "UPDATE accounts SET server_primary = '" . $form_data ['server_primary'] . "', server_port_primary = " . $form_data ['server_port_primary'] . ", transport_primary = '" . $form_data ['transport_primary'] . "', server_backup = '" . $form_data ['server_backup'] . "', server_port_backup = " . $form_data ['server_port_backup'] . ", transport_backup = '" . $form_data ['transport_backup'] . "', ext_range = " . $form_data ['ext_range'] . ", dialplan = '" . $form_data ['dialplan'] . "', dialplan_timeout = '" . $form_data ['dialplan_timeout'] . "' WHERE account_number = '" . $form_data ['account_number'] . "'";
    if ( $ubF->DB ['main']->query ( $query ) == true) {
      $retVal = 1;
      $messages = "Account Information Updated.\n";
      
      if ( $form_data ['uad'] >= 1 ) {
        $query = "SELECT vdid, extensions, mac FROM voip_devices WHERE account_number = '" . $form_data ['account_number'] . "' AND account_locked = 1";
        $result = $ubF->DB ['main']->query ( $query );
        while ( $qD = $result->fetch_row () ) {
          $ext = json_decode ( $qD [1], true );
          
          for ( $i = 1 ; $i <= count ( $ext ) ; $i++ ) {
            $ext [$i] ['server_primary'] = $form_data ['server_primary'];
            $ext [$i] ['server_backup'] = $form_data ['server_backup'];
            $ext [$i] ['transport_primary'] = $form_data ['transport_primary'];
            $ext [$i] ['transport_backup'] = $form_data ['transport_backup'];
            $ext [$i] ['server_port_primary'] = $form_data ['server_port_primary'];
            $ext [$i] ['server_port_backup'] = $form_data ['server_port_backup'];
          }
          
          $query = "UPDATE voip_devices SET extensions = '" . json_encode ( $ext ) . "', dialplan = '" . $form_data ['dialplan'] . "', dialplan_timeout = '" . $form_data ['dialplan_timeout'] . "' WHERE vdid = " . $qD [0];
          
          if ( $ubF->DB ['main']->query ( $query ) == true )
            $messages .= "Account Device " . $qD [2] . " Has Been Updated.\n";
        }
        
        $result->free ();
      }
      
      if ( $form_data ['uas'] == 2 ) {
        $query = "SELECT vdid, extensions, mac FROM voip_devices WHERE extensions LIKE '%\"voip_code\":\"" . $form_data ['voip_code'] . "\"%'";
        $result = $ubF->DB ['main']->query ( $query );
        
        while ( $qD = $result->fetch_row () ) {
          
          $ext = json_decode ( $qD [1], true );
          
          for ( $i = 1 ; $i <= count ( $ext ) ; $i++ ) {
            
            if ( $ext [$i] ['voip_code'] == $form_data ['voip_code'] && $ext [$i] ['uas'] == 1 ) {
              $ext [$i] ['server_primary'] = $form_data ['server_primary'];
              $ext [$i] ['server_backup'] = $form_data ['server_backup'];
              $ext [$i] ['transport_primary'] = $form_data ['transport_primary'];
              $ext [$i] ['transport_backup'] = $form_data ['transport_backup'];
              $ext [$i] ['server_port_primary'] = $form_data ['server_port_primary'];
              $ext [$i] ['server_port_backup'] = $form_data ['server_port_backup'];
              
              $query = "UPDATE voip_devices SET extensions = '" . json_encode ( $ext ) . "' WHERE vdid = " . $qD [0];
              
              if ( $ubF->DB ['main']->query ( $query ) == true )
                $messages .= "Device " . $qD [2] . " - " . $ext [$i] ['extension'] . " Has Been Updated.\n";
            }
          }
        }
      }
    }
    else {
      $retVal = 0;
      $messages = "Error Updating Account";
    }
    
    $rData ['data'] = Array (
        'ret' => $retVal,
        'ret_string' => $messages 
    );
    break;
  default :
    $rData ['data'] = Array (
        0 
    );
    break;
}

print json_encode ( $rData );

?>