public function connectJazpar($start, $end){
$login = $this->getConfiguration('login');
$password = $this->getConfiguration('password');
$mypce = null;
$conso = null;
$comparison = null;
$thresholds = null;
log::add(__CLASS__, 'info', $this->getHumanName() . ' Authentication in progress..');
$postdata = array(
'password' => $password,
'email' => $login,
'capp' => 'meg',
'goto' => 'https://sofa-connexion.grdf.fr:443/openam/oauth2/externeGrdf/authorize?response_type=code&scope=openid%20profile%20email%20infotravaux%20%2Fv1%2Faccreditation%20%2Fv1%2Faccreditations%20%2Fdigiconso%2Fv1%20%2Fdigiconso%2Fv1%2Fconsommations%20new_meg%20%2FDemande.read%20%2FDemande.write&client_id=prod_espaceclient&state=0&redirect_uri=https%3A%2F%2Fmonespace.grdf.fr%2F_codexch&nonce=7cV89oGyWnw28DYdI-702Gjy9f&by_pass_okta=1&capp=meg'
);
$curl = curl_init();
$opts = array(
CURLOPT_URL => 'https://login.monespace.grdf.fr/sofit-account-api/api/v1/auth',
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_HEADER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_COOKIEFILE => '',
CURLOPT_POSTFIELDS => http_build_query($postdata),
CURLOPT_HTTPHEADER => [
'Content-type: application/x-www-form-urlencoded; charset=UTF-8',
'Referer: https://login.monespace.grdf.fr/mire/connexion?goto=https:%2F%2Fsofa-connexion.grdf.fr:443%2Fopenam%2Foauth2%2FexterneGrdf%2Fauthorize%3Fresponse_type%3Dcode%26scope%3Dopenid%2520profile%2520email%2520infotravaux%2520%252Fv1%252Faccreditation%2520%252Fv1%252Faccreditations%2520%252Fdigiconso%252Fv1%2520%252Fdigiconso%252Fv1%252Fconsommations%2520new_meg%2520%252FDemande.read%2520%252FDemande.write%26client_id%3Dprod_espaceclient%26state%3D0%26redirect_uri%3Dhttps%253A%252F%252Fmonespace.grdf.fr%252F_codexch%26nonce%3D7cV89oGyWnw28DYdI-702Gjy9f',
'Domain: grdf.fr',
],
);
curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' Authent1: ' . $response);
if ($responseStatus != "200") {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Authentification error');
return null;
} else {
$obj = json_decode($response);
if ($obj->state != "SUCCESS") {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Authentification error, state = ' . $obj->state . ', captcha = ' . $obj->displayCaptcha);
if ($obj->displayCaptcha == true) {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Authentification error, a captcha needs to be entered on the website');
if (config::byKey('captcha-warning','jazpar','',true) == 1) {
message::add($this->getHumanName(), __('Un captcha a été détecté. Connectez-vous à votre espace pour le résoudre.',__FILE__), '', $this->getId());
}
if (config::byKey('captcha-disable','jazpar','',true) == 1) {
$this->setIsEnable(0);
log::add(__CLASS__, 'info', $this->getHumanName() . ' Equipment disabled (as per plugin config)');
}
}
return null;
}
}
curl_setopt($curl, CURLOPT_URL, "https://monespace.grdf.fr/");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Referer: https://sofa-connexion.grdf.fr:443/openam/oauth2/externeGrdf/authorize?response_type=code&scope=openid profile email infotravaux /v1/accreditation /v1/accreditations /digiconso/v1 /digiconso/v1/consommations new_meg /Demande.read /Demande.write&client_id=prod_espaceclient&state=0&redirect_uri=https://monespace.grdf.fr/_codexch&nonce=7cV89oGyWnw28DYdI-702Gjy9f5XdIJ_4dKE_hbsvag&by_pass_okta=1&capp=meg'));
$response = curl_exec($curl);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' Authent2: ' . $response);
if ($responseStatus != "200") {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Authentification error');
return null;
} else {
log::add(__CLASS__, 'info', $this->getHumanName() . ' ...authenticated!');
}
if (empty($this->getConfiguration('pce'))) {
log::add(__CLASS__, 'info', $this->getHumanName() . ' Retrieve PCE...');
curl_setopt($curl, CURLOPT_URL, "https://monespace.grdf.fr/api/e-connexion/users/pce/historique-consultation");
$response = curl_exec($curl);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' PCE: ' . $response);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($responseStatus != "200") {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Unable to retrieve PCE');
log::add(__CLASS__, 'debug', $this->getHumanName() . ' error: ' . $response);
return null;
} else {
$obj = json_decode($response);
foreach ($obj as $pce) {
$mypce = $pce->numPce;
}
log::add(__CLASS__, 'info', $this->getHumanName() . ' ...PCE retrieved: '.$mypce);
}
} else {
log::add(__CLASS__, 'info', $this->getHumanName() . ' PCE provided as input: ' . $this->getConfiguration('pce'));
$mypce = $this->getConfiguration('pce');
}
log::add(__CLASS__, 'info', $this->getHumanName() . ' Get consumption data...');
curl_setopt($curl, CURLOPT_URL, "https://monespace.grdf.fr/api/e-conso/pce/consommation/informatives?dateDebut=".$start."&dateFin=".$end."&pceList%5B%5D=". $mypce);
$response = curl_exec($curl);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' conso: ' . $response);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($responseStatus != "200") {
log::add(__CLASS__, 'error', $this->getHumanName() . ' Unable to retrieve consumption data');
log::add(__CLASS__, 'debug', $this->getHumanName() . ' error: ' . $response);
return null;
} else {
$conso = json_decode($response);
log::add(__CLASS__, 'info', $this->getHumanName() . ' ...consumption data retrieved!');
}
log::add(__CLASS__, 'info', $this->getHumanName() . ' Get comparison data...');
curl_setopt($curl, CURLOPT_URL, "https://monespace.grdf.fr/api/e-conso/pce/".$mypce."/consommation-anuelle-reference");
$response = curl_exec($curl);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' comparison: ' . $response);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($responseStatus != "200") {
log::add(__CLASS__, 'warning', $this->getHumanName() . ' Unable to retrieve comparison data');
log::add(__CLASS__, 'debug', $this->getHumanName() . ' error: ' . $response);
} else {
$comparison = json_decode($response);
log::add(__CLASS__, 'info', $this->getHumanName() . ' ...comparison data retrieved!');
}
$thresholdsType = config::byKey('thresholds-site','jazpar','',true);
if ($thresholdsType == 1) {
log::add(__CLASS__, 'info', $this->getHumanName() . ' Get monthly threshold datas...');
curl_setopt($curl, CURLOPT_URL, "https://monespace.grdf.fr/api/e-conso/pce/".$mypce."/seuils?frequence=Mensuel");
$response = curl_exec($curl);
log::add(__CLASS__, 'debug', $this->getHumanName() . ' thresholds: ' . $response);
$responseStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($responseStatus != "200") {
log::add(__CLASS__, 'warning', $this->getHumanName() . ' Unable to retrieve monthly thresholds data');
log::add(__CLASS__, 'debug', $this->getHumanName() . ' error: ' . $response);
} else {
$thresholds = json_decode($response);
log::add(__CLASS__, 'info', $this->getHumanName() . ' ...monthly thresholds data retrieved!');
}
}
curl_close($curl);
return array($mypce, $conso, $comparison, $thresholds);
}