-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.php
78 lines (66 loc) · 2.15 KB
/
webhook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
use \Dropbox as dbx;
function verify(){
echo htmlspecialchars($_GET['challenge']);
}
function webhook(){
#'''Receive a list of changed user IDs from Dropbox and process each.'''
//1 recup de l'header et verifie si signature dropbox
$signature = (isset(getallheaders()['X-Dropbox-Signature'])) ? getallheaders()['X-Dropbox-Signature'] : "signature invalide" ;
//comment vérifier la signature ? (non facultatif)
//2 recup du json
$data = file_get_contents("php://input");
$uidList = json_decode($data);
// file_put_contents('dblog.txt',$data."\n".$uidList);
//3 repondre rapidement
echo 'Lancement process_user';
process_user();
//nb : on n'utilise pas les uid donc cette fonction pourrait se résumer en process_user();
}
function delta($myCustomClient ,$cursortxt, $url, $pathPrefix){
$cursor=file_get_contents($cursortxt);
// echo $cursor;
$deltaPage = $myCustomClient->getDelta($cursor,$pathPrefix);
$numAdds = 0;
$numRemoves = 0;
foreach ($deltaPage["entries"] as $entry) {
list($lcPath, $metadata) = $entry;
if ($metadata === null) {
echo "- $lcPath\n";
$numRemoves++;
} else {
echo "+ $lcPath\n";
$numAdds++;
}
$id = explode("/", substr($lcPath, strlen($pathPrefix)+1, 4))[0]; //create array separate by *
echo $id;
}
file_put_contents($cursortxt,$deltaPage["cursor"]);
if($numAdds+$numRemoves>0){
header('Location: lib/ajax/'.$url.'.php?id='.$id);
}
}
function process_user(){
#'''Call /delta for the given user ID and process any changes.'''
// creation d'un client dropbox
include("lib/dropboxAPI.php");
$myCustomClient = new dbx\Client($accessToken, $clientIdentifier);
//Articles
$pathPrefix="/Chargements appareil photo/ArticleTdm";
$cursortxt = "lib/cursor.txt";
$url="url";
delta($myCustomClient ,$cursortxt, $url, $pathPrefix);
//Challenge
$pathPrefix="/Chargements appareil photo/ChallengeTdm";
$cursortxt = "lib/cursorC.txt";
$url="challenge_update";
delta($myCustomClient , $cursortxt, $url, $pathPrefix);
}
if(isset($_GET['challenge'])){
verify();
}elseif (isset(getallheaders()['X-Dropbox-Signature'])) {
webhook();
}else {
process_user();
}
?>