<?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/
//
// --------------------------------------------------------------------------
// expiry reminder class start
class ItemExpiryReminder {
var $TBL_CFG;
var $TBL_ITEM;
var $CC_TO_ADM = FALSE;
// group email custom
/* var $ITEM_AMT = 0;
var $ITEM_COUNT = 0;
var $EMAIL_COUNT = 0;
var $EN_SINGLE_EMAIL = false;
*/
function ItemExpiryReminder($tbl_cfg, $tbl_item){
$this->TBL_CFG = $tbl_cfg;
$this->TBL_ITEM = $tbl_item;
}
function SQLGetInitial(){
return 'SELECT i.*,u.email FROM '.TBL_AD.' i, '.TBL_USER.' u WHERE i.userid=u.id';
}
function SendEmails(){
global $db;
if($this->TBL_ITEM==TBL_AD) $type = 'FEE';
elseif($this->TBL_ITEM==TBL_USER){
$type = 'SUB';
//$this->EN_SINGLE_EMAIL = false;
}
elseif($this->TBL_ITEM==hwModTable('Cron_General','extra_remind')) $type = 'EXTRA';
else return false;
$A_RM = $db->select('SELECT plan AS hw_pay_plan, days AS days_rem, mail_subj, mail_body FROM '.$this->TBL_CFG.
' WHERE type="'.$type.'" AND enabled=1 ORDER BY days_rem');
if(!$A_RM) return false;
// reset status for valid exp.dates
$days_rem = $A_RM[count($A_RM)-1]['days_rem'];
if($days_rem){
$db->query('UPDATE '.$this->TBL_ITEM.' SET hw_remind=-1 WHERE exp_date > DATE_ADD('.SQL_NOW.',INTERVAL '.$days_rem.' DAY)');
}
$s_ql = $this->SQLGetInitial().' AND i.exp_date>"2001-01-01"';
if($A_RM) foreach($A_RM as $k=>$v){
// hw_pay_plan, days_rem, mail_subj, mail_body
$ql = $s_ql." AND i.exp_date < DATE_ADD(".SQL_NOW.",INTERVAL $v[days_rem] DAY)
AND (i.hw_remind>$v[days_rem] OR i.hw_remind=-1)";
if($v['hw_pay_plan']){
if ($type!='EXTRA') $ql.=' AND i.hw_pay_plan LIKE "%['.$v['hw_pay_plan'].'-%"';
else $ql.=' AND i.opt_id = "'.$v['hw_pay_plan'].'"';
}
//if($this->EN_SINGLE_EMAIL) $ql .= ' ORDER BY u.email';
$res = $db->query($ql);
$amt = mysql_num_rows($res);
// group email custom
/*if ($this->EN_SINGLE_EMAIL) {
$this->ITEM_AMT = $amt;
$this->ITEM_COUNT = 0;
$this->EMAIL_COUNT = 0;
}*/
if($amt>0) while ($a_Item = mysql_fetch_assoc($res)){
$this->ProcessItem($a_Item, $v);
}
unset($A_RM[$k]['mail_body']); // remove mail body
// group email custom
//if ($this->EN_SINGLE_EMAIL) $A_RM[$k]['amt'] = $this->EMAIL_COUNT;
//else
$A_RM[$k]['amt'] = $amt;
if(!$A_RM[$k]['hw_pay_plan']) $A_RM[$k]['hw_pay_plan'] = 'any';
}
return $A_RM;
}
function ProcessItem ($a_Ad, $a_Rem) {
global $db;
// group email custom
// static $prev_email, $group_amt, $tpl;
// $a_Rem - hw_pay_plan, days_rem, mail_subj, mail_body
$a_Ad['hw_days_rem'] = $a_Rem['days_rem'];
$a_Ad['ad_url'] = hwGetAdURL($a_Ad['link_id']);
$a_Ad['cname'] = ShowCategoryPath($a_Ad['catid']);
// Load Fields
if(strpos($a_Rem['mail_body'], '##email_rows##') !== false){
$cid = $a_Ad['catid'];
$O_LFS = &Factory::singleton('FieldsSet', hwGetFsByCat($cid) );
$O_LFS->LoadValues($a_Ad);
$a_Ad['email_rows'] = $O_LFS->GetRowsForMail();
}
// group email custom
/*if($this->EN_SINGLE_EMAIL) {
if($this->ITEM_COUNT==0) {
$prev_email = false;
$group_amt = 0;
$tpl = new HawkTpl;
$tpl->InitArray('row');
}
$this->ITEM_COUNT++;
}*/
if(IsEmail($a_Ad['email'])){
// group email custom
//if (!$this->EN_SINGLE_EMAIL){
$mail_body = EvalBuffer($a_Rem['mail_body'], $a_Ad);
hwSendMail( $a_Ad['email'], ADMIN_EMAIL, $a_Rem['mail_subj'], $mail_body );
if($this->CC_TO_ADM) hwSendMail( ADMIN_EMAIL, ADMIN_EMAIL, $a_Rem['mail_subj'], $mail_body );
/*}
else{
if (($prev_email && $prev_email!=$a_Ad['email']) || $this->ITEM_COUNT==$this->ITEM_AMT) {
if ($this->ITEM_COUNT == $this->ITEM_AMT) {
$tpl->AddCell('row', $a_Ad);
$group_amt++;
}
$tpl->SetBuf($a_Rem['mail_body']);
$mail_body = $tpl->Parse('', array('listing_amt'=>$group_amt, 'hw_days_rem'=>$a_Ad['hw_days_rem'] );
hwSendMail( $prev_email, ADMIN_EMAIL, $a_Rem['mail_subj'], $mail_body );
$this->EMAIL_COUNT++;
$group_amt = 0;
$tpl = new HawkTpl;
$tpl->InitArray('row');
if ($this->ITEM_COUNT < $this->ITEM_AMT) {
$tpl->AddCell('row', $a_Ad);
$group_amt++;
}
}
else {
$tpl->AddCell('row', $a_Ad);
$group_amt++;
}
}*/
}
// group email custom
//if ($this->EN_SINGLE_EMAIL) $prev_email = $a_Ad['email'];
if($this->TBL_ITEM==TBL_AD) $col = 'link_id';
else $col = 'id';
$db->query('UPDATE '.$this->TBL_ITEM.' SET hw_remind='.$a_Rem['days_rem']." WHERE $col=".$a_Ad[$col]);
}
}
class ExtraExpiryReminder extends ItemExpiryReminder {
function SQLGetInitial(){
return 'SELECT i.id, i.opt_id, i.exp_date, CONCAT("{ex_",e.type,"}") AS extra_opt_descr, e.descr AS extra_opt_val, a.*, u.email FROM '.$this->TBL_ITEM.' i , '.TBL_EXTRA_OPT.' e, '.TBL_AD.' a, '.TBL_USER.' u WHERE i.opt_id=CONCAT("AD_",e.type) AND i.opt_val=e.val AND i.ad_id=a.link_id AND a.userid=u.id';
}
function SendEmails(){
global $db;
// Synchronization the TBL_AD with the 'extra_remind' table
# 1. Delete Ads without extra option(s) from the 'extra_remind'
$IDS_AMT = 1000; # Max IDs in SQL-request
$res = $db->query('SELECT e.id FROM '.$this->TBL_ITEM.' e LEFT JOIN '.TBL_AD.' a ON (e.ad_id=a.link_id AND a.hw_pay_plan LIKE CONCAT("%[",e.opt_id,"-%")) WHERE a.link_id is NULL');
$i = 0;
$ids = '';
while ($v = mysql_fetch_assoc($res)) {
if ($i<$IDS_AMT) {
$i++;
$ids .= $v['id'].',';
}
else {
$ids = substr($ids,0,-1);
$db->query('DELETE FROM '.$this->TBL_ITEM.' WHERE id IN('.$ids.')');
$i = 0;
$ids = '';
}
}
if ($ids) {
$ids = substr($ids,0,-1);
$db->query('DELETE FROM '.$this->TBL_ITEM.' WHERE id IN('.$ids.')');
}
# 2. Insert into 'extra_remind' new and update existing Ads
$res = $db->query('SELECT link_id, hw_pay_plan FROM '.TBL_AD.' WHERE hw_pay_plan REGEXP "\\[.+-.+-.+\\]"');
while ($v = mysql_fetch_assoc($res)) {
preg_match_all('/\[(\w+)-(\d+)-(\w+)\]/',$v['hw_pay_plan'],$out,PREG_SET_ORDER);
if (count($out)) foreach ($out AS $value) {
$ex_id = $db->one_data('SELECT id FROM '.$this->TBL_ITEM.' WHERE ad_id='.$v['link_id'].' AND opt_id="'.$value[1].'"');
if ($ex_id) $db->query('UPDATE '.$this->TBL_ITEM.' SET exp_date="'.date('Y-m-d H:i:s',$value[2]).'", opt_val="'.$value[3].'" WHERE id='.$ex_id);
else $db->query('INSERT INTO '.$this->TBL_ITEM.' SET ad_id='.$v['link_id'].', opt_id="'.$value[1].'", exp_date="'.date('Y-m-d H:i:s',$value[2]).'", opt_val="'.$value[3].'"');
}
}
return parent::SendEmails();
}
}
class UserExpiryReminder extends ItemExpiryReminder {
function SQLGetInitial(){
return 'SELECT id, username, email FROM '.TBL_USER.' i WHERE 1';
}
function ProcessItem($a_User,$a_Rem) {
global $db;
// $a_Rem - hw_pay_plan, days_rem, mail_subj, mail_body
// $a_User: id,username,email
$a_User['hw_days_rem'] = $a_Rem['days_rem'];
// group email custom: expired ads
$a_User['listing_amt'] = $db->one_data('SELECT COUNT(*) FROM '.TBL_AD.' WHERE userid='.$a_User['id'].' AND exp_date<NOW()');
// turn off listing expiry reminder? ...
if(IsEmail($a_User['email'])){
$mail_body = EvalBuffer($a_Rem['mail_body'], $a_User);
hwSendMail( $a_User['email'], ADMIN_EMAIL, $a_Rem['mail_subj'], $mail_body);
if($this->CC_TO_ADM) hwSendMail( ADMIN_EMAIL, ADMIN_EMAIL, $a_Rem['mail_subj'], $mail_body );
}
$db->query('UPDATE '.TBL_USER.' SET hw_remind='.$a_Rem['days_rem'].' WHERE id='.$a_User['id']);
}
}
// expiry reminder class end
function Cron_General_Run(&$args) {
global $O_HW,$db;
$HW_MOD = 'Cron_General';
hwModLoadLng($HW_MOD);
$MOD_CFG = $O_HW->GetModOptions($HW_MOD);
// Expiration reminders
if($MOD_CFG['EN_EXP_R']){
$o_exp = new UserExpiryReminder(hwModTable($HW_MOD,'remind'), TBL_USER);
// $o_exp->CC_TO_ADM = TRUE;
$a_stat_user = $o_exp->SendEmails();
if($a_stat_user){
$total = 0;
foreach($a_stat_user as $v){
$k = sprintf(hwLng('cr_exp_sign'),$v['days_rem'],$v['hw_pay_plan']);
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_user_title')][$k] = $v['amt'];
$total+=$v['amt'];
}
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_user_title')][hwLng('cr_exp_total')] = $total;
}
$o_exp = new ItemExpiryReminder(hwModTable($HW_MOD,'remind'), TBL_AD);
// $o_exp->CC_TO_ADM = TRUE;
$a_stat_item = $o_exp->SendEmails();
if($a_stat_item){
$total = 0;
foreach($a_stat_item as $v){
$k = sprintf(hwLng('cr_exp_sign'),$v['days_rem'],$v['hw_pay_plan']);
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_ad_title')][$k] = $v['amt'];
$total+=$v['amt'];
}
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_ad_title')][hwLng('cr_exp_total')] = $total;
}
$o_exp = new ExtraExpiryReminder(hwModTable($HW_MOD,'remind'), hwModTable($HW_MOD,'extra_remind'));
// $o_exp->CC_TO_ADM = TRUE;
$a_stat_extra = $o_exp->SendEmails();
if($a_stat_extra){
$total = 0;
foreach($a_stat_extra as $v){
$k = sprintf(hwLng('cr_exp_sign'),$v['days_rem'],$v['hw_pay_plan']);
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_extra_title')][$k] = $v['amt'];
$total+=$v['amt'];
}
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_extra_title')][hwLng('cr_exp_total')] = $total;
}
if($MOD_CFG['EN_ADM_R']){
$tpl = new HawkTpl;
$tpl->InitArray('stat_item,stat_user');
if($a_stat_item) foreach($a_stat_item as $v){
$tpl->AddCell('stat_item', $v);
}
if($a_stat_user) foreach($a_stat_user as $v){
$tpl->AddCell('stat_user', $v);
}
if($a_stat_extra) foreach($a_stat_extra as $v){
$tpl->AddCell('stat_extra', $v);
}
hwSendMail( ADMIN_EMAIL, ADMIN_EMAIL, '', $tpl->Parse(MOD_DIR.$HW_MOD.'/tpl/expiry_notify_adm.mail') );
}
}
if($MOD_CFG['EN_DEL_EXP'] && ctype_digit($MOD_CFG['EXP_DAYS_TO_LIVE'])){
$total = hwDeleteAds('exp_date < DATE_SUB('.SQL_NOW.',INTERVAL '.$MOD_CFG['EXP_DAYS_TO_LIVE'].' DAY) AND exp_date > "2001-01-01"');
$args['A_CRON'][$HW_MOD][hwLng('cr_del_exp')] = $total;
}
// some clean-up
hwExecMaintenance();
$db->query('DELETE FROM '.hwModTable('Cron_General','log').' WHERE added < DATE_SUB('.SQL_NOW.',INTERVAL 30 DAY)');
if($MOD_CFG['EN_TBL_OPT']){
// Optimize tables after all
$a_tables = $db->one_col_array('SHOW tables');
$len = strlen(TBL_PREFIX);
if($len) foreach($a_tables as $k=>$v){
if(substr($v,0,$len)!=TBL_PREFIX) unset($a_tables[$k]);
}
$s_tables = implode(',',$a_tables);
$db->query('REPAIR TABLE '.$s_tables);
$db->query('OPTIMIZE TABLE '.$s_tables);
$db->query('ANALYZE TABLE '.$s_tables);
$args['A_CRON'][$HW_MOD][hwLng('cr_opt_tabl')] = count($a_tables);
}
if($MOD_CFG['EXP_FOR_PAYM'] && ctype_digit($MOD_CFG['EXP_FOR_PAYM'])){
// Delete old pending payments
$db->query('DELETE FROM '.TBL_PAY_PEND.' WHERE odate < DATE_SUB('.SQL_NOW.',INTERVAL '.$MOD_CFG['EXP_FOR_PAYM'].' DAY)');
$args['A_CRON'][$HW_MOD][hwLng('cr_exp_paym')] = $db->affected_rows();
}
if($MOD_CFG['LIVE_TRANS'] && ctype_digit($MOD_CFG['LIVE_TRANS'])){
// Delete old payments
$db->query('DELETE FROM '.TBL_PAY_LOG.' WHERE cdate < DATE_SUB('.SQL_NOW.',INTERVAL '.$MOD_CFG['LIVE_TRANS'].' DAY)');
$args['A_CRON'][$HW_MOD][hwLng('cr_live_trans')] = $db->affected_rows();
}
if($MOD_CFG['NOEXP_DEL'] && ctype_digit($MOD_CFG['NOEXP_DEL'])){
// Delete ads with empty expiration date
hwDeleteAds('hw_updated < DATE_SUB('.SQL_NOW.',INTERVAL '.$MOD_CFG['NOEXP_DEL'].' DAY) AND exp_date < "2001-01-01"');
}
if($MOD_CFG['OLD_USER'] && ctype_digit($MOD_CFG['OLD_USER'])){
// Delete old users with no ads
hwDeleteUsers('SELECT u.* FROM '.TBL_USER.' u LEFT JOIN '.TBL_AD.' a ON u.id=a.userid WHERE a.userid IS NULL'.
' AND u.lastlogin < DATE_SUB('.SQL_NOW.',INTERVAL '.$MOD_CFG['OLD_USER'].' DAY) AND u.lastlogin > "2001-01-01"');
}
include_once SITE_PATH.'inc/ft_search.php';
ftSynchAllItems();
}