| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the TYPO3 CMS project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * It is free software; you can redistribute it and/or modify it under | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * the terms of the GNU General Public License, either version 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please read the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * LICENSE.txt file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * The TYPO3 project - inspiring people to share! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace TYPO3\CMS\Core\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class with helper functions for CSV handling | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | class CsvUtility | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * Convert a string, formatted as CSV, into a multidimensional array | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * This cannot be done by str_getcsv, since it's impossible to handle enclosed cells with a line feed in it | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $input The CSV input | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $fieldDelimiter The field delimiter | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $fieldEnclosure The field enclosure | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param int $maximumColumns The maximum amount of columns | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public static function csvToArray($input, $fieldDelimiter = ',', $fieldEnclosure = '"', $maximumColumns = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $multiArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $maximumCellCount = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (($handle = fopen('php://memory', 'r+')) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             fwrite($handle, $input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             rewind($handle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             while (($cells = fgetcsv($handle, 0, $fieldDelimiter, $fieldEnclosure)) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $cells = is_array($cells) ? $cells : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $maximumCellCount = max(count($cells), $maximumCellCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $multiArray[] = preg_replace('|<br */?>|i', LF, $cells); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             fclose($handle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($maximumColumns > $maximumCellCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $maximumCellCount = $maximumColumns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         foreach ($multiArray as &$row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             for ($key = 0; $key < $maximumCellCount; $key++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     $maximumColumns > 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     && $maximumColumns < $maximumCellCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     && $key >= $maximumColumns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     if (isset($row[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         unset($row[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 } elseif (!isset($row[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     $row[$key] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |         return $multiArray; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param array $row Input array of values | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $delim Delimited, default is comma | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $quote Quote-character to wrap around the values. | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return string A single line of CSV | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public static function csvValues(array $row, $delim = ',', $quote = '"') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $out = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         foreach ($row as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $out[] = str_replace($quote, $quote . $quote, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |         return $quote . implode($quote . $delim . $quote, $out) . $quote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |