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