Newer
Older
ubFramework / Source / include / ubm / ubm.php
<?php

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

 *****************************************************************************************/

class ubManagin {
  private $ubmURL;
  private $ubmPort;
  
  /* Class Constructor */
  public function __construct($_url,$_port = 9999) {
  	$this->ubmURL  = $_url;
  	$this->ubmPort = $_port;
    }

  /*
    ubmCall will send a command to the ubManagin gateway
  */
  public function ubmCall(&$oData) {
    /* Unset error incase it was previously used */
    unset($oData['error']);

    /* Verify that we have all required data */
    if (!$oData['mod'])
      $oData['error'] .= "Missing/Invalid Module\n";
  
    if (!$oData['modXML'])
      $oData['error'] .= "Missing/Invalid Module XML\n";

    /* If a test failed error */
    if (isset($oData['error']))
      return(-1);

    /* Generate XML */
    $request  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $request .= "<ubManaginData ubmClVer=\"1.0\" ubmUser=\"$oData[ubmUser]\" ubmPass=\"$oData[ubmPass]\">\n";
    if (isset($oData['modCmd']))
      $request .= " <modCall modName=\"" . $oData['mod'] . "\" modCmd=\"" . $oData['modCmd'] . "\">\n";
    else
      $request .= " <modCall modName=\"" . $oData['mod'] . "\">\n";
    $request .= $oData['modXML'];
    $request .= " </modCall>\n";
    $request .= "</ubManaginData>\n";

    $request = utf8_encode($request);

    /* Create request HEADER */
    $header[] = "Content-type: application/PTI47";
    $header[] = "Content-length: " . strlen($request);
    $header[] = "Mime-Version: 1.1"; 
    $header[] = "Content-transfer-encoding: text";
    $header[] = "Document-type: Request";
    $header[] = "Request-number: 1";
 
    /* Create a curl handle to post data */
    $handle = curl_init();

    /* Set curl options */
    curl_setopt($handle, CURLOPT_URL,$this->ubmURL);
    curl_setopt($handle, CURLOPT_PORT,$this->ubmPort);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $request);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_TIMEOUT, 10);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
 
    /* Process request */
    $oData['response'] = utf8_decode(curl_exec($handle));
    $oData['error']    = curl_errno($handle);
    $oData['errorMsg'] = curl_error($handle);
 
    curl_close($handle);

    return($oData['error']);
    } /* END */
    
  } /* End ubManagin Class */ 
?>