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.         $this->dbObj->sqlSet("DELETE FROM {$this->_prefix}etchat_useronline WHERE etchat_onlinetimestamp < ".(date('U')-(((int)$_REQUEST['reloadsequenz']/1000)*4)));
  39.  
  40.         // Get all Session from the etchat_useronline with all user sessions
  41.         $feld=$this->dbObj->sqlGet("SELECT
  42.         etchat_user_online_user_name,
  43.         etchat_user_online_room_name,
  44.         etchat_fid_room,
  45.         etchat_onlineuser_fid,
  46.         etchat_onlineip,
  47.         etchat_user_online_user_priv,
  48.         etchat_user_online_room_goup,
  49.         etchat_user_online_user_sex,
  50.         etchat_user_online_user_status_img,
  51.         etchat_user_online_user_status_text 
  52.         FROM {$this->_prefix}etchat_useronline
  53.         ORDER BY etchat_fid_room, etchat_user_online_user_name");
  54.  
  55.         // Get only empty rooms becouse this rooms are not cointained in the session-tab etchat_useronline
  56.         $roomarray=$this->dbObj->sqlGet("SELECT etchat_id_room, etchat_roomname, etchat_room_goup FROM {$this->_prefix}etchat_rooms
  57.         WHERE etchat_id_room NOT IN (SELECT DISTINCT etchat_fid_room FROM {$this->_prefix}etchat_useronline)");
  58.         
  59.         if (!is_array($feld)) $this->errorOutput(0);
  60.         else{
  61.             // 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.
  62.             if (!$this->checkIfUserJustInserted($feld)) $this->errorOutput(0);
  63.             else{
  64.                 // 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
  65.                 // reloaderUserOnline.php will be empty, so no unnecessary traffic will be made
  66.                 if (!$this->anyChangesSinceLastPolling($feld,$roomarray)) $this->dbObj->close();
  67.                 // make the JSON-response
  68.                 else $this->makeJsonOutput($feld,$roomarray);
  69.             }
  70.         }
  71.     }
  72.     
  73.     /**
  74.     * Checks if there where any changes since the last request of this document
  75.     *
  76.     * @param  array $feld Dataset from etchat_useronline
  77.     * @param  array $roomarray Dataset from etchat_rooms if there any empty rooms
  78.     * @return bool
  79.     */
  80.     private function anyChangesSinceLastPolling($feld,$roomarray){
  81.         
  82.         // create one string with all DB-params
  83.         for ($a=0; $a < count($feld); $a++)
  84.             $rel .= $feld[$a][1].$feld[$a][2].$feld[$a][3].$feld[$a][5].$feld[$a][8];
  85.         for ($a=0; $a < count($roomarray); $a++)
  86.             $rel .= $roomarray[$a][0].$roomarray[$a][1];
  87.         
  88.         // add the blocking parameters to the same string
  89.         if (is_array ($_SESSION['etchat_v3_block_all'])) $rel .= implode("-",$_SESSION['etchat_v3_block_all']);
  90.         if (is_array ($_SESSION['etchat_v3_block_priv'])) $rel .= implode("-",$_SESSION['etchat_v3_block_priv']);
  91.         if (is_array ($_SESSION['etchat_v3_roompw_array'])) $rel .= implode("-",$_SESSION['etchat_v3_roompw_array']);
  92.  
  93.         // equalize the actual made string with the string saved at last turn/pull
  94.         if($_SESSION['etchat_v3_reload_user_anz'] == $rel) return false;
  95.         else {
  96.         
  97.             // Save the actual string to the session
  98.             $_SESSION['etchat_v3_reload_user_anz'] = $rel;
  99.             return true;
  100.         }
  101.     }
  102.     
  103.     /**
  104.     * Checks if the user is actualy in the DB - etchat_useronline - session table
  105.     *
  106.     * @param  array $feld Dataset from etchat_useronline
  107.     * @return bool
  108.     */
  109.     private function checkIfUserJustInserted($feld){
  110.     
  111.         $benutzer_id_in_db = 0;
  112.         for ($a=0; $a < count($feld); $a++)    
  113.             if ($feld[$a][3]==$_SESSION['etchat_v3_user_id']) $benutzer_id_in_db++;
  114.  
  115.         if($benutzer_id_in_db!=1) return false;
  116.         else return true;
  117.  
  118.     }
  119.     
  120.     /**
  121.     * Print a error message, and close db connect
  122.     *
  123.     * @param  string $message Outputmessage, usualy "0" (if any error)
  124.     * @uses ConnectDB::close()
  125.     * @return void
  126.     */
  127.     private function errorOutput($message=0){
  128.         echo $message; 
  129.         $this->dbObj->close();
  130.     }
  131.     
  132.     /**
  133.     * Send a JSON Message at chat.js, and close db connect
  134.     *
  135.     * @param  array $feld Dataset from etchat_useronline Tab
  136.     * @param  array $roomarray dataset from etchat_rooms Tab contains just all empty room.
  137.     * @uses RoomAllowed 
  138.     * @uses RoomAllowed::$room_status if the room is open/closed/pw-protected
  139.     * @return void
  140.     */
  141.     private function makeJsonOutput($feld,$roomarray){
  142.     
  143.         // creating the JSON output vor userOnline
  144.         echo"{ \"userOnline\" : [\n";
  145.         
  146.         // turn for creating every user data
  147.         for ($a=0; $a < count($feld); $a++){
  148.  
  149.             // strikethrough the user name if this user ist blocked by you
  150.             if (is_array ($_SESSION['etchat_v3_block_all']) && in_array($feld[$a][3], $_SESSION['etchat_v3_block_all'])) $feld[$a][0]="<strike>".$feld[$a][0]."</strike>";
  151.             if (is_array ($_SESSION['etchat_v3_block_priv']) && in_array($feld[$a][3], $_SESSION['etchat_v3_block_priv'])) $feld[$a][0]="<strike>".$feld[$a][0]."</strike>";
  152.  
  153.             // who ist allowed to visit this room
  154.             $room_allowed = new RoomAllowed($feld[$a][6], $feld[$a][2]);
  155.     
  156.             // Put the chat status in yourSession
  157.             if ($_SESSION['etchat_v3_user_id']==$feld[$a][3]) $_SESSION['etchat_v3_userstatus'] = $feld[$a][8];
  158.  
  159.             if ($a>0) echo "\n,\n";
  160.  
  161.             echo"{";
  162.             echo"\"user\": \"".addslashes($feld[$a][0])."\",\"user_id\": \"".$feld[$a][3]."\",";
  163.             echo"\"user_priv\": \"".$feld[$a][5]."\",\"user_sex\": \"".$feld[$a][7]."\",\"user_simg\": \"".$feld[$a][8]."\",";
  164.             echo"\"user_stext\": \"".$feld[$a][9]."\",\"room_id\": \"".$feld[$a][2]."\",\"room_allowed\": \"".$room_allowed->room_status."\",";
  165.             echo"\"room\": \"".addslashes($feld[$a][1])."\"";
  166.             
  167.             // this data is only for admin and mod.  Its importand for blacklist
  168.             if ($_SESSION['etchat_v3_user_priv']=="admin" || $_SESSION['etchat_v3_user_priv']=="mod"
  169.                 echo",\"user_ip\": \"".addslashes(str_replace("@"," - ",$feld[$a][4]))."\"";
  170.             echo"}";
  171.         }
  172.  
  173.         echo "\n]";
  174.         
  175.         // now all the empty rooms. so the room the no any user in
  176.         if (is_array($roomarray)){
  177.             
  178.             echo ",\n\n \"all_empty_rooms\" : [\n";
  179.             
  180.             // turn for creating every empty room data
  181.             for ($a=0; $a < count($roomarray); $a++){
  182.  
  183.                 // who ist allowed to visit this room
  184.                 $room_allowed2 = new RoomAllowed($roomarray[$a][2], $roomarray[$a][0]);
  185.  
  186.                 if ($a>0) echo "\n,\n";
  187.                 echo"{\"room_id\": \"".(addslashes($roomarray[$a][0]))."\",\"room_allowed\": \"".$room_allowed2->room_status."\",\"room\": \"".(addslashes($roomarray[$a][1]))."\"}";
  188.             }
  189.             echo "\n]";
  190.         }        
  191.         echo "\n}";
  192.     }

Documentation generated on Thu, 13 Aug 2009 08:21:59 +0200 by phpDocumentor 1.4.1