Source for file ReloaderMessages.class.php

Documentation is available at ReloaderMessages.class.php

  1. <?php
  2. /**
  3.  * Class ReloaderMessages, the AJAX Request for getting and setting new messges,  pull session and more,
  4.  *
  5.  * LICENSE: CREATIVE COMMONS PUBLIC LICENSE  "Namensnennung — Nicht-kommerziell 2.0"
  6.  *
  7.  * @copyright  2010 <SEDesign />
  8.  * @license    http://creativecommons.org/licenses/by-nc/2.0/de/
  9.  * @version    $3.0.7$
  10.  * @link       http://www.sedesign.de/de_produkte_chat-v3.html
  11.  * @since      File available since Alpha 2.0
  12.  */
  13.  
  14. {
  15.  
  16.     /**
  17.     * Constructor
  18.     *
  19.     * @uses ConnectDB::sqlGet()
  20.     * @uses Blacklist object creation
  21.     * @uses Blacklist::userInBlacklist() checks if in the Blacklist
  22.     * @uses Blacklist::allowedToAndSetCookie()
  23.     * @uses Blacklist::killUserSession()
  24.     * @uses MessageInserter object creation
  25.     * @uses MessageInserter::$status break if status is "spam"
  26.     * @return void 
  27.     */
  28.     public function __construct ()
  29.         
  30.         // call parent Constructor from class DbConectionMaker
  31.         parent::__construct()
  32.  
  33.         session_start();
  34.  
  35.         header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0');
  36.         header('content-type: application/json; charset=utf-8');
  37.  
  38.         // Checks if the user is in the kick list
  39.         if ($this->checkKicklist()) $this->errorOutput("kick")return false
  40.         
  41.         // create new Blacklist Object
  42.         $blackListObj = new Blacklist($this->dbObj);
  43.         
  44.         // Is the curent user IP in zhe Blacklist or has the user browser an actual "black cookie"?
  45.         if ($blackListObj->userInBlacklist()){
  46.             if ($blackListObj->allowedToAndSetCookie())
  47.                 $blackListObj->killUserSession();
  48.                 $this->errorOutput("blacklist");
  49.                 return false;
  50.             }
  51.         }
  52.         
  53.         // Get room array
  54.         $raum_array $this->getRoomArray();
  55.         // it kannt happen, but if somebody tries to fool the JavaScript in chat.js ;-)
  56.         if (!is_array($raum_array)) return false;
  57.         
  58.         // all needed Userdata for current user
  59.         $user_array=$this->dbObj->sqlGet("SELECT etchat_user_id, etchat_username, etchat_userprivilegien, etchat_usersex FROM {$this->_prefix}etchat_user where etchat_user_id = ".$_SESSION['etchat_'.$this->_prefix.'user_id']);
  60.  
  61.         // Update etchat_useronline if the session exists or create a new dataset in the table
  62.         $this->refreshUserSession($user_array$raum_array$blackListObj->user_param_all);
  63.         
  64.         if (empty($_POST['privat'])) $_POST['privat']=0;
  65.  
  66.         // Make message
  67.         if (isset($_POST['message']&& !empty($_POST['message']&& trim($_POST['message'])!="/window:" && !empty($_SESSION['etchat_'.$this->_prefix.'user_id'])){
  68.             
  69.             // create new MessageInserter Object
  70.             $inserterObj = new MessageInserter($this->dbObj$raum_array);
  71.             
  72.             // if $inserterObj->status="spam" then the user is now inserted in Blacklist and just send "spam" message to the JacaScript at AJAX
  73.             if (!empty($inserterObj->status)) { 
  74.                 $this->errorOutput($inserterObj->status)
  75.                 return false
  76.             }
  77.         }
  78.         
  79.         // selects all needed Messages to display and make a JSON output from it
  80.         $this->makeJsonOutput($this->selectMessagesForTheUser());
  81.     }
  82.  
  83.  
  84.     /**
  85.     * Creates the JSON-Output for AJAX-Request 
  86.     *
  87.     * @param Array $feld this array contains the messages to be transmitted to the user
  88.     * @uses ConnectDB::sqlGet()    
  89.     * @uses ConnectDB::close()    
  90.     * @uses StaticMethods::filtering()
  91.     * @return void
  92.     */
  93.     private function makeJsonOutput($feld){
  94.     
  95.         $ausgabeJSON_Inhalt=array();
  96.  
  97.         // Get the smileys list
  98.         $sml = $this->dbObj->sqlGet("SELECT etchat_smileys_sign, etchat_smileys_img FROM {$this->_prefix}etchat_smileys");
  99.  
  100.         // JSON creation
  101.         if (is_array($feld)){
  102.             $ausgabeJSON_Anfang = "{\"data\" : [";
  103.  
  104.             for ($a=0; $a < count($feld); $a++){
  105.                 // Blocking if the opponent user that is in the blocklist of user own session
  106.                 if (!$this->blockiere($feld[$a][6],$feld[$a][5])){
  107.                     
  108.                     // outputed messages counter, is used as a continuous message id in chat.js
  109.                     
  110.                     $message2send = addslashes(StaticMethods::filtering($feld[$a][2], $sml, $this->_prefix));
  111.                     
  112.                     // private messages in extra window
  113.                     if (substr($message2send08)=="/window:" && $feld[$a][5]!=0{
  114.                         $message2send = substr($message2send, 8, strlen($message2send));
  115.                         $normal_message_counter = "";
  116.                     }
  117.                     else {
  118.                         $_SESSION['etchat_'.$this->_prefix.'count']++;
  119.                         $normal_message_counter $_SESSION['etchat_'.$this->_prefix.'count'];
  120.                     }
  121.                     
  122.                     $ausgabeJSON_Inhalt[] = "{\"id\":\"".$normal_message_counter."\",\"user\":\"".(addslashes($feld[$a][1]))."\",\"user_id\":\"".(addslashes($feld[$a][6]))."\",\"message\":\"".$message2send."\",\"time\":\"".date("H:i",$feld[$a][3])."\",\"privat\":\"".$feld[$a][5]."\",\"css\":\"".$feld[$a][7]."\",\"priv\":\"".$feld[$a][8]."\",\"sex\":\"".$feld[$a][9]."\"}";
  123.                 }
  124.             }
  125.  
  126.             $ausgabeJSON_Ende ="]}";
  127.         }
  128.  
  129.         // close DB connection
  130.         $this->dbObj->close();
  131.         
  132.         // make JSON-Output
  133.         if (count($ausgabeJSON_Inhalt)>0echo $ausgabeJSON_Anfang.implode(","$ausgabeJSON_Inhalt).$ausgabeJSON_Ende;
  134.     }
  135.     
  136.  
  137.     
  138.     /**
  139.     * Every pull refreshes the user data in the session table, etchat_useronline
  140.     *
  141.     * @param Array $user_array requested data from user table
  142.     * @param Array $raum_array requested data from room table
  143.     * @param String $user_param_all User IP data for Blacklist
  144.     * @uses ConnectDB::sqlGet()    
  145.     * @uses ConnectDB::sqlSet()    
  146.     * @return void
  147.     */
  148.     private function refreshUserSession($user_array, $raum_array, $user_param_all){
  149.     
  150.         $user_onlineid = $this->dbObj->sqlGet("SELECT etchat_onlineid FROM {$this->_prefix}etchat_useronline where etchat_onlineuser_fid = ".$_SESSION['etchat_'.$this->_prefix.'user_id']);
  151.  
  152.         // if the usersession was created and is now existing
  153.         if(is_array($user_onlineid))
  154.             $this->dbObj->sqlSet("UPDATE {$this->_prefix}etchat_useronline SET
  155.                 etchat_onlineuser_fid = ".$user_array[0][0].",
  156.                 etchat_onlinetimestamp = ".date('U').",
  157.                 etchat_onlineip = '".$user_param_all."',
  158.                 etchat_fid_room = ".$raum_array[0][0].",
  159.                 etchat_user_online_room_goup = ".$raum_array[0][2].",
  160.                 etchat_user_online_room_name = '".$raum_array[0][1]."',
  161.                 etchat_user_online_user_name = '".$user_array[0][1]."',
  162.                 etchat_user_online_user_priv = '".$user_array[0][2]."',
  163.                 etchat_user_online_user_sex = '".$user_array[0][3]."'
  164.                 WHERE etchat_onlineid = ".$user_onlineid[0][0]);
  165.                 
  166.         // the user session is not yet existing, so create it
  167.         else {
  168.             $this->dbObj->sqlSet("INSERT INTO {$this->_prefix}etchat_useronline ( etchat_onlineuser_fid, etchat_onlinetimestamp, etchat_onlineip, etchat_fid_room, etchat_user_online_room_goup, etchat_user_online_room_name, etchat_user_online_user_name, etchat_user_online_user_priv, etchat_user_online_user_sex)
  169.                 VALUES ( '".$user_array[0][0]."', ".date('U').", '".$user_param_all."', ".$raum_array[0][0].", ".$raum_array[0][2].", '".$raum_array[0][1]."', '".$user_array[0][1]."', '".$user_array[0][2]."', '".$user_array[0][3]."')");
  170.             
  171.             // if user shoul be invisible on enter
  172.             if ($_SESSION['etchat_'.$this->_prefix.'invisible_on_enter'])
  173.                 $this->dbObj->sqlSet("UPDATE {$this->_prefix}etchat_useronline SET 
  174.                     etchat_user_online_user_status_img = 'status_invisible', etchat_user_online_user_status_text = ''
  175.                     WHERE etchat_onlineuser_fid = ".(int)$_SESSION['etchat_'.$this->_prefix.'user_id']);
  176.                     
  177.             //unset($_SESSION['etchat_'.$this->_prefix.'invisible_on_enter']);
  178.         }
  179.     }
  180.  
  181.     /**
  182.     * Get all room from DB with all information
  183.     *
  184.     * @uses ConnectDB::sqlGet()    
  185.     * @uses RoomAllowed 
  186.     * @uses RoomAllowed::$room_status if the room is open/closed/pw-protected
  187.     * @return Array 
  188.     */
  189.     private function getRoomArray(){
  190.  
  191.         // Get room Array
  192.         $raum_array=$this->dbObj->sqlGet("SELECT etchat_id_room, etchat_roomname, etchat_room_goup, etchat_room_message FROM {$this->_prefix}etchat_rooms where etchat_id_room =".(int)$_POST['room']);
  193.  
  194.         // Checks if the posted roomID exists now, it could be just deleted by admin
  195.         if (!is_array($raum_array)) {
  196.             $_POST['room'] = 1;
  197.             $raum_array=$this->dbObj->sqlGet("SELECT etchat_id_room, etchat_roomname, etchat_room_goup, etchat_room_message FROM {$this->_prefix}etchat_rooms where etchat_id_room = 1");
  198.         }
  199.         else{
  200.             // who ist allowed to visit this room
  201.             $room_allowed=new RoomAllowed($raum_array[0][2], $raum_array[0][0]);
  202.             if ($room_allowed->room_status!=1){
  203.                 $raum_array=$this->dbObj->sqlGet("SELECT etchat_id_room, etchat_roomname, etchat_room_goup FROM {$this->_prefix}etchat_rooms where etchat_id_room = 1");
  204.             }
  205.         }
  206.         
  207.         return $raum_array;
  208.     }
  209.  
  210.  
  211.     /**
  212.     * Checks if the user is in the kicklist now
  213.     *
  214.     * @uses ConnectDB::sqlGet()    
  215.     * @uses ConnectDB::sqlSet()
  216.     * @return bool
  217.     */
  218.     private function checkKicklist(){    
  219.         
  220.         // Get all data from the kick tab
  221.         $kicklist=$this->dbObj->sqlGet("SELECT id from {$this->_prefix}etchat_kick_user where etchat_kicked_user_id = ".$_SESSION['etchat_'.$this->_prefix.'user_id']);
  222.         
  223.         if (is_array($kicklist)){
  224.             
  225.             // delete the user from kicklist
  226.             $this->dbObj->sqlSet("delete from {$this->_prefix}etchat_kick_user where etchat_kicked_user_id = ".$_SESSION['etchat_'.$this->_prefix.'user_id']);
  227.  
  228.             $rechte_zum_kicken=$this->dbObj->sqlGet("select etchat_userprivilegien FROM {$this->_prefix}etchat_user where etchat_user_id = ".$_SESSION['etchat_'.$this->_prefix.'user_id']);
  229.             
  230.             if ($rechte_zum_kicken[0][0]!="admin" && $rechte_zum_kicken[0][0]!="mod"return true;
  231.             else return false;
  232.         }
  233.         else return false;
  234.     }
  235.  
  236.     
  237.     /**
  238.     * Print a error message, and close db connect
  239.     *
  240.     * @param  string $message Outputmessage, usualy "0" (if any error)
  241.     * @uses ConnectDB::close()
  242.     * @return void
  243.     */
  244.     private function errorOutput($message=0){
  245.         echo $message; 
  246.         $this->dbObj->close();
  247.     }
  248.  
  249.     
  250.     /**
  251.     * Creates a dataset with all needed messages for the user
  252.     *
  253.     * @uses ConnectDB::sqlGet()    
  254.     * @uses ConnectDB::sqlSet()
  255.     * @return Array
  256.     */
  257.     private function selectMessagesForTheUser(){
  258.     
  259.         // on first message / on entrance
  260.         if (empty($_SESSION['etchat_'.$this->_prefix.'last_id'])) {
  261.             // checks if the own last_id is realy the last one
  262.             $counted_ids=$this->dbObj->sqlGet("SELECT count(etchat_id) FROM {$this->_prefix}etchat_messages WHERE etchat_id > ".$_SESSION['etchat_'.$this->_prefix.'my_first_mess_id']);
  263.             if (is_array($counted_ids&& $counted_ids[0][0]>=$this->_messages_shown_on_entrance$this->_messages_shown_on_entrance+=$counted_ids[0][0];
  264.             
  265.             // get all messages
  266.             $feld=$this->dbObj->sqlGet("SELECT etchat_id, etchat_username, etchat_text, etchat_timestamp, etchat_fid_room, etchat_privat, etchat_user_id, etchat_text_css, etchat_userprivilegien, etchat_usersex 
  267.                 FROM {$this->_prefix}etchat_messages, {$this->_prefix}etchat_user where (etchat_fid_room = ".(int)$_POST['room']." or etchat_fid_room = 0 or etchat_privat=".$_SESSION['etchat_'.$this->_prefix.'user_id'].") and
  268.                 (etchat_privat=0 or etchat_privat=".$_SESSION['etchat_'.$this->_prefix.'user_id']." or etchat_user_fid=".$_SESSION['etchat_'.$this->_prefix.'user_id'].") and etchat_user_id=etchat_user_fid ORDER BY etchat_id DESC LIMIT ".$this->_messages_shown_on_entrance);
  269.             
  270.             // Set last DB id
  271.             $_SESSION['etchat_'.$this->_prefix.'last_id'$feld[0][0];
  272.             
  273.             $feld = array_reverse($feld);
  274.         }
  275.         else {
  276.             // get all messages
  277.             $feld=$this->dbObj->sqlGet("SELECT etchat_id, etchat_username, etchat_text, etchat_timestamp, etchat_fid_room, etchat_privat, etchat_user_id, etchat_text_css, etchat_userprivilegien, etchat_usersex 
  278.                 FROM {$this->_prefix}etchat_messages, {$this->_prefix}etchat_user WHERE (etchat_fid_room = ".(int)$_POST['room']." or etchat_fid_room = 0 or etchat_privat=".$_SESSION['etchat_'.$this->_prefix.'user_id'].")
  279.                 and etchat_id > ".$_SESSION['etchat_'.$this->_prefix.'last_id']." and
  280.                 (etchat_privat=0 or etchat_privat=".$_SESSION['etchat_'.$this->_prefix.'user_id']." or etchat_user_fid=".$_SESSION['etchat_'.$this->_prefix.'user_id'].")
  281.                 and etchat_user_id=etchat_user_fid ORDER BY etchat_id ");
  282.             
  283.             if (is_array($feld)) $_SESSION['etchat_'.$this->_prefix.'last_id']$feld[(count($feld)-1)][0];
  284.             else
  285.             // DE
  286.             // Das ist wichtig hier die last_id aus der DB auszulesen sogar wenn für das Raum in bem sich der User befindet keine
  287.             // neuen Messages gab. Sonst bleibt das last_id das alte und beim Raumwechsel kanns passieren, dass alle sonstigen Messages
  288.             // aus dem Raum in den gewächselt wurde, ausgegeben werden.
  289.             
  290.             // EN
  291.             // It is importent to get the last_id from the DB, even there is no messges for the user. Othewise it kan happen that
  292.             // when the user is going to the other chat room hi's got all messges from this room
  293.             {
  294.                 $id=$this->dbObj->sqlGet("SELECT etchat_id FROM {$this->_prefix}etchat_messages ORDER BY etchat_id DESC LIMIT 1");
  295.                 $_SESSION['etchat_'.$this->_prefix.'last_id']=$id[0][0];
  296.             }
  297.         }
  298.         return $feld;
  299.     }
  300.  
  301.  
  302.     /**
  303.     * Blocking if the opponent user that is in the blocklist of user own session
  304.     *
  305.     * @param int $user_id 
  306.     * @param int $privat_id 
  307.     * @return void
  308.     */
  309.     private function blockiere($user_id, $privat_id){
  310.         if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_all']&& in_array($user_id$_SESSION['etchat_'.$this->_prefix.'block_all'])) return true;
  311.         if (is_array ($_SESSION['etchat_'.$this->_prefix.'block_priv']&& in_array($user_id$_SESSION['etchat_'.$this->_prefix.'block_priv']&& $privat_id==$_SESSION['etchat_'.$this->_prefix.'user_id']return true;
  312.     }
  313.  
  314.     

Documentation generated on Thu, 05 May 2011 14:05:58 +0000 by phpDocumentor 1.4.3