Newer
Older
ubFramework / Source / include / core / site_lib.php
<?php

/******************************************************************************************
 Copyright (c) 2013, 2014 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: site_lib.php 2496 2015-07-24 21:44:28Z reddawg $
 
 *****************************************************************************************/
class ubFunctions {

  private $data;

  private $encryptionMethod = "AES-256-CBC";

  private $secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";

  public $voip_servers = array (
      "AS-EAST-001.uBixVoIP.net" => "AS-EAST-001",
      "AS-EAST-002.uBixVoIP.net" => "AS-EAST-002",
      "AS-EAST-003.uBixVoIP.net" => "AS-EAST-003",
      "AS-EAST-004.uBixVoIP.net" => "AS-EAST-004",
      "AS-EAST-INT-004.uBixVoIP.net" => "AS-EAST-INT-004" 
  );

  public $voip_ports = array (
      5060 
  );

  public $voip_transports = array (
      "DNSnaptr",
      "TCPPreferred",
      "UDPOnly" 
  );

  /* Payment Vars */
  public $paymentMethodTypes;

  public $paymentCardTypes;

  public $paymentTypes;

  public $paymentStatus;

  /* Plan Vars */
  public $pTypes;

  public $pUnits;

  /* VoIP Vars */
  public $vDIDTypes;

  /* Account Vars */
  public $aStatus;

  /* Class Constructor */
  public function __construct ( &$_data ) {

    $this->data = &$_data;
    
    /* Payment Method Type */
    $this->paymentMethodTypes = Array (
        0 => 'Credit',
        1 => 'Debit',
        2 => 'Check' 
    );
    $this->paymentCardTypes = Array (
        0 => 'American Express',
        1 => 'Mastercard',
        2 => 'Visa' 
    );
    $this->paymentTypes = Array (
        0 => 'Personal',
        1 => 'Business' 
    );
    $this->paymentStatus = Array (
        0 => 'Scheduled',
        1 => 'Canceled',
        2 => 'Complete',
        3 => 'In Progress',
        4 => 'Failed' 
    );
    
    /* Plan Item Types */
    $this->pTypes = Array (
        0 => 'SDU',
        1 => 'E-Mail',
        2 => 'Hosting',
        3 => 'Security',
        4 => 'Backup',
        5 => 'Support',
        6 => 'Virtualization',
        7 => 'VoIP',
        8 => 'Misc' 
    );
    
    /* Plan Item Units */
    $this->pUnits = Array (
        0 => 'Each',
        1 => 'Account',
        2 => 'GB',
        3 => 'Domain' 
    );
    
    /* Voip DID Types */
    $this->vDIDTypes = Array (
        0 => 'Local',
        1 => 'Toll Free',
        2 => 'International' 
    );
    
    /* Account Status */
    $this->aStatus = Array (
        0 => 'Inactive',
        1 => 'Active' 
    );
  
  }

  /* Generate Account Number */
  public function genAccountNumber () {

    genAgain:
    $a = str_pad ( rand ( 0, 99 ), 2, "0", STR_PAD_LEFT );
    $b = str_pad ( rand ( 0, 99999 ), 5, "0", STR_PAD_LEFT );
    $c = str_pad ( rand ( 0, 9999 ), 4, "0", STR_PAD_LEFT );
    
    /* Format Account Number */
    $account_number = "$a-$b-$c";
    
    /* Verify If Account Number Is Available */
    $query = "SELECT account_number FROM accounts WHERE account_number = '$account_number'";
    $result = $this->data->DB['main']->query ( $query );
    
    if ( $result->num_rows == 0 )
      return ($account_number); /* Return Acount Number If Available */
    else
      goto genAgain;
  
    /* Generate New Account Number If Not Available */
  }

  public function getNews () {

    return ("NEWS");
  
  }

  /* Return Plan Type String */
  public function getPlanType ( $_ptid ) {

    return ($this->pTypes [$_ptid]);
  
  }

  /* End getPlanType */
  
  /* Return Unit String */
  public function getPlanUnit ( $_puid ) {

    return ($this->pUnits [$_puid]);
  
  }

  /* End getPlanUnit */
  
  /* Encrypt Data */
  public function encryptData ( $_data ) {

    $encryptedData = openssl_encrypt ( $_data, $this->encryptionMethod, $this->secretHash );
    return ($encryptedData);
  
  }

  /* Decrypt Data */
  public function decryptDat ( $_data ) {

    $decryptedData = openssl_decrypt ( $_data, $this->encryptionMethod, $this->secretHash );
    return ($decryptedData);
  
  }

  public function localize_us_number ( $phone ) {

    $numbers_only = preg_replace ( "/[^\d]/", "", $phone );
    return preg_replace ( "/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only );
  
  }

  public function get_cubes () {

    $query = "SELECT jid, hostname FROM jails WHERE account_number = '" . $this->data ['session']->account_number . "'";
    $res = $this->data->DB['main']->query ( $query );
    
    $cubes = "";
    while ( $qData = $res->fetch_assoc () ) {
      $cubes .= "<option value=\"" . $qData ['jid'] . "\">" . $qData ['hostname'] . "</option>";
    }
    return ($cubes);
  
  }

  public function sendMail ( $subject, $message, $recipient, $cc = '', $bcc = '' ) {

    $inline = chunk_split ( base64_encode ( file_get_contents ( "/usr/web/sites/portal.spherepbx.com/docroot/assets/images/spherepbx_logo.png" ) ) );
    
    $random_hash = md5 ( date ( 'r', time () ) );
    
    $message .= "<br>\n<br>\nSincerely,<br>\n<br>\nSpherePBX Support Team<br>\n<br>\n<img src=\"cid:PHP-CID-" . $random_hash . "\">";
    
    $message_body = '--PHP-mixed-' . $random_hash . "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash . "\"\n\n";
    $message_body .= "--PHP-alt-" . $random_hash . "\nContent-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\n";
    
    $message_body .= strip_tags ( $message ) . "\n\n";
    
    $message_body .= "--PHP-alt-" . $random_hash . "\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-prinatable\n\n";
    
    $message_body .= $message . "\n\n";
    
    $message_body .= "--PHP-alt-" . $random_hash . "--\n\n";
    
    $message_body .= "--PHP-mixed-" . $random_hash . "\nContent-Type: image/jpeg\nContent-Transfer-Encoding: base64\nContent-ID: <PHP-CID-$random_hash>\n\n";
    $message_body .= $inline;
    $message_body .= "\n\n";
    $message_body .= "--PHP-mixed-" . $random_hash . "--\n\n";
    
    $header = "From: SpherePBX <support@SpherePBX.com>\r\n";
    
    if ( $cc != '' )
      $header .= "CC: $cc\r\n";
    
    if ( $bcc != '' )
      $header .= "BCC: $bcc\r\n";
    
    $header .= "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\"\n";
    
    // print "[$header]\n[$message_body]\n";
    return (mail ( $recipient, $subject, $message_body, $header ));
  
  }

  function generatePassword ( $length = 9 ) {

    $vowels = "aeuyAEUY";
    $consonants = "bdghjmnpqrstvzBDGHJLMNPQRSTVWXZ0123456789!@#$%";
    
    $password = '';
    $alt = time () % 2;
    for ( $i = 0 ; $i < $length ; $i++ ) {
      if ( $alt == 1 ) {
        $password .= $consonants [(rand () % strlen ( $consonants ))];
        $alt = 0;
      }
      else {
        $password .= $vowels [(rand () % strlen ( $vowels ))];
        $alt = 1;
      }
    }
    return $password;
  
  }

  public function voip_selectServer ( $server ) {

    $opts = "";
    
    foreach ( $this->voip_servers as $key => $val ) {
      if ( $server == $key )
        $opts .= "<option value=\"" . $key . "\" selected>" . $val . "</option>";
      else
        $opts .= "<option value=\"" . $key . "\">" . $val . "</option>";
    }
    
    return ($opts);
  
  }

  public function voip_selectPort ( $port ) {

    $opts = "";
    
    foreach ( $this->voip_ports as $key ) {
      if ( $port == $key )
        $opts .= "<option value=\"" . $key . "\" selected>" . $key . "</option>";
      else
        $opts .= "<option value=\"" . $key . "\">" . $key . "</option>";
    }
    
    return ($opts);
  
  }

  public function voip_selectTransport ( $transport ) {

    $opts = "";
    
    foreach ( $this->voip_transports as $key ) {
      if ( $transport == $key )
        $opts .= "<option value=\"" . $key . "\" selected>" . $key . "</option>";
      else
        $opts .= "<option value=\"" . $key . "\">" . $key . "</option>";
    }
    
    return ($opts);
  
  }

  public function voip_getExtensions ( $an = '', $ext = '', $wvc = false ) {

    $opts = "<option value=\"\">Not Assigned</option>";
    
    if ( $an != '' ) {
      $query = "SELECT voip_code FROM accounts WHERE account_number = '$an'";
      $result = $this->data->DB['main']->query ( $query );
      $vc = $result->fetch_row () [0];
      $result->free ();
      
      $query = "SELECT name, accountcode FROM sippeers WHERE accountcode = '$vc' ORDER BY name";
    }
    else
      $query = "SELECT name, accountcode FROM sippeers ORDER BY name";
    
    $result = $this->data ['voip_db']->query ( $query );
    
    while ( $qData = $result->fetch_assoc () ) {
      if ( $ext == $qData ['name'] )
        if ( $wvc == false )
          $opts .= "<option value=\"" . $qData ['name'] . "\" selected>" . $qData ['name'] . "</option>";
        else
          $opts .= "<option value=\"" . $qData ['name'] . ":" . $qData ['accountcode'] . "\" selected>" . $qData ['name'] . "</option>";
      else if ( $wvc == false )
        $opts .= "<option value=\"" . $qData ['name'] . "\">" . $qData ['name'] . "</option>";
      else
        $opts .= "<option value=\"" . $qData ['name'] . ":" . $qData ['accountcode'] . "\">" . $qData ['name'] . "</option>";
    }
    
    $result->free ();
    
    return ($opts);
  
  }

  public function voip_getSecret ( $ext ) {

    $query = "SELECT secret FROM sippeers WHERE NAME = '$ext'";
    $result = $this->data->DB['voip']->query ( $query );
    
    if ( $result === false )
      $secret = time ();
    else {
      $secret = $result->fetch_row () [0];
      $result->free ();
    }
    
    return ($secret);
  
  }

  public function voip_deviceList ( $vdtid = '' ) {

    $opts = "<option value=\"0\">Select Device</option>";
    
    $query = "SELECT vdtid, manufacturer, model, firmware, line_keys FROM voip_device_types ORDER BY manufacturer, model";
    
    $result = $this->data->DB['main']->query ( $query );
    
    while ( $qData = $result->fetch_assoc () ) {
      if ( $vdtid == $qData ['vdtid'] )
        $opts .= "<option value=\"" . $qData ['vdtid'] . "\" selected>" . $qData ['manufacturer'] . " " . $qData ['model'] . " - Firmware: " . $qData ['firmware'] . " - Line Keys: " . $qData ['line_keys'] . "</option>";
      else
        $opts .= "<option value=\"" . $qData ['vdtid'] . "\">" . $qData ['manufacturer'] . " " . $qData ['model'] . " - Firmware: " . $qData ['firmware'] . " - Line Keys: " . $qData ['line_keys'] . "</option>";
    }
    
    $result->free ();
    
    return ($opts);
  
  }

  public function voip_deviceLines ( $vdtid ) {

    if ( $vdtid != "" ) {
      $query = "SELECT line_keys FROM voip_device_types WHERE vdtid = $vdtid";
      $result = $this->data->DB['main']->query ( $query );
      $ret = $result->fetch_row () [0];
      $result->free ();
    }
    else
      $ret = "0";
    return ($ret);
  
  }

  public function account_getList ( $an, $wC = false ) {

    $opts = "<option value=\"\">Select Account</option>";
    
    $query = "SELECT account_number, company FROM accounts ORDER BY company";
    $result = $this->data->DB['main']->query ( $query );
    
    if ( $wC == false ) {
      while ( $qData = $result->fetch_assoc () ) {
        if ( $an == $qData ['account_number'] )
          $opts .= "<option value=\"" . $qData ['account_number'] . "\" selected>" . $qData ['account_number'] . "</option>";
        else
          $opts .= "<option value=\"" . $qData ['account_number'] . "\">" . $qData ['account_number'] . "</option>";
      }
    }
    else {
      while ( $qData = $result->fetch_assoc () ) {
        if ( $an == $qData ['account_number'] )
          $opts .= "<option value=\"" . $qData ['account_number'] . "\" selected>" . $qData ['company'] . " : " . $qData ['account_number'] . "</option>";
        else
          $opts .= "<option value=\"" . $qData ['account_number'] . "\">" . $qData ['company'] . " : " . $qData ['account_number'] . "</option>";
      }
    }
    
    $result->free ();
    
    return ($opts);
  
  }

}

?>