<?php
/*
  $Id: cresecure.php,v 1.1.0.0 2008/01/10 13:41:11 datazen Exp $

  CRE Loaded, Open Source E-Commerce Solutions
  http://www.creloaded.com

  Copyright (c) 2007 CRE Loaded
  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class cresecure {
    var $title, $debug_info;

    function cresecure() {
      $this->code = 'cresecure';
      if (defined('MODULE_ADDONS_CRESECURE_TITLE')) {
        $this->title = MODULE_ADDONS_CRESECURE_TITLE;
      } else {
        $this->title = '';
      }      
      if (defined('MODULE_ADDONS_CRESECURE_DESCRIPTION')) {
        $this->description = MODULE_ADDONS_CRESECURE_DESCRIPTION;
      } else {
        $this->description = '';
      }      
      if (defined('MODULE_ADDONS_CRESECURE_STATUS')) {
        $this->enabled = ((MODULE_ADDONS_CRESECURE_STATUS == 'True') ? true : false);
      } else {
        $this->enabled = false;
      }
      if (defined('MODULE_ADDONS_CRESECURE_SORT_ORDER')) {
        $this->sort_order = (int)MODULE_ADDONS_CRESECURE_SORT_ORDER;
      } else {
        $this->sort_order = '';
      }    
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ADDONS_CRESECURE_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function keys() {
      return array('MODULE_ADDONS_CRESECURE_STATUS', 
                   'MODULE_ADDONS_CRESECURE_CRE_ID', 
                   'MODULE_ADDONS_CRESECURE_SEAL_TYPE',
                   );
    }

    function install() {
      global $languages_id;
      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 CRE Secure Module', 'MODULE_ADDONS_CRESECURE_STATUS', 'True', 'Select True to enable CRE Secure.', '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 ('CRE Secure Domain ID', 'MODULE_ADDONS_CRESECURE_CRE_ID', '', 'Enter your CRE Secure Domain ID provided by CRE.', '6', '2', now())");
      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 ('Seal Type', 'MODULE_ADDONS_CRESECURE_SEAL_TYPE', 'Verified', 'Select the types of CRE Secure seals to display.', '6', '3', '_CREselectOptions(array(\'Verified\',\'Business Review\', \'EVSSL\'), ', now())");
      // get template id                                                
      $template_name = defined('DEFAULT_TEMPLATE') ? DEFAULT_TEMPLATE : '';
      $template = tep_db_fetch_array(tep_db_query("SELECT template_id from " . TABLE_TEMPLATE . " WHERE template_name = '" . $template_name . "'"));
      $template_id = $template['template_id'];
      // insert infobox configuration
      tep_db_query("INSERT INTO infobox_configuration VALUES ('" . $template_id . "', '', 'cresecure.php', 'BOX_HEADING_CRESECURE', 'yes', 'left', 1, now(), now(), 'buySAFE', 'infobox', '#FFFFFF')"); 
      // get infobox_id
      $infobox = tep_db_fetch_array(tep_db_query("SELECT infobox_id from " . TABLE_INFOBOX_CONFIGURATION . " WHERE template_id = '" . $template_id . "' and infobox_file_name = 'cresecure.php'"));
      $infobox_id = $infobox['infobox_id'];    
      // insert infobox heading         
      tep_db_query("INSERT IGNORE INTO infobox_heading VALUES ('" . $infobox_id . "', '" . $languages_id . "', 'CRE Secure')");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_INFOBOX_CONFIGURATION . " where infobox_define = 'BOX_HEADING_CRESECURE'");        
      tep_db_query("delete from " . TABLE_INFOBOX_HEADING . " where box_heading = 'CRE Secure'"); 
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
  }  

  function _CREselectOptions($select_array, $key_value, $key = '') {
    for ($i=0; $i<(sizeof($select_array)); $i++) {
      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
      $string .= '<br><input type="checkbox" name="' . $name . '" value="' . $select_array[$i] . '"';
      $key_values = explode(", ", $key_value);
      if (in_array($select_array[$i], $key_values)) $string .= ' checked="checked"';
      $string .= '> ' . $select_array[$i];
    }
    return $string;    
  } 
  
  function cre_cfg_textarea($text) {
    return tep_draw_textarea_field('custom_seal_code', false, 35, 5, $text);
  }
?>