Passed
Push — master ( e78604...9c0ac0 )
by Stanislav
32:56 queued 29:42
created

comment::is_able()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
*
4
* @package phpBB Gallery
5
* @version $Id$
6
* @copyright (c) 2007 nickvergessen [email protected] http://www.flying-bits.org
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
namespace phpbbgallery\core;
12
13
class comment
14
{
15
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
	protected $user;
17
18
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	protected $db;
20
21
	/** @var \phpbbgallery\core\config */
22
	protected $config;
23
24
	/** @var \phpbbgallery\core\auth\auth */
25
	protected $auth;
26
27
	/** @var \phpbbgallery\core\block */
28
	protected $block;
29
30
	/** @var string */
31
	protected $comments_table;
32
33
	/** @var string */
34
	protected $images_table;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\user                       $user
40
	 * @param \phpbb\db\driver\driver_interface $db
41
	 * @param \phpbbgallery\core\config         $config
42
	 * @param \phpbbgallery\core\auth\auth      $auth
43
	 * @param block                             $block
44
	 * @param                                   $comments_table
45
	 * @param                                   $images_table
46
	 * @internal param image\image $image
47
	 * @internal param album\album $album
48
	 */
49
50 27
	public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db,
51
								\phpbbgallery\core\config $config, \phpbbgallery\core\auth\auth $auth, \phpbbgallery\core\block $block,
52
								$comments_table, $images_table)
53
	{
54 27
		$this->user = $user;
55 27
		$this->db = $db;
56 27
		$this->config = $config;
57 27
		$this->auth = $auth;
58 27
		$this->block = $block;
59 27
		$this->comments_table = $comments_table;
60 27
		$this->images_table = $images_table;
61 27
	}
62
63
	/**
64
	 * Is the user allowed to comment?
65
	 * Following statements must be true:
66
	 *    - User must have permissions.
67
	 *    - User is neither owner of the image nor guest.
68
	 *    - Album and image are not locked.
69
	 *
70
	 * @param $album_data
71
	 * @param $image_data
72
	 * @return bool
73
	 */
74 3
	public function is_allowed($album_data, $image_data)
75
	{
76 3
		return $this->config->get('allow_comments') && (!$this->config->get('comment_user_control') || $image_data['image_allow_comments']) &&
77 2
			($this->auth->acl_check('m_status', $album_data['album_id'], $album_data['album_user_id']) ||
78 3
			(($image_data['image_status'] == $this->block->get_image_status_approved()) && ($album_data['album_status'] != $this->block->get_album_status_locked())));
79
	}
80
81
	/**
82
	 * Is the user able to comment?
83
	 * Following statements must be true:
84
	 *    - User must be allowed to rate
85
	 *    - If the image is in a contest, it must be finished
86
	 *
87
	 * @param $album_data
88
	 * @param $image_data
89
	 * @return bool
90
	 */
91 1
	public function is_able($album_data, $image_data)
92
	{
93 1
		return $this->is_allowed($album_data, $image_data); //&& phpbb_ext_gallery_core_contest::is_step('comment', $album_data);
94
	}
95
96
	/**
97
	 * Add a comment
98
	 *
99
	 * @param        $data
100
	 * @param string $comment_username
101
	 * @return int|void
102
	 */
103 2
	public function add($data, $comment_username = '')
104
	{
105 2
		if (!isset($data['comment_image_id']) || !isset($data['comment']))
106
		{
107 1
			return;
108
		}
109
110
		$data = $data + array(
111 1
			'comment_user_id'		=> $this->user->data['user_id'],
112 1
			'comment_username'		=> ($this->user->data['user_id'] != ANONYMOUS) ? $this->user->data['username'] : $comment_username,
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
113 1
			'comment_user_colour'	=> $this->user->data['user_colour'],
114 1
			'comment_user_ip'		=> $this->user->ip,
115 1
			'comment_time'			=> time(),
116
		);
117
118 1
		$this->db->sql_query('INSERT INTO ' .$this->comments_table .' ' . $this->db->sql_build_array('INSERT', $data));
119 1
		$newest_comment_id = (int) $this->db->sql_nextid();
120 1
		$this->config->inc('num_comments', 1);
121
122 1
		$sql = 'UPDATE ' . $this->images_table . ' 
123
			SET image_comments = image_comments + 1,
124 1
				image_last_comment = ' . (int) $newest_comment_id . '
125 1
			WHERE image_id = ' . (int) $data['comment_image_id'];
126 1
		$this->db->sql_query($sql);
127
128 1
		return $newest_comment_id;
129
	}
130
131
	/**
132
	 * Edit comment
133
	 * @param $comment_id
134
	 * @param $data
135
	 * @return bool|void
136
	 */
137 2
	public function edit($comment_id, $data)
138
	{
139 2
		if (!isset($data['comment']))
140
		{
141 1
			return;
142
		}
143
144
		$data = $data + array(
145 1
			'comment_edit_time'		=> time(),
146 1
			'comment_edit_user_id'	=> $this->user->data['user_id'],
147
		);
148
149 1
		$sql = 'UPDATE ' . $this->comments_table . '
150 1
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
151 1
			WHERE comment_id = ' . (int) $comment_id;
152 1
		$this->db->sql_query($sql);
153
154 1
		return true;
155
	}
156
157
	/**
158
	 * Sync last comment information
159
	 * @param bool $image_ids
160
	 */
161 2
	public function sync_image_comments($image_ids = false)
162
	{
163 2
		$sql_where = $sql_where_image = '';
164 2
		$resync = array();
165 2
		if ($image_ids != false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
166
		{
167 2
			$image_ids = self::cast_mixed_int2array($image_ids);
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\comment::cast_mixed_int2array() is not static, but was called statically. ( Ignorable by Annotation )

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

167
			/** @scrutinizer ignore-call */ 
168
   $image_ids = self::cast_mixed_int2array($image_ids);
Loading history...
168 2
			$sql_where = 'WHERE ' . $this->db->sql_in_set('comment_image_id', $image_ids);
169 2
			$sql_where_image = 'WHERE ' . $this->db->sql_in_set('image_id', $image_ids);
170
		}
171
172
		$sql = 'SELECT comment_image_id, COUNT(comment_id) AS num_comments, MAX(comment_id) AS last_comment
173 2
			FROM ' . $this->comments_table . ' 
174 2
			' . $sql_where . '
175
			GROUP BY comment_image_id, comment_id
176
			ORDER BY comment_id DESC';
177 2
		$result = $this->db->sql_query($sql);
178 2
		while ($row = $this->db->sql_fetchrow($result))
179
		{
180 1
			$resync[$row['comment_image_id']] = array(
181 1
				'last_comment'	=> $row['last_comment'],
182 1
				'num_comments'	=> $row['num_comments'],
183
			);
184
		}
185 2
		$this->db->sql_freeresult($result);
186
187 2
		$sql = 'UPDATE ' . $this->images_table . ' 
188
			SET image_last_comment = 0,
189
				image_comments = 0
190 2
			' . $sql_where_image;
191 2
		$this->db->sql_query($sql);
192
193 2
		if (!empty($resync))
194
		{
195 1
			foreach ($resync as $image_id => $data)
196
			{
197 1
				$sql = 'UPDATE ' . $this->images_table . ' 
198 1
					SET image_last_comment = ' . (int) $data['last_comment'] . ',
199 1
						image_comments = ' . (int) $data['num_comments'] . '
200 1
					WHERE image_id = ' . (int) $image_id;
201 1
				$this->db->sql_query($sql);
202
			}
203
		}
204 2
	}
205
206
	/**
207
	* Delete comments
208
	*
209
	* @param	mixed	$comment_ids	Array or integer with comment_id we delete.
210
	*/
211 1
	public function delete_comments($comment_ids)
212
	{
213 1
		$comment_ids = $this->cast_mixed_int2array($comment_ids);
214
215
		$sql = 'SELECT comment_image_id, COUNT(comment_id) AS num_comments
216 1
			FROM ' . $this->comments_table . '
217 1
			WHERE ' . $this->db->sql_in_set('comment_id', $comment_ids) . '
218
			GROUP BY comment_image_id';
219 1
		$result = $this->db->sql_query($sql);
220
221 1
		$image_ids = array();
222 1
		$total_comments = 0;
223 1
		while ($row = $this->db->sql_fetchrow($result))
224
		{
225 1
			$image_ids[] = (int) $row['comment_image_id'];
226 1
			$total_comments += $row['num_comments'];
227
		}
228 1
		$this->db->sql_freeresult($result);
229
230 1
		$sql = 'DELETE FROM ' . $this->comments_table . '
231 1
			WHERE ' . $this->db->sql_in_set('comment_id', $comment_ids);
232 1
		$this->db->sql_query($sql);
233
234 1
		$this->sync_image_comments($image_ids);
0 ignored issues
show
Bug introduced by
$image_ids of type array|integer[] is incompatible with the type boolean expected by parameter $image_ids of phpbbgallery\core\comment::sync_image_comments(). ( Ignorable by Annotation )

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

234
		$this->sync_image_comments(/** @scrutinizer ignore-type */ $image_ids);
Loading history...
235
236 1
		$this->config->dec('num_comments', $total_comments);
237 1
	}
238
239
	/**
240
	* Delete comments for given image_ids
241
	*
242
	* @param	mixed	$image_ids		Array or integer with image_id where we delete the comments.
243
	* @param	bool	$reset_stats	Shall we also reset the statistics? We can save that query, when the images are deleted anyway.
244
	*/
245 2
	public function delete_images($image_ids, $reset_stats = false)
246
	{
247 2
		$image_ids = $this->cast_mixed_int2array($image_ids);
248
249 2
		$sql = 'DELETE FROM ' . $this->comments_table . '
250 2
			WHERE ' . $this->db->sql_in_set('comment_image_id', $image_ids);
251 2
		$this->db->sql_query($sql);
252
253 2
		if ($reset_stats)
254
		{
255 1
			$sql = 'UPDATE ' . $this->images_table . '
256
				SET image_comments = 0
257
					image_last_comment = 0
258 1
				WHERE ' . $this->db->sql_in_set('image_id', $image_ids);
259 1
			$this->db->sql_query($sql);
260
		}
261 2
	}
262
263 5
	public function cast_mixed_int2array($ids)
264
	{
265 5
		if (is_array($ids))
266
		{
267 5
			return array_map('intval', $ids);
268
		}
269
		else
270
		{
271 1
			return array((int) $ids);
272
		}
273
	}
274
}
275