<commit_before><?php

namespace Nettools\Core\ExceptionHandlers\StackTraceFormatters;





/**
 * Extends StackTraceFormatter and implements output of exception stack trace through HTML mail and simple onscreen output
 */
class PublicStackTraceFormatter extends HtmlStackTraceFormatter
{
	/**
	 * Constructor
	 */
	function __construct()
	{
		// hide stack trace
		parent::__construct(false);
	}
	
	
	
	/**
	 * Get mail recipient ; by default, send error to <EMAIL>
	 *
	 * @return string
	 */
	protected function getRecipient()
	{
		return \K_NETTOOLS_POSTMASTER;
	}
	
	
	
	/**
	 * Get mail sender ; by default, send error from <EMAIL>
	 *
	 * @return string
	 */
	protected function getSender()
	{
		return 'exception-handler@' . $_SERVER['HTTP_HOST'];
	}
	
	
	
	/**
	 * Get mail subject
	 *
     * @param \Throwable $e Exception object
     * @param string $h1 The title displayed on the error page
	 * @return string
	 */
	protected function getSubject(\Throwable $e, $h1 = 'An error occured')
	{
		return "Exception $h1 on " . $_SERVER['HTTP_HOST'];
	}
	
	
	
    /**
     * Get a string with exception stack trace properly formatted
     * 
     * @param \Throwable $e Exception object
     * @param string $h1 The title displayed on the error page
     * @return string Returns a string with $e exception stack trace properly formatted to be human-readable
     */
    public function format(\Throwable $e, $h1 = 'An error occured')
    {
		// get exception details WITH stack trace for emailing
		$html = (new HtmlStackTraceFormatter(true))->format($e, $h1);

		$sep = sha1(uniqid());  
		$headers = ["Content-Type" => "multipart/mixed; boundary=\"$sep\"",
					"From" => $this->getSender()];
		$msg = 	"--$sep\r\nContent-Type: text/plain;\r\n\r\nSee attachment.\r\n\r\n" .
				"--$sep\r\nContent-Type: text/html; name=\"stack-trace.html\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"stack-trace.html\"\r\n\r\n" . trim(chunk_split(base64_encode($html))) . "\r\n\r\n" .
				"--$sep--";
		
		// send mail
		mail($this->getRecipient(), $this->getSubject($e, $h1), $msg, $headers);
		
		
		// get exception string as HTML, with no stack trace
		return parent::format($e, $h1);	
	}
}

?><commit_msg>php < 7.2 compatibility : mail doesn't accept array for headers before 7.2<commit_after><?php

namespace Nettools\Core\ExceptionHandlers\StackTraceFormatters;





/**
 * Extends StackTraceFormatter and implements output of exception stack trace through HTML mail and simple onscreen output
 */
class PublicStackTraceFormatter extends HtmlStackTraceFormatter
{
	/**
	 * Constructor
	 */
	function __construct()
	{
		// hide stack trace
		parent::__construct(false);
	}
	
	
	
	/**
	 * Get mail recipient ; by default, send error to <EMAIL>
	 *
	 * @return string
	 */
	protected function getRecipient()
	{
		return \K_NETTOOLS_POSTMASTER;
	}
	
	
	
	/**
	 * Get mail sender ; by default, send error from <EMAIL>
	 *
	 * @return string
	 */
	protected function getSender()
	{
		return 'exception-handler@' . $_SERVER['HTTP_HOST'];
	}
	
	
	
	/**
	 * Get mail subject
	 *
     * @param \Throwable $e Exception object
     * @param string $h1 The title displayed on the error page
	 * @return string
	 */
	protected function getSubject(\Throwable $e, $h1 = 'An error occured')
	{
		return "Exception $h1 on " . $_SERVER['HTTP_HOST'];
	}
	
	
	
    /**
     * Get a string with exception stack trace properly formatted
     * 
     * @param \Throwable $e Exception object
     * @param string $h1 The title displayed on the error page
     * @return string Returns a string with $e exception stack trace properly formatted to be human-readable
     */
    public function format(\Throwable $e, $h1 = 'An error occured')
    {
		// get exception details WITH stack trace for emailing
		$html = (new HtmlStackTraceFormatter(true))->format($e, $h1);

		$sep = sha1(uniqid());  
		$headers = "Content-Type: multipart/mixed; boundary=\"$sep\"\r\n" .
					"From: {$this->getSender()};";
		$msg = 	"--$sep\r\nContent-Type: text/plain;\r\n\r\nSee attachment.\r\n\r\n" .
				"--$sep\r\nContent-Type: text/html; name=\"stack-trace.html\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"stack-trace.html\"\r\n\r\n" . trim(chunk_split(base64_encode($html))) . "\r\n\r\n" .
				"--$sep--";
		
		// send mail
		mail($this->getRecipient(), $this->getSubject($e, $h1), $msg, $headers);
		
		
		// get exception string as HTML, with no stack trace
		return parent::format($e, $h1);	
	}
}

?>
{'fraction_non_alphanumeric': 0.1426370239929562, 'fraction_numerical': 0.006603565925599824, 'mean_word_length': 3.172635445362718, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 19, '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': '5529221', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1420, 'n_words': 400}