Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
user_form
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 get_user_row
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 check_allow
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
72
 bind
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 submit
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 render
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\message;
15
16/**
17* Class user_form
18* Allows users to send emails to other users
19*/
20class user_form extends form
21{
22    /** @var int */
23    protected $recipient_id;
24    /** @var array */
25    protected $recipient_row;
26    /** @var string */
27    protected $subject;
28
29    /**
30    * Get the data of the recipient
31    *
32    * @param int $user_id
33    * @return    false|array        false if the user does not exist, array otherwise
34    */
35    protected function get_user_row($user_id)
36    {
37        $sql = 'SELECT user_id, username, user_colour, user_email, user_allow_viewemail, user_lang, user_jabber, user_notify_type
38            FROM ' . USERS_TABLE . '
39            WHERE user_id = ' . (int) $user_id . '
40                AND user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
41        $result = $this->db->sql_query($sql);
42        $row = $this->db->sql_fetchrow($result);
43        $this->db->sql_freeresult($result);
44
45        return $row;
46    }
47
48    /**
49    * {inheritDoc}
50    */
51    public function check_allow()
52    {
53        $error = parent::check_allow();
54        if ($error)
55        {
56            return $error;
57        }
58
59        if (!$this->auth->acl_get('u_sendemail'))
60        {
61            return 'NO_EMAIL';
62        }
63
64        if ($this->recipient_id == ANONYMOUS || !$this->config['board_email_form'])
65        {
66            return 'NO_EMAIL';
67        }
68
69        if (!$this->recipient_row)
70        {
71            return 'NO_USER';
72        }
73
74        // Can we send email to this user?
75        if (!$this->recipient_row['user_allow_viewemail'] && !$this->auth->acl_get('a_user'))
76        {
77            return 'NO_EMAIL';
78        }
79
80        return false;
81    }
82
83    /**
84    * {inheritDoc}
85    */
86    public function bind(\phpbb\request\request_interface $request)
87    {
88        parent::bind($request);
89
90        $this->recipient_id = $request->variable('u', 0);
91        $this->subject = $request->variable('subject', '', true);
92
93        $this->recipient_row = $this->get_user_row($this->recipient_id);
94    }
95
96    /**
97    * {inheritDoc}
98    */
99    public function submit(\messenger $messenger)
100    {
101        if (!$this->subject)
102        {
103            $this->errors[] = $this->user->lang['EMPTY_SUBJECT_EMAIL'];
104        }
105
106        if (!$this->body)
107        {
108            $this->errors[] = $this->user->lang['EMPTY_MESSAGE_EMAIL'];
109        }
110
111        $this->message->set_template('profile_send_email');
112        $this->message->set_subject($this->subject);
113        $this->message->set_body($this->body);
114        $this->message->add_recipient_from_user_row($this->recipient_row);
115
116        parent::submit($messenger);
117    }
118
119    /**
120    * {inheritDoc}
121    */
122    public function render(\phpbb\template\template $template)
123    {
124        parent::render($template);
125
126        $template->assign_vars(array(
127            'S_SEND_USER'            => true,
128            'S_POST_ACTION'            => append_sid($this->phpbb_root_path . 'memberlist.' . $this->phpEx, 'mode=email&amp;u=' . $this->recipient_id),
129
130            'L_SEND_EMAIL_USER'        => $this->user->lang('SEND_EMAIL_USER', $this->recipient_row['username']),
131            'USERNAME_FULL'            => get_username_string('full', $this->recipient_row['user_id'], $this->recipient_row['username'], $this->recipient_row['user_colour']),
132            'SUBJECT'                => $this->subject,
133            'MESSAGE'                => $this->body,
134        ));
135    }
136}