Skip to content

Commit

Permalink
Update printgcp.modules.php
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic34 committed Apr 20, 2016
1 parent 0ad9715 commit e1d55d2
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions htdocs/core/modules/printing/printgcp.modules.php
Expand Up @@ -65,52 +65,52 @@ function __construct($db)
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current

$this->db = $db;
$this->google_id = $conf->global->OAUTH_GOOGLE_ID;
$this->google_secret = $conf->global->OAUTH_GOOGLE_SECRET;
// Token storage
$storage = new DoliStorage($this->db, $this->conf);
//$storage->clearToken('Google');
// Setup the credentials for the requests
$credentials = new Credentials(
$this->google_id,
$this->google_secret,
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
);
$access = ($storage->hasAccessToken('Google')?'HasAccessToken':'NoAccessToken');
$serviceFactory = new \OAuth\ServiceFactory();
$apiService = $serviceFactory->createService('Google', $credentials, $storage, array());
$token_ok=true;
try {
$token = $storage->retrieveAccessToken('Google');
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
$token_ok = false;
}
//var_dump($this->errors);exit;

$expire = false;
// Is token expired or will token expire in the next 30 seconds
if ($token_ok) {
$expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
}

// Token expired so we refresh it
if ($token_ok && $expire) {
try {
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
$refreshtoken = $token->getRefreshToken();
$token = $apiService->refreshAccessToken($token);
$token->setRefreshToken($refreshtoken);
$storage->storeAccessToken('Google', $token);
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
}
if (!$conf->oauth->enabled) {
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'ModuleAuthNotActive', 'type'=>'info');
} else {

$this->db = $db;
$this->google_id = $conf->global->OAUTH_GOOGLE_ID;
$this->google_secret = $conf->global->OAUTH_GOOGLE_SECRET;
// Token storage
$storage = new DoliStorage($this->db, $this->conf);
//$storage->clearToken('Google');
// Setup the credentials for the requests
$credentials = new Credentials(
$this->google_id,
$this->google_secret,
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
);
$access = ($storage->hasAccessToken('Google')?'HasAccessToken':'NoAccessToken');
$serviceFactory = new \OAuth\ServiceFactory();
$apiService = $serviceFactory->createService('Google', $credentials, $storage, array());
$token_ok=true;
try {
$token = $storage->retrieveAccessToken('Google');
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
$token_ok = false;
}
//var_dump($this->errors);exit;

$expire = false;
// Is token expired or will token expire in the next 30 seconds
if ($token_ok) {
$expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
}

// Token expired so we refresh it
if ($token_ok && $expire) {
try {
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
$refreshtoken = $token->getRefreshToken();
$token = $apiService->refreshAccessToken($token);
$token->setRefreshToken($refreshtoken);
$storage->storeAccessToken('Google', $token);
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
}
if ($this->google_id != '' && $this->google_secret != '') {
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthConfigured', 'type'=>'info');
$this->conf[] = array('varname'=>'PRINTGCP_TOKEN_ACCESS', 'info'=>$access, 'type'=>'info', 'renew'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'delete'=>($storage->hasAccessToken('Google')?$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'):''));
Expand Down

0 comments on commit e1d55d2

Please sign in to comment.