<?php
// $Id: bin_base.php,v 1.4 2006/09/19 03:16:56 ohwada Exp $

// 2006-09-18 K.OHWADA
// add $_argv_1 etc

// 2006-07-10 K.OHWADA
// this is new file
// porting from bin_base_class.php

//=========================================================
// Happy Linux Framework Module
// 2006-07-10 K.OHWADA
//=========================================================

class happy_linux_bin_base
{
	var $_DIRNAME;

// constant
	var $_X_MAILER = 'XOOPS';

// test parameter
	var $_flag_print = false;
	var $_flag_write = true;
	var $_limit      = 10;

	var $_FLAG_PRINT_WEB = true;
	var $_FLAG_WRITE_WEB = true;
	var $_LIMIT_WEB      = 10;

	var $_FLAG_PRINT_COMMAND = false;
	var $_FLAG_WRITE_COMMAND = true;
	var $_LIMIT_COMMAND      = 0;	// unlimited

// xoops parameter
	var $_CHARSET;
	var $_sitename;
	var $_adminmail;

// command parameter
	var $_argv_1 = null;
	var $_argv_2 = null;
	var $_argv_3 = null;
	var $_argv_4 = null;
	var $_argv_5 = null;

// result file
	var $_sub_dir    = 'cache';
	var $_goto_admin = 'goto admin index';
	var $_fp;
	var $_file_result;
	var $_url_result;
	var $_url_admin;

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function happy_linux_bin_base( $dirname )
{
	$this->_DIRNAME = $dirname;

// system parameter
	global $xoops_sitename, $xoops_adminmail; 
	$this->_sitename  = $xoops_sitename;
	$this->_adminmail = $xoops_adminmail;
	$this->_CHARSET   = _CHARSET;

// result file
	$this->_url_admin   = XOOPS_URL.'/modules/'.$this->_DIRNAME.'/admin/index.php';
}

//=========================================================
// private
//=========================================================
//---------------------------------------------------------
// web_param
//---------------------------------------------------------
function _set_env_param($pass)
{

// web
	if ( isset($_SERVER['REQUEST_METHOD']) )
	{
		$this->_flag_print = $this->_FLAG_PRINT_WEB;
		$this->_flag_write = $this->_FLAG_WRITE_WEB;
		$this->_limit      = $this->_LIMIT_WEB;

		if ( isset($_GET['pass']) )
		{
			if ( $_GET['pass'] != $pass )
			{
				echo "no action <br />\n";
				return false;
			}
		}
		else
		{
			echo "no action <br />\n";
			return false;
		}

		if ( isset($_GET['limit']) )
		{
			$this->_limit = $_GET['limit'];
		}
	}

// command line
	else
	{
		$this->_flag_print = $this->_FLAG_PRINT_COMMAND;
		$this->_flag_write = $this->_FLAG_WRITE_COMMAND;
		$this->_limit      = $this->_LIMIT_COMMAND;

		if ( !isset( $_SERVER['argv'][1] ) )
		{
			return false;
		}

		$this->_argv_1 = $_SERVER['argv'][1];

		if ( $_SERVER['argv'][1] != $pass )
		{
			return false;
		}

		if ( !isset( $_SERVER['argv'][2] ) )
		{
			$this->_argv_2 = $_SERVER['argv'][2];
		}

		if ( !isset( $_SERVER['argv'][3] ) )
		{
			$this->_argv_3 = $_SERVER['argv'][3];
		}

		if ( !isset( $_SERVER['argv'][4] ) )
		{
			$this->_argv_4 = $_SERVER['argv'][4];
		}

		if ( !isset( $_SERVER['argv'][5] ) )
		{
			$this->_argv_4 = $_SERVER['argv'][5];
		}
	}

	return true;
}

//---------------------------------------------------------
// html header & footer
//---------------------------------------------------------
function _html_header()
{
	$text = <<<END_OF_TEXT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=$this->_CHARSET">
<title> $this->_TITLE </title>
</head><body>
<h3> $this->_TITLE </h3>
<hr />
END_OF_TEXT;

	$this->_print_write_data($text);
}

function _html_footer()
{
	$text = <<<END_OF_TEXT
<br />
<hr />
<a href="$this->_url_admin">$this->_goto_admin</a><br />
</head></html>
END_OF_TEXT;

	$this->_print_write_data($text);
}

//---------------------------------------------------------
// result file
//---------------------------------------------------------
function _file_open($filename)
{
	if ($this->_flag_write)
	{
		$this->_fp = fopen($filename, 'w');
	}
}

function _file_close()
{
	if ($this->_flag_write && $this->_fp)
	{
		fclose($this->_fp);
	}
}

function _print_write_data($data)
{
	if ($this->_flag_print)
	{
		echo $data;
	}

	if ($this->_flag_write)
	{
		fwrite($this->_fp, $data);
	}
}

//---------------------------------------------------------
// mail
//---------------------------------------------------------
function _send_mail($mailto, $title, $content)
{
	$mailto  = $this->_adminmail;
	$subject = '['. $this->_sitename .'] '. $title;
	$body    = $this->_build_mail_body($title, $content);
	$header  = 'From: '. $this->_adminmail ." \n";
	$header .= 'X-Mailer: '. $this->_X_MAILER ." \n"; 

	$ret = happy_linux_send_mail($mailto, $subject, $body, $header);
	return $ret;
}

function _build_mail_body($title, $body)
{
	$siteurl = XOOPS_URL.'/';

	$msg  = '';
	if ( $this->_flag_write )
	{
		$msg  = "You can view detail here:\n";
		$msg .= $this->_url_result."\n";
	}

	$text = <<<END_OF_TEXT
$title

$body

$msg
-----------
$this->_sitename ( $siteurl )
webmaster
$this->_adminmail
-----------
END_OF_TEXT;

	return $text;
}

//---------------------------------------------------------
// set param
//---------------------------------------------------------
function set_mailer($val)
{
	$this->_X_MAILER = $val;
}

function set_result_file($file)
{
	$this->_file_result = XOOPS_ROOT_PATH.'/modules/'.$this->_DIRNAME.'/'.$this->_sub_dir.'/'.$file;
	$this->_url_result  = XOOPS_URL.      '/modules/'.$this->_DIRNAME.'/'.$this->_sub_dir.'/'.$file;
}

// --- class end ---
}

?>