2019-02-22 13:56:39 +00:00
< ? php
2022-08-04 13:58:00 +00:00
/* Copyright ( C ) 2022 Laurent Destailleur < eldy @ users . sourceforge . net >
2019-02-22 13:56:39 +00:00
* Copyright ( C ) 2019 Thibault FOUCART < support @ ptibogxiv . net >
2024-09-13 01:09:23 +00:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-11-06 18:39:46 +00:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2019-02-22 13:56:39 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2019-02-22 13:56:39 +00:00
*/
/**
2019-08-22 20:50:41 +00:00
* \file htdocs / core / modules / oauth / stripelive_oauthcallback . php
2019-02-22 13:56:39 +00:00
* \ingroup oauth
* \brief Page to get oauth callback
*/
2022-09-07 18:08:59 +00:00
// Load Dolibarr environment
2019-02-22 13:56:39 +00:00
require '../../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/OAuth/bootstrap.php' ;
2024-11-06 18:39:46 +00:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var Translate $langs
* @ var User $user
*
2024-12-01 22:15:27 +00:00
* @ var string $dolibarr_main_url_root
2024-11-06 18:39:46 +00:00
*/
2019-02-22 13:56:39 +00:00
use OAuth\Common\Storage\DoliStorage ;
use OAuth\Common\Consumer\Credentials ;
// Define $urlwithroot
2020-04-10 08:59:32 +00:00
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
2019-02-22 13:56:39 +00:00
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2020-09-16 17:39:50 +00:00
$action = GETPOST ( 'action' , 'aZ09' );
2019-02-22 13:56:39 +00:00
$backtourl = GETPOST ( 'backtourl' , 'alpha' );
2022-06-27 20:35:58 +00:00
$keyforprovider = GETPOST ( 'keyforprovider' , 'aZ09' );
if ( empty ( $keyforprovider ) && ! empty ( $_SESSION [ " oauthkeyforproviderbeforeoauthjump " ]) && ( GETPOST ( 'code' ) || $action == 'delete' )) {
$keyforprovider = $_SESSION [ " oauthkeyforproviderbeforeoauthjump " ];
}
2019-02-22 13:56:39 +00:00
/**
* Create a new instance of the URI class with the current URI , stripping the query string
*/
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory ();
//$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
//$currentUri->setQuery('');
2019-08-22 20:50:41 +00:00
$currentUri = $uriFactory -> createFromAbsolute ( $urlwithroot . '/core/modules/oauth/stripelive_oauthcallback.php' );
2019-02-22 13:56:39 +00:00
/**
* Load the credential for the service
*/
2023-12-07 19:03:24 +00:00
/** @var \OAuth\ServiceFactory $serviceFactory An OAuth service factory. */
2019-02-22 13:56:39 +00:00
$serviceFactory = new \OAuth\ServiceFactory ();
$httpClient = new \OAuth\Common\Http\Client\CurlClient ();
// TODO Set options for proxy and timeout
// $params=array('CURLXXX'=>value, ...)
//$httpClient->setCurlParameters($params);
$serviceFactory -> setHttpClient ( $httpClient );
// Dolibarr storage
2023-02-07 19:12:53 +00:00
$storage = new DoliStorage ( $db , $conf , $keyforprovider );
2019-02-22 13:56:39 +00:00
// Setup the credentials for the requests
2024-10-01 19:53:45 +00:00
$keyforparamid = 'OAUTH_STRIPELIVE' . ( $keyforprovider ? '-' . $keyforprovider : '' ) . '_ID' ;
$keyforparamsecret = 'OAUTH_STRIPELIVE' . ( $keyforprovider ? '-' . $keyforprovider : '' ) . '_SECRET' ;
2019-02-22 13:56:39 +00:00
$credentials = new Credentials (
2022-06-27 20:35:58 +00:00
getDolGlobalString ( $keyforparamid ),
getDolGlobalString ( $keyforparamsecret ),
2020-10-31 13:32:18 +00:00
$currentUri -> getAbsoluteUri ()
2019-02-22 13:56:39 +00:00
);
2020-04-10 08:59:32 +00:00
$requestedpermissionsarray = array ();
2021-02-23 21:03:23 +00:00
if ( GETPOST ( 'state' )) {
$requestedpermissionsarray = explode ( ',' , GETPOST ( 'state' )); // Example: 'userinfo_email,userinfo_profile,cloud_print'. 'state' parameter is standard to retrieve some parameters back
}
2019-02-22 13:56:39 +00:00
/* if ( $action != 'delete' && empty ( $requestedpermissionsarray ))
{
2021-02-23 21:03:23 +00:00
print 'Error, parameter state is not defined' ;
exit ;
2019-02-22 13:56:39 +00:00
} */
//var_dump($requestedpermissionsarray);exit;
2024-09-13 01:09:23 +00:00
$apiService = null ;
$state = null ;
2019-02-22 13:56:39 +00:00
// Instantiate the Api service using the credentials, http client and storage mechanism for the token
//$apiService = $serviceFactory->createService('StripeTest', $credentials, $storage, $requestedpermissionsarray);
2022-06-28 12:59:15 +00:00
$servicesuffix = ( $keyforprovider ? '-' . $keyforprovider : '' );
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " oauth_token SET service = 'StripeLive " . $db -> escape ( $servicesuffix ) . " ', entity = " . (( int ) $conf -> entity );
2019-02-22 13:56:39 +00:00
$db -> query ( $sql );
// access type needed to have oauth provider refreshing token
//$apiService->setAccessType('offline');
$langs -> load ( " oauth " );
2022-06-27 20:35:58 +00:00
if ( ! getDolGlobalString ( $keyforparamid )) {
accessforbidden ( 'Setup of service is not complete. Customer ID is missing' );
}
if ( ! getDolGlobalString ( $keyforparamsecret )) {
accessforbidden ( 'Setup of service is not complete. Secret key is missing' );
}
2019-02-22 13:56:39 +00:00
/*
* Actions
*/
2024-08-31 17:26:46 +00:00
if ( $action == 'delete' && ( ! empty ( $user -> admin ) || $user -> id == GETPOSTINT ( 'userid' ))) {
$storage -> userid = GETPOSTINT ( 'userid' );
2020-10-31 13:32:18 +00:00
$storage -> clearToken ( 'StripeLive' );
2019-02-22 13:56:39 +00:00
2020-10-31 13:32:18 +00:00
setEventMessages ( $langs -> trans ( 'TokenDeleted' ), null , 'mesgs' );
2019-02-22 13:56:39 +00:00
2023-06-05 13:20:59 +00:00
if ( empty ( $backtourl )) {
$backtourl = DOL_URL_ROOT . '/' ;
}
2020-10-31 13:32:18 +00:00
header ( 'Location: ' . $backtourl );
exit ();
2019-02-22 13:56:39 +00:00
}
2023-02-07 19:12:53 +00:00
if ( GETPOST ( 'code' )) { // We are coming from oauth provider page
2019-02-22 13:56:39 +00:00
// We should have
//$_GET=array('code' => string 'aaaaaaaaaaaaaa' (length=20), 'state' => string 'user,public_repo' (length=16))
2024-10-01 19:53:45 +00:00
dol_syslog ( basename ( __FILE__ ) . " We are coming from the oauth provider page code= " . dol_trunc ( GETPOST ( 'code' ), 5 ));
2020-10-31 13:32:18 +00:00
// This was a callback request from service, get the token
2024-09-13 01:09:23 +00:00
if ( $apiService === null ) {
dol_syslog ( " No API Service " , LOG_ERR );
} else {
2024-09-15 23:49:38 +00:00
'@phan-var-force OAuth\OAuth2\Service\AbstractService|OAuth\OAuth1\Service\AbstractService $apiService' ;
2024-09-13 01:09:23 +00:00
try {
//var_dump($state);
//var_dump($apiService); // OAuth\OAuth2\Service\Stripe
2023-02-07 19:12:53 +00:00
2024-09-13 01:09:23 +00:00
//$token = $apiService->requestAccessToken(GETPOST('code'), $state);
$token = $apiService -> requestAccessToken ( GETPOST ( 'code' ));
// Stripe is a service that does not need state to be stored as second parameter of requestAccessToken
2020-10-31 13:32:18 +00:00
2024-09-13 01:09:23 +00:00
setEventMessages ( $langs -> trans ( 'NewTokenStored' ), null , 'mesgs' ); // Stored into object managed by class DoliStorage so into table oauth_token
2020-10-31 13:32:18 +00:00
2024-09-13 01:09:23 +00:00
$backtourl = $_SESSION [ " backtourlsavedbeforeoauthjump " ];
unset ( $_SESSION [ " backtourlsavedbeforeoauthjump " ]);
2020-10-31 13:32:18 +00:00
2024-09-13 01:09:23 +00:00
header ( 'Location: ' . $backtourl );
exit ();
} catch ( Exception $e ) {
print $e -> getMessage ();
}
2020-10-31 13:32:18 +00:00
}
2023-12-04 11:07:31 +00:00
} else { // If entry on page with no parameter, we arrive here
2020-10-31 13:32:18 +00:00
$_SESSION [ " backtourlsavedbeforeoauthjump " ] = $backtourl ;
2022-06-27 20:35:58 +00:00
$_SESSION [ " oauthkeyforproviderbeforeoauthjump " ] = $keyforprovider ;
$_SESSION [ 'oauthstateanticsrf' ] = $state ;
2020-10-31 13:32:18 +00:00
// This may create record into oauth_state before the header redirect.
// Creation of record with state in this tables depend on the Provider used (see its constructor).
2021-02-23 21:03:23 +00:00
if ( GETPOST ( 'state' )) {
2024-09-13 01:09:23 +00:00
if ( $apiService === null ) {
dol_syslog ( " No API Service " , LOG_ERR );
} else {
2024-10-03 17:40:34 +00:00
'@phan-var-force OAuth\OAuth2\Service\AbstractService|OAuth\OAuth1\Service\AbstractService $apiService' ;
2024-09-13 01:09:23 +00:00
$url = $apiService -> getAuthorizationUri ( array ( 'state' => GETPOST ( 'state' )));
}
2020-10-31 13:32:18 +00:00
} else {
//$url = $apiService->getAuthorizationUri(); // Parameter state will be randomly generated
//https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_AX27ut70tJ1j6eyFCV3ObEXhNOo2jY6V&scope=read_write
2023-10-15 13:32:35 +00:00
$url = 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=' . getDolGlobalString ( $keyforparamid ) . '&scope=read_write' ;
2020-10-31 13:32:18 +00:00
}
2023-06-05 13:20:59 +00:00
if ( empty ( $url )) {
$url = DOL_URL_ROOT . '/' ;
}
2020-10-31 13:32:18 +00:00
// we go on oauth provider authorization page
header ( 'Location: ' . $url );
exit ();
2019-02-22 13:56:39 +00:00
}
/*
* View
*/
// No view at all, just actions
$db -> close ();