|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Ingenico; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Title: Ingenico security class |
|
7
|
|
|
* Description: |
|
8
|
|
|
* Copyright: Copyright (c) 2005 - 2016 |
|
9
|
|
|
* Company: Pronamic |
|
10
|
|
|
* |
|
11
|
|
|
* @author Remco Tolsma |
|
12
|
|
|
* @version 2.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Security { |
|
15
|
|
|
/** |
|
16
|
|
|
* The Ogone calculations parameters in |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $calculations_parameters_in; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The Ogone calculations parameters out |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $calculations_parameters_out; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get calculations parameters in |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public static function get_calculations_parameters_in() { |
|
33
|
3 |
|
if ( ! isset( self::$calculations_parameters_in ) ) { |
|
34
|
1 |
|
self::$calculations_parameters_in = array(); |
|
35
|
|
|
|
|
36
|
1 |
|
$file = dirname( __FILE__ ) . '/../data/calculations-parameters-sha-in.txt'; |
|
37
|
1 |
|
if ( is_readable( $file ) ) { |
|
38
|
1 |
|
self::$calculations_parameters_in = file( $file, FILE_IGNORE_NEW_LINES ); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
return self::$calculations_parameters_in; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get calculations parameters in |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public static function get_calculations_parameters_out() { |
|
49
|
2 |
|
if ( ! isset( self::$calculations_parameters_out ) ) { |
|
50
|
1 |
|
self::$calculations_parameters_out = array(); |
|
51
|
|
|
|
|
52
|
1 |
|
$file = dirname( __FILE__ ) . '/../data/calculations-parameters-sha-out.txt'; |
|
53
|
1 |
|
if ( is_readable( $file ) ) { |
|
54
|
1 |
|
self::$calculations_parameters_out = file( $file, FILE_IGNORE_NEW_LINES ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
return self::$calculations_parameters_out; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get request data |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function get_request_data() { |
|
67
|
|
|
$data = array(); |
|
68
|
|
|
|
|
69
|
|
|
if ( isset( $_SERVER['REQUEST_METHOD'] ) ) { // WPCS: input var ok. |
|
70
|
|
|
switch ( $_SERVER['REQUEST_METHOD'] ) { // WPCS: input var ok. |
|
71
|
|
|
case 'GET': |
|
72
|
|
|
// @todo see how we can improve security around this |
|
73
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
74
|
|
|
$data = $_GET; |
|
75
|
|
|
|
|
76
|
|
|
break; |
|
77
|
|
|
case 'POST': |
|
78
|
|
|
// @todo see how we can improve security around this |
|
79
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
80
|
|
|
$data = $_POST; |
|
81
|
|
|
|
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $data; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get calculation fields. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $calculation_fields Calculation fields. |
|
93
|
|
|
* @param array $fields Fields. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public static function get_calculation_fields( $calculation_fields, $fields ) { |
|
98
|
3 |
|
$calculation_fields = array_flip( $calculation_fields ); |
|
99
|
|
|
|
|
100
|
3 |
|
return array_intersect_key( $fields, $calculation_fields ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get signature. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $fields Fields. |
|
107
|
|
|
* @param string $passphrase Pass phrase. |
|
108
|
|
|
* @param string $hash_algorithm Hashing algorithm. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public static function get_signature( $fields, $passphrase, $hash_algorithm ) { |
|
113
|
|
|
// This string is constructed by concatenating the values of the fields sent with the order (sorted |
|
114
|
|
|
// alphabetically, in the format ‘parameter=value’), separated by a passphrase. |
|
115
|
3 |
|
$string = ''; |
|
116
|
|
|
|
|
117
|
|
|
// All parameters need to be put alphabetically. |
|
118
|
3 |
|
ksort( $fields ); |
|
119
|
|
|
|
|
120
|
|
|
// Loop. |
|
121
|
3 |
|
foreach ( $fields as $name => $value ) { |
|
122
|
3 |
|
$value = (string) $value; |
|
123
|
|
|
|
|
124
|
|
|
// Use of empty will fail, value can be string '0'. |
|
125
|
3 |
|
if ( strlen( $value ) > 0 ) { |
|
126
|
2 |
|
$name = strtoupper( $name ); |
|
127
|
|
|
|
|
128
|
3 |
|
$string .= $name . '=' . $value . $passphrase; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Hash. |
|
133
|
3 |
|
$result = hash( $hash_algorithm, $string ); |
|
134
|
|
|
|
|
135
|
|
|
// String to uppercase. |
|
136
|
3 |
|
$result = strtoupper( $result ); |
|
137
|
|
|
|
|
138
|
3 |
|
return $result; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Sign data. |
|
143
|
|
|
* |
|
144
|
|
|
* @param Data $data Data. |
|
145
|
|
|
* @param string $pass_phrase Pass phrase. |
|
146
|
|
|
* @param string $hash_algorithm Hashing algorithm. |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function sign_data( Data $data, $pass_phrase, $hash_algorithm ) { |
|
149
|
|
|
$calculation_fields = self::get_calculations_parameters_in(); |
|
150
|
|
|
|
|
151
|
|
|
$fields = self::get_calculation_fields( $calculation_fields, $data->get_fields() ); |
|
152
|
|
|
|
|
153
|
|
|
$signature = self::get_signature( $fields, $pass_phrase, $hash_algorithm ); |
|
154
|
|
|
|
|
155
|
|
|
$data->set_field( 'SHASign', $signature ); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|