/
logger.php
197 lines (173 loc) · 7.87 KB
/
logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* @package org.openpsa.directmarketing
* @author Nemein Oy http://www.nemein.com/
* @copyright Nemein Oy http://www.nemein.com/
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
*/
use Symfony\Component\HttpFoundation\Request;
/**
* org.openpsa.directmarketing campaign handler and viewer class.
* @package org.openpsa.directmarketing
*/
class org_openpsa_directmarketing_handler_logger extends midcom_baseclasses_components_handler
{
/**
* Logs a bounce from bounce_detector.php for POSTed token, marks the send receipt
* and the campaign member as bounced.
*
* @param Request $request The request object
*/
public function _handler_bounce(Request $request)
{
if (!$request->request->has('token')) {
throw new midcom_error('Token not present in POST or empty');
}
$this->_request_data['update_status'] = ['receipts' => [], 'members' => []];
midcom::get()->auth->request_sudo('org.openpsa.directmarketing');
$ret = $this->_qb_token_receipts($request->request->get('token'));
//While in theory we should have only one token lets use foreach just to be sure
foreach ($ret as $receipt) {
//Mark receipt as bounced
debug_add("Found receipt #{$receipt->id}, marking bounced");
$receipt->bounced = time();
$this->_request_data['update_status']['receipts'][$receipt->guid] = $receipt->update();
//Mark member(s) as bounced (first get campaign trough message)
$message = org_openpsa_directmarketing_campaign_message_dba::get_cached($receipt->message);
$campaign = org_openpsa_directmarketing_campaign_dba::get_cached($message->campaign);
debug_add("Receipt belongs to message '{$message->title}' (#{$message->id}) in campaign '{$campaign->title}' (#{$campaign->id})");
$qb2 = org_openpsa_directmarketing_campaign_member_dba::new_query_builder();
$qb2->add_constraint('orgOpenpsaObtype', '=', org_openpsa_directmarketing_campaign_member_dba::NORMAL);
//PONDER: or should be just mark the person bounced in ALL campaigns while we're at it ?
$qb2->add_constraint('campaign', '=', $campaign->id);
$qb2->add_constraint('person', '=', $receipt->person);
foreach ($qb2->execute() as $member) {
debug_add("Found member #{$member->id}, marking bounced");
$member->orgOpenpsaObtype = org_openpsa_directmarketing_campaign_member_dba::BOUNCED;
$this->_request_data['update_status']['members'][$member->guid] = $member->update();
}
}
midcom::get()->auth->drop_sudo();
midcom::get()->skip_page_style = true;
midcom::get()->header('Content-type: text/plain');
}
/**
*
* @param mixed $handler_id The ID of the handler.
* @param array $data The local request data.
*/
public function _show_bounce($handler_id, array &$data)
{
echo "OK\n";
//PONDER: check $this->_request_data['update_status'] and display something else in case all is not ok ?
}
/**
* QB search for message receipts with given token and type
*
* @param string $token token string
* @param int $type receipt type, defaults to org_openpsa_directmarketing_campaign_messagereceipt_dba::SENT
* @return org_openpsa_directmarketing_campaign_messagereceipt_dba[]
*/
private function _qb_token_receipts($token, $type = org_openpsa_directmarketing_campaign_messagereceipt_dba::SENT)
{
debug_add("Looking for token '{$token}' in sent receipts");
$qb = org_openpsa_directmarketing_campaign_messagereceipt_dba::new_query_builder();
$qb->add_constraint('token', '=', $token);
$qb->add_constraint('orgOpenpsaObtype', '=', $type);
$ret = $qb->execute();
debug_print_r("_qb_token_receipts({$token}) returned", $ret);
if (empty($ret)) {
midcom::get()->auth->drop_sudo();
throw new midcom_error_notfound("No receipts with token '{$token}' found");
}
return $ret;
}
/**
* Logs a link click from link_detector.php for POSTed token, binds to person
* and creates received and read receipts as well
*
* @param Request $request The request object
*/
public function _handler_link(Request $request)
{
$token = $request->request->get('token');
$link = $request->request->get('link');
if (!$token) {
throw new midcom_error('Token not present in POST or empty');
}
if (!$link) {
throw new midcom_error('Link not present in POST or empty');
}
midcom::get()->auth->request_sudo('org.openpsa.directmarketing');
$ret = $this->_qb_token_receipts($token);
//While in theory we should have only one token lets use foreach just to be sure
foreach ($ret as $receipt) {
$this->_create_link_receipt($receipt, $token, $link);
}
midcom::get()->auth->drop_sudo();
midcom::get()->skip_page_style = true;
midcom::get()->header('Content-type: text/plain');
}
private function _create_link_receipt($receipt, $token, $target)
{
if (!array_key_exists('create_status', $this->_request_data)) {
$this->_request_data['create_status'] = ['receipts' => [], 'links' => []];
}
//Store the click in database
$link = new org_openpsa_directmarketing_link_log_dba();
$link->person = $receipt->person;
$link->message = $receipt->message;
$link->target = $target;
$link->token = $token;
$this->_request_data['create_status']['links'][$target] = $link->create();
//Create received and read receipts
$read_receipt = new org_openpsa_directmarketing_campaign_messagereceipt_dba();
$read_receipt->person = $receipt->person;
$read_receipt->message = $receipt->message;
$read_receipt->token = $token;
$read_receipt->orgOpenpsaObtype = org_openpsa_directmarketing_campaign_messagereceipt_dba::RECEIVED;
$this->_request_data['create_status']['receipts'][$token] = $read_receipt->create();
}
/**
*
* @param mixed $handler_id The ID of the handler.
* @param array $data The local request data.
*/
public function _show_link($handler_id, array &$data)
{
echo "OK\n";
//PONDER: check $this->_request_data['create_status'] and display something else in case all is not ok ?
}
/**
* Duplicates link_detector.php functionality in part (to avoid extra apache configurations)
* and handles the logging mentioned above as well.
*
* @param Request $request The request object
* @param string $token The token
* @param string $url The URL
*/
public function _handler_redirect(Request $request, $token, $url = null)
{
if (!empty($url)) {
//Due to the way browsers handle the URLs this form only works for root pages
$target = $url;
} elseif ($request->query->has('link')) {
$target = $request->query->get('link');
} else {
throw new midcom_error('Target not present in address or GET, or is empty');
}
//TODO: valid target domains check
//If we have a dummy token don't bother with looking for it, just go on.
if ($token === 'dummy') {
return new midcom_response_relocate($target);
}
midcom::get()->auth->request_sudo($this->_component);
$ret = $this->_qb_token_receipts($token);
//While in theory we should have only one token lets use foreach just to be sure
foreach ($ret as $receipt) {
$this->_create_link_receipt($receipt, $token, $target);
}
midcom::get()->auth->drop_sudo();
return new midcom_response_relocate($target);
}
}