streams/Code/Lib/DReport.php

157 lines
4.1 KiB
PHP
Raw Normal View History

2015-09-20 07:27:25 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2015-09-20 07:27:25 +00:00
2022-02-16 04:08:28 +00:00
use Code\Extend\Hook;
2021-12-02 23:02:31 +00:00
class DReport
{
private $location;
private $sender;
private $recipient;
private $message_id;
private $status;
private $date;
public function __construct($location, $sender, $recipient, $message_id, $status = 'deliver')
{
$this->location = $location;
$this->sender = $sender;
$this->recipient = $recipient;
$this->name = EMPTY_STR;
$this->message_id = $message_id;
$this->status = $status;
$this->date = datetime_convert();
}
public function update($status)
{
$this->status = $status;
$this->date = datetime_convert();
}
public function set_name($name)
{
$this->name = $name;
}
public function addto_update($status)
{
$this->status = $this->status . ' ' . $status;
}
public function set($arr)
{
$this->location = $arr['location'];
$this->sender = $arr['sender'];
$this->recipient = $arr['recipient'];
$this->name = $arr['name'];
$this->message_id = $arr['message_id'];
$this->status = $arr['status'];
$this->date = $arr['date'];
}
public function get()
{
return array(
'location' => $this->location,
'sender' => $this->sender,
'recipient' => $this->recipient,
'name' => $this->name,
'message_id' => $this->message_id,
'status' => $this->status,
'date' => $this->date
);
}
/**
* @brief decide whether to store a returned delivery report
*
* @param array $dr
* @return bool
*/
public static function is_storable($dr)
{
2021-12-03 03:01:39 +00:00
if (get_config('system', 'disable_dreport')) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
/**
* @hooks dreport_is_storable
* Called before storing a dreport record to determine whether to store it.
* * \e array
*/
Hook::call('dreport_is_storable', $dr);
2021-12-02 23:02:31 +00:00
// let plugins accept or reject - if neither, continue on
2021-12-03 03:01:39 +00:00
if (array_key_exists('accept', $dr) && intval($dr['accept'])) {
2021-12-02 23:02:31 +00:00
return true;
2021-12-03 03:01:39 +00:00
}
if (array_key_exists('reject', $dr) && intval($dr['reject'])) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if (!($dr['sender'])) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// Is the sender one of our channels?
2021-12-03 03:01:39 +00:00
$c = q(
"select channel_id from channel where channel_hash = '%s' limit 1",
2021-12-02 23:02:31 +00:00
dbesc($dr['sender'])
);
2021-12-03 03:01:39 +00:00
if (!$c) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// is the recipient one of our connections, or do we want to store every report?
$rxchan = $dr['recipient'];
$pcf = get_pconfig($c[0]['channel_id'], 'system', 'dreport_store_all');
2021-12-03 03:01:39 +00:00
if ($pcf) {
2021-12-02 23:02:31 +00:00
return true;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// We always add ourself as a recipient to private and relayed posts
// So if a remote site says they can't find us, that's no big surprise
// and just creates a lot of extra report noise
2021-12-03 03:01:39 +00:00
if (($dr['location'] !== z_root()) && ($dr['sender'] === $rxchan) && ($dr['status'] === 'recipient not found')) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// If you have a private post with a recipient list, every single site is going to report
// back a failed delivery for anybody on that list that isn't local to them. We're only
// concerned about this if we have a local hubloc record which says we expected them to
// have a channel on that site.
2021-12-03 03:01:39 +00:00
$r = q(
"select hubloc_id from hubloc where hubloc_hash = '%s' and hubloc_url = '%s'",
2021-12-02 23:02:31 +00:00
dbesc($rxchan),
dbesc($dr['location'])
);
2021-12-03 03:01:39 +00:00
if ((!$r) && ($dr['status'] === 'recipient not found')) {
2021-12-02 23:02:31 +00:00
return false;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select abook_id from abook where abook_xchan = '%s' and abook_channel = %d limit 1",
2021-12-02 23:02:31 +00:00
dbesc($rxchan),
intval($c[0]['channel_id'])
);
2021-12-03 03:01:39 +00:00
if ($r) {
2021-12-02 23:02:31 +00:00
return true;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
return false;
}
2015-09-20 07:27:25 +00:00
}