Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / OpenDocument.php
blob15fe8f63e279653eb966d04852ba6a284a9dce60
1 <?php
2 /**
3 * Simple interface for creating OASIS OpenDocument files.
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin;
10 use DateTime;
12 use const DATE_W3C;
14 /**
15 * Simplified OpenDocument creator class
17 class OpenDocument
19 public const NS = <<<'EOT'
20 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
21 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
22 xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
23 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
24 xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
25 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
26 EOT;
28 /**
29 * Minimalistic creator of OASIS OpenDocument
31 * @param string $mime desired MIME type
32 * @param string $data document content
34 * @return string OASIS OpenDocument data
36 public static function create(string $mime, string $data): string
38 // Use the same date method as other PHP libs
39 // https://github.com/PHPOffice/PhpSpreadsheet/blob/1.22.0/src/PhpSpreadsheet/Writer/Ods/Meta.php#L49
40 $dateTimeCreation = (new DateTime())->format(DATE_W3C);
41 $data = [
42 $mime,
43 $data,
44 '<?xml version="1.0" encoding="UTF-8"?' . '>'
45 . '<office:document-meta '
46 . 'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" '
47 . 'xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" '
48 . 'office:version="1.0">'
49 . '<office:meta>'
50 . '<meta:generator>phpMyAdmin ' . Version::VERSION . '</meta:generator>'
51 . '<meta:initial-creator>phpMyAdmin ' . Version::VERSION
52 . '</meta:initial-creator>'
53 . '<meta:creation-date>' . $dateTimeCreation
54 . '</meta:creation-date>'
55 . '</office:meta>'
56 . '</office:document-meta>',
57 '<?xml version="1.0" encoding="UTF-8"?' . '>'
58 . '<office:document-styles ' . self::NS
59 . ' office:version="1.0">'
60 . '<office:font-face-decls>'
61 . '<style:font-face style:name="Arial Unicode MS"'
62 . ' svg:font-family="\'Arial Unicode MS\'" style:font-pitch="variable"/>'
63 . '<style:font-face style:name="DejaVu Sans1"'
64 . ' svg:font-family="\'DejaVu Sans\'" style:font-pitch="variable"/>'
65 . '<style:font-face style:name="HG Mincho Light J"'
66 . ' svg:font-family="\'HG Mincho Light J\'" style:font-pitch="variable"/>'
67 . '<style:font-face style:name="DejaVu Serif"'
68 . ' svg:font-family="\'DejaVu Serif\'" style:font-family-generic="roman"'
69 . ' style:font-pitch="variable"/>'
70 . '<style:font-face style:name="Thorndale"'
71 . ' svg:font-family="Thorndale" style:font-family-generic="roman"'
72 . ' style:font-pitch="variable"/>'
73 . '<style:font-face style:name="DejaVu Sans"'
74 . ' svg:font-family="\'DejaVu Sans\'" style:font-family-generic="swiss"'
75 . ' style:font-pitch="variable"/>'
76 . '</office:font-face-decls>'
77 . '<office:styles>'
78 . '<style:default-style style:family="paragraph">'
79 . '<style:paragraph-properties fo:hyphenation-ladder-count="no-limit"'
80 . ' style:text-autospace="ideograph-alpha" style:punctuation-wrap="hanging"'
81 . ' style:line-break="strict" style:tab-stop-distance="0.4925in"'
82 . ' style:writing-mode="page"/>'
83 . '<style:text-properties style:use-window-font-color="true"'
84 . ' style:font-name="DejaVu Serif" fo:font-size="12pt" fo:language="en"'
85 . ' fo:country="US" style:font-name-asian="DejaVu Sans1"'
86 . ' style:font-size-asian="12pt" style:language-asian="none"'
87 . ' style:country-asian="none" style:font-name-complex="DejaVu Sans1"'
88 . ' style:font-size-complex="12pt" style:language-complex="none"'
89 . ' style:country-complex="none" fo:hyphenate="false"'
90 . ' fo:hyphenation-remain-char-count="2"'
91 . ' fo:hyphenation-push-char-count="2"/>'
92 . '</style:default-style>'
93 . '<style:style style:name="Standard" style:family="paragraph"'
94 . ' style:class="text"/>'
95 . '<style:style style:name="Text_body" style:display-name="Text body"'
96 . ' style:family="paragraph" style:parent-style-name="Standard"'
97 . ' style:class="text">'
98 . '<style:paragraph-properties fo:margin-top="0in"'
99 . ' fo:margin-bottom="0.0835in"/>'
100 . '</style:style>'
101 . '<style:style style:name="Heading" style:family="paragraph"'
102 . ' style:parent-style-name="Standard" style:next-style-name="Text_body"'
103 . ' style:class="text">'
104 . '<style:paragraph-properties fo:margin-top="0.1665in"'
105 . ' fo:margin-bottom="0.0835in" fo:keep-with-next="always"/>'
106 . '<style:text-properties style:font-name="DejaVu Sans" fo:font-size="14pt"'
107 . ' style:font-name-asian="DejaVu Sans1" style:font-size-asian="14pt"'
108 . ' style:font-name-complex="DejaVu Sans1" style:font-size-complex="14pt"/>'
109 . '</style:style>'
110 . '<style:style style:name="Heading_1" style:display-name="Heading 1"'
111 . ' style:family="paragraph" style:parent-style-name="Heading"'
112 . ' style:next-style-name="Text_body" style:class="text"'
113 . ' style:default-outline-level="1">'
114 . '<style:text-properties style:font-name="Thorndale" fo:font-size="24pt"'
115 . ' fo:font-weight="bold" style:font-name-asian="HG Mincho Light J"'
116 . ' style:font-size-asian="24pt" style:font-weight-asian="bold"'
117 . ' style:font-name-complex="Arial Unicode MS"'
118 . ' style:font-size-complex="24pt" style:font-weight-complex="bold"/>'
119 . '</style:style>'
120 . '<style:style style:name="Heading_2" style:display-name="Heading 2"'
121 . ' style:family="paragraph" style:parent-style-name="Heading"'
122 . ' style:next-style-name="Text_body" style:class="text"'
123 . ' style:default-outline-level="2">'
124 . '<style:text-properties style:font-name="DejaVu Serif"'
125 . ' fo:font-size="18pt" fo:font-weight="bold"'
126 . ' style:font-name-asian="DejaVu Sans1" style:font-size-asian="18pt"'
127 . ' style:font-weight-asian="bold" style:font-name-complex="DejaVu Sans1"'
128 . ' style:font-size-complex="18pt" style:font-weight-complex="bold"/>'
129 . '</style:style>'
130 . '</office:styles>'
131 . '<office:automatic-styles>'
132 . '<style:page-layout style:name="pm1">'
133 . '<style:page-layout-properties fo:page-width="8.2673in"'
134 . ' fo:page-height="11.6925in" style:num-format="1"'
135 . ' style:print-orientation="portrait" fo:margin-top="1in"'
136 . ' fo:margin-bottom="1in" fo:margin-left="1.25in"'
137 . ' fo:margin-right="1.25in" style:writing-mode="lr-tb"'
138 . ' style:footnote-max-height="0in">'
139 . '<style:footnote-sep style:width="0.0071in"'
140 . ' style:distance-before-sep="0.0398in"'
141 . ' style:distance-after-sep="0.0398in" style:adjustment="left"'
142 . ' style:rel-width="25%" style:color="#000000"/>'
143 . '</style:page-layout-properties>'
144 . '<style:header-style/>'
145 . '<style:footer-style/>'
146 . '</style:page-layout>'
147 . '</office:automatic-styles>'
148 . '<office:master-styles>'
149 . '<style:master-page style:name="Standard" style:page-layout-name="pm1"/>'
150 . '</office:master-styles>'
151 . '</office:document-styles>',
152 '<?xml version="1.0" encoding="UTF-8"?' . '>'
153 . '<manifest:manifest'
154 . ' xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">'
155 . '<manifest:file-entry manifest:media-type="' . $mime
156 . '" manifest:full-path="/"/>'
157 . '<manifest:file-entry manifest:media-type="text/xml"'
158 . ' manifest:full-path="content.xml"/>'
159 . '<manifest:file-entry manifest:media-type="text/xml"'
160 . ' manifest:full-path="meta.xml"/>'
161 . '<manifest:file-entry manifest:media-type="text/xml"'
162 . ' manifest:full-path="styles.xml"/>'
163 . '</manifest:manifest>',
166 $name = ['mimetype', 'content.xml', 'meta.xml', 'styles.xml', 'META-INF/manifest.xml'];
168 $zipExtension = new ZipExtension();
170 $fileContents = $zipExtension->createFile($data, $name);
172 if ($fileContents === false) {
173 // TODO: this needs to gracefully fail instead of returning false or empty string
174 return '';
177 return $fileContents;