PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wwwroot/phpbb/phpbb/notification/type/quote.php

https://github.com/spring/spring-website
PHP | 190 lines | 93 code | 26 blank | 71 comment | 4 complexity | ea82c4f0e0cc4df7c48df29c28313945 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0, BSD-3-Clause
  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. namespace phpbb\notification\type;
  14. /**
  15. * Post quoting notifications class
  16. * This class handles notifying users when they have been quoted in a post
  17. */
  18. class quote extends \phpbb\notification\type\post
  19. {
  20. /**
  21. * Get notification type name
  22. *
  23. * @return string
  24. */
  25. public function get_type()
  26. {
  27. return 'notification.type.quote';
  28. }
  29. /**
  30. * regular expression to match to find usernames
  31. *
  32. * @var string
  33. */
  34. protected static $regular_expression_match = '#\[quote=&quot;(.+?)&quot;#';
  35. /**
  36. * Language key used to output the text
  37. *
  38. * @var string
  39. */
  40. protected $language_key = 'NOTIFICATION_QUOTE';
  41. /**
  42. * Notification option data (for outputting to the user)
  43. *
  44. * @var bool|array False if the service should use it's default data
  45. * Array of data (including keys 'id', 'lang', and 'group')
  46. */
  47. public static $notification_option = array(
  48. 'lang' => 'NOTIFICATION_TYPE_QUOTE',
  49. 'group' => 'NOTIFICATION_GROUP_POSTING',
  50. );
  51. /**
  52. * Is available
  53. */
  54. public function is_available()
  55. {
  56. return true;
  57. }
  58. /**
  59. * Find the users who want to receive notifications
  60. *
  61. * @param array $post Data from submit_post
  62. * @param array $options Options for finding users for notification
  63. *
  64. * @return array
  65. */
  66. public function find_users_for_notification($post, $options = array())
  67. {
  68. $options = array_merge(array(
  69. 'ignore_users' => array(),
  70. ), $options);
  71. $usernames = false;
  72. preg_match_all(self::$regular_expression_match, $post['post_text'], $usernames);
  73. if (empty($usernames[1]))
  74. {
  75. return array();
  76. }
  77. $usernames[1] = array_unique($usernames[1]);
  78. $usernames = array_map('utf8_clean_string', $usernames[1]);
  79. $users = array();
  80. $sql = 'SELECT user_id
  81. FROM ' . USERS_TABLE . '
  82. WHERE ' . $this->db->sql_in_set('username_clean', $usernames) . '
  83. AND user_id <> ' . (int) $post['poster_id'];
  84. $result = $this->db->sql_query($sql);
  85. while ($row = $this->db->sql_fetchrow($result))
  86. {
  87. $users[] = (int) $row['user_id'];
  88. }
  89. $this->db->sql_freeresult($result);
  90. return $this->get_authorised_recipients($users, $post['forum_id'], $options, true);
  91. }
  92. /**
  93. * Update a notification
  94. *
  95. * @param array $post Data specific for this type that will be updated
  96. */
  97. public function update_notifications($post)
  98. {
  99. $old_notifications = array();
  100. $sql = 'SELECT n.user_id
  101. FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt
  102. WHERE n.notification_type_id = ' . (int) $this->notification_type_id . '
  103. AND n.item_id = ' . self::get_item_id($post) . '
  104. AND nt.notification_type_id = n.notification_type_id
  105. AND nt.notification_type_enabled = 1';
  106. $result = $this->db->sql_query($sql);
  107. while ($row = $this->db->sql_fetchrow($result))
  108. {
  109. $old_notifications[] = $row['user_id'];
  110. }
  111. $this->db->sql_freeresult($result);
  112. // Find the new users to notify
  113. $notifications = $this->find_users_for_notification($post);
  114. // Find the notifications we must delete
  115. $remove_notifications = array_diff($old_notifications, array_keys($notifications));
  116. // Find the notifications we must add
  117. $add_notifications = array();
  118. foreach (array_diff(array_keys($notifications), $old_notifications) as $user_id)
  119. {
  120. $add_notifications[$user_id] = $notifications[$user_id];
  121. }
  122. // Add the necessary notifications
  123. $this->notification_manager->add_notifications_for_users($this->get_type(), $post, $add_notifications);
  124. // Remove the necessary notifications
  125. if (!empty($remove_notifications))
  126. {
  127. $sql = 'DELETE FROM ' . $this->notifications_table . '
  128. WHERE notification_type_id = ' . (int) $this->notification_type_id . '
  129. AND item_id = ' . self::get_item_id($post) . '
  130. AND ' . $this->db->sql_in_set('user_id', $remove_notifications);
  131. $this->db->sql_query($sql);
  132. }
  133. // return true to continue with the update code in the notifications service (this will update the rest of the notifications)
  134. return true;
  135. }
  136. /**
  137. * {inheritDoc}
  138. */
  139. public function get_redirect_url()
  140. {
  141. return $this->get_url();
  142. }
  143. /**
  144. * Get email template
  145. *
  146. * @return string|bool
  147. */
  148. public function get_email_template()
  149. {
  150. return 'quote';
  151. }
  152. /**
  153. * Get email template variables
  154. *
  155. * @return array
  156. */
  157. public function get_email_template_variables()
  158. {
  159. $user_data = $this->user_loader->get_user($this->get_data('poster_id'));
  160. return array_merge(parent::get_email_template_variables(), array(
  161. 'AUTHOR_NAME' => htmlspecialchars_decode($user_data['username']),
  162. ));
  163. }
  164. }