<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * SMPP based SMS driver
 *
 * PHP versions 4 and 5
 *
 * @category   Networking
 * @package    Net_SMS
 * @author     Ian Eure <ieure@php.net>
 * @copyright  (c) 2005 WebSprockets, LLC
 * @license    http://www.fsf.org/copyleft/lgpl.html  LGPL
 * @version    Release: @package_version@
 * @version    CVS:     $Revision$
 * @link       http://pear.php.net/package/Net_SMS
 * @since      File available since Release 0.0.3
 */

// Place includes, constant defines and $_GLOBAL settings here.
require_once 'Net/SMPP/Client.php';

/**
 * SMPP based SMS driver
 *
 * This driver interfaces with the email-to-sms gateways provided by many
 * carriers, particularly those based in the U.S.
 *
 * @category   Networking
 * @package    Net_SMS
 * @author     Ian Eure <ieure@php.net>
 * @copyright  (c) 2005 WebSprockets, LLC
 * @license    http://www.fsf.org/copyleft/lgpl.html  LGPL
 * @version    Release: @package_version@
 * @version    CVS:     $Revision$
 * @link       http://pear.php.net/package/Net_SMS
 * @since      Class available since Release 0.0.3
 */
class Net_SMS_generic_smpp extends Net_SMS
{
    /**
     * Capabilities of this driver
     *
     * @var  array
     */
    var $capabilities = array(
        'auth'        => true,
        'batch'       => false,
        'multi'       => false,
        'receive'     => false,
        'credit'      => false,
        'addressbook' => false,
        'lists'       => false
    );

    /**
     * Driver paramaters
     *
     * @var     array
     * @access  private
     */
    var $_params = array(
        'host'         => null,
        'port'         => 0,
        'vendor'       => null,
        'bindParams'   => array(),
        'submitParams' => array()
    );

    /**
     * Net_SMPP_Client instance
     *
     * @var     object
     * @access  private
     */
    var $_client = null;

    /**
     * Constructor
     *
     * @param   array  $params  Parameters
     * @return  void
     */
    function Net_SMS_generic_smpp($params = null)
    {
        parent::Net_SMS($params);
        $this->_client =& new Net_SMPP_Client($this->_params['host'], $this->_params['port']);
        if (!is_null($this->_params['vendor'])) {
            Net_SMPP::setVendor($this->_params['vendor']);
        }
    }

    /**
     * Identify this driver
     *
     * @return  array  Driver info.
     */
    function getInfo()
    {
        return array(
            'name' => _("generic_smpp"),
            'desc' => _("SMPP gateway driver.")
        );
    }

    /**
     * Get required paramaters
     *
     * @return array  Array of required parameters.
     */
    function getParams()
    {
        return array(
            'host' => array(
                'label' => _('Host'), 'type' => 'text'),
            'port' => array(
                'label' => _('Port'), 'type' => 'text'),
            'bindParams' => array(
                'label' => _('bind_transmitter paramaters'), 'type' => 'array'),
            'submitParams' => array(
                'label' => _('submit_sm parameters'), 'type' => 'array'
            )
        );
    }

    /**
     * Send the message
     *
     * You may also specify the carrier with the 'carrier' key of the message
     * to avoid creating a new instance for each carrier, or fiddling with the
     * paramaters.
     *
     * @param   array    $message  Message to send
     * @param   string   $to       Destination phone number
     * @return  mixed    boolean true on success, PEAR_Error on failure
     * @access  private
     */
    function &_send(&$message, $to)
    {
        $pdu =& Net_SMPP::PDU('submit_sm', $this->_params['submitParams']);
        $pdu->destination_addr = $to;
        $pdu->short_message = $message['text'];
        if (count($message) > 1) {
            // Other params to set
            $v = $message;
            unset($v['text']);
            $pdu->set($v);
            unset($v);
        }

        $res =& $this->_client->sendPDU($pdu);

        // Error sending?
        if ($res === false) {
            return array(0, 'Error sending PDU');
        }

        $resp =& $this->_client->readPDU();
        if ($resp === false) {
            return array(0, 'Could not read response PDU');
        }
        if ($resp->isError()) {
            return array(0, 'Sending failed: ' . $resp->statusDesc());
        }

        // Success!
        return array(1, $resp->message_id);
    }

    /**
     * Authenticate with the SMSC
     *
     * This connects to the SMSC (if not already connected) and authenticates
     * with the bind_transmitter command (if not already bound).
     *
     * @access  protected
     */
    function _authenticate()
    {
        if ($this->_client->state == NET_SMPP_CLIENT_STATE_CLOSED) {
            $res = $this->_client->connect();
            if ($res === false) {
                echo "Couldn't connect\n";
                return false;
            }
        }

        if ($this->_client->state == NET_SMPP_CLIENT_STATE_OPEN) {
            $resp =& $this->_client->bind($this->_params['bindParams']);
            if ($resp === false || (is_object($resp) && $resp->isError())) {
                return false;
            }
        }
        return true;
    }

    function accept(&$obj)
    {
        return $this->_client->accept($obj);
    }
}

?>