Move System::xmlExit to DFRN\Notify->xmlExit

- This will ensure headers set in BaseModule->run will be carried in xmlExit scenarios
- Deprecate xmlExit() method in Core\System
This commit is contained in:
Hypolite Petovan 2023-09-21 19:28:18 -04:00
parent 46180d7d5b
commit d165a96220
3 changed files with 43 additions and 38 deletions

View File

@ -33,6 +33,7 @@ use Friendica\Module\Response;
use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Friendica\Util\XML;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;

View File

@ -311,27 +311,24 @@ class System
* Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
* of $st and an optional text <message> of $message and terminates the current process. * of $st and an optional text <message> of $message and terminates the current process.
* *
* @param $st * @param mixed $status
* @param string $message * @param string $message
* @throws \Exception * @throws \Exception
* @deprecated since 2023.09 Use BaseModule->httpExit() instead
*/ */
public static function xmlExit($st, $message = '') public static function xmlExit($status, string $message = '')
{ {
$result = ['status' => $st]; $result = ['status' => $status];
if ($message != '') { if ($message != '') {
$result['message'] = $message; $result['message'] = $message;
} }
if ($st) { if ($status) {
Logger::notice('xml_status returning non_zero: ' . $st . " message=" . $message); Logger::notice('xml_status returning non_zero: ' . $status . " message=" . $message);
} }
DI::apiResponse()->setType(Response::TYPE_XML); self::httpExit(XML::fromArray(['result' => $result]), Response::TYPE_XML);
DI::apiResponse()->addContent(XML::fromArray(['result' => $result]));
self::echoResponse(DI::apiResponse()->generate());
self::exit();
} }
/** /**

View File

@ -21,40 +21,22 @@
namespace Friendica\Module\DFRN; namespace Friendica\Module\DFRN;
use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Conversation; use Friendica\Model\Conversation;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\OStatus\Salmon;
use Friendica\Module\Response; use Friendica\Module\Response;
use Friendica\Network\HTTPException;
use Friendica\Protocol\DFRN; use Friendica\Protocol\DFRN;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Network\HTTPException; use Friendica\Util\XML;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* DFRN Notify * DFRN Notify
*/ */
class Notify extends BaseModule class Notify extends BaseModule
{ {
/** @var Database */
private $database;
public function __construct(Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->database = $database;
}
protected function post(array $request = []) protected function post(array $request = [])
{ {
$postdata = Network::postdata(); $postdata = Network::postdata();
@ -88,21 +70,21 @@ class Notify extends BaseModule
$contact_id = Contact::getIdForURL($msg['author']); $contact_id = Contact::getIdForURL($msg['author']);
if (empty($contact_id)) { if (empty($contact_id)) {
$this->logger->notice('Contact not found', ['address' => $msg['author']]); $this->logger->notice('Contact not found', ['address' => $msg['author']]);
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found'); $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
} }
// Fetch the importer (Mixture of sender and receiver) // Fetch the importer (Mixture of sender and receiver)
$importer = DFRN::getImporter($contact_id); $importer = DFRN::getImporter($contact_id);
if (empty($importer)) { if (empty($importer)) {
$this->logger->notice('Importer contact not found', ['address' => $msg['author']]); $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found'); $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
} }
$this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]); $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
// Now we should be able to import it // Now we should be able to import it
$ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY); $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
System::xmlExit($ret, 'Done'); $this->xmlExit($ret, 'Done');
return true; return true;
} }
@ -111,32 +93,57 @@ class Notify extends BaseModule
{ {
$msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? ''); $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
if (!is_array($msg)) { if (!is_array($msg)) {
System::xmlExit(4, 'Unable to parse message'); $this->xmlExit(4, 'Unable to parse message');
} }
// Fetch the contact // Fetch the contact
$contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']); $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
if (empty($contact['id'])) { if (empty($contact['id'])) {
$this->logger->notice('Contact not found', ['address' => $msg['author']]); $this->logger->notice('Contact not found', ['address' => $msg['author']]);
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found'); $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
} }
if ($contact['pending'] || $contact['blocked']) { if ($contact['pending'] || $contact['blocked']) {
$this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]); $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found'); $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
} }
// Fetch the importer (Mixture of sender and receiver) // Fetch the importer (Mixture of sender and receiver)
$importer = DFRN::getImporter($contact['id'], $user['uid']); $importer = DFRN::getImporter($contact['id'], $user['uid']);
if (empty($importer)) { if (empty($importer)) {
$this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]); $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found'); $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
} }
$this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]); $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
// Now we should be able to import it // Now we should be able to import it
$ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH); $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
System::xmlExit($ret, 'Done'); $this->xmlExit($ret, 'Done');
}
/**
* Generic XML return
* Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
* of $st and an optional text <message> of $message and terminates the current process.
*
* @param mixed $status
* @param string $message
* @throws \Exception
*/
private function xmlExit($status, string $message = '')
{
$result = ['status' => $status];
if ($message != '') {
$result['message'] = $message;
}
if ($status) {
$this->logger->notice('xml_status returning non_zero: ' . $status . " message=" . $message);
}
$this->httpExit(XML::fromArray(['result' => $result]), Response::TYPE_XML);
} }
} }