1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
$Id$ |
4
|
|
|
|
5
|
|
|
osCommerce, Open Source E-Commerce Solutions |
6
|
|
|
http://www.oscommerce.com |
7
|
|
|
|
8
|
|
|
Copyright (c) 2017 osCommerce |
9
|
|
|
|
10
|
|
|
Released under the GNU General Public License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/functions/compatibility.php'); |
14
|
|
|
|
15
|
|
|
class OSCOM_PayPal { |
16
|
|
|
var $_code = 'paypal'; |
17
|
|
|
var $_title = 'PayPal App'; |
18
|
|
|
var $_version; |
19
|
|
|
var $_api_version = '204'; |
20
|
|
|
var $_identifier = 'osCommerce_PPapp_v5'; |
21
|
|
|
var $_definitions = array(); |
22
|
|
|
|
23
|
|
|
function log($module, $action, $result, $request, $response, $server, $is_ipn = false) { |
24
|
|
|
global $customer_id; |
25
|
|
|
|
26
|
|
|
$do_log = false; |
27
|
|
|
|
28
|
|
|
if ( in_array(OSCOM_APP_PAYPAL_LOG_TRANSACTIONS, array('1', '0')) ) { |
29
|
|
|
$do_log = true; |
30
|
|
|
|
31
|
|
|
if ( (OSCOM_APP_PAYPAL_LOG_TRANSACTIONS == '0') && ($result === 1) ) { |
32
|
|
|
$do_log = false; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ( $do_log !== true ) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$filter = array('ACCT', 'CVV2', 'ISSUENUMBER'); |
41
|
|
|
|
42
|
|
|
$request_string = ''; |
43
|
|
|
|
44
|
|
|
if ( is_array($request) ) { |
45
|
|
|
foreach ( $request as $key => $value ) { |
46
|
|
View Code Duplication |
if ( (strpos($key, '_nh-dns') !== false) || in_array($key, $filter) ) { |
|
|
|
|
47
|
|
|
$value = '**********'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$request_string .= $key . ': ' . $value . "\n"; |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$request_string = $request; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$response_string = ''; |
57
|
|
|
|
58
|
|
|
if ( is_array($response) ) { |
59
|
|
|
foreach ( $response as $key => $value ) { |
60
|
|
|
if ( is_array($value) ) { |
61
|
|
|
if ( function_exists('http_build_query') ) { |
62
|
|
|
$value = http_build_query($value); |
63
|
|
|
} |
64
|
|
View Code Duplication |
} elseif ( (strpos($key, '_nh-dns') !== false) || in_array($key, $filter) ) { |
|
|
|
|
65
|
|
|
$value = '**********'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$response_string .= $key . ': ' . $value . "\n"; |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$response_string = $response; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$data = array('customers_id' => tep_session_is_registered('customer_id') ? $customer_id : 0, |
75
|
|
|
'module' => $module, |
76
|
|
|
'action' => $action . (($is_ipn === true) ? ' [IPN]' : ''), |
77
|
|
|
'result' => $result, |
78
|
|
|
'server' => ($server == 'live') ? 1 : -1, |
79
|
|
|
'request' => trim($request_string), |
80
|
|
|
'response' => trim($response_string), |
81
|
|
|
'ip_address' => sprintf('%u', ip2long($this->getIpAddress())), |
82
|
|
|
'date_added' => 'now()'); |
83
|
|
|
|
84
|
|
|
tep_db_perform('oscom_app_paypal_log', $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
function migrate() { |
|
|
|
|
88
|
|
|
$migrated = false; |
89
|
|
|
|
90
|
|
|
foreach ( $this->getModules() as $module ) { |
91
|
|
|
if ( !defined('OSCOM_APP_PAYPAL_' . $module . '_STATUS') ) { |
92
|
|
|
$this->saveParameter('OSCOM_APP_PAYPAL_' . $module . '_STATUS', ''); |
93
|
|
|
|
94
|
|
|
$class = 'OSCOM_PayPal_' . $module; |
95
|
|
|
|
96
|
|
|
if ( !class_exists($class) ) { |
97
|
|
|
$this->loadLanguageFile('modules/' . $module . '/' . $module . '.php'); |
98
|
|
|
|
99
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/' . $module . '.php'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$m = new $class(); |
103
|
|
|
|
104
|
|
|
if ( method_exists($m, 'canMigrate') && $m->canMigrate() ) { |
105
|
|
|
$m->migrate($this); |
106
|
|
|
|
107
|
|
|
if ( $migrated === false ) { |
108
|
|
|
$migrated = true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $migrated; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
function getModules() { |
|
|
|
|
118
|
|
|
static $result; |
119
|
|
|
|
120
|
|
|
if ( !isset($result) ) { |
121
|
|
|
$result = array(); |
122
|
|
|
|
123
|
|
|
if ( $dir = @dir(DIR_FS_CATALOG . 'includes/apps/paypal/modules/') ) { |
124
|
|
|
while ( $file = $dir->read() ) { |
125
|
|
|
if ( !in_array($file, array('.', '..')) && is_dir(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $file) && file_exists(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $file . '/' . $file . '.php') ) { |
126
|
|
|
$sort_order = $this->getModuleInfo($file, 'sort_order'); |
127
|
|
|
|
128
|
|
|
if ( is_numeric($sort_order) ) { |
129
|
|
|
$counter = (int)$sort_order; |
130
|
|
|
} else { |
131
|
|
|
$counter = count($result); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
while ( true ) { |
135
|
|
|
if ( isset($result[$counter]) ) { |
136
|
|
|
$counter++; |
137
|
|
|
|
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$result[$counter] = $file; |
142
|
|
|
|
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
ksort($result, SORT_NUMERIC); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
function isInstalled($module) { |
|
|
|
|
156
|
|
|
if ( file_exists(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . basename($module) . '/' . basename($module) . '.php') ) { |
157
|
|
|
return defined('OSCOM_APP_PAYPAL_' . basename($module) . '_STATUS') && tep_not_null(constant('OSCOM_APP_PAYPAL_' . basename($module) . '_STATUS')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
function getModuleInfo($module, $info) { |
|
|
|
|
164
|
|
|
$class = 'OSCOM_PayPal_' . $module; |
165
|
|
|
|
166
|
|
|
if ( !class_exists($class) ) { |
167
|
|
|
$this->loadLanguageFile('modules/' . $module . '/' . $module . '.php'); |
168
|
|
|
|
169
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/' . $module . '.php'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$m = new $class(); |
173
|
|
|
|
174
|
|
|
return $m->{'_' . $info}; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function hasCredentials($module, $type = null) { |
178
|
|
|
if ( !defined('OSCOM_APP_PAYPAL_' . $module . '_STATUS') ) { |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$server = constant('OSCOM_APP_PAYPAL_' . $module . '_STATUS'); |
183
|
|
|
|
184
|
|
|
if ( !in_array($server, array('1', '0')) ) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$server = ($server == '1') ? 'LIVE' : 'SANDBOX'; |
189
|
|
|
|
190
|
|
|
if ( $type == 'email') { |
191
|
|
|
$creds = array('OSCOM_APP_PAYPAL_' . $server . '_SELLER_EMAIL'); |
192
|
|
|
} elseif ( substr($type, 0, 7) == 'payflow' ) { |
193
|
|
|
if ( strlen($type) > 7 ) { |
194
|
|
|
$creds = array('OSCOM_APP_PAYPAL_PF_' . $server . '_' . strtoupper(substr($type, 8))); |
195
|
|
|
} else { |
196
|
|
|
$creds = array('OSCOM_APP_PAYPAL_PF_' . $server . '_VENDOR', |
197
|
|
|
'OSCOM_APP_PAYPAL_PF_' . $server . '_PASSWORD', |
198
|
|
|
'OSCOM_APP_PAYPAL_PF_' . $server . '_PARTNER'); |
199
|
|
|
} |
200
|
|
View Code Duplication |
} else { |
|
|
|
|
201
|
|
|
$creds = array('OSCOM_APP_PAYPAL_' . $server . '_API_USERNAME', |
202
|
|
|
'OSCOM_APP_PAYPAL_' . $server . '_API_PASSWORD', |
203
|
|
|
'OSCOM_APP_PAYPAL_' . $server . '_API_SIGNATURE'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
foreach ( $creds as $c ) { |
|
|
|
|
207
|
|
|
if ( !defined($c) || (strlen(trim(constant($c))) < 1) ) { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
function getCredentials($module, $type) { |
216
|
|
|
if ( constant('OSCOM_APP_PAYPAL_' . $module . '_STATUS') == '1' ) { |
217
|
|
View Code Duplication |
if ( $type == 'email') { |
|
|
|
|
218
|
|
|
return constant('OSCOM_APP_PAYPAL_LIVE_SELLER_EMAIL'); |
219
|
|
|
} elseif ( $type == 'email_primary') { |
220
|
|
|
return constant('OSCOM_APP_PAYPAL_LIVE_SELLER_EMAIL_PRIMARY'); |
221
|
|
|
} elseif ( substr($type, 0, 7) == 'payflow' ) { |
222
|
|
|
return constant('OSCOM_APP_PAYPAL_PF_LIVE_' . strtoupper(substr($type, 8))); |
223
|
|
|
} else { |
224
|
|
|
return constant('OSCOM_APP_PAYPAL_LIVE_API_' . strtoupper($type)); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
if ( $type == 'email') { |
|
|
|
|
229
|
|
|
return constant('OSCOM_APP_PAYPAL_SANDBOX_SELLER_EMAIL'); |
230
|
|
|
} elseif ( $type == 'email_primary') { |
231
|
|
|
return constant('OSCOM_APP_PAYPAL_SANDBOX_SELLER_EMAIL_PRIMARY'); |
232
|
|
|
} elseif ( substr($type, 0, 7) == 'payflow' ) { |
233
|
|
|
return constant('OSCOM_APP_PAYPAL_PF_SANDBOX_' . strtoupper(substr($type, 8))); |
234
|
|
|
} else { |
235
|
|
|
return constant('OSCOM_APP_PAYPAL_SANDBOX_API_' . strtoupper($type)); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
function hasApiCredentials($server, $type = null) { |
240
|
|
|
$server = ($server == 'live') ? 'LIVE' : 'SANDBOX'; |
241
|
|
|
|
242
|
|
|
if ( $type == 'email') { |
243
|
|
|
$creds = array('OSCOM_APP_PAYPAL_' . $server . '_SELLER_EMAIL'); |
244
|
|
|
} elseif ( substr($type, 0, 7) == 'payflow' ) { |
245
|
|
|
$creds = array('OSCOM_APP_PAYPAL_PF_' . $server . '_' . strtoupper(substr($type, 8))); |
246
|
|
View Code Duplication |
} else { |
|
|
|
|
247
|
|
|
$creds = array('OSCOM_APP_PAYPAL_' . $server . '_API_USERNAME', |
248
|
|
|
'OSCOM_APP_PAYPAL_' . $server . '_API_PASSWORD', |
249
|
|
|
'OSCOM_APP_PAYPAL_' . $server . '_API_SIGNATURE'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
View Code Duplication |
foreach ( $creds as $c ) { |
|
|
|
|
253
|
|
|
if ( !defined($c) || (strlen(trim(constant($c))) < 1) ) { |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
function getApiCredentials($server, $type) { |
|
|
|
|
262
|
|
|
if ( ($server == 'live') && defined('OSCOM_APP_PAYPAL_LIVE_API_' . strtoupper($type)) ) { |
263
|
|
|
return constant('OSCOM_APP_PAYPAL_LIVE_API_' . strtoupper($type)); |
264
|
|
|
} elseif ( defined('OSCOM_APP_PAYPAL_SANDBOX_API_' . strtoupper($type)) ) { |
265
|
|
|
return constant('OSCOM_APP_PAYPAL_SANDBOX_API_' . strtoupper($type)); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
View Code Duplication |
function getParameters($module) { |
|
|
|
|
270
|
|
|
$result = array(); |
271
|
|
|
|
272
|
|
|
if ( $module == 'G' ) { |
273
|
|
|
if ( $dir = @dir(DIR_FS_CATALOG . 'includes/apps/paypal/cfg_params/') ) { |
274
|
|
|
while ( $file = $dir->read() ) { |
275
|
|
|
if ( !is_dir(DIR_FS_CATALOG . 'includes/apps/paypal/cfg_params/' . $file) && (substr($file, strrpos($file, '.')) == '.php') ) { |
276
|
|
|
$result[] = 'OSCOM_APP_PAYPAL_' . strtoupper(substr($file, 0, strrpos($file, '.'))); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} else { |
281
|
|
|
if ( $dir = @dir(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/cfg_params/') ) { |
282
|
|
|
while ( $file = $dir->read() ) { |
283
|
|
|
if ( !is_dir(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/cfg_params/' . $file) && (substr($file, strrpos($file, '.')) == '.php') ) { |
284
|
|
|
$result[] = 'OSCOM_APP_PAYPAL_' . $module . '_' . strtoupper(substr($file, 0, strrpos($file, '.'))); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $result; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
View Code Duplication |
function getInputParameters($module) { |
|
|
|
|
294
|
|
|
$result = array(); |
295
|
|
|
|
296
|
|
|
if ( $module == 'G' ) { |
297
|
|
|
$cut = 'OSCOM_APP_PAYPAL_'; |
298
|
|
|
} else { |
299
|
|
|
$cut = 'OSCOM_APP_PAYPAL_' . $module . '_'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$cut_length = strlen($cut); |
303
|
|
|
|
304
|
|
|
foreach ( $this->getParameters($module) as $key ) { |
305
|
|
|
$p = strtolower(substr($key, $cut_length)); |
306
|
|
|
|
307
|
|
|
if ( $module == 'G' ) { |
308
|
|
|
$cfg_class = 'OSCOM_PayPal_Cfg_' . $p; |
309
|
|
|
|
310
|
|
|
if ( !class_exists($cfg_class) ) { |
311
|
|
|
$this->loadLanguageFile('cfg_params/' . $p . '.php'); |
312
|
|
|
|
313
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/cfg_params/' . $p . '.php'); |
314
|
|
|
} |
315
|
|
|
} else { |
316
|
|
|
$cfg_class = 'OSCOM_PayPal_' . $module . '_Cfg_' . $p; |
317
|
|
|
|
318
|
|
|
if ( !class_exists($cfg_class) ) { |
319
|
|
|
$this->loadLanguageFile('modules/' . $module . '/cfg_params/' . $p . '.php'); |
320
|
|
|
|
321
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/cfg_params/' . $p . '.php'); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$cfg = new $cfg_class(); |
326
|
|
|
|
327
|
|
|
if ( !defined($key) ) { |
328
|
|
|
$this->saveParameter($key, $cfg->default, isset($cfg->title) ? $cfg->title : null, isset($cfg->description) ? $cfg->description : null, isset($cfg->set_func) ? $cfg->set_func : null); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if ( !isset($cfg->app_configured) || ($cfg->app_configured !== false) ) { |
332
|
|
|
if ( isset($cfg->sort_order) && is_numeric($cfg->sort_order) ) { |
333
|
|
|
$counter = (int)$cfg->sort_order; |
334
|
|
|
} else { |
335
|
|
|
$counter = count($result); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
while ( true ) { |
339
|
|
|
if ( isset($result[$counter]) ) { |
340
|
|
|
$counter++; |
341
|
|
|
|
342
|
|
|
continue; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$set_field = $cfg->getSetField(); |
346
|
|
|
|
347
|
|
|
if ( !empty($set_field) ) { |
348
|
|
|
$result[$counter] = $set_field; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
break; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
ksort($result, SORT_NUMERIC); |
357
|
|
|
|
358
|
|
|
return $result; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
// APP calls require $server to be "live" or "sandbox" |
362
|
|
|
function getApiResult($module, $call, $extra_params = null, $server = null, $is_ipn = false) { |
363
|
|
|
if ( $module == 'APP' ) { |
364
|
|
|
$function = 'OSCOM_PayPal_Api_' . $call; |
365
|
|
|
|
366
|
|
|
if ( !function_exists($function) ) { |
367
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/api/' . $call . '.php'); |
368
|
|
|
} |
369
|
|
|
} else { |
370
|
|
|
if ( !isset($server) ) { |
371
|
|
|
$server = (constant('OSCOM_APP_PAYPAL_' . $module . '_STATUS') == '1') ? 'live' : 'sandbox'; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
$function = 'OSCOM_PayPal_' . $module . '_Api_' . $call; |
375
|
|
|
|
376
|
|
|
if ( !function_exists($function) ) { |
377
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/api/' . $call . '.php'); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
$result = $function($this, $server, $extra_params); |
382
|
|
|
|
383
|
|
|
$this->log($module, $call, ($result['success'] === true) ? 1 : -1, $result['req'], $result['res'], $server, $is_ipn); |
384
|
|
|
|
385
|
|
|
return $result['res']; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
function makeApiCall($url, $parameters = null, $headers = null, $opts = null) { |
389
|
|
|
$server = parse_url($url); |
390
|
|
|
|
391
|
|
|
if ( !isset($server['port']) ) { |
392
|
|
|
$server['port'] = ($server['scheme'] == 'https') ? 443 : 80; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
if ( !isset($server['path']) ) { |
396
|
|
|
$server['path'] = '/'; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '')); |
400
|
|
|
curl_setopt($curl, CURLOPT_PORT, $server['port']); |
401
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
402
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
403
|
|
|
curl_setopt($curl, CURLOPT_FORBID_REUSE, true); |
404
|
|
|
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true); |
405
|
|
|
curl_setopt($curl, CURLOPT_ENCODING, ''); // disable gzip |
406
|
|
|
|
407
|
|
View Code Duplication |
if ( isset($parameters) ) { |
|
|
|
|
408
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
409
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
View Code Duplication |
if ( isset($headers) && is_array($headers) && !empty($headers) ) { |
|
|
|
|
413
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
View Code Duplication |
if ( isset($server['user']) && isset($server['pass']) ) { |
|
|
|
|
417
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, $server['user'] . ':' . $server['pass']); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
if ( defined('OSCOM_APP_PAYPAL_VERIFY_SSL') && (OSCOM_APP_PAYPAL_VERIFY_SSL == '1') ) { |
421
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); |
422
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); |
423
|
|
|
|
424
|
|
|
if ( (substr($server['host'], -10) == 'paypal.com') && file_exists(DIR_FS_CATALOG . 'ext/modules/payment/paypal/paypal.com.crt') ) { |
425
|
|
|
curl_setopt($curl, CURLOPT_CAINFO, DIR_FS_CATALOG . 'ext/modules/payment/paypal/paypal.com.crt'); |
426
|
|
|
} elseif ( file_exists(DIR_FS_CATALOG . 'includes/cacert.pem') ) { |
427
|
|
|
curl_setopt($curl, CURLOPT_CAINFO, DIR_FS_CATALOG . 'includes/cacert.pem'); |
428
|
|
|
} |
429
|
|
|
} else { |
430
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
431
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
View Code Duplication |
if (substr($server['host'], -10) == 'paypal.com') { |
|
|
|
|
435
|
|
|
$ssl_version = 0; |
436
|
|
|
|
437
|
|
|
if ( defined('OSCOM_APP_PAYPAL_SSL_VERSION') && (OSCOM_APP_PAYPAL_SSL_VERSION == '1') ) { |
438
|
|
|
$ssl_version = 6; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
if (isset($opts['sslVersion']) && is_int($opts['sslVersion'])) { |
442
|
|
|
$ssl_version = $opts['sslVersion']; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
if ($ssl_version !== 0) { |
446
|
|
|
curl_setopt($curl, CURLOPT_SSLVERSION, $ssl_version); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
View Code Duplication |
if ( defined('OSCOM_APP_PAYPAL_PROXY') && tep_not_null(OSCOM_APP_PAYPAL_PROXY) ) { |
|
|
|
|
451
|
|
|
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, true); |
452
|
|
|
curl_setopt($curl, CURLOPT_PROXY, OSCOM_APP_PAYPAL_PROXY); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$result = curl_exec($curl); |
456
|
|
|
|
457
|
|
View Code Duplication |
if (isset($opts['returnFull']) && ($opts['returnFull'] === true)) { |
|
|
|
|
458
|
|
|
$result = array( |
459
|
|
|
'response' => $result, |
460
|
|
|
'error' => curl_error($curl), |
461
|
|
|
'info' => curl_getinfo($curl) |
462
|
|
|
); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
curl_close($curl); |
466
|
|
|
|
467
|
|
|
return $result; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
View Code Duplication |
function drawButton($title = null, $link = null, $type = null, $params = null, $force_css = false) { |
|
|
|
|
471
|
|
|
$colours = array('success' => '#1cb841', |
472
|
|
|
'error' => '#ca3c3c', |
473
|
|
|
'warning' => '#ebaa16', |
474
|
|
|
'info' => '#42B8DD', |
475
|
|
|
'primary' => '#0078E7'); |
476
|
|
|
|
477
|
|
|
if ( !isset($type) || !in_array($type, array_keys($colours)) ) { |
478
|
|
|
$type = 'info'; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$css = 'font-size:14px;color:#fff;padding:8px 16px;border:0;border-radius:4px;text-shadow:0 1px 1px rgba(0, 0, 0, 0.2);text-decoration:none;display:inline-block;cursor:pointer;white-space:nowrap;vertical-align:baseline;text-align:center;background-color:' . $colours[$type] . ';'; |
482
|
|
|
|
483
|
|
|
$button = ''; |
484
|
|
|
|
485
|
|
|
if ( isset($link) ) { |
486
|
|
|
$button .= '<a href="' . $link . '" class="pp-button'; |
487
|
|
|
|
488
|
|
|
if ( isset($type) ) { |
489
|
|
|
$button .= ' pp-button-' . $type; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$button .= '"'; |
493
|
|
|
|
494
|
|
|
if ( isset($params) ) { |
495
|
|
|
$button .= ' ' . $params; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
if ( $force_css == true ) { |
|
|
|
|
499
|
|
|
$button .= ' style="' . $css . '"'; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$button .= '>' . $title . '</a>'; |
503
|
|
|
} else { |
504
|
|
|
$button .= '<button type="submit" class="pp-button'; |
505
|
|
|
|
506
|
|
|
if ( isset($type) ) { |
507
|
|
|
$button .= ' pp-button-' . $type; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
$button .= '"'; |
511
|
|
|
|
512
|
|
|
if ( isset($params) ) { |
513
|
|
|
$button .= ' ' . $params; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
if ( $force_css == true ) { |
|
|
|
|
517
|
|
|
$button .= ' style="' . $css . '"'; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
$button .= '>' . $title . '</button>'; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
return $button; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
View Code Duplication |
function createRandomValue($length, $type = 'mixed') { |
|
|
|
|
527
|
|
|
if ( ($type != 'mixed') && ($type != 'chars') && ($type != 'digits')) $type = 'mixed'; |
528
|
|
|
|
529
|
|
|
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
530
|
|
|
$digits = '0123456789'; |
531
|
|
|
|
532
|
|
|
$base = ''; |
533
|
|
|
|
534
|
|
|
if ( ($type == 'mixed') || ($type == 'chars') ) { |
535
|
|
|
$base .= $chars; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
if ( ($type == 'mixed') || ($type == 'digits') ) { |
539
|
|
|
$base .= $digits; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
$value = ''; |
543
|
|
|
|
544
|
|
|
if ( !class_exists('PasswordHash') && file_exists(DIR_FS_CATALOG . 'includes/classes/passwordhash.php') ) { |
545
|
|
|
include(DIR_FS_CATALOG . 'includes/classes/passwordhash.php'); |
546
|
|
|
|
547
|
|
|
$hasher = new PasswordHash(10, true); |
548
|
|
|
|
549
|
|
|
do { |
550
|
|
|
$random = base64_encode($hasher->get_random_bytes($length)); |
551
|
|
|
|
552
|
|
|
for ($i = 0, $n = strlen($random); $i < $n; $i++) { |
553
|
|
|
$char = substr($random, $i, 1); |
554
|
|
|
|
555
|
|
|
if ( strpos($base, $char) !== false ) { |
556
|
|
|
$value .= $char; |
557
|
|
|
} |
558
|
|
|
} |
559
|
|
|
} while ( strlen($value) < $length ); |
560
|
|
|
|
561
|
|
|
if ( strlen($value) > $length ) { |
562
|
|
|
$value = substr($value, 0, $length); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $value; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
// fallback for v2.3.1 |
569
|
|
|
while ( strlen($value) < $length ) { |
570
|
|
|
if ($type == 'digits') { |
571
|
|
|
$char = tep_rand(0,9); |
572
|
|
|
} else { |
573
|
|
|
$char = chr(tep_rand(0,255)); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
if ( $type == 'mixed' ) { |
577
|
|
|
if (preg_match('/^[a-z0-9]$/i', $char)) $value .= $char; |
578
|
|
|
} elseif ($type == 'chars') { |
579
|
|
|
if (preg_match('/^[a-z]$/i', $char)) $value .= $char; |
580
|
|
|
} elseif ($type == 'digits') { |
581
|
|
|
if (preg_match('/^[0-9]$/i', $char)) $value .= $char; |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
return $value; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
View Code Duplication |
function saveParameter($key, $value, $title = null, $description = null, $set_func = null) { |
|
|
|
|
589
|
|
|
if ( !defined($key) ) { |
590
|
|
|
if ( !isset($title) ) { |
591
|
|
|
$title = 'PayPal App Parameter'; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
if ( !isset($description) ) { |
595
|
|
|
$description = 'A parameter for the PayPal Application.'; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('" . tep_db_input($title) . "', '" . tep_db_input($key) . "', '" . tep_db_input($value) . "', '" . tep_db_input($description) . "', '6', '0', now())"); |
599
|
|
|
|
600
|
|
|
if ( isset($set_func) ) { |
601
|
|
|
tep_db_query("update " . TABLE_CONFIGURATION . " set set_function = '" . tep_db_input($set_func) . "' where configuration_key = '" . tep_db_input($key) . "'"); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
define($key, $value); |
605
|
|
|
} else { |
606
|
|
|
tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . tep_db_input($value) . "' where configuration_key = '" . tep_db_input($key) . "'"); |
607
|
|
|
} |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
function deleteParameter($key) { |
611
|
|
|
tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key = '" . tep_db_input($key) . "'"); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
View Code Duplication |
function formatCurrencyRaw($total, $currency_code = null, $currency_value = null) { |
|
|
|
|
615
|
|
|
global $currencies, $currency; |
616
|
|
|
|
617
|
|
|
if ( !isset($currency_code) ) { |
618
|
|
|
$currency_code = tep_session_is_registered('currency') ? $currency : DEFAULT_CURRENCY; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
if ( !isset($currency_value) || !is_numeric($currency_value) ) { |
622
|
|
|
$currency_value = $currencies->currencies[$currency_code]['value']; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
return number_format(tep_round($total * $currency_value, $currencies->currencies[$currency_code]['decimal_places']), $currencies->currencies[$currency_code]['decimal_places'], '.', ''); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
function getCode() { |
629
|
|
|
return $this->_code; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
function getTitle() { |
633
|
|
|
return $this->_title; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
View Code Duplication |
function getVersion() { |
|
|
|
|
637
|
|
|
if ( !isset($this->_version) ) { |
638
|
|
|
$version = trim(file_get_contents(DIR_FS_CATALOG . 'includes/apps/paypal/version.txt')); |
639
|
|
|
|
640
|
|
|
if ( is_numeric($version) ) { |
641
|
|
|
$this->_version = $version; |
642
|
|
|
} else { |
643
|
|
|
trigger_error('OSCOM APP [PAYPAL]: Could not read App version number.'); |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
return $this->_version; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
function getApiVersion() { |
651
|
|
|
return $this->_api_version; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
function getIdentifier() { |
655
|
|
|
return $this->_identifier; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
function hasAlert() { |
659
|
|
|
return tep_session_is_registered('OSCOM_PayPal_Alerts'); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
View Code Duplication |
function addAlert($message, $type) { |
|
|
|
|
663
|
|
|
global $OSCOM_PayPal_Alerts; |
664
|
|
|
|
665
|
|
|
if ( in_array($type, array('error', 'warning', 'success')) ) { |
666
|
|
|
if ( !tep_session_is_registered('OSCOM_PayPal_Alerts') ) { |
667
|
|
|
$OSCOM_PayPal_Alerts = array(); |
668
|
|
|
tep_session_register('OSCOM_PayPal_Alerts'); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
$OSCOM_PayPal_Alerts[$type][] = $message; |
672
|
|
|
} |
673
|
|
|
} |
674
|
|
|
|
675
|
|
View Code Duplication |
function getAlerts() { |
|
|
|
|
676
|
|
|
global $OSCOM_PayPal_Alerts; |
677
|
|
|
|
678
|
|
|
$output = ''; |
679
|
|
|
|
680
|
|
|
if ( tep_session_is_registered('OSCOM_PayPal_Alerts') && !empty($OSCOM_PayPal_Alerts) ) { |
681
|
|
|
$result = array(); |
682
|
|
|
|
683
|
|
|
foreach ( $OSCOM_PayPal_Alerts as $type => $messages ) { |
684
|
|
|
if ( in_array($type, array('error', 'warning', 'success')) ) { |
685
|
|
|
$m = '<ul class="pp-alerts-' . $type . '">'; |
686
|
|
|
|
687
|
|
|
foreach ( $messages as $message ) { |
688
|
|
|
$m .= '<li>' . tep_output_string_protected($message) . '</li>'; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
$m .= '</ul>'; |
692
|
|
|
|
693
|
|
|
$result[] = $m; |
694
|
|
|
} |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
if ( !empty($result) ) { |
698
|
|
|
$output .= '<div class="pp-alerts">' . implode("\n", $result) . '</div>'; |
699
|
|
|
} |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
tep_session_unregister('OSCOM_PayPal_Alerts'); |
703
|
|
|
|
704
|
|
|
return $output; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
View Code Duplication |
function install($module) { |
|
|
|
|
708
|
|
|
$cut_length = strlen('OSCOM_APP_PAYPAL_' . $module . '_'); |
709
|
|
|
|
710
|
|
|
foreach ( $this->getParameters($module) as $key ) { |
711
|
|
|
$p = strtolower(substr($key, $cut_length)); |
712
|
|
|
|
713
|
|
|
$cfg_class = 'OSCOM_PayPal_' . $module . '_Cfg_' . $p; |
714
|
|
|
|
715
|
|
|
if ( !class_exists($cfg_class) ) { |
716
|
|
|
$this->loadLanguageFile('modules/' . $module . '/cfg_params/' . $p . '.php'); |
717
|
|
|
|
718
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/cfg_params/' . $p . '.php'); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
$cfg = new $cfg_class(); |
722
|
|
|
|
723
|
|
|
$this->saveParameter($key, $cfg->default, isset($cfg->title) ? $cfg->title : null, isset($cfg->description) ? $cfg->description : null, isset($cfg->set_func) ? $cfg->set_func : null); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
$m_class = 'OSCOM_PayPal_' . $module; |
727
|
|
|
|
728
|
|
|
if ( !class_exists($m_class) ) { |
729
|
|
|
$this->loadLanguageFile('modules/' . $module . '/' . $module . '.php'); |
730
|
|
|
|
731
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/' . $module . '.php'); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
$m = new $m_class(); |
735
|
|
|
|
736
|
|
|
if ( method_exists($m, 'install') ) { |
737
|
|
|
$m->install($this); |
738
|
|
|
} |
739
|
|
|
} |
740
|
|
|
|
741
|
|
View Code Duplication |
function uninstall($module) { |
|
|
|
|
742
|
|
|
tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key like 'OSCOM_APP_PAYPAL_" . tep_db_input($module) . "_%'"); |
743
|
|
|
|
744
|
|
|
$m_class = 'OSCOM_PayPal_' . $module; |
745
|
|
|
|
746
|
|
|
if ( !class_exists($m_class) ) { |
747
|
|
|
$this->loadLanguageFile('modules/' . $module . '/' . $module . '.php'); |
748
|
|
|
|
749
|
|
|
include(DIR_FS_CATALOG . 'includes/apps/paypal/modules/' . $module . '/' . $module . '.php'); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
$m = new $m_class(); |
753
|
|
|
|
754
|
|
|
if ( method_exists($m, 'uninstall') ) { |
755
|
|
|
$m->uninstall($this); |
756
|
|
|
} |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
function logUpdate($message, $version) { |
760
|
|
View Code Duplication |
if ( is_writable(DIR_FS_CATALOG . 'includes/apps/paypal/work') ) { |
|
|
|
|
761
|
|
|
file_put_contents(DIR_FS_CATALOG . 'includes/apps/paypal/work/update_log-' . $version . '.php', '[' . date('d-M-Y H:i:s') . '] ' . $message . "\n", FILE_APPEND); |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
|
765
|
|
View Code Duplication |
public function loadLanguageFile($filename, $lang = null) { |
|
|
|
|
766
|
|
|
global $language; |
767
|
|
|
|
768
|
|
|
$lang = isset($lang) ? basename($lang) : basename($language); |
769
|
|
|
|
770
|
|
|
if ( $lang != 'english' ) { |
771
|
|
|
$this->loadLanguageFile($filename, 'english'); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
$pathname = DIR_FS_CATALOG . 'includes/apps/paypal/languages/' . $lang . '/' . $filename; |
775
|
|
|
|
776
|
|
|
if ( file_exists($pathname) ) { |
777
|
|
|
$contents = file($pathname); |
778
|
|
|
|
779
|
|
|
$ini_array = array(); |
780
|
|
|
|
781
|
|
|
foreach ( $contents as $line ) { |
782
|
|
|
$line = trim($line); |
783
|
|
|
|
784
|
|
|
if ( !empty($line) && (substr($line, 0, 1) != '#') ) { |
785
|
|
|
$delimiter = strpos($line, '='); |
786
|
|
|
|
787
|
|
|
if ( ($delimiter !== false) && (preg_match('/^[A-Za-z0-9_-]/', substr($line, 0, $delimiter)) === 1) && (substr_count(substr($line, 0, $delimiter), ' ') == 1) ) { |
788
|
|
|
$key = trim(substr($line, 0, $delimiter)); |
789
|
|
|
$value = trim(substr($line, $delimiter + 1)); |
790
|
|
|
|
791
|
|
|
$ini_array[$key] = $value; |
792
|
|
|
} elseif ( isset($key) ) { |
793
|
|
|
$ini_array[$key] .= "\n" . $line; |
794
|
|
|
} |
795
|
|
|
} |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
unset($contents); |
799
|
|
|
|
800
|
|
|
$this->_definitions = array_merge($this->_definitions, $ini_array); |
801
|
|
|
|
802
|
|
|
unset($ini_array); |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
806
|
|
View Code Duplication |
function getDef($key, $values = null) { |
|
|
|
|
807
|
|
|
$def = isset($this->_definitions[$key]) ? $this->_definitions[$key] : $key; |
808
|
|
|
|
809
|
|
|
if ( is_array($values) ) { |
810
|
|
|
$keys = array_keys($values); |
811
|
|
|
|
812
|
|
|
foreach ( $keys as &$k ) { |
813
|
|
|
$k = ':' . $k; |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
$def = str_replace($keys, array_values($values), $def); |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
return $def; |
820
|
|
|
} |
821
|
|
|
|
822
|
|
View Code Duplication |
function getDirectoryContents($base, &$result = array()) { |
|
|
|
|
823
|
|
|
foreach ( scandir($base) as $file ) { |
824
|
|
|
if ( ($file == '.') || ($file == '..') ) { |
825
|
|
|
continue; |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
$pathname = $base . '/' . $file; |
829
|
|
|
|
830
|
|
|
if ( is_dir($pathname) ) { |
831
|
|
|
$this->getDirectoryContents($pathname, $result); |
832
|
|
|
} else { |
833
|
|
|
$result[] = str_replace('\\', '/', $pathname); // Unix style directory separator "/" |
834
|
|
|
} |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
return $result; |
838
|
|
|
} |
839
|
|
|
|
840
|
|
View Code Duplication |
function isWritable($location) { |
|
|
|
|
841
|
|
|
if ( !file_exists($location) ) { |
842
|
|
|
while ( true ) { |
843
|
|
|
$location = dirname($location); |
844
|
|
|
|
845
|
|
|
if ( file_exists($location) ) { |
846
|
|
|
break; |
847
|
|
|
} |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
return is_writable($location); |
852
|
|
|
} |
853
|
|
|
|
854
|
|
View Code Duplication |
function rmdir($dir) { |
|
|
|
|
855
|
|
|
foreach ( scandir($dir) as $file ) { |
856
|
|
|
if ( !in_array($file, array('.', '..')) ) { |
857
|
|
|
if ( is_dir($dir . '/' . $file) ) { |
858
|
|
|
$this->rmdir($dir . '/' . $file); |
859
|
|
|
} else { |
860
|
|
|
unlink($dir . '/' . $file); |
861
|
|
|
} |
862
|
|
|
} |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
return rmdir($dir); |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
function displayPath($pathname) { |
869
|
|
|
if ( DIRECTORY_SEPARATOR == '/' ) { |
870
|
|
|
return $pathname; |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, $pathname); |
874
|
|
|
} |
875
|
|
|
// OSCOM v2.2rc2a compatibility |
876
|
|
View Code Duplication |
function getIpAddress() { |
|
|
|
|
877
|
|
|
if ( function_exists('tep_get_ip_address') ) { |
878
|
|
|
return tep_get_ip_address(); |
879
|
|
|
} |
880
|
|
|
global $HTTP_SERVER_VARS; |
881
|
|
|
$ip_address = null; |
882
|
|
|
$ip_addresses = array(); |
883
|
|
|
if (isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']) && !empty($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) { |
884
|
|
|
foreach ( array_reverse(explode(',', $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) as $x_ip ) { |
885
|
|
|
$x_ip = trim($x_ip); |
886
|
|
|
if ($this->isValidIpAddress($x_ip)) { |
887
|
|
|
$ip_addresses[] = $x_ip; |
888
|
|
|
} |
889
|
|
|
} |
890
|
|
|
} |
891
|
|
|
if (isset($HTTP_SERVER_VARS['HTTP_CLIENT_IP']) && !empty($HTTP_SERVER_VARS['HTTP_CLIENT_IP'])) { |
892
|
|
|
$ip_addresses[] = $HTTP_SERVER_VARS['HTTP_CLIENT_IP']; |
893
|
|
|
} |
894
|
|
|
if (isset($HTTP_SERVER_VARS['HTTP_X_CLUSTER_CLIENT_IP']) && !empty($HTTP_SERVER_VARS['HTTP_X_CLUSTER_CLIENT_IP'])) { |
895
|
|
|
$ip_addresses[] = $HTTP_SERVER_VARS['HTTP_X_CLUSTER_CLIENT_IP']; |
896
|
|
|
} |
897
|
|
|
if (isset($HTTP_SERVER_VARS['HTTP_PROXY_USER']) && !empty($HTTP_SERVER_VARS['HTTP_PROXY_USER'])) { |
898
|
|
|
$ip_addresses[] = $HTTP_SERVER_VARS['HTTP_PROXY_USER']; |
899
|
|
|
} |
900
|
|
|
$ip_addresses[] = $HTTP_SERVER_VARS['REMOTE_ADDR']; |
901
|
|
|
foreach ( $ip_addresses as $ip ) { |
902
|
|
|
if (!empty($ip) && $this->isValidIpAddress($ip)) { |
903
|
|
|
$ip_address = $ip; |
904
|
|
|
break; |
905
|
|
|
} |
906
|
|
|
} |
907
|
|
|
return $ip_address; |
908
|
|
|
} |
909
|
|
|
// OSCOM v2.2rc2a compatibility |
910
|
|
View Code Duplication |
function isValidIpAddress($ip_address) { |
|
|
|
|
911
|
|
|
if ( function_exists('tep_validate_ip_address') ) { |
912
|
|
|
return tep_validate_ip_address($ip_address); |
913
|
|
|
} |
914
|
|
|
if (function_exists('filter_var') && defined('FILTER_VALIDATE_IP')) { |
915
|
|
|
return filter_var($ip_address, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)); |
916
|
|
|
} |
917
|
|
|
if (preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip_address)) { |
918
|
|
|
$parts = explode('.', $ip_address); |
919
|
|
|
foreach ($parts as $ip_parts) { |
920
|
|
|
if ( (intval($ip_parts) > 255) || (intval($ip_parts) < 0) ) { |
921
|
|
|
return false; // number is not within 0-255 |
922
|
|
|
} |
923
|
|
|
} |
924
|
|
|
return true; |
925
|
|
|
} |
926
|
|
|
return false; |
927
|
|
|
} |
928
|
|
|
} |
929
|
|
|
?> |
|
|
|
|
930
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.