<?
error_reporting(E_ERROR | E_PARSE);

class Helper {

	private $conn = null;

	function __construct(){
	}

	function to_string($val){
		return strval($val);
	}

	function DOMfindByClass($html, $classname){
		$prefix = '<?xml encoding="utf-8" ?>';
		$doc = DOMDocument::loadHTML($prefix.$html);
		$finder = new DomXPath($doc);
		// $classname="divDH2";
		$nodes = $finder->query("//*[contains(@class, '$classname')]");

		return $this->DOMinnerHTML($nodes[0]);

	}

	function DOMinnerHTML(DOMNode $element) 
	{ 
	    $innerHTML = ""; 
	    $children  = $element->childNodes;

	    foreach ($children as $child) 
	    { 
	        $innerHTML .= $element->ownerDocument->saveHTML($child);
	    }

	    return $innerHTML; 
	} 

	function htmlTextValue($html){
		if($html == "") return "";
		$prefix = '<?xml encoding="utf-8" ?>';
		try{
		$doc = DOMDocument::loadHTML($prefix.$html);
		return strip_tags($doc->textContent);
		}catch(Exception $e){
			return "";
		}
	}

	// function substr_r($str, $start_key, $end_key, $skip_key=true){
	// 	$data = $str;
	// 	$pos = strpos($data, $start_key); 
	// 	$data = substr($data, $pos);

	// 	$data = substr($data, $pos);
	// 	$pos = strrpos($data, $end_key, strlen($start_key) +1);

	// 	if(!$skip_key){ $pos += strlen($end_key); }
	// 	$data = substr($data, 0, $pos);

	// 	if($skip_key){
	// 		$data = substr($data, strlen($start_key));
	// 	}

	// 	return $data;
	// }

	function substr_remove($str, $keyword){
		$substr_start = strpos($str, $keyword); 
		$str = substr($str, $substr_start + strlen($keyword), strlen($str));

		return $str;

	}

	function substr($str, $start_key, $end_key, $skip_key=true){
		$data = $str;
		$pos = strpos($data, $start_key);
		// echo $pos;
		// echo "<br/>";
		// echo $data."<br/>"; 
		$data = substr($data, $pos);
		$pos = strpos($data, $end_key, strlen($start_key) +1);

		if(!$skip_key){ $pos += strlen($end_key); }
		$data = substr($data, 0, $pos);

		// echo $data."\n";

		if($skip_key){
			$data = substr($data, strlen($start_key));
		}
		// echo "\n";
		// echo "\n";
		

		return $data;
	}

	function curl($url, $setting = array('header' => array(
				'User-Agent'=> 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
				, 'Accept-Language'=> 'zh-HK,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6,zh-CN;q=0.5'
	), 'post_data' => array())){
		// echo $url."\n aaa";
		// print_r($setting);
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	    if(strpos($url, 'https') == 0){
	    	// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	    }
	    // if(count(@$setting['header']) > 0)
	    // print_r($setting['header']);
	    if(@$setting['header'] !== @array_values($setting['header'])){
	    	// print_r($setting['header']);
	    	curl_setopt($ch, CURLOPT_HTTPHEADER, $setting['header']);
	    }


	    // if(count(@$setting['post_data']) > 0){
	    // if(count(array_filter(array_keys(@$setting['post_data']), 'is_string')) > 0){
	    if(is_string(@$setting['post_data'])){
	    	curl_setopt($ch, CURLOPT_POSTFIELDS, $setting['post_data']);
	    }

	    if(@$setting['post_data'] !== @array_values($setting['post_data'])){

		    curl_setopt($ch, CURLOPT_POST, 1);
			// curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");

		    $str = "";
		    foreach($setting['post_data'] as $key => $val){
		    	$str .= $key."=".$val;
		    	$str .= "&";
		    }
		    $str = substr($str, 0, strlen($str) -1);
		    // echo $str;
			curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
		}

		if(@$setting['cookie'] !== @array_values($setting['cookie']))
			curl_setopt($ch, CURLOPT_COOKIE, $setting['cookie']);
		
		// curl_setopt($ch, CURLOPT_HEADER  , true);  // we want headers
		// curl_setopt($ch, CURLOPT_NOBODY  , true);

	    $raw = curl_exec($ch);

	    curl_close($ch);

	    return $raw;
	}

	function str_split($str, $keyword, $ignore_empty=true){
		$output = array();
		$temp = explode($keyword, $str);
		foreach($temp as $row){
			if(strlen($row) > 0){
				array_push($output, $row);
			}
		}
		return $output;
	}

	// $str = temp.txt, $keyword = .
	// Output = 4
	function str_pos_last_occurance($str, $keyword){
		$pos = strrpos($str, $keyword);
		return $pos;
	}

	// $str = temp.txt, $pos = 4
	// Output: Array ( [0] => temp [1] => .txt )
	function str_split_by_pos($str, $pos){
		
		$left = substr($str, 0, $pos);
		$right = substr($str, $pos, strlen($str));
		$output = array($left, $right);

		// print_r($output);
		return $output;
	}

	function file_read($file){
		return file_get_contents($file);
	}

	function file_save($data_str, $file_name, $save_path=__DIR__."/", $backup=true){

		// make a backup to the file in page/lib/log folder
		if($backup){
			$str = $this->str_split_by_pos($file_name, 
				$this->str_pos_last_occurance($file_name, ".")
			);

			// $t = $this->to_string(time());
			$t = time();
			$s = $str[0].'_'.$t.$str[1];

			$this->file_save_direct($data_str, __DIR__."/log/".$s); // backup
		}

		// actually save the file
		$this->file_save_direct($data_str, $save_path.$file_name);
	}

	function file_save_direct($data_str, $f){
		$myfile = fopen($f, "w") or die("Unable to open file!");
		fwrite($myfile, $data_str);
		fclose($myfile);	
	}


}




?>