Issues (2687)

Security Analysis    not enabled

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/Eccube/Service/CsvImportService.php (1 issue)

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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service;
25
26
use Eccube\Application;
27
28
29
/**
30
 * Copyright (C) 2012-2014 David de Boer <[email protected]>
31
 *
32
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
33
 * this software and associated documentation files (the "Software"), to deal in
34
 * the Software without restriction, including without limitation the rights to
35
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
36
 * the Software, and to permit persons to whom the Software is furnished to do so,
37
 * subject to the following conditions:
38
 *
39
 * The above copyright notice and this permission notice shall be included in all
40
 * copies or substantial portions of the Software.
41
 *
42
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48
 * SOFTWARE.
49
 */
50
class CsvImportService implements \Iterator, \SeekableIterator, \Countable
51
{
52
53
    const DUPLICATE_HEADERS_INCREMENT = 1;
54
    const DUPLICATE_HEADERS_MERGE = 2;
55
56
    /**
57
     * Number of the row that contains the column names
58
     *
59
     * @var integer
60
     */
61
    protected $headerRowNumber;
62
63
    /**
64
     * CSV file
65
     *
66
     * @var \SplFileObject
67
     */
68
    protected $file;
69
70
    /**
71
     * Column headers as read from the CSV file
72
     *
73
     * @var array
74
     */
75
    protected $columnHeaders = array();
76
77
    /**
78
     * Number of column headers, stored and re-used for performance
79
     *
80
     * In case of duplicate headers, this is always the number of unmerged headers.
81
     *
82
     * @var integer
83
     */
84
    protected $headersCount;
85
86
    /**
87
     * Total number of rows in the CSV file
88
     *
89
     * @var integer
90
     */
91
    protected $count;
92
93
    /**
94
     * Faulty CSV rows
95
     *
96
     * @var array
97
     */
98
    protected $errors = array();
99
100
    /**
101
     * How to handle duplicate headers
102
     *
103
     * @var integer
104
     */
105
    protected $duplicateHeadersFlag;
106
107
108
    /**
109
     * @param \SplFileObject $file
110
     * @param string $delimiter
111
     * @param string $enclosure
112
     * @param string $escape
113
     */
114 14
    public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
115
    {
116 14
        ini_set('auto_detect_line_endings', true);
117
118 14
        $this->file = $file;
119 14
        $this->file->setFlags(
120 14
            \SplFileObject::READ_CSV |
121 14
            \SplFileObject::SKIP_EMPTY |
122 14
            \SplFileObject::READ_AHEAD |
123 14
            \SplFileObject::DROP_NEW_LINE
124
        );
125 14
        $this->file->setCsvControl(
126
            $delimiter,
127
            $enclosure,
128
            $escape
129
        );
130
    }
131
132
    /**
133
     * Return the current row as an array
134
     *
135
     * If a header row has been set, an associative array will be returned
136
     *
137
     * @return array
138
     */
139 9
    public function current()
140
    {
141
        // If the CSV has no column headers just return the line
142 9
        if (empty($this->columnHeaders)) {
143
            return $this->file->current();
144
        }
145
146
        // Since the CSV has column headers use them to construct an associative array for the columns in this line
147 9
        if ($this->valid()) {
148 9
            $current = $this->file->current();
149 9
            $current = $this->convertEncodingRows($current);
150
151 9
            $line = $current;
152
153
            // See if values for duplicate headers should be merged
154 9
            if (self::DUPLICATE_HEADERS_MERGE === $this->duplicateHeadersFlag) {
155 1
                $line = $this->mergeDuplicates($line);
156
            }
157
158
            // Count the number of elements in both: they must be equal.
159 9
            if (count($this->columnHeaders) === count($line)) {
160 9
                return array_combine(array_keys($this->columnHeaders), $line);
161
            } else {
162
                return $line;
163
            }
164
165
        };
166
167
        return null;
168
    }
169
170
    /**
171
     * Get column headers
172
     *
173
     * @return array
174
     */
175 8
    public function getColumnHeaders()
176
    {
177 8
        return array_keys($this->columnHeaders);
178
    }
179
180
    /**
181
     * Set column headers
182
     *
183
     * @param array $columnHeaders
184
     */
185 11
    public function setColumnHeaders(array $columnHeaders)
186
    {
187 11
        $columnHeaders = $this->convertEncodingRows($columnHeaders);
188 11
        $this->columnHeaders = array_count_values($columnHeaders);
189 11
        $this->headersCount = count($columnHeaders);
190
    }
191
192
    /**
193
     * Set header row number
194
     *
195
     * @param integer $rowNumber Number of the row that contains column header names
196
     * @param integer $duplicates How to handle duplicates (optional). One of:
197
     *                        - CsvReader::DUPLICATE_HEADERS_INCREMENT;
198
     *                        increments duplicates (dup, dup1, dup2 etc.)
199
     *                        - CsvReader::DUPLICATE_HEADERS_MERGE; merges
200
     *                        values for duplicate headers into an array
201
     *                        (dup => [value1, value2, value3])
202
     * @return boolean
203
     */
204 9
    public function setHeaderRowNumber($rowNumber, $duplicates = null)
205
    {
206 9
        $this->duplicateHeadersFlag = $duplicates;
207 9
        $this->headerRowNumber = $rowNumber;
208 9
        $headers = $this->readHeaderRow($rowNumber);
209
210 9
        if ($headers === false) {
211
            return false;
212
        }
213 9
        $this->setColumnHeaders($headers);
214 9
        return true;
0 ignored issues
show
Missing blank line before return statement
Loading history...
215
    }
216
217
    /**
218
     * Rewind the file pointer
219
     *
220
     * If a header row has been set, the pointer is set just below the header
221
     * row. That way, when you iterate over the rows, that header row is
222
     * skipped.
223
     */
224 13
    public function rewind()
225
    {
226 13
        $this->file->rewind();
227 13
        if (null !== $this->headerRowNumber) {
228 9
            $this->file->seek($this->headerRowNumber + 1);
229
        }
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 9
    public function count()
236
    {
237 9
        if (null === $this->count) {
238 9
            $position = $this->key();
239
240 9
            $this->count = iterator_count($this);
241
242 9
            $this->seek($position);
243
        }
244
245 9
        return $this->count;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 12
    public function next()
252
    {
253 12
        $this->file->next();
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 13
    public function valid()
260
    {
261 13
        return $this->file->valid();
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 9
    public function key()
268
    {
269 9
        return $this->file->key();
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275 10
    public function seek($pointer)
276
    {
277 10
        $this->file->seek($pointer);
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283 1
    public function getFields()
284
    {
285 1
        return $this->getColumnHeaders();
286
    }
287
288
    /**
289
     * Get a row
290
     *
291
     * @param integer $number Row number
292
     *
293
     * @return array
294
     */
295 1
    public function getRow($number)
296
    {
297 1
        $this->seek($number);
298
299 1
        return $this->current();
300
    }
301
302
    /**
303
     * Get rows that have an invalid number of columns
304
     *
305
     * @return array
306
     */
307
    public function getErrors()
308
    {
309
        if (0 === $this->key()) {
310
            // Iterator has not yet been processed, so do that now
311
            foreach ($this as $row) { /* noop */
312
            }
313
        }
314
315
        return $this->errors;
316
    }
317
318
    /**
319
     * Does the reader contain any invalid rows?
320
     *
321
     * @return boolean
322
     */
323
    public function hasErrors()
324
    {
325
        return count($this->getErrors()) > 0;
326
    }
327
328
    /**
329
     * Read header row from CSV file
330
     *
331
     * @param integer $rowNumber Row number
332
     *
333
     * @return array
334
     *
335
     */
336 9
    protected function readHeaderRow($rowNumber)
337
    {
338 9
        $this->file->seek($rowNumber);
339 9
        $headers = $this->file->current();
340
341 9
        return $headers;
342
    }
343
344
    /**
345
     * Add an increment to duplicate headers
346
     *
347
     * So the following line:
348
     * |duplicate|duplicate|duplicate|
349
     * |first    |second   |third    |
350
     *
351
     * Yields value:
352
     * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third'
353
     *
354
     * @param array $headers
355
     *
356
     * @return array
357
     */
358
    protected function incrementHeaders(array $headers)
359
    {
360
        $incrementedHeaders = array();
361
        foreach (array_count_values($headers) as $header => $count) {
362
            if ($count > 1) {
363
                $incrementedHeaders[] = $header;
364
                for ($i = 1; $i < $count; $i++) {
365
                    $incrementedHeaders[] = $header . $i;
366
                }
367
            } else {
368
                $incrementedHeaders[] = $header;
369
            }
370
        }
371
372
        return $incrementedHeaders;
373
    }
374
375
    /**
376
     * Merges values for duplicate headers into an array
377
     *
378
     * So the following line:
379
     * |duplicate|duplicate|duplicate|
380
     * |first    |second   |third    |
381
     *
382
     * Yields value:
383
     * $duplicate => ['first', 'second', 'third']
384
     *
385
     * @param array $line
386
     *
387
     * @return array
388
     */
389 1
    protected function mergeDuplicates(array $line)
390
    {
391 1
        $values = array();
392
393 1
        $i = 0;
394 1
        foreach ($this->columnHeaders as $count) {
395 1
            if (1 === $count) {
396 1
                $values[] = $line[$i];
397
            } else {
398 1
                $values[] = array_slice($line, $i, $count);
399
            }
400
401 1
            $i += $count;
402
        }
403
404 1
        return $values;
405
    }
406
407
    /**
408
     * 行の文字エンコーディングを変換する.
409
     *
410
     * Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する.
411
     * それ以外の環境では何もしない。
412
     */
413 11
    protected function convertEncodingRows($row) {
414 11
        if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
415
            foreach ($row as &$col) {
416
                $col = mb_convert_encoding($col , 'UTF-8', 'SJIS-win');
417
            }
418
        }
419 11
        return $row;
420
    }
421
}
422