Location: PHPKode > projects > QuickTalk Forum > quicktalk/bin/qtf_class_vip.php
<?php

// QuickTalk 2.5 build:20100924

class cFLD
{

var $id = '';     // field key (can be "emails_i")
var $uid = '';
var $sort = false;// default sort order (ASC,DESC,FALSE)
var $name = '';   // field translation
var $class_th = '';
var $style_th = '';
var $class_td = '';
var $style_td = '';
var $class_dynamic = false; // To use this, define an array('formula-%s','field',array-of-classes). Note: keys of the array-of-classes must be strings
var $style_dynamic = false; // To use this, define an array('formula-%s','field',array-of-styles). Note: keys of the array-of-styles must be strings

function cFLD($id,$name,$class_th='',$style_th='',$class_td='',$style_td='',$sort=false)
{
  $this->id = $id;
  $this->uid = ( substr($id,-2,2)=='_i' ? substr($id,0,-2) : $id );
  $this->name = $name;
  $this->class_th = $class_th;
  $this->style_th = $style_th;
  $this->class_td = $class_td;
  $this->style_td = $style_td;
  $this->sort = $sort;
}

function AddStyleDynamic($arr)
{
  // Change $this->style_td to add a dynamic style based on the parameters in $this->style_dynamic and according to the values in $arr
  if ( !is_array($arr) ) return; // row not defined
  if ( count($arr)==0 ) return; // row not defined
  if ( !is_array($this->style_dynamic) ) return; // formula not defined
  if ( count($this->style_dynamic)!=3 ) return; // formula not defined
  if ( !is_string($this->style_dynamic[0]) ) return; // formula not defined
  if ( !is_string($this->style_dynamic[1]) ) return; // formula not defined
  if ( !is_array($this->style_dynamic[2]) ) return; // styles not defined
  $key      = $this->style_dynamic[1];
  $arrStyle = $this->style_dynamic[2]; 
  $strStyle = '';
  if ( isset($arr[$key]) ) {
  if ( isset($arrStyle[strval($arr[$key])]) ) {
    $strStyle = (empty($this->style_td) ? '' : ';').sprintf( $this->style_dynamic[0], $arrStyle[strval($arr[$key])] );
  }}
  return $strStyle;
}

function AddClassDynamic($arr)
{
  // Change $this->style_td to add a dynamic style based on the parameters in $this->style_dynamic and according to the values in $arr
  if ( !is_array($arr) ) return; // row not defined
  if ( count($arr)==0 ) return; // row not defined
  if ( !is_array($this->class_dynamic) ) return; // formula not defined
  if ( count($this->class_dynamic)!=3 ) return; // formula not defined
  if ( !is_string($this->class_dynamic[0]) ) return; // formula not defined
  if ( !is_string($this->class_dynamic[1]) ) return; // formula not defined
  if ( !is_array($this->class_dynamic[2]) ) return; // styles not defined
  $key      = $this->class_dynamic[1];
  $arrClass = $this->class_dynamic[2]; 
  $strClass = '';
  if ( isset($arr[$key]) ) {
  if ( isset($arrClass[strval($arr[$key])]) ) {
    $strClass = ' '.sprintf( $this->class_dynamic[0], $arrClass[strval($arr[$key])] );
  }}
  return $strClass;
}

}

// ==========
// VIP means Visitor In Page: This class includes info on the current user and the current page,
// The class also provides major lists or global stats used in most of the pages
// ==========

class cVIP
{

// --------

var $auth = false;
var $coockieconfirm = false;
var $id = 0;
var $username = 'Guest';
var $role = 'V';  // A=Administator,M=Moderator,U=User,V=Visitor
var $numpost = 0;

var $selfurl = 'qtf_index.php'; // PAGE INFO
var $selfuri = '';  // URL parameters
var $selfname = '';
var $exiturl = 'qtf_index.php';
var $exitname = 'Back';
var $arrCss = array();
var $arrJava = array('bin/qtf_base.js'); // by default all pages includes basic js. Can be disable in each page.

// --------

function cVIP()
{
  if ( isset($_SESSION[QT.'_usr_auth']) ) {
  if ( $_SESSION[QT.'_usr_auth']=='yes' ) {

    $this->auth = true;
    if ( isset($_SESSION[QT.'_usr_id']) ) $this->id = intval($_SESSION[QT.'_usr_id']);
    if ( isset($_SESSION[QT.'_usr_name']) ) $this->username = $_SESSION[QT.'_usr_name'];
    if ( isset($_SESSION[QT.'_usr_role']) ) $this->role = $_SESSION[QT.'_usr_role'];
    if ( isset($_SESSION[QT.'_usr_posts']) ) $this->numpost = $_SESSION[QT.'_usr_posts'];
    if ( isset($_SESSION[QT.'_usr_team_id']) ) $this->team_id = intval($_SESSION[QT.'_usr_team_id']);
    if ( isset($_SESSION[QT.'_usr_team_name']) ) $this->team_name = $_SESSION[QT.'_usr_team_name'];
    
  }}

  if ( !$this->auth ) {
  if ( isset($_COOKIE[QT.'_cookname']) && isset($_COOKIE[QT.'_cookpass']) ) {
  
    global $oDB;
    $oDB->Query('SELECT * FROM '.TABUSER.' WHERE name="'.$_COOKIE[QT.'_cookname'].'" AND pwd="'.$_COOKIE[QT.'_cookpass'].'"');
    if ( $row = $oDB->Getrow() )
    {
      $this->auth = true;
      if ( isset($row['id']) )   $this->id = intval($row['id']);
      if ( isset($row['name']) ) $this->username= $row['name'];
      if ( isset($row['role']) ) $this->role = substr($row['role'],0,1);
      if ( isset($row['numpost']) ) { if ( !empty($row['numpost']) ) $this->numpost=intval($row['numpost']); }
      $this->Register();
      $this->coockieconfirm=true;
    }

  }}

  // Web Team Login

  if ( !$this->auth ) {
  if ( isset($_SESSION[QT]['login_qte_web']) ) {
  if ( !empty($_SESSION[QT]['login_qte_web']) ) {
    
    $this->LoginTeam('qte');

  }}}

  // Team login

  if ( !$this->auth ) {
  if ( isset($_SESSION[QT]['login_qte']) ) {
  if ( !empty($_SESSION[QT]['login_qte']) ) {
    
    $this->LoginTeam($_SESSION[QT]['login_qte']);

  }}}

}

// --------

function IsStaff() { return ($this->role=='M' || $this->role=='A'); }

// --------

function IsPrivate($str,$id)
{
  // Check the privacy setting. $str is the user's privacy level
  // Returns true/false if current user can see the private info
  if ( $str=='2' || $this->IsStaff() || $this->id==$id ) return false;
  if ( $str=='1' && $this->role!='V') return false;  
  return true;
}

// --- [i] ---

function LangAdd($strType='',$strLang='en',$strId='',$strName='',$bCheck=false)
{
  QTargs( 'cVIP->LangAdd',array($strType,$strLang,$strId,$strName,$bCheck),array('str','str','str','str','boo') );
  QTargs( 'cVIP->LangAdd',array($strType,$strLang,$strId,$strName),'empty' );

  // Process

  global $oDB;

  if ( $bCheck )
  {
  $oDB->Query('SELECT count(objid) AS countid FROM '.TABLANG.' WHERE objtype="'.$strType.'" AND objlang="'.strtolower($strLang).'" AND objid="'.$strId.'"');
  $row=$oDB->Getrow();
  if ( $row['countid']!=0 ) return False;
  }
  $oDB->Query('INSERT INTO '.TABLANG.' (objtype,objlang,objid,objname) VALUES ("'.$strType.'","'.strtolower($strLang).'","'.$strId.'","'.addslashes(QTconv($strName,'3',QTF_CONVERT_AMP,false)).'")');
  return true;
}

// --- [i] ---

function LangDel($strType='',$strId='')
{
  if ( is_array($strType) ) $strType = implode('" OR objtype="',$strType);
  QTargs( 'cVIP->LangDel',array($strType,$strId) );
  QTargs( 'cVIP->LangDel',array($strType,$strId),'empty' );

  // Process

  global $oDB;
  $oDB->Query( 'DELETE FROM '.TABLANG.' WHERE (objtype="'.$strType.'") AND objid="'.$strId.'"' );
}

// --- [i] ---

function LangGet($strType='',$strLang='en',$strId='*')
{
  // Return the object name (translated)
  // Can return an array of object names (in this language) when $strId is '*'
  // Can return an array of object translation when $strLang is '*'

  QTargs('cVIP->LangGet',array($strType,$strLang,$strId));
  QTargs('cVIP->LangGet',array($strType,$strLang,$strId),'empty');
  if ( $strId==='*' && $strLang==='*' ) die('cVIP->LangGet: Arg 2 and 3 cannot be *.');

  // Process

  global $oDB;
  if ( $strId==='*' )
  {
    $arr = array();
    $oDB->Query('SELECT objid,objname FROM '.TABLANG.' WHERE objtype="'.$strType.'" AND objlang="'.strtolower($strLang).'"');
    while($row=$oDB->Getrow())
    {
      if ( !empty($row['objname']) ) $arr[$row['objid']]=$row['objname'];
    }
    return $arr;
  }
  elseif ( $strLang==='*' )
  {
    $arr = array();
    $oDB->Query('SELECT objlang,objname FROM '.TABLANG.' WHERE objtype="'.$strType.'" AND objid="'.$strId.'"');
    while($row=$oDB->Getrow())
    {
      $arr[$row['objlang']]=$row['objname'];
    }
    return $arr;
  }
  else
  {
    $oDB->Query('SELECT objname FROM '.TABLANG.' WHERE objtype="'.$strType.'" AND objlang="'.strtolower($strLang).'" AND objid="'.$strId.'"');
    $row=$oDB->Getrow();
    return (empty($row['objname']) ? '' : $row['objname']);
  }
}

// --------
// DOLOGIN login and return extra info (fullname and coppa)

function Login($strUsername='',$strPwd='',$bRemember=FALSE)
{
  global $oDB;
  $oDB->Query('SELECT count(*) as countid FROM '.TABUSER.' WHERE name="'.$strUsername.'" AND pwd="'.sha1($strPwd).'"');

  if ( $row = $oDB->Getrow() ) {
  if ( isset($row['countid']) ) {
  if ( $row['countid']==1 ) {

    $this->auth = true;
    
    // Get user info
    $oDB->Query('SELECT id,name,role,children,numpost,closed FROM '.TABUSER.' WHERE name="'.$strUsername.'" AND pwd="'.sha1($strPwd).'"');
    $row = $oDB->Getrow();
    $this->id = intval($row['id']);
    $this->username = $row['name'];
    $this->role = $row['role'];
    if ( !empty($row['numpost']) ) $this->numpost=intval($row['numpost']);

    // Register VIP info in session
    $this->Register();
    
    if ( $bRemember )
    {
    setcookie(QT.'_cookname', htmlspecialchars($this->username,ENT_QUOTES), time()+60*60*24*100, '/');
    setcookie(QT.'_cookpass', sha1($strPwd), time()+60*60*24*100, '/');
    }
    
    // Reset parameters (because the Role can impact the lists)
    unset($_SESSION[QT]['sys_sections']);

    return array('fullname'=>$strUsername,'coppa'=>intval($row['children']),'closed'=>intval($row['closed']));

  }}}
  
  return array('fullname'=>'','coppa'=>0,'closed'=>0);

}

// --------
// DOLOGINTEAM check weblogin/teamlogin and return extra info (fullname and coppa)

function LoginTeam($sid='qte')
{
  if ( isset($_SESSION[$sid.'_usr_auth']) && ( isset($_SESSION[$sid.'_usr_name']) || isset($_SESSION[$sid.'_usr_username']) ) ) {
  if ( $_SESSION[$sid.'_usr_auth']=='yes' ) {
    
    global $oDB;
    
    // check coherence: name must exist
    if ( isset($_SESSION[$sid.'_usr_name']) ) { $str = $_SESSION[$sid.'_usr_name']; } else { $str=$_SESSION[$sid.'_usr_username']; }
    $oDB->Query( 'SELECT count(*) as countid FROM '.TABUSER.' WHERE name="'.$str.'"' );
    
    if ( $row = $oDB->Getrow() ) {
    if ( isset($row['countid']) ) {
    if ( $row['countid']==1 ) {    

      $this->auth = true;

      // Get user info
      $oDB->Query('SELECT id,name,role,children,numpost,closed FROM '.TABUSER.' WHERE name="'.$str.'"');
      $row = $oDB->Getrow();
      $this->id = intval($row['id']);
      $this->username = $row['name'];
      $this->role = $row['role'];
      if ( !empty($row['numpost']) ) $this->numpost=intval($row['numpost']);

      // Register VIP info in session
      $this->Register();
      unset($_SESSION[QT]['sys_sections']);

      return array('fullname'=>$str,'coppa'=>intval($row['children']),'closed'=>intval($row['closed']));
        
    }}}
    
  }}
  
  return array('fullname'=>'','coppa'=>0,'closed'=>0);
}

// --------
// LOGOUT
// Remove session info (and cookie)

function Logout()
{  
  unset($_SESSION);
  session_destroy();  
  if ( isset($_COOKIE[QT.'_cookname']) ) setcookie(QT.'_cookname', '', time()+60*60*24*100, '/');
  if ( isset($_COOKIE[QT.'_cookpass']) ) setcookie(QT.'_cookpass', '', time()+60*60*24*100, '/');
}

// --------
// REGISTER

function Register()
{  
  $_SESSION[QT.'_usr_auth'] = 'yes';
  $_SESSION[QT.'_usr_id']   = $this->id;
  $_SESSION[QT.'_usr_name'] = $this->username;
  $_SESSION[QT.'_usr_role'] = $this->role;
  $_SESSION[QT.'_usr_posts']= $this->numpost;
  unset($_SESSION['L']);
}

// --------

function BackButton()
{  
  if ( QTF_BACKBUTTON ) return '<td class="button" style="width:20px"><a href="'.Href($this->exiturl).'" class="button">'.QTF_BACKBUTTON.'</a></td>'.N;
  return '';
}

// --------
// @$strTitle: title of the message box. When null or empty string, uses the page name ($this->selfname)
//  when $strTitle=="0", it makes a direct exit
// @$strMessage: message body.
// @$strSkin: the skin folder
// @$intTime: the pause (in second) before redirecting to the exit page. Use 0 to NOT redirect.
// @$strWidth: css width parameter ("300px" or "90%")
// @$strIdHead: css id of the header
// @$strIdMain: css id of the body

function EndMessage($strTitle,$strMessage='Access denied',$strSkin='skin/default',$intTime=0,$strWidth='300px',$strTitleId='msgboxtitle',$strBodyId='msgbox')
{
  $this->exiturl = str_replace('&amp;','&',$this->exiturl);
  if ( $strTitle=='0' ) HtmlRedirect($this->exiturl,$this->exitname);

  if ( empty($strTitle) ) $strTitle = $this->selfname;
  HtmlHead('',$strSkin);
  HtmlPageCtrl(0,'90%');
  HtmlMsg(0,$strWidth,$strTitleId,$strTitle,$strBodyId);
  echo $strMessage,'
  <p><a id="exiturl" href="',Href($this->exiturl),'">',$this->exitname,'</a></p>';
  HtmlMsg(1);
  HtmlPageCtrl(1);

  if ( $intTime>0 )
  {
  echo '
  <script type="text/javascript">
  <!--
  setTimeout(\'window.location=document.getElementById("exiturl").href\',',($intTime*1000),');
  -->
  </script>
  ';
  }
  echo '</body></html>';
  exit;
}

// --------
// $strCanView user role (V[i], U, M or A) that can access the page (i=public access level)
// $bStopOff stop when application off-line

function CanView($strCanView='V5',$bStopOff=true)
{  
  if ( $this->role=='A' ) { if ( $_SESSION[QT]['board_offline']=='1' ) echo '<p style="padding:4px;background-color:#ff0000;color:#ffffff">Board is offline but Administrators can make some actions.</p>'; return true; }
  if ( $strCanView=='U' && $this->role=='V') return false;
  if ( $strCanView=='M' && !$this->IsStaff() ) return false;  
  if ( $strCanView=='A' && $this->role!='A' ) return false;  
  if ( strlen($strCanView)==2 ) { $strPAL=substr($strCanView,-1,1); } else { $strPAL='5'; }
  if ( $this->role=='V' && $_SESSION[QT]['visitor_right']<$strPAL ) return false;
  if ( $_SESSION[QT]['board_offline']=='1' && $bStopOff ) return false;
  return true;
}

// --------

function SysCount($strObject='topics')
{  
  global $oDB;
  switch($strObject)
  {
  case 'topics':
    $oDB->Query('SELECT count(id) as countid FROM '.TABTOPIC);
    $row = $oDB->Getrow();
    return intval($row['countid']);
    break;
  case 'replies':
    $oDB->Query('SELECT count(id) as countid FROM '.TABPOST.' WHERE type<>"P"');
    $row = $oDB->Getrow();
    return intval($row['countid']);
    break;
  case 'members':
    $oDB->Query('SELECT count(id) as countid FROM '.TABUSER.' WHERE id>0');
    $row = $oDB->Getrow();
    return intval($row['countid']);
    break;
  case 'states':
    $arr = array();
      $oDB->Query('SELECT max(id) as countid FROM '.TABUSER);
      $row = $oDB->Getrow();
    $arr['newuserid'] = intval($row['countid']);
      $oDB->Query('SELECT name,firstdate FROM '.TABUSER.' WHERE id='.$row['countid'] );
      $row = $oDB->Getrow();
    $arr['newusername'] = $row['name'];
    $arr['newuserdate'] = (empty($row['firstdate']) ? '0' : substr($row['firstdate'],0,8)); // date only
    return $arr;
    break;
  }
}

// --------

function GetStatuses()
{
  global $L;
  return array('0'=>$L['Opened'],'1'=>$L['Closed']);
}

// --------

function GetTypes()
{
  global $L;
  return array(
  'T'=>array('name'=>$L['Topic'],'color'=>''),
  'A'=>array('name'=>$L['News'],'color'=>''));
}

// --------

}
?>
Return current item: QuickTalk Forum