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

/*
 * ****************************************************************************************
 * Copyright (c) 2016 Christopher W. Olsen <cwolsen@uBixStudios.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: extension_json.php 148 2015-11-28 00:58:35Z reddawg $
 *
 * ***************************************************************************************
 */

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

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

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

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

$recOpt = Array (
    'None',
    'Outbound',
    'Inbound',
    'In & Out' 
);

switch ($form_data ['req']) {
  case 'ring_group_list' :
    /* START: Get Ring Group List */
    $query = "SELECT * FROM ring_Groups WHERE accountcode = '" . $ubF->session->account_code . "' ORDER BY label";
    
    $result = $ubF->DB ['main']->query ( $query );
    
    if ($result->num_rows == 0)
      $rData ['data'] = null;
    else {
      while ( $qData = $result->fetch_assoc () ) {
        $rData ['data'] [] = array (
            "<a class=\"edit_ring_group\" id=\"" . $qData ['rg_id'] . "\">" . $qData ['label'] . "</a>",
            $qData ['callerid'],
            explode ( "@", $qData ['mailbox'] ) [0],
            $recOpt [$qData ['ext_recording']],
            ($qData ['ext_active'] == 0) ? 'Disabled' : 'Active',
            "<input type=\"submit\" name=\"data[del][" . $qData ['name'] . "]\" value=\"Delete\" onClick=\"return dV('" . $qData ['name'] . "');\">",
            "<a href=\"#\" onClick=\"openVoIP('" . $qData ['name'] . "');\">Launch</a>" 
        );
      }
    }
    
    /* END: Get Extension List */
    break;
  case 'mailbox_list' :
    $rData ['data'] = $data->libs ['voip']->getMailboxes ( $form_data ['account_code'] );
    break;
  case 'extension_info' :
    $query = "SELECT * FROM sippeers WHERE name = '" . $form_data ['ext'] . "'";
    $result = $ubF->DB ['voip']->query ( $query );
    $rData ['data'] = $result->fetch_assoc ();
    $result->free ();
    break;
  case 'extension_edit' :
    $cid = $form_data ['callerid_name'] . " <" . $form_data ['callerid_number'] . ">";
    
    if (isset ( $form_data ['uvm'] )) {
      $mb_info = explode ( '@', $form_data ['mailbox'] );
      $query = "UPDATE voicemail SET fullname = '" . $ubF->DB ['voip']->real_escape_string ( $form_data ['callerid_name'] ) . "' WHERE mailbox = '" . $mb_info [0] . "' AND context = '" . $mb_info [1] . "'";
      $ubF->DB ['voicemail']->query ( $query );
    }
    
    $query = "UPDATE sippeers SET callerid = '" . $cid . "', mailbox = '" . $form_data ['mailbox'] . "', ext_recording = " . $form_data ['ext_recording'] . ", ext_active = " . $form_data ['ext_active'] . " WHERE name = '" . $form_data ['ext'] . "'";
    if ($ubF->DB ['voip']->query ( $query ) == true) {
      $rData ['data'] = Array (
          'ret' => 1,
          'ret_string' => 'Extension Updated' 
      );
    }
    else {
      $rData ['data'] = Array (
          'ret' => 0,
          'ret_string' => 'Extension Failed To Update' 
      );
    }
    break;
  case 'extension_existing' :
    
    $erows = "";
    
    $query = "SELECT name, callerid FROM sippeers WHERE accountcode = '" . $form_data ['account_code'] . "' ORDER BY name";
    $result = $ubF->DB ['voip']->query ( $query );
    while ( $qData = $result->fetch_row () ) {
      $erows .= "<tr><td>" . $qData [0] . "</td><td>" . $qData [1] . "</td></tr>\n";
    }
    
    $result->free ();
    
    $rData ['data'] = $erows;
    break;
  case 'recording_options' :
    foreach ( $recOpt as $key => $val ) {
      $rData ['data'] [] = Array (
          'label' => $val,
          'id' => $key 
      );
    }
    break;
  case 'seats_list' :
    $query = "SELECT u.uid, CONCAT(u.first_name, ' ', u.last_name) AS full_name, u.email, IFNULL(vd.extensions, 'None') AS extensions, IFNULL(CONCAT(vdt.manufacturer, ' ', vdt.model, ' ', vd.mac),'None') AS device, u.status FROM users u LEFT JOIN voip_devices vd ON u.vdid = vd.vdid LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid WHERE u.account_number = '" . $ubF->session->account_number . "'";
    $query = "SELECT s.seat_id, IFNULL(u.email, 'Not Assigned') as email, IFNULL(CONCAT(vdt.manufacturer, ' ', vdt.model, ' - ', vd.mac),'None') AS device, IFNULL(vd.extensions, 'None') AS extensions, s.service_id, s.status FROM seats s LEFT JOIN users u ON s.uid=u.uid LEFT JOIN voip_devices vd ON s.vdid = vd.vdid LEFT JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid WHERE s.account_number = '" . $ubF->session->account_number . "'";
    $result = $ubF->DB ['main']->query ( $query );
    
    if ($result->num_rows == 0) {
      $rData ['data'] = null;
    }
    else {
      
      $eStart = strlen ( $ubF->session->account_code );
      
      while ( $qData = $result->fetch_assoc () ) {
        if ($qData ['extensions'] != "None") {
          $extension = json_decode ( $qData ['extensions'], true );
          
          $lexts = "";
          
          foreach ( $extension as $ext => $keys ) {
            if ($keys ['extension'] != '') {
              $act = preg_replace ( '/[0-9]/', "", $keys ['extension'] );
              $ext = substr ( $keys ['extension'], $eStart ); // preg_replace ( '/\D/', '', $keys ['extension'] );
              $lexts .= 'Ext: ' . $ext . ' Label: ' . $keys ['label'] . "<br />\n";
            }
          }
          $qData ['extensions'] = $lexts;
        }
        
        $rData ['data'] [] = array (
            $qData ['seat_id'],
            "<a class=\"edit_seat\" id=\"" . $qData ['seat_id'] . "\">" . $qData ['seat_id'] . "</a>",
            $qData ['email'],
            $qData ['device'],
            $qData ['extensions'],
            $qData ['service_id'],
            ($qData ['status'] == 0) ? 'Inactive' : 'Active' 
        );
      }
    }
    break;
  case 'seat_info' :
    $query = "SELECT seat_id, uid, vdid, status FROM seats WHERE account_number = '" . $ubF->session->account_number . "' AND seat_id = " . $form_data ['seat'];
    $result = $ubF->DB ['main']->query ( $query );
    
    $rData ['data'] = $result->fetch_assoc ();
    break;
  case 'seat_devices' :
    $query = "SELECT vd.vdid, vd.mac, vd.extensions, vdt.manufacturer, vdt.model FROM voip_devices vd JOIN voip_device_types vdt ON vd.vdtid = vdt.vdtid WHERE vd.account_number = '" . $ubF->session->account_number . "' AND (vd.seat_id = 0 OR vd.seat_id = " . $form_data ['seat'] . ")";
    $result = $ubF->DB ['main']->query ( $query );
    
    while ( $qData = $result->fetch_assoc () ) {
      
      $extension = json_decode ( $qData ['extensions'], true );
      
      $lexts = "";
      
      foreach ( $extension as $ext => $keys ) {
        if ($keys ['extension'] != '') {
          $act = preg_replace ( '/[0-9]/', "", $keys ['extension'] );
          $ext = preg_replace ( '/\D/', '', $keys ['extension'] );
          // $lexts .= 'Label: ' . $keys ['label'] . ' Ext: ' . $ext . ", ";
          $lexts .= $ext . ",  ";
        }
      }
      
      $label = $qData ['manufacturer'] . " " . $qData ['model'] . " - " . $qData ['mac'] . " - Ext: " . rtrim ( $lexts, ', ' );
      
      $rData ['data'] [] = array (
          'id' => $qData ['vdid'],
          'label' => $label 
      );
    }
    $result->free ();
    break;
  case 'seat_users' :
    $query = "SELECT uid, email, CONCAT(first_name, ' ', last_name) AS full_name FROM users WHERE account_number = '" . $ubF->session->account_number . "'";
    $result = $ubF->DB ['main']->query ( $query );
    
    $rData ['data'] [] = array (
        'uid' => 0,
        'email' => 'Not Assigned',
        'full_name' => '' 
    );
    
    while ( $qData = $result->fetch_assoc () ) {
      $rData ['data'] [] = $qData;
    }
    
    $result->free ();
    
    break;
  case 'seat_edit' :
    $query = "UPDATE voip_devices SET seat_id = 0 WHERE seat_id = " . $form_data ['seat'];
    $ubF->DB ['main']->query ( $query );
    $query = "UPDATE voip_devices SET seat_id = " . $form_data ['seat'] . " WHERE vdid = " . $form_data ['device'];
    $ubF->DB ['main']->query ( $query );
    $query = "UPDATE seats SET uid = " . $form_data ['user'] . ", vdid = " . $form_data ['device'] . " WHERE account_number = '" . $ubF->session->account_number . "' AND seat_id = " . $form_data ['seat'];
    
    if ($ubF->DB ['main']->query ( $query ) === true) {
      $jD ['msg'] = "Seat Updated";
      $jD ['code'] = 1;
    }
    else {
      $jD ['msg'] = "Error Updating Seat";
      $jD ['code'] = 0;
    }
    $rData ['data'] = $jD;
    break;
  default :
    $rData ['data'] = Array (
        0 
    );
    break;
}

print json_encode ( $rData );

?>