<commit_before><?php namespace Nahidz\Imapx;

/*
	This library is for laravel 5. If you using laravel 5 then 
	run this command in your terminal

	"composer require nahidz/imapx"
*/
use Illuminate\Support\Facades\Log;
class Imapx
{
	private $driver;
	private $hostname;
	private $username;
	private $password;
	private $ssl;
	private $novalidate;


	protected $isConnect		= 	false;
	protected $stream			= 	'';
	protected $emails			=	'';
	protected $inbox			=	array();
	protected $msgId			=	0;

	protected $sortBy = [
                        'order' => [
                        	'asc' 	=> 0,
                            'desc' 	=> 1
                             ],

                        'by'    => [
                        	'date' 		=> SORTDATE,
                            'arrival' 	=> SORTARRIVAL,
                            'from' 		=> SORTFROM,
                            'subject' 	=> SORTSUBJECT,
                            'size'		=> SORTSIZE
                                                ]
                        ];


	function __construct()
	{
		if(config('imapx.auto-connect')){

			$this->connect();

		}
	}

	/**
	 * @param null $user
	 * @param null $password
	 * @param null $host
	 * @param null $port
	 * @param null $ssl
	 * @param null $driver
	 * @param null $novalidate
	 * @return bool
	 */
	function connect($user = null, $password = null, $host = null, $port = null, $ssl = null, $driver = null, $novalidate = null)
	{
		$this->driver = !is_null($driver) ? $driver : config('imapx.driver');
		$this->hostname = !is_null($host) ? $host : config('imapx.host');
		$this->username = !is_null($user) ? $user : config('imapx.username');
		$this->password = !is_null($password) ? $password : config('imapx.password');
		$this->port = !is_null($port) ? $port : config('imapx.port');
		$this->port = ':'.$this->port;
		$this->ssl = !is_null($ssl) ? $ssl : config('imapx.ssl') ? '/ssl' : '';
		$this->novalidate = !is_null($novalidate) ? $novalidate : config('imapx.novalidate');
		$this->novalidate = $this->novalidate ? '/novalidate-cert' : '';
		try {
			$this->stream = imap_open('{'.$this->hostname.$this->port.'/'.$this->driver.$this->ssl.$this->novalidate.'}INBOX',$this->username,$this->password);
		} catch (\Exception $e) {
			Log::info('Cannot connect to Imap Server: '.$e->getMessage()."\n");
			return false;
		}
		$this->isConnect = true;
	}


	/*
	* close the current connection
	*/
	function close()
	{
		if(!$this->isConnect) return false;
		imap_close($this->stream);
	}

	public function totalEmail()
	{
		if(!$this->isConnect) return false;

		return imap_num_msg($this->stream);
	}

	public function getInbox($page=1, $perPage=25, $sort=null)
	{
		if(!$this->isConnect) return false;

		$start=$page==1?0:(($page*$perPage)-($perPage-1));
		$order=0;
		$by=SORTDATE;

		if(is_array($sort)){
			$order	= $this->sortBy['order'][$sort[0]];
			$by	= $this->sortBy['by'][$sort[1]];
		}

		$sorted=imap_sort($this->stream, $by, $order);
		$mails = array_chunk($sorted, $perPage);
		$mails = (!empty($mails)) ? $mails[$page-1] : $mails;

		$mbox = imap_check($this->stream);
		$inbox = imap_fetch_overview($this->stream, implode($mails,','), 0);

		if(!is_array($inbox)) return false;

		if(is_array($inbox)){
			$temp_inbox=array();
			foreach($inbox as $msg){
				$temp_inbox[$msg->msgno]=$msg;
			}

			foreach($mails as $msgno){
				$this->inbox[$msgno]=$temp_inbox[$msgno];
			}
		}

		return $this->inbox;
	}


	function readMail($id=null)<commit_msg>Fix imapx connect reutrns<commit_after><?php namespace Nahidz\Imapx;

/*
	This library is for laravel 5. If you using laravel 5 then 
	run this command in your terminal

	"composer require nahidz/imapx"
*/
use Illuminate\Support\Facades\Log;
class Imapx
{
	private $driver;
	private $hostname;
	private $username;
	private $password;
	private $ssl;
	private $novalidate;


	protected $isConnect		= 	false;
	protected $stream			= 	'';
	protected $emails			=	'';
	protected $inbox			=	array();
	protected $msgId			=	0;

	protected $sortBy = [
                        'order' => [
                        	'asc' 	=> 0,
                            'desc' 	=> 1
                             ],

                        'by'    => [
                        	'date' 		=> SORTDATE,
                            'arrival' 	=> SORTARRIVAL,
                            'from' 		=> SORTFROM,
                            'subject' 	=> SORTSUBJECT,
                            'size'		=> SORTSIZE
                                                ]
                        ];


	function __construct()
	{
		if(config('imapx.auto-connect')){

			$this->connect();

		}
	}

	/**
	 * @param null $user
	 * @param null $password
	 * @param null $host
	 * @param null $port
	 * @param null $ssl
	 * @param null $driver
	 * @param null $novalidate
	 * @return bool
	 */
	function connect($user = null, $password = null, $host = null, $port = null, $ssl = null, $driver = null, $novalidate = null)
	{
		$this->driver = !is_null($driver) ? $driver : config('imapx.driver');
		$this->hostname = !is_null($host) ? $host : config('imapx.host');
		$this->username = !is_null($user) ? $user : config('imapx.username');
		$this->password = !is_null($password) ? $password : config('imapx.password');
		$this->port = !is_null($port) ? $port : config('imapx.port');
		$this->port = ':'.$this->port;
		$this->ssl = !is_null($ssl) ? $ssl : config('imapx.ssl') ? '/ssl' : '';
		$this->novalidate = !is_null($novalidate) ? $novalidate : config('imapx.novalidate');
		$this->novalidate = $this->novalidate ? '/novalidate-cert' : '';
		try {
			$this->stream = imap_open('{'.$this->hostname.$this->port.'/'.$this->driver.$this->ssl.$this->novalidate.'}INBOX',$this->username,$this->password);
		} catch (\Exception $e) {
			Log::info('Cannot connect to Imap Server: '.$e->getMessage()."\n");
			return false;
		}
		$this->isConnect = true;
	}


	/*
	* close the current connection
	*/
	function close()
	{
		if(!$this->isConnect) return false;
		imap_close($this->stream);
	}

	public function totalEmail()
	{
		if(!$this->isConnect) return false;

		return imap_num_msg($this->stream);
	}

	public function getInbox($page=1, $perPage=25, $sort=null)
	{
		if(!$this->isConnect) return false;

		$start=$page==1?0:(($page*$perPage)-($perPage-1));
		$order=0;
		$by=SORTDATE;

		if(is_array($sort)){
			$order	= $this->sortBy['order'][$sort[0]];
			$by	= $this->sortBy['by'][$sort[1]];
		}

		$sorted=imap_sort($this->stream, $by, $order);
		$mails = array_chunk($sorted, $perPage);
		if(empty($mails)){
			return false;
		}
		$mails = $mails[$page-1];

		$mbox = imap_check($this->stream);
		$inbox = imap_fetch_overview($this->stream, implode($mails,','), 0);

		if(!is_array($inbox)) return false;

		if(is_array($inbox)){
			$temp_inbox=array();
			foreach($inbox as $msg){
				$temp_inbox[$msg->msgno]=$msg;
			}

			foreach($mails as $msgno){
				$this->inbox[$msgno]=$temp_inbox[$msgno];
			}
		}

		return $this->inbox;
	}


	function readMail($id=null)
{'fraction_non_alphanumeric': 0.18548617051611063, 'fraction_numerical': 0.004562303963501569, 'mean_word_length': 2.6365992742353552, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '53260', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2511, 'n_words': 463}