Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Reader;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\DefinedName;
8
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\PageSetup;
9
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\Properties;
10
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\Styles;
11
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
12
use PhpOffice\PhpSpreadsheet\ReferenceHelper;
13
use PhpOffice\PhpSpreadsheet\RichText\RichText;
14
use PhpOffice\PhpSpreadsheet\Settings;
15
use PhpOffice\PhpSpreadsheet\Shared\File;
16
use PhpOffice\PhpSpreadsheet\Spreadsheet;
17
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
18
use SimpleXMLElement;
19
use XMLReader;
20
 
21
class Gnumeric extends BaseReader
22
{
23
    const NAMESPACE_GNM = 'http://www.gnumeric.org/v10.dtd'; // gmr in old sheets
24
 
25
    const NAMESPACE_XSI = 'http://www.w3.org/2001/XMLSchema-instance';
26
 
27
    const NAMESPACE_OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
28
 
29
    const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink';
30
 
31
    const NAMESPACE_DC = 'http://purl.org/dc/elements/1.1/';
32
 
33
    const NAMESPACE_META = 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
34
 
35
    const NAMESPACE_OOO = 'http://openoffice.org/2004/office';
36
 
37
    /**
38
     * Shared Expressions.
39
     *
40
     * @var array
41
     */
42
    private $expressions = [];
43
 
44
    /**
45
     * Spreadsheet shared across all functions.
46
     *
47
     * @var Spreadsheet
48
     */
49
    private $spreadsheet;
50
 
51
    /** @var ReferenceHelper */
52
    private $referenceHelper;
53
 
54
    /** @var array */
55
    public static $mappings = [
56
        'dataType' => [
57
            '10' => DataType::TYPE_NULL,
58
            '20' => DataType::TYPE_BOOL,
59
            '30' => DataType::TYPE_NUMERIC, // Integer doesn't exist in Excel
60
            '40' => DataType::TYPE_NUMERIC, // Float
61
            '50' => DataType::TYPE_ERROR,
62
            '60' => DataType::TYPE_STRING,
63
            //'70':        //    Cell Range
64
            //'80':        //    Array
65
        ],
66
    ];
67
 
68
    /**
69
     * Create a new Gnumeric.
70
     */
71
    public function __construct()
72
    {
73
        parent::__construct();
74
        $this->referenceHelper = ReferenceHelper::getInstance();
75
        $this->securityScanner = XmlScanner::getInstance($this);
76
    }
77
 
78
    /**
79
     * Can the current IReader read the file?
80
     */
81
    public function canRead(string $filename): bool
82
    {
83
        $data = null;
84
        if (File::testFileNoThrow($filename)) {
85
            $data = $this->gzfileGetContents($filename);
86
            if (strpos($data, self::NAMESPACE_GNM) === false) {
87
                $data = '';
88
            }
89
        }
90
 
91
        return !empty($data);
92
    }
93
 
94
    private static function matchXml(XMLReader $xml, string $expectedLocalName): bool
95
    {
96
        return $xml->namespaceURI === self::NAMESPACE_GNM
97
            && $xml->localName === $expectedLocalName
98
            && $xml->nodeType === XMLReader::ELEMENT;
99
    }
100
 
101
    /**
102
     * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
103
     *
104
     * @param string $filename
105
     *
106
     * @return array
107
     */
108
    public function listWorksheetNames($filename)
109
    {
110
        File::assertFile($filename);
111
        if (!$this->canRead($filename)) {
112
            throw new Exception($filename . ' is an invalid Gnumeric file.');
113
        }
114
 
115
        $xml = new XMLReader();
116
        $contents = $this->gzfileGetContents($filename);
117
        $xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
118
        $xml->setParserProperty(2, true);
119
 
120
        $worksheetNames = [];
121
        while ($xml->read()) {
122
            if (self::matchXml($xml, 'SheetName')) {
123
                $xml->read(); //    Move onto the value node
124
                $worksheetNames[] = (string) $xml->value;
125
            } elseif (self::matchXml($xml, 'Sheets')) {
126
                //    break out of the loop once we've got our sheet names rather than parse the entire file
127
                break;
128
            }
129
        }
130
 
131
        return $worksheetNames;
132
    }
133
 
134
    /**
135
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
136
     *
137
     * @param string $filename
138
     *
139
     * @return array
140
     */
141
    public function listWorksheetInfo($filename)
142
    {
143
        File::assertFile($filename);
144
        if (!$this->canRead($filename)) {
145
            throw new Exception($filename . ' is an invalid Gnumeric file.');
146
        }
147
 
148
        $xml = new XMLReader();
149
        $contents = $this->gzfileGetContents($filename);
150
        $xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
151
        $xml->setParserProperty(2, true);
152
 
153
        $worksheetInfo = [];
154
        while ($xml->read()) {
155
            if (self::matchXml($xml, 'Sheet')) {
156
                $tmpInfo = [
157
                    'worksheetName' => '',
158
                    'lastColumnLetter' => 'A',
159
                    'lastColumnIndex' => 0,
160
                    'totalRows' => 0,
161
                    'totalColumns' => 0,
162
                ];
163
 
164
                while ($xml->read()) {
165
                    if (self::matchXml($xml, 'Name')) {
166
                        $xml->read(); //    Move onto the value node
167
                        $tmpInfo['worksheetName'] = (string) $xml->value;
168
                    } elseif (self::matchXml($xml, 'MaxCol')) {
169
                        $xml->read(); //    Move onto the value node
170
                        $tmpInfo['lastColumnIndex'] = (int) $xml->value;
171
                        $tmpInfo['totalColumns'] = (int) $xml->value + 1;
172
                    } elseif (self::matchXml($xml, 'MaxRow')) {
173
                        $xml->read(); //    Move onto the value node
174
                        $tmpInfo['totalRows'] = (int) $xml->value + 1;
175
 
176
                        break;
177
                    }
178
                }
179
                $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
180
                $worksheetInfo[] = $tmpInfo;
181
            }
182
        }
183
 
184
        return $worksheetInfo;
185
    }
186
 
187
    /**
188
     * @param string $filename
189
     *
190
     * @return string
191
     */
192
    private function gzfileGetContents($filename)
193
    {
194
        $data = '';
195
        $contents = @file_get_contents($filename);
196
        if ($contents !== false) {
197
            if (substr($contents, 0, 2) === "\x1f\x8b") {
198
                // Check if gzlib functions are available
199
                if (function_exists('gzdecode')) {
200
                    $contents = @gzdecode($contents);
201
                    if ($contents !== false) {
202
                        $data = $contents;
203
                    }
204
                }
205
            } else {
206
                $data = $contents;
207
            }
208
        }
209
        if ($data !== '') {
210
            $data = $this->getSecurityScannerOrThrow()->scan($data);
211
        }
212
 
213
        return $data;
214
    }
215
 
216
    public static function gnumericMappings(): array
217
    {
218
        return array_merge(self::$mappings, Styles::$mappings);
219
    }
220
 
221
    private function processComments(SimpleXMLElement $sheet): void
222
    {
223
        if ((!$this->readDataOnly) && (isset($sheet->Objects))) {
224
            foreach ($sheet->Objects->children(self::NAMESPACE_GNM) as $key => $comment) {
225
                $commentAttributes = $comment->attributes();
226
                //    Only comment objects are handled at the moment
227
                if ($commentAttributes && $commentAttributes->Text) {
228
                    $this->spreadsheet->getActiveSheet()->getComment((string) $commentAttributes->ObjectBound)
229
                        ->setAuthor((string) $commentAttributes->Author)
230
                        ->setText($this->parseRichText((string) $commentAttributes->Text));
231
                }
232
            }
233
        }
234
    }
235
 
236
    /**
237
     * @param mixed $value
238
     */
239
    private static function testSimpleXml($value): SimpleXMLElement
240
    {
241
        return ($value instanceof SimpleXMLElement) ? $value : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
242
    }
243
 
244
    /**
245
     * Loads Spreadsheet from file.
246
     */
247
    protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
248
    {
249
        // Create new Spreadsheet
250
        $spreadsheet = new Spreadsheet();
251
        $spreadsheet->removeSheetByIndex(0);
252
 
253
        // Load into this instance
254
        return $this->loadIntoExisting($filename, $spreadsheet);
255
    }
256
 
257
    /**
258
     * Loads from file into Spreadsheet instance.
259
     */
260
    public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
261
    {
262
        $this->spreadsheet = $spreadsheet;
263
        File::assertFile($filename);
264
        if (!$this->canRead($filename)) {
265
            throw new Exception($filename . ' is an invalid Gnumeric file.');
266
        }
267
 
268
        $gFileData = $this->gzfileGetContents($filename);
269
 
270
        $xml2 = simplexml_load_string($gFileData, 'SimpleXMLElement', Settings::getLibXmlLoaderOptions());
271
        $xml = self::testSimpleXml($xml2);
272
 
273
        $gnmXML = $xml->children(self::NAMESPACE_GNM);
274
        (new Properties($this->spreadsheet))->readProperties($xml, $gnmXML);
275
 
276
        $worksheetID = 0;
277
        foreach ($gnmXML->Sheets->Sheet as $sheetOrNull) {
278
            $sheet = self::testSimpleXml($sheetOrNull);
279
            $worksheetName = (string) $sheet->Name;
280
            if (is_array($this->loadSheetsOnly) && !in_array($worksheetName, $this->loadSheetsOnly, true)) {
281
                continue;
282
            }
283
 
284
            $maxRow = $maxCol = 0;
285
 
286
            // Create new Worksheet
287
            $this->spreadsheet->createSheet();
288
            $this->spreadsheet->setActiveSheetIndex($worksheetID);
289
            //    Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
290
            //        cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
291
            //        name in line with the formula, not the reverse
292
            $this->spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
293
 
294
            $visibility = $sheet->attributes()['Visibility'] ?? 'GNM_SHEET_VISIBILITY_VISIBLE';
295
            if ((string) $visibility !== 'GNM_SHEET_VISIBILITY_VISIBLE') {
296
                $this->spreadsheet->getActiveSheet()->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
297
            }
298
 
299
            if (!$this->readDataOnly) {
300
                (new PageSetup($this->spreadsheet))
301
                    ->printInformation($sheet)
302
                    ->sheetMargins($sheet);
303
            }
304
 
305
            foreach ($sheet->Cells->Cell as $cellOrNull) {
306
                $cell = self::testSimpleXml($cellOrNull);
307
                $cellAttributes = self::testSimpleXml($cell->attributes());
308
                $row = (int) $cellAttributes->Row + 1;
309
                $column = (int) $cellAttributes->Col;
310
 
311
                $maxRow = max($maxRow, $row);
312
                $maxCol = max($maxCol, $column);
313
 
314
                $column = Coordinate::stringFromColumnIndex($column + 1);
315
 
316
                // Read cell?
317
                if ($this->getReadFilter() !== null) {
318
                    if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) {
319
                        continue;
320
                    }
321
                }
322
 
323
                $this->loadCell($cell, $worksheetName, $cellAttributes, $column, $row);
324
            }
325
 
326
            if ($sheet->Styles !== null) {
327
                (new Styles($this->spreadsheet, $this->readDataOnly))->read($sheet, $maxRow, $maxCol);
328
            }
329
 
330
            $this->processComments($sheet);
331
            $this->processColumnWidths($sheet, $maxCol);
332
            $this->processRowHeights($sheet, $maxRow);
333
            $this->processMergedCells($sheet);
334
            $this->processAutofilter($sheet);
335
 
336
            $this->setSelectedCells($sheet);
337
            ++$worksheetID;
338
        }
339
 
340
        $this->processDefinedNames($gnmXML);
341
 
342
        $this->setSelectedSheet($gnmXML);
343
 
344
        // Return
345
        return $this->spreadsheet;
346
    }
347
 
348
    private function setSelectedSheet(SimpleXMLElement $gnmXML): void
349
    {
350
        if (isset($gnmXML->UIData)) {
351
            $attributes = self::testSimpleXml($gnmXML->UIData->attributes());
352
            $selectedSheet = (int) $attributes['SelectedTab'];
353
            $this->spreadsheet->setActiveSheetIndex($selectedSheet);
354
        }
355
    }
356
 
357
    private function setSelectedCells(?SimpleXMLElement $sheet): void
358
    {
359
        if ($sheet !== null && isset($sheet->Selections)) {
360
            foreach ($sheet->Selections as $selection) {
361
                $startCol = (int) ($selection->StartCol ?? 0);
362
                $startRow = (int) ($selection->StartRow ?? 0) + 1;
363
                $endCol = (int) ($selection->EndCol ?? $startCol);
364
                $endRow = (int) ($selection->endRow ?? 0) + 1;
365
 
366
                $startColumn = Coordinate::stringFromColumnIndex($startCol + 1);
367
                $endColumn = Coordinate::stringFromColumnIndex($endCol + 1);
368
 
369
                $startCell = "{$startColumn}{$startRow}";
370
                $endCell = "{$endColumn}{$endRow}";
371
                $selectedRange = $startCell . (($endCell !== $startCell) ? ':' . $endCell : '');
372
                $this->spreadsheet->getActiveSheet()->setSelectedCell($selectedRange);
373
 
374
                break;
375
            }
376
        }
377
    }
378
 
379
    private function processMergedCells(?SimpleXMLElement $sheet): void
380
    {
381
        //    Handle Merged Cells in this worksheet
382
        if ($sheet !== null && isset($sheet->MergedRegions)) {
383
            foreach ($sheet->MergedRegions->Merge as $mergeCells) {
384
                if (strpos((string) $mergeCells, ':') !== false) {
385
                    $this->spreadsheet->getActiveSheet()->mergeCells($mergeCells, Worksheet::MERGE_CELL_CONTENT_HIDE);
386
                }
387
            }
388
        }
389
    }
390
 
391
    private function processAutofilter(?SimpleXMLElement $sheet): void
392
    {
393
        if ($sheet !== null && isset($sheet->Filters)) {
394
            foreach ($sheet->Filters->Filter as $autofilter) {
395
                if ($autofilter !== null) {
396
                    $attributes = $autofilter->attributes();
397
                    if (isset($attributes['Area'])) {
398
                        $this->spreadsheet->getActiveSheet()->setAutoFilter((string) $attributes['Area']);
399
                    }
400
                }
401
            }
402
        }
403
    }
404
 
405
    private function setColumnWidth(int $whichColumn, float $defaultWidth): void
406
    {
407
        $columnDimension = $this->spreadsheet->getActiveSheet()
408
            ->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1));
409
        if ($columnDimension !== null) {
410
            $columnDimension->setWidth($defaultWidth);
411
        }
412
    }
413
 
414
    private function setColumnInvisible(int $whichColumn): void
415
    {
416
        $columnDimension = $this->spreadsheet->getActiveSheet()
417
            ->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1));
418
        if ($columnDimension !== null) {
419
            $columnDimension->setVisible(false);
420
        }
421
    }
422
 
423
    private function processColumnLoop(int $whichColumn, int $maxCol, ?SimpleXMLElement $columnOverride, float $defaultWidth): int
424
    {
425
        $columnOverride = self::testSimpleXml($columnOverride);
426
        $columnAttributes = self::testSimpleXml($columnOverride->attributes());
427
        $column = $columnAttributes['No'];
428
        $columnWidth = ((float) $columnAttributes['Unit']) / 5.4;
429
        $hidden = (isset($columnAttributes['Hidden'])) && ((string) $columnAttributes['Hidden'] == '1');
430
        $columnCount = (int) ($columnAttributes['Count'] ?? 1);
431
        while ($whichColumn < $column) {
432
            $this->setColumnWidth($whichColumn, $defaultWidth);
433
            ++$whichColumn;
434
        }
435
        while (($whichColumn < ($column + $columnCount)) && ($whichColumn <= $maxCol)) {
436
            $this->setColumnWidth($whichColumn, $columnWidth);
437
            if ($hidden) {
438
                $this->setColumnInvisible($whichColumn);
439
            }
440
            ++$whichColumn;
441
        }
442
 
443
        return $whichColumn;
444
    }
445
 
446
    private function processColumnWidths(?SimpleXMLElement $sheet, int $maxCol): void
447
    {
448
        if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Cols))) {
449
            //    Column Widths
450
            $defaultWidth = 0;
451
            $columnAttributes = $sheet->Cols->attributes();
452
            if ($columnAttributes !== null) {
453
                $defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4;
454
            }
455
            $whichColumn = 0;
456
            foreach ($sheet->Cols->ColInfo as $columnOverride) {
457
                $whichColumn = $this->processColumnLoop($whichColumn, $maxCol, $columnOverride, $defaultWidth);
458
            }
459
            while ($whichColumn <= $maxCol) {
460
                $this->setColumnWidth($whichColumn, $defaultWidth);
461
                ++$whichColumn;
462
            }
463
        }
464
    }
465
 
466
    private function setRowHeight(int $whichRow, float $defaultHeight): void
467
    {
468
        $rowDimension = $this->spreadsheet->getActiveSheet()->getRowDimension($whichRow);
469
        if ($rowDimension !== null) {
470
            $rowDimension->setRowHeight($defaultHeight);
471
        }
472
    }
473
 
474
    private function setRowInvisible(int $whichRow): void
475
    {
476
        $rowDimension = $this->spreadsheet->getActiveSheet()->getRowDimension($whichRow);
477
        if ($rowDimension !== null) {
478
            $rowDimension->setVisible(false);
479
        }
480
    }
481
 
482
    private function processRowLoop(int $whichRow, int $maxRow, ?SimpleXMLElement $rowOverride, float $defaultHeight): int
483
    {
484
        $rowOverride = self::testSimpleXml($rowOverride);
485
        $rowAttributes = self::testSimpleXml($rowOverride->attributes());
486
        $row = $rowAttributes['No'];
487
        $rowHeight = (float) $rowAttributes['Unit'];
488
        $hidden = (isset($rowAttributes['Hidden'])) && ((string) $rowAttributes['Hidden'] == '1');
489
        $rowCount = (int) ($rowAttributes['Count'] ?? 1);
490
        while ($whichRow < $row) {
491
            ++$whichRow;
492
            $this->setRowHeight($whichRow, $defaultHeight);
493
        }
494
        while (($whichRow < ($row + $rowCount)) && ($whichRow < $maxRow)) {
495
            ++$whichRow;
496
            $this->setRowHeight($whichRow, $rowHeight);
497
            if ($hidden) {
498
                $this->setRowInvisible($whichRow);
499
            }
500
        }
501
 
502
        return $whichRow;
503
    }
504
 
505
    private function processRowHeights(?SimpleXMLElement $sheet, int $maxRow): void
506
    {
507
        if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Rows))) {
508
            //    Row Heights
509
            $defaultHeight = 0;
510
            $rowAttributes = $sheet->Rows->attributes();
511
            if ($rowAttributes !== null) {
512
                $defaultHeight = (float) $rowAttributes['DefaultSizePts'];
513
            }
514
            $whichRow = 0;
515
 
516
            foreach ($sheet->Rows->RowInfo as $rowOverride) {
517
                $whichRow = $this->processRowLoop($whichRow, $maxRow, $rowOverride, $defaultHeight);
518
            }
519
            // never executed, I can't figure out any circumstances
520
            // under which it would be executed, and, even if
521
            // such exist, I'm not convinced this is needed.
522
            //while ($whichRow < $maxRow) {
523
            //    ++$whichRow;
524
            //    $this->spreadsheet->getActiveSheet()->getRowDimension($whichRow)->setRowHeight($defaultHeight);
525
            //}
526
        }
527
    }
528
 
529
    private function processDefinedNames(?SimpleXMLElement $gnmXML): void
530
    {
531
        //    Loop through definedNames (global named ranges)
532
        if ($gnmXML !== null && isset($gnmXML->Names)) {
533
            foreach ($gnmXML->Names->Name as $definedName) {
534
                $name = (string) $definedName->name;
535
                $value = (string) $definedName->value;
536
                if (stripos($value, '#REF!') !== false) {
537
                    continue;
538
                }
539
 
540
                [$worksheetName] = Worksheet::extractSheetTitle($value, true);
541
                $worksheetName = trim($worksheetName, "'");
542
                $worksheet = $this->spreadsheet->getSheetByName($worksheetName);
543
                // Worksheet might still be null if we're only loading selected sheets rather than the full spreadsheet
544
                if ($worksheet !== null) {
545
                    $this->spreadsheet->addDefinedName(DefinedName::createInstance($name, $worksheet, $value));
546
                }
547
            }
548
        }
549
    }
550
 
551
    private function parseRichText(string $is): RichText
552
    {
553
        $value = new RichText();
554
        $value->createText($is);
555
 
556
        return $value;
557
    }
558
 
559
    private function loadCell(
560
        SimpleXMLElement $cell,
561
        string $worksheetName,
562
        SimpleXMLElement $cellAttributes,
563
        string $column,
564
        int $row
565
    ): void {
566
        $ValueType = $cellAttributes->ValueType;
567
        $ExprID = (string) $cellAttributes->ExprID;
568
        $type = DataType::TYPE_FORMULA;
569
        if ($ExprID > '') {
570
            if (((string) $cell) > '') {
571
                $this->expressions[$ExprID] = [
572
                    'column' => $cellAttributes->Col,
573
                    'row' => $cellAttributes->Row,
574
                    'formula' => (string) $cell,
575
                ];
576
            } else {
577
                $expression = $this->expressions[$ExprID];
578
 
579
                $cell = $this->referenceHelper->updateFormulaReferences(
580
                    $expression['formula'],
581
                    'A1',
582
                    $cellAttributes->Col - $expression['column'],
583
                    $cellAttributes->Row - $expression['row'],
584
                    $worksheetName
585
                );
586
            }
587
            $type = DataType::TYPE_FORMULA;
588
        } else {
589
            $vtype = (string) $ValueType;
590
            if (array_key_exists($vtype, self::$mappings['dataType'])) {
591
                $type = self::$mappings['dataType'][$vtype];
592
            }
593
            if ($vtype === '20') {        //    Boolean
594
                $cell = $cell == 'TRUE';
595
            }
596
        }
597
 
598
        $this->spreadsheet->getActiveSheet()->getCell($column . $row)->setValueExplicit((string) $cell, $type);
599
        if (isset($cellAttributes->ValueFormat)) {
600
            $this->spreadsheet->getActiveSheet()->getCell($column . $row)
601
                ->getStyle()->getNumberFormat()
602
                ->setFormatCode((string) $cellAttributes->ValueFormat);
603
        }
604
    }
605
}