Issues (326)

src/Controller/ContactsController.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Controller;
14
15
use App\Form\ContactForm;
16
use App\Form\ContactFormOwner;
17
use Cake\Datasource\Exception\RecordNotFoundException;
18
use Cake\Event\Event;
19
use Cake\Form\Form;
20
use Cake\Http\Exception\BadRequestException;
21
use Cake\ORM\TableRegistry;
22
use Saito\Exception\Logger\ExceptionLogger;
23
24
class ContactsController extends AppController
25
{
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function beforeFilter(Event $event)
31
    {
32
        parent::beforeFilter($event);
33
        $this->set('showDisclaimer', true);
34
        $this->Authentication->allowUnauthenticated(['owner']);
35
    }
36
37
    /**
38
     * Contacts forum's owner via contact address
39
     *
40
     * @return void
41
     */
42
    public function owner()
43
    {
44
        $recipient = 'contact';
45
        if ($this->CurrentUser->isLoggedIn()) {
46
            $user = $this->CurrentUser;
47
            $sender = $user->getId();
48
            $this->request = $this->request->withData('sender_contact', $user->get('user_email'));
49
        } else {
50
            $senderContact = $this->request->getData('sender_contact');
51
            $sender = [$senderContact => $senderContact];
52
        }
53
54
        $this->_contact(new ContactFormOwner(), $recipient, $sender);
55
    }
56
57
    /**
58
     * Contacts individual user
59
     *
60
     * @param string $id user-ID
61
     * @return void
62
     * @throws \InvalidArgumentException
63
     * @throws BadRequestException
64
     */
65
    public function user($id = null)
66
    {
67
        if (empty($id) || !$this->CurrentUser->isLoggedIn()) {
68
            throw new BadRequestException();
69
        }
70
71
        $Users = TableRegistry::get('Users');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        $Users = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Users');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
        try {
73
            $recipient = $Users->get($id);
74
        } catch (RecordNotFoundException $e) {
75
            throw new BadRequestException();
76
        }
77
        $this->set('user', $recipient);
78
79
        if (
80
            !$recipient->get('personal_messages')
81
            && !$this->CurrentUser->permission('saito.core.user.contact')
82
        ) {
83
            throw new BadRequestException(null, 1562415010);
84
        }
85
86
        $this->set(
87
            'titleForPage',
88
            __('user_contact_title', $recipient->get('username'))
89
        );
90
91
        $sender = $this->CurrentUser->getId();
92
        $this->_contact(new ContactForm(), $recipient, $sender);
93
    }
94
95
    /**
96
     *  contact form validating and email sending
97
     *
98
     * @param Form $contact contact-form
99
     * @param mixed $recipient recipient
100
     * @param mixed $sender sender
101
     * @return \Cake\Http\Response|void
102
     */
103
    protected function _contact(Form $contact, $recipient, $sender)
104
    {
105
        if ($this->request->is('get')) {
106
            if ($this->request->getData('cc') === null) {
107
                $this->request = $this->request->withData('cc', true);
108
            }
109
        }
110
111
        if ($this->request->is('post')) {
112
            $isValid = $contact->validate($this->request->getData());
0 ignored issues
show
It seems like $this->request->getData() can also be of type null; however, parameter $data of Cake\Form\Form::validate() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $isValid = $contact->validate(/** @scrutinizer ignore-type */ $this->request->getData());
Loading history...
113
            if ($isValid) {
114
                try {
115
                    $email = [
116
                        'recipient' => $recipient,
117
                        'sender' => $sender,
118
                        'subject' => $this->request->getData('subject'),
119
                        'message' => $this->request->getData('text'),
120
                        'template' => 'user_contact',
121
                        'ccsender' => (bool)$this->request->getData('cc'),
122
                    ];
123
                    $this->SaitoEmail->email($email);
124
                    $message = __('Message was send.');
125
                    $this->Flash->set($message, ['element' => 'success']);
126
127
                    return $this->redirect('/');
128
                } catch (\Exception $e) {
129
                    $Logger = new ExceptionLogger();
130
                    $Logger->write('Contact email failed', ['e' => $e]);
131
                    $message = $e->getMessage();
132
                    $message = __('Message couldn\'t be send: {0}', $message);
133
                    $this->Flash->set($message, ['element' => 'error']);
134
                }
135
            }
136
        }
137
138
        $this->set(compact('contact'));
139
    }
140
}
141