<?php

class Cmd_trackback
{
	/**
	 * インスタンスを取得する。
	 */
	static function getinstance()
	{
		return new Cmd_trackback;
	}
	
	
	function run()
	{
		$get = KinoWiki::getinstance()->getGET();
		$post = KinoWiki::getinstance()->getPOST();
		$smarty = new CmdSmarty('trackback');
		
		if(!isset($get['page']) || Page::getinstance($get['page'])->getsource() == ''){
			$smarty->assign('errormes', 'unreceivable page');
			$smarty->display('receive_fail.tpl.htm');
			exit;
		}
		
		if(isset($post['url'])){
			$data =& $post;
		}
		else if(isset($get['url'])){
			$data =& $get;
		}
		else{
			$smarty->assign('errormes', 'no url');
			$smarty->display('receive_fail.tpl.htm');
			exit;
		}
		
		$pagename = $get['page'];
		$title = isset($data['title']) ? $data['title'] : '';
		$excerpt = isset($data['excerpt']) ? $data['excerpt'] : '';
		$blog_name = isset($data['blog_name']) ? $data['blog_name'] : '';
		$url = $data['url'];
		
		/*
		$encode = mb_detect_encoding($excerpt . $blog_name . $title);
		$title = mb_convert_encoding($title, 'UTF-8', $encode);
		$excerpt = mb_convert_encoding($excerpt, 'UTF-8', $encode);
		$blog_name = mb_convert_encoding($blog_name, 'UTF-8', $encode);
		*/
		
		$title = mb_strlen($title) >= 64 ? mb_substr($title, 0, 60) . '...' : $title;
		$excerpt = mb_strlen($excerpt) >= 256 ? mb_substr($excerpt, 0, 252) . '...' : $excerpt;
		
		
		$db = DataBase::getinstance();
		
		$_pagename = $db->escape($pagename);
		$_title = $db->escape($title);
		$_excerpt = $db->escape($excerpt);
		$_url = $db->escape($url);
		$_blog_name = $db->escape($blog_name);
		$_timestamp = TIME;
		
		$query  = "INSERT INTO trackback";
		$query .= " (num, pagename, title, excerpt, url, blog_name, timestamp)";
		$query .= " VALUES(null, '$_pagename', '$_title', '$_excerpt', '$_url', '$_blog_name', $_timestamp)";
		$db->query($query);
		
		$smarty->display('receive_success.tpl.htm');
		exit;
	}
}

?>