fix typo
[Pman.Core] / DataObjects / Core_notify_sender.php
1 <?php
2 /**
3  * Table Definition for core_notify_sender
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_notify_sender extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_notify_sender';    // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $email;
15     public $poolname;
16     public $is_active;
17     public $priority;
18     
19     
20     
21     function emailToSender($email , $notify)
22     {
23         require_once 'Mail/RFC822.php';
24         $parser = new Mail_RFC822();
25         $addresses = $parser->parseAddressList( $email['headers']['From'], 'localhost', false);
26         if (is_a($addresses, 'PEAR_Error')) {
27             return false;
28         }
29         $from_addr = $addresses[0];
30         //print_r($email['headers']['From']);         print_R($from_addr);exit;
31
32         $from = $from_addr->mailbox . '@' . $from_addr->host;
33
34         $ns = DB_DataObject::Factory($this->tableName());
35         $ns->setFrom(array(
36             'email' => $from,
37             'poolname' => $notify->tableName()
38         ));
39         if (!$ns->find(true)) {
40             $ns->is_active = 1;
41             $ns->priority = 1;
42             $ns->insert();
43             return false;
44         }
45         return $ns;
46     }
47     
48     
49     function filterEmail($email, $notify)
50     {
51         //
52         if (empty($email['headers']['From']) || empty($notify)) {
53             return $email;
54         }
55         $bits = explode('@', $notify->to_email);
56         $to_dom = DB_DataObject::factory('core_domain')->loadOrCreate($bits[1]);
57         
58         
59         $ns = $this->emailToSender($email, $notify);
60         if ($ns == false) {
61             return $email;
62         }
63         // is it blacklisted?
64         $bl = DB_DAtaObject::Factory('core_notify_sender_blacklist');
65         $bl->setFrom(array(
66                 'sender_id'=> $ns->id,
67                 'domain_id' => $to_dom->id
68         ));
69         if (!$bl->count()) {
70             return $email;
71         }
72         // it's blacklisted..
73         // try finding alternative.
74         $bl = DB_DAtaObject::Factory('core_notify_sender_blacklist');
75         $bl->domain_id = $to_dom->id;
76         $bad_ids = $bl->fetchAll('sender_id');
77         
78         $ns = DB_DataObject::Factory($this->tableName());
79         $ns->setFrom(array(
80             'email' => $from,
81             'is_active' => 1,
82         ));
83         $ns->whereAddIn('!id', $bad_ids, 'int');
84         if (!$ns->count()){
85             return $email; // no alternative available
86         }
87         $ns->limit(1);
88         $ns->find(true);
89         $mail['headers']['From'] = $from_addr->personal . ' <' . $ns->email .'>';
90         
91         return $mail;
92         
93         //check blacklist for 
94         
95          
96         
97     }
98     
99     function checkSmtpResponse($email, $notify, $errmsg)
100     {
101         $bl = DB_DataObject::factory('core_notify_sender_blacklist');
102         if (!$bl->messageIsBlacklisted($errmsg)) {
103             return; // ok.
104         }
105         // create a record.
106         
107         $bits = explode('@', $notify->to_email);
108         $to_dom = DB_DataObject::factory('core_domain')->loadOrCreate($bits[1]);
109         
110         $ns = $this->emailToSender($email, $notify);
111         $bl->setFrom(array(
112             'sender_id' => $ns->id,
113             'domain_id' => $to_dom->id,
114         ));
115         if ($bl->count()) {
116             return;
117         }
118         $bl->error_msg = $errmsg;
119         $bl->added_dt = $bl->sqlValue('NOW()');
120         $bl->insert();
121         
122     }
123     
124 }