<?php
// $Id: strings.php,v 1.5 2006/09/27 02:59:07 ohwada Exp $

// 2006-09-20 K.OHWADA
// add replace_return_to_space() convert_str_to_crlf()
// add prepare_text()
// add $_flag_remove_control_code
// change build_summary() shorten_text()

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

//=========================================================
// Happy Linux Framework Module
// 2006-07-10 K.OHWADA
// —L•üŽ©‰“•û—ˆ
//=========================================================

//---------------------------------------------------------
// TODO
// global $xoopsConfig;
//---------------------------------------------------------

//=========================================================
// class happy_linux_strings
//=========================================================
class happy_linux_strings
{
	var $_MAX_DEPTH = 10;

// local variable
	var $_max_summary = 100;
	var $_time_start  = 0;

// same language match contorl code
// ex) BIG-5 GB2312 Ž` C05C B2CD —V B943 904A 
	var $_flag_remove_control_code = false;

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function happy_linux_strings()
{
	// dummy
}

function &getInstance()
{
	static $instance;
	if (!isset($instance)) 
	{
		$instance = new happy_linux_strings();
	}

	return $instance;
}

//=========================================================
// for object->setVar()
//=========================================================
function prepare_text($str, $not_gpc=false)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control($str);
		$str = $this->strip_tab($str);
		$str = $this->strip_return($str);
	}
	$str = $this->strip_slashes_gpc_flag($str, $not_gpc);
	$str = $this->replace_javascript($str);
	return $str;
}

function prepare_textarea($str, $not_gpc=false)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control($str);
	}
	$str = $this->strip_slashes_gpc_flag($str, $not_gpc);
	$str = $this->replace_javascript($str);
	return $str;
}

function prepare_url($str, $not_gpc=false)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control($str);
		$str = $this->strip_tab($str);
		$str = $this->strip_return($str);
	}
	$str = $this->strip_slashes_gpc_flag($str, $not_gpc);
	$str = $this->deny_javascript($str);
	$str = $this->deny_http_only($str);
	$str = $this->allow_http($str);
	return $str;
}

//=========================================================
// sanitize strings
//=========================================================
// --------------------------------------------------------
// sanitize and convert htmlspecialchars for textarea
//   <  -> &lt;
//   >  -> &gt;
//   "  -> &quot;
//   '  -> &#039;
// --------------------------------------------------------
function sanitize_text($str, $flag_java=true)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control_all($str);
	}
	if ($flag_java)
	{
		$str = $this->replace_javascript($str);
	}
	$str = htmlspecialchars($str, ENT_QUOTES);
	$str = preg_replace("/&amp;/i", '&', $str);
	return $str;
}

// --------------------------------------------------------
// sanitize and convert htmlspecialchars for textarea
//   <  -> &lt;
//   >  -> &gt;
//   "  -> &quot;
//   '  -> &#039;
// --------------------------------------------------------
function sanitize_textarea($str, $flag_java=true)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control($str);
	}
	if ($flag_java)
	{
		$str = $this->replace_javascript($str);
	}
	$str = htmlspecialchars($str, ENT_QUOTES);
	$str = preg_replace("/&amp;/i", '&', $str);
	return $str;
}

//---------------------------------------------------------
// sanitize and convert htmlspecialchars for url
//   <     -> &lt;
//   >     -> &gt;
//   "     -> &quot;
//   '     -> &#039;
//   &     -> &amp;
//   &amp; -> &amp;
//---------------------------------------------------------
function sanitize_url($str, $flag_java=true, $flag_deny=true)
{
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control_all($str);
	}
	if ($flag_java)
	{
		$str = $this->replace_javascript($str);
	}
	if ($flag_deny)
	{
		$str = $this->deny_http_only($str);
	}
	$str = preg_replace('/&amp;/i', '&', $str);
	$str = htmlspecialchars($str, ENT_QUOTES);
	return $str;
}

//---------------------------------------------------------
// strip control code 
//---------------------------------------------------------
function strip_control_all($str)
{
	$str = $this->strip_control($str);
	$str = $this->strip_tab($str);
	$str = $this->strip_return($str);
	return $str;
}

//---------------------------------------------------------
// strip control code except TAB, LF, CR
// TAB \x09 \t
// LF  \xOA \n
// CR  \xOD \r
//---------------------------------------------------------
function strip_control($str)
{
	$str = preg_replace('/[\x00-\x08]/','',$str);
	$str = preg_replace('/[\x0B-\x0C]/','',$str);
	$str = preg_replace('/[\x0E-\x1F]/','',$str);
	$str = preg_replace('/[\x7F]/',     '',$str);
	return $str;
}

// strip TAB code
function strip_tab($str)
{
	$str = preg_replace("/\t/",'',$str);
	return $str;
}

// strip LF, CR code
function strip_return($str)
{
	$str = preg_replace("/\n/",'',$str);
	$str = preg_replace("/\r/",'',$str);
	return $str;
}

function replace_return_to_space( $str )
{
	$str = preg_replace("/\n/",' ',$str);
	$str = preg_replace("/\r/",' ',$str);
	return $str;
}

//---------------------------------------------------------
// strip space code
// SPACE \x20
//---------------------------------------------------------
function strip_space_code($str)
{
	return preg_replace('/[\x20]/', '', $str);
}

function replace_space_code_to_single_space($str)
{
	return preg_replace("/[\x20]+/", ' ', $str);
}

function strip_html_space_code($str)
{
	return preg_replace("/&nbsp;/i", '', $str);
}

function replace_html_space_code_to_space($str)
{
	return preg_replace("/&nbsp;/i", ' ', $str);
}

//--------------------------------------------------------
// javascript
//--------------------------------------------------------
function replace_javascript($str)
{
	$str = preg_replace('/javascript:/si', 'java script:', $str);
	$str = preg_replace('/about:/si',      'about :',      $str);
	$str = preg_replace('/vbscript:/si',   'vb script:',   $str);
	return $str;
}

// deny_javascript
function deny_javascript($str)
{
	if ( $this->check_javascript($str) )
	{
		return '';
	}
	return $str;
}

// Checks if Javascript are included in string
function check_javascript($str)
{
	$str = $this->strip_control_all($str);

	if ( preg_match('/javascript:/si', $str) )
	{
		return true;	// include Javascript
	}
	if ( preg_match('/about:/si', $str) )
	{
		return true;	// include about
	}
	if ( preg_match('/vbscript:/si', $str) )
	{
		return true;	// include vbscript
	}
	return false;
}

//--------------------------------------------------------
// sanitize array
//--------------------------------------------------------
function &sanitize_array_text($arr_in)
{
	$ret = $this->_sanitize_array_text_recursive(0, $arr_in);
	return $ret;
}

function _sanitize_array_text_recursive($num, $arr_in)
{
	$num ++;
	if ( $num > $this->_MAX_DEPTH )
	{	return false;	}

	if ( is_array($arr_in) )
	{
		$arr_out = array();
		reset ($arr_in);

		foreach ($arr_in as $k => $v) 
		{
			if ( is_array($v) )
			{
				$arr_out[$k] = $this->_sanitize_array_text_recursive($num, $v);
			}
			else
			{
				$arr_out[$k] = $this->sanitize_text($v);
			}
		}

		return $arr_out;
	}

	$ret = $this->sanitize_text($arr_in);
	return $ret;
}

//--------------------------------------------------------
// sanitize for XOOPS object
//--------------------------------------------------------
function sanitize_format($value, $format='s')
{
	$ret = $this->sanitize_format_text($value, $format);
	return $ret;
}

function sanitize_format_text($value, $format='s')
{
	switch (strtolower($format)) 
	{
// strip GPC slashes when set by serVar()
		case 's':
		case 'show':
		case 'p':
		case 'preview':
		case 'f':
		case 'formpreview':
			$value = $this->sanitize_text($value);
			break;

		case 'e':
		case 'edit':
			$value = htmlspecialchars($value, ENT_QUOTES);
			break;

		case 'n':
		case 'none':
		default:
			break;
	}

	return $value;
}

function sanitize_format_textarea($value, $format='s')
{
	switch (strtolower($format)) 
	{
// strip GPC slashes when set by serVar()	
		case 's':
		case 'show':
		case 'p':
		case 'preview':
		case 'f':
		case 'formpreview':
			$value = $this->sanitize_textarea($value);
			break;

		case 'e':
		case 'edit':
			$value = htmlspecialchars($value, ENT_QUOTES);
			break;

		case 'n':
		case 'none':
		default:
			break;
	}

	return $value;
}

function sanitize_format_url($value, $format='s')
{
	switch (strtolower($format)) 
	{
// strip GPC slashes when set by serVar();
		case 's':
		case 'show':
		case 'p':
		case 'preview':
		case 'f':
		case 'formpreview':
			$value = $this->sanitize_url($value);
			break;

		case 'e':
		case 'edit':
			$value = htmlspecialchars($value, ENT_QUOTES);
			break;

		case 'n':
		case 'none':
		default:
			break;
	}

	return $value;
}

function sanitize_format_text_short($value, $format='s', $max=100)
{
	if ($value)
	{
		if ($max)
		{
			$value = $this->shorten_text($value, $max);
		}

		$value = $this->sanitize_format_text($value, $format);
		$value = wordwrap($value);
		$value = '<pre>'.$value.'</pre>'."\n";
	}

	return $value;
}

//--------------------------------------------------------
// get_magic_quotes_gpc
//--------------------------------------------------------
function strip_slashes_gpc_flag($str, $not_gpc=false)
{
	if (!$not_gpc)
	{
		$str = $this->strip_slashes_gpc($str);
	}
	return $str;
}

function strip_slashes_gpc($str)
{
	if (get_magic_quotes_gpc()) 
	{
		$str = stripslashes($str);
	}
	return $str;
}

function &add_slashes_gpc($str)
{
	if (!get_magic_quotes_gpc()) 
	{
		$str = addslashes($str);
	}
	return $str;
}

//--------------------------------------------------------
// check string
//--------------------------------------------------------
function deny_http_only($str)
{
	if ( $this->check_http_only($str) )
	{
		return '';
	}
	return $str;
}

function allow_http($str)
{
	if ( $this->check_http_start($str) )
	{
		return $str;
	}
	return '';
}

// Checks if string is started from HTTP
function check_http_start($str)
{
	if ( preg_match("|^https?://|", $str) )
	{
		return true;	// include HTTP
	}
	return false;
}

// Checks if string is HTTP only
function check_http_only($str)
{
	if ( ($str == 'http://') || ($str == 'https://') )
	{
		return true;	// http only
	}
	return false;
}

function check_http_fill($str)
{
	if ( ($str != '') && ($str != 'http://') && ($str != 'https://') )
	{
		return true;
	}
	return false;
}

//=========================================================
// convert strings
//=========================================================
// --------------------------------------------------------
// summary
// --------------------------------------------------------
function build_summary($text, $max, $keyword_array=null, $format='n')
{
	$text = $this->strip_tags_for_text($text);
	$text = $this->shorten_text($text, $max, $keyword_array);
	$text = $this->sanitize_format($text, $format);
	return $text;
}

function strip_tags_for_text($text)
{
	if ( $this->_flag_remove_control_code )
	{
		$text = $this->strip_control_all($text);
	}
	$text = $this->strip_style_tag($text);
	$text = $this->add_space_after_tag($text);
	$text = strip_tags($text);
	$text = $this->strip_space($text);
	return $text;
}

function strip_style_tag($text)
{
	$text = preg_replace('|<\s*style\s?.*?>(.*)<\s*/\s*style\s*>|is', '', $text);
	return $text;
}

function add_space_after_tag($text)
{
	$text = str_replace(">",   "> ", $text);
	return $text;
}

function strip_space($str)
{
	$str = $this->convert_space_zen_to_han($str);
	$str = $this->replace_html_space_code_to_space($str);
	$str = $this->replace_space_code_to_single_space($str);
	return $str;
}

//--------------------------------------------------------
// split_nl
//--------------------------------------------------------
function split_nl($text)
{
	$text = $this->convert_nl($text);
	$text = split("\n", $text);
	return $text;
}

//--------------------------------------------------------
// convert_nl
//--------------------------------------------------------
function convert_nl($text)
{
	$text = str_replace( "\r\n", "\n", $text ); 
	$text = str_replace( "\n\r", "\n", $text ); 
	$text = str_replace( "\r",   "\n", $text ); 
	return $text;
}

function convert_str_to_crlf($str)
{
	$str = str_replace('\r\n', "\r\n", $str);
	$str = str_replace('\r',   "\r",   $str);
	$str = str_replace('\n',   "\n",   $str);
	return $str;
}

//--------------------------------------------------------
// check valid strings
//--------------------------------------------------------
function check_valid($str)
{
// remove control and space
	if ( $this->_flag_remove_control_code )
	{
		$str = $this->strip_control_all($str);
	}

	$str = $this->strip_space($str);

	if ( strlen($str) > 0 )
	{
		return true;
	}

	return false;
}

// --------------------------------------------------------
// utility for array
// --------------------------------------------------------
function &merge_unique_array($arr1, $arr2)
{
	$arr = false;
	if ( is_array($arr1) && is_array($arr2) )
	{
		$arr = array_merge( $arr1, $arr2 );
		$arr = array_unique( $arr );
	}
	return $arr;
}

function urlencode_from_array($arr)
{
	$val = false;
	if ( is_array($arr) )
	{
		$val = implode(' ', $arr);
		$val = urlencode($val);
	}
	return $val;
}

// --------------------------------------------------------
// onvert_array_key_to_string
// array($key => $value)  ==> $key:$value
// --------------------------------------------------------
function convert_array_key_to_string($arr)
{
	if ( (count($arr) == 0) || !is_array($arr) )
	{
		return '';
	}

	$str = '';

	foreach ($arr as $key => $value)
	{
// replace return code
		$value = preg_replace("/\n/", '\\n', $value);

		$str .= $key.":".$value."\n";
	}

	return $str;
}

function convert_string_to_array_key($str, $format='n')
{
	$array = array();

	$line_arr = $this->convert_string_to_array($str, "\n");

	if ( count($line_arr) == 0 )  return $array;

	foreach ($line_arr as $line)
	{
		list($key, $value) = split(":", $line, 2);

// replace return code
		$value = preg_replace('/\\n/', "\n", $value);

		$array[$key] = $this->make_format($value, $format);
	}

	return $array;
}

//---------------------------------------------------------
// convert array to string
// array($a, $b, $c)  ==> $a & $b & $c
//---------------------------------------------------------
function convert_array_to_string($arr, $pattern='&')
{
	if ( is_array($arr) )
	{
		$str = $pattern;

		foreach ($arr as $value)
		{
			$str .= intval($value).$pattern;
		}
	}
	elseif ( is_int($arr) )
	{
		$str = $pattern.intval($arr).$pattern;
	}
	else
	{
		$str = $arr;
	}

	return $str;
}

function convert_string_to_array($str, $pattern='&')
{
	$str_arr = split($pattern, $str);

	$i = 0;	
	$arr = array();
	foreach ($str_arr as $value)
	{
		$value = trim($value);

		if ($value == '')  continue;

		$arr[$i++] = $value;
	}

	return $arr;
}

//-------------------------------------------------------------------
// measure time
//-------------------------------------------------------------------
function start_measure_time()
{
	$this->_time_start = $this->get_microtime();
}

function get_measure_time()
{
	$time = $this->get_microtime() - $this->_time_start;
	return $time;
}

function get_microtime()
{ 
	list($usec, $sec) = explode(" ",microtime()); 
	$time = floatval($sec) + floatval($usec); 
	return $time; 
}

//--------------------------------------------------------
// set parameter
//--------------------------------------------------------
function set_max_summary($value)
{
	$this->_max_summary = intval($value);
}

function set_flag_remove_control_code($val)
{
	$this->_flag_remove_control_code = (bool)$val;
}

//========================================================
// multibyte function
//========================================================
function shorten_text($text, $max, $keyword_array=null)
{
// nothing, if zero
	if ($max == 0)
	{
		return '';
	}

// unlimited, if minus
	if ($max < 0)
	{
		return $text;
	}

// less than
	if ( strlen($text) <= $max)
	{
		return $text;
	}

	if ( is_array($keyword_array) )
	{
		$text = happy_linux_build_search_context( $text, $keyword_array, $max );
	}
	else
	{
		$text = happy_linux_strcut( $text, 0, $max )." ...";;
	}

	return $text;
}

function convert_space_zen_to_han($str)
{
	return happy_linux_convert_kana($str, "s");
}

// --- class end ---
}

?>