<?php
//	page_viewer.inc.php 0.3	'06/10/13	taru	plugin

define('PLUGIN_PAGE_VIEWER_URL_CHECK', "");
define('PLUGIN_PAGE_VIEWER_PAGE_FILTER', "");
define('PLUGIN_PAGE_VIEWER_SCRIPT', '');

function plugin_page_viewer_action()
{
	global $vars, $title;
	$page = $vars['refer'];
	static $count = 0;

	if (PLUGIN_PAGE_VIEWER_URL_CHECK != '') {
		if (! preg_match(PLUGIN_PAGE_VIEWER_URL_CHECK, $_SERVER['HTTP_REFERER'])) return;
	}
	if (PLUGIN_PAGE_VIEWER_PAGE_FILTER != '') {
		if (! preg_match(PLUGIN_PAGE_VIEWER_PAGE_FILTER, $page))
			 $error_mg = "<br /><br />It is not an admitted page<br /><br />";
	}
	if(! is_page($page,$reload=FALSE))
		 $error_mg .= '<br /><br />There is not the page name that you appointed<br /><br />';

	if (! isset($error_mg)){
		$source  = get_source($page);
		$body_source = "";

		if(isset($vars['part_num'])){
			$find_part = 0;
			foreach ($source as $source_line) {
				if (preg_match("/^\*{1,3}/", $source_line)) {
					$count++;
					$find_part = ($vars['part_num'] == $count) ? 1 : 0 ;
				}
				if ( $find_part ) $body_source .= $source_line;
			}
		} else {
			$body_source = $source;
		}

		$get['page'] = $post['page'] = $vars['page'] = $page;
		$body = convert_html($body_source);
	}

	pkwk_common_headers();
	header('Content-type: text/html');
	$head = <<<EOD
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP">
EOD;

	$head .= <<<EOD
<head>
 <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
 <meta http-equiv="content-style-type" content="text/css" />
 <meta http-equiv="content-script-type" content="text/javascript" />
 <title>$title</title>
EOD;

	$head .= ' <link rel="stylesheet" href="' . SKIN_DIR . 'default.css" type="text/css" media="screen" charset="Shift_JIS" />' . "\r\n";
	$head .= ' <script type="text/javascript">' . "\r\n";

	$head .= " <!--\r\n";
	if (exist_plugin_convert('js_init')) $head .= do_plugin_convert('js_init');
	$head .= " -->\n </script>\r\n";

	$head .= ' <script type="text/javascript" src="' . SKIN_DIR . 'lang/ja_JP.js"></script>' . "\r\n";
	$head .= ' <script type="text/javascript" src="' . SKIN_DIR . 'default.js"></script>' . "\r\n";
	$head .= ' <script type="text/javascript" src="' . SKIN_DIR . 'ajax/textloader.js"></script>' . "\r\n";
	$head .= ' <script type="text/javascript" src="' . SKIN_DIR . 'ajax/glossary.js"></script>' . "\r\n";
	$head .= "</head>\r\n<body>\r\n";
	$head .= PLUGIN_PAGE_VIEWER_SCRIPT . "\r\n";
	print $head . $body . "</body>\r\n<html>\r\n";
	exit;
}
function plugin_page_viewer_convert()
{
	global $script, $vars;

	$args = func_get_args();
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', $opt_key);
		$opt[strtolower($key)] = htmlspecialchars($val);
	}

	if(! isset($opt['refer'])) return 'Please put the page name';

	if(isset($opt['url'])){
		$url = $opt['url'];
	} else {
		$url = $script;
		if(! is_page($opt['refer'],$reload=FALSE)) return 'There is not the page name that you appointed';
	}

	if(! preg_match("/\?$/",$url)){
		$url .= preg_match("/index.php$/",$url) ? '?' : 'index.php?';
	}
	$url .= 'plugin=page_viewer&refer=' . $opt['refer'];
	if(isset($opt['part_num'])) $url .= '&part_num=' . $opt['part_num'];

	if (exist_plugin_convert('greybox')){
		 $body .= do_plugin_convert(greybox,$opt['caption'] . ',' . $url . ',' . $opt['img'] . ',' . $opt['height'] . ',' . $opt['width']);
	}

	return $body;

}
function plugin_page_viewer_inline()
{
	$args = func_get_args();
	array_pop($args);
	return call_user_func_array('plugin_page_viewer_convert', $args);
}
?>
