Issues (13)

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/Parser/CsvParser.php (2 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
3
namespace Linio\Component\SpreadsheetParser\Parser;
4
5
class CsvParser implements ParserInterface
6
{
7
    const OPTION_HAS_COLUMN_NAMES = 'has_column_names';
8
    const OPTION_LENGTH = 'length';
9
    const OPTION_DELIMITER = 'delimiter';
10
    const OPTION_ENCLOSURE = 'enclosure';
11
    const OPTION_ESCAPE = 'escape';
12
13
    /**
14
     * @var string
15
     */
16
    protected $filePath;
17
18
    /**
19
     * @var resource
20
     */
21
    protected $handle;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $hasColumnNames;
27
28
    /**
29
     * @var int
30
     */
31
    protected $length;
32
33
    /**
34
     * @var string
35
     */
36
    protected $delimiter;
37
38
    /**
39
     * @var string
40
     */
41
    protected $enclosure;
42
43
    /**
44
     * @var string
45
     */
46
    protected $escape;
47
48
    /**
49
     * @var array
50
     */
51
    protected $columnNames;
52
53
    /**
54
     * @param string $filePath
55
     */
56
    public function __construct($filePath, array $options = [])
57
    {
58
        $this->filePath = $filePath;
59
60
        // default options
61
        $this->hasColumnNames = true;
62
        $this->length = 0;
63
        $this->delimiter = ',';
64
        $this->enclosure = '"';
65
        $this->escape = '\\';
66
67
        $this->loadParserOptions($options);
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function open()
74
    {
75
        if ($this->handle) {
76
            return true;
77
        }
78
79
        $this->handle = fopen($this->filePath, 'r');
80
81
        return true;
82
    }
83
84
    /**
85
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
86
     *
87
     * @return array|false
88
     */
89
    public function getColumnNames()
90
    {
91
        if (!$this->hasColumnNames) {
92
            return false;
93
        }
94
95
        if ($this->columnNames) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columnNames of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
96
            return $this->columnNames;
97
        }
98
99
        $this->open();
100
        $this->columnNames = fgetcsv($this->handle, $this->length, $this->delimiter, $this->enclosure, $this->escape);
101
102
        return $this->columnNames;
103
    }
104
105
    /**
106
     * @param int $numRows
107
     *
108
     * @return array
109
     */
110 View Code Duplication
    public function getData($numRows = 0)
0 ignored issues
show
This method seems to be duplicated in 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...
111
    {
112
        $this->close();
113
        $this->open();
114
115
        $skipLine = false;
116
        if ($this->hasColumnNames) {
117
            $skipLine = true;
118
        }
119
120
        $data = $this->readDataFromFile($numRows, $skipLine);
121
122
        return $data;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function close()
129
    {
130
        if ($this->handle) {
131
            fclose($this->handle);
132
            $this->handle = null;
133
        }
134
135
        return true;
136
    }
137
138
    public function __destruct()
139
    {
140
        $this->close();
141
    }
142
143
    /**
144
     * @param array $options
145
     *
146
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
147
     */
148
    protected function loadParserOptions(array $options)
149
    {
150
        if (isset($options[static::OPTION_HAS_COLUMN_NAMES])) {
151
            $this->hasColumnNames = $options[static::OPTION_HAS_COLUMN_NAMES];
152
        }
153
154
        if (isset($options[static::OPTION_LENGTH])) {
155
            $this->length = $options[static::OPTION_LENGTH];
156
        }
157
158
        if (isset($options[static::OPTION_DELIMITER])) {
159
            $this->delimiter = $options[static::OPTION_DELIMITER];
160
        }
161
162
        if (isset($options[static::OPTION_ENCLOSURE])) {
163
            $this->enclosure = $options[static::OPTION_ENCLOSURE];
164
        }
165
166
        if (isset($options[static::OPTION_ESCAPE])) {
167
            $this->escape = $options[static::OPTION_ESCAPE];
168
        }
169
    }
170
171
    /**
172
     * @param $numRows
173
     * @param $skipLine
174
     *
175
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
176
     *
177
     * @return array
178
     */
179
    protected function readDataFromFile($numRows, $skipLine)
180
    {
181
        $data = [];
182
        while (($row = fgetcsv($this->handle, $this->length, $this->delimiter, $this->enclosure, $this->escape)) !== false) {
183
            if ($skipLine) {
184
                $skipLine = false;
185
                continue;
186
            }
187
188
            $data[] = $row;
189
190
            $numRows--;
191
            if ($numRows == 0) {
192
                break;
193
            }
194
        }
195
196
        return $data;
197
    }
198
}
199