<?php
// $Id: rssc_link_handler.php,v 1.10 2006/09/19 03:18:30 ohwada Exp $

// 2006-09-18 K.OHWADA
// move build_error_rssurl_list() from rssc_link_exist_handler
// change _build_insert_sql() get_list_by_rssurl()

// 2006-07-10 K.OHWADA
// use happy_linux_object happy_linux_object_handler

// 2006-06-04 K.OHWADA
// add build_show(), get_export_channel()
// move refreshExpired(), update_xml_url(), etc to link_basic
// move _sanitize_link(), etc to xml_object
// use get_cache()
// suppress notice : Only variable references should be returned by reference

// 2006-01-20 K.OHWADA
// small change

//=========================================================
// Rss Center Module
// this file contain 2 class
//   rssc_link
//   rssc_link_handler
// 2006-01-01 K.OHWADA
//=========================================================

// === class begin ===
if( !class_exists('rssc_link_handler') ) 
{

//=========================================================
// class link
//=========================================================
class rssc_link extends happy_linux_object
{
	var $_charset = _CHARSET;

//---------------------------------------------------------
// ltype : 1 = rss search site
//---------------------------------------------------------

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function rssc_link()
{
	$this->happy_linux_object();

	$this->initVar('lid', XOBJ_DTYPE_INT, null, false);
	$this->initVar('uid', XOBJ_DTYPE_INT, 0, false);
	$this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
	$this->initVar('p1',  XOBJ_DTYPE_INT, 0, false);
	$this->initVar('p2',  XOBJ_DTYPE_INT, 0, false);
	$this->initVar('p3',  XOBJ_DTYPE_INT, 0, false);
	$this->initVar('title',  XOBJ_DTYPE_TXTBOX, null, true, 255);
	$this->initVar('url',    XOBJ_DTYPE_URL,    null, true, 255);
	$this->initVar('ltype',     XOBJ_DTYPE_INT, 0, false);
	$this->initVar('refresh',   XOBJ_DTYPE_INT, 0, false);
	$this->initVar('headline',  XOBJ_DTYPE_INT, 0, false);
	$this->initVar('mode',      XOBJ_DTYPE_INT, 0, false);
	$this->initVar('rdf_url',   XOBJ_DTYPE_URL, null, false, 255);
	$this->initVar('rss_url',   XOBJ_DTYPE_URL, null, false, 255);
	$this->initVar('atom_url',  XOBJ_DTYPE_URL, null, false, 255);
	$this->initVar('encoding',  XOBJ_DTYPE_TXTBOX, null, false);
	$this->initVar('updated_unix',   XOBJ_DTYPE_INT, 0, false);
	$this->initVar('channel',    XOBJ_DTYPE_TXTAREA);
	$this->initVar('xml',        XOBJ_DTYPE_TXTAREA);
	$this->initVar('aux_int_1',  XOBJ_DTYPE_INT,   0);
	$this->initVar('aux_int_2',  XOBJ_DTYPE_INT,   0);
	$this->initVar('aux_text_1', XOBJ_DTYPE_TXTBOX, null, false, 255);
	$this->initVar('aux_text_2', XOBJ_DTYPE_TXTBOX, null, false, 255);
}

//---------------------------------------------------------
// set
//---------------------------------------------------------
function set_vars_keyword( $site )
{
	if ( isset($_POST['keyword']) )
	{
		$key = $_POST['keyword'];
	}
	else
	{
		return false;
	}

	$convert =& happy_linux_convert_encoding::getInstance();

	$key_conv   = $convert->convert($key, $site['code'], $this->_charset);
	$key_encode = urlencode( $key_conv );

	$title   = $site['title'].': '.$key;
	$url     = $site['url']. $key_encode;
	$rss_url = $site['rss']. $key_encode;

	$this->setVars( $_POST );
	$this->setVar('title',    $title );
	$this->setVar('url',      $url );
	$this->setVar('encoding', $site['encoding'] );

	switch ( $site['mode'] )
	{
		case RSSC_C_MODE_RDF:
			$this->setVar('mode',     RSSC_C_MODE_RDF );
			$this->setVar('rdf_url',  $rss_url );
			break;
	
		case RSSC_C_MODE_ATOM:
			$this->setVar('mode',     RSSC_C_MODE_ATOM );
			$this->setVar('atom_url',  $rss_url );
			break;

		case RSSC_C_MODE_RSS:
		default:
			$this->setVar('mode',     RSSC_C_MODE_RSS );
			$this->setVar('rss_url',  $rss_url );
			break;
	}

}

//---------------------------------------------------------
// get
//---------------------------------------------------------
function &getVarAll($format='n')
{
// suppress notice : Only variable references should be returned by reference
	$arr = array(
		'lid' => $this->getVar('lid', $format),
		'uid' => $this->getVar('uid', $format),
		'mid' => $this->getVar('mid', $format),
		'p1'  => $this->getVar('p1',  $format),
		'p2'  => $this->getVar('p2',  $format),
		'p3'  => $this->getVar('p3',  $format),
		'title'     => $this->getVar('title', $format),
		'url'       => $this->getVar('url',   $format),
		'ltype'     => $this->getVar('ltype', $format),
		'refresh'   => $this->getVar('refresh',  $format),
		'headline'  => $this->getVar('headline', $format),
		'mode'      => $this->getVar('mode',      $format),
		'rdf_url'   => $this->getVar('rdf_url',   $format),
		'rss_url'   => $this->getVar('rss_url',   $format),
		'atom_url'  => $this->getVar('atom_url',  $format),
		'encoding'  => $this->getVar('encoding',  $format),
		'xml'       => $this->getVar('xml',       $format),
		'updated_unix' => $this->getVar('updated_unix',   $format),
		'channel'   => $this->get_export_channel(),
	);

	return $arr;
}

function &build_show()
{
	$link =& $this->getVarAll('s');
	$link['url_xml'] = $this->get_rssurl_by_mode('s');
	$link['icon']    = $this->get_rss_icon_by_mode();
	$time = $link['updated_unix'];
	$link['updated_long']   = $this->_format_time( $time, 'l' );
	$link['updated_middle'] = $this->_format_time( $time, 'm' );
	$link['updated_short']  = $this->_format_time( $time, 's' );
	$link['updated_mysql']  = $this->_format_time( $time, 'mysql' );
	return $link;
}

function get_rssurl_by_mode($format='n')
{
	$mode = $this->getVar('mode');
	$ret  = $this->get_rssurl_select_by_mode($mode);
	return $ret;
}

function get_rssurl_select_by_mode($mode, $format='n')
{
	switch ($mode)
	{
		case RSSC_C_MODE_RDF:
			$val = $this->getVar('rdf_url', $format);
			break;

		case RSSC_C_MODE_RSS:
			$val = $this->getVar('rss_url', $format);
			break;

		case RSSC_C_MODE_ATOM:
			$val = $this->getVar('atom_url', $format);
			break;

		default:
			$val = false;
			break;
	}
	return $val;
}

function get_rss_icon_by_mode()
{
	$mode = $this->getVar('mode');

	switch ($mode)
	{
		case RSSC_C_MODE_RDF:
			return 'rdf.png';
			break;

		case RSSC_C_MODE_RSS:
			return 'rss.png';
			break;

		case RSSC_C_MODE_ATOM:
			return 'atom.png';
			break;
	}

	return false;
}

function setChannel($channel)
{
	$value = serialize($channel);
	$this->setVar('channel', $value);
}

function &getChannel()
{
	$ret = false;
	$channel =  $this->getVar('channel', 'n');
	if ($channel)
	{
		$ret = unserialize($channel);
	}
	return $ret;
}

function &get_export_channel()
{
	$channel = $this->getChannel();
	$text = var_export($channel, TRUE);
	$ret  = htmlspecialchars($text, ENT_QUOTES);
	return $ret;
}

function _format_time( $unixtime, $format)
{
	if ($unixtime)
	{
		$text = formatTimestamp( $unixtime, $format );
		return $text;
	}
	return false;
}

function get_mode_option()
{
	$opt = array(
		_RSSC_RSS_MODE_NON  => RSSC_C_MODE_NON,
		_RSSC_RSS_MODE_AUTO => RSSC_C_MODE_AUTO,
		_RSSC_RSS_MODE_RDF  => RSSC_C_MODE_RDF,
		_RSSC_RSS_MODE_RSS  => RSSC_C_MODE_RSS,
		_RSSC_RSS_MODE_ATOM => RSSC_C_MODE_ATOM,
	);

	return $opt;
}

// --- class end ---
}

//=========================================================
// class link handler
//=========================================================
class rssc_link_handler extends happy_linux_object_handler
{
// link table
	var $_link_lid;
	var $_link_obj;

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function rssc_link_handler( $dirname )
{
	$this->happy_linux_object_handler($dirname, 'link', 'lid', 'rssc_link');

	$this->set_debug_db_sql(   RSSC_DEBUG_LINK_SQL );
	$this->set_debug_db_error( RSSC_DEBUG_ERROR );

}

//=========================================================
// Public
//=========================================================
//---------------------------------------------------------
// basic function
//---------------------------------------------------------
function _build_insert_sql(&$obj)
{
	foreach ($obj->gets() as $k => $v) 
	{	${$k} = $v;	}

	$sql  = 'INSERT INTO '.$this->_table.' (';
	$sql .= 'uid, ';
	$sql .= 'mid, ';
	$sql .= 'p1, ';
	$sql .= 'p2, ';
	$sql .= 'p3, ';
	$sql .= 'title, ';
	$sql .= 'url, ';
	$sql .= 'ltype, ';
	$sql .= 'rdf_url, ';
	$sql .= 'rss_url, ';
	$sql .= 'atom_url, ';
	$sql .= 'mode, ';
	$sql .= 'encoding, ';
	$sql .= 'refresh, ';
	$sql .= 'headline, ';
	$sql .= 'updated_unix, ';
	$sql .= 'channel, ';
	$sql .= 'xml, ';
	$sql .= 'aux_int_1, ';
	$sql .= 'aux_int_2, ';
	$sql .= 'aux_text_1, ';
	$sql .= 'aux_text_2 ';

	$sql .= ') VALUES (';
	$sql .= intval($uid).', ';
	$sql .= intval($mid).', ';
	$sql .= intval($p1).', ';
	$sql .= intval($p2).', ';
	$sql .= intval($p3).', ';
	$sql .= $this->quote($title).', ';
	$sql .= $this->quote($url).', ';
	$sql .= intval($ltype).', ';
	$sql .= $this->quote($rdf_url).', ';
	$sql .= $this->quote($rss_url).', ';
	$sql .= $this->quote($atom_url).', ';
	$sql .= intval($mode).', ';
	$sql .= $this->quote($encoding).', ';
	$sql .= intval($refresh).', ';
	$sql .= intval($headline).', ';
	$sql .= intval($updated_unix).', ';
	$sql .= $this->quote($channel).', ';
	$sql .= $this->quote($xml).', ';
	$sql .= intval($aux_int_1).', ';
	$sql .= intval($aux_int_2).', ';
	$sql .= $this->quote($aux_text_1).', ';
	$sql .= $this->quote($aux_text_2).' ';
	$sql .= ')';

	return $sql;
}

function _build_update_sql(&$obj)
{
	foreach ($obj->gets() as $k => $v) 
	{	${$k} = $v;	}

	$sql = 'UPDATE '.$this->_table.' SET ';
	$sql .= 'uid='.intval($uid).', ';
	$sql .= 'mid='.intval($mid).', ';
	$sql .= 'p1='.intval($p1).', ';
	$sql .= 'p2='.intval($p2).', ';
	$sql .= 'p3='.intval($p3).', ';
	$sql .= 'title='.$this->quote($title).', ';
	$sql .= 'url='.$this->quote($url).', ';
	$sql .= 'ltype='.intval($ltype).', ';
	$sql .= 'rdf_url='.$this->quote($rdf_url).', ';
	$sql .= 'rss_url='.$this->quote($rss_url).', ';
	$sql .= 'atom_url='.$this->quote($atom_url).', ';
	$sql .= 'mode='.intval($mode).', ';
	$sql .= 'encoding='.$this->quote($encoding).', ';
	$sql .= 'refresh='.intval($refresh).', ';
	$sql .= 'headline='.intval($headline).', ';
	$sql .= 'updated_unix='.intval($updated_unix).', ';
	$sql .= 'channel='.$this->quote($channel).', ';
	$sql .= 'xml='.$this->quote($xml).', ';
	$sql .= 'aux_int_1='.intval($aux_int_1).', ';
	$sql .= 'aux_int_2='.intval($aux_int_2).', ';
	$sql .= 'aux_text_1='.$this->quote($aux_text_1).', ';
	$sql .= 'aux_text_2='.$this->quote($aux_text_2).' ';
	$sql .= ' WHERE lid='.intval($lid);

	return $sql;
}

function &get_channel_by_lid($lid)
{
	$ret = false;
	$obj =& $this->getCache($lid);
	if ( !is_object($obj) )
	{
		$this->_set_errors( "rssc_link_handler: no link record: lid = $lid" );
		return $ret;
	}

	$ret =& $obj->getChannel();
	return $ret;
}

//---------------------------------------------------------
// get same link list
// for admin/link_manage.php
//---------------------------------------------------------
function &get_list_by_rssurl( $url1, $url2='', $url3='', $lid=0 )
{
	$list   = false;
	$q_url1 = '';
	$q_url2 = '';
	$q_url3 = '';

	$sql1 = 'SELECT lid FROM '.$this->_table.' WHERE ';
	$sql2 = '';

	if ( $url1 && ($url1 != 'http://') )
	{
		$q_url1 = $this->quote($url1);
		$sql2 .= 'rdf_url='. $q_url1.' OR ';
		$sql2 .= 'rss_url='. $q_url1.' OR ';
		$sql2 .= 'atom_url='.$q_url1;
	}

	if ( $url2 && ($url2 != 'http://') )
	{
		if ($q_url1)
		{
			$sql2 .= ' OR ';
		}

		$q_url2 = $this->quote($url2);
		$sql2 .= 'rdf_url='. $q_url2.' OR ';
		$sql2 .= 'rss_url='. $q_url2.' OR ';
		$sql2 .= 'atom_url='.$q_url2;
	}

	if ( $url3 && ($url3 != 'http://') )
	{
		if ($q_url1 || $q_url2)
		{
			$sql2 .= ' OR ';
		}

		$q_url3 = $this->quote($url3);
		$sql2 .= 'rdf_url='. $q_url3.' OR ';
		$sql2 .= 'rss_url='. $q_url3.' OR ';
		$sql2 .= 'atom_url='.$q_url3;
	}

	if ( $sql2 )
	{
		$sql = $sql1.' ( '.$sql2.' ) ';

		if ( $lid )
		{
			$sql .= 'AND lid != '.intval($lid); 
		}

		$list =& $this->get_first_rows_by_sql($sql);
		if ( !is_array($list) || !count($list) )
		{
			$list = false;
		}
	}

	return $list;
}

//---------------------------------------------------------
// headline
//---------------------------------------------------------
function &get_headline_lids($limit=0, $start=0)
{
	$criteria = new CriteriaCompo();
	$criteria->setStart($start);
	$criteria->setLimit($limit);
	$criteria->setSort('headline ASC');
	$criteria->add( new Criteria('headline', 0, '>') );
	$objs = $this->getObjects($criteria);

	$lids = array();

	if ( $objs )
	{
		foreach ($objs as $obj)
		{
			$lids[] = $obj->getVar('lid');
		}
	}

	return $lids;
}

function get_headline_objects($limit=0, $start=0)
{
	$criteria = new CriteriaCompo();
	$criteria->setStart($start);
	$criteria->setLimit($limit);
	$criteria->setSort('headline ASC');
	$criteria->add( new Criteria('headline', 0, '>') );
	$objs = $this->getObjects($criteria);
	return $objs;
}

//---------------------------------------------------------
// format
//---------------------------------------------------------
function &add_ltype_items($item_arr)
{
	$data = array();

	foreach ($item_arr as $item)
	{
		$data[] =& $this->add_ltype_single_item($item);
	}

	return $data;
}

function &add_ltype_single_item($item)
{
	$data = $item;
	$data['ltype'] = $this->_get_item_ltype( $item );
	return $data;
}

//---------------------------------------------------------
// check_exist_rssurl
// for admin/link_manage.php
//---------------------------------------------------------
function build_error_rssurl_list($list, $script)
{
	$msg = null;
	if ( is_array($list) && (count($list) > 0) )
	{
		$msg = "<ul>";

		foreach ($list as $lid)
		{
			$msg .= $this->_build_error_rssurl_list_single($lid, $script);
		}

		$msg .= "</ul>\n";
	}
	return $msg;
}

function _build_error_rssurl_list_single($lid, $script)
{
	$text = null;
	$obj  = $this->getCache($lid);
	if ( is_object($obj) )
	{
		$lid_s   = sprintf("%03d", $lid);
		$url_l   = $script.$lid;
		$title_s = $obj->getVar('title');
		$url_s   = $obj->getVar('url');

		$text  = '<li>';
		$text .= '<a href="'.$url_l.'" target="_blank">'.$lid_s.'</a> : ';
		$text .= '<a href="'.$url_s.'" target="_blank">'.$title_s.'</a> ';
		$text .= "</li>\n";
	}
	return $text;
}

//=========================================================
// private
//=========================================================
//---------------------------------------------------------
// link object
//---------------------------------------------------------
function _set_lid($lid)
{
	$lid = intval($lid);
	if ($lid < 0)
	{
		$this->_set_errors( 'rssc_link_handler: lid not above zero' );
		return false;
	}

	$this->_link_lid = $lid;
	return true;
}

function _get_link_obj($lid)
{
	if ( !$this->_set_lid($lid) )
	{
		$this->_set_errors( 'rssc_link_handler: lid not above zero' );
		return false;
	}

	$obj =& $this->get($lid);

	if ( !is_object($obj) )
	{
		$this->_set_errors( 'rssc_link_handler: link object not exist' );
		return false;
	}

	$this->_link_obj  = $obj;
	return $obj;
}

//---------------------------------------------------------
// format
//---------------------------------------------------------
function _get_item_ltype($item)
{
	if ( isset($item['lid']) && $item['lid'] )
	{
		$obj = $this->getCache( $item['lid'] );
		if ( !is_object($obj) )
		{
			$this->_set_errors( "rssc_link_handler: no link record: lid = ".$item['lid'] );
			return false;
		}
		else
		{
			$ret = $obj->getVar('ltype');
			return $ret;
		}
	}

	return false;
}



// --- class end ---
}

// === class end ===
}

?>