libCurlPost()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 2
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * global_config.inc.php
4
 *
5
 * PHP Toolkit for PayPal v0.51
6
 * http://www.paypal.com/pdn
7
 *
8
 * Copyright (c) 2004 PayPal Inc
9
 *
10
 * Released under Common Public License 1.0
11
 * http://opensource.org/licenses/cpl.php
12
 *
13
 */
14
15
//create variable names to perform additional order processing
16
17
/**
18
 * @return array
19
 */
20
function create_local_variables()
21
{
22
    $array_name = array();
23
    $variables  = array(
24
        'business',
25
        'receiver_email',
26
        'receiver_id',
27
        'item_name',
28
        'item_number',
29
        'quantity',
30
        'invoice',
31
        'custom',
32
        'memo',
33
        'tax',
34
        'option_selection1',
35
        'option_name1',
36
        'option_selection2',
37
        'option_name2',
38
        'num_cart_items',
39
        'mc_gross',
40
        'mc_fee',
41
        'mc_currency',
42
        'settle_amount',
43
        'settle_currency',
44
        'exchange_rate',
45
        'payment_gross',
46
        'payment_fee',
47
        'payment_status',
48
        'pending_reason',
49
        'reason_code',
50
        'payment_date',
51
        'txn_id',
52
        'txn_type',
53
        'payment_type',
54
        'for_auction',
55
        'auction_buyer_id',
56
        'auction_closing_date',
57
        'auction_multi_item',
58
        'first_name',
59
        'last_name',
60
        'payer_business_name',
61
        'address_name',
62
        'address_street',
63
        'address_city',
64
        'address_state',
65
        'address_zip',
66
        'address_country',
67
        'address_status',
68
        'payer_email',
69
        'payer_id',
70
        'payer_status',
71
        'notify_version',
72
        'verify_sign'
73
    );
74
75 View Code Duplication
    foreach ($variables as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
76
        if (isset($_POST[$v])) {
77
            $array_name[$v] = "$_POST[$v]";
78
        } else {
79
            $array_name[$v] = '';
80
        }
81
    }
82
83
    //print_r($array_name);
84
    return $array_name;
85
}
86
87
//post transaction data using curl
88
89
/**
90
 * @param $url
91
 * @param $data
92
 * @return array|string|\XoopsFormElementTray|\XoopsFormText
0 ignored issues
show
Bug introduced by
The type XoopsFormElementTray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type XoopsFormText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
 */
94
function curlPost($url, $data)
95
{
96
    global $paypal, $info;
97
98
    //build post string
99
100
    foreach ($data as $i => $v) {
101
        $postdata .= $i . '=' . urlencode($v) . '&';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $postdata seems to be never defined.
Loading history...
102
    }
103
104
    $postdata .= 'cmd=_notify-validate';
105
106
    //execute curl on the command line
107
108
    exec("$paypal[curl_location] -d \"$postdata\" $url", $info);
109
110
    $info = implode(',', $info);
111
112
    return $info;
113
}
114
115
//posts transaction data using libCurl
116
117
/**
118
 * @param $url
119
 * @param $data
120
 * @return string
121
 */
122
function libCurlPost($url, $data)
123
{
124
    //build post string
125
126
    foreach ($data as $i => $v) {
127
        $postdata .= $i . '=' . urlencode($v) . '&';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $postdata seems to be never defined.
Loading history...
128
    }
129
130
    $postdata .= 'cmd=_notify-validate';
131
132
    $ch = curl_init();
133
134
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
135
    curl_setopt($ch, CURLOPT_URL, $url);
136
    curl_setopt($ch, CURLOPT_POST, 1);
137
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
138
139
    //Start ob to prevent curl_exec from displaying stuff.
140
    ob_start();
141
    curl_exec($ch);
142
143
    //Get contents of output buffer
144
    $info = ob_get_contents();
145
    curl_close($ch);
146
147
    //End ob and erase contents.
148
    ob_end_clean();
149
150
    return $info;
151
}
152
153
//posts transaction data using fsockopen.
154
/**
155
 * @param $url
156
 * @param $data
157
 * @return array|string
158
 */
159
function fsockPost($url, $data)
160
{
161
    //Parse url
162
    $web = parse_url($url);
163
164
    $postdata = '';
165
    //build post string
166
    foreach ($data as $i => $v) {
167
        $postdata .= $i . '=' . urlencode($v) . '&';
168
    }
169
170
    $postdata .= 'cmd=_notify-validate';
171
172
    //Set the port number
173
    if ($web['scheme'] === 'https') {
174
        $web['port'] = '443';
175
        $ssl         = 'ssl://';
176
    } else {
177
        $web['port'] = '80';
178
    }
179
180
    //Create paypal connection
181
    $fp = @fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30);
0 ignored issues
show
Bug introduced by
$web['port'] of type string is incompatible with the type integer expected by parameter $port of fsockopen(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
    $fp = @fsockopen($ssl . $web['host'], /** @scrutinizer ignore-type */ $web['port'], $errnum, $errstr, 30);
Loading history...
Comprehensibility Best Practice introduced by
The variable $ssl does not seem to be defined for all execution paths leading up to this point.
Loading history...
182
183
    //Error checking
184
    if (!$fp) {
185
        echo "$errnum: $errstr";
186
    } //Post Data
187
    else {
188
        fwrite($fp, "POST $web[path] HTTP/1.1\r\n");
189
        fwrite($fp, "Host: $web[host]\r\n");
190
        fwrite($fp, "Content-type: application/x-www-form-urlencoded\r\n");
191
        fwrite($fp, 'Content-length: ' . strlen($postdata) . "\r\n");
192
        fwrite($fp, "Connection: close\r\n\r\n");
193
        fwrite($fp, $postdata . "\r\n\r\n");
194
195
        //loop through the response from the server
196
        while (!feof($fp)) {
197
            $info[] = @fgets($fp, 1024);
198
        }
199
200
        //close fp - we are done with it
201
        fclose($fp);
202
203
        //break up results into a string
204
        $info = implode(',', $info);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $info does not seem to be defined for all execution paths leading up to this point.
Loading history...
205
    }
206
207
    return $info;
208
}
209
210
//Display Paypal Hidden Variables
211
212
function showVariables()
213
{
214
    global $paypal; ?>
215
216
    <!-- PayPal Configuration -->
217
    <input type="hidden" name="business" value="<?= $paypal['business'] ?>">
218
    <input type="hidden" name="cmd" value="<?= $paypal['cmd'] ?>">
219
    <input type="hidden" name="image_url" value="<?php echo "$paypal[site_url]$paypal[image_url]"; ?>">
220
    <input type="hidden" name="return" value="<?php echo "$paypal[site_url]$paypal[success_url]"; ?>">
221
    <input type="hidden" name="cancel_return" value="<?php echo "$paypal[site_url]$paypal[cancel_url]"; ?>">
222
    <input type="hidden" name="notify_url" value="<?php echo "$paypal[site_url]$paypal[notify_url]"; ?>">
223
    <input type="hidden" name="rm" value="<?= $paypal['return_method'] ?>">
224
    <input type="hidden" name="currency_code" value="<?= $paypal['currency_code'] ?>">
225
    <input type="hidden" name="lc" value="<?= $paypal['lc'] ?>">
226
    <input type="hidden" name="bn" value="<?= $paypal['bn'] ?>">
227
    <input type="hidden" name="cbt" value="<?= $paypal['continue_button_text'] ?>">
228
229
    <!-- Payment Page Information -->
230
    <input type="hidden" name="no_shipping" value="<?= $paypal['display_shipping_address'] ?>">
231
    <input type="hidden" name="no_note" value="<?= $paypal['display_comment'] ?>">
232
    <input type="hidden" name="cn" value="<?= $paypal['comment_header'] ?>">
233
    <input type="hidden" name="cs" value="<?= $paypal['background_color'] ?>">
234
235
    <!-- Product Information -->
236
    <input type="hidden" name="item_name" value="<?= $paypal['item_name'] ?>">
237
    <input type="hidden" name="amount" value="<?= $paypal['amount'] ?>">
238
    <input type="hidden" name="quantity" value="<?= $paypal['quantity'] ?>">
239
    <input type="hidden" name="item_number" value="<?= $paypal['item_number'] ?>">
240
    <input type="hidden" name="undefined_quantity" value="<?= $paypal['edit_quantity'] ?>">
241
    <input type="hidden" name="on0" value="<?= $paypal['on0'] ?>">
242
    <input type="hidden" name="os0" value="<?= $paypal['os0'] ?>">
243
    <input type="hidden" name="on1" value="<?= $paypal['on1'] ?>">
244
    <input type="hidden" name="os1" value="<?= $paypal['os1'] ?>">
245
246
    <!-- Shipping and Misc Information -->
247
    <input type="hidden" name="shipping" value="<?= $paypal['shipping_amount'] ?>">
248
    <input type="hidden" name="shipping2" value="<?= $paypal['shipping_amount_per_item'] ?>">
249
    <input type="hidden" name="handling" value="<?= $paypal['handling_amount'] ?>">
250
    <input type="hidden" name="tax" value="<?= $paypal['tax'] ?>">
251
    <input type="hidden" name="custom" value="<?= $paypal['custom'] ?>">
252
    <input type="hidden" name="invoice" value="<?= $paypal['invoice'] ?>">
253
254
    <!-- Customer Information -->
255
    <input type="hidden" name="first_name" value="<?= $paypal['firstname'] ?>">
256
    <input type="hidden" name="last_name" value="<?= $paypal['lastname'] ?>">
257
    <input type="hidden" name="address1" value="<?= $paypal['address1'] ?>">
258
    <input type="hidden" name="address2" value="<?= $paypal['address2'] ?>">
259
    <input type="hidden" name="city" value="<?= $paypal['city'] ?>">
260
    <input type="hidden" name="state" value="<?= $paypal['state'] ?>">
261
    <input type="hidden" name="zip" value="<?= $paypal['zip'] ?>">
262
    <input type="hidden" name="email" value="<?= $paypal['email'] ?>">
263
    <input type="hidden" name="night_phone_a" value="<?= $paypal['phone_1'] ?>">
264
    <input type="hidden" name="night_phone_b" value="<?= $paypal['phone_2'] ?>">
265
    <input type="hidden" name="night_phone_c" value="<?= $paypal['phone_3'] ?>">
266
267
    <?php
268
}
269