aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/content_visibility.php
blob: a1b7c16c2d7e8da1cce5212f3e90f6e1a20395c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
/**
*
* @package phpbb
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* phpbb_visibility
* Handle fetching and setting the visibility for topics and posts
* @package phpbb
*/
class phpbb_content_visibility
{
	/**
	* Can the current logged-in user soft-delete posts?
	*
	* @param $forum_id		int		Forum ID whose permissions to check
	* @param $poster_id		int		Poster ID of the post in question
	* @param $post_locked	bool	Is the post locked?
	* @return bool
	*/
	static function can_soft_delete($forum_id, $poster_id, $post_locked)
	{
		global $auth, $user;

		if ($auth->acl_get('m_softdelete', $forum_id))
		{
			return true;
		}
		else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked)
		{
			return true;
		}

		return false;
	}

	/**
	* Get the topics post count or the forums post/topic count based on permissions
	*
	* @param $mode			string	One of topic_posts, forum_posts or forum_topics
	* @param $data			array	Array with the topic/forum data to calculate from
	* @param $forum_id		int		The forum id is used for permission checks
	* @return int	Number of posts/topics the user can see in the topic/forum
	*/
	static public function get_count($mode, $data, $forum_id)
	{
		global $auth;

		if (!$auth->acl_get('m_approve', $forum_id))
		{
			return (int) $data[$mode];
		}

		return (int) $data[$mode] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted'];
	}

	/**
	* Create topic/post visibility SQL for a given forum ID
	*
	* Note: Read permissions are not checked.
	*
	* @param $mode			string	Either "topic" or "post"
	* @param $forum_id		int		The forum id is used for permission checks
	* @param $table_alias	string	Table alias to prefix in SQL queries
	* @return string	The appropriate combination SQL logic for topic/post_visibility
	*/
	static public function get_visibility_sql($mode, $forum_id, $table_alias = '')
	{
		global $auth;

		if ($auth->acl_get('m_approve', $forum_id))
		{
			return '1 = 1';
		}

		return $table_alias . $mode . '_visibility = ' . ITEM_APPROVED;
	}

	/**
	* Create topic/post visibility SQL for a set of forums
	*
	* Note: Read permissions are not checked. Forums without read permissions
	*		should not be in $forum_ids
	*
	* @param $mode			string	Either "topic" or "post"
	* @param $forum_ids		array	Array of forum ids which the posts/topics are limited to
	* @param $table_alias	string	Table alias to prefix in SQL queries
	* @return string	The appropriate combination SQL logic for topic/post_visibility
	*/
	static public function get_forums_visibility_sql($mode, $forum_ids = array(), $table_alias = '')
	{
		global $auth, $db;

		$where_sql = '(';

		$approve_forums = array_intersect($forum_ids, array_keys($auth->acl_getf('m_approve', true)));

		if (sizeof($approve_forums))
		{
			// Remove moderator forums from the rest
			$forum_ids = array_diff($forum_ids, $approve_forums);

			if (!sizeof($forum_ids))
			{
				// The user can see all posts/topics in all specified forums
				return $db->sql_in_set($table_alias . 'forum_id', $approve_forums);
			}
			else
			{
				// Moderator can view all posts/topics in some forums
				$where_sql .= $db->sql_in_set($table_alias . 'forum_id', $approve_forums) . ' OR ';
			}
		}
		else
		{
			// The user is just a normal user
			return "$table_alias{$mode}_visibility = " . ITEM_APPROVED . '
				AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids, false, true);
		}

		$where_sql .= "($table_alias{$mode}_visibility = " . ITEM_APPROVED . '
			AND ' . $db->sql_in_set($table_alias . 'forum_id', $forum_ids) . '))';

		return $where_sql;
	}

	/**
	* Create topic/post visibility SQL for all forums on the board
	*
	* Note: Read permissions are not checked. Forums without read permissions
	*		should be in $exclude_forum_ids
	*
	* @param $mode				string	Either "topic" or "post"
	* @param $exclude_forum_ids	array	Array of forum ids which are excluded
	* @param $table_alias		string	Table alias to prefix in SQL queries
	* @return string	The appropriate combination SQL logic for topic/post_visibility
	*/
	static public function get_global_visibility_sql($mode, $exclude_forum_ids = array(), $table_alias = '')
	{
		global $auth, $db;

		$where_sqls = array();

		$approve_forums = array_diff(array_keys($auth->acl_getf('m_approve', true)), $exclude_forum_ids);

		if (sizeof($exclude_forum_ids))
		{
			$where_sqls[] = '(' . $db->sql_in_set($table_alias . 'forum_id', $exclude_forum_ids, true) . "
				AND $table_alias{$mode}_visibility = " . ITEM_APPROVED . ')';
		}
		else
		{
			$where_sqls[] = "$table_alias{$mode}_visibility = " . ITEM_APPROVED;
		}

		if (sizeof($approve_forums))
		{
			$where_sqls[] = $db->sql_in_set($table_alias . 'forum_id', $approve_forums);
			return '(' . implode(' OR ', $where_sqls) . ')';
		}

		// There is only one element, so we just return that one
		return $where_sqls[0];
	}

	/**
	* Change visibility status of one post or all posts of a topic
	*
	* @param $visibility	int		Element of {ITEM_APPROVED, ITEM_DELETED}
	* @param $post_id		mixed	Post ID or array of post IDs to act on,
	*								if it is empty, all posts of topic_id will be modified
	* @param $topic_id		int		Topic where $post_id is found
	* @param $forum_id		int		Forum where $topic_id is found
	* @param $user_id		int		User performing the action
	* @param $time			int		Timestamp when the action is performed
	* @param $reason		string	Reason why the visibilty was changed.
	* @param $is_starter	bool	Is this the first post of the topic changed?
	* @param $is_latest		bool	Is this the last post of the topic changed?
	* @param $limit_visibility	mixed	Limit updating per topic_id to a certain visibility
	* @param $limit_delete_time	mixed	Limit updating per topic_id to a certain deletion time
	* @return array		Changed post data, empty array if an error occured.
	*/
	static public function set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $limit_visibility = false, $limit_delete_time = false)
	{
		global $db;

		if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED)))
		{
			return array();
		}

		if ($post_id)
		{
			if (is_array($post_id))
			{
				$where_sql = $db->sql_in_set('post_id', array_map('intval', $post_id));
			}
			else
			{
				$where_sql = 'post_id = ' . (int) $post_id;
			}
			$where_sql .= ' AND topic_id = ' . (int) $topic_id;
		}
		else
		{
			$where_sql = 'topic_id = ' . (int) $topic_id;

			// Limit the posts to a certain visibility and deletion time
			// This allows us to only restore posts, that were approved
			// when the topic got soft deleted. So previous soft deleted
			// and unapproved posts are still soft deleted/unapproved
			if ($limit_visibility !== false)
			{
				$where_sql .= ' AND post_visibility = ' . (int) $limit_visibility;
			}

			if ($limit_delete_time !== false)
			{
				$where_sql .= ' AND post_delete_time = ' . (int) $limit_delete_time;
			}
		}

		$sql = 'SELECT poster_id, post_id, post_postcount, post_visibility
			FROM ' . POSTS_TABLE . '
			WHERE ' . $where_sql;
		$result = $db->sql_query($sql);

		$post_ids = $poster_postcounts = $postcounts = $postcount_visibility = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$post_ids[] = (int) $row['post_id'];

			if ($row['post_visibility'] != $visibility)
			{
				if ($row['post_postcount'] && !isset($poster_postcounts[$row['poster_id']]))
				{
					$poster_postcounts[$row['poster_id']] = 1;
				}
				else if ($row['post_postcount'])
				{
					$poster_postcounts[$row['poster_id']]++;
				}

				if (!isset($postcount_visibility[$row['post_visibility']]))
				{
					$postcount_visibility[$row['post_visibility']] = 1;
				}
				else
				{
					$postcount_visibility[$row['post_visibility']]++;
				}
			}
		}
		$db->sql_freeresult($result);

		if (empty($post_ids))
		{
			return array();
		}

		$data = array(
			'post_visibility'		=> (int) $visibility,
			'post_delete_user'		=> (int) $user_id,
			'post_delete_time'		=> ((int) $time) ?: time(),
			'post_delete_reason'	=> truncate_string($reason, 255, 255, false),
		);

		$sql = 'UPDATE ' . POSTS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $data) . '
			WHERE ' . $db->sql_in_set('post_id', $post_ids);
		$db->sql_query($sql);

		// Group the authors by post count, to reduce the number of queries
		foreach ($poster_postcounts as $poster_id => $num_posts)
		{
			$postcounts[$num_posts][] = $poster_id;
		}

		// Update users postcounts
		foreach ($postcounts as $num_posts => $poster_ids)
		{
			if ($visibility == ITEM_DELETED)
			{
				$sql = 'UPDATE ' . USERS_TABLE . '
					SET user_posts = 0
					WHERE ' . $db->sql_in_set('user_id', $poster_ids) . '
						AND user_posts < ' . $num_posts;
				$db->sql_query($sql);

				$sql = 'UPDATE ' . USERS_TABLE . '
					SET user_posts = user_posts - ' . $num_posts . '
					WHERE ' . $db->sql_in_set('user_id', $poster_ids) . '
						AND user_posts >= ' . $num_posts;
				$db->sql_query($sql);
			}
			else
			{
				$sql = 'UPDATE ' . USERS_TABLE . '
					SET user_posts = user_posts + ' . $num_posts . '
					WHERE ' . $db->sql_in_set('user_id', $poster_ids) . '
						AND user_posts >= ' . $num_posts;
				$db->sql_query($sql);
			}
		}

		$update_topic_postcount = true;

		// Sync the first/last topic information if needed
		if (!$is_starter && $is_latest)
		{
			// update_post_information can only update the last post info ...
			if ($topic_id)
			{
				update_post_information('topic', $topic_id, false);
			}
			if ($forum_id)
			{
				update_post_information('forum', $forum_id, false);
			}
		}
		else if ($is_starter && $topic_id)
		{
			if (!function_exists('sync'))
			{
				global $phpEx, $phpbb_root_path;
				include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
			}

			// ... so we need to use sync, if the first post is changed.
			// The forum is resynced recursive by sync() itself.
			sync('topic', 'topic_id', $topic_id, true);

			// sync recalculates the topic replies and forum posts by itself, so we don't do that.
			$update_topic_postcount = false;
		}

		// Update the topic's reply count and the forum's post count
		if ($update_topic_postcount)
		{
			$cur_posts = $cur_unapproved_posts = $cur_softdeleted_posts = 0;
			foreach ($postcount_visibility as $post_visibility => $visibility_posts)
			{
				// We need to substract the posts from the counters ...
				if ($post_visibility == ITEM_APPROVED)
				{
					$cur_posts += $visibility_posts;
				}
				else if ($post_visibility == ITEM_UNAPPROVED)
				{
					$cur_unapproved_posts += $visibility_posts;
				}
				else if ($post_visibility == ITEM_DELETED)
				{
					$cur_softdeleted_posts += $visibility_posts;
				}
			}

			$sql_ary = array();
			if ($visibility == ITEM_DELETED)
			{
				if ($cur_posts)
				{
					$sql_ary['posts'] = ' - ' . $cur_posts;
				}
				if ($cur_unapproved_posts)
				{
					$sql_ary['posts_unapproved'] = ' - ' . $cur_unapproved_posts;
				}
				if ($cur_posts + $cur_unapproved_posts)
				{
					$sql_ary['posts_softdeleted'] = ' + ' . ($cur_posts + $cur_unapproved_posts);
				}
			}
			else
			{
				if ($cur_unapproved_posts)
				{
					$sql_ary['posts_unapproved'] = ' - ' . $cur_unapproved_posts;
				}
				if ($cur_softdeleted_posts)
				{
					$sql_ary['posts_softdeleted'] = ' - ' . $cur_softdeleted_posts;
				}
				if ($cur_softdeleted_posts + $cur_unapproved_posts)
				{
					$sql_ary['posts'] = ' + ' . ($cur_softdeleted_posts + $cur_unapproved_posts);
				}
			}

			if (sizeof($sql_ary))
			{
				$topic_sql = $forum_sql = array();

				foreach ($sql_ary as $field => $value_change)
				{
					$topic_sql[] = 'topic_' . $field . ' = topic_' . $field . $value_change;
					$forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change;
				}

				// Update the number for replies and posts
				$sql = 'UPDATE ' . TOPICS_TABLE . '
					SET ' . implode(', ', $topic_sql) . '
					WHERE topic_id = ' . $topic_id;
				$db->sql_query($sql);

				$sql = 'UPDATE ' . FORUMS_TABLE . '
					SET ' . implode(', ', $forum_sql) . '
					WHERE forum_id = ' . $forum_id;
				$db->sql_query($sql);
			}
		}

		return $data;
	}

	/**
	* Set topic visibility
	*
	* Allows approving (which is akin to undeleting/restore) or soft deleting an entire topic.
	* Calls set_post_visibility as needed.
	*
	* Note: By default, when a soft deleted topic is restored. Only posts that
	*		were approved at the time of soft deleting, are being restored.
	*		Same applies to soft deleting. Only approved posts will be marked
	*		as soft deleted.
	*		If you want to update all posts, use the force option.
	*
	* @param $visibility	int		Element of {ITEM_APPROVED, ITEM_DELETED}
	* @param $topic_id		mixed	Topic ID to act on
	* @param $forum_id		int		Forum where $topic_id is found
	* @param $user_id		int		User performing the action
	* @param $time			int		Timestamp when the action is performed
	* @param $reason		string	Reason why the visibilty was changed.
	* @param $force_update_all	bool	Force to update all posts within the topic
	* @return array		Changed topic data, empty array if an error occured.
	*/
	static public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false)
	{
		global $db;

		if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED)))
		{
			return array();
		}

		if (!$force_update_all)
		{
			$sql = 'SELECT topic_visibility, topic_delete_time
				FROM ' . TOPICS_TABLE . '
				WHERE topic_id = ' . (int) $topic_id;
			$result = $db->sql_query($sql);
			$original_topic_data = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);

			if (!$original_topic_data)
			{
				// The topic does not exist...
				return array();
			}
		}

		// Note, we do not set a reason for the posts, just for the topic
		$data = array(
			'topic_visibility'		=> (int) $visibility,
			'topic_delete_user'		=> (int) $user_id,
			'topic_delete_time'		=> ((int) $time) ?: time(),
			'topic_delete_reason'	=> truncate_string($reason, 255, 255, false),
		);

		$sql = 'UPDATE ' . TOPICS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $data) . '
			WHERE topic_id = ' . (int) $topic_id;
		$db->sql_query($sql);

		if (!$db->sql_affectedrows())
		{
			return array();
		}

		if (!$force_update_all && $original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED)
		{
			// If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion.
			self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility'], $original_topic_data['topic_delete_time']);
		}
		else if (!$force_update_all && $original_topic_data['topic_visibility'] == ITEM_APPROVED && $visibility == ITEM_DELETED)
		{
			// If we're soft deleting a topic we only approved posts are soft deleted.
			self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility']);
		}
		else
		{
			self::set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true);
		}

		return $data;
	}

	/**
	* Add post to topic and forum statistics
	*
	* @param $data			array	Contains information from the topics table about given topic
	* @param $sql_data		array	Populated with the SQL changes, may be empty at call time
	* @return void
	*/
	static public function add_post_to_statistic($data, &$sql_data)
	{
		$sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_replies = topic_replies + 1';

		$sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts + 1';

		if ($data['post_postcount'])
		{
			$sql_data[USERS_TABLE] = (($sql_data[USERS_TABLE]) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts + 1';
		}

		set_config_count('num_posts', 1, true);
	}

	/**
	* Remove post from topic and forum statistics
	*
	* @param $data			array	Contains information from the topics table about given topic
	* @param $sql_data		array	Populated with the SQL changes, may be empty at call time
	* @return void
	*/
	static public function remove_post_from_statistic($data, &$sql_data)
	{
		$sql_data[TOPICS_TABLE] = ((!empty($sql_data[TOPICS_TABLE])) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_posts = topic_posts - 1';
		$sql_data[FORUMS_TABLE] = ((!empty($sql_data[FORUMS_TABLE])) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_posts = forum_posts - 1';

		if ($data['post_postcount'])
		{
			$sql_data[USERS_TABLE] = ((!empty($sql_data[USERS_TABLE])) ? $sql_data[USERS_TABLE] . ', ' : '') . 'user_posts = user_posts - 1';
		}

		set_config_count('num_posts', -1, true);
	}

	/**
	* Remove topic from forum statistics
	*
	* @param $topic_id		int		The topic to act on
	* @param $forum_id		int		Forum where the topic is found
	* @param $topic_row		array	Contains information from the topic, may be empty at call time
	* @param $sql_data		array	Populated with the SQL changes, may be empty at call time
	* @return void
	*/
	static public function remove_topic_from_statistic($topic_id, $forum_id, &$topic_row, &$sql_data)
	{
		global $db;

		// Do we need to grab some topic informations?
		if (!sizeof($topic_row))
		{
			$sql = 'SELECT topic_type, topic_posts, topic_posts_unapproved, topic_posts_softdeleted, topic_visibility
				FROM ' . TOPICS_TABLE . '
				WHERE topic_id = ' . $topic_id;
			$result = $db->sql_query($sql);
			$topic_row = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);
		}

		// If this is an edited topic or the first post the topic gets completely disapproved later on...
		$sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . 'forum_topics = forum_topics - 1';
		$sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . $topic_row['topic_posts'];
		$sql_data[FORUMS_TABLE] .= ', forum_posts_unapproved = forum_posts_unapproved - ' . $topic_row['topic_posts_unapproved'];
		$sql_data[FORUMS_TABLE] .= ', forum_posts_softdeleted = forum_posts_softdeleted - ' . $topic_row['topic_posts_softdeleted'];

		set_config_count('num_topics', -1, true);
		set_config_count('num_posts', $topic_row['topic_posts'] * (-1), true);

		// Get user post count information
		$sql = 'SELECT poster_id, COUNT(post_id) AS num_posts
			FROM ' . POSTS_TABLE . '
			WHERE topic_id = ' . $topic_id . '
				AND post_postcount = 1
				AND post_visibility = ' . ITEM_APPROVED . '
			GROUP BY poster_id';
		$result = $db->sql_query($sql);

		$postcounts = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$postcounts[(int) $row['num_posts']][] = (int) $row['poster_id'];
		}
		$db->sql_freeresult($result);

		// Decrement users post count
		foreach ($postcounts as $num_posts => $poster_ids)
		{
			$sql = 'UPDATE ' . USERS_TABLE . '
				SET user_posts = 0
				WHERE user_posts < ' . $num_posts . '
					AND ' . $db->sql_in_set('user_id', $poster_ids);
			$db->sql_query($sql);

			$sql = 'UPDATE ' . USERS_TABLE . '
				SET user_posts = user_posts - ' . $num_posts . '
				WHERE user_posts >= ' . $num_posts . '
					AND ' . $db->sql_in_set('user_id', $poster_ids);
			$db->sql_query($sql);
		}
	}
}