<?php
/**
 *\licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
 *\portion code Copyright (c) 2002 osCommerce
 *\package osCSS-2 <www http://www.oscss.org>
 *\version 2.1.0
 *\date  18/10/10, 22:44
 *\author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
 *\encode UTF-8
 *\file
 *\brief Page de login
*/
  require('includes/application_top.php');
error_reporting(E_ALL);
  $page->init(basename(__FILE__),$language);

//   if(!isset($_GET['newsID']))  tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'SSL'));
//   elseif(isset($_GET['newsID'])){
//     if()
//     NewslettersUtility::UpNewsId($newsid,$custid, $key, $value='now()');
//   }
// print_r($_GET);
  try {
    /// compta
    $mode=(!isset($_GET['mode'])? 'view' : $_GET['mode'] );

    // ctrl news id
    if(!isset($_GET['newsID'])) throw new Exception (__('pas de news Id '));
    $newsid=tep_db_prepare_input($_GET['newsID']);

    // ctr cID
    if(!isset($_GET['cID'])) throw new Exception (__('pas de cID'));
    $custid=tep_db_prepare_input($_GET['cID']);
    $cust=customer::getInstance();

    if(!isset($_GET['hash'])) throw new Exception (__('pas de hash'));


    if( ($user=$cust->the_customer($custid)) && ( $user === false || !is_array($user) )) throw new Exception (__('error customers'));


    if($mode=='view')
      if(md5($user['customers_id'].$user['customers_email_address'].$newsid) !=$_GET['hash']) throw new Exception (__('erreur hash'));

    if ( ($module= NewslettersUtility::CheckNewsId($newsid) ) &&  $module ==false )throw new Exception (__('error newsletter module'));




    switch($mode){
      case 'opened':
	NewslettersUtility::UpNewsId($newsid,$custid, 'date_opened');
      break;
      case 'link':
	NewslettersUtility::UpNewsId($newsid,$custid, 'date_click');
      break;
      case 'view':
      default:
	NewslettersUtility::UpNewsId($newsid,$custid, 'date_opened');
	NewslettersUtility::UpNewsId($newsid,$custid, 'view_online','1');
    }
  }
  catch (Exception $e) {
   echo $e->getMessage();
//       $messageStack->add($e->getMessage(), 'error');
      tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'SSL'));
  }

  $page->post_init();




    switch($mode){

      /**
	@brief format url passé par htaccess
	cf RewriteRule n-([0-9]+)-([0-9]+).jpg newsletters.php?mode=opened&newsID=$1&cID=$2 [L,QSA]

	Une image est insere dans les newseletter
	cf '<img src="'.HTTP_SERVER.DIR_WS_HTTP_CATALOG.'n-'.$newsletter_id.'-'.$mail['customers_id'].'.jpg" width="1" height="1">'
      */
      case 'opened':
	header('Content-type: image/jpeg');
	$im = imagecreate(1, 1);
	$couleur_fond = imagecolorallocate($im, 255, 255, 255);
	imagejpeg($im);
      break;
      case 'link':
	tep_redirect($_GET['u']);
      break;

      case 'view':
      default:
// 	$myarray = array(
// 	'email_use_html'=>_cst_bool('EMAIL_USE_HTML'),
// 	'message'=> NewslettersUtility::ApplModele($module->content, $module->modele_id),
// 	);

      	$module['content']=str_replace(NewslettersUtility::$replace, array($user['customers_id'],$user['customers_gender'],$user['customers_firstname'],$user['customers_lastname'],$user['customers_email_address'],'','',$user['customers_shopping_points']) ,$module['content']);

var_dump($module);
	/// force link by redirect shop
	$module['content']= preg_replace('~href[\s]*=[\s]*"~', 'href="'.HTTP_SERVER.DIR_WS_HTTP_CATALOG.'news.php?i='.$newsid.'&c='.$user['customers_email_address'].'&u=$1',$module['content']);

	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">'.
	      '<html>'.
'<head>'.
'</head>'.
'<body align="center">'.
'<div style="color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;width:650px;margin:0 auto;padding:0;">
<table cellspacing="0" cellpadding="0" border="0" width="98%" style="margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;">
  <tr>
    <td align="center" valign="top"><!-- [ header starts here] -->
<!-- [ middle starts here] -->
      <table cellspacing="0" cellpadding="0" border="0" width="650">
	<tr>
          <td valign="top">'.
	 stripslashes(NewslettersUtility::ApplModele($module['content'], $module['modele_id'])). //tep_post_prepare_email($module->module.'.tpl',$language,$myarray));
'</body>'.
'</html>';
	exit;
// 	NewslettersUtility::UpNewsId($newsid,$custid, 'date_click');
    }



  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>