Location: PHPKode > projects > Esvon Personals > esvon-personals/modules/Mailing_Lists/hw_user.php
<?php

// --------------------------------------------------------------------------
//
// Esvon Classifieds v.4.0
// Copyright(C), Esvon LTD, 2001-2010, All Rights Reserved.
// E-mail: hide@address.com
//
// All forms of reproduction, including, but not limited to, internet posting, 
// printing, e-mailing, faxing and recording are strictly prohibited.
// One license required per site running Esvon Classifieds. 
// To obtain a license for using Esvon Classifieds, please register at
// http://www.esvon.com/pg/products/p_classifieds/
//
// --------------------------------------------------------------------------

if(!defined('SITE_PATH')) die('Access denied');

define('TBL_ML',hwModTable(HW_MOD,'list'));
define('TBL_ML_IDX',hwModTable(HW_MOD,'idx'));
define('TBL_ML_LOG',hwModTable(HW_MOD,'log'));
define('TBL_ML_PENDING',hwModTable(HW_MOD,'pending'));

// functions

function Mailing_Lists_main(){
  $mact = $_REQUEST['mact'];
  if($mact=='ml') ProcessForm();
  elseif($mact=='confirm') ProcessConfirm();
  else ShowForm();
}

// sub area

function ShowForm() {
  global $db,$err_msg;

  $tpl_v = compact('err_msg');
  $tpl_v['email'] = html_esc($_REQUEST['email']);
  $tpl_v['mlist'] = $tpl_v['a_Descr'] = '';
  
  $res = $db->query('SELECT * FROM '.TBL_ML_IDX.' WHERE id>0 AND hide=0 ORDER BY id');
  while($v = mysql_fetch_assoc($res)){
    $tpl_v['mlist'].='<OPTION '.($_REQUEST['mlist_id']==$v['id']?'SELECTED':'').' VALUE="'.$v['id'].'">'.$v['name'];
    $tpl_v['a_Descr'].="'".addslashes($v['descr'])."',\n";    
  }
  if($tpl_v['a_Descr']) $tpl_v['a_Descr'] = substr($tpl_v['a_Descr'],0,-2);
  
  EvalAdvTpl(HW_MOD_TPL.'ml.htm', $tpl_v, 1);
}

function ProcessForm() {
  global $db,$err_msg;
    
  $email = trim($_REQUEST['email']);
  if(!$email || !IsEmail($email)) $err_msg = hwLng('req_email');
  
  $mlist_id = (int)$_REQUEST['mlist_id'];
  
  if(!$err_msg) {
    
    $list_name = $db->one_data('SELECT name FROM '.TBL_ML_IDX.' WHERE id="'.$mlist_id.'"');
    if(!$list_name) die('Incorrect Mailing List ID');

    if(!$_REQUEST['act'] || $_REQUEST['act']=='subscribe') {
      
        $db_email = $db->quote($email);
      
        $res = $db->query('SELECT email FROM '.TBL_ML.' WHERE email="'.$db_email.'" AND list_id="'.$mlist_id.'"');
        if(mysql_num_rows($res)>0) {
          $err_msg = hwLng('ml_already');
          ShowForm();
        }
        else {

        $code = $db->one_data('SELECT code FROM '.TBL_ML_PENDING.' WHERE email="'.$db_email.'" AND list_id="'.$mlist_id.'"');

        if(!$code) {
           $code = substr(md5(mt_rand().HW_TIME),0,15);
           $db->query('INSERT INTO '.TBL_ML_PENDING." (list_id,email,code,date) VALUES ('$mlist_id','$db_email','$code',".SQL_NOW.")");
        }

          $tpl_v = compact('list_name', 'email');
          $tpl_v['confirm_url'] = SITE_URL.HW_MOD_URL.'&mact=confirm&code='.$code;
          
          // send email
          $email_body = EvalAdvTpl(HW_MOD_TPL.'ml_confirm.mail', $tpl_v);
          hwSendMail($email,ADMIN_EMAIL,hwLng('m_ml_sub_confirm').' - "'.$list_name.'"',$email_body);
      
          EvalAdvTpl(HW_MOD_TPL.'ml_confirm.htm', $tpl_v, 1);
      }
    }

    elseif($_REQUEST['act']=='unsubscribe') {

      $db_email = $db->quote($email);

      if($mlist_id > 0){
        $res = $db->query('SELECT email FROM '.TBL_ML.' WHERE email="'.$db_email.'" AND list_id="'.$mlist_id.'"');
        if(mysql_num_rows($res)<1) {
          $err_msg = hwLng('ml_noemail').' - "'.$list_name.'"';
          ShowForm(); 
        }
        else {
          $db->query('DELETE FROM '.TBL_ML.' WHERE email="'.$db_email.'" AND list_id="'.$mlist_id.'"');
        }
      }
      else{
        $list_name = '';
        $ok = $db->one_data('SELECT COUNT(*) FROM '.TBL_USER.' WHERE email="'.$db_email.'"');
        if($ok) $db->query('INSERT IGNORE INTO '.hwModTable(HW_MOD,'unsub').' SET email="'.$db_email.'", list_id="'.$mlist_id.'", date=NOW()');
      }
      EvalAdvTpl(HW_MOD_TPL.'ml_unsubscribed.htm', compact('email','list_name'), 1);
    }     
  }

  else ShowForm();

}

function ProcessConfirm() {
  global $db;

  $code = $db->quote($_REQUEST['code']);
  list($list_id,$email) = $db->one_row('SELECT list_id,email FROM '.TBL_ML_PENDING.' WHERE code="'.$code.'"');
  if(!$email) { // not found
    include HW_MOD_TPL.'ml_confirm_err.htm';
  }
  else {

    $db->query('INSERT IGNORE INTO '.TBL_ML." VALUES (NULL,'$list_id',".$db->esc($email).",".SQL_NOW.")");
    $db->query("DELETE FROM ".TBL_ML_PENDING." WHERE code = '$code'");
    
    $tpl_v = compact('email');
    $tpl_v['list_name'] = $db->one_data('SELECT name FROM '.TBL_ML_IDX.' WHERE id="'.$list_id.'"');
    $tpl_v['unsub_link'] = SITE_URL.HW_MOD_URL.'&mact=ml&act=unsubscribe&mlist_id='.$list_id.'&email='.urlencode($email);

    $email_body = EvalAdvTpl(HW_MOD_TPL.'ml_confirmed.mail', $tpl_v);
    hwSendMail($email, ADMIN_EMAIL, hwLng('ml_added').' - "'.$tpl_v['list_name'].'"', $email_body);
    
    EvalAdvTpl(HW_MOD_TPL.'ml_subscribed.htm', $tpl_v, 1);
  }
}

?>
Return current item: Esvon Personals