$conf["SITE_NAME"] = "Dore Designs Resources";
$conf["SITE_URL"] = "localhost/";
$conf["SITE_DESC"] = "Dore Designs Exquisite Gowns";
$conf["LINKS_PER_PAGE"] = "200";
$conf["MAILS_PER_PAGE"] = "20";
$conf["ENABLE_HITS"] = "0";
$conf["CATS_PER_ROW"] = "2";
$conf["ADMIN_EMAIL"] = "";
$conf["DB_HOST"] = "localhost";
$conf["DB_USER"] = "doredesi_dore";
$conf["DB_PASS"] = "dd9dd8dd7";
$conf["DB_NAME"] = "doredesi_dir";
?>
/**
* Class: class.Category.php
*
* Category Data Object.
*
* This is a specialisation ot the DataObject that is an abstract
* representation of a Category from the database.
*
*/
require_once('class.DataObject.php');
class Category extends DataObject {
/**
* Some of the object properties
*/
var $title;
var $description;
var $parent;
var $date;
var $accepted;
/**
* Constructor method
* Initializes the Category DataObject
*
* @param $dbh database handler
* @param $params
*/
function Category($dbh, $params) {
$params['table'] = 'Category';
$params['table'] = $params['table'] ? $params['table'] : 'Category';
// $params['where'] = "accepted =1";
$this->DataObject($dbh, $params);
}
/**
* Default method for presentation
*
* @param object $tpl reference to template object
* @param boolean $is_home true if this is root category
* @return string formatted output
*/
function view( &$tpl, $mode = 'view', $override = 0 ) {
global $dbh, $HTTP_SERVER_VARS;
//echo "
".func_num_args()."
";
/*
if ( $edit ) {
$tpl->set_file('category', 'edit_category.tpl', 1);
$tpl->set_var('ERROR', '');
} else {
$tpl->set_file('category', 'view_category.tpl', 1);
}
*/
//echo $this->getMode() . $this->mode;
//if(func_num_args() > 1) {
// $
//}
if(func_num_args() == 1 && !empty($this->mode)) {
$mode = $this->mode;
}
switch($mode){
case 'view':
$file = (empty($override)) ? 'view_category.tpl' : $override;
$tpl->set_file('category', $file, 1);
break;
case 'preview':
$file = (empty($override)) ? 'preview_category.tpl' : $override;
$tpl->set_file('category', $file, 1);
break;
case 'edit':
$file = (empty($override)) ? 'edit_category.tpl' : $override;
$tpl->set_file('category', $file, 1);
//$tpl->set_var('ERROR', $this->getError());
$tpl->set_file('error', 'error_message.tpl', 1);
if(!empty($this->error)) {
$tpl->set_var('ERROR_MESSAGE', $this->error);
$tpl->process('BLOCK_ERROR_MESSAGE', 'block_error_message');
} else {
$tpl->set_var('BLOCK_ERROR_MESSAGE', '');
}
$tpl->process('ERROR', 'error');
break;
}
//$id = $this->getProperty('id');
//$url = $this->getProperty('url');
if($mode == 'edit') {
$out = '';
$params['classname'] = "Category";
//$params[where] = "category = " . (($id) ? $id : 0) . " AND accepted = 1";
$params['orderby'] = "parent";
$objects = Category::getTreeArray($dbh, $params);
//print_r($objects);
/*
if(is_array($objects) && count($objects) > 0) {
while(list($key, $obj) = each($objects)){
//print_r($obj->getParents());
//$out .= '';
}
}*/
$out .= Category::makeTreeBranch(0, $objects, 0);
$tpl->set_var('PARENTS', $out);
}
$id = $this->getProperty('id') ? $this->getProperty('id') : '';
$title = $this->getProperty('title') ? $this->getProperty('title') : '';
$description = $this->getProperty('description') ? $this->getProperty('description') : '';
$accepted = $this->getProperty('accepted') ? '0' : '1';
$tpl->set_var('OUT_CAT', $_SERVER['PHP_SELF'] . '?c=' . $id);
$tpl->set_var('CAT_ID', $id);
$tpl->set_var('CAT_TITLE', $title);
$tpl->set_var('CAT_DESCRIPTION', nl2br($description) );
$tpl->set_var('CAT_ACCEPTED', $accepted );
//$tpl->set_var('LINK_URL', $url);
//$tpl->set_var('LINK_DATE', date( "M j, Y", $this->getProperty('date') ));
//$tpl->set_var('CAT_DATE', date("M j, Y", strtotime($this->getProperty('date'))) );
/*
*TODO add Hits to Link
*/
$tpl->process('BLOCK_CAT_INFO', 'block_cat_info');
return $tpl->process('CATEGORIES', 'category');
}
/**
* Method for presentation of the category header
*
* @param object $tpl reference to template object
* @return string formatted output
*/
function view_path(&$tpl){
global $dbh;
$out = '';
$id = $this->getProperty('id');
if($id) {
$tpl->set_file('subcat', 'view_sub_category.tpl');
// setting URL
$tpl->set_var('SUBCAT_URL', '?c='.$id);
// setting title
$tpl->set_var('SUBCAT_TITLE', $this->getProperty('title') );
//setting description
$tpl->set_var('SUBCAT_DESCRIPTION', $this->getProperty('description') );
/*
*TODO add count number
*/
// output
$out = $tpl->process('out', 'subcat');
// free memory
$tpl->drop_var('subsubcat');
}
return $out;
}
function getParents($db = null) {
// returns ref to array of object parent nodes up to root
global $dbh;
if (!is_null($db)) {
$dbh = $db;
}
$parents = array();
if($this->getProperty('parent') != 0) {
$parent_id = $this->getProperty('parent');
$params['classname'] = 'Category';
$parent = new Category($dbh, $params);
$parent->setId($parent_id);
$parent->load();
$parents = $parent->getParents();
$parents[] = $parent;
}
return $parents;
}
function getParentsArray($prop = 'title', $db = null) {
// returns ref to array of object parent nodes up to root
global $dbh;
if (!is_null($db)) {
$dbh = $db;
}
$parents = array();
if($this->getProperty('parent') != 0) {
$parent_id = $this->getProperty('parent');
$params['classname'] = 'Category';
$parent = new Category($dbh, $params);
$parent->setId($parent_id);
$parent->load();
$parents = $parent->getParentsArray($prop);
$parents[] = $parent->getProperty($prop);
}
return $parents;
}
function getTreeArray($dbh, $params='') {
$params['classname'] = "Category";
// $params['where'] = "category = " . (($id) ? $id : 0) . " AND accepted = 1";
$objects = Category::getObjects($dbh, $params);
//print_r($objects);
/*
if(is_array($objects) && count($objects) > 0) {
while(list($key, $obj) = each($objects)){
}
}*/
//$out = '';
for( $i = 0; $i < count($objects); $i++){
$obj = $objects[$i];
$table[$obj->getProperty('parent')][$obj->getProperty('id')] = $obj->getProperty('title');
}
return $table;
}
function action() {
global $HTTP_POST_VARS;
//print_r($this);
if( isset($HTTP_POST_VARS['action']) )
$action = $HTTP_POST_VARS['action'];
else
return $this;
switch($action){
case 'save':
//$this->title = $HTTP_POST_VARS['title'];
//$this->url = $HTTP_POST_VARS['url'];
if($this->validate() || !empty($_POST['ignore_error'])) {
$this->save($this->getId());
$this->setMode('preview');
}
break;
case 'delete':
//$this->delete();
break;
}
return $this;
}
/**
* Validation method
* Check for duplicates
*/
function validate() {
global $dbh, $tpl;
$error = '';
if(empty($this->title)) {
$error .= "Title field is mandatory";
}
$this->setError($error);
return empty($error) ? 1 : 0;
}
function makeTreeBranch($parent, $table, $level){
//print_r($table);
$list=$table[$parent];
//asort($list); // here we do the sorting
$result = '';
while(list($key,$val)=each($list)){
// do the indent
if ($level=="0"){
$out = ' ';
}else{
$out = str_repeat(' ', $level) . ' - ';
}
// the resulting HTML - feel free to change it
// $level is optional
$chk = ($key == $this->getProperty('parent')) ? ' selected' : '';
$result.= "$output ";
if (isset($table[$key])){
$result.= Link::makeTreeBranch($key, $table, $level+1);
}
}
return $result;
}
/**
* Returns an formatted array of objects
* usefull for template loops etc.
*/
function getObjectsArray($dbh, $params='') {
$table = array();
$params['classname'] = "Category";
//$params[where] = "category = " . (($id) ? $id : 0) . " AND accepted = 1";
$objects = Category::getObjects($dbh, $params);
for( $i = 0; $i < count($objects); $i++) {
$obj = $objects[$i];
$parent = $obj->getParent($dbh);
$table[$i] = array(
'id' => $obj->getProperty('id'),
'title' => $obj->getProperty('title'),
'description' => $obj->getProperty('description'),
'parent' => ($parent->getProperty('title') ? $parent->getProperty('title') : 'Home'),
// 'accepted' => 1,
);
//[$obj->getProperty('id')] = $obj->getProperty('title');
}
//print_r($table);
return $table;
}
/**
* Returns an formatted array of objects
* usefull for template loops etc.
*/
function getParent() {
global $dbh;
//$table = array();
//$params[classname] = "Category";
//$params[where] = "category = " . $this->getProperty('category');
$params['table'] = "Category";
$params['classname'] = "Category";
$obj = new Category($dbh, $params);
$obj->setId($this->getProperty('parent'));
$obj->load();
return $obj;
}
}
?>
/**
* Class: class.Link.php
*
* Link Data Object.
*
* This is a specialisation ot the DataObject that is an abstract
* representation of a Link from the database.
*
*/
require_once('class.DataObject.php');
class Link extends DataObject {
var $title;
var $url;
var $description;
var $date;
var $category;
var $accepted;
var $name;
var $email;
var $recpr_link;
/**
* Constructor method
* Initializes the Link DataObject
*
* @param object $dbh database handler
* @param $params
*/
function Link($dbh, $params) {
$params['table'] = $params['table'] ? $params['table'] : 'Link';
//$params[classname] = $params[classname] ? $params[classname] : 'Link';
$this->DataObject($dbh, $params);
}
/**
* Simple method for presentation
*
* @param boolean $html to format as HTML
* @return string formatted output
*/
function view_plain( $html = 0 ){
if($html){
$sep = '
';
$b_start = '';
$b_end = '';
}
else {
$sep = "\n";
$b_start = '';
$b_end = '';
}
$text = $sep;
$text .= $b_start . "Link Title:" . $b_end . $sep . $this->getProperty('title') . $sep;
$text .= $b_start . "URL:" . $b_end . $sep . $this->getProperty('url') . $sep;
$text .= $b_start . "Description:" . $b_end . $sep . $this->getProperty('description') . $sep;
$text .= $b_start . "Submited by:" . $b_end . $sep . $this->getProperty('name') . $sep;
$text .= $b_start . "Submited by-Email:" . $b_end . $sep . $this->getProperty('email') . $sep;
$text .= $b_start . "Reciprocal Link:" . $b_end . $sep . $this->getProperty('recpr_link') . $sep;
return $text;
}
/**
* Default method for presentation
*
* @param object $tpl reference to template object
* @param boolean $is_home true if this is root category
* @return string formatted output
*/
function view( &$tpl, $mode = 'view', $override = 0 ) {
global $dbh, $HTTP_SERVER_VARS, $conf;
if(!empty($this->mode)) {
$mode = $this->mode;
}
switch($mode){
case 'view':
$file = (empty($override)) ? 'view_link.tpl' : $override;
$tpl->set_file('link', $file, 1);
break;
case 'preview':
$file = (empty($override)) ? 'preview_link.tpl' : $override;
$tpl->set_file('link', $file, 1);
break;
case 'edit':
$file = (empty($override)) ? 'edit_link.tpl' : $override;
$tpl->set_file('link', $file, 1);
//$tpl->set_var('ERROR', $this->getError());
$tpl->set_file('error', 'error_message.tpl', 1);
if(!empty($this->error)) {
$tpl->set_var('ERROR_MESSAGE', $this->error);
$tpl->process('BLOCK_ERROR_MESSAGE', 'block_error_message');
} else {
$tpl->set_var('BLOCK_ERROR_MESSAGE', '');
}
$tpl->process('ERROR', 'error');
break;
}
if($mode == 'edit') {
$out = '';
$params['classname'] = "Category";
//$params[where] = "category = " . (($id) ? $id : 0) . " AND accepted = 1";
$params['orderby'] = "parent";
$objects = Category::getTreeArray($dbh, $params);
//print_r($objects);
/*
if(is_array($objects) && count($objects) > 0) {
while(list($key, $obj) = each($objects)){
//print_r($obj->getParents());
//$out .= '';
}
}*/
$out .= Link::makeTreeBranch(0, $objects, 0);
$tpl->set_var('CATEGORIES', $out);
}
$id = $this->getProperty('id') ? $this->getProperty('id') : '';
$title = $this->getProperty('title') ? $this->getProperty('title') : '';
$url = $this->getProperty('url') ? $this->getProperty('url') : '';
$description = $this->getProperty('description') ? $this->getProperty('description') : '';
$accepted = $this->getProperty('accepted') ? '0' : '1';
$name = $this->getProperty('name') ? $this->getProperty('name') : '';
$email = $this->getProperty('email') ? $this->getProperty('email') : '';
$recpr_link = $this->getProperty('recpr_link') ? $this->getProperty('recpr_link') : '';
$tpl->set_var('OUT_LINK', $url);
if($conf["ENABLE_HITS"]) {
$tpl->set_var('OUT_LINK', "go.php?id=" . $id );
}
$tpl->set_var('LINK_ID', $id);
$tpl->set_var('LINK_TITLE', $title);
$tpl->set_var('LINK_DESCRIPTION', nl2br($description) );
$tpl->set_var('LINK_URL', $url);
$tpl->set_var('LINK_NAME', $name);
$tpl->set_var('LINK_EMAIL', $email);
$tpl->set_var('LINK_RECPR_LINK', $recpr_link);
//$tpl->set_var('LINK_DATE', date( "M j, Y", $this->getProperty('date') ));
$tpl->set_var('LINK_DATE', date("M j, Y", strtotime($this->getProperty('date'))) );
$tpl->set_var('LINK_ACCEPTED', $accepted );
/*
*TODO add Hits to Link
*/
$tpl->set_var('LINK_HITS', " " );
if($conf["ENABLE_HITS"]) {
$tpl->set_var('LINK_HITS', "Hits: " . $this->getHits() );
}
$tpl->process('BLOCK_LINK_INFO', 'block_link_info');
return $tpl->process('LINKS', 'link');
}
function action() {
global $HTTP_POST_VARS;
if( isset($HTTP_POST_VARS['action']) )
$action = $HTTP_POST_VARS['action'];
else
return $this;
switch($action){
case 'save':
//$this->title = $HTTP_POST_VARS['title'];
//$this->url = $HTTP_POST_VARS['url'];
if($this->validate() || !empty($_POST['ignore_error'])) {
$this->save();
$this->setMode('preview');
}
break;
case 'delete':
//$this->delete();
break;
}
return $this;
}
/**
* Validation method
* Check for duplicates
*/
function validate() {
global $dbh, $tpl;
//$tpl->set_file('error', 'error_message.tpl', 1);
$error = '';
if(empty($this->title) || empty($this->url)||empty($this->name)||empty($this->email)||empty($this->recpr_link)) {
$error .= "URL, Title, Name, Email and Reciprocal Link fields are mandatory";
} else {
$params['classname'] = 'Link';
$params['where'] = "accepted=1 AND (url = '" . $this->url . "' OR title = '" . $this->title . "')";
$objects = Link::getObjects($dbh, $params);
//print_r($objects);
if(is_array($objects) && count($objects) > 0) {
$obj = $objects[0];
//echo"";print_r($obj);echo "
";
//while(list($key, $obj) = each($objects)){
$error .= "Listing
";
if(strcasecmp($obj->getProperty('title'), $this->getProperty('title')) == 0 ) {
$error .= 'This Site Title Already exists in the Directory';
}
if(strcasecmp($obj->getProperty('url'), $this->getProperty('url')) == 0 ) {
$error .= 'This link already exists in the directory';
}
//}
}
}
$this->setError($error);
/*
if(!empty($error)) {
$tpl->set_var('ERROR_MESSAGE', $error);
$tpl->process('BLOCK_ERROR_MESSAGE', 'block_error_message');
} else {
$tpl->set_var('BLOCK_ERROR_MESSAGE', '');
}
$tpl->process('ERROR', 'error');
*/
return empty($error) ? 1 : 0;
}
function makeTreeBranch($parent, $table, $level){
//print_r($table);
$list=$table[$parent];
//asort($list); // here we do the sorting
while(list($key,$val)=each($list)){
// do the indent
if ($level=="0"){
$out = '';
}else{
$out = str_repeat(' ', $level) . ' - ';
}
// the resulting HTML - feel free to change it
// $level is optional
$chk = ($key == $this->getProperty('category')) ? ' selected' : '';
$result.= "$output ";
if (isset($table[$key])){
$result.= Link::makeTreeBranch($key, $table, $level+1);
}
}
return $result;
}
/**
* Returns an formatted array of objects
* usefull for template loops etc.
*/
function getObjectsArray($dbh, $params='') {
$table = array();
$params['classname'] = "Link";
//$params[where] = "category = " . (($id) ? $id : 0) . " AND accepted = 1";
$objects = Link::getObjects($dbh, $params);
for( $i = 0; $i < count($objects); $i++){
$obj = $objects[$i];
$parent = $obj->getParent($dbh);
$table[$i] = array(
'id' => $obj->getProperty('id'),
'title' => $obj->getProperty('title'),
'url' => $obj->getProperty('url'),
'category' => $parent->getProperty('title'),
'description' => $obj->getProperty('description'),
'name' => $obj->getProperty('name'),
'email' => $obj->getProperty('email'),
'recpr_link' => $obj->getProperty('recpr_link'),
'date' => date("M j, Y h:i:s A", strtotime($obj->getProperty('date'))),
);
//[$obj->getProperty('id')] = $obj->getProperty('title');
}
//print_r($table);
return $table;
}
/**
* Returns an formatted array of objects
* usefull for template loops etc.
*/
function getParent($db = null) {
global $dbh;
if (!is_null($db)) {
$dbh = $db;
}
//$table = array();
//$params[classname] = "Category";
//$params[where] = "category = " . $this->getProperty('category');
$params['table'] = "Category";
$params['classname'] = "Category";
$obj = new Category($dbh, $params);
$obj->setId($this->getProperty('category'));
$obj->load();
return $obj;
}
/* validateURL($href)
*
* Check if a link is online or offline.
* This script assumes that the url has a valid written value
*/
function validateURL(){
$url = $this->getProperty('url');
if(substr($url, 0, 7) != "http://")
$url = "http://" . $url;
//process url
$parts = parse_url($url);
//print_r($parts);
//Check the link
$fp = fsockopen($parts['host'], 80, $errno, $errstr, 30);
//Return
if (!$fp) {
//echo "BAD HOST";
//return false;
return array(
'host' => 'NOT FOUND',
'link' => '?',
'msg' => 'Could not connect to host',
);
}
else {
//echo "GOOD HOST";
if(!$parts[path]) {
$parts[path] = '/';
}
//HTTP request
$out = "GET $parts[path] HTTP/1.1\r\n";
$out .= "Host: $parts[host]\r\n";
$out .= "Connection: Close\r\n\r\n";
fputs($fp, $out);
//GET returned header
$res = fgets( $fp, 256 );
//echo "
".$res."
";
$res_arr = split(" ", $res);
//print_r($res_arr);
if($res_arr[1] == 200) {
$status = 'OK';
} else {
$status = 'BAD';
}
fclose($fp);
return array(
'host' => 'FOUND',
'link' => $status,
'msg' => $res,
);
//return true;
}
}
function validate_URL($url)
{
if(!ereg("^http://",$url))
$url="http://".$url;
$urlArray = parse_url($url);
if (!$urlArray[port]) {
if ($urlArray[scheme] == 'http') { $urlArray[port] = 80; }
elseif ($urlArray[scheme] == 'https') { $urlArray[port] = 443; }
elseif ($urlArray[scheme] == 'ftp') { $urlArray[port] = 21; }
}
if (!$urlArray[path]) { $urlArray[path] = '/'; }
$errno="";
$errstr="";
$fp = @fsockopen ($urlArray[host].'.', $urlArray[port], &$errno, &$errstr , 10);
$sstatus = "OK";
if (!$fp)
return false;
$req=sprintf( "HEAD %s HTTP/1.0\r\nHost: %s\r\n\r\n", $urlArray[path], $urlArray[host]);
fputs( $fp, $req );
while (!feof($fp))
{
$line = fgets($fp,1000);
if( eregi( "HTTP/1.(.) ([0-9]*) (.*)", $line, $parts ) )
{
if( $parts[2] < "400" )
{
fclose($fp);
return true;
}
else
{
fclose($fp);
return false;
}
}
}
fclose($fp);
return false;
}
/*
* Add Hit to link
*/
function addHit() {
global $dbh, $HTTP_SERVER_VARS;
//init the hit object
$params['table'] = "Hit";
$params['classname'] = "Hit";
$hit = new Hit($dbh, $params);
//set hit link prop
$hit->link = $this->id;
/*
$hit->ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
$hit->host = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
$hit->agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
$hit->date = 'now()';
*/
$hit->init();
$hit->save();
}
/*
* Get Hit to link
*/
function getHits() {
global $dbh, $HTTP_SERVER_VARS;
//init the hit object
$params['table'] = "Hit";
$params['classname'] = "Hit";
$params['where'] = "link=$this->id";
$hits = Hit::getObjectsCount($dbh, $params);
return $hits;
}
}
?>
/**
* Class: class.Category.php
*
* Category Data Object.
*
* This is a specialisation ot the DataObject that is an abstract
* representation of a Hit from the database.
*
*/
require_once('class.DataObject.php');
class Hit extends DataObject {
/**
* Some of the object properties
*/
var $link;
var $ip;
var $host;
var $agent;
var $date;
/**
* Constructor method
* Initializes the Hit DataObject
*
* @param $dbh database handler
* @param $params
*/
function Hit($dbh, $params) {
$params[table] = $params[table] ? $params[table] : 'Hit';
$this->DataObject($dbh, $params);
}
function init() {
global $HTTP_SERVER_VARS;
//set hit props
$this->ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
$this->host = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
$this->agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
$this->date = 'now()';
}
/**
* Default method for presentation
*
* @param object $tpl reference to template object
* @param boolean $is_home true if this is root category
* @return string formatted output
*/
function view( &$tpl, $mode = 'view', $override = 0 ) {
global $dbh, $HTTP_SERVER_VARS;
//echo "".func_num_args()."
";
/*
if ( $edit ) {
$tpl->set_file('category', 'edit_category.tpl', 1);
$tpl->set_var('ERROR', '');
} else {
$tpl->set_file('category', 'view_category.tpl', 1);
}
*/
//echo $this->getMode() . $this->mode;
//if(func_num_args() > 1) {
// $
//}
if(func_num_args() == 1 && !empty($this->mode)) {
$mode = $this->mode;
}
switch($mode){
case 'view':
$file = (empty($override)) ? 'view_hit.tpl' : $override;
$tpl->set_file('hit', $file, 1);
break;
case 'preview':
$file = (empty($override)) ? 'preview_hit.tpl' : $override;
$tpl->set_file('hit', $file, 1);
break;
case 'edit':
$file = (empty($override)) ? 'edit_hit.tpl' : $override;
$tpl->set_file('hit', $file, 1);
//$tpl->set_var('ERROR', $this->getError());
$tpl->set_file('error', 'error_message.tpl', 1);
if(!empty($this->error)) {
$tpl->set_var('ERROR_MESSAGE', $this->error);
$tpl->process('BLOCK_ERROR_MESSAGE', 'block_error_message');
} else {
$tpl->set_var('BLOCK_ERROR_MESSAGE', '');
}
$tpl->process('ERROR', 'error');
break;
}
$id = $this->getProperty('id') ? $this->getProperty('id') : '';
$link = $this->getProperty('id') ? $this->getProperty('link') : '';
$ip = $this->getProperty('id') ? $this->getProperty('ip') : '';
$host = $this->getProperty('id') ? $this->getProperty('host') : '';
$browser = $this->getProperty('id') ? $this->getProperty('browser') : '';
$os = $this->getProperty('id') ? $this->getProperty('os') : '';
//$id = $this->getProperty('id') ? $this->getProperty('id') : '';
$tpl->set_var('OUT_CAT', $_SERVER['PHP_SELF'] . '?c=' . $id);
$tpl->set_var('HIT_ID', $id) ;
$tpl->set_var('HIT_LINK', $link );
$tpl->set_var('HIT_IP', $ip );
$tpl->set_var('HIT_HOST', $host );
$tpl->set_var('HIT_BROWSER', $browser );
$tpl->set_var('HIT_OS', $os );
$tpl->set_var('HIT_DATE', date("M j, Y", strtotime($this->getProperty('date'))) );
$tpl->process('BLOCK_HIT_INFO', 'block_hit_info');
return $tpl->process('HITS', 'hit');
}
function action() {
global $HTTP_POST_VARS;
//print_r($this);
if( isset($HTTP_POST_VARS['action']) )
$action = $HTTP_POST_VARS['action'];
else
return $this;
switch($action){
case 'save':
//$this->title = $HTTP_POST_VARS['title'];
//$this->url = $HTTP_POST_VARS['url'];
if($this->validate() || !empty($_POST['ignore_error'])) {
$this->save($this->getId());
$this->setMode('preview');
}
break;
case 'delete':
//$this->delete();
break;
}
return $this;
}
/**
* Validation method
* Check for duplicates
*/
function validate() {
global $dbh, $tpl;
$error = '';
if(empty($this->link)) {
$error .= "Hit is applied to a link";
}
$this->setError($error);
return empty($error) ? 1 : 0;
}
/**
* Returns an formatted array of objects
* usefull for template loops etc.
*/
function getObjectsArray($dbh, $params='') {
$table = array();
$params[classname] = "Hit";
//$params[where] = "link = " . (($id) ? $id : 0) . " AND accepted = 1";
$objects = Hit::getObjects($dbh, $params);
for( $i = 0; $i < count($objects); $i++) {
$obj = $objects[$i];
//$parent = $obj->getLink();
$table[$i] = array(
'id' => $obj->getProperty('id'),
//'link' => $obj->getProperty('title'),
'ip' => $obj->getProperty('description'),
'host' => $parent->getProperty('url'),
'browser' => $obj->getProperty('title'),
'os' => $obj->getProperty('title'),
'date' => $obj->getProperty('title'),
);
//[$obj->getProperty('id')] = $obj->getProperty('title');
}
//print_r($table);
return $table;
}
/**
* Returns the link object
*/
function getLink() {
global $dbh;
$params[table] = "Link";
$params[classname] = "Link";
$obj = new Link($dbh, $params);
$obj->setId($this->getProperty('link'));
if(is_resource($obj->load())) {
return $obj;
}
return null;
}
}
?>
/**
* Class: class.DB.php
*
* Database handler.
*
* This is a minimal abstraction layer providing an interface with MySQL.
* The main purpose of this layer is to take care of all the details like
* result error-checking and query debugging. If for some reason we decide
* to move to another database platform in the future, this will also make
* the switch easy.
*/
define ('_DEBUG', FALSE);
class DB
{
/**
* Hostname of the MySQL server.
*/
var $server;
/**
* MySQL username.
*/
var $user;
/**
* MySQL password.
*/
var $pass;
/**
* Name of the MySQL database.
*/
var $database;
/**
* *true* if a db connection is open, *false* otherwise.
*/
var $connected;
/**
* Set to a MySQL connection handle when a connection is opened.
*/
var $handle;
function DB()
{
$this->connected = false;
}
function disconnect()
{
@mysql_close($this->handle);
$this->connected = false;
}
/**
* Connect to database
*
* @return boolean. true for connected false otherwise
*/
function connect()
{
if ($this->connected === false)
{
$this->handle = mysql_connect($this->server, $this->user, $this->pass);// or die(mysql_error());
if ($this->handle === false)
{
$this->connected = false;
echo _DEBUG ? "Could not connect to database.
\n" : "";
return false;
}
else
{
$this->connected = true;
if (mysql_select_db($this->database, $this->handle))
{
return true;
}
else
{
echo _DEBUG ? mysql_error($this->handle)."
\n" : "";
}
}
}
else
{
return true;
}
}
/**
* Alias for query()
*
* @param string $query the query
* @return sql resultset or boolean telling that was executed or not
*/
function execute($query)
{
return $this->query($query);
}
/**
* Returns the next ID to insert into a table.
*
* TODO: some revisions
*
* @param string $table the table name
* @return sql resultset or boolean telling that was executed or not
*/
function nextId($table)
{
$result = $this->query("SELECT MAX(id)+1 FROM " . $table);
$row = mysql_fetch_assoc($result);
echo $row['id'];
return $row['id'];
}
/**
* Executes a database query and returns the results.
*
* @param string $query query to add LIMIT clause
* @return sql resultset or boolean telling that was executed or not
*/
function query($query)
{
if ($this->connect())
{
echo _DEBUG ? "$query
\n" : "";
$result = mysql_query($query, $this->handle);
if ($result === false)
{
echo _DEBUG ? mysql_error($this->handle)."
\n" : "";
return false;
}
else
{
if ($result === true)
{
return true;
}
elseif ($result !== false)
{
/* $rows = array();
while ($row = mysql_fetch_object($result))
$rows[] = $row;
mysql_free_result($result);
return $rows; */
return $result;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
/**
* Generate and execute a query adding a LIMIT clause
*
* @param string $query query to add LIMIT clause
* @param int $from record to start from
* @param int $count number of records to fetch
* @return sql resultset or boolean telling that was executed or not
*/
function limitQuery($query, $from, $count)
{
$query .= " LIMIT $from, $count";
return $this->query($query);
}
function importFile($file, $table, $term='|', $encl='"', $escp='\\', $line='\n') {
$query = sprintf("LOAD DATA LOCAL INFILE \"%s\" INTO TABLE `%s` FIELDS TERMINATED BY \"\%s\" OPTIONALLY ENCLOSED BY \"\%s\" ESCAPED BY \"\%s\" LINES TERMINATED BY \"%s\" ", $file, $table, $term, $encl, $escp, $line);
return $this->query($query);
}
function exportFile($file, $table, $term='|', $encl='"', $escp='\\', $line='\n') {
$query = sprintf("SELECT * INTO OUTFILE '%s' FIELDS TERMINATED BY '%s' OPTIONALLY ENCLOSED BY '%s' ESCAPED BY '%s' LINES TERMINATED BY '%s' FROM `%s`", $file, $term, $encl, $escp, $line, $table);
return $this->query($query);
}
/**
* Execute a query autogenerated by buidSQL()
*
* TODO: Some revisions
*
* @param string $table name of the table
* @param array $table_fields ordered array containing the fields names
* @param string $mode type of query to make (insert or update)
* @param string $where in case of update queries, this string will be put after the sql WHERE statement
* @return sql resultset or booean telling that was executed or not
*/
function autoExecute($table, $fields_values, $mode = "insert", $where = false)
{
$query = $this->buildSQL($table, $fields_values, $mode, $where);
//echo $query;
return $this->query($query);
}
/**
* Generate automaticaly an sql query from an array
*
* @param string $table name of the table
* @param array $table_fields ordered array containing the fields names
* @param string $mode type of query to make (insert or update)
* @param string $where in case of update queries, this string will be put after the sql WHERE statement
* @return string sql query string
*/
function buildSQL($table, $table_fields, $mode, $where = false)
{
if (count($table_fields) == 0) {
return "No field";
}
$first = true;
switch ($mode) {
case "insert":
$values = '';
$names = '';
while (list($key, $value) = each($table_fields)) {
if ($first) {
$first = false;
} else {
$names .= ',';
$values .= ',';
}
$names .= $key;
if($value == 'now()') {
$values .= $value;//'?';
} else {
$values .= '\''.$value.'\'';//'?';
}
}
return "INSERT INTO $table ($names) VALUES ($values)";
break;
case "update":
$set = '';
while (list($key, $value) = each($table_fields)) {
if ($first) {
$first = false;
} else {
$set .= ',';
}
if($value == 'now()') {
$set .= "$key = $value";
} else {
$set .= "$key = '$value'";
}
}
$sql = "UPDATE $table SET $set";
if ($where) {
$sql .= " WHERE $where";
}
return $sql;
break;
}
}
}
?>