Merge pull request #21988 from fboitel/imap-by-oauth-patch
Refresh button on emailcollector card
This commit is contained in:
commit
19ab213459
@ -34,6 +34,16 @@ include_once DOL_DOCUMENT_ROOT.'/emailcollector/class/emailcollectorfilter.class
|
||||
include_once DOL_DOCUMENT_ROOT.'/emailcollector/class/emailcollectoraction.class.php';
|
||||
include_once DOL_DOCUMENT_ROOT.'/emailcollector/lib/emailcollector.lib.php';
|
||||
|
||||
// use Webklex\PHPIMAP;
|
||||
require DOL_DOCUMENT_ROOT.'/includes/webklex/php-imap/vendor/autoload.php';
|
||||
use Webklex\PHPIMAP\ClientManager;
|
||||
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
|
||||
use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException;
|
||||
|
||||
|
||||
use OAuth\Common\Storage\DoliStorage;
|
||||
use OAuth\Common\Consumer\Credentials;
|
||||
|
||||
if (!$user->admin) {
|
||||
accessforbidden();
|
||||
}
|
||||
@ -377,50 +387,142 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
|
||||
$connectstringserver = $object->getConnectStringIMAP($usessl);
|
||||
|
||||
if ($action == 'scan') {
|
||||
try {
|
||||
if ($sourcedir) {
|
||||
//$connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir);
|
||||
$connectstringsource = $connectstringserver.$object->getEncodedUtf7($sourcedir);
|
||||
if (!empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
|
||||
if ($object->acces_type == 1) {
|
||||
// Mode OAUth2 with PHP-IMAP
|
||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
|
||||
$keyforsupportedoauth2array = $object->oauth_service;
|
||||
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
|
||||
$keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
|
||||
} else {
|
||||
$keyforprovider = '';
|
||||
}
|
||||
$keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
|
||||
$keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
|
||||
|
||||
$OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
|
||||
//$debugtext = "Host: ".$this->host."<br>Port: ".$this->port."<br>Login: ".$this->login."<br>Password: ".$this->password."<br>access type: ".$this->acces_type."<br>oauth service: ".$this->oauth_service."<br>Max email per collect: ".$this->maxemailpercollect;
|
||||
//dol_syslog($debugtext);
|
||||
|
||||
$storage = new DoliStorage($db, $conf);
|
||||
|
||||
try {
|
||||
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
|
||||
$expire = true;
|
||||
// Is token expired or will token expire in the next 30 seconds
|
||||
// if (is_object($tokenobj)) {
|
||||
// $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
|
||||
// }
|
||||
// Token expired so we refresh it
|
||||
if (is_object($tokenobj) && $expire) {
|
||||
$credentials = new Credentials(
|
||||
getDolGlobalString('OAUTH_'.$object->oauth_service.'_ID'),
|
||||
getDolGlobalString('OAUTH_'.$object->oauth_service.'_SECRET'),
|
||||
getDolGlobalString('OAUTH_'.$object->oauth_service.'_URLAUTHORIZE')
|
||||
);
|
||||
$serviceFactory = new \OAuth\ServiceFactory();
|
||||
$oauthname = explode('-', $OAUTH_SERVICENAME);
|
||||
// ex service is Google-Emails we need only the first part Google
|
||||
$apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
|
||||
// We have to save the token because Google give it only once
|
||||
$refreshtoken = $tokenobj->getRefreshToken();
|
||||
$tokenobj = $apiService->refreshAccessToken($tokenobj);
|
||||
$tokenobj->setRefreshToken($refreshtoken);
|
||||
$storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
|
||||
}
|
||||
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
|
||||
if (is_object($tokenobj)) {
|
||||
$token = $tokenobj->getAccessToken();
|
||||
} else {
|
||||
$object->error = "Token not found";
|
||||
return -1;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
print $e->getMessage();
|
||||
}
|
||||
|
||||
$cm = new ClientManager();
|
||||
$client = $cm->make([
|
||||
'host' => $object->host,
|
||||
'port' => $object->port,
|
||||
'encryption' => 'ssl',
|
||||
'validate_cert' => true,
|
||||
'protocol' => 'imap',
|
||||
'username' => $object->login,
|
||||
'password' => $token,
|
||||
'authentication' => "oauth",
|
||||
]);
|
||||
} else {
|
||||
// Mode login/pass with PHP-IMAP
|
||||
$cm = new ClientManager();
|
||||
$client = $cm->make([
|
||||
'host' => $object->host,
|
||||
'port' => $object->port,
|
||||
'encryption' => 'ssl',
|
||||
'validate_cert' => true,
|
||||
'protocol' => 'imap',
|
||||
'username' => $object->login,
|
||||
'password' => $object->password,
|
||||
'authentication' => "login",
|
||||
]);
|
||||
}
|
||||
if ($targetdir) {
|
||||
//$connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir);
|
||||
$connectstringtarget = $connectstringserver.$object->getEncodedUtf7($targetdir);
|
||||
try {
|
||||
$client->connect();
|
||||
} catch (ConnectionFailedException $e) {
|
||||
print $e->getMessage();
|
||||
}
|
||||
|
||||
$timeoutconnect = empty($conf->global->MAIN_USE_CONNECT_TIMEOUT) ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT;
|
||||
$timeoutread = empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT) ? 20 : $conf->global->MAIN_USE_RESPONSE_TIMEOUT;
|
||||
|
||||
dol_syslog("imap_open connectstring=".$connectstringsource." login=".$object->login." password=".$object->password." timeoutconnect=".$timeoutconnect." timeoutread=".$timeoutread);
|
||||
|
||||
$result1 = imap_timeout(IMAP_OPENTIMEOUT, $timeoutconnect); // timeout seems ignored with ssl connect
|
||||
$result2 = imap_timeout(IMAP_READTIMEOUT, $timeoutread);
|
||||
$result3 = imap_timeout(IMAP_WRITETIMEOUT, 5);
|
||||
$result4 = imap_timeout(IMAP_CLOSETIMEOUT, 5);
|
||||
|
||||
dol_syslog("result1=".$result1." result2=".$result2." result3=".$result3." result4=".$result4);
|
||||
|
||||
$connection = imap_open($connectstringsource, $object->login, $object->password);
|
||||
|
||||
//dol_syslog("end imap_open connection=".var_export($connection, true));
|
||||
} catch (Exception $e) {
|
||||
print $e->getMessage();
|
||||
}
|
||||
|
||||
if (!$connection) {
|
||||
$morehtml .= 'Failed to open IMAP connection '.$connectstringsource;
|
||||
if (function_exists('imap_last_error')) {
|
||||
$morehtml .= '<br>'.imap_last_error();
|
||||
}
|
||||
dol_syslog("Error ".$morehtml, LOG_WARNING);
|
||||
//var_dump(imap_errors())
|
||||
$f = $client->getFolders(false, $object->source_directory);
|
||||
$nbemail = $f[0]->examine()["exists"];
|
||||
$morehtml .= $nbemail;
|
||||
} else {
|
||||
dol_syslog("Imap connected. Now we call imap_num_msg()");
|
||||
$morehtml .= imap_num_msg($connection);
|
||||
}
|
||||
try {
|
||||
if ($sourcedir) {
|
||||
//$connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir);
|
||||
$connectstringsource = $connectstringserver.$object->getEncodedUtf7($sourcedir);
|
||||
}
|
||||
if ($targetdir) {
|
||||
//$connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir);
|
||||
$connectstringtarget = $connectstringserver.$object->getEncodedUtf7($targetdir);
|
||||
}
|
||||
|
||||
if ($connection) {
|
||||
dol_syslog("Imap close");
|
||||
imap_close($connection);
|
||||
$timeoutconnect = empty($conf->global->MAIN_USE_CONNECT_TIMEOUT) ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT;
|
||||
$timeoutread = empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT) ? 20 : $conf->global->MAIN_USE_RESPONSE_TIMEOUT;
|
||||
|
||||
dol_syslog("imap_open connectstring=".$connectstringsource." login=".$object->login." password=".$object->password." timeoutconnect=".$timeoutconnect." timeoutread=".$timeoutread);
|
||||
|
||||
$result1 = imap_timeout(IMAP_OPENTIMEOUT, $timeoutconnect); // timeout seems ignored with ssl connect
|
||||
$result2 = imap_timeout(IMAP_READTIMEOUT, $timeoutread);
|
||||
$result3 = imap_timeout(IMAP_WRITETIMEOUT, 5);
|
||||
$result4 = imap_timeout(IMAP_CLOSETIMEOUT, 5);
|
||||
|
||||
dol_syslog("result1=".$result1." result2=".$result2." result3=".$result3." result4=".$result4);
|
||||
|
||||
$connection = imap_open($connectstringsource, $object->login, $object->password);
|
||||
|
||||
//dol_syslog("end imap_open connection=".var_export($connection, true));
|
||||
} catch (Exception $e) {
|
||||
print $e->getMessage();
|
||||
}
|
||||
|
||||
if (!$connection) {
|
||||
$morehtml .= 'Failed to open IMAP connection '.$connectstringsource;
|
||||
if (function_exists('imap_last_error')) {
|
||||
$morehtml .= '<br>'.imap_last_error();
|
||||
}
|
||||
dol_syslog("Error ".$morehtml, LOG_WARNING);
|
||||
//var_dump(imap_errors())
|
||||
} else {
|
||||
dol_syslog("Imap connected. Now we call imap_num_msg()");
|
||||
$morehtml .= imap_num_msg($connection);
|
||||
}
|
||||
|
||||
if ($connection) {
|
||||
dol_syslog("Imap close");
|
||||
imap_close($connection);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$morehtml .= '<a class="flat" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=scan&token='.newToken().'">'.img_picto('', 'refresh', 'class="paddingrightonly"').$langs->trans("Refresh").'</a>';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user