Issues (21)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Shipment/File.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace MrPrompt\Centercob\Shipment;
3
4
use DateTime;
5
use MrPrompt\ShipmentCommon\Base\Bank;
6
use MrPrompt\ShipmentCommon\Base\Cart;
7
use MrPrompt\ShipmentCommon\Base\Email;
8
use MrPrompt\ShipmentCommon\Base\Phone;
9
use MrPrompt\ShipmentCommon\Base\Charge;
10
use MrPrompt\ShipmentCommon\Base\Holder;
11
use MrPrompt\ShipmentCommon\Base\Parcel;
12
use MrPrompt\ShipmentCommon\Base\Seller;
13
use MrPrompt\ShipmentCommon\Util\Number;
14
use MrPrompt\ShipmentCommon\Base\Address;
15
use MrPrompt\ShipmentCommon\Base\Parcels;
16
use MrPrompt\ShipmentCommon\Base\Customer;
17
use MrPrompt\ShipmentCommon\Base\Document;
18
use MrPrompt\ShipmentCommon\Base\Sequence;
19
use MrPrompt\ShipmentCommon\Base\Purchaser;
20
use MrPrompt\ShipmentCommon\Base\CreditCard;
21
use MrPrompt\ShipmentCommon\Base\Occurrence;
22
use MrPrompt\ShipmentCommon\Base\BankAccount;
23
use MrPrompt\Centercob\Shipment\Partial\Detail;
24
use MrPrompt\Centercob\Shipment\Partial\Footer;
25
use MrPrompt\Centercob\Shipment\Partial\Header;
26
use MrPrompt\ShipmentCommon\Base\Authorization;
27
use MrPrompt\ShipmentCommon\Base\ConsumerUnity;
28
29
/**
30
 * Shipment file class
31
 *
32
 * @author Thiago Paes <[email protected]>
33
 */
34
class File
35
{
36
    /**
37
     * File name template
38
     *
39
     * @var string
40
     */
41
    const TEMPLATE_GENERATED = '{CLIENT}_{DDMMYYYY}_{SEQUENCE}.TXT';
42
43
    /**
44
     * File name template
45
     *
46
     * @var string
47
     */
48
    const TEMPLATE_PROCESSED = '{CLIENT}_{DDMMYYYY}_{SEQUENCE}.TXT.RET';
49
50
    /**
51
     * @var DateTime
52
     */
53
    private $now;
54
55
    /**
56
     * @var string
57
     */
58
    private $content;
59
60
    /**
61
     * @var Header
62
     */
63
    private $header;
64
65
    /**
66
     * @var Cart
67
     */
68
    private $cart;
69
70
    /**
71
     * @var Footer
72
     */
73
    private $footer;
74
75
    /**
76
     * @var Sequence
77
     */
78
    private $sequence;
79
80
    /**
81
     * @var Customer
82
     */
83
    private $customer;
84
85
    /**
86
     * @var string
87
     */
88
    private $storage;
89
90
    /**
91
     * @var string
92
     */
93
    private $template;
94
95
    /**
96
     * @param Customer $customer
97
     * @param Sequence $sequence
98
     * @param DateTime $today
99
     * @param string   $storageDir
100
     */
101 8
    public function __construct(
102
        Customer $customer,
103
        Sequence $sequence,
104
        DateTime $today,
105
        $storageDir = null
106
    ) {
107 8
        $this->customer     = $customer;
108 8
        $this->now          = $today;
109 8
        $this->sequence     = $sequence;
110 8
        $this->storage      = $storageDir;
111 8
        $this->content      = null;
112 8
    }
113
114
    /**
115
     * @return Cart
116
     */
117 1
    public function getCart()
118
    {
119 1
        return $this->cart;
120
    }
121
122
    /**
123
     * @param Cart $cart
124
     */
125 1
    public function setCart(Cart $cart)
126
    {
127 1
        $this->cart = $cart;
128 1
    }
129
130
    /**
131
     * @return Footer
132
     */
133 1
    public function getFooter()
134
    {
135 1
        return $this->footer;
136
    }
137
138
    /**
139
     * @param Footer $footer
140
     */
141 1
    public function setFooter(Footer $footer)
142
    {
143 1
        $this->footer = $footer;
144 1
    }
145
146
    /**
147
     * @return Header
148
     */
149 1
    public function getHeader()
150
    {
151 1
        return $this->header;
152
    }
153
154
    /**
155
     * @param Header $header
156
     */
157 1
    public function setHeader(Header $header)
158
    {
159 1
        $this->header = $header;
160 1
    }
161
162
    /**
163
     * Create the file name
164
     *
165
     * @return string
166
     */
167
    private function createFilename($type = self::TEMPLATE_GENERATED)
168
    {
169
        $search = [
170
            '{CLIENT}',
171
            '{DDMMYYYY}',
172
            '{SEQUENCE}'
173
        ];
174
175
        $replace = [
176
            Number::zeroFill($this->customer->getCode(), 6, Number::FILL_LEFT),
177
            $this->now->format('dmY'),
178
            Number::zeroFill($this->sequence->getValue(), 5, Number::FILL_LEFT),
179
        ];
180
181
        return str_replace($search, $replace, $type);
182
    }
183
184
    /**
185
     * @param $parcels
186
     * @return int
187
     */
188
    private function getTotalPrice($parcels)
189
    {
190
        $price = 0;
191
192
        foreach ($parcels as $parcel) {
193
            $price += $parcel->getPrice();
194
        }
195
196
        return $price;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    private function generateContent()
203
    {
204
        $totalPrice     = 0;
205
        $counter        = 2;
206
207
        $this->header = new Header($this->customer, $this->sequence, $this->now);
208
        $this->content  = $this->header->render() . PHP_EOL;
209
210
        /* @var $detail \Centercob\Gateway\Shipment\Partial\Detail */
211
        foreach ($this->cart as $detail) {
212
            $detail->getSequence()->setValue($counter);
213
214
            $this->content .= $detail->render() . PHP_EOL;
215
216
            $totalPrice += $this->getTotalPrice( $detail->getParcels() );
217
            $counter++;
218
        }
219
220
        $sequence = clone $this->sequence;
221
        $sequence->setValue($counter);
222
223
        $this->footer = new Footer(($counter - 1), $totalPrice, $sequence);
224
225
        $this->content .= $this->footer->render();
226
227
        return $this->content;
228
    }
229
230
    /**
231
     * @return string
232
     */
233 1
    public function save()
234
    {
235 1
        if (null === $this->content) {
236 1
            $this->content = $this->generateContent();
237
        }
238
239 1
        $this->template = self::TEMPLATE_GENERATED;
240
241 1
        $this->header->setSequence($this->sequence);
242
243 1
        $filename   = $this->createFilename($this->template);
244 1
        $outputFile = $this->storage . DIRECTORY_SEPARATOR . $filename;
245
246 1
        file_put_contents($outputFile, $this->content);
247
248 1
        return $outputFile;
249
    }
250
251
    /**
252
     * @return string
253
     */
254 1
    public function read()
255
    {
256 1
        $this->template = self::TEMPLATE_GENERATED;
257
258 1
        $file = $this->storage . DIRECTORY_SEPARATOR . $this->createFilename($this->template);
259
260 1
        $this->content  = file_get_contents($file);
261
262 1
        $details        = explode(PHP_EOL, $this->content);
263 1
        $headerLine     = array_shift($details);
264 1
        $footerLine     = array_pop($details);
265
266 1
        if (null == $footerLine) {
267
            $footerLine = array_pop($details);
268
        }
269
270 1
        $this->header   = new Header(
271 1
            $this->customer,
272 1
            new Sequence(substr($headerLine, 1000, 6)),
273 1
            $this->now
274
        );
275
276 1
        $this->footer   = new Footer(
277 1
            substr($footerLine, 1, 6),
278 1
            substr($footerLine, 7, 10),
279 1
            (new Sequence(substr($footerLine, 1000, 6)))
280
        );
281
282 1
        $this->cart = new Cart();
283
284
        /* @var $detail \Centercob\Gateway\Received\Partial\Detail */
285 1
        foreach ($details as $row) {
286
            $charge         = new Charge();
287
            $card           = new CreditCard();
288
            $account        = new BankAccount(new Bank(), new Holder());
289
            $unity          = new ConsumerUnity();
290
            $seller         = new Seller();
291
292
            $parcels        = $this->createParcels($row);
293
            $authorization  = $this->createAuthorization($row);
294
            $sequence       = new Sequence(substr($row, 1000, 6));
295
296
            // extracting object from line
297
            $unity->setNumber(substr($row, 9, 25));
298
            $unity->setRead(DateTime::createFromFormat('dmY', substr($row, 141, 8)));
0 ignored issues
show
It seems like \DateTime::createFromFor..., substr($row, 141, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\...onsumerUnity::setRead() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
299
            $unity->setMaturity(DateTime::createFromFormat('dmY', substr($row, 149, 8)));
0 ignored issues
show
It seems like \DateTime::createFromFor..., substr($row, 149, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\...merUnity::setMaturity() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
300
301
            $occurrence = new Occurrence();
302
            $occurrence->setType(substr($row, 44, 1));
303
            $occurrence->setReturn(substr($row, 783, 2));
304
            $occurrence->setDescription(substr($row, 785, 100));
305
306
            $charge->setCharging(substr($row, 8, 1));
307
            $charge->setOccurrence($occurrence);
308
309
            $address = new Address();
310
            $address->setCity(substr($row, 69, 50));
311
            $address->setState(substr($row, 119, 2));
312
            $address->setPostalCode(substr($row, 121, 8));
313
            $address->setAddress(substr($row, 461, 50));
314
            $address->setNumber(substr($row, 511, 6));
315
            $address->setDistrict(substr($row, 517, 30));
316
            $address->setComplement(substr($row, 547, 30));
317
318
            $seller->setCode(substr($row, 135, 6));
319
320
            $document = new Document();
321
            $document->setNumber(substr($row, 368, 15));
322
323
            $account->getHolder()->setName(substr($row, 158, 70));
324
            $account->getHolder()->setDocument($document);
325
326
            switch ($charge->getCharging()) {
327
                case Charge::CREDIT_CARD:
328
                    $card->setFlag(substr(substr($row, 129, 6), -2));
329
                    break;
330
331
                case Charge::DEBIT:
332
                    $account->getBank()->setCode(substr(substr($row, 129, 6), -2));
333
                    break;
334
335
                case Charge::ENERGY:
336
                    $unity->setCode(substr(substr($row, 129, 6), -2));
337
                    break;
338
            }
339
340
            $purchaser = $this->createPurchaser($row);
341
            $purchaser->setAddress($address);
342
            $purchaser->setDocument($document);
343
344
            $detail = new Detail(
345
                $this->customer,
346
                $charge,
347
                $seller,
348
                $purchaser,
349
                $parcels,
350
                $authorization,
351
                $card,
352
                $account,
353
                $unity,
354
                $sequence
355
            );
356
357
            $this->cart->append($detail);
358
        }
359
360
        return [
361 1
            $this->header,
362 1
            $this->cart,
363 1
            $this->footer
364
        ];
365
    }
366
367
    /**
368
     * @param string $row
369
     * @return Purchaser
370
     */
371
    private function createPurchaser($row)
372
    {
373
        $purchaser      = new Purchaser();
374
        $purchaser->setPerson(substr($row, 157, 1));
375
        $purchaser->setName(substr($row, 158, 70));
376
        $purchaser->setFantasyName(substr($row, 228, 70));
377
        $purchaser->setSocialReason(substr($row, 298, 70));
378
        $purchaser->setBirth(DateTime::createFromFormat('dmY', substr($row, 403, 8)));
0 ignored issues
show
It seems like \DateTime::createFromFor..., substr($row, 403, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\Base\Person::setBirth() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
379
        $purchaser->setEmail(new Email(substr($row, 411, 50)));
380
        $purchaser->setHomePhone(new Phone(substr($row, 577, 11), Phone::TELEPHONE));
381
        $purchaser->setCellPhone(new Phone(substr($row, 599, 11), Phone::CELLPHONE));
382
        $purchaser->setFatherName(substr($row, 617, 50));
383
        $purchaser->setMotherName(substr($row, 667, 50));
384
385
        return $purchaser;
386
    }
387
388
    /**
389
     * @param $row
390
     * @return Parcels
391
     */
392
    private function createParcels($row)
393
    {
394
395
        $parcelOne = new Parcel();
396
        $parcelOne->setMaturity(DateTime::createFromFormat('dmY', substr($row, 717, 8)));
0 ignored issues
show
It seems like \DateTime::createFromFor..., substr($row, 717, 8)) targeting DateTime::createFromFormat() can also be of type false; however, MrPrompt\ShipmentCommon\Base\Parcel::setMaturity() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
397
        $parcelOne->setPrice(substr($row, 725, 10));
398
        $parcelOne->setQuantity(substr($row, 735, 2));
399
        $parcelOne->setKey(0);
400
401
        $parcelTwo = new Parcel();
402
        $parcelTwo->setPrice(substr($row, 737, 10));
403
        $parcelTwo->setQuantity(substr($row, 747, 2));
404
        $parcelTwo->setKey(1);
405
406
        $parcelThree = new Parcel();
407
        $parcelThree->setPrice(substr($row, 749, 10));
408
        $parcelThree->setQuantity(substr($row, 759, 2));
409
        $parcelThree->setKey(2);
410
411
        $parcelFour = new Parcel();
412
        $parcelFour->setPrice(substr($row, 761, 10));
413
        $parcelFour->setQuantity(substr($row, 771, 2));
414
        $parcelFour->setKey(3);
415
416
        $parcels = new Parcels(4);
417
        $parcels->addParcel($parcelOne);
418
        $parcels->addParcel($parcelTwo);
419
        $parcels->addParcel($parcelThree);
420
        $parcels->addParcel($parcelFour);
421
422
        return $parcels;
423
    }
424
425
    /**
426
     * @param $row
427
     * @return Authorization
428
     */
429
    private function createAuthorization($row)
430
    {
431
        $authorization  = new Authorization();
432
        $authorization->setNumber(substr($row, 773, 10));
433
434
        return $authorization;
435
    }
436
}
437