-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-api.php
More file actions
101 lines (75 loc) · 2.85 KB
/
Copy pathcustom-api.php
File metadata and controls
101 lines (75 loc) · 2.85 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
<?php
require 'vendor/autoload.php';
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
error_reporting(E_ERROR | E_PARSE);
$host_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http").'://'.$_SERVER['HTTP_HOST'];
$client = new Client(['base_uri' => $host_url ]);
$file = file_get_contents('su_token.key');
$SU_TOKEN = preg_replace( '/\s*/m', '',$file );
$userName = $_GET['userName'];
$password = $_GET['password'];
$siteName = $_GET['siteName'];
$defaultParams = '/?module=API&format=JSON';
$defaultParams = array(
'module' => 'API',
'format' => 'JSON'
);
// ---- Function for Matomo call -----//
function callMatomo($params,$userExistCall = false) {
$response = $GLOBALS['client']->get(dirname($_SERVER['PHP_SELF']),[
'query' => $params
]);
$body = $response->getBody();
$json = json_decode($body);
if($userExistCall && !property_exists($json, "value")){
$res -> errorCode = 77;
header('Content-Type: application/json');
echo json_encode($res);
exit();
}
if(!property_exists($json, "value")){
header('Content-Type: application/json');
echo $body;
exit();
}
return $json;
}
// ---- Function for Matomo call end-----//
// ----------------------- Validating username and password ---------------------//
// ------------ Get User Token --------------//
$params = $defaultParams;
$params["method"]="UsersManager.getTokenAuth";
$params["userLogin"]=$userName;
$params["md5Password"]=$password;
$userToken = callMatomo($params) -> value;
// ------------ Get User Token end --------------//
// Note the Matomo API returns token even the password is incorrect, so we need to validate the token to verify the password.
// ------------ Validate User Token ---------//
$params = $defaultParams;
$params["method"]="UsersManager.userExists";
$params["userLogin"]=$userName;
$params["token_auth"]=$userToken;
callMatomo($params,true);
// ------------ Validate User Token end ---------//
// ----------------------- Validating username and password end ---------------------//
// ------------ Adding New Site ----------------//
$params = $defaultParams;
$params["method"]="SitesManager.addSite";
$params["siteName"]=$siteName;
$params["token_auth"]=$SU_TOKEN;
$siteId = callMatomo($params) -> value;
// ------------ Adding New Site end----------------//
// ------------ Set View Access for the site to the user ---------//
$params = $defaultParams;
$params["method"]="UsersManager.setUserAccess";
$params["userLogin"]=$userName;
$params["access"]="view";
$params["idSites"]=$siteId;
$params["token_auth"]=$SU_TOKEN;
$res = callMatomo($params);
header('Content-Type: application/json');
echo json_encode($res);
// ------------ Set View Access for the site to the user end ---------//