summaryrefslogtreecommitdiffstats
path: root/frontend/api/api_test.php
blob: e714ed5265911b258af532da845c90b16f1af653 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 *
 *	Skripta za testiranje API-ja
 *
**/

// Nastavimo url api-ja
$api_url = 'http://localhost/FDV/frontend/api/api.php';
$api_url = 'https://www.1ka.si/frontend/api/api.php';

// Nastavimo identifier in key userja
$identifier = '';
$private_key = '';


// Nastavimo parametre
//$action = 'login';
$action = 'logout';


// Izvedemo klic (GET ali POST)
$result = executeGET();
//$result = executePOST();




// Izvedemo json decode
$result_array = json_decode($result, true);

// redirectamo ce imamo tako nastavljeno
if(isset($result_array['redirect']) && $result_array['redirect'] != ''){
	header('Location: '.$result_array['redirect']);
}
// Drugace izpisemo rezultat
else{
	echo 'REZULTAT (RAW):<br />';
	echo $result;

	echo '<br /><br /><br />';
	
	// Nastavimo nazaj popravljen cookie
	//$_COOKIE = $result_array['cookie'];
	
	echo 'REZULTAT (JSON DECODE):';
	var_dump($result_array);	
}




// GET
function executeGET(){
	global $api_url;
	global $identifier;
	global $private_key;
	global $ank_id;
	global $action;
		
	// GET params
	$params = 'action='.$action;		// Funkcija, ki jo želimo izvesti
	$params .= '&ank_id='.$ank_id;		// ostali parametri potrebni za klic funkcije (id ankete, vprašanja...)
			
	// Pripravimo podatke za hashiranje
	$request_method = 'GET';
	$request = $api_url.'?'.$params;

	$data = $request_method . $request;
		
	// Izracunamo hash (token)
	$token = hash_hmac('sha256', $data, $private_key);
		
	// Pripravimo klic – dodamo parametra »identifikator« in »token«
	$ch = curl_init($request.'&identifier='.$identifier.'&token='.$token);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
	// Izvedemo klic
	$result = curl_exec($ch);
	
	return $result;
}

// POST
function executePOST(){
	global $api_url;
	global $identifier;
	global $private_key;
	global $ank_id;
	global $action;
	
	// GET params
	$params = 'action='.$action;		// Funkcija, ki jo želimo izvesti
			
	// POST data
	$post_data = array(
		"cookie" => $_COOKIE
	);

	
	// Pripravimo podatke za hashiranje
	$request_method = 'POST';
	$request = $api_url.'?'.$params;
	$raw_post_data = http_build_query($post_data);

	$data = $request_method . $request . $raw_post_data;
		
	// Izracunamo hash (token)
	$token = hash_hmac('sha256', $data, $private_key);
	
	
	// Pripravimo klic – dodamo parametra »identifikator« in »token«
	$ch = curl_init($request.'&identifier='.$identifier.'&token='.$token);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));		// JSON string za POST
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	curl_setopt($ch, CURLOPT_HEADER  ,1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLINFO_HEADER_OUT, true);
	/*$cookie_file = 'cookie.txt';
	curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file); 
	curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);*/
	
	
	// Pripravimo cookije ki jih posljemo cez
	$cookie_string = '';
	foreach($_COOKIE as $key => $value){
		$cookie_string .= $key.'='.$value.';';
	}	
	$cookie_string = substr($cookie_string, 0, -2);
	curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
		
		
	// Izvedemo klic
	$result = curl_exec($ch);

	
	// Popravimo piskotke
	preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches); // get cookie
	$cookies = array();
	foreach($matches[1] as $item) {
		$new_cookie = explode("=", $item);
		setcookie($new_cookie[0], $new_cookie[1], time()-3600, '/', '');
	}	
	
	// Izluscimo samo json response (ker imamo tudi header zraven)
	preg_match_all('{".*"}', $result, $matches);
	$result = '{'.$matches[0][0].'}';
	
	
	return $result;
}