NotifySend.php
[Pman.Core] / NotifySend.php
1 <?php
2 require_once 'Pman.php';
3
4 /**
5  * notification script sender - designed to be run by the Notify script - with many children running
6  * in parallel.
7  *
8  * called with an id of a core_notify element
9  *
10  * uses core_notify - to find an event to object and person.
11  *
12  * uses Events table to log failures
13  * 
14  * 
15  * calls $object->toEmail($person,$last_send, $notify) to generate an email struct with
16  *  array (
17  *      headers =>
18  *      recipients =>
19  *      body =>
20  *  )
21  * 
22  * 
23  */
24
25
26 class Pman_Core_NotifySend extends Pman
27 {
28     
29     var $table = 'core_notify';
30     function getAuth()
31     {
32         $ff = HTML_FlexyFramework::get();
33         if (!$ff->cli) {
34             die("access denied");
35         }
36         //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
37         return true;
38         
39     }
40     
41     var $pool = array();
42     
43     function get($id)    
44     {
45         //DB_DataObject::debugLevel(1);
46         //date_default_timezone_set('UTC');
47         // phpinfo();exit;
48         
49         $w = DB_DataObject::factory($this->table);
50         
51         if (!$w->get($id)) {
52             die("invalid id or time\n");
53         }
54         if (strtotime($w->act_when) < strtotime($w->sent)) {
55             die("send repeat to early\n");
56         }
57          
58         $o = $w->object();
59         $p = $w->person();
60         
61         // let's work out the last notification sent to this user..
62         $l = DB_DataObject::factory($this->table);
63         $l->setFrom( array(
64                 'ontable' => $w->ontable,
65                 'onid' => $w->onid,
66                 'person_id' => $w->person_id,
67         ));        
68         $l->whereAdd('id != '. $w->id);
69         $l->orderBy('sent DESC');
70         $l->limit(1);
71         $ar = $l->fetchAll('sent');
72         $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
73         
74         // find last event..
75         $ev = DB_DataObject::factory('Events');
76         $ev->on_id = $w->id;                           // int(11)
77         $ev->od_table = $this->table;
78         $ev->limit(1);
79         $ev->orderBy('event_when DESC');
80         $ar = $ev->fetchAll('event_when');
81         $last_event = empty($ar) ? 0 : $ar[0];
82         $next_try_min = 5;
83         if ($last_event) {
84             $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
85         }
86         $next_try = $next_try_min . ' MINUTES';
87         
88         
89         $email = $o->toEmail($p,$last);
90         
91         //$p->email = 'alan@akbkhome.com'; //for testing..
92         //print_r($email);exit;
93         // should we fetch the watch that caused it.. - which should contain the method to call..
94         $dom = array_pop(explode('@', $p->email));
95         
96         $mxs = $this->mxs($dom);
97         $ww = clone($w);
98         
99         $fail = false;
100         require_once 'Mail.php';
101         
102         foreach($mxs as $dom) {
103             
104             $mailer = Mail::factory('smtp', array( 'host'         => $dom ));
105             $res = $mailer->send($p->email, $email['headers'], $email['body']);
106             if ($res === true) {
107                 // success....
108                 
109                 $w->sent = date('Y-m-d H:i:s');
110                 $w->msgid = $email['headers']['Message-Id'];
111                 $w->event_id = -1; // sent ok.. - no need to record it..
112                 $w->update($ww);
113                 die(date('Y-m-d h:i:s') . " - SENT");
114             }
115             // what type of error..
116             list($code, $response) = $mailer->_smtp->getResponse();
117             if ($code < 0) {
118                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
119             }
120             // give up after 2 days..
121             if ($code == 451 && $next_try_min < (2*24*60)) {
122                 // try again later..
123                 // check last event for this item..
124                 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
125                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
126                 $w->update($ww);
127                 die(date('Y-m-d h:i:s') . " - GREYLISTED");
128             }
129             $fail = true;
130             break;
131         }
132         if ($fail || $next_try_min > (2*24*60)) {
133         // fail.. = log and give up..
134             $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
135             $w->sent = date('Y-m-d H:i:s');
136             $w->msgid = '';
137             $w->event_id = $id;
138             $w->update($ww);
139             die(date('Y-m-d h:i:s') . " - DONE");
140         }
141         
142         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
143         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
144         $w->update($ww);
145         die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE");
146
147         
148     }
149     function mxs($fqdn)
150     {
151         $mx_records = array();
152         $mx_weight = array();
153         $mxs = array();
154         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
155             return array($fqdn);
156         }
157         
158         asort($mx_weight,SORT_NUMERIC);
159         
160         foreach($mx_weight as $k => $weight) {
161             $mxs[] = $mx_records[$k];
162         }
163         return $mxs;
164     }
165     
166     
167 }