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     function get($id)    
42     {
43         //DB_DataObject::debugLevel(1);
44         //date_default_timezone_set('UTC');
45         // phpinfo();exit;
46         
47         $w = DB_DataObject::factory($this->table);
48         
49         if (!$w->get($id)) {
50             die("invalid id or time\n");
51         }
52         if (strtotime($w->act_when) < strtotime($w->sent)) {
53             die("send repeat to early\n");
54         }
55          
56         $o = $w->object();
57         $p = $w->person();
58         
59         // let's work out the last notification sent to this user..
60         $l = DB_DataObject::factory($this->table);
61         $l->setFrom( array(
62                 'ontable' => $w->ontable,
63                 'onid' => $w->onid,
64                 'person_id' => $w->person_id,
65         ));        
66         $l->whereAdd('id != '. $w->id);
67         $l->orderBy('sent DESC');
68         $l->limit(1);
69         $ar = $l->fetchAll('sent');
70         $last = empty($ar) ? date('Y-m-d H:i:s', 0) : $ar[0];
71         
72         // find last event..
73         $ev = DB_DataObject::factory('Events');
74         $ev->on_id = $w->id;                           // int(11)
75         $ev->od_table = $this->table;
76         $ev->limit(1);
77         $ev->orderBy('event_when DESC');
78         $ar = $ev->fetchAll('event_when');
79         $last_event = empty($ar) ? 0 : $ar[0];
80         $next_try_min = 5;
81         if ($last_event) {
82             $next_try_min = floor((time() - strtotime($last_event)) / 60) * 2;
83         }
84         $next_try = $next_try_min . ' MINUTES';
85         
86         $email =  $this->makeEmail($o, $p,$last);
87         
88         
89         
90         //$p->email = 'alan@akbkhome.com'; //for testing..
91         //print_r($email);exit;
92         // should we fetch the watch that caused it.. - which should contain the method to call..
93         $dom = array_pop(explode('@', $p->email));
94         
95         $mxs = $this->mxs($dom);
96         $ww = clone($w);
97         
98         $fail = false;
99         require_once 'Mail.php';
100         
101         foreach($mxs as $dom) {
102             
103             $mailer = Mail::factory('smtp', array( 'host'    => $dom ));
104             $res = $mailer->send($p->email, $email['headers'], $email['body']);
105             if ($res === true) {
106                 // success....
107                 
108                 $w->sent = date('Y-m-d H:i:s');
109                 $w->msgid = $email['headers']['Message-Id'];
110                 $w->event_id = -1; // sent ok.. - no need to record it..
111                 $w->update($ww);
112                 die(date('Y-m-d h:i:s') . " - SENT");
113             }
114             // what type of error..
115             list($code, $response) = $mailer->_smtp->getResponse();
116             if ($code < 0) {
117                 continue; // try next mx... ??? should we wait??? - nope we did not even connect..
118             }
119             // give up after 2 days..
120             if ($code == 421 && $next_try_min < (2*24*60)) {
121                 // try again later..
122                 // check last event for this item..
123                 $this->addEvent('NOTIFY', $w, 'GREYLISTED');
124                 $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
125                 $w->update($ww);
126                 die(date('Y-m-d h:i:s') . " - GREYLISTED");
127             }
128             $fail = true;
129             break;
130         }
131         if ($fail || $next_try_min > (2*24*60)) {
132         // fail.. = log and give up..
133             $id = $this->addEvent('NOTIFY', $w, 'FAILED - '. ($fail ? $res->toString() : "RETRY TIME EXCEEDED"));
134             $w->sent = date('Y-m-d H:i:s');
135             $w->msgid = '';
136             $w->event_id = $id;
137             $w->update($ww);
138             die(date('Y-m-d h:i:s') . ' - FAILED - '. $fail ? $res->toString() : "RETRY TIME EXCEEDED");
139         }
140         
141         $this->addEvent('NOTIFY', $w, 'NO HOST CAN BE CONTACTED');
142         $w->act_when = date('Y-m-d H:i:s', strtotime('NOW + 5 MINUTES'));
143         $w->update($ww);
144         die(date('Y-m-d h:i:s') ." - NO HOST AVAILABLE");
145
146         
147     }
148     function mxs($fqdn)
149     {
150         $mx_records = array();
151         $mx_weight = array();
152         $mxs = array();
153         if (!getmxrr($fqdn, $mx_records, $mx_weight)) {
154             return array($fqdn);
155         }
156         
157         asort($mx_weight,SORT_NUMERIC);
158         
159         foreach($mx_weight as $k => $weight) {
160             $mxs[] = $mx_records[$k];
161         }
162         return $mxs;
163     }
164     
165     /**
166      * wrapper to call object->toEmail()
167      * 
168      **/
169     function makeEmail($o, $p, $last)
170     {
171         return $o->toEmail($p,$last);
172     }
173 }