forked from check-your-data/checkyourdata
/
wshelper.inc.php
105 lines (94 loc) · 3.28 KB
/
wshelper.inc.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
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
<?php
/**
* 2015 CheckYourDataWSHelper
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author Thomas RIBIERE <thomas.ribiere@gmail.com>
* @copyright 2015 CheckYourData
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
class CheckYourDataWSHelper
{
private static $trackers_data=array();
public static function addTrackerData($k, $v)
{
self::$trackers_data[$k] = $v;
}
/**
* Encode data in json and send with APP CYD pubkey
* @param type $data : data array
* @return string : encoded string
*/
public static function encodeData($data)
{
$retour = false;
// Get APP public key
$dir = dirname(__FILE__);
$pubkey = openssl_pkey_get_public('file://'.$dir.'/ssl/publickey.cer');
// encoding
if ($pubkey !== false) {
$ekeys = array();
$crypted = '';
if (openssl_seal(Tools::jsonEncode($data), $crypted, $ekeys, array($pubkey))) {
$retour = array(
'key' => urlencode($ekeys[0]),
'data' => urlencode($crypted),
);
}
openssl_free_key($pubkey);
}
return $retour;
}
/**
* WS call APP
* @param type $data : data array
* @return array : WS return JSON decoded
*/
public static function send($url, $data)
{
// add data for trackers if not empty
if (!empty(self::$trackers_data)) {
$data['data']['trackers_data'] = self::$trackers_data;
}
$url = 'http://'.$url.'ws/';
$enc = self::encodeData($data);
$dat = 'k='.$enc['key'].'&d='.$enc['data'];
if (function_exists('curl_init')) {
// if curl active
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $dat);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($c);
curl_close($c);
} else {
// otherwise : file_get_contents
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $dat
)
);
$context = stream_context_create($opts);
$res = Tools::file_get_contents($url, false, $context);
}
$ret = Tools::jsonDecode($res, true);
if ($ret != null) {
Configuration::updateValue('checkyourdata_last_errors', implode(', ', $ret['errors']));
}
if (!empty($ret['data']['demoEnd'])) {
Configuration::updateValue('checkyourdata_demo_end', $ret['data']['demoEnd']);
}
return $ret;
}
}