[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/phpbb/notification/type/ -> base.php (source)

   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  
  14  namespace phpbb\notification\type;
  15  
  16  /**
  17  * Base notifications class
  18  */
  19  abstract class base implements \phpbb\notification\type\type_interface
  20  {
  21      /** @var \phpbb\notification\manager */
  22      protected $notification_manager;
  23  
  24      /** @var \phpbb\db\driver\driver_interface */
  25      protected $db;
  26  
  27      /** @var \phpbb\language\language */
  28      protected $language;
  29  
  30      /** @var \phpbb\user */
  31      protected $user;
  32  
  33      /** @var \phpbb\auth\auth */
  34      protected $auth;
  35  
  36      /** @var string */
  37      protected $phpbb_root_path;
  38  
  39      /** @var string */
  40      protected $php_ext;
  41  
  42      /** @var string */
  43      protected $user_notifications_table;
  44  
  45      /**
  46      * Notification option data (for outputting to the user)
  47      *
  48      * @var bool|array False if the service should use its default data
  49      *                     Array of data (including keys 'id', 'lang', and 'group')
  50      */
  51      static public $notification_option = false;
  52  
  53      /**
  54      * The notification_type_id, set upon creation of the class
  55      * This is the notification_type_id from the notification_types table
  56      *
  57      * @var int
  58      */
  59      protected $notification_type_id;
  60  
  61      /**
  62      * Identification data
  63      * notification_type_id    - ID of the item type (auto generated, from notification types table)
  64      * item_id                - ID of the item (e.g. post_id, msg_id)
  65      * item_parent_id        - Parent item id (ex: for topic => forum_id, for post => topic_id, etc)
  66      * user_id
  67      * notification_read
  68      * notification_time
  69      * notification_data (special serialized field that each notification type can use to store stuff)
  70      *
  71      * @var array $data Notification row from the database
  72      *         This must be private, all interaction should use __get(), __set(), get_data(), set_data()
  73      */
  74      private $data = array();
  75  
  76      /**
  77       * Notification Type Base Constructor
  78       *
  79       * @param \phpbb\db\driver\driver_interface $db
  80       * @param \phpbb\language\language          $language
  81       * @param \phpbb\user                       $user
  82       * @param \phpbb\auth\auth                  $auth
  83       * @param string                            $phpbb_root_path
  84       * @param string                            $php_ext
  85       * @param string                            $user_notifications_table
  86       */
  87  	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)
  88      {
  89          $this->db = $db;
  90          $this->language = $language;
  91          $this->user = $user;
  92          $this->auth = $auth;
  93  
  94          $this->phpbb_root_path = $phpbb_root_path;
  95          $this->php_ext = $php_ext;
  96  
  97          $this->user_notifications_table = $user_notifications_table;
  98      }
  99  
 100      /**
 101      * Set notification manager (required)
 102      *
 103      * @param \phpbb\notification\manager $notification_manager
 104      */
 105  	public function set_notification_manager(\phpbb\notification\manager $notification_manager)
 106      {
 107          $this->notification_manager = $notification_manager;
 108  
 109          $this->notification_type_id = $this->notification_manager->get_notification_type_id($this->get_type());
 110      }
 111  
 112      /**
 113      * Set initial data from the database
 114      *
 115      * @param array $data Row directly from the database
 116      */
 117  	public function set_initial_data($data = array())
 118      {
 119          // The row from the database (unless this is a new notification we're going to add)
 120          $this->data = $data;
 121          $this->data['notification_data'] = (isset($this->data['notification_data'])) ? unserialize($this->data['notification_data']) : array();
 122      }
 123  
 124      /**
 125      * Magic method to get data from this notification
 126      *
 127      * @param mixed $name
 128      * @return mixed
 129      */
 130  	public function __get($name)
 131      {
 132          return (!isset($this->data[$name])) ? null : $this->data[$name];
 133      }
 134  
 135  
 136      /**
 137      * Magic method to set data on this notification
 138      *
 139      * @param mixed $name
 140      * @param mixed $value
 141      *
 142      * @return null
 143      */
 144  	public function __set($name, $value)
 145      {
 146          $this->data[$name] = $value;
 147      }
 148  
 149  
 150      /**
 151      * Magic method to get a string of this notification
 152      *
 153      * Primarily for testing
 154      *
 155      * @return mixed
 156      */
 157  	public function __toString()
 158      {
 159          return (!empty($this->data)) ? var_export($this->data, true) : $this->get_type();
 160      }
 161  
 162      /**
 163      * Get special data (only important for the classes that extend this)
 164      *
 165      * @param string $name Name of the variable to get
 166      * @return mixed
 167      */
 168  	protected function get_data($name)
 169      {
 170          return ($name === false) ? $this->data['notification_data'] : ((isset($this->data['notification_data'][$name])) ? $this->data['notification_data'][$name] : null);
 171      }
 172  
 173      /**
 174      * Set special data (only important for the classes that extend this)
 175      *
 176      * @param string $name Name of the variable to set
 177      * @param mixed $value Value to set to the variable
 178      * @return mixed
 179      */
 180  	protected function set_data($name, $value)
 181      {
 182          $this->data['notification_data'][$name] = $value;
 183      }
 184  
 185      /**
 186      * {@inheritdoc}
 187      */
 188  	public function create_insert_array($type_data, $pre_create_data = array())
 189      {
 190          // Defaults
 191          $this->data = array_merge(array(
 192              'item_id'                => static::get_item_id($type_data),
 193              'notification_type_id'    => $this->notification_type_id,
 194              'item_parent_id'        => static::get_item_parent_id($type_data),
 195  
 196              'notification_time'        => time(),
 197              'notification_read'        => false,
 198  
 199              'notification_data'        => array(),
 200          ), $this->data);
 201      }
 202  
 203      /**
 204      * {@inheritdoc}
 205      */
 206  	public function get_insert_array()
 207      {
 208          $data = $this->data;
 209  
 210          $data['notification_data'] = serialize($data['notification_data']);
 211  
 212          return $data;
 213      }
 214  
 215      /**
 216      * Function for preparing the data for update in an SQL query
 217      * (The service handles insertion)
 218      *
 219      * @param array $type_data Data unique to this notification type
 220      * @return array Array of data ready to be updated in the database
 221      */
 222  	public function create_update_array($type_data)
 223      {
 224          $this->create_insert_array($type_data);
 225          $data = $this->get_insert_array();
 226  
 227          // Unset data unique to each row
 228          unset(
 229              $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)
 230              $data['notification_id'],
 231              $data['notification_read'],
 232              $data['user_id']
 233          );
 234  
 235          return $data;
 236      }
 237  
 238      /**
 239      * Mark this item read
 240      *
 241      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 242      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 243      */
 244  	public function mark_read($return = false)
 245      {
 246          return $this->mark(false, $return);
 247      }
 248  
 249      /**
 250      * Mark this item unread
 251      *
 252      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 253      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 254      */
 255  	public function mark_unread($return = false)
 256      {
 257          return $this->mark(true, $return);
 258      }
 259  
 260      /**
 261      * {inheritDoc}
 262      */
 263  	public function get_redirect_url()
 264      {
 265          return $this->get_url();
 266      }
 267  
 268      /**
 269      * Prepare to output the notification to the template
 270      *
 271      * @return array Template variables
 272      */
 273  	public function prepare_for_display()
 274      {
 275          $mark_hash = generate_link_hash('mark_notification_read');
 276  
 277          if ($this->get_url())
 278          {
 279              $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&amp;hash=' . $mark_hash);
 280          }
 281          else
 282          {
 283              $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'] : '');
 284  
 285              $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));
 286          }
 287  
 288          return array(
 289              'NOTIFICATION_ID'    => $this->notification_id,
 290              'STYLING'            => $this->get_style_class(),
 291              'AVATAR'            => $this->get_avatar(),
 292              'FORMATTED_TITLE'    => $this->get_title(),
 293              'REFERENCE'            => $this->get_reference(),
 294              'FORUM'                => $this->get_forum(),
 295              'REASON'            => $this->get_reason(),
 296              'URL'                => $this->get_url(),
 297              'TIME'                   => $this->user->format_date($this->notification_time),
 298              'UNREAD'            => !$this->notification_read,
 299              'U_MARK_READ'        => (!$this->notification_read) ? $u_mark_read : '',
 300          );
 301      }
 302  
 303      /**
 304      * -------------- Fall back functions -------------------
 305      */
 306  
 307      /**
 308      * URL to unsubscribe to this notification (fall back)
 309      *
 310      * @param string|bool $method Method name to unsubscribe from (email|jabber|etc), False to unsubscribe from all notifications for this item
 311      * @return false
 312      */
 313  	public function get_unsubscribe_url($method = false)
 314      {
 315          return false;
 316      }
 317  
 318      /**
 319      * Get the CSS style class of the notification (fall back)
 320      *
 321      * @return string
 322      */
 323  	public function get_style_class()
 324      {
 325          return '';
 326      }
 327  
 328      /**
 329      * Get the user's avatar (fall back)
 330      *
 331      * @return string
 332      */
 333  	public function get_avatar()
 334      {
 335          return '';
 336      }
 337  
 338      /**
 339      * Get the reference of the notification (fall back)
 340      *
 341      * @return string
 342      */
 343  	public function get_reference()
 344      {
 345          return '';
 346      }
 347  
 348      /**
 349      * Get the forum of the notification reference (fall back)
 350      *
 351      * @return string
 352      */
 353  	public function get_forum()
 354      {
 355          return '';
 356      }
 357  
 358      /**
 359      * Get the reason for the notification (fall back)
 360      *
 361      * @return string
 362      */
 363  	public function get_reason()
 364      {
 365          return '';
 366      }
 367  
 368      /**
 369      * Get the special items to load (fall back)
 370      *
 371      * @return array
 372      */
 373  	public function get_load_special()
 374      {
 375          return array();
 376      }
 377  
 378      /**
 379       * Load the special items (fall back)
 380       *
 381       * @param array $data
 382       * @param array $notifications
 383       */
 384  	public function load_special($data, $notifications)
 385      {
 386          return;
 387      }
 388  
 389      /**
 390      * Is available (fall back)
 391      *
 392      * @return bool
 393      */
 394  	public function is_available()
 395      {
 396          return true;
 397      }
 398  
 399      /**
 400       * Pre create insert array function (fall back)
 401       *
 402       * @param array $type_data
 403       * @param array $notify_users
 404       * @return array
 405       */
 406  	public function pre_create_insert_array($type_data, $notify_users)
 407      {
 408          return array();
 409      }
 410  
 411      /**
 412      * -------------- Helper functions -------------------
 413      */
 414  
 415      /**
 416       * Find the users who want to receive notifications (helper)
 417       *
 418       * @param array|bool $user_ids User IDs to check if they want to receive notifications
 419       *                             (Bool False to check all users besides anonymous and bots (USER_IGNORE))
 420       * @param array      $options
 421       * @return array
 422       */
 423  	protected function check_user_notification_options($user_ids = false, $options = array())
 424      {
 425          $options = array_merge(array(
 426              'ignore_users'        => array(),
 427              'item_type'            => $this->get_type(),
 428              'item_id'            => 0, // Global by default
 429          ), $options);
 430  
 431          if ($user_ids === false)
 432          {
 433              $user_ids = array();
 434  
 435              $sql = 'SELECT user_id
 436                  FROM ' . USERS_TABLE . '
 437                  WHERE user_id <> ' . ANONYMOUS . '
 438                      AND user_type <> ' . USER_IGNORE;
 439              $result = $this->db->sql_query($sql);
 440              while ($row = $this->db->sql_fetchrow($result))
 441              {
 442                  $user_ids[] = $row['user_id'];
 443              }
 444              $this->db->sql_freeresult($result);
 445          }
 446  
 447          if (empty($user_ids))
 448          {
 449              return array();
 450          }
 451  
 452          $rowset = $output = array();
 453  
 454          $sql = 'SELECT user_id, method, notify
 455              FROM ' . $this->user_notifications_table . '
 456              WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . "
 457                  AND item_type = '" . $this->db->sql_escape($options['item_type']) . "'
 458                  AND item_id = " . (int) $options['item_id'];
 459          $result = $this->db->sql_query($sql);
 460  
 461          while ($row = $this->db->sql_fetchrow($result))
 462          {
 463              if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']]))
 464              {
 465                  continue;
 466              }
 467  
 468              if (!isset($rowset[$row['user_id']]))
 469              {
 470                  $rowset[$row['user_id']] = array();
 471              }
 472              $rowset[$row['user_id']][$row['method']] = $row['notify'];
 473  
 474              if (!isset($output[$row['user_id']]))
 475              {
 476                  $output[$row['user_id']] = array();
 477              }
 478              if ($row['notify'])
 479              {
 480                  $output[$row['user_id']][] = $row['method'];
 481              }
 482          }
 483  
 484          $this->db->sql_freeresult($result);
 485  
 486          $default_methods = $this->notification_manager->get_default_methods();
 487  
 488          foreach ($user_ids as $user_id)
 489          {
 490              if (isset($options['ignore_users'][$user_id]))
 491              {
 492                  continue;
 493              }
 494              if (!array_key_exists($user_id, $rowset))
 495              {
 496                  // No rows at all for this user, use the default methods
 497                  $output[$user_id] = $default_methods;
 498              }
 499              else
 500              {
 501                  foreach ($default_methods as $default_method)
 502                  {
 503                      if (!array_key_exists($default_method, $rowset[$user_id]))
 504                      {
 505                          // No user preference for this type recorded, but it should be enabled by default.
 506                          $output[$user_id][] = $default_method;
 507                      }
 508                  }
 509              }
 510          }
 511  
 512          return $output;
 513      }
 514  
 515      /**
 516      * Mark this item read/unread helper
 517      *
 518      * @param bool $unread Unread (True/False) (Default: False)
 519      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 520      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 521      */
 522  	protected function mark($unread = true, $return = false)
 523      {
 524          $this->notification_read = (bool) !$unread;
 525  
 526          if ($return)
 527          {
 528              $where = array(
 529                  'notification_type_id = ' . (int) $this->notification_type_id,
 530                  'item_id = ' . (int) $this->item_id,
 531                  'user_id = ' . (int) $this->user_id,
 532              );
 533  
 534              $where = implode(' AND ', $where);
 535              return $where;
 536          }
 537          else
 538          {
 539              $this->notification_manager->mark_notifications($this->get_type(), (int) $this->item_id, (int) $this->user_id, false, $this->notification_read);
 540          }
 541  
 542          return null;
 543      }
 544  
 545      /**
 546       * Get a list of users that are authorised to receive notifications
 547       *
 548       * @param array $users Array of users that have subscribed to a notification
 549       * @param int $forum_id Forum ID of the forum
 550       * @param array $options Array of notification options
 551       * @param bool $sort Whether the users array should be sorted. Default: false
 552       * @return array Array of users that are authorised recipients
 553       */
 554  	protected function get_authorised_recipients($users, $forum_id, $options, $sort = false)
 555      {
 556          if (empty($users))
 557          {
 558              return array();
 559          }
 560  
 561          $users = array_unique($users);
 562  
 563          if ($sort)
 564          {
 565              sort($users);
 566          }
 567  
 568          $auth_read = $this->auth->acl_get_list($users, 'f_read', $forum_id);
 569  
 570          if (empty($auth_read))
 571          {
 572              return array();
 573          }
 574  
 575          return $this->check_user_notification_options($auth_read[$forum_id]['f_read'], $options);
 576      }
 577  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1