aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/notification/type/base.php
blob: e4600add11dd28fcae1e2f6c292be24ba6df6c59 (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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\notification\type;

/**
* Base notifications class
*/
abstract class base implements \phpbb\notification\type\type_interface
{
	/** @var \phpbb\notification\manager */
	protected $notification_manager;

	/** @var \phpbb\db\driver\driver_interface */
	protected $db;

	/** @var \phpbb\language\language */
	protected $language;

	/** @var \phpbb\user */
	protected $user;

	/** @var \phpbb\auth\auth */
	protected $auth;

	/** @var string */
	protected $phpbb_root_path;

	/** @var string */
	protected $php_ext;

	/** @var string */
	protected $user_notifications_table;

	/**
	* Notification option data (for outputting to the user)
	*
	* @var bool|array False if the service should use its default data
	* 					Array of data (including keys 'id', 'lang', and 'group')
	*/
	static public $notification_option = false;

	/**
	* The notification_type_id, set upon creation of the class
	* This is the notification_type_id from the notification_types table
	*
	* @var int
	*/
	protected $notification_type_id;

	/**
	* Identification data
	* notification_type_id	- ID of the item type (auto generated, from notification types table)
	* item_id				- ID of the item (e.g. post_id, msg_id)
	* item_parent_id		- Parent item id (ex: for topic => forum_id, for post => topic_id, etc)
	* user_id
	* notification_read
	* notification_time
	* notification_data (special serialized field that each notification type can use to store stuff)
	*
	* @var array $data Notification row from the database
	* 		This must be private, all interaction should use __get(), __set(), get_data(), set_data()
	*/
	private $data = array();

	/**
	 * Notification Type Base Constructor
	 *
	 * @param \phpbb\db\driver\driver_interface $db
	 * @param \phpbb\language\language          $language
	 * @param \phpbb\user                       $user
	 * @param \phpbb\auth\auth                  $auth
	 * @param string                            $phpbb_root_path
	 * @param string                            $php_ext
	 * @param string                            $user_notifications_table
	 */
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, \phpbb\user $user, \phpbb\auth\auth $auth, $phpbb_root_path, $php_ext, $user_notifications_table)
	{
		$this->db = $db;
		$this->language = $language;
		$this->user = $user;
		$this->auth = $auth;

		$this->phpbb_root_path = $phpbb_root_path;
		$this->php_ext = $php_ext;

		$this->user_notifications_table = $user_notifications_table;
	}

	/**
	* Set notification manager (required)
	*
	* @param \phpbb\notification\manager $notification_manager
	*/
	public function set_notification_manager(\phpbb\notification\manager $notification_manager)
	{
		$this->notification_manager = $notification_manager;

		$this->notification_type_id = $this->notification_manager->get_notification_type_id($this->get_type());
	}

	/**
	* Set initial data from the database
	*
	* @param array $data Row directly from the database
	*/
	public function set_initial_data($data = array())
	{
		// The row from the database (unless this is a new notification we're going to add)
		$this->data = $data;
		$this->data['notification_data'] = (isset($this->data['notification_data'])) ? unserialize($this->data['notification_data']) : array();
	}

	/**
	* Magic method to get data from this notification
	*
	* @param mixed $name
	* @return mixed
	*/
	public function __get($name)
	{
		return (!isset($this->data[$name])) ? null : $this->data[$name];
	}


	/**
	* Magic method to set data on this notification
	*
	* @param mixed $name
	* @param mixed $value
	*
	* @return null
	*/
	public function __set($name, $value)
	{
		$this->data[$name] = $value;
	}


	/**
	* Magic method to get a string of this notification
	*
	* Primarily for testing
	*
	* @return mixed
	*/
	public function __toString()
	{
		return (!empty($this->data)) ? var_export($this->data, true) : $this->get_type();
	}

	/**
	* Get special data (only important for the classes that extend this)
	*
	* @param string $name Name of the variable to get
	* @return mixed
	*/
	protected function get_data($name)
	{
		return ($name === false) ? $this->data['notification_data'] : ((isset($this->data['notification_data'][$name])) ? $this->data['notification_data'][$name] : null);
	}

	/**
	* Set special data (only important for the classes that extend this)
	*
	* @param string $name Name of the variable to set
	* @param mixed $value Value to set to the variable
	* @return mixed
	*/
	protected function set_data($name, $value)
	{
		$this->data['notification_data'][$name] = $value;
	}

	/**
	* {@inheritdoc}
	*/
	public function create_insert_array($type_data, $pre_create_data = array())
	{
		// Defaults
		$this->data = array_merge(array(
			'item_id'				=> static::get_item_id($type_data),
			'notification_type_id'	=> $this->notification_type_id,
			'item_parent_id'		=> static::get_item_parent_id($type_data),

			'notification_time'		=> time(),
			'notification_read'		=> false,

			'notification_data'		=> array(),
		), $this->data);
	}

	/**
	* {@inheritdoc}
	*/
	public function get_insert_array()
	{
		$data = $this->data;

		$data['notification_data'] = serialize($data['notification_data']);

		return $data;
	}

	/**
	* Function for preparing the data for update in an SQL query
	* (The service handles insertion)
	*
	* @param array $type_data Data unique to this notification type
	* @return array Array of data ready to be updated in the database
	*/
	public function create_update_array($type_data)
	{
		$this->create_insert_array($type_data);
		$data = $this->get_insert_array();

		// Unset data unique to each row
		unset(
			$data['notification_time'], // Also unsetting time, since it always tries to change the time to current (if you actually need to change the time, over-ride this function)
			$data['notification_id'],
			$data['notification_read'],
			$data['user_id']
		);

		return $data;
	}

	/**
	* Mark this item read
	*
	* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
	* @return string|null If $return is False, nothing will be returned, else the sql code to update this item
	*/
	public function mark_read($return = false)
	{
		return $this->mark(false, $return);
	}

	/**
	* Mark this item unread
	*
	* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
	* @return string|null If $return is False, nothing will be returned, else the sql code to update this item
	*/
	public function mark_unread($return = false)
	{
		return $this->mark(true, $return);
	}

	/**
	* {inheritDoc}
	*/
	public function get_redirect_url()
	{
		return $this->get_url();
	}

	/**
	* Prepare to output the notification to the template
	*
	* @return array Template variables
	*/
	public function prepare_for_display()
	{
		$mark_hash = generate_link_hash('mark_notification_read');

		if ($this->get_url())
		{
			$u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&amp;hash=' . $mark_hash);
		}
		else
		{
			$redirect = (($this->user->page['page_dir']) ? $this->user->page['page_dir'] . '/' : '') . $this->user->page['page_name'] . (($this->user->page['query_string']) ? '?' . $this->user->page['query_string'] : '');

			$u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&amp;hash=' . $mark_hash . '&amp;redirect=' . urlencode($redirect));
		}

		return array(
			'NOTIFICATION_ID'	=> $this->notification_id,
			'STYLING'			=> $this->get_style_class(),
			'AVATAR'			=> $this->get_avatar(),
			'FORMATTED_TITLE'	=> $this->get_title(),
			'REFERENCE'			=> $this->get_reference(),
			'FORUM'				=> $this->get_forum(),
			'REASON'			=> $this->get_reason(),
			'URL'				=> $this->get_url(),
			'TIME'	   			=> $this->user->format_date($this->notification_time),
			'UNREAD'			=> !$this->notification_read,
			'U_MARK_READ'		=> (!$this->notification_read) ? $u_mark_read : '',
		);
	}

	/**
	* -------------- Fall back functions -------------------
	*/

	/**
	* URL to unsubscribe to this notification (fall back)
	*
	* @param string|bool $method Method name to unsubscribe from (email|jabber|etc), False to unsubscribe from all notifications for this item
	* @return false
	*/
	public function get_unsubscribe_url($method = false)
	{
		return false;
	}

	/**
	* Get the CSS style class of the notification (fall back)
	*
	* @return string
	*/
	public function get_style_class()
	{
		return '';
	}

	/**
	* Get the user's avatar (fall back)
	*
	* @return string
	*/
	public function get_avatar()
	{
		return '';
	}

	/**
	* Get the reference of the notification (fall back)
	*
	* @return string
	*/
	public function get_reference()
	{
		return '';
	}

	/**
	* Get the forum of the notification reference (fall back)
	*
	* @return string
	*/
	public function get_forum()
	{
		return '';
	}

	/**
	* Get the reason for the notification (fall back)
	*
	* @return string
	*/
	public function get_reason()
	{
		return '';
	}

	/**
	* Get the special items to load (fall back)
	*
	* @return array
	*/
	public function get_load_special()
	{
		return array();
	}

	/**
	 * Load the special items (fall back)
	 *
	 * @param array $data
	 * @param array $notifications
	 */
	public function load_special($data, $notifications)
	{
		return;
	}

	/**
	* Is available (fall back)
	*
	* @return bool
	*/
	public function is_available()
	{
		return true;
	}

	/**
	 * Pre create insert array function (fall back)
	 *
	 * @param array $type_data
	 * @param array $notify_users
	 * @return array
	 */
	public function pre_create_insert_array($type_data, $notify_users)
	{
		return array();
	}

	/**
	* -------------- Helper functions -------------------
	*/

	/**
	 * Find the users who want to receive notifications (helper)
	 *
	 * @param array|bool $user_ids User IDs to check if they want to receive notifications
	 *                             (Bool False to check all users besides anonymous and bots (USER_IGNORE))
	 * @param array      $options
	 * @return array
	 */
	protected function check_user_notification_options($user_ids = false, $options = array())
	{
		$options = array_merge(array(
			'ignore_users'		=> array(),
			'item_type'			=> $this->get_type(),
			'item_id'			=> 0, // Global by default
		), $options);

		if ($user_ids === false)
		{
			$user_ids = array();

			$sql = 'SELECT user_id
				FROM ' . USERS_TABLE . '
				WHERE user_id <> ' . ANONYMOUS . '
					AND user_type <> ' . USER_IGNORE;
			$result = $this->db->sql_query($sql);
			while ($row = $this->db->sql_fetchrow($result))
			{
				$user_ids[] = $row['user_id'];
			}
			$this->db->sql_freeresult($result);
		}

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

		$rowset = $output = array();

		$sql = 'SELECT user_id, method, notify
			FROM ' . $this->user_notifications_table . '
			WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . "
				AND item_type = '" . $this->db->sql_escape($options['item_type']) . "'
				AND item_id = " . (int) $options['item_id'];
		$result = $this->db->sql_query($sql);

		while ($row = $this->db->sql_fetchrow($result))
		{
			if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']]))
			{
				continue;
			}

			if (!isset($rowset[$row['user_id']]))
			{
				$rowset[$row['user_id']] = array();
			}
			$rowset[$row['user_id']][$row['method']] = $row['notify'];

			if (!isset($output[$row['user_id']]))
			{
				$output[$row['user_id']] = array();
			}
			if ($row['notify'])
			{
				$output[$row['user_id']][] = $row['method'];
			}
		}

		$this->db->sql_freeresult($result);

		$default_methods = $this->notification_manager->get_default_methods();

		foreach ($user_ids as $user_id)
		{
			if (isset($options['ignore_users'][$user_id]))
			{
				continue;
			}
			if (!array_key_exists($user_id, $rowset))
			{
				// No rows at all for this user, use the default methods
				$output[$user_id] = $default_methods;
			}
			else
			{
				foreach ($default_methods as $default_method)
				{
					if (!array_key_exists($default_method, $rowset[$user_id]))
					{
						// No user preference for this type recorded, but it should be enabled by default.
						$output[$user_id][] = $default_method;
					}
				}
			}
		}

		return $output;
	}

	/**
	* Mark this item read/unread helper
	*
	* @param bool $unread Unread (True/False) (Default: False)
	* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
	* @return string|null If $return is False, nothing will be returned, else the sql code to update this item
	*/
	protected function mark($unread = true, $return = false)
	{
		$this->notification_read = (bool) !$unread;

		if ($return)
		{
			$where = array(
				'notification_type_id = ' . (int) $this->notification_type_id,
				'item_id = ' . (int) $this->item_id,
				'user_id = ' . (int) $this->user_id,
			);

			$where = implode(' AND ', $where);
			return $where;
		}
		else
		{
			$this->notification_manager->mark_notifications($this->get_type(), (int) $this->item_id, (int) $this->user_id, false, $this->notification_read);
		}

		return null;
	}

	/**
	 * Get a list of users that are authorised to receive notifications
	 *
	 * @param array $users Array of users that have subscribed to a notification
	 * @param int $forum_id Forum ID of the forum
	 * @param array $options Array of notification options
	 * @param bool $sort Whether the users array should be sorted. Default: false
	 * @return array Array of users that are authorised recipients
	 */
	protected function get_authorised_recipients($users, $forum_id, $options, $sort = false)
	{
		if (empty($users))
		{
			return array();
		}

		$users = array_unique($users);

		if ($sort)
		{
			sort($users);
		}

		$auth_read = $this->auth->acl_get_list($users, 'f_read', $forum_id);

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

		return $this->check_user_notification_options($auth_read[$forum_id]['f_read'], $options);
	}
}