Location: PHPKode > projects > Quick Guestbook > quicktalk/bin/qtg_class_post.php
<?php

// QuickTalk guestbook version 2.5 build:20100805

class cPost
{

var $id = '-1'; // in case of file system, this is the filename (hached)
var $name = '';
var $message = '';
var $userage = 0;
var $useremail = '';
var $issuedate = '0';
var $postfrom = '';
var $visible = 'Y';
var $x = 0;
var $y = 0;
var $z = 0;
var $ip = '';

function cPost($aPost=null)
{
  if ( isset($aPost) )
  {
    global $oDB;
    if ( is_array($aPost) )
    {
      $this->MakePostFromArray($aPost);
    }
    else
    {
      if ( is_string($aPost) && is_a($oDB,'cDT') )
      {
        $this->id = $this->Unhash($aPost);
        if ( file_exists($oDB->Getpath($this->id).$this->id.'.txt') )
        {
        $oDB->arrFiles = array($this->id);
        $oDB->curFiles = 0;
        $row = $oDB->Getrow();
        $this->MakePostFromArray($row);
        }
        else
        {
        echo 'file '.$this->id.'.txt not found';
        }
      }
      elseif ( is_string($aPost) && is_a($oDB,'cDB') )
      {
        if ( $aPost<0 ) die('No post '.$aPost);
        $oDB->Query('SELECT * FROM '.TABMESSAGE.' WHERE id='.$aPost);
        $row = $oDB->Getrow();
        $this->MakePostFromArray($row);
      }
      else
      {
        die('Invalide constructor parameter#1 for class cPost');
      }
    }
    // override id by filename
    if ( is_a($oDB,'cDT') ) $this->id = substr($this->issuedate,0,8).'-'.substr($this->issuedate,8,6).'-'.$this->ip.'-'.strtolower($this->visible);
  }
}

// --------

function MakePostFromArray($aPost)
{
  if ( !is_array($aPost) ) die('cPost->MakeFromArray: aPost is not an array');
  foreach ($aPost as $strKey => $oValue) {
  switch ($strKey) {
    case 'id':       $this->id = intval($oValue); break;
    case 'name':     $this->name = $oValue; break;
    case 'userage':  $this->userage = intval($oValue); if ($this->userage<0 ) $this->userage = 0; break;
    case 'useremail':$this->useremail = $oValue; break;
    case 'issuedate':$this->issuedate = $oValue; break;
    case 'postfrom': $this->postfrom = $oValue; break;
    case 'visible':  $this->visible = $oValue; break;
    case 'message':  $this->message = $oValue; break;
    case 'textmsg':  $this->message = $oValue; break; // required for firebird
    case 'x':        $this->x = floatval($oValue); break;
    case 'y':        $this->y = floatval($oValue); break;
    case 'z':        $this->z = floatval($oValue); break;
    case 'ip':       $this->ip = $oValue; break;
  }}
}

//----------

function CheckMessage($max_size=5000,$min_char=false,$min_word=false)
{
  // check shortness
  if ( $min_char ) { if ( strlen($this->message)<$min_char ) return False; }
  if ( $min_word ) { $str = strtolower(substr($this->message,0,100)); $iWords = str_word_count($str); if ( $iWords < $min_word ) return False; }
  // check longness
  if ( strlen($this->message)>$max_size ) return False;
  // end
  return True;
}

//----------

function CheckMeaning()
{
  // no message
  if ( strlen($this->message)<2 ) return false; 
  // no letters  
  $str = strtolower(substr($this->message,0,200));
  if ( !preg_match('/[a-z]+/',$str) ) return false;
  // check vowels exists
  $str = strtr($str,'éèêëÉÈÊË'  ,'eeeeeeee');
  $str = strtr($str,'áàâäÁÀÂÄÅå','aaaaaaaaaa');
  $str = strtr($str,'íìîïÍÌÎÏ'  ,'iiiiiiii');
  $str = strtr($str,'óòôöÓÒÔÖõÕ','oooooooooo');
  $str = strtr($str,'úùûüÚÙÛÜ'  ,'uuuuuuuu'); 
  if ( !preg_match('/[aeiouy]+/',$str) ) return false;
  // check random keyboard
  $arrWords = str_word_count($str,1);
  $i=0;
  foreach ( $arrWords as $strWord) {
  foreach ( array('qwe','wqe','qsd','sdf','dfg','fgh','jkl','klm','ghg','fjf','jfj','fgf','dfd','fdj','dfj') as $a ) {
    if ( strpos($strWord,$a)===false ) continue;
    $i++;
  }}  
  if ( $i>1 ) return false;
  // check multiple (4 and some 3)
  $str = substr($str,0,50);
  foreach ( array('a','b','c','d','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z') as $a )
  {
    $a = $a.$a.$a.$a;
    if ( strpos($str,$a)===false ) continue;
    return false;
  }
  foreach ( array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','u','v','z') as $a )
  {
    $a = $a.$a.$a;
    if ( strpos($str,$a)===false ) continue;
    return false;
  }
  // check at least 1/2 words contains vowel
  $intWords = count($arrWords);
  $i=0;
  foreach ($arrWords as $strWord)
  {
    if ( preg_match('/[aeiouy]+/',$strWord) ) $i++;
  }
  if ( $i==0 ) return false;
  if ( $i/$intWords<0.5 ) return false;
  // end control
  Return True;
}

//----------

function CheckWording($voc=array())
{
  if (count($voc)>0)
  {
    $str = strtolower(substr($this->message,0,1000));
    $words = str_word_count($str,1);
    $intwords = array_intersect($voc['good'],$words);
    if (count($intwords)==0) Return FALSE;
    $intwords = array_intersect($voc['bad'],$words);
    $ibad = count($intwords);
    if ($ibad>1) Return FALSE;
  }
  Return True;
}

// --------

function Insert()
{
  global $oDB;

  if ( is_a($oDB,'cDT') )
  {
    $this->id = date('Ymd-His').'-'.$oDB->ip.'-y';
    $strPath = $oDB->GetPath($this->id);
    $strFilename = $strPath.$this->id.'.txt';
    $strTxt  = 'name '.$this->name."\r\n";
    $strTxt .= 'userage '.(empty($this->userage) ? '-1' : $this->userage)."\r\n";
    $strTxt .= 'useremail '.(empty($this->useremail) ? '' : $this->useremail)."\r\n";
    $strTxt .= 'postfrom '.(empty($this->postfrom) ? '' : $this->postfrom)."\r\n";
    $strTxt .= 'x '.(empty($this->x) ? '0' : $this->x)."\r\n";
    $strTxt .= 'y '.(empty($this->y) ? '0' : $this->y)."\r\n";
    $strTxt .= 'z '.(empty($this->z) ? '0' : $this->z)."\r\n";
    $strTxt .= 'message '.$this->message;

    if ( !is_dir($strPath) ) $error = $oDB->MakeDir($strPath);

    if ( empty($error) )
    {
      if ( !$handle=fopen($strFilename, 'w') ) $error="Impossible to open the file [$strFilename].";
    }
    if ( empty($error) )
    {
      if ( fwrite($handle,$strTxt)===FALSE ) $error="Impossible to write into the file [$strFilename].";
      fclose($handle);
    }
    if ( !empty($error) ) echo '<div class="setup_err">',$error,'</div>';
  }
  else
  {
    $this->id = $oDB->Nextid(TABMESSAGE);    
    $oDB->Query( 'INSERT INTO '.TABMESSAGE.' (id,name,'.($oDB->type=='ibase' ? 'textmsg' : 'message').',issuedate,userage,useremail,postfrom,ip,x,y,z,visible) VALUES ('.$this->id.',"'.$this->name.'","'.$this->message.'","'.date('YmdHis').'",'.(!empty($this->userage) ? $this->userage : 'NULL').',"'.(!empty($this->useremail) ? $this->useremail : '').'","'.(!empty($this->postfrom) ? $this->postfrom : '').'","'.$oDB->ip.'",'.$this->x.','.$this->y.',0,"Y")' );
  }
}

//----------

function Delete()
{
  global $oDB;
  if ( is_a($oDB,'cDT') )
  {
  // get month path
  $strPath = $oDB->GetPath($this->id);
  if ( file_exists($strPath.$this->id.'.txt') ) unlink($strPath.$this->id.'.txt');
  }
  else
  {
  $oDB->Query('DELETE FROM '.TABMESSAGE.' WHERE id='.$this->id);
  }
}

//----------

function Update()
{
  global $oDB;
  if ( is_a($oDB,'cDT') )
  {
  $this->Delete();
  $this->Insert();
  }
  else
  {
  $oDB->Query( 'DELETE FROM '.TABMESSAGE.' WHERE id='.$this->id );
  $oDB->Query( 'INSERT INTO '.TABMESSAGE.' (id,name,'.($oDB->type=='ibase' ? 'textmsg' : 'message').',issuedate,userage,useremail,postfrom,ip,x,y,z,visible) VALUES ('.$this->id.',"'.$this->name.'","'.$this->message.'","'.date('YmdHis').'",'.(!empty($this->userage) ? $this->userage : 'NULL').',"'.(!empty($this->useremail) ? $this->useremail : '').'","'.(!empty($this->postfrom) ? $this->postfrom : '').'","'.$oDB->ip.'",'.$this->x.','.$this->y.',0,"Y")' );
  }
}

//----------

function SetStatus($str='Y')
{
  global $oDB;
  if ( is_a($oDB,'cDT') )
  {
  $strPath = $oDB->GetPath($this->id);
  if ( file_exists($strPath.$this->id.'.txt') ) rename($strPath.$this->id.'.txt',$strPath.substr($this->id,0,-1).strtolower($str).'.txt');
  }
  else
  {
  $oDB->Query('UPDATE '.TABMESSAGE.' SET visible="'.$str.'" WHERE id='.$this->id);
  }
}

//--- [i] ---

function Hash($str)
{
  // hexdec(x2) of each bock and use g and h as separators
  if ( is_int($str) ) return $str;
  if ( empty($str) ) return $str;
  $str = str_replace(array('-','.','y','n'),array('g','h','1','0'),$str);
  $arr = explode('g',$str);
  $ip = explode('h',$arr[2]); for($i=0;$i<count($ip);$i++) $ip[$i] = dechex(intval($ip[$i])*2);
  $arr[0] = dechex(intval($arr[0])*2);
  $arr[1] = dechex(intval($arr[1])*2);
  return $arr[0].'g'.$arr[1].'g'.implode('h',$ip).'g'.$arr[3];
}
function Unhash($str)
{
  if ( is_int($str) ) return $str;
  if ( empty($str) ) return $str;
  $arr = explode('g',$str);
  $arr[0] = strval(hexdec($arr[0])/2);
  $arr[1] = substr('000000'.strval(hexdec($arr[1])/2),-6);
  $ip = explode('h',$arr[2]); for($i=0;$i<count($ip);$i++) $ip[$i] = strval(hexdec($ip[$i])/2);
  $arr[2] = implode('.',$ip);
  $arr[3] = ($arr[3]=='1' ? 'y' : 'n');
  return implode('-',$arr);
}

//----------

}

?>
Return current item: Quick Guestbook