Source for file CheckUserName.class.php

Documentation is available at CheckUserName.class.php

  1. <?php
  2. /**
  3.  * Class CheckUserName, chat login class
  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.     * LangXml Obj for login page
  17.     * @var LangXml 
  18.     */
  19.     public $lang;
  20.  
  21.     /**
  22.     * bridge for an foreign user application used or not
  23.     * @var bool 
  24.     */
  25.     protected $user_application;
  26.     
  27.     /**
  28.     * Constructor, contains all once needed operations and steps to check the user, login him and s.o.
  29.     *
  30.     * @uses LangXml object creation
  31.     * @uses LangXml::getLang() parser method
  32.     * @uses Blacklist object creation
  33.     * @uses Blacklist::userInBlacklist() checks if in the Blacklist
  34.     * @uses Blacklist::killUserSession()
  35.     * @uses UserCheckerAndInserter object creation
  36.     * @uses UserCheckerAndInserter::$status status check for insertion
  37.     * @uses ConnectDB::sqlGet()
  38.     * @uses ConnectDB::sqlSet()
  39.     * @uses ConnectDB::close()
  40.     * @return void 
  41.     */
  42.     public function __construct ($user_application=false$username=''$gender=''){
  43.     
  44.         // call parent Constructor from class DbConectionMaker
  45.         parent::__construct();
  46.         
  47.         // starts session in index.php
  48.         @session_start();
  49.         
  50.         // all documentc requested per AJAX should have this part to turn off the browser and proxy cache for any XHR request
  51.         header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0');
  52.         
  53.         $this->user_application = $user_application;
  54.         
  55.         
  56.         // for extern user application 
  57.         if (!$this->user_application){
  58.             // Sets charset and content-type
  59.             header('content-type: application/json; charset=utf-8');
  60.             $username trim($_POST['username']);
  61.             $gender trim($_POST['gender']);
  62.             
  63.         }else{
  64.             // Set all Data from [prefix]_etchat_config Table to Session-Vars. So needs only to be run once on login page.
  65.             $this->configTabData2Session();
  66.             
  67.             // something like cron-job to delete wasteful/old data from db
  68.             $this->dbObj->sqlSet("delete FROM {$this->_prefix}etchat_messages where etchat_timestamp < ".(date('U')-($_SESSION['etchat_'.$this->_prefix.'loeschen_nach']*3600*24)));
  69.             $this->dbObj->sqlSet("delete FROM {$this->_prefix}etchat_blacklist where etchat_blacklist_time < ".date('U'));
  70.             $this->dbObj->sqlSet("delete FROM {$this->_prefix}etchat_kick_user where etchat_kicked_user_time < ".date('U'));
  71.         }
  72.         
  73.         if (empty($gender))$gender="n";
  74.         
  75.         // cookies are explicit duty
  76.         if (!$this->user_application)
  77.             if(!isset($_COOKIE[$this->_prefix.'cookie_test'])){ $this->errorMessage("Please aktivate your cookies.")return false;}
  78.         
  79.         // check if the request comes from index.php
  80.         if (!$this->user_application)
  81.             if(!isset($_POST[$_SESSION[$this->_prefix.'set_check']])){ $this->errorMessage("no hacks and bots")return false;}
  82.         
  83.         // create new LangXml Object
  84.         $langObj = new LangXml();
  85.         $this->lang=$langObj->getLang()->checkusername_php[0];
  86.  
  87.         // checking for to many tries for login 
  88.         if (!$this->user_application)
  89.             if (!$this->loginCounter()) return false;
  90.         
  91.         // open the style.CSS and get the user text-color and system text-color from the header part of any et-chat css styles
  92.         $style_lines = file("styles/".$_SESSION['etchat_'.$this->_prefix.'style']."/style.css");
  93.         foreach($style_lines as $line){
  94.             if (substr($line, 0, 10)=="Textfarbe:") {
  95.                 $ft = explode(":", $line);
  96.                 $_SESSION['etchat_'.$this->_prefix.'textcolor'= trim($ft[1]);
  97.             }
  98.             if (substr($line, 0, 12)=="Systemfarbe:") {
  99.                 $fs = explode(":", $line);
  100.                 $_SESSION['etchat_'.$this->_prefix.'syscolor'= trim($fs[1]);
  101.             }
  102.         }
  103.  
  104.         
  105.         // create new BlacklistChecker Object
  106.         $blackListObj = new Blacklist($this->dbObj);
  107.         
  108.         // Is the curent user IP in zhe Blacklist or has the user browser an actual "black cookie"?
  109.         if ($blackListObj->userInBlacklist()){
  110.             setcookie("cookie_anzahl_logins_in_XX_sek",1);
  111.             if (!$this->user_application$this->errorMessage("blacklist")
  112.             $blackListObj->killUserSession();
  113.             if ($this->user_applicationheader('Location: ./?AfterBlacklistInsertion');
  114.             return false
  115.         }
  116.         
  117.         // delete all old datasets from the etchat_useronline table (session table)
  118.         $this->dbObj->sqlSet("DELETE FROM {$this->_prefix}etchat_useronline WHERE etchat_onlinetimestamp < ".(date('U')-(($_SESSION['etchat_'.$this->_prefix.'config_reloadsequenz']/1000)*4)));
  119.  
  120.         // abort, if $username empty or to long
  121.         if (empty($username|| strlen($username)>100{ $this->errorMessage("")return false}
  122.         
  123.         // convert username with htmlspecialchars
  124.         $username = htmlspecialchars($username, ENT_QUOTES, "UTF-8");
  125.         
  126.         // abort, if this username is occupied now
  127.         if (!$this->user_application)
  128.             if ($this->userInChatNow($username)) { $this->errorMessage($this->lang->name_busy[0]->tagData)return false}
  129.         
  130.         // Dataset with Userparameter from etchat_user tab. The dataset is empty if there no such user with this name
  131.         $user_exists = $this->dbObj->sqlGet("SELECT etchat_user_id, etchat_username, etchat_userpw, etchat_userprivilegien FROM {$this->_prefix}etchat_user WHERE etchat_username = '".$username."' order by etchat_userpw DESC");
  132.         
  133.         // create new CheckerAndInserterObj Object
  134.         $userCheckerAndInserterObj = new UserCheckerAndInserter($this->dbObj$user_exists$username$_POST['pw']$gender$this->lang);
  135.         
  136.         //  Status 1 means that the loggining was sucessfull
  137.         if ($userCheckerAndInserterObj->status==1$this->messageOnEnter();
  138.         else $this->errorMessage($userCheckerAndInserterObj->status);
  139.     }
  140.     
  141.     /**
  142.     * Checking how many logins the user have done in the last 3 Mutes and saves it to zhe cookie. 
  143.     * If  more then is set in  var _limit_logins_in_three_minutes, then the user
  144.     * will get a black-cookie for 3 minutes and an error message
  145.     *
  146.     * @return bool
  147.     */
  148.     private function loginCounter (){
  149.         if(!isset($_COOKIE['cookie_last_login'])) {
  150.             setcookie("cookie_last_login", date('U'));
  151.             setcookie("cookie_anzahl_logins_in_XX_sek",1);
  152.             return true;
  153.         }
  154.         else{
  155.             if (date('U')-$_COOKIE['cookie_last_login'] < 180) {
  156.                 $c_anzahl_logins=$_COOKIE['cookie_anzahl_logins_in_XX_sek']+1;
  157.                 setcookie("cookie_anzahl_logins_in_XX_sek", $c_anzahl_logins);
  158.                 
  159.                 if ($_COOKIE['cookie_anzahl_logins_in_XX_sek']>($this->_limit_logins_in_three_minutes-1)) {
  160.                 setcookie("cookie_last_login", date('U'));
  161.                 $this->errorMessage($this->lang->many_logins[0]->tagData);
  162.                 return false;
  163.                 }
  164.             else return true;
  165.             }
  166.             else {
  167.                 setcookie("cookie_last_login", date('U'));
  168.                 setcookie("cookie_anzahl_logins_in_XX_sek", 1);
  169.                 return true;
  170.             }
  171.         }
  172.     }
  173.     
  174.     /**
  175.     * Checks if this username is occupied now
  176.     *
  177.     * @param  string $user Username
  178.     * @uses ConnectDB::sqlGet()
  179.     * @return bool
  180.     */
  181.     private function userInChatNow($user){
  182.         // if a returned dataset is an array, then the Username is now in etchat_useronline tab, so in the chat session
  183.         if(is_array($this->dbObj->sqlGet("SELECT etchat_username FROM {$this->_prefix}etchat_useronline, {$this->_prefix}etchat_user WHERE etchat_username = '".$user."' AND etchat_user_id = etchat_onlineuser_fid LIMIT 1"))){
  184.             setcookie("cookie_anzahl_logins_in_XX_sek",1);
  185.             return true;
  186.         }
  187.         else return false;
  188.     }
  189.     
  190.     /**
  191.     * Print a error message, and close db connect
  192.     *
  193.     * @param  string $message Outputmessage is status != 1, so the user cant be loggin to chat
  194.     * @uses ConnectDB::close
  195.     * @return bool
  196.     */
  197.     private function errorMessage($message){
  198.         echo $message; 
  199.         $this->dbObj->close();
  200.         return false;
  201.     }
  202.     
  203.     /**
  204.     * The login was sucessfull, so send a message on entrance to the system and close db connection
  205.     * @uses RoomEntrance object creation
  206.     * @uses ConnectDB::close()
  207.     * @return bool
  208.     */
  209.     private function messageOnEnter(){
  210.         // create new RoomEntrance Object
  211.         new RoomEntrance($this->dbObj$this->lang);
  212.  
  213.         $this->dbObj->close();
  214.         
  215.         // it was sucessfull so give it to JavaScript in login.js as AJAX Response
  216.         if (!$this->user_applicationecho "1";
  217.         else header('Location: ./?Chat');
  218.         return true;
  219.     }

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