<commit_before><?php
	session_start();
	require_once("helpers.php");
	class Journal{
		public $db = null;
		public $config = null;
		public $entries = null;
		public $dbFile = null;

		function __construct($database = "journal.db"){
			$this->dbFile = $database;
			$this->db = new SQLite3(dirname(__FILE__)."/".$database);

			$r = $this->db->query("select * from config");
			while($v = $r->fetchArray()) $this->config->$v[1] = $v[2];
			
			$r = $this->db->query("select * from ".$this->config->tblIPs);
			while($v = $r->fetchArray()) $this->config->webIPs[] = $v[1];

			date_default_timezone_set($this->config->timezone);
		}
		function __destruct(){
			$this->db->close();
		}
		public function isAllowedIP($ip){
			if($ip == "fe80::1" || $ip == "127.0.0.1" || empty($this->config->webIPs)) return true;
			else return in_array($ip, $this->config->webIPs);
		}
		public function isInstalled(){
			return is_writeable($this->dbFile) && is_writeable($this->config->mailFile);
		}
		public function readCrontab(){
			return trim(shell_exec("crontab -l -u ".$this->config->cronUser));
		}
		/*
		*      Gets latest mail to the user by reading user's mail file
		*      Usually in /var/mail/username, but can be set in config
		*      This is, by all accounts, a dirty hack
		*      Triggering an event on postfix receive is much, much better.
		*
		*      Returns the raw source of the email.
		*/
		public function getMail(){
			$data = file_get_contents($this->config->mailFile);
			if($data == NULL || trim($data) == "") return false;
			return $data;
		}
		/*
		*      Clears out the user's mailbox file.
		*      Yes, you read that right.
		*      Probably shouldn't be doing this. Could cause major problems.
		*      Should change this to only delete the message passed in.
		*/
		public function deleteMail($mail = NULL){
			if($mail == NULL){
				$f = fopen($this->config->mailFile, 'w');
				fwrite($f, "");
				fclose($f);
				return (trim(file_get_contents($this->config->mailFile)) == "");
			} else {
				$mailFileContents = file_get_contents($this->config->mailFile);
				$f = fopen($this->config->mailFile, 'w');
				$r = fwrite($f, str_replace($mail, "", $mailFileContents));
				fclose($f);
				return !($r == false);
			}
		}
		public function login($password = ""){
			if(!$this->isAllowedIP($_SERVER['REMOTE_ADDR'])) return false;
			$query = "select count(*) as login from ".$this->config->tblUser." where password = '".<PASSWORD>($password)."'";
			$r = $this->db->query($query);
			$a = $r->fetchArray();
			$_SESSION['loggedin'] = $a[0] == 1;
			return $a[0] == 1;
		}
		public function protect($var = true){
			if(!$this->isLoggedIn() || !$var) header("Location: ./");
			return !$this->isLoggedIn();
		}
		public function isLoggedIn(){
			return $_SESSION['loggedin'];
		}
		public function logVisit(){
			$stmt = $this->db->prepare('insert into '.$this->config->tblVisits.' 
										values(NULL, datetime("now"), :ip, :page, :loggedin)');
			$stmt->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
			$stmt->bindValue(':page', $_SERVER['PHP_SELF']);
			$stmt->bindValue(':loggedin', ($this->isLoggedIn() ? 1 : 0));
			return $stmt->execute();
		}
		/*
		 *	Adds a Journal entry to the DB.
		 *	Returns through from DB call. (false on error)
		 *
		 */
		public function submitEntry($sendDate, $receiveDate, $header, $body){
			$stmt = $this->db->prepare('insert into '.$this->config->tblEntries.' 
										values(NULL, datetime(:send, "unixepoch"), datetime(:receive, "unixepoch"), :header, :body, 0)');
			$stmt->bindValue(':send', $sendDate);
			$stmt->bindValue(':receive', $receiveDate);
			$stmt->bindValue(':header', htmlentities($header));
			$stmt->bindValue(':body', htmlentities($body));
			return $stmt->execute();
		}

		/*
		 *	Gets a random journal entry that hasn't been reflected at the user yet.
		 *	Used for peeks back at the past.
		 *	After grabbing a row, it automatically sets its "reflected" flag to 1 (true)
		 *	Returns array of row data or false on no historical rows left.
		 *
		 */
		public function getRandomEntry(){
			$entry = $this->db->querySingle("select * from ".$this->config->tblEntries." where reflected = 0 order by random() limit 1", true);
			if($entry){
				$this->db->query("update ".$this->config->tblEntries." set reflected = 1 where id = ".$entry['id']);
				return $entry;
			} else return false;
		}
		public function getAllEntries(){
			$q = $this->db->query("select * from ".$this->config->tblEntries." order by sent desc, received asc");
			while($row = $q->fetchArray()){$in[] = $row;}
			foreach($in as $key => $row){
				$out[date("Y-m", strtotime($row['sent']." GMT"))][date("Y-m-d", strtotime($row['sent']." GMT"))][] = $row;
			}
			$this->entries = $out;
			return $out;
		}
		public function countDays(){
			$count = 0;
			foreach($this->entries as $month => $days)
				foreach($days as $day => $entries) $count += count($entries);
			return $count;
		}
		public function countTotalDays(){
			$count = 0;
			foreach(array_keys($this->entries) as $month){
				if(date("n", strtotime($month)) == date("n")) $count += date("j") - (time() < strtotime($this->config->emailTime));
				else $count += date("t", strtotime($month));
			}
			return $count;
		}

		/*
		 *	Grabs only the latest email from your email address.
		 *	Could be improved by checking more in the headers for the string "ohJournal."
		 *
		 */
		public function parseMailFile($raw){
			if($raw === false) return false;
			$s = array(	".", 	"+"		);
			$r = array(	"\.", 	"\+"	);
			$m = preg_match("/^(From ".str_replace(".", "\.", $this->config->responseEmail()).".+)(^From )?/ms", $raw, $matches);
			if($m)	return trim($matches[1]);
			else return null;
		}
<commit_msg>Fixed bounce from responseEmail in receiving emails.<commit_after><?php
	session_start();
	require_once("helpers.php");
	class Journal{
		public $db = null;
		public $config = null;
		public $entries = null;
		public $dbFile = null;

		function __construct($database = "journal.db"){
			$this->dbFile = $database;
			$this->db = new SQLite3(dirname(__FILE__)."/".$database);

			$r = $this->db->query("select * from config");
			while($v = $r->fetchArray()) $this->config->$v[1] = $v[2];
			
			$r = $this->db->query("select * from ".$this->config->tblIPs);
			while($v = $r->fetchArray()) $this->config->webIPs[] = $v[1];

			date_default_timezone_set($this->config->timezone);
		}
		function __destruct(){
			$this->db->close();
		}
		public function isAllowedIP($ip){
			if($ip == "fe80::1" || $ip == "127.0.0.1" || empty($this->config->webIPs)) return true;
			else return in_array($ip, $this->config->webIPs);
		}
		public function isInstalled(){
			return is_writeable($this->dbFile) && is_writeable($this->config->mailFile);
		}
		public function readCrontab(){
			return trim(shell_exec("crontab -l -u ".$this->config->cronUser));
		}
		/*
		*      Gets latest mail to the user by reading user's mail file
		*      Usually in /var/mail/username, but can be set in config
		*      This is, by all accounts, a dirty hack
		*      Triggering an event on postfix receive is much, much better.
		*
		*      Returns the raw source of the email.
		*/
		public function getMail(){
			$data = file_get_contents($this->config->mailFile);
			if($data == NULL || trim($data) == "") return false;
			return $data;
		}
		/*
		*      Clears out the user's mailbox file.
		*      Yes, you read that right.
		*      Probably shouldn't be doing this. Could cause major problems.
		*      Should change this to only delete the message passed in.
		*/
		public function deleteMail($mail = NULL){
			if($mail == NULL){
				$f = fopen($this->config->mailFile, 'w');
				fwrite($f, "");
				fclose($f);
				return (trim(file_get_contents($this->config->mailFile)) == "");
			} else {
				$mailFileContents = file_get_contents($this->config->mailFile);
				$f = fopen($this->config->mailFile, 'w');
				$r = fwrite($f, str_replace($mail, "", $mailFileContents));
				fclose($f);
				return !($r == false);
			}
		}
		public function login($password = ""){
			if(!$this->isAllowedIP($_SERVER['REMOTE_ADDR'])) return false;
			$query = "select count(*) as login from ".$this->config->tblUser." where password = '".<PASSWORD>($password)."'";
			$r = $this->db->query($query);
			$a = $r->fetchArray();
			$_SESSION['loggedin'] = $a[0] == 1;
			return $a[0] == 1;
		}
		public function protect($var = true){
			if(!$this->isLoggedIn() || !$var) header("Location: ./");
			return !$this->isLoggedIn();
		}
		public function isLoggedIn(){
			return $_SESSION['loggedin'];
		}
		public function logVisit(){
			$stmt = $this->db->prepare('insert into '.$this->config->tblVisits.' 
										values(NULL, datetime("now"), :ip, :page, :loggedin)');
			$stmt->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
			$stmt->bindValue(':page', $_SERVER['PHP_SELF']);
			$stmt->bindValue(':loggedin', ($this->isLoggedIn() ? 1 : 0));
			return $stmt->execute();
		}
		/*
		 *	Adds a Journal entry to the DB.
		 *	Returns through from DB call. (false on error)
		 *
		 */
		public function submitEntry($sendDate, $receiveDate, $header, $body){
			$stmt = $this->db->prepare('insert into '.$this->config->tblEntries.' 
										values(NULL, datetime(:send, "unixepoch"), datetime(:receive, "unixepoch"), :header, :body, 0)');
			$stmt->bindValue(':send', $sendDate);
			$stmt->bindValue(':receive', $receiveDate);
			$stmt->bindValue(':header', htmlentities($header));
			$stmt->bindValue(':body', htmlentities($body));
			return $stmt->execute();
		}

		/*
		 *	Gets a random journal entry that hasn't been reflected at the user yet.
		 *	Used for peeks back at the past.
		 *	After grabbing a row, it automatically sets its "reflected" flag to 1 (true)
		 *	Returns array of row data or false on no historical rows left.
		 *
		 */
		public function getRandomEntry(){
			$entry = $this->db->querySingle("select * from ".$this->config->tblEntries." where reflected = 0 order by random() limit 1", true);
			if($entry){
				$this->db->query("update ".$this->config->tblEntries." set reflected = 1 where id = ".$entry['id']);
				return $entry;
			} else return false;
		}
		public function getAllEntries(){
			$q = $this->db->query("select * from ".$this->config->tblEntries." order by sent desc, received asc");
			while($row = $q->fetchArray()){$in[] = $row;}
			foreach($in as $key => $row){
				$out[date("Y-m", strtotime($row['sent']." GMT"))][date("Y-m-d", strtotime($row['sent']." GMT"))][] = $row;
			}
			$this->entries = $out;
			return $out;
		}
		public function countDays(){
			$count = 0;
			foreach($this->entries as $month => $days)
				foreach($days as $day => $entries) $count += count($entries);
			return $count;
		}
		public function countTotalDays(){
			$count = 0;
			foreach(array_keys($this->entries) as $month){
				if(date("n", strtotime($month)) == date("n")) $count += date("j") - (time() < strtotime($this->config->emailTime));
				else $count += date("t", strtotime($month));
			}
			return $count;
		}

		/*
		 *	Grabs only the latest email from your email address.
		 *	Could be improved by checking more in the headers for the string "ohJournal."
		 *
		 */
		public function parseMailFile($raw){
			if($raw === false) return false;
			$s = array(	".", 	"+"		);
			$r = array(	"\.", 	"\+"	);
			$m = preg_match("/^(From ".str_replace(".", "\.", $this->responseEmail()).".+)(^From )?/ms", $raw, $matches);
			if($m)	return trim($matches[1]);
			else return null;
		}

{'fraction_non_alphanumeric': 0.1979103178058337, 'fraction_numerical': 0.004701784936874184, 'mean_word_length': 3.5779194898365883, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 176, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2169038', 'n_tokens_mistral': 4421, 'n_tokens_neox': 3817, 'n_words': 968}