<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * EmailBlacklist
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
/**
 * This is the model class for table "email_blacklist".
 *
 * The followings are the available columns in table 'email_blacklist':
 * @property integer $email_id
 * @property integer $subscriber_id
 * @property string $email
 * @property string $reason
 * @property string $date_added
 * @property string $last_updated
 */
class EmailBlacklist extends ActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{email_blacklist}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        $rules = array(
            array('email, reason', 'required'),
            array('email', 'length', 'max' => 150),
            array('email', 'email'),
            array('email', 'unique'),
            
            array('email, reason', 'safe', 'on' => 'search'),
        );

        return CMap::mergeArray($rules, parent::rules());
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        $relations = array();
        return CMap::mergeArray($relations, parent::relations());
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $labels = array(
            'email_id'      => Yii::t('email_blacklist', 'Email'),
            'subscriber_id' => Yii::t('email_blacklist', 'Subscriber'),
            'email'         => Yii::t('email_blacklist', 'Email'),
            'reason'        => Yii::t('email_blacklist', 'Reason'),
        );
        
        return CMap::mergeArray($labels, parent::attributeLabels());
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        $criteria = new CDbCriteria;
        $criteria->compare('email', $this->email, true);
        $criteria->compare('reason', $this->reason, true);
        
        return new CActiveDataProvider(get_class($this), array(
            'criteria'      => $criteria,
            'pagination'    => array(
                'pageSize'  => (int)Yii::app()->request->getQuery('pageSize', 10),
                'pageVar'   => 'page',
            ),
            'sort'=>array(
                'defaultOrder'  => array(
                    'email_id'  => CSort::SORT_DESC,
                ),
            ),
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return EmailBlacklist the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    public function delete()
    {
        // when taken out of blacklist remove all the log records
        // NOTE: when a subscriber is deleted the column subscriber_id gets nulled so that we keep 
        // the blacklist email for future additions.
        if (!empty($this->subscriber_id)) {
            $attributes = array('subscriber_id' => (int)$this->subscriber_id);
            CampaignDeliveryLog::model()->deleteAllByAttributes($attributes);
            CampaignBounceLog::model()->deleteAllByAttributes($attributes);    
        }
        
        return parent::delete();
    }

    public static function isBlacklisted($email)
    {
        if (Yii::app()->options->get('system.email_blacklist.local_check', 'yes') == 'no') {
            return false;
        }
        
        $emailParts = explode('@', $email);
        
        if (count($emailParts) != 2) {
            return false;
        }
        
        list($name, $domain) = $emailParts;
        
        $criteria = new CDbCriteria();
        $criteria->select = 'email_id';
        $criteria->compare('email', $email);
        
        if ($name != '*') {
            $criteria->compare('email', '*@'.$domain, false, 'OR');
        }
        
        $blacklisted = self::model()->find($criteria);

        if (!empty($blacklisted)) {
            return true;
        }
        
        $hooks = Yii::app()->hooks;
        
        if (Yii::app()->options->get('system.email_blacklist.remote_check', 'no') == 'yes') {
            $hooks->addFilter('email_blacklist_is_email_blacklisted', array(__CLASS__, 'checkAgainstDnsBl'));
        }
        
        $blacklistedAt  = new CList();
        $blacklisted    = $hooks->applyFilters('email_blacklist_is_email_blacklisted', $blacklisted, $email, $blacklistedAt);
        
        if ($blacklisted) {
            $add = new self();
            $add->email  = $email;
            if ($blacklistedAt->count > 0) {
                $add->email  = '*@' . $domain;
                $reason = Yii::t('email_blacklist', 'Email blacklisted at {list}', array(
                    '{list}' => implode(', ', $blacklistedAt->toArray())
                ));
            } else {
                $reason = Yii::t('email_blacklist', 'Email blacklisted at various DNSBL');
            }
            $add->reason = $reason;
            $add->save();
        }
        
        return !empty($blacklisted);
    }
    
    public static function checkAgainstDnsBl($blacklisted = false, $email, CList $blacklistedAt)
    {
        if ($blacklisted) {
            return $blacklisted;
        }
        
        $neededFunctions = array('gethostbyname', 'checkdnsrr');
        foreach ($neededFunctions as $function) {
            if (!function_exists($function)) {
                return $blacklisted;
            }
        }
        
        $emailParts = explode('@', $email);
        
        if (count($emailParts) != 2) {
            return $blacklisted;
        }
        
        $dnsBls = (array)Yii::app()->options->get('system.email_blacklist.remote_dnsbls', array());
        if (empty($dnsBls)) {
            return $blacklisted;
        }
        
        list($name, $domain) = $emailParts;
        if (filter_var($domain, FILTER_VALIDATE_IP)) {
            $ipAddress = $domain;
        } else {
            $ipAddress = @gethostbyname($domain);
        }
        if (strpos($ipAddress, '.') === false || !filter_var($ipAddress, FILTER_VALIDATE_IP)) {
            return $blacklisted;
        }
        
        $ipAddressBlocks  = explode('.', $ipAddress);
        $reverseIpAddress = implode ('.', array_reverse($ipAddressBlocks));

        shuffle($dnsBls);
        
        foreach ($dnsBls as $host) {
            if (@checkdnsrr($reverseIpAddress . '.' . $host . '.', 'A')) {
                $blacklisted = true;
                if (!$blacklistedAt->contains($host)) {
                    $blacklistedAt->add($host);    
                }
                break;
            }
        }
        
        return $blacklisted;
    }
}
