<?php
// QuickTalk guestbook 2.5 build:20100731
// Note:
// Here after
// $host is the path to the repository directory
// $root is the repository directory (i.e. $host + "data/")
// $path is the root + the year and month sub-directories
// When using the default installation directory, $host remains "" (empty string) and $root is thus simply "data/".
// If you want to store the messages in an other directory:
// the fullpath of your storage location must be declared in the bin/qtg_init.php file: in the variable $qtg_host (with final /)
// Example: To store data in //storage/qtg/data/ you must declare $qtg_host = "//storage/qtg/" in the bin/qtg_init.php file
class cDT
{
var $id = -1;
var $type = 'file';
var $host;
var $root;
var $path;
var $ip;
var $arrFiles = array(); // list of files (without .txt)
var $curFiles = 0;
var $lastfile = '';
var $firstfile = '';
function cDT($qtg_host='')
{
$this->host = $qtg_host; // path to the repository (with final /). This value comes from the bin/qtg_init.php file
$this->root = $this->host.'data/';
$this->path = $this->root.date('Y').'/'.date('m').'/';
$this->ip = $_SERVER['REMOTE_ADDR'];
}
//----------
function FilesCount($option='',$intMax=0,$strStart='',$strEnd='',$bStats=false)
{
$arr = $this->GetFiles($option,$intMax,$strStart,$strEnd);
if ( count($arr)>0 && $bStats )
{
rsort($arr);
$this->lastfile = $arr[0];
$this->firstfile = $arr[count($arr)-1];
}
return count($arr);
}
//---------- this override $oDB->Getrow, it required $arrFiles is set before
function Getrow()
{
if ( !isset($this->arrFiles[$this->curFiles]) ) return false;
$row = array();
$strId = $this->arrFiles[$this->curFiles];
$arrFile = explode('-',$strId);
$strPath = $this->GetPath($strId);
$row['id'] = $this->curFiles;
$row['ip'] = $arrFile[2];
$row['issuedate'] = $arrFile[0].$arrFile[1];
$row['visible'] = strtoupper($arrFile[3]);
// Read file
$str = file_get_contents($strPath.$strId.'.txt');
$arrLines = explode("\n",$str,8);
foreach($arrLines as $strLine)
{
$strLine = trim($strLine); // required as the line can start with \n
if ( substr($strLine,0,4)=='name' ) { $row['name'] = trim(substr($strLine,4)); continue; }
if ( substr($strLine,0,7)=='userage' ) { $row['userage'] = trim(substr($strLine,7)); continue; }
if ( substr($strLine,0,9)=='useremail' ) { $row['useremail'] = trim(substr($strLine,9)); continue; }
if ( substr($strLine,0,8)=='postfrom' ) { $row['postfrom'] = trim(substr($strLine,8)); continue; }
if ( substr($strLine,0,1)=='x' ) { $row['x'] = trim(substr($strLine,1)); continue; }
if ( substr($strLine,0,1)=='y' ) { $row['y'] = trim(substr($strLine,1)); continue; }
if ( substr($strLine,0,1)=='z' ) { $row['z'] = trim(substr($strLine,1)); continue; }
if ( substr($strLine,0,7)=='message' ) $row['message'] = trim(substr($strLine,7));
}
$this->curFiles++;
return $row;
}
//----------
function GetPath($id)
{
if ( !is_string($id) ) die('GetPath: arg #1 must be a string');
return $this->root.substr($id,0,4).'/'.substr($id,4,2).'/';
}
//----------
function GetFiles($option='',$intMax=0,$strStart='',$strEnd='')
{
$this->arrFiles = array();
// Read directory. Return a list of file id (without .txt)
if ( empty($strStart) ) $strStart=date('Ym');
if ( strlen($strStart)==4 ) $strStart.='12';
$intYstart = intval(substr($strStart,0,4));
if ( empty($strEnd) ) $strEnd=strval($intYstart-5).'01';
if ( strlen($strEnd)==4 ) $strEnd.='01';
$intYend = intval(substr($strEnd,0,4));
$intMstart = intval(substr($strStart,4,2));
$intMend = intval(substr($strEnd,4,2));
$i=0;
for($intY=$intYstart;$intY>=$intYend;$intY--)
{
if ( !file_exists($this->root.$intY) ) continue;
for($intM=$intMstart;$intM>=$intMend;$intM--)
{
$strM = substr('00'.$intM,-2);
if ( !file_exists($this->root.$intY.'/'.$strM) ) continue;
$strPath = $this->root.$intY.'/'.$strM.'/';
$handle = opendir($strPath);
while(false!==($strFile=readdir($handle)))
{
if ( $strFile=='.' || $strFile=='..' ) continue;
if ( substr($strFile,-4,4)!='.txt' ) continue;
if ( $intMax>0 && $i>$intMax ) { closedir($handle); return $this->arrFiles;} // this breaks
if ( $i>10000 ) { closedir($handle); return $this->arrFiles; } // this breaks
$strFile = substr($strFile,0,-4);
if ( empty($option) ) { $this->arrFiles[]=$strFile; } else { if ( strstr($strFile,$option) ) $this->arrFiles[]=$strFile; }
$i++;
}
closedir($handle);
}
}
return $this->arrFiles;
}
//----------
function MakeDir($strPath)
{
// Returns '' (empty string), or a message in case of error
if ( !is_string($strPath) ) die('MakeDir: arg #1 must be a string');
if ( substr($strPath,-1,1)=='/' ) $strPath = substr($strPath,0,-1);
// Check dir
if ( is_dir($strPath) ) return "Cannot create already existing directory: $strPath";
// Search the starting directory (the last "data" in path)
$arr = explode('/',$strPath);
$arrData = array_keys($arr, 'data'); // find all "data" in the path
if ( empty($arrData) ) return "Cannot find the data directory in $strPath";
$intData = end($arrData); // find last "data"
// Get the year and month dir in the path
if ( !isset($arr[$intData+1]) ) return "Cannot find the year directory in $strPath";
if ( !isset($arr[$intData+2]) ) return "Cannot find the month directory in $strPath";
$strYear = $arr[$intData+1]; // year dir (next to the last data dir)
$strMonth = $arr[$intData+2]; // month dir (next to the year dir)
$strRoot = implode('/',array_slice($arr, 0, $intData+1)); // group all parts before the year
// Check the year directory
if ( !is_dir($strRoot.'/'.$strYear) )
{
if ( !mkdir($strRoot.'/'.$strYear) ) return "Cannot create the directory: $strRoot/$strYear"; // make the year directory
}
// Check the month directory
if ( !is_dir($strRoot.'/'.$strYear.'/'.$strMonth) )
{
if ( !mkdir($strRoot.'/'.$strYear.'/'.$strMonth) ) return "Cannot create the directory: $strRoot/$strYear/$strMonth"; // make the month directory
}
return '';
}
//----------
}
?>