<?php
# Settings
$apiKey
=
'Your API key here'
;
$apiSecret
=
'Your API secret here'
;
$loggingEnabled
= true;
$emailsInputFile
= __DIR__ .
'/maillist.txt'
;
$emailsValidatedFile
= __DIR__ .
'/maillist-verified.txt'
;
$logFile
= __DIR__ .
'/maillist-log.txt'
;
$thisUrl
=
"http"
. (!
empty
(
$_SERVER
[
'HTTPS'
])?
"s"
:
""
) .
"://"
.
$_SERVER
[
'SERVER_NAME'
]
.
$_SERVER
[
'REQUEST_URI'
];
$emailsPerRequest
= 100;
ini_set
(
'display_errors'
, 1);
error_reporting
(E_ALL);
function
fileAppend(
$fileName
,
$data
)
{
$fp
=
fopen
(
$fileName
,
"a"
);
flock
(
$fp
, LOCK_EX);
fwrite(
$fp
,
$data
);
flock
(
$fp
, LOCK_UN);
fclose(
$fp
);
}
function
mlog(
$message
)
{
global
$logFile
,
$loggingEnabled
;
if
(
$loggingEnabled
)
{
$dateTime
=
new
\DateTime(
'now'
,
new
\DateTimeZone(
'UTC'
));
$time
=
$dateTime
->format(
'Y-m-d H:i:s'
);
$line
=
'['
.
$time
.
'] '
.
$message
.
"\n"
;
fileAppend(
$logFile
,
$line
);
}
}
function
writeOutput(
$message
)
{
global
$emailsValidatedFile
;
$line
=
$message
.
"\n"
;
fileAppend(
$emailsValidatedFile
,
$line
);
}
function
odtSendRequest(
$key
,
$secret
,
$action
,
array
$postData
)
{
$postFields
= http_build_query(
$postData
);
$dateTime
=
new
\DateTime(
'now'
,
new
\DateTimeZone(
'UTC'
));
$time
=
$dateTime
->format(
'Y-m-d H:i:s'
);
$message
=
$key
.
$time
.
$postFields
;
$signature
= hash_hmac(
'sha512'
,
$message
,
$secret
);
$headers
=
array
(
'Sign: '
.
$signature
,
'Time: '
.
$time
,
'Key: '
.
$key
);
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
.
$action
.
'/'
);
curl_setopt(
$ch
, CURLOPT_CUSTOMREQUEST,
"POST"
);
curl_setopt(
$ch
, CURLOPT_POST, 1);
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$postFields
);
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
$headers
);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt(
$ch
, CURLOPT_TIMEOUT, 240);
$data
= curl_exec(
$ch
);
if
(
$data
=== false)
{
$error
= curl_error(
$ch
);
$result
=
array
(false,
$error
);
}
else
{
$result
=
array
(true,
$data
);
}
curl_close(
$ch
);
return
$result
;
}
function
initialization()
{
mlog(
"initialization()"
);
$result
= false;
global
$emailsInputFile
,
$emailsValidatedFile
,
$logFile
;
$inputContents
=
file_get_contents
(
$emailsInputFile
);
if
(
$inputContents
!== false)
{
$res
=
file_put_contents
(
$emailsInputFile
,
$inputContents
);
if
(
$res
!== false)
{
$res
=
file_put_contents
(
$emailsValidatedFile
,
""
);
if
(
$res
!== false)
{
$res
=
file_put_contents
(
$logFile
,
""
);
if
(
$res
!== false)
{
$result
= true;
}
else
mlog(
"initialization(): ERROR: Unable to write to log file '$logFile'."
);
}
else
mlog(
"initialization(): ERROR: Unable to write to output file "
.
"'$emailsValidatedFile'."
);
}
else
mlog(
"initialization(): ERROR: Unable to write to input file '$emailsInputFile'."
);
}
else
mlog(
"initialization(): ERROR: Unable to read input file '$emailsInputFile'."
);
mlog(
"initialization(-):"
. (int)
$result
);
return
$result
;
}
function
processIncomingData()
{
mlog(
"processIncomingData()"
);
$result
= false;
if
(
$json
)
{
$jsonLen
=
strlen
(
$json
);
mlog(
"processIncomingData(): Incoming data ($jsonLen bytes) detected "
.
"(first 256 bytes):\n"
.
substr
(
$json
, 0, 256) .
"\n"
);
$response
= json_decode(
$json
, true);
if
(
$response
!== null)
{
if
(
$response
[
'success'
] == 1)
{
mlog(
"processIncomingData(): Call succeeded."
);
$output
=
$response
[
'output'
];
$emailsInfo
=
$output
[
'emailsInfo'
];
$counter
= 0;
foreach
(
$emailsInfo
as
&
$info
)
{
mlog(
"processIncomingData(): Email "
.
$info
[
'email'
] .
" status is "
.
$info
[
'finalStatus'
] .
"."
);
if
((
$info
[
'finalStatus'
] == 2) || (
$info
[
'finalStatus'
] == 3)
|| (
$info
[
'finalStatus'
] == 4))
{
writeOutput(
$info
[
'email'
]);
}
$counter
++;
}
mlog(
"processIncomingData(): $counter emails processed."
);
$result
= removeEmailsFromInputFile(
$counter
);
}
else
mlog(
"processIncomingData(): ERROR: Call failed with error message: '"
.
$response
[
'message'
] .
"'"
);
}
else
mlog(
"processIncomingData(): ERROR: Call failed. Server response is not a valid"
.
" JSON message."
);
}
else
{
mlog(
"processIncomingData(): No incoming data. This is first call, perform file access"
.
" rights checks."
);
$result
= initialization();
}
mlog(
"processIncomingData(-):"
. (int)
$result
);
return
$result
;
}
function
loadEmailsFromInputFile()
{
mlog(
"loadEmailsFromInputFile()"
);
global
$emailsInputFile
,
$emailsPerRequest
;
$result
=
array
();
$fp
=
fopen
(
$emailsInputFile
,
"r"
);
if
(
$fp
)
{
$remain
=
$emailsPerRequest
;
while
(((
$line
=
fgets
(
$fp
)) !== false) && (
$remain
> 0))
{
$result
[] =
$line
;
$remain
--;
}
fclose(
$fp
);
}
else
mlog(
"loadEmailsFromInputFile(): ERROR: Unable to open file '$emailsInputFile'."
);
mlog(
"loadEmailsFromInputFile(-)"
);
return
$result
;
}
function
removeEmailsFromInputFile(
$count
)
{
mlog(
"removeEmailsFromInputFile(count:$count)"
);
global
$emailsInputFile
;
$result
= false;
$lines
= file(
$emailsInputFile
, FILE_IGNORE_NEW_LINES);
if
(
$lines
!== false)
{
$newLines
=
array_slice
(
$lines
,
$count
);
$newContent
= implode(
"\n"
,
$newLines
);
$res
=
file_put_contents
(
$emailsInputFile
,
$newContent
);
if
(
$res
!== false)
{
$result
= true;
}
else
mlog(
"removeEmailsFromInputFile(): ERROR: Unable to write to file"
.
" '$emailsInputFile'."
);
}
else
mlog(
"removeEmailsFromInputFile(): ERROR: Unable to read from file"
.
" '$emailsInputFile'."
);
mlog(
"removeEmailsFromInputFile(-):"
. (int)
$result
);
return
$result
;
}
echo
"ODT: OK"
;
if
(processIncomingData())
{
$emailList
= loadEmailsFromInputFile();
$count
=
count
(
$emailList
);
if
(
$count
> 0)
{
$commaSeparatedList
= implode(
","
,
$emailList
);
list(
$isHttpRequestOk
,
$result
) = odtSendRequest(
$apiKey
,
$apiSecret
,
"tool/email-verifier/check"
,
array
(
"emails"
=>
$commaSeparatedList
,
"asyncCallback"
=>
$thisUrl
));
if
(
$isHttpRequestOk
)
{
$response
= json_decode(
$result
, true);
if
(
$response
!== null)
{
if
(
$response
[
'success'
] == 1)
{
mlog(
"API call succeeded, waiting for callback ..."
);
}
else
mlog(
"ERROR: API call failed with error: "
.
$response
[
'message'
] .
"."
);
}
else
mlog(
"ERROR: API call failed. Server response is not a valid JSON message."
);
}
else
mlog(
"ERROR: HTTP request failed with error: "
.
$result
.
"."
);
}
else
mlog(
"No more emails to process, all done!"
);
}
else
mlog(
"ERROR: Processing incoming data failed, stopping execution."
);