Completed
Push — master ( 62d6d0...3be28e )
by Rudie
02:13
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
<pre><?php
2
3
require 'env.php';
4
require 'IMAP.php';
5
6
$mbox = imap_open('{' . IMAP_HELPDESK_HOST . '/novalidate-cert}INBOX', IMAP_HELPDESK_USER, IMAP_HELPDESK_PASS);
7
8
$headers = imap_headers($mbox);
9
print_r($headers);
10
11
foreach ( $headers AS $hd ) {
12
	if ( preg_match('/(\d+)\)/', $hd, $match) ) {
13
		$msgNum = $match[1];
14
15
		$hd = imap_headerinfo($mbox, $msgNum);
16
		$new = !!trim($hd->Unseen);
17
18
		if ( $new ) {
19
20
			// subject -- should contain #code#
21
			$title = get_plain_text_subject($mbox, $msgNum, $hd);
22
			$code = preg_match('/#(\d+)#$/', $title, $match) ? (int)$match[1] : 0;
23
echo $title . "\n";
24
25
			// body -- get only last part (no conversation history)
26
			$attachments = array();
27
			$full_body = get_plain_text_body($mbox, $msgNum, $attachments);
28
			$body = get_last_body_part($full_body);
29
			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...
30
				$body .= "\n\n== Attachments:\n* " . implode("\n* ", $attachments);
31
			}
32
#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...
33
echo $body . "\n\n===================================================\n===================================================\n===================================================\n";
34
35
			flush();
36
		}
37
38
	}
39
}
40
41
echo "\n";
42
43
imap_close($mbox, CL_EXPUNGE);
44
45
46
function get_last_body_part( $body ) {
47
	$lines = preg_split('/(\r\n|\r|\n)/', $body);
48
49
	$body = '';
50
	foreach ( $lines AS $line ) {
51
		if ( '>' == substr($line, 0, 1) || '-----' == substr($line, 0, 5) || '_____' == substr($line, 0, 5) ) {
52
			break;
53
		}
54
55
		$body .= $line . "\r\n";
56
	}
57
58
	return trim($body);
59
}
60
61
function get_plain_text_subject( $mbox, $msgNum, $headers = null ) {
62
	$headers or $headers = imap_headerinfo($mbox, $msgNum);
63
64
	$subjectParts = imap_mime_header_decode($headers->Subject);
65
	$subject = '';
66
	foreach ( $subjectParts AS $p ) {
67
		$subject .= $p->text;
68
	}
69
70
	return trim($subject);
71
}
72
73
function get_plain_text_body( $mbox, $msgNum, &$attachments = array() ) {
74
	$structure = imap_fetchstructure($mbox, $msgNum);
75
76
	// only plain text
77
	if ( 'PLAIN' == $structure->subtype ) {
78
		return trim(imap_qprint(imap_body($mbox, $msgNum)));
79
	}
80
81
	if ( isset($structure->parts) ) {
82
		// get attachments
83
		foreach ( $structure->parts AS $partNum => $part ) {
84
			if ( in_array($part->subtype, array('JPEG', 'PNG', 'GIF')) ) {
85
				// oeh an image
86
				$name = 'image-from-email-'.$msgNum.'.' . strtolower($part->subtype);
87
				foreach ( $part->parameters AS $param ) {
88
					if ( 'NAME' == $param->attribute ) {
89
						$name = $param->value;
90
					}
91
				}
92
				$data = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
93
				file_put_contents($attachments[] = 'attachments/'.time().'--'.$name, base64_decode($data));
94
			}
95
		}
96
97
		// multipart (probably) -- look for plain text part
98
		foreach ( $structure->parts AS $partNum => $part ) {
99
			if ( 'PLAIN' == $part->subtype ) {
100
				$body = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
101
102
				return trim($body);
103
			}
104
			else if ( 'ALTERNATIVE' == $part->subtype && isset($part->parts) ) {
105
				foreach ( $part->parts AS $subPartNum => $subPart ) {
106
					if ( 'PLAIN' == $subPart->subtype ) {
107
						$body = imap_fetchbody($mbox, $msgNum, ($partNum+1).'.'.($subPartNum+1));
108
109
						return trim($body);
110
					}
111
				}
112
			}
113
		}
114
	}
115
}
116
117
118