Fix #7796 - media outreach crm - needs distributed email sending (due to spam blocks)
[Pman.Core] / DataObjects / Core_notify_server.php
1 <?php
2 /**
3  * Table Definition for core_notify_server
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_notify_server 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_server';    // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $hostname;
15     public $helo;
16     public $poolname;
17     public $is_active;
18     public $last_send;
19     
20     // most services should call this first..
21     
22     function getCurrent($notify)
23     {
24         static $current = false;;
25         
26         if ($current !== false) {
27             return $current;
28         }
29         
30         $ns = DB_DataObject::factory('core_notify_server');
31         $ns->poolname = $notify->poolname;
32         $ns->is_active = 1;
33         $ns->hostname = gethostname();
34         if (!$ns->count()) {
35             $notify->jerr("Server not found for this server " .  gethostname() . " in core_notify_server" );
36         }
37         $ns->find(true);
38         $current = $ns;
39         return $ns;
40     }
41     
42     
43     function isFirstServer()
44     {
45         $servers = $this->availableServers();
46         if (empty($servers)) {
47             return false;
48         }
49         // only run this on the first server...
50         return $this->id == $servers[0]->id;
51     }
52     
53     
54     // called on current server.
55     function assignQueues($notify)
56     {
57          
58         
59         $servers = $this->availableServers();
60         $ids = array();
61         foreach($servers as $s) {
62             $ids[] = $s->id;
63         }
64         
65         
66         if (empty($ids)) {
67             $notify->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
68             
69         }
70          
71         // only run this on the first server...
72         if ($this->id != $ids[0]) {
73             return; 
74         }
75         foreach($ids as $rn) {
76             $up[$rn]  = array();
77         }
78         
79         $num_servers = count($ids);
80         
81         if ($num_servers == 1) {
82             $p = DB_DataObject::factory($notify->table);
83             $p->query("
84                 UPDATE
85                     {$notify->table}
86                 SET
87                     server_id = {$ids[0]}
88                 WHERE
89                     sent < '2000-01-01'
90                     and
91                     event_id = 0
92                     and
93                     act_start < NOW() +  INTERVAL 3 HOUR 
94                     and
95                     server_id != {$ids[0]}
96             ");
97             return;
98         }
99         
100         // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})
101         
102         
103         
104         $p = DB_DataObject::factory($notify->table);
105         $p->whereAdd("
106                 sent < '2000-01-01'
107                 and
108                 event_id = 0
109                 and
110                 act_start < NOW() +  INTERVAL 3 HOUR 
111                 and
112                 server_id NOT IN (" . implode(",", $ids) . ")
113         ");
114         if ($p->count() < 1) {
115             return;
116         }
117         
118         $p->selectAdd();
119         $p->selectAdd("id, ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})  as rn");
120         $kv = $p->fetchAll('id,rn');
121         foreach($kv as $id => $r) {
122             $up[ $ids[$r] ][] = $id;
123         }
124         foreach($up as $sid => $nids) {
125             $p = DB_DataObject::factory($notify->table);
126             $p->query("
127                 UPDATE
128                     {$this->table}
129                 SET
130                     server_id = $sid
131                 WHERE
132                     id IN (". implode(",', $nids"). ')'
133             );
134         }
135          
136         DB_DataObject::factory("core_notify_blacklist")->prune();
137         
138     }
139         // called on current server.
140
141     function availableServers()
142     {
143         $ns = DB_DataObject::factory('core_notify_server');
144         $ns->poolname = $this->poolname;
145         $ns->is_active = 1;
146         $ns->orderBy('id ASC');
147         return  $ns->fetchAll();
148         
149     }
150     
151     function updateNotifyToNextServer( $cn , $when = false, $allow_same = false)
152     {
153         // fixme - this should take into account blacklisted - and return false if no more servers are available
154         $email = empty($cn->to_email) ? ($cn->person() ? $cn->person()->email : $cn->to_email) : $cn->to_email;
155
156         $w = DB_DataObject::factory($cn->tableName());
157         $w->get($cn->id);
158         
159         $servers = $this->availableServerIds();
160         $start = 0;
161         foreach($servers as $i => $s) {
162             if ($s->id == $this->id) {
163                 $start = $i;
164             }
165         }
166         
167         $offset = ($start + 1)  % count($ids);
168         $good = false;
169         while ($offset  != $start) {
170             $s = $servers[$offset];
171             if (!$s->isBlacklisted($email)) {
172                 $good = $s;
173                 break;
174             }
175         }
176         if ($good == false && $allow_same) {
177             $good = $this;
178         }
179         
180         if ($good == false) {
181             return false;
182         }
183         
184         
185         // next server..
186         $pp = clone($w);
187         $w->server_id = $good->id;   
188         $w->act_when = $when === false ? $w->sqlValue('NOW() + INTERVAL 1 MINUTE') : $when;
189         $w->update($pp);
190         return true;
191     }
192     
193     
194     function isBlacklisted($email)
195     {
196         // return current server id..
197         static $cache = array();
198         
199         // get the domain..
200         $ea = explode('@',$email);
201         $dom = strtolower(array_pop($ea));
202         if (isset($cache[$dom])) {
203             return $cache[$dom];
204         }
205         
206         $cd = DB_DataObject::factory('core_domain')->loadOrCreate($dom);
207         
208         $bl = DB_DataObject::factory('core_notify_blacklist');
209         $bl->server_id = $this->id;
210         $bl->domain_id = $cd->id;
211         if ($bl->count()) {
212             $cache[$dom] = true;
213             return true;
214         }
215         
216         return ralse; 
217     }
218     function initHelo()
219     {
220         
221         $ff->Mail['helo'] = $this->helo;
222         
223     }
224     function checkSmtpResponse($errmsg, $core_domain)
225     {
226         $bl = DB_DataObject::factory('core_notify_blacklist');
227         $bl->server_id = $this->id;
228         $bl->domain_id = $core_domain->id;
229         if ($bl->count()) {
230             return;
231         }
232         // is it a blacklist message
233         if (!$bl->messageIsBlacklisted($errmsg)) {
234             return;
235         }
236         $bl->added_dt = $bl->sqlValue("NOW()");
237         $bl->insert();
238         
239         
240     }
241     
242 }