3 * Table Definition for core_notify_server
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_notify_server'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
20 // most services should call this first..
22 function getCurrent($notify)
24 static $current = false;;
26 if ($current !== false) {
30 $ns = DB_DataObject::factory('core_notify_server');
31 $ns->poolname = $notify->poolname;
33 $ns->hostname = gethostname();
35 $notify->jerr("Server not found for this server " . gethostname() . " in core_notify_server" );
43 function isFirstServer()
45 $servers = $this->availableServers();
46 if (empty($servers)) {
49 // only run this on the first server...
50 return $this->id == $servers[0]->id;
54 // called on current server.
55 function assignQueues($notify)
59 $servers = $this->availableServers();
61 foreach($servers as $s) {
67 $notify->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
71 // only run this on the first server...
72 if ($this->id != $ids[0]) {
75 foreach($ids as $rn) {
79 $num_servers = count($ids);
81 if ($num_servers == 1) {
82 $p = DB_DataObject::factory($notify->table);
93 act_start < NOW() + INTERVAL 3 HOUR
95 server_id != {$ids[0]}
100 // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END +1) % {$num_servers})
104 $p = DB_DataObject::factory($notify->table);
110 act_start < NOW() + INTERVAL 3 HOUR
112 server_id NOT IN (" . implode(",", $ids) . ")
114 if ($p->count() < 1) {
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;
124 foreach($up as $sid => $nids) {
125 $p = DB_DataObject::factory($notify->table);
132 id IN (". implode(",', $nids"). ')'
136 DB_DataObject::factory("core_notify_blacklist")->prune();
139 // called on current server.
141 function availableServers()
143 $ns = DB_DataObject::factory('core_notify_server');
144 $ns->poolname = $this->poolname;
146 $ns->orderBy('id ASC');
147 return $ns->fetchAll();
151 function updateNotifyToNextServer( $cn , $when = false, $allow_same = false)
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;
156 $w = DB_DataObject::factory($cn->tableName());
159 $servers = $this->availableServers();
161 foreach($servers as $i => $s) {
162 if ($s->id == $this->id) {
167 $offset = ($start + 1) % count($servers);
169 while ($offset != $start) {
170 $s = $servers[$offset];
171 if (!$s->isBlacklisted($email)) {
176 if ($good == false && $allow_same) {
180 if ($good == false) {
187 $w->server_id = $good->id;
188 $w->act_when = $when === false ? $w->sqlValue('NOW() + INTERVAL 1 MINUTE') : $when;
194 function isBlacklisted($email)
196 // return current server id..
197 static $cache = array();
200 $ea = explode('@',$email);
201 $dom = strtolower(array_pop($ea));
202 if (isset($cache[$dom])) {
206 $cd = DB_DataObject::factory('core_domain')->loadOrCreate($dom);
208 $bl = DB_DataObject::factory('core_notify_blacklist');
209 $bl->server_id = $this->id;
210 $bl->domain_id = $cd->id;
220 $ff = HTML_FlexyFramework::get();
221 $ff->Mail['helo'] = $this->helo;
224 function checkSmtpResponse($errmsg, $core_domain)
226 $bl = DB_DataObject::factory('core_notify_blacklist');
227 $bl->server_id = $this->id;
228 $bl->domain_id = $core_domain->id;
232 // is it a blacklist message
233 if (!$bl->messageIsBlacklisted($errmsg)) {
236 $bl->added_dt = $bl->sqlValue("NOW()");