Newer
Older
ubFramework / Portal / docroot / user / voip / extension_json.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 5 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: 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 ( $ubF->data ['req'] ))
  $ubF->data ['req'] = - 1;

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

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


switch ($ubF->data ['req']) {
  case 'extension_list' :
    /* START: Get Extension List */
      $query = "SELECT * FROM sippeers WHERE accountcode = '" . $ubF->session->account_code . "' ORDER BY name";
    
    $result = $ubF->DB ['voip']->query ( $query );
    
    if ($result->num_rows == 0)
      $rData ['data'] = null;
    else {

      while ( $qData = $result->fetch_assoc () ) {
        $ext = substr ( $qData ['name'], strlen ( $qData ['accountcode'] ) );

        $rData ['data'] [] = array (
          $qData['name'],
          "<a class=\"edit_extension\" id=\"" . $qData ['name'] . "\">" . $ext . "</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'] = $ubF->libs ['voip']->getMailboxes ( $ubF->data ['account_code'] );
    break;
  case 'extension_info' :
    $query = "SELECT * FROM sippeers WHERE name = '" . $ubF->data ['ext'] . "'";
    $result = $ubF->DB ['voip']->query ( $query );
    $rData ['data'] = $result->fetch_assoc ();
    $result->free ();

    break;
  case 'extension_edit' :
    $cid = $ubF->data ['callerid_name'] . " <" . $ubF->data ['callerid_number'] . ">";

    if (isset($ubF->data['uvm'])) {
      $mb_info = explode('@', $ubF->data['mailbox']);
      $query = "UPDATE voicemail SET fullname = '" . $ubF->DB['voip']->real_escape_string($ubF->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 = '" . $ubF->data ['mailbox'] . "', ext_recording = " . $ubF->data['ext_recording'] . ", ext_active = " . $ubF->data['ext_active'] . " WHERE name = '" . $ubF->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 = '" . $ubF->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;
  default :
    $rData ['data'] = Array (
      0 
    );
    break;
}

print json_encode ( $rData );

?>