-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
186 lines (161 loc) · 3.82 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<html>
<head>
<title>OAuth2 IMAP example with Gmail</title>
</head>
<body>
<?php
/** initial vars **/
$commonWords = array(
'wrote',
'mon',
'tue',
'wed',
'thu',
'fri',
'sat',
'sun',
'jan',
'feb',
'mar',
'apr',
'may',
'jun',
'jul',
'aug',
'sep',
'oct',
'nov',
'dec',
'2010',
'2011',
'2012',
'2013',
'mike',
'eng',
'interaction',
'designer',
'skype',
'mrengy',
'michael',
'kolendowicz',
'sent',
'AM',
'PM',
'sent',
'from',
'my',
'iphone',
);
foreach ($commonWords as &$word){
$word = '/\b' . preg_quote($word, '/') . '\b/i';
}
/*
//adding length filter to end of commonWords array
array_push($commonWords, '~\b\S{30,}\b~');
*/
/**
* Given an open and authenticated IMAP connection, displays some basic info
* about the INBOX folder.
*/
function showInbox($mailbox, $commonWords) {
/**
* Print the INBOX message count and the subject of all messages
* in the INBOX
*/
/*
$storage = new Zend_Mail_Storage_Imap($imap);
include 'header.php';
echo '<h1>Total messages: ' . $storage->countMessages() . "</h1>\n";
echo 'First five messages: <ul>';
for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++ ){
echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n";
}
echo '</ul>';
*/
/**
*Get message IDs from Jack Smooth
*/
//print_r($imap);
$smoothIds = imap_search($mailbox, 'FROM "kolendowicz"');
/*
echo '<br/>';
echo 'Message IDs from Jack Smooth: ';
echo '<br />';
print_r($smoothIds);
*/
echo '<br />';
echo 'Number of smooth messages: ';
echo(count($smoothIds));
//output each message individually
/*
foreach ($smoothIds as $smooth_id){
$email_info = imap_fetch_overview($mailbox,$smooth_id,0);
$message = imap_fetchbody($mailbox,$smooth_id,2);
echo "Subject: " . $email_info[0]->subject . "\n";
echo "Message: " . $message . "\n";
}
*/
//output aggregate message
$messageAggregate = '';
foreach ($smoothIds as $smooth_id){
$message = imap_fetchbody($mailbox,$smooth_id,2);
$messageAggregate .= $message;
}
//$messageFilteredOnce = preg_replace('~\b\S{30,}\b~', '', $messageAggregate);
//$messageFilteredTwice = preg_replace($commonWords, '', $MessageFilteredOnce);
//$messageNoTags = strip_tags($messageAggregate);
$messageFiltered = preg_replace($commonWords, '', $messageAggregate);
$messageShortened = preg_replace('~\b\S{30,}\b~', '', $messageFiltered);
echo $messageShortened;
}
/**
* Tries to login to IMAP and show inbox stats.
*/
function tryImapLogin($email, $password, $commonWords) {
/**
* Make the IMAP connection and send the auth request
*/
/*
$imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
if (oauth2Authenticate($imap, $email, $password)) {
echo '<h1>Successfully authenticated!</h1>';
showInbox($imap);
} else {
echo '<h1>Failed to login</h1>';
}
*/
$imap_host = "{imap.gmail.com:993/imap/ssl}";
$imap_folder = "INBOX"; //it's what is called label in Gmail
print_r($email);
$mailbox = imap_open($imap_host . $imap_folder,$email,$password) or die('Failed to open connection with Gmail: ' . imap_last_error());
if($mailbox){
echo '<h1>Successfully authenticated!</h1>';
showInbox($mailbox, $commonWords);
}
}
/**
* Displays a form to collect the email address and access token.
*/
function displayForm($email, $password) {
echo <<<END
<form method="POST" action="index.php">
<h1>Please enter your e-mail address: </h1>
<input type="text" name="email" value="$email"/>
<p>
<h1>Please enter your password: </h1>
<input type="text" name="password" value="$password"/>
<input type="submit"/>
</form>
<hr>
END;
}
$email = $_POST['email'];
$password = $_POST['password'];
displayForm($email, $password);
//phpinfo();
if ($email && $password) {
tryImapLogin($email, $password, $commonWords);
}
?>
</body>
</html>