https://www.diffchecker.com/Iej8vv2m/
Si tu utilises le "set" je serai intéressé de savoir si ca fonctionne toujours.
- Code : Tout sélectionner
<?php
/*
indice 1.1 du 19 decembre 2019 par Merguez07
1.0 : Creation du plugin
1.1 : integration des chatieres de type door connect
modification de la gestion du cache
1.2 : Correction d'un bug dans les led hub et changement d'icone
remerciements :
Bart de Eedomus pour le code de la gestion du cache
RAR69 pour les icones
eedomus.yves.delort pour les beta-tests
Kebiel pour l'integration du door connect(indice 1.1)
2.0 : Mise à jour vers la nouvelle API :
- nouveau endpoint + header par défaut + suppression de sdk_logout
- réorganisation du xml de sortie pour coller à l'API et limiter les appels :
suppresion de sdk_getpetlocation et sdk_getCurfewStatus
*/
$mode = getArg('mode',false); // mode=list pour la configuration du plugin
$device_id = (string) sdk_genuuid();
$endpoint = "https://app-api.production.surehub.io";
$url_logout = "/api/auth/logout";
$url_login = "/api/auth/login";
$url_household = "/api/household";
$url_device = "/api/device";
$url_pet = "/api/pet";
$header_default = array(
"Host: app.api.surehub.io",
"Connection: keep-alive",
"Accept: application/json, text/plain, */*",
"Accept-Encoding: gzip, deflate",
"Accept-Language: en-US,en-GB;q=0.9",
"Content-Type: application/json",
"Origin: https://surepetcare.io",
"Referer: https://surepetcare.io",
"X-Requested-With: com.sureflap.surepetcare",
"X-Device-Id: ".$device_id
);
//---------------------------config du plugin------------------------------------
if ($mode == 'list') {
if (isset($_POST['email'])&& isset($_POST['motdepasse'])){
$email_address = $_POST['email'];
$password = $_POST['motdepasse'];
saveVariable("email",$email_address);
saveVariable("password",$password );
sdk_login();sdk_gethousehold(); sdk_getdevices();sdk_getpet();
sdk_inithtml();
echo utf8_encode($head.$html_hub.$html_flap.$html_pet.$html_script.$end);
die();
} else {
sdk_inithtml();
echo utf8_encode($head.$html_login.$end);
}
die;
}
//-------------------------------------------------------------------------------
$email_address = loadVariable("email");
$password = loadVariable("password");
$set = getArg('set',false); // ledbright/leddim/ledoff
// lockin/lockout/lockboth/locknone
// pos1/pos2
// curfew
//---------------------------lumiere du Hub-------------------------------------
$test=sdk_Targ($set,'led');
if ($test[0]) {
$hub_id= getArg('hubid',true);
sdk_login();
sdk_gethousehold();
sdk_getdevices();
sdk_sethubledbrightness($test[1],$hub_id) ;
echo $xml;
die;
}
//-------------------------------------------------------------------------------
//--------------------verrouillage de la chatiere--------------------------------
$test=sdk_Targ($set,'lock');
if ($test[0]) {
$flap_id= getArg('flapid',true);
sdk_login();
sdk_gethousehold();
sdk_getdevices();
sdk_setlockmode ($test[1],$flap_id) ;
echo $xml;
die;
}
//-------------------------------------------------------------------------------
//---------------------------position de l'animal--------------------------------
$test=sdk_Targ($set,'pos');
if ($test[0]) {
$pet_id= getArg('petid',true);
sdk_login();
sdk_setpetlocation($test[1],$pet_id) ;
echo $xml;
die;
}
//-------------------------------------------------------------------------------
//---------------------------reglage du couvre-feu-------------------------------
if ($set=="curfew") {
$locktime = getArg('locktime',false);
$unlocktime = getArg('unlocktime',false);
$id = getArg('id',true);
sdk_login();
sdk_setEnableCurfew($locktime,$unlocktime,$id) ;
echo $xml;
die;
}
//-------------------------------------------------------------------------------
$cache_duration = 1; // minutes
$time_last_xml = loadVariable('time_last_xml');
if ((time() - $time_last_xml) / 60 < $cache_duration)
{
sdk_header('text/xml');
$cached_xml = loadVariable('cached_xml');
echo $cached_xml;
die();
}
//$xml = '<root>' ."\n";
sdk_login();
sdk_gethousehold();
sdk_getdevices();
sdk_getpet();
//$xml .= '</root>' ."\n";
$cached_xml = '<root>';
$cached_xml .= '<cached>0</cached>';
$cached_xml .= $xml;
$cached_xml .= '</root>';
echo $cached_xml;
if ($xml != '') // non vide
{
$cached_xml = str_replace('<cached>0</cached>', '<cached>1</cached>', $cached_xml);
saveVariable('cached_xml', $cached_xml);
saveVariable('time_last_xml', time());
}
die;
function sdk_genuuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
function sdk_headerTokened(){
global $token,$header_default;
return array_merge($header_default, array("Authorization: Bearer $token"));
}
#---------------------------------------logout---------------------------------------------
function sdk_logout(){
global $oldtoken,$endpoint,$url_logout;
$url = $endpoint.$url_logout;
$action = 'GET';
$header = array("Content-Type: application/json",
"Authorization: Bearer $oldtoken");
$result = sdk_curl($url,$action,null,$header);
}
#------------------------------------------------------------------------------------------
#---------------------------------------login----------------------------------------------
function sdk_login(){
global $oldtoken,$email_address,$password,$device_id,
$endpoint,$url_login,$token,$xml,$header_default;
if ($oldtoken = loadVariable("token")) {
//sdk_logout();
}
$url = $endpoint.$url_login;
$action = 'POST';
$post = '{"email_address":"'.$email_address.'",
"password":"' .$password. '",
"device_id":"' .$device_id. '"}';
$result =sdk_curl($url,$action,$post,$header_default);
if (array_key_exists(token,$result['data'])) {
$token = $result['data']['token'];
saveVariable("token",$token);
$xml .= '<status>' ."success!" .'</status>'."\n";
} else {
$xml .= '<status>' ."Token Invalid/Expired!" .'</status>'."\n";
//$xml .= '</root>' ."\n";
die("Login Failed!\n");
}
}
#------------------------------------------------------------------------------------------
#-------------------------------------gethouseold------------------------------------------
function sdk_gethousehold() {
global $endpoint,$url_household,$xml,$Thouses;
$xml .= '<Houses>'."\n";
$Thouses = array();
$url = $endpoint.$url_household;
$action = 'GET';
$result = sdk_curl($url,$action,null,sdk_headerTokened());
$houses = $result['data'];
if($houses) {
foreach($houses as $house){
$Thouses += array($house['id']=>$house['name']);
$xml .= ' <ID'.$house['id'].'>' .$house['name'] .'</ID'.$house['id'].'>'."\n";
}
$xml .= '</Houses>'."\n";
} else {
$xml .= ' <House>' ."No House!" .'</House>'."\n";
$xml .= '</Houses>'."\n";
die("No House!");
}
}
#------------------------------------------------------------------------------------------
#---------------------------------------getpet---------------------------------------------
function sdk_getpet() {
global $endpoint,$url_pet,$xml,$Thouses,$Tpets;
$xml .= '<Pets>'."\n";
$Tpets=array();
$url = $endpoint.$url_pet;
$action = 'GET';
$result = sdk_curl($url,$action,null,sdk_headerTokened());
$pets = $result['data'];
if($pets) {
foreach ($pets as $pet) {
$chien = ($pet['gender'] === 0 )? "Chienne":"Chien";
$chat = ($pet['gender'] === 0 )? "Chatte":"Chat";
$species= ($pet['species_id'] === 2 )? $chien:$chat;
$Tpets += array($pet['id']=>$pet['name']);
$xml .= ' <ID'.$pet['id'].'>' ."\n";
$xml .= ' <House>' .$Thouses[$pet['household_id']] .'</House>'."\n";
$xml .= ' <Species>' .$species .'</Species>'."\n";
$xml .= ' <Name>' .$pet['name'] .'</Name>'."\n";
if (array_key_exists(position,$pet)) {
if($pet['position']['where'] == "1") {
$xml .= ' <Location>' ."Inside" .'</Location>'."\n";
} else {
$xml .= ' <Location>' ."Outside".'</Location>'."\n";
}
$xml .= ' <LocationSince>'.$pet['position']['since'].'</LocationSince>'."\n";
} else {
$xml .= ' <Location>' ."No location!" .'</Location>'."\n";
}
$xml .= ' </ID'.$pet['id'].'>' . "\n";
}
} else {
$xml .= ' <Pet>' ."No Pet!" .'</Pet>'."\n";
}
$xml .= '</Pets>'."\n";
}
#------------------------------------------------------------------------------------------
#-------------------------------------getdevices-------------------------------------------
function sdk_getdevices() {
global $endpoint,$url_device,$xml,$Tflaps,$Thubs, $Thouses, $Trepeaters,$Tfeeder_connects,$Tprogrammers ;
$xml .= '<Devices>'."\n";
$Tflaps = array();$Thubs = array();$Trepeaters= array() ;
$Tfeeder_connects= array();$Tprogrammers= array();
$url = $endpoint.$url_device;
$action = 'GET';
$result = sdk_curl($url,$action,null,sdk_headerTokened());
$devices= $result['data'];
if ($devices) {
foreach ($devices as $device) {
switch($device['product_id']) {
case 1:
$Thubs += array($device['id']=>$device['name']);
$DeviceType = "Hub";
break;
case 2:
$Trepeaters += array($device['id']=>$device['name']);
$DeviceType = "Repeater";
break;
case 3:
$Tflaps += array($device['id']=>$device['name']);
$DeviceType = "Pet Door Connect";
break;
case 4:
$Tfeeder_connects += array($device['id']=>$device['name']);
$DeviceType = "Pet Feeder Connect";
break;
case 5:
$Tprogrammers += array($device['id']=>$device['name']);
$DeviceType = "Programmer";
break;
case 6:
$Tflaps += array($device['id']=>$device['name']);
$DeviceType = "DualScan Cat Flap Connect";
break;
}
if (array_key_exists(serial_number,$device)) $DeviceSN = $device['serial_number']; else $DeviceSN = "";
$xml .= ' <ID'.$device['id'].">\n";
$xml .= ' <House>' .$Thouses[$device['household_id']] .'</House>'."\n";
$xml .= ' <Type>' .$DeviceType .'</Type>'."\n";
$xml .= ' <Name>' .$device['name'] .'</Name>'."\n";
$xml .= ' <Mac>' .$device['mac_address'] .'</Mac>' ."\n";
$xml .= ' <SN>' .$DeviceSN .'</SN>' ."\n";
if (array_key_exists(status,$device)) {
if (array_key_exists(battery,$device['status'])) {
$xml .= ' <Battery>' .$device['status']['battery'] .'</Battery>' ."\n";
}
if (array_key_exists(locking,$device['status'])) {
if (array_key_exists(mode,$device['status']['locking'])) {
$Tlock=array(2=>"In", 1=>"Out", 3=>"Both",0=>"None");
$xml .= ' <Lock>' .$Tlock[$device['status']['locking']['mode']] .'</Lock>' ."\n";
}
}
if (array_key_exists(led_mode,$device['status'])) {
$Tled=array(1=>"Bright", 4=>"Dim", 0=>"Off");
$xml .= ' <Led>' .$Tled[$device['status']['led_mode']] .'</Led>' ."\n";
}
}
if (array_key_exists(control,$device)) {
if (array_key_exists(curfew,$device['control'])) {
if (array_key_exists(0,$device['control']['curfew'])) {
for ($i = 0; $i < count($device['control']['curfew']); $i++) {
$etat= ($device['control']['curfew'][$i]['enabled'])? "Enabled":"Disabled";
$xml .= ' <curfew'.($i+1).'>' .$etat .'</curfew'.($i+1).'>' ."\n";
if ($etat=="Enabled") {
$Curfew_LockTime = $device['control']['curfew'][$i]['lock_time'];
$Curfew_UnlockTime = $device['control']['curfew'][$i]['unlock_time'];
$xml .= ' <LockTime>' .$Curfew_LockTime .'</LockTime>' ."\n";
$xml .= ' <UnLockTime>' .$Curfew_UnlockTime .'</UnLockTime>' ."\n";
}
}
} else {
$etat= ($device['control']['curfew']['enabled'])? "Enabled":"Disabled";
$xml .= ' <curfew'.($i+1).'>' .$etat .'</curfew'.($i+1).'>' ."\n";
if ($etat=="Enabled") {
$Curfew_LockTime = $device['control']['curfew']['lock_time'];
$Curfew_UnlockTime = $device['control']['curfew']['unlock_time'];
$xml .= ' <LockTime>' .$Curfew_LockTime .'</LockTime>' ."\n";
$xml .= ' <UnLockTime>' .$Curfew_UnlockTime .'</UnLockTime>' ."\n";
}
}
}
}
$xml .= ' </ID'.$device['id'].'>' ."\n";
}
} else {
$xml .= ' <Device>' ."No Device!" .'</Device>'."\n";
}
$xml .= '</Devices>'."\n";
}
#------------------------------------------------------------------------------------------
#-----------------------------sethubledbrightness bright|dim|off-----------------------------
function sdk_sethubledbrightness($led,$hub_id) {
global $endpoint,$Thubs,$xml ;
$xml = '<root>' ."\n";
switch($led) {
case "bright":
$ledN = 1;
break;
case "dim":
$ledN = 4;
break;
case "off":
$ledN = 0;
break;
default:
$ledN = null;
}
if (isset($ledN)) {
$post = "{\"led_mode\":$ledN}";
$url = $endpoint."/api/device/$hub_id/control";
$action ='PUT';
$result = sdk_curl($url,$action,$post,sdk_headerTokened());
if($result['data']['led_mode']==$ledN) {
$hubname=$Thubs[$hub_id];
$xml .= sdk_framed('<action>' ."Successfully Set \"$hubname\" LED Brightness! \"$led\"" .'</action>');
} else {
$xml .= sdk_framed('<action>' ."LED Brightness Change Failed!" .'</action>');
}
} else {
$xml .= sdk_framed('<action>' ."wrong parameter \"$led\"==> reminder: &ledbright ou &leddim ou &ledoff" .'</action>');
}
$xml .= '</root>' ."\n";
}
#---------------------------------------------------------------------------------------------
#------------------------------setlockmode(in|out|both|none,$flap_id)-------------------------
function sdk_setlockmode ($lock,$flap_id) {
global $endpoint,$Tflaps,$xml ;
$xml = '<root>' ."\n";
switch($lock) {
case "in":
$lockN = 2;
break;
case "out":
$lockN = 1;
break;
case "both":
$lockN = 3;
break;
case "none":
$lockN = 0;
break;
default:
$lockN = -1;
}
if (!($lockN==-1)) {
$post = "{\"locking\":\"$lockN\"}";
$url = $endpoint."/api/device/$flap_id/control";
$action = 'PUT';
$result =sdk_curl($url,$action,$post,sdk_headerTokened());
if($result['data']['locking']==$lockN) {
$flap_name = $Tflaps[$flap_id];
$xml .= sdk_framed('<action>' ."Successfully Set \"$flap_name\" Lock Mode! \"$lock\"" .'</action>');
} else {
$xml .= sdk_framed('<action>' ."Lock Mode Change Failed!" .'</action>');
}
} else {
$xml .= sdk_framed('<action>' ."wrong parameter \"$lock\"==> reminder: &set=lockin|&set=lockout|&set=lockboth|&set=locknone" .'</action>');
}
$xml .= '</root>' ."\n";
}
#---------------------------------------------------------------------------------------------
#-------------------------setpetlocation(1(inside)|2(outside),$pet_id)------------------------
function sdk_setpetlocation($position,$pet_id){
global $endpoint,$xml ;
$xml = '<root>' ."\n";
$date= date("Y-m-d H:i");
$post = "{\"where\":\"$position\",\"since\":\"$date\"}";
$url = $endpoint."/api/pet/$pet_id/position";
$action = 'POST';
$result =sdk_curl($url,$action,$post,sdk_headerTokened());
if(!$result['error']){
if($result['data']['where']==$position) {
$Tposition=($position==1)? "inside":"outside";
$xml .= sdk_framed('<action>' ."Successfully Set \"$pet_id\" location \"$Tposition\"!" .'</action>');
} else {
$xml .= sdk_framed('<action>' ."Set Location Failed" .'</action>');
}
} else {
$xml .= sdk_framed('<action>' ."pet not found" .'</action>');
}
$xml .= '</root>' ."\n";
}
#---------------------------------------------------------------------------------------------
#-----------------------setEnableCurfew("18:00", "06:00",$device_id)--------------------------------------------
function sdk_setEnableCurfew($locktime, $unlocktime,$device_id) {
global $endpoint,$xml ;
$xml = '<root>' ."\n";
$set = (($locktime)&&($unlocktime));
$set1= ($set)?"true":"false";
$post = "{\"curfew\":[{\"enabled\":$set1,\"lock_time\":\"$locktime\",\"unlock_time\":\"$unlocktime\"},[],[]]}" ;
$url = $endpoint."/api/device/$device_id/control";
$action = 'PUT';
$result =sdk_curl($url,$action,$post,sdk_headerTokened(),$set);
if($result['data']['curfew'][0]['enabled']==true) {
$xml .= sdk_framed('<action>' ."Successfully Enabled Curfew For \"$DeviceName\" Between $locktime & $unlocktime" .'</action>');
} else {
$xml .= sdk_framed('<action>' ."Successfully disabled Curfew!" .'</action>');
}
$xml .= '</root>' ."\n";
}
#---------------------------------------------------------------------------------------------
function sdk_Targ($arg,$text){
#$arg='pos1' et $text='pos' --> array(true,1)
$Targ[0] = (substr($arg,0,strlen($text))==$text);
$Targ[1] = substr($arg,-(strlen($arg)-strlen($text)));
return $Targ;
}
function sdk_Utf8_ansi($text) {
#remet en forme $text pour voir les accents
$text = str_replace('\u','u',$text);
$text = preg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $text);
$text = sdk_json_decode($text);
return $text;
}
function sdk_since($time){
#donne le temps ecoule depuis $time
$duration = strtotime(date("Y-m-d H:i:s", time()))-strtotime($time);
switch (true) {
case $duration < 60:
return $duration." s";
case $duration < 3600:
return floor($duration/60)." min";
case $duration < 86400:
return floor($duration/3600)." h";
case $duration < 172800:
return "1 jour";
case $duration < 2592000:
return floor($duration/86400)." jours";
case $duration < 5184000:
return "1 mois";
case $duration < 31536000:
return floor($duration/2592000)." mois";
case $duration < 63072000:
return "1 an";
case $duration > 63071999:
return floor($duration/31536000)." annees";
default:
}
}
function sdk_only_letters($text){
#ne garde que les lettres du debut de $text
$text = substr($text,0,sdk_getPosInteger($text));
$text = preg_replace('`[0-9. ]`sm', '', $text);
return $text;
}
function sdk_getPosInteger($text) {
#donne la position du premier chiffre dans $text
$i = 0;
$nLen = strlen($text);
while ($i<$nLen) {
if ( is_numeric( $text[$i] ) ) break;
$i++;
}
return $i;
}
function sdk_curl($url,$action,$post,$header){
$response = httpQuery( $url = $url,
$action = $action,
$post = $post,
$oauth_token = null,
$header = $header,
$use_cookies = false,
$ignore_errors = false,
$info = null);
$result = sdk_Utf8_ansi($response);
return $result;
}
function sdk_framed($text){
$text="#".$text."#";
$nLen = strlen($text);
$text="\n".str_repeat("#", $nLen)."\n".$text."\n".str_repeat("#", $nLen)."\n"."\n";
return $text;
}
function sdk_inithtml() {
global $head,$html_login,$html_hub,$html_flap,$html_pet,$html_script,$end,$Thubs,$Tflaps,$Tpets;
$head = "<html>".
"<head>".
"<meta charset='UTF-8'>".
"<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>".
"<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>".
"<style>".
"body {font-size: 14px;font-family: 'Roboto', sans-serif;width:50%;}".
"body {font-size: 14px;font-family: 'Roboto', sans-serif;width:50%;}".
"input[type='text'] {font-family: 'Roboto', sans-serif;}".
"ul {list-style: symbols;}".
"</style>".
"</head>".
"<body>".
"<img src='https://www.surepetcare.io/assets/images/onboarding/Sure_Petcare_Logo.png' alt='sure petcare' style='width:50%;' /><br />";
$html_login = "<p> Veuillez taper vos identifiants Sure Petcare</p>".
"<form action='/script/?exec=sureflap.php&mode=list' method='post'>".
"<p>";
$email =(loadVariable('email'))?loadVariable('email'):"";
$password=(loadVariable('password'))?loadVariable('password'):"";
$html_login .= "<input type='text' size='60' placeholder='votre email' name='email' value='$email' /><br /><br />".
"<input type='password' size='20' placeholder='votre mot de passe' name='motdepasse' value='$password' /><br /><br />".
"<input type='submit' value='Valider' />".
"</p>".
"</form>";
$html_hub = "<div class='wrapper'>".
"<p>identifiant de votre Hub : ".
"<input id='hub_id' type='text' size='20' name='device_list' class='inputChoice' value='' onclick='this.select();' >".
"<ul class='elements'>";
foreach ($Thubs as $hubid=>$hubname){
$html_hub.= "<li><input type='radio' name='hub_list' class='inputRadio' value='$hubid' id=''>".$hubid."-".$hubname."<br /></li>";
}
$html_hub.= "</ul>".
"</p>".
"</div>";
$html_flap= "<div class='wrapper'>".
"<p>identifiant de votre chatiere : ".
"<input id='flap_id' type='text' size='20' name='device_list' class='inputChoice' value='' onclick='this.select();' >".
"<ul class='elements'>";
foreach ($Tflaps as $flapid=>$flapname){
$html_flap .="<li><input type='radio' name='flap_list' class='inputRadio' value='$flapid' id=''>".$flapid."-".$flapname."<br /></li>";
}
$html_flap.= "</ul>".
"</p>".
"</div>";
$html_pet = "<div class='wrapper'>".
"<p>identifiant de votre animal : ".
"<input id='pet_id' type='text' size='20' name='device_list' class='inputChoice' value='' onclick='this.select();' >".
"<ul class='elements'>";
foreach ($Tpets as $petid=>$petname){
$html_pet .="<li><input type='radio' name='pet_list' class='inputRadio' value='$petid' id=''>".$petid."-".$petname."<br /></li>";
}
$html_pet.= "</ul>".
"</p>".
"</div>";
$html_script= "<script type='text/javascript'>".
"$('html').on('click','.inputRadio', function(e) {".
"$(this).closest('.wrapper').find('.inputChoice').val($(this).val());".
"});".
"</script>";
$end = "</body>".
"</html>";
return;
}