Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / metamta / controller / PhabricatorMetaMTASendGridReceiveController.php
blob28ebdbe0ca2eba47e307be539c7177b423c1b273
1 <?php
3 final class PhabricatorMetaMTASendGridReceiveController
4 extends PhabricatorMetaMTAController {
6 public function shouldRequireLogin() {
7 return false;
10 public function handleRequest(AphrontRequest $request) {
11 // SendGrid doesn't sign payloads so we can't be sure that SendGrid
12 // actually sent this request, but require a configured SendGrid mailer
13 // before we activate this endpoint.
14 $mailers = PhabricatorMetaMTAMail::newMailers(
15 array(
16 'inbound' => true,
17 'types' => array(
18 PhabricatorMailSendGridAdapter::ADAPTERTYPE,
20 ));
21 if (!$mailers) {
22 return new Aphront404Response();
25 // No CSRF for SendGrid.
26 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
27 $user = $request->getUser();
29 $raw_headers = $request->getStr('headers');
30 $raw_headers = explode("\n", rtrim($raw_headers));
31 $raw_dict = array();
32 foreach (array_filter($raw_headers) as $header) {
33 list($name, $value) = explode(':', $header, 2);
34 $raw_dict[$name] = ltrim($value);
37 $headers = array(
38 'to' => $request->getStr('to'),
39 'from' => $request->getStr('from'),
40 'subject' => $request->getStr('subject'),
41 ) + $raw_dict;
43 $received = new PhabricatorMetaMTAReceivedMail();
44 $received->setHeaders($headers);
45 $received->setBodies(array(
46 'text' => $request->getStr('text'),
47 'html' => $request->getStr('from'),
48 ));
50 $file_phids = array();
51 foreach ($_FILES as $file_raw) {
52 try {
53 $file = PhabricatorFile::newFromPHPUpload(
54 $file_raw,
55 array(
56 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
57 ));
58 $file_phids[] = $file->getPHID();
59 } catch (Exception $ex) {
60 phlog($ex);
63 $received->setAttachments($file_phids);
64 $received->save();
66 $received->processReceivedMail();
68 $response = new AphrontWebpageResponse();
69 $response->setContent(pht('Got it! Thanks, SendGrid!')."\n");
70 return $response;