b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/_whosonline.plugin.php - 333 lines - 8368 bytes - Summary - Text - Print

Description: This file implements the Whosonline plugin. This file is part of the b2evolution project - {@link http://b2evolution.net/}

   1  <?php
   2  /**
   3   * This file implements the Whosonline plugin.
   4   *
   5   * This file is part of the b2evolution project - {@link http://b2evolution.net/}
   6   *
   7   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   8   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * Daniel HAHLER grants Francois PLANQUE the right to license
  21   * Daniel HAHLER's contributions to this file and the b2evolution project
  22   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  23   * }}
  24   *
  25   * @package plugins
  26   *
  27   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  28   * @author blueyed: Daniel HAHLER.
  29   * @author fplanque: Francois PLANQUE - {@link http://fplanque.net/}
  30   * @author jeffbearer: Jeff BEARER - {@link http://www.jeffbearer.com/}.
  31   *
  32   * @version $Id: _whosonline.plugin.php 6136 2014-03-08 07:59:48Z manuel $
  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  
  37  /**
  38   * Calendar Plugin
  39   *
  40   * This plugin displays
  41   */
  42  class whosonline_plugin extends Plugin
  43  {
  44      /**
  45       * Variables below MUST be overriden by plugin implementations,
  46       * either in the subclass declaration or in the subclass constructor.
  47       */
  48  
  49      var $name;
  50      var $code = 'evo_WhosOnline';
  51      var $priority = 96;
  52      var $version = '5.0.0';
  53      var $author = 'The b2evo Group';
  54      var $group = 'widget';
  55  
  56  
  57      /**
  58       * Init
  59       */
  60  	function PluginInit( & $params )
  61      {
  62          $this->name = T_( 'Who\'s online Widget' );
  63          $this->short_desc = T_('This skin tag displays a list of whos is online.');
  64          $this->long_desc = T_('All logged in users and guest users who have requested a page in the last 5 minutes are listed.');
  65      }
  66  
  67  
  68    /**
  69     * Get definitions for widget specific editable params
  70     *
  71       * @see Plugin::GetDefaultSettings()
  72       * @param local params like 'for_editing' => true
  73       */
  74  	function get_widget_param_definitions( $params )
  75      {
  76          $r = array(
  77              'contacticons' => array(
  78                  'label' => T_('Contact icons'),
  79                  'note' => T_('Display contact icons allowing to send private messages to logged in users.'),
  80                  'type' => 'checkbox',
  81                  'defaultvalue' => true,
  82              ),
  83              'timeout_online_user' => array(
  84                  'label' => T_( 'Online session timeout' ),
  85                  'note' => T_( 'seconds. After how much time of inactivity an user is considered to be offline? 300 seconds are 5 minutes.' ),
  86                  'type' => 'integer',
  87                  'defaultvalue' => 300,
  88                  'valid_range' => array(
  89                      'min' => 1, // 0 would not make sense.
  90                  ),
  91              ),
  92          );
  93          return $r;
  94      }
  95  
  96  
  97      /**
  98       * Event handler: SkinTag (widget)
  99       *
 100       * @param array Associative array of parameters.
 101       * @return boolean did we display?
 102       */
 103  	function SkinTag( $params )
 104      {
 105          global $Plugins;
 106  
 107          if( $Plugins->trigger_event_first_true('CacheIsCollectingContent') )
 108          { // A caching plugin collecting the content
 109              return false;
 110          }
 111  
 112          echo $params['block_start'];
 113  
 114          echo $params['block_title_start'];
 115          echo T_('Who\'s Online?');
 116          echo $params['block_title_end'];
 117  
 118          $OnlineSessions = new OnlineSessions( $params['timeout_online_user'] );
 119          $OnlineSessions->display_onliners( $params );
 120  
 121          echo $params['block_end'];
 122  
 123          return true;
 124      }
 125  }
 126  
 127  
 128  /**
 129   * This tracks who is online
 130   *
 131   * @todo dh> I wanted to add a MySQL INDEX on the sess_lastseen field, but this "plugin"
 132   *       is the only real user of this. So, when making this a plugin, this should
 133   *       add the index perhaps.
 134   * @package evocore
 135   */
 136  class OnlineSessions
 137  {
 138      /**
 139       * Number of guests (and users that want to be anonymous)
 140       *
 141       * Gets lazy-filled when needed, through {@link init()}.
 142       *
 143       * @access protected
 144       */
 145      var $_count_guests;
 146  
 147  
 148      /**
 149       * List of registered users.
 150       *
 151       * Gets lazy-filled when needed, through {@link init()}.
 152       *
 153       * @access protected
 154       */
 155      var $_registered_Users;
 156  
 157      /**
 158       * Online session timeout in seconds.
 159       *
 160       * Default value: 300 (5 minutes). Set by {@link OnlineSessions::OnlineSessions()}.
 161       *
 162       * @access protected
 163       */
 164      var $_timeout_online_user;
 165  
 166      var $_initialized = false;
 167  
 168  
 169      /**
 170       * Constructor.
 171       *
 172       * @param integer Online session timeout in seconds.
 173       */
 174  	function OnlineSessions( $timeout_online_user = 300 )
 175      {
 176          $this->_timeout_online_user = $timeout_online_user;
 177      }
 178  
 179  
 180      /**
 181       * Get an array of registered users and guests.
 182       *
 183       * @return array containing number of registered users and guests ('registered' and 'guests')
 184       */
 185  	function init()
 186      {
 187          if( $this->_initialized )
 188          {
 189              return true;
 190          }
 191          global $DB, $UserSettings, $localtimenow;
 192  
 193          $this->_count_guests = 0;
 194          $this->_registered_Users = array();
 195  
 196          $timeout_YMD = date( 'Y-m-d H:i:s', ($localtimenow - $this->_timeout_online_user) );
 197  
 198          $UserCache = & get_UserCache();
 199  
 200          // We get all sessions that have been seen in $timeout_YMD and that have a session key.
 201          // NOTE: we do not use DISTINCT here, because guest users are all "NULL".
 202          $online_user_ids = $DB->get_col( "
 203              SELECT SQL_NO_CACHE sess_user_ID
 204                FROM T_sessions INNER JOIN T_hitlog ON sess_ID = hit_sess_ID
 205               WHERE sess_lastseen_ts > '".$timeout_YMD."'
 206                 AND sess_key IS NOT NULL
 207                 AND hit_agent_type = 'browser'
 208               GROUP BY sess_ID", 0, 'Sessions: get list of relevant users.' );
 209          $registered_online_user_ids = array_diff( $online_user_ids, array( NULL ) );
 210          // load all online users into the cache because we need information ( login, avatar ) about them
 211          $UserCache->load_list( $registered_online_user_ids );
 212          foreach( $online_user_ids as $user_ID )
 213          {
 214              if( !empty( $user_ID ) && ( $User = & $UserCache->get_by_ID( $user_ID, false ) ) )
 215              {
 216                  // assign by ID so that each user is only counted once (he could use multiple user agents at the same time)
 217                  $this->_registered_Users[ $user_ID ] = & $User;
 218  
 219                  if( $UserSettings->get( 'show_online', $User->ID ) )
 220                  {
 221                      $this->_count_guests++;
 222                  }
 223              }
 224              else
 225              {
 226                  $this->_count_guests++;
 227              }
 228          }
 229  
 230          $this->_initialized = true;
 231      }
 232  
 233  
 234      /**
 235       * Get the number of guests.
 236       *
 237       * @param boolean display?
 238       */
 239  	function number_of_guests( $display = true )
 240      {
 241          if( !isset($this->_count_guests) )
 242          {
 243              $this->init();
 244          }
 245  
 246          if( $display )
 247          {
 248              echo $this->_count_guests;
 249          }
 250          return $this->_count_guests;
 251      }
 252  
 253  
 254      /**
 255       * Template function: Display onliners, both registered users and guests.
 256       *
 257       * @todo get class="" out of here (put it into skins)
 258       */
 259  	function display_onliners( $params )
 260      {
 261          $this->display_online_users( $params );
 262          $this->display_online_guests( $params );
 263      }
 264  
 265      /**
 266       * Template function: Display the registered users who are online
 267       *
 268       * @todo get class="" out of here (put it into skins)
 269       *
 270       * @param array
 271       */
 272  	function display_online_users( $params )
 273      {
 274          global $DB, $Blog, $UserSettings;
 275  
 276          if( !isset($this->_registered_Users) )
 277          {
 278              $this->init();
 279          }
 280  
 281          // Note: not all users want to appear online, so we might have an empty list.
 282          $r = '';
 283  
 284          foreach( $this->_registered_Users as $User )
 285          {
 286              if( $UserSettings->get( 'show_online', $User->ID ) )
 287              {
 288                  if( empty($r) )
 289                  { // first user
 290                      $r .= $params['list_start'];
 291                  }
 292  
 293                  $r .= $params['item_start'];
 294                  $r .= $User->dget('preferredname');
 295                  if( $params['contacticons'] )
 296                  {    // We want contact icons:
 297                      $r .= $User->get_msgform_link();
 298                  }
 299                  $r .= $params['item_end'];
 300              }
 301          }
 302  
 303          if( !empty($r) )
 304          { // we need to close the list
 305              $r .= $params['list_end'];;
 306          }
 307  
 308          echo $r;
 309      }
 310  
 311  
 312      /**
 313       * Template function: Display number of online guests.
 314       */
 315  	function display_online_guests( $params )
 316      {
 317          if( !isset($this->_count_guests) )
 318          {
 319              $this->init();
 320          }
 321  
 322          $r = $params['list_start'];
 323          $r .= $params['item_start'];
 324          $r .= T_('Guest Users:').' ';
 325          $r .= $this->_count_guests;
 326          $r .= $params['item_end'];
 327          $r .= $params['list_end'];;
 328  
 329          echo $r;
 330      }
 331  }
 332  
 333  ?>

title

Description

title

Description

title

Description

title

title

Body