-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.php
More file actions
124 lines (100 loc) · 3.9 KB
/
Copy pathsdk.php
File metadata and controls
124 lines (100 loc) · 3.9 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require 'Auth/FirebaseAuth.php';
require 'Util/Util.php';
/**
* Class with an initialize function and query submition function
*
*/
class PrivataAudit {
private $firebaseAuth;
private $util;
function __construct(){
global $firebaseAuth, $util;
$firebaseAuth = new FirebaseAuth;
$util = new Util;
}
/**
* Initialize
*
* Signs in the user using his dbKey and dbSecret
*
* Fails with an error if the dbKey or dbSecret do not match
*/
public function initialize($dbKeyUser ,$dbSecret){
global $firebaseAuth;
$response = $firebaseAuth -> auth($dbKeyUser, $dbSecret);
return $response;
}
/**
* Submit Queries
*
* Connects to the database and submits user queries in json format
*
* Fails with an error message should a request fail.
*/
public function submitQuery($queries){
global $dbKey, $firebaseAuth, $util;
$response = $firebaseAuth -> verifyIdToken();
if ($response['code']== 200 ){
$idToken = $response['idToken'];
} else {
return $response['error'];
}
define ("apiUrl", 'https://api-sandbox.privata.ai',true);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer ".$idToken
);
$url = apiUrl."/databases"."/".$dbKey;
try{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
} catch( Exception $e){
return($e->getMessage());
}
$objQuery = json_decode($queries, true);
try{
$personalData = $util -> getPersonalDataParams($response);
$filteredQueryArray = $util -> filterQuery($objQuery, $personalData); // filter the query submited in order to only send relevant data
} catch (Exception $e){
return($e->getMessage());
}
if (empty($filteredQueryArray))
return 200;
$filteredJson = json_encode($filteredQueryArray);
$url = apiUrl."/databases"."/".$dbKey."/queries";
try{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $filteredJson);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
} catch (Exception $e){
return($e->getMessage());
}
if(http_response_code() == 200){
$obj = json_decode($response, true);
if (isset($obj["error"])) { //Checks in order to detect an unsuccessful submit request
if(isset($obj["message"])){
global $errorMessage;
$errorMessage = $obj["message"];
return $errorMessage;
} else {
global $errorMessage;
$errorMessage = $obj["error"];
return $errorMessage;
}
} else{
return 200; //query has been submited successfully
}
}else {
return(http_response_code());
}
}
}
?>