-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostback_paymentwall.php
More file actions
147 lines (121 loc) · 3.25 KB
/
postback_paymentwall.php
File metadata and controls
147 lines (121 loc) · 3.25 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class Postback_paymentwall extends MX_Controller
{
private $currency;
private $uid;
/**
* Load the config options
*/
public function __construct()
{
parent::__construct();
$this->load->config('donate');
$this->pm_config = $this->config->item('donate_paymentwall');
}
/**
* Handle the pingback request
*/
public function index()
{
if ( ! $this->isIpAddressValid($this->input->ip_address())
{
die("WRONG IP");
}
$params = array();
foreach ($_GET as $key => $value)
$params[$key] = $value;
unset($params['sig']);
$log_data = array(
'user_id' => $this->input->get('uid'),
'signature' => $this->input->get('sig'),
'dp_amount' => $this->input->get('currency'),
'type' => $this->input->get('type'),
'ref' => $this->input->get('ref'),
'timestamp' => time(),
);
// verify signature
if ($this->input->get('sig') == $this->calculatePingbackSignature($params))
{
// ok, update DPs (this also handles chargebacks)
$this->currency = $this->input->get('currency');
$this->db->query("UPDATE `account_data` SET `dp` = `dp` + ? WHERE `id` = ?", array($this->currency, $this->input->get('uid')));
$this->updateMonthlyIncome();
// insert log
$log_data['status'] = 'success';
$this->db->insert('paymentwall_logs', $log_data);
$this->plugins->onDonationPostback($this->input->get('uid'), true, -1, $this->currency);
die("OK");
}
else
{
// insert log
$log_data['status'] = 'failed';
$this->db->insert('paymentwall_logs', $log_data);
$this->plugins->onDonationPostback($this->input->get('uid'), false, -1, $this->currency);
die("WRONG SIGNATURE");
}
}
/**
* Calculate the signature value
* @return String
*/
private function calculatePingbackSignature($params)
{
// work with sorted data
ksort($params);
// generate the base string
$baseString = '';
foreach($params as $key => $value) {
$baseString .= $key . '=' . $value;
}
$baseString .= $this->pm_config['secret_key'];
return md5($baseString);
}
/**
* Keep track of the monthly income
*/
private function updateMonthlyIncome()
{
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM monthly_income WHERE month=?", array(date("Y-m")));
$row = $query->result_array();
if($row[0]['total'])
{
$this->db->query("UPDATE monthly_income SET amount = amount + ".$this->currency." WHERE month=?", array(date("Y-m")));
}
else
{
$this->db->query("INSERT INTO monthly_income(month, amount) VALUES(?, ?)", array(date("Y-m"), floor($this->currency)));
}
}
public function isIpAddressValid($ipAddress)
{
$ipsWhitelist = array(
'174.36.92.186',
'174.36.96.66',
'174.36.92.187',
'174.36.92.192',
'174.37.14.28'
);
$rangesWhitelist = array(
'216.127.71.0/24'
);
if (in_array($ipAddress, $ipsWhitelist)) {
return true;
}
foreach ($rangesWhitelist as $range) {
if ($this->isCidrMatched($this->ipAddress, $range)) {
return true;
}
}
return false;
}
public function isCidrMatched($ip, $range)
{
list($subnet, $bits) = explode('/', $range);
$ip = ip2long($ip);
$subnet = ip2long($subnet);
$mask = -1 << (32 - $bits);
$subnet &= $mask;
return ($ip & $mask) == $subnet;
}
}