Completed
Push — master ( cd335b...070c71 )
by Rudie
02:12
created

test.br-autohelpdesk.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use rdx\imap\IMAPMailbox;
4
5
require 'env.php';
6
require 'autoload.php';
7
8
header('Content-type: text/plain');
9
10
$mbox = new IMAPMailbox(IMAP_HELPDESK_HOST, IMAP_HELPDESK_USER, IMAP_HELPDESK_PASS, 'INBOX', ['novalidate-cert']);
11
12
print_r($mbox->headers());
13
14
$messages = $mbox->messages();
15
var_dump(count($messages));
16
print_r($messages);
17
18
exit;
19
20
$headers = imap_headers($mbox);
21
print_r($headers);
22
23
foreach ( $headers AS $hd ) {
24
	if ( preg_match('/(\d+)\)/', $hd, $match) ) {
25
		$msgNum = $match[1];
26
27
		$hd = imap_headerinfo($mbox, $msgNum);
28
		$new = !!trim($hd->Unseen);
29
30
		if ( $new ) {
31
32
			// subject -- should contain #code#
33
			$title = get_plain_text_subject($mbox, $msgNum, $hd);
34
			$code = preg_match('/#(\d+)#$/', $title, $match) ? (int)$match[1] : 0;
35
echo $title . "\n";
36
37
			// body -- get only last part (no conversation history)
38
			$attachments = array();
39
			$full_body = get_plain_text_body($mbox, $msgNum, $attachments);
40
			$body = get_last_body_part($full_body);
41
			if ( $attachments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attachments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
				$body .= "\n\n== Attachments:\n* " . implode("\n* ", $attachments);
43
			}
44
#echo $full_body . "\n===================================================\n";
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
echo $body . "\n\n===================================================\n===================================================\n===================================================\n";
46
47
			flush();
48
		}
49
50
	}
51
}
52
53
echo "\n";
54
55
imap_close($mbox, CL_EXPUNGE);
56
57
58
function get_last_body_part( $body ) {
59
	$lines = preg_split('/(\r\n|\r|\n)/', $body);
60
61
	$body = '';
62
	foreach ( $lines AS $line ) {
63
		if ( '>' == substr($line, 0, 1) || '-----' == substr($line, 0, 5) || '_____' == substr($line, 0, 5) ) {
64
			break;
65
		}
66
67
		$body .= $line . "\r\n";
68
	}
69
70
	return trim($body);
71
}
72
73
function get_plain_text_subject( $mbox, $msgNum, $headers = null ) {
74
	$headers or $headers = imap_headerinfo($mbox, $msgNum);
75
76
	$subjectParts = imap_mime_header_decode($headers->Subject);
77
	$subject = '';
78
	foreach ( $subjectParts AS $p ) {
79
		$subject .= $p->text;
80
	}
81
82
	return trim($subject);
83
}
84
85
function get_plain_text_body( $mbox, $msgNum, &$attachments = array() ) {
86
	$structure = imap_fetchstructure($mbox, $msgNum);
87
88
	// only plain text
89
	if ( 'PLAIN' == $structure->subtype ) {
90
		return trim(imap_qprint(imap_body($mbox, $msgNum)));
91
	}
92
93
	if ( isset($structure->parts) ) {
94
		// get attachments
95
		foreach ( $structure->parts AS $partNum => $part ) {
96
			if ( in_array($part->subtype, array('JPEG', 'PNG', 'GIF')) ) {
97
				// oeh an image
98
				$name = 'image-from-email-'.$msgNum.'.' . strtolower($part->subtype);
99
				foreach ( $part->parameters AS $param ) {
100
					if ( 'NAME' == $param->attribute ) {
101
						$name = $param->value;
102
					}
103
				}
104
				$data = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
105
				file_put_contents($attachments[] = 'attachments/'.time().'--'.$name, base64_decode($data));
106
			}
107
		}
108
109
		// multipart (probably) -- look for plain text part
110
		foreach ( $structure->parts AS $partNum => $part ) {
111
			if ( 'PLAIN' == $part->subtype ) {
112
				$body = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
113
114
				return trim($body);
115
			}
116
			else if ( 'ALTERNATIVE' == $part->subtype && isset($part->parts) ) {
117
				foreach ( $part->parts AS $subPartNum => $subPart ) {
118
					if ( 'PLAIN' == $subPart->subtype ) {
119
						$body = imap_fetchbody($mbox, $msgNum, ($partNum+1).'.'.($subPartNum+1));
120
121
						return trim($body);
122
					}
123
				}
124
			}
125
		}
126
	}
127
}
128
129
130