NotifySend.php
[Pman.Core] / NotifySend.php
1 <?php
2 require_once 'Pman.php';
3
4 /**
5  * notification script runner
6  *
7  * This does not actualy send stuf out, it only starts the NotifySend/{id}
8  * which does the actuall notifcations.
9  *
10  * It manages a pool of notifiers.
11  * 
12  * 
13  */
14
15
16 class Pman_Core_NotifySend extends Pman
17 {
18     
19     var $table = 'core_notify';
20     function getAuth()
21     {
22         $ff = HTML_FlexyFramework::get();
23         if (!$ff->cli) {
24             die("access denied");
25         }
26         //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
27         return true;
28         
29     }
30     
31     var $pool = array();
32     
33     function get($id)    
34     {
35         DB_DataObject::debugLevel(1);
36         //date_default_timezone_set('UTC');
37         // phpinfo();exit;
38         
39         $w = DB_DataObject::factory($this->table);
40         
41         if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
42             die("invalid id or time");
43         }
44          
45         $o = $w->object();
46         $p = $w->person();
47         
48         // let's work out the last notification sent to this user..
49         $l = DB_DataObject::factory($this->table);
50         $l->setFrom( array(
51                 'ontable' => $w->ontable,
52                 'onid' => $w->onid,
53                 'person_id' => $w->person_id,
54         ));        
55         $l->whereAdd('id != '. $w->id);
56         $l->orderBy('sent DESC');
57         $l->limit(1);
58         $ar = $l->fetchAll('sent');
59         $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
60         
61         
62         $email = $o->toEmail($p,$last);
63         // should we fetch the watch that caused it.. - which should contain the method to call..
64         $dom = array_pop(explode('@', $p->email));
65         
66         $mxs = $this->mxs($dom);
67         $ww = clone($w);
68         foreach($mxs as $dom) {
69             
70             $mailer = Mail::factory('smtp', array( 'host'         => $dom ));
71             $res = $mailer->send($email['recipients'], $email['headers'], $email['body']);
72             if ($res === true) {
73                 // success....
74                 
75                 $w->sent = date('Y-m-d H:i:s');
76                 $w->msgid = $email['headers']['Message-Id'];
77                 $w->event_id = -1; // sent ok.. - no need to record it..
78                 $w->update($ww);
79                 die("SENT");
80             }
81             // what type of error..
82             list($code, $response) = $mailer->_smtp->getResponse();
83             if ($code < 0) {
84                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
85             }
86             if ($code == 451) {
87                 // try again later..
88                 // check last event for this item..
89                 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
90                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
91                 $w->update($ww);
92                 die("GREYLISTED");
93             }
94             // fail.. = log and give up..
95             $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $res->toString());
96             $w->sent = date('Y-m-d H:i:s');
97             $w->msgid = '';
98             $w->event_id = $id;
99             $w->update($ww);
100             die("DONE");
101         }
102         
103         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
104         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
105         $w->update($ww);
106         die("NO HOST AVAILABLE");
107
108         
109     }
110     function mxs($fqdn)
111     {
112         $mx_records = array();
113         $mx_weight = array();
114         $mxs = array();
115         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
116             return araray($fqdn);
117         }
118         
119         asort($mx_weight,SORT_NUMERIC);
120         
121         forach($mx_weight as $k => $weight) {
122             $mxs[] = $mx_records[$k];
123         }
124         return $mxs;
125     }
126     
127     
128 }