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         // find last event..
62         $ev = DB_DataObject::factory('Events');
63         $ev->on_id = $w->id;                           // int(11)
64         $ev->od_table = $this->table;
65         $ev->limit(1);
66         $ev->orderBy('event_when DESC');
67         $ar = $ev->fetchAll('event_when');
68         $last_event = empty($ar) ? 0 : $ar[0];
69         $next_try_min = 5;
70         if ($last_event) {
71             $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
72         }
73         $next_try = $next_try_min . ' MINUTES';
74         
75         
76         $email = $o->toEmail($p,$last);
77         // should we fetch the watch that caused it.. - which should contain the method to call..
78         $dom = array_pop(explode('@', $p->email));
79         
80         $mxs = $this->mxs($dom);
81         $ww = clone($w);
82         
83         
84         
85         
86         foreach($mxs as $dom) {
87             
88             $mailer = Mail::factory('smtp', array( 'host'         => $dom ));
89             $res = $mailer->send($email['recipients'], $email['headers'], $email['body']);
90             if ($res === true) {
91                 // success....
92                 
93                 $w->sent = date('Y-m-d H:i:s');
94                 $w->msgid = $email['headers']['Message-Id'];
95                 $w->event_id = -1; // sent ok.. - no need to record it..
96                 $w->update($ww);
97                 die("SENT");
98             }
99             // what type of error..
100             list($code, $response) = $mailer->_smtp->getResponse();
101             if ($code < 0) {
102                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
103             }
104             // give up after 2 days..
105             if ($code == 451 && $next_try_min < (2*24*60)) {
106                 // try again later..
107                 // check last event for this item..
108                 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
109                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
110                 $w->update($ww);
111                 die("GREYLISTED");
112             }
113             $fail = true;
114             break;
115         }
116         if ($fail || $next_try_min > (2*24*60)) {
117         // fail.. = log and give up..
118             $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
119             $w->sent = date('Y-m-d H:i:s');
120             $w->msgid = '';
121             $w->event_id = $id;
122             $w->update($ww);
123             die("DONE");
124         }
125         
126         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
127         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
128         $w->update($ww);
129         die("NO HOST AVAILABLE");
130
131         
132     }
133     function mxs($fqdn)
134     {
135         $mx_records = array();
136         $mx_weight = array();
137         $mxs = array();
138         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
139             return araray($fqdn);
140         }
141         
142         asort($mx_weight,SORT_NUMERIC);
143         
144         forach($mx_weight as $k => $weight) {
145             $mxs[] = $mx_records[$k];
146         }
147         return $mxs;
148     }
149     
150     
151 }