<?php
include_once("postget.php");
include_once("common.php");
include_once("ticketsfunc.php");
include_once("ticketspipefunc.php");
$silent = 'true';
$log = '<hr>';
$query = "SELECT * FROM hd_tickets_dep WHERE server!='' AND port!='' AND user!='' ORDER BY orderby ASC";
$result = mysql_query ($query);
while ($data = mysql_fetch_array ($result))  {
	$host = $data['server'];
	$port = $data['port'];
	$login = $data['user'];
	$password = $data['password'];
	$log .= '
	<b>'.ucfirst(_DATE).':</b> '.date($df.' H:i:s',time()+$config[31][1]*3600).' | 
	<b>'.ucfirst(_HOST_NAME).':</b> '.$host .' | 
	<b>'.ucfirst(_PORT).':</b> '.$port .' | 
	<b>'.ucfirst(_USER).':</b> '.$login .' | 
	<b>'.ucfirst(_EMAIL).':</b> '.$login.' ';

	if ($host == 'imap.gmail.com') {
		$mbox_open = '{' . $host . ':' . $port . '/imap/ssl}INBOX';
		$mbox = @imap_open ($mbox_open, $login, $password);
		$headers = @imap_headers ($mbox);
		if ($mbox == false)      {
			$log .= ' | <font color="red"><b>'.strtoupper(_AUTHENTICATION_FAILED).'</b></font> <br>';
		}else{
			$connectsuccess = true;
		}
	}elseif ($host == 'pop.gmail.com') {

		$mbox_open = '{' . $host . ':' . $port . '/pop3/ssl/novalidate-cert}INBOX';
		$mbox = @imap_open ($mbox_open, $login, $password);
		$headers = @imap_headers ($mbox);
		if ($mbox == false)      {
			$log .= ' | <font color="red"><b>'.strtoupper(_AUTHENTICATION_FAILED).'</b></font> <br>';
		}else{
			$connectsuccess = true;
		}
	}else{
		$mbox_open = '{' . $host . ':' . $port . '/pop3}INBOX';
		$mbox = @imap_open ($mbox_open, $login, $password);
		$headers = @imap_headers ($mbox);
		if ($mbox == false)    {
			$mbox_open = '{' . $host . ':' . $port . '/pop3/notls}INBOX';
			$mbox = @imap_open ($mbox_open, $login, $password);
			$headers = @imap_headers ($mbox);
			if ($mbox == false)      {
				$log .= ' | <font color="red"><b>'.strtoupper(_AUTHENTICATION_FAILED).'</b></font> <br>';
			}else{
				$connectsuccess = true;
			}
		}else{
			$connectsuccess = true;
		}
	}
	if ($connectsuccess)   {
		$emailcount = count ($headers);
		$log .= ' | <b>'.ucfirst(_EMAILS).':</b> '.$emailcount.'<br>';
		$msgno = 1;
		while ($msgno <= $emailcount)    {
			$header_info = getheaders ($mbox, $msgno);
			if ($header_info['to']<>$config[19][1] AND $header_info['fromAddr']<>$config[19][1]) {
				$structure = @imap_fetchstructure ($mbox, $msgno);
				if (1 < sizeof ($structure->parts))    {
					$sections = parse ($structure);
					$attachments = get_attachments ($sections);
				}

				if ($attachments)    {
					$pid = $attachments[0]['pid'];
					$encoding = $attachments[0]['encoding'];
					$attachmentname = $attachments[0]['name'];
					$attachmentdata = @imap_fetchbody ($mbox, $msgno, $pid);
					if ($encoding == 'base64')  {
						$attachmentdata = @imap_base64 ($attachmentdata);
					}

					$extension = explode('.',$attachmentname) ;
					$extension = array_pop($extension) ;
					if (substr_count($config[9][1],$extension)>0) {
						$newfilename = time() . '_' . $attachmentname;
						$fp = fopen ('attachments/' . $newfilename, 'w');
						fwrite ($fp, $attachmentdata);
						fclose ($fp);
					}
				}

				$dataTxt = get_part ($mbox, $msgno, 'TEXT/PLAIN');
				if (!$dataTxt)   {
					$dataTxt = get_part ($mbox, $msgno, 'TEXT/HTML');
					$dataTxt = strip_tags ($dataTxt);
				}

				$msgBody = $dataTxt;
				$msgBody = str_replace ('&nbsp;', ' ', $msgBody);
				processpipedticket ($header_info['to'], $header_info['fromName'], $header_info['fromAddr'], $header_info['subject'], $msgBody, $newfilename);
				@imap_delete ($mbox, $msgno);
			}
			$msgno += 1;
		}

		@imap_expunge ($mbox);
		@imap_close ($mbox);
		continue;
	}
}

$fecha = time()-$config[10][1]*86400 ;
query("UPDATE hd_tickets
SET hd_tickets.status = '5' 
WHERE hd_tickets.lastreply < ".$fecha." AND hd_tickets.status = '3'",0);

?>