Source for file ReloaderUserOnline.class.php

Documentation is available at ReloaderUserOnline.class.php

  1. <?php
  2. /**
  3.  * Class ReloaderUserOnline, generate userOnline-JSON
  4.  *
  5.  * LICENSE: CREATIVE COMMONS PUBLIC LICENSE  "Namensnennung — Nicht-kommerziell 2.0"
  6.  *
  7.  * @copyright  2009 <SEDesign />
  8.  * @license    http://creativecommons.org/licenses/by-nc/2.0/de/
  9.  * @version    $3.0.6$
  10.  * @link       http://www.sedesign.de/de_produkte_chat-v3.html
  11.  * @since      File available since Alpha 1.0
  12.  */
  13.  
  14. {
  15.     /**
  16.     * Constructor
  17.     *
  18.     * @uses ConnectDB::sqlGet()
  19.     * @uses ConnectDB::sqlSet()
  20.     * @uses ConnectDB::close()
  21.     * @return void 
  22.     */
  23.     public function __construct (){
  24.     
  25.         // call parent Constructor from class DbConectionMaker
  26.         parent::__construct();
  27.         
  28.         // starts session
  29.         session_start();
  30.         
  31.         // Disable Browser Chache
  32.         header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0');
  33.         
  34.         // JSON Content
  35.         header('content-type: application/json; charset=utf-8');
  36.         
  37.         // delete all old datasets from the etchat_useronline table (session table)
  38.         if (!empty($_SESSION['etchat_'.$this->_prefix.'config_reloadsequenz']))
  39.             $this->dbObj->sqlSet("DELETE FROM {$this->_prefix}etchat_useronline WHERE etchat_onlinetimestamp < ".(date('U')-(((int)$_SESSION['etchat_'.$this->_prefix.'config_reloadsequenz']/1000)*4)));
  40.  
  41.         // Get all Session from the etchat_useronline with all user sessions
  42.         $feld=$this->dbObj->sqlGet("SELECT
  43.         etchat_user_online_user_name,
  44.         etchat_user_online_room_name,
  45.         etchat_fid_room,
  46.         etchat_onlineuser_fid,
  47.         etchat_onlineip,
  48.         etchat_user_online_user_priv,
  49.         etchat_user_online_room_goup,
  50.         etchat_user_online_user_sex,
  51.         etchat_user_online_user_status_img,
  52.         etchat_user_online_user_status_text 
  53.         FROM {$this->_prefix}etchat_useronline
  54.         ORDER BY etchat_fid_room, etchat_user_online_user_name");
  55.  
  56.         // Get only empty rooms becouse this rooms are not cointained in the session-tab etchat_useronline
  57.         $roomarray=$this->dbObj->sqlGet("SELECT etchat_id_room, etchat_roomname, etchat_room_goup FROM {$this->_prefix}etchat_rooms
  58.         WHERE etchat_id_room NOT IN (SELECT DISTINCT etchat_fid_room FROM {$this->_prefix}etchat_useronline)");
  59.         
  60.         if (!is_array($feld)) $this->errorOutput(0);
  61.         else{
  62.             // Sometimes the user not instandly setted in the DB. So if your own session ist not found hust retorn '0' and the chat.js will make a new request.
  63.             if (!$this->checkIfUserJustInserted($feld)) $this->errorOutput(0);
  64.             else{
  65.                 // all the user online data will be settet to the tmp-session Var. If by the next turn there are no changes in the online-list, the response of
  66.                 // reloaderUserOnline.php will be empty, so no unnecessary traffic will be made
  67.                 if (!$this->anyChangesSinceLastPolling($feld,$roomarray)) $this->dbObj->close();
  68.                 // make the JSON-response
  69.                 else $this->makeJsonOutput($feld,$roomarray);
  70.             }
  71.         }
  72.     }
  73.     
  74.     /**
  75.     * Checks if there where any changes since the last request of this document
  76.     *
  77.     * @param  array $feld Dataset from etchat_useronline
  78.     * @param  array $roomarray Dataset from etchat_rooms if there any empty rooms
  79.     * @return bool
  80.     */
  81.     private function anyChangesSinceLastPolling($feld,$roomarray){
  82.  
  83.         // create one string with all DB-params
  84.         for ($a=0; $a < count($feld); $a++)
  85.             $rel .= $feld[$a][1].$feld[$a][2].$feld[$a][3].$feld[$a][5].$feld[$a][8];
  86.         for ($a=0; $a < count($roomarray); $a++)
  87.             $rel .= $roomarray[$a][0].$roomarray[$a][1];
  88.         
  89.         // add the blocking parameters to the same string
  90.         if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_all'])) $rel .= implode("-",$_SESSION['etchat_'.$this->_prefix.'block_all']);
  91.         if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_priv'])) $rel .= implode("-",$_SESSION['etchat_'.$this->_prefix.'block_priv']);
  92.         if (is_array ($_SESSION['etchat_'.$this->_prefix.'roompw_array'])) $rel .= implode("-",$_SESSION['etchat_'.$this->_prefix.'roompw_array']);
  93.  
  94.         // equalize the actual made string with the string saved at last turn/pull
  95.         if($_SESSION['etchat_'.$this->_prefix.'reload_user_anz'== $relreturn false;
  96.         else {
  97.         
  98.             // Save the actual string to the session
  99.             $_SESSION['etchat_'.$this->_prefix.'reload_user_anz'$rel;
  100.             return true;
  101.         }
  102.     }
  103.     
  104.     /**
  105.     * Checks if the user is actualy in the DB - etchat_useronline - session table
  106.     *
  107.     * @param  array $feld Dataset from etchat_useronline
  108.     * @return bool
  109.     */
  110.     private function checkIfUserJustInserted($feld){
  111.     
  112.         $benutzer_id_in_db = 0;
  113.         for ($a=0; $a < count($feld); $a++)    
  114.             if ($feld[$a][3]==$_SESSION['etchat_'.$this->_prefix.'user_id']$benutzer_id_in_db++;
  115.  
  116.         if($benutzer_id_in_db!=1return false;
  117.         else return true;
  118.  
  119.     }
  120.     
  121.     /**
  122.     * Print a error message, and close db connect
  123.     *
  124.     * @param  string $message Outputmessage, usualy "0" (if any error)
  125.     * @uses ConnectDB::close()
  126.     * @return void
  127.     */
  128.     private function errorOutput($message=0){
  129.         echo $message; 
  130.         $this->dbObj->close();
  131.     }
  132.     
  133.     /**
  134.     * Send a JSON Message at chat.js, and close db connect
  135.     *
  136.     * @param  array $feld Dataset from etchat_useronline Tab
  137.     * @param  array $roomarray dataset from etchat_rooms Tab contains just all empty room.
  138.     * @uses RoomAllowed 
  139.     * @uses RoomAllowed::$room_status if the room is open/closed/pw-protected
  140.     * @return void
  141.     */
  142.     private function makeJsonOutput($feld,$roomarray){
  143.     
  144.         // creating the JSON output vor userOnline
  145.         echo"{ \"userOnline\" : [\n";
  146.         
  147.         // turn for creating every user data
  148.         for ($a=0; $a < count($feld); $a++){
  149.  
  150.             // strikethrough the user name if this user ist blocked by you
  151.             if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_all']&& in_array($feld[$a][3]$_SESSION['etchat_'.$this->_prefix.'block_all'])) $feld[$a][0]="<strike>".$feld[$a][0];
  152.             if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_priv']&& in_array($feld[$a][3]$_SESSION['etchat_'.$this->_prefix.'block_priv'])) $feld[$a][0]="<strike>".$feld[$a][0];
  153.  
  154.             // who ist allowed to visit this room
  155.             $room_allowed = new RoomAllowed($feld[$a][6]$feld[$a][2]);
  156.     
  157.             // Put the chat status in yourSession
  158.             if ($_SESSION['etchat_'.$this->_prefix.'user_id']==$feld[$a][3]$_SESSION['etchat_'.$this->_prefix.'userstatus'$feld[$a][8];
  159.  
  160.             if ($a>0echo "\n,\n";
  161.  
  162.             echo"{";
  163.             echo"\"user\": \"".addslashes($feld[$a][0])."\",\"user_id\": \"".$feld[$a][3]."\",";
  164.             echo"\"user_priv\": \"".$feld[$a][5]."\",\"user_sex\": \"".$feld[$a][7]."\",\"user_simg\": \"".$feld[$a][8]."\",";
  165.             echo"\"user_stext\": \"".$feld[$a][9]."\",\"room_id\": \"".$feld[$a][2]."\",\"room_allowed\": \"".$room_allowed->room_status."\",";
  166.             echo"\"room\": \"".addslashes($feld[$a][1])."\"";
  167.             
  168.             // this data is only for admin and mod.  Its importand for blacklist
  169.             if ($_SESSION['etchat_'.$this->_prefix.'user_priv']=="admin" || $_SESSION['etchat_'.$this->_prefix.'user_priv']=="mod"
  170.                 echo",\"user_ip\": \"".addslashes(str_replace("@"," - ",$feld[$a][4]))."\"";
  171.             echo"}";
  172.         }
  173.  
  174.         echo "\n]";
  175.         
  176.         // now all the empty rooms. so the room the no any user in
  177.         if (is_array($roomarray)){
  178.             
  179.             echo ",\n\n \"all_empty_rooms\" : [\n";
  180.             
  181.             // turn for creating every empty room data
  182.             for ($a=0; $a < count($roomarray); $a++){
  183.  
  184.                 // who ist allowed to visit this room
  185.                 $room_allowed2 = new RoomAllowed($roomarray[$a][2], $roomarray[$a][0]);
  186.  
  187.                 if ($a>0) echo "\n,\n";
  188.                 echo"{\"room_id\": \"".(addslashes($roomarray[$a][0]))."\",\"room_allowed\": \"".$room_allowed2->room_status."\",\"room\": \"".(addslashes($roomarray[$a][1]))."\"}";
  189.             }
  190.             echo "\n]";
  191.         }        
  192.         echo "\n}";
  193.     }

Documentation generated on Tue, 22 Dec 2009 09:42:51 +0100 by phpDocumentor 1.4.1