ExportOds.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Set of functions used to build OpenDocument Spreadsheet dumps of tables
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\FieldMetadata;
  9. use PhpMyAdmin\OpenDocument;
  10. use PhpMyAdmin\Plugins\ExportPlugin;
  11. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  12. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  13. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  14. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  15. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  16. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  17. use function __;
  18. use function bin2hex;
  19. use function date;
  20. use function htmlspecialchars;
  21. use function stripslashes;
  22. use function strtotime;
  23. /**
  24. * Handles the export for the ODS class
  25. */
  26. class ExportOds extends ExportPlugin
  27. {
  28. protected function init(): void
  29. {
  30. $GLOBALS['ods_buffer'] = '';
  31. }
  32. /**
  33. * @psalm-return non-empty-lowercase-string
  34. */
  35. public function getName(): string
  36. {
  37. return 'ods';
  38. }
  39. protected function setProperties(): ExportPluginProperties
  40. {
  41. $exportPluginProperties = new ExportPluginProperties();
  42. $exportPluginProperties->setText('OpenDocument Spreadsheet');
  43. $exportPluginProperties->setExtension('ods');
  44. $exportPluginProperties->setMimeType('application/vnd.oasis.opendocument.spreadsheet');
  45. $exportPluginProperties->setForceFile(true);
  46. $exportPluginProperties->setOptionsText(__('Options'));
  47. // create the root group that will be the options field for
  48. // $exportPluginProperties
  49. // this will be shown as "Format specific options"
  50. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  51. // general options main group
  52. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  53. // create primary items and add them to the group
  54. $leaf = new TextPropertyItem(
  55. 'null',
  56. __('Replace NULL with:')
  57. );
  58. $generalOptions->addProperty($leaf);
  59. $leaf = new BoolPropertyItem(
  60. 'columns',
  61. __('Put columns names in the first row')
  62. );
  63. $generalOptions->addProperty($leaf);
  64. $leaf = new HiddenPropertyItem('structure_or_data');
  65. $generalOptions->addProperty($leaf);
  66. // add the main group to the root group
  67. $exportSpecificOptions->addProperty($generalOptions);
  68. // set the options for the export plugin property item
  69. $exportPluginProperties->setOptions($exportSpecificOptions);
  70. return $exportPluginProperties;
  71. }
  72. /**
  73. * Outputs export header
  74. */
  75. public function exportHeader(): bool
  76. {
  77. $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
  78. . '<office:document-content '
  79. . OpenDocument::NS . ' office:version="1.0">'
  80. . '<office:automatic-styles>'
  81. . '<number:date-style style:name="N37"'
  82. . ' number:automatic-order="true">'
  83. . '<number:month number:style="long"/>'
  84. . '<number:text>/</number:text>'
  85. . '<number:day number:style="long"/>'
  86. . '<number:text>/</number:text>'
  87. . '<number:year/>'
  88. . '</number:date-style>'
  89. . '<number:time-style style:name="N43">'
  90. . '<number:hours number:style="long"/>'
  91. . '<number:text>:</number:text>'
  92. . '<number:minutes number:style="long"/>'
  93. . '<number:text>:</number:text>'
  94. . '<number:seconds number:style="long"/>'
  95. . '<number:text> </number:text>'
  96. . '<number:am-pm/>'
  97. . '</number:time-style>'
  98. . '<number:date-style style:name="N50"'
  99. . ' number:automatic-order="true"'
  100. . ' number:format-source="language">'
  101. . '<number:month/>'
  102. . '<number:text>/</number:text>'
  103. . '<number:day/>'
  104. . '<number:text>/</number:text>'
  105. . '<number:year/>'
  106. . '<number:text> </number:text>'
  107. . '<number:hours number:style="long"/>'
  108. . '<number:text>:</number:text>'
  109. . '<number:minutes number:style="long"/>'
  110. . '<number:text> </number:text>'
  111. . '<number:am-pm/>'
  112. . '</number:date-style>'
  113. . '<style:style style:name="DateCell" style:family="table-cell"'
  114. . ' style:parent-style-name="Default" style:data-style-name="N37"/>'
  115. . '<style:style style:name="TimeCell" style:family="table-cell"'
  116. . ' style:parent-style-name="Default" style:data-style-name="N43"/>'
  117. . '<style:style style:name="DateTimeCell" style:family="table-cell"'
  118. . ' style:parent-style-name="Default" style:data-style-name="N50"/>'
  119. . '</office:automatic-styles>'
  120. . '<office:body>'
  121. . '<office:spreadsheet>';
  122. return true;
  123. }
  124. /**
  125. * Outputs export footer
  126. */
  127. public function exportFooter(): bool
  128. {
  129. $GLOBALS['ods_buffer'] .= '</office:spreadsheet></office:body></office:document-content>';
  130. return $this->export->outputHandler(
  131. OpenDocument::create(
  132. 'application/vnd.oasis.opendocument.spreadsheet',
  133. $GLOBALS['ods_buffer']
  134. )
  135. );
  136. }
  137. /**
  138. * Outputs database header
  139. *
  140. * @param string $db Database name
  141. * @param string $dbAlias Aliases of db
  142. */
  143. public function exportDBHeader($db, $dbAlias = ''): bool
  144. {
  145. return true;
  146. }
  147. /**
  148. * Outputs database footer
  149. *
  150. * @param string $db Database name
  151. */
  152. public function exportDBFooter($db): bool
  153. {
  154. return true;
  155. }
  156. /**
  157. * Outputs CREATE DATABASE statement
  158. *
  159. * @param string $db Database name
  160. * @param string $exportType 'server', 'database', 'table'
  161. * @param string $dbAlias Aliases of db
  162. */
  163. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  164. {
  165. return true;
  166. }
  167. /**
  168. * Outputs the content of a table in NHibernate format
  169. *
  170. * @param string $db database name
  171. * @param string $table table name
  172. * @param string $crlf the end of line sequence
  173. * @param string $errorUrl the url to go back in case of error
  174. * @param string $sqlQuery SQL query for obtaining data
  175. * @param array $aliases Aliases of db/table/columns
  176. */
  177. public function exportData(
  178. $db,
  179. $table,
  180. $crlf,
  181. $errorUrl,
  182. $sqlQuery,
  183. array $aliases = []
  184. ): bool {
  185. global $what, $dbi;
  186. $db_alias = $db;
  187. $table_alias = $table;
  188. $this->initAlias($aliases, $db_alias, $table_alias);
  189. // Gets the data from the database
  190. $result = $dbi->query($sqlQuery, DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_UNBUFFERED);
  191. $fields_cnt = $result->numFields();
  192. /** @var FieldMetadata[] $fieldsMeta */
  193. $fieldsMeta = $dbi->getFieldsMeta($result);
  194. $GLOBALS['ods_buffer'] .= '<table:table table:name="' . htmlspecialchars($table_alias) . '">';
  195. // If required, get fields name at the first line
  196. if (isset($GLOBALS[$what . '_columns'])) {
  197. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  198. foreach ($fieldsMeta as $field) {
  199. $col_as = $field->name;
  200. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  201. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  202. }
  203. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
  204. . '<text:p>'
  205. . htmlspecialchars(
  206. stripslashes($col_as)
  207. )
  208. . '</text:p>'
  209. . '</table:table-cell>';
  210. }
  211. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  212. }
  213. // Format the data
  214. while ($row = $result->fetchRow()) {
  215. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  216. for ($j = 0; $j < $fields_cnt; $j++) {
  217. if ($fieldsMeta[$j]->isMappedTypeGeometry) {
  218. // export GIS types as hex
  219. $row[$j] = '0x' . bin2hex($row[$j]);
  220. }
  221. if (! isset($row[$j])) {
  222. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
  223. . '<text:p>'
  224. . htmlspecialchars($GLOBALS[$what . '_null'])
  225. . '</text:p>'
  226. . '</table:table-cell>';
  227. } elseif ($fieldsMeta[$j]->isBinary && $fieldsMeta[$j]->isBlob) {
  228. // ignore BLOB
  229. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
  230. . '<text:p></text:p>'
  231. . '</table:table-cell>';
  232. } elseif ($fieldsMeta[$j]->isType(FieldMetadata::TYPE_DATE)) {
  233. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date"'
  234. . ' office:date-value="'
  235. . date('Y-m-d', strtotime($row[$j]))
  236. . '" table:style-name="DateCell">'
  237. . '<text:p>'
  238. . htmlspecialchars($row[$j])
  239. . '</text:p>'
  240. . '</table:table-cell>';
  241. } elseif ($fieldsMeta[$j]->isType(FieldMetadata::TYPE_TIME)) {
  242. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="time"'
  243. . ' office:time-value="'
  244. . date('\P\TH\Hi\Ms\S', strtotime($row[$j]))
  245. . '" table:style-name="TimeCell">'
  246. . '<text:p>'
  247. . htmlspecialchars($row[$j])
  248. . '</text:p>'
  249. . '</table:table-cell>';
  250. } elseif ($fieldsMeta[$j]->isType(FieldMetadata::TYPE_DATETIME)) {
  251. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="date"'
  252. . ' office:date-value="'
  253. . date('Y-m-d\TH:i:s', strtotime($row[$j]))
  254. . '" table:style-name="DateTimeCell">'
  255. . '<text:p>'
  256. . htmlspecialchars($row[$j])
  257. . '</text:p>'
  258. . '</table:table-cell>';
  259. } elseif (
  260. $fieldsMeta[$j]->isNumeric
  261. ) {
  262. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="float"'
  263. . ' office:value="' . $row[$j] . '" >'
  264. . '<text:p>'
  265. . htmlspecialchars($row[$j])
  266. . '</text:p>'
  267. . '</table:table-cell>';
  268. } else {
  269. $GLOBALS['ods_buffer'] .= '<table:table-cell office:value-type="string">'
  270. . '<text:p>'
  271. . htmlspecialchars($row[$j])
  272. . '</text:p>'
  273. . '</table:table-cell>';
  274. }
  275. }
  276. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  277. }
  278. $GLOBALS['ods_buffer'] .= '</table:table>';
  279. return true;
  280. }
  281. /**
  282. * Outputs result raw query in ODS format
  283. *
  284. * @param string $errorUrl the url to go back in case of error
  285. * @param string|null $db the database where the query is executed
  286. * @param string $sqlQuery the rawquery to output
  287. * @param string $crlf the end of line sequence
  288. */
  289. public function exportRawQuery(string $errorUrl, ?string $db, string $sqlQuery, string $crlf): bool
  290. {
  291. global $dbi;
  292. if ($db !== null) {
  293. $dbi->selectDb($db);
  294. }
  295. return $this->exportData($db ?? '', '', $crlf, $errorUrl, $sqlQuery);
  296. }
  297. }