<commit_before><?php
namespace MOFG_form\Member;

/**
 * Mail class
 *
 * @package MOFG_form
 * @author <NAME>
 * @copyright Copyright (c) 2016 <NAME> mofg.net
 */
class Mail{
	private $to = "";
	private $subject = "";
	private $body = "";
	private $header = "";

	const FORMAT_EMAIL_ADDRESS = '#\A[a-zA-Z0-9.!\#$%&\'*+/=?^_`{|}~-]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+\z#';
	const FORMAT_EMAIL_HEADER = '#\A[a-zA-Z0-9_\-]+ *: *[^:]+\z#';

	/**
	 * @param string $to (optional)
	 * @param string $subject (optional)
	 * @param string $body (optional)
	 * @param string $header (optional)
	 * @return boolean
	 */
	public function send($to = null, $subject = null, $body = null, $header = null){
		if( !isset($to) ){
			$to = $this->to;
			$subject = $this->subject;
			$body = $this->body;
			$header = $this->header;
		}else if( !isset($subject) && !isset($body) ){
			return false;
		}

		$result = false;
		$to = $this->construct_data($to, ",", self::FORMAT_EMAIL_ADDRESS);
		if( !empty($to) ){
			$header = $this->construct_data($header, "\n", self::FORMAT_EMAIL_HEADER);
			if( empty($header) ){
				$result = mb_send_mail($to, $subject, $body);
			}else{
				$header = $this->group_header($header);
				$result = mb_send_mail($to, $subject, $body, $header);
			}
		}
		return $result;
	}

	/**
	 * @param mixed $data (string|array)
	 * @param string $separator (optional)
	 * @param string $pattern (optional)
	 * @return string
	 */
	public function construct_data($data, $separator = ",", $pattern = '/.*/'){
		if( is_string($data) ){
			$data = trim($data, " \t\n\r\0\x0B");
			$data = explode($separator, $data);
		}
		if( !is_array($data) ) return false;

		$count = count($data);
		for($i = 0; $i < $count; $i++){
			if( is_string($data[$i]) ){
				$data[$i] = trim($data[$i], " \t\n\r\0\x0B");
				if( !preg_match($pattern, $data[$i]) ) unset($data[$i]);
			}else{
				unset($data[$i]);
			}
		}
		return implode($separator, $data);
	}

	/**
	 * @param string $header
	 * @return string
	 */
	 public function group_header($header){
		 if( !is_string($header) ) return false;
		 $result = "";
		 $groups = array();

		 $items = explode("\n", $header);
		 foreach($items as $i){
			 list($k, $v) = explode(":", $i);
			 $k = trim($k, " \t\n\r\0\x0B");
			 $v = trim($v, " \t\n\r\0\x0B");
			 if( !isset($groups[$k]) ) $groups[$k] = array();
			 $groups[$k][] = $v;
		 }

		 while( list($k, $v) = each($groups) ){
			 $result .= $k.": ".implode(", ", $v)."\n";
		 }
		 return $result;
	 }

	/**
	 * @param mixed $to (string|array)
	 */
	public function add_to($to){
		$add = $this->construct_data($to, ",", self::FORMAT_EMAIL_ADDRESS);
		if( $add === false ) return;
		if( !empty($this->to) && $add !== "" ) $this->to .= ",";
		$this->to .= $add;<commit_msg>Delete spaces<commit_after><?php
namespace MOFG_form\Member;

/**
 * Mail class
 *
 * @package MOFG_form
 * @author <NAME>
 * @copyright Copyright (c) 2016 <NAME> mofg.net
 */
class Mail{
	private $to = "";
	private $subject = "";
	private $body = "";
	private $header = "";

	const FORMAT_EMAIL_ADDRESS = '#\A[a-zA-Z0-9.!\#$%&\'*+/=?^_`{|}~-]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+\z#';
	const FORMAT_EMAIL_HEADER = '#\A[a-zA-Z0-9_\-]+ *: *[^:]+\z#';

	/**
	 * @param string $to (optional)
	 * @param string $subject (optional)
	 * @param string $body (optional)
	 * @param string $header (optional)
	 * @return boolean
	 */
	public function send($to = null, $subject = null, $body = null, $header = null){
		if( !isset($to) ){
			$to = $this->to;
			$subject = $this->subject;
			$body = $this->body;
			$header = $this->header;
		}else if( !isset($subject) && !isset($body) ){
			return false;
		}

		$result = false;
		$to = $this->construct_data($to, ",", self::FORMAT_EMAIL_ADDRESS);
		if( !empty($to) ){
			$header = $this->construct_data($header, "\n", self::FORMAT_EMAIL_HEADER);
			if( empty($header) ){
				$result = mb_send_mail($to, $subject, $body);
			}else{
				$header = $this->group_header($header);
				$result = mb_send_mail($to, $subject, $body, $header);
			}
		}
		return $result;
	}

	/**
	 * @param mixed $data (string|array)
	 * @param string $separator (optional)
	 * @param string $pattern (optional)
	 * @return string
	 */
	public function construct_data($data, $separator = ",", $pattern = '/.*/'){
		if( is_string($data) ){
			$data = trim($data, " \t\n\r\0\x0B");
			$data = explode($separator, $data);
		}
		if( !is_array($data) ) return false;

		$count = count($data);
		for($i = 0; $i < $count; $i++){
			if( is_string($data[$i]) ){
				$data[$i] = trim($data[$i], " \t\n\r\0\x0B");
				if( !preg_match($pattern, $data[$i]) ) unset($data[$i]);
			}else{
				unset($data[$i]);
			}
		}
		return implode($separator, $data);
	}

	/**
	 * @param string $header
	 * @return string
	 */
	public function group_header($header){
		if( !is_string($header) ) return false;
		$result = "";
		$groups = array();

		$items = explode("\n", $header);
		foreach($items as $i){
			list($k, $v) = explode(":", $i);
			$k = trim($k, " \t\n\r\0\x0B");
			$v = trim($v, " \t\n\r\0\x0B");
			if( !isset($groups[$k]) ) $groups[$k] = array();
			$groups[$k][] = $v;
		}

		while( list($k, $v) = each($groups) ){
			$result .= $k.": ".implode(", ", $v)."\n";
		}
		return $result;
	}

	/**
	 * @param mixed $to (string|array)
	 */
	public function add_to($to){
		$add = $this->construct_data($to, ",", self::FORMAT_EMAIL_ADDRESS);
		if( $add === false ) return;
		if( !empty($this->to) && $add !== "" ) $this->to .= ",";
		$this->to .= $add;
{'fraction_non_alphanumeric': 0.2489581445914115, 'fraction_numerical': 0.007610074288820439, 'mean_word_length': 3.3636363636363638, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4583620', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2145, 'n_words': 444}