<?php
/*
  $Id: redcoal.php,v 1.1.1.1 2003/09/18 19:03:49 wilt Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  sms.php - a class to send sms messages to redcoal.com in building mime-HTML eMails

*/
  class redcoal {
    var $handle;
	var $enabled;
	var $_connected;
	var $sort_order;
	var $title;
	var $code;
	var $description;
	var $host_url;
	function redcoal(){
		global $HTTP_SESSION_VARS;
		$this->code = 'redcoal';
		$this->title = MODULE_SMS_REDCOAL_TEXT_TITLE;
		$this->description = MODULE_SMS_REDCOAL_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_SMS_REDCOAL_SORT_ORDER;
		$this->enabled = ((MODULE_SMS_REDCOAL_STATUS == 'True') ? true : false);
		if (tep_session_is_registered('login_id') && $HTTP_SESSION_VARS['login_id']!='')
			$this->enabled=true;
		else
			$this->enabled=false;
		$this->host_url=MODULE_SMS_REDCOAL_HOST;
		//$this->key=MODULE_SMS_REDCOAL_KEY;
		//$this->serial_no=MODULE_SMS_REDCOAL_SERIAL_NO;
	}
	function connecthost(){
		$this->handle=& new email(array('X-Mailer: osCommerce'));
		$this->_connected=true;
		return true;
	}
	function get_error(){
		return $this->error_description;
	}
	function send_sms($to_addr,$to_name,$contents,$country){
		$result="";
		if (!$this->_connected)
			$this->connecthost();
		$this->handle->add_text($contents);
      	$this->handle->build_message();
		$this->handle->send($to_name, $to_addr . "@" . $this->host_url, SMS_FROM_NAME, EVENTS_SMS_EMAIL, '');
		return true;
	}
    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SMS_REDCOAL_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }
	
	function set_option($name,$value){
		if (isset($this->$name)){
			$element=&$this->$name;
			$element=$value;
		}
	}
    function get_failed_messages(){
		if (sizeof($this->failed)<=0) return "";

		reset($this->failed);
		$result="";
		while(list($id,)=each($this->failed))
			$result.="To: " . $id . " - " . $this->failed[$id] .'<br>';
		return $result;
	}
	// install related procedures
    function install() {
	  //install into configuration group
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable REDCOAL sms Module', 'MODULE_SMS_REDCOAL_STATUS', 'True', 'Do you want to include Redcoal SMS?',6, '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Redcoal Sort order of display.', 'MODULE_SMS_REDCOAL_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.',6, '2', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Redcoal Host.', 'MODULE_SMS_REDCOAL_HOST', '', 'Gateway name used to access',6, '3', now())");
      //tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Redcoal Key.', 'MODULE_SMS_REDCOAL_KEY', '', 'Key used to authenticate gateway',6, '', now())");
      //tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Redcoal Serial No.', 'MODULE_SMS_REDCOAL_SERIAL_NO', '', 'Serial No used to authenticate gateway',6, '', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_SMS_REDCOAL_HOST','MODULE_SMS_REDCOAL_STATUS',  'MODULE_SMS_REDCOAL_SORT_ORDER');
    }

}
?>
