ExportHtmlword.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. /**
  3. * HTML-Word export code
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Plugins\ExportPlugin;
  9. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  10. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  11. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  12. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  13. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  14. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  15. use PhpMyAdmin\Util;
  16. use function __;
  17. use function htmlspecialchars;
  18. use function in_array;
  19. use function str_replace;
  20. use function stripslashes;
  21. /**
  22. * Handles the export for the HTML-Word format
  23. */
  24. class ExportHtmlword extends ExportPlugin
  25. {
  26. /**
  27. * @psalm-return non-empty-lowercase-string
  28. */
  29. public function getName(): string
  30. {
  31. return 'htmlword';
  32. }
  33. protected function setProperties(): ExportPluginProperties
  34. {
  35. $exportPluginProperties = new ExportPluginProperties();
  36. $exportPluginProperties->setText('Microsoft Word 2000');
  37. $exportPluginProperties->setExtension('doc');
  38. $exportPluginProperties->setMimeType('application/vnd.ms-word');
  39. $exportPluginProperties->setForceFile(true);
  40. $exportPluginProperties->setOptionsText(__('Options'));
  41. // create the root group that will be the options field for
  42. // $exportPluginProperties
  43. // this will be shown as "Format specific options"
  44. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  45. // what to dump (structure/data/both)
  46. $dumpWhat = new OptionsPropertyMainGroup(
  47. 'dump_what',
  48. __('Dump table')
  49. );
  50. // create primary items and add them to the group
  51. $leaf = new RadioPropertyItem('structure_or_data');
  52. $leaf->setValues(
  53. [
  54. 'structure' => __('structure'),
  55. 'data' => __('data'),
  56. 'structure_and_data' => __('structure and data'),
  57. ]
  58. );
  59. $dumpWhat->addProperty($leaf);
  60. // add the main group to the root group
  61. $exportSpecificOptions->addProperty($dumpWhat);
  62. // data options main group
  63. $dataOptions = new OptionsPropertyMainGroup(
  64. 'dump_what',
  65. __('Data dump options')
  66. );
  67. $dataOptions->setForce('structure');
  68. // create primary items and add them to the group
  69. $leaf = new TextPropertyItem(
  70. 'null',
  71. __('Replace NULL with:')
  72. );
  73. $dataOptions->addProperty($leaf);
  74. $leaf = new BoolPropertyItem(
  75. 'columns',
  76. __('Put columns names in the first row')
  77. );
  78. $dataOptions->addProperty($leaf);
  79. // add the main group to the root group
  80. $exportSpecificOptions->addProperty($dataOptions);
  81. // set the options for the export plugin property item
  82. $exportPluginProperties->setOptions($exportSpecificOptions);
  83. return $exportPluginProperties;
  84. }
  85. /**
  86. * Outputs export header
  87. */
  88. public function exportHeader(): bool
  89. {
  90. global $charset;
  91. return $this->export->outputHandler(
  92. '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  93. xmlns:x="urn:schemas-microsoft-com:office:word"
  94. xmlns="http://www.w3.org/TR/REC-html40">
  95. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
  96. . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  97. <html>
  98. <head>
  99. <meta http-equiv="Content-type" content="text/html;charset='
  100. . ($charset ?? 'utf-8') . '" />
  101. </head>
  102. <body>'
  103. );
  104. }
  105. /**
  106. * Outputs export footer
  107. */
  108. public function exportFooter(): bool
  109. {
  110. return $this->export->outputHandler('</body></html>');
  111. }
  112. /**
  113. * Outputs database header
  114. *
  115. * @param string $db Database name
  116. * @param string $dbAlias Aliases of db
  117. */
  118. public function exportDBHeader($db, $dbAlias = ''): bool
  119. {
  120. if (empty($dbAlias)) {
  121. $dbAlias = $db;
  122. }
  123. return $this->export->outputHandler(
  124. '<h1>' . __('Database') . ' ' . htmlspecialchars($dbAlias) . '</h1>'
  125. );
  126. }
  127. /**
  128. * Outputs database footer
  129. *
  130. * @param string $db Database name
  131. */
  132. public function exportDBFooter($db): bool
  133. {
  134. return true;
  135. }
  136. /**
  137. * Outputs CREATE DATABASE statement
  138. *
  139. * @param string $db Database name
  140. * @param string $exportType 'server', 'database', 'table'
  141. * @param string $dbAlias Aliases of db
  142. */
  143. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  144. {
  145. return true;
  146. }
  147. /**
  148. * Outputs the content of a table in HTML-Word format
  149. *
  150. * @param string $db database name
  151. * @param string $table table name
  152. * @param string $crlf the end of line sequence
  153. * @param string $errorUrl the url to go back in case of error
  154. * @param string $sqlQuery SQL query for obtaining data
  155. * @param array $aliases Aliases of db/table/columns
  156. */
  157. public function exportData(
  158. $db,
  159. $table,
  160. $crlf,
  161. $errorUrl,
  162. $sqlQuery,
  163. array $aliases = []
  164. ): bool {
  165. global $what, $dbi;
  166. $db_alias = $db;
  167. $table_alias = $table;
  168. $this->initAlias($aliases, $db_alias, $table_alias);
  169. if (
  170. ! $this->export->outputHandler(
  171. '<h2>'
  172. . __('Dumping data for table') . ' ' . htmlspecialchars($table_alias)
  173. . '</h2>'
  174. )
  175. ) {
  176. return false;
  177. }
  178. if (! $this->export->outputHandler('<table width="100%" cellspacing="1">')) {
  179. return false;
  180. }
  181. // Gets the data from the database
  182. $result = $dbi->query($sqlQuery, DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_UNBUFFERED);
  183. $fields_cnt = $result->numFields();
  184. // If required, get fields name at the first line
  185. if (isset($GLOBALS['htmlword_columns'])) {
  186. $schema_insert = '<tr class="print-category">';
  187. foreach ($result->getFieldNames() as $col_as) {
  188. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  189. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  190. }
  191. $col_as = stripslashes($col_as);
  192. $schema_insert .= '<td class="print"><strong>'
  193. . htmlspecialchars($col_as)
  194. . '</strong></td>';
  195. }
  196. $schema_insert .= '</tr>';
  197. if (! $this->export->outputHandler($schema_insert)) {
  198. return false;
  199. }
  200. }
  201. // Format the data
  202. while ($row = $result->fetchRow()) {
  203. $schema_insert = '<tr class="print-category">';
  204. for ($j = 0; $j < $fields_cnt; $j++) {
  205. if (! isset($row[$j])) {
  206. $value = $GLOBALS[$what . '_null'];
  207. } elseif ($row[$j] == '0' || $row[$j] != '') {
  208. $value = $row[$j];
  209. } else {
  210. $value = '';
  211. }
  212. $schema_insert .= '<td class="print">'
  213. . htmlspecialchars((string) $value)
  214. . '</td>';
  215. }
  216. $schema_insert .= '</tr>';
  217. if (! $this->export->outputHandler($schema_insert)) {
  218. return false;
  219. }
  220. }
  221. return $this->export->outputHandler('</table>');
  222. }
  223. /**
  224. * Returns a stand-in CREATE definition to resolve view dependencies
  225. *
  226. * @param string $db the database name
  227. * @param string $view the view name
  228. * @param string $crlf the end of line sequence
  229. * @param array $aliases Aliases of db/table/columns
  230. *
  231. * @return string resulting definition
  232. */
  233. public function getTableDefStandIn($db, $view, $crlf, $aliases = [])
  234. {
  235. global $dbi;
  236. $schema_insert = '<table width="100%" cellspacing="1">'
  237. . '<tr class="print-category">'
  238. . '<th class="print">'
  239. . __('Column')
  240. . '</th>'
  241. . '<td class="print"><strong>'
  242. . __('Type')
  243. . '</strong></td>'
  244. . '<td class="print"><strong>'
  245. . __('Null')
  246. . '</strong></td>'
  247. . '<td class="print"><strong>'
  248. . __('Default')
  249. . '</strong></td>'
  250. . '</tr>';
  251. /**
  252. * Get the unique keys in the view
  253. */
  254. $unique_keys = [];
  255. $keys = $dbi->getTableIndexes($db, $view);
  256. foreach ($keys as $key) {
  257. if ($key['Non_unique'] != 0) {
  258. continue;
  259. }
  260. $unique_keys[] = $key['Column_name'];
  261. }
  262. $columns = $dbi->getColumns($db, $view);
  263. foreach ($columns as $column) {
  264. $col_as = $column['Field'];
  265. if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) {
  266. $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as];
  267. }
  268. $schema_insert .= $this->formatOneColumnDefinition($column, $unique_keys, $col_as);
  269. $schema_insert .= '</tr>';
  270. }
  271. $schema_insert .= '</table>';
  272. return $schema_insert;
  273. }
  274. /**
  275. * Returns $table's CREATE definition
  276. *
  277. * @param string $db the database name
  278. * @param string $table the table name
  279. * @param bool $do_relation whether to include relation comments
  280. * @param bool $do_comments whether to include the pmadb-style column
  281. * comments as comments in the structure;
  282. * this is deprecated but the parameter is
  283. * left here because /export calls
  284. * PMA_exportStructure() also for other
  285. * export types which use this parameter
  286. * @param bool $do_mime whether to include mime comments
  287. * at the end
  288. * @param bool $view whether we're handling a view
  289. * @param array $aliases Aliases of db/table/columns
  290. *
  291. * @return string resulting schema
  292. */
  293. public function getTableDef(
  294. $db,
  295. $table,
  296. $do_relation,
  297. $do_comments,
  298. $do_mime,
  299. $view = false,
  300. array $aliases = []
  301. ) {
  302. global $dbi;
  303. $relationParameters = $this->relation->getRelationParameters();
  304. $schema_insert = '';
  305. /**
  306. * Gets fields properties
  307. */
  308. $dbi->selectDb($db);
  309. // Check if we can use Relations
  310. [$res_rel, $have_rel] = $this->relation->getRelationsAndStatus(
  311. $do_relation && $relationParameters->relationFeature !== null,
  312. $db,
  313. $table
  314. );
  315. /**
  316. * Displays the table structure
  317. */
  318. $schema_insert .= '<table width="100%" cellspacing="1">';
  319. $schema_insert .= '<tr class="print-category">';
  320. $schema_insert .= '<th class="print">'
  321. . __('Column')
  322. . '</th>';
  323. $schema_insert .= '<td class="print"><strong>'
  324. . __('Type')
  325. . '</strong></td>';
  326. $schema_insert .= '<td class="print"><strong>'
  327. . __('Null')
  328. . '</strong></td>';
  329. $schema_insert .= '<td class="print"><strong>'
  330. . __('Default')
  331. . '</strong></td>';
  332. if ($do_relation && $have_rel) {
  333. $schema_insert .= '<td class="print"><strong>'
  334. . __('Links to')
  335. . '</strong></td>';
  336. }
  337. if ($do_comments) {
  338. $schema_insert .= '<td class="print"><strong>'
  339. . __('Comments')
  340. . '</strong></td>';
  341. $comments = $this->relation->getComments($db, $table);
  342. }
  343. if ($do_mime && $relationParameters->browserTransformationFeature !== null) {
  344. $schema_insert .= '<td class="print"><strong>'
  345. . __('Media type')
  346. . '</strong></td>';
  347. $mime_map = $this->transformations->getMime($db, $table, true);
  348. }
  349. $schema_insert .= '</tr>';
  350. $columns = $dbi->getColumns($db, $table);
  351. /**
  352. * Get the unique keys in the table
  353. */
  354. $unique_keys = [];
  355. $keys = $dbi->getTableIndexes($db, $table);
  356. foreach ($keys as $key) {
  357. if ($key['Non_unique'] != 0) {
  358. continue;
  359. }
  360. $unique_keys[] = $key['Column_name'];
  361. }
  362. foreach ($columns as $column) {
  363. $col_as = $column['Field'];
  364. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  365. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  366. }
  367. $schema_insert .= $this->formatOneColumnDefinition($column, $unique_keys, $col_as);
  368. $field_name = $column['Field'];
  369. if ($do_relation && $have_rel) {
  370. $schema_insert .= '<td class="print">'
  371. . htmlspecialchars(
  372. $this->getRelationString(
  373. $res_rel,
  374. $field_name,
  375. $db,
  376. $aliases
  377. )
  378. )
  379. . '</td>';
  380. }
  381. if ($do_comments && $relationParameters->columnCommentsFeature !== null) {
  382. $schema_insert .= '<td class="print">'
  383. . (isset($comments[$field_name])
  384. ? htmlspecialchars($comments[$field_name])
  385. : '') . '</td>';
  386. }
  387. if ($do_mime && $relationParameters->browserTransformationFeature !== null) {
  388. $schema_insert .= '<td class="print">'
  389. . (isset($mime_map[$field_name]) ?
  390. htmlspecialchars(
  391. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  392. )
  393. : '') . '</td>';
  394. }
  395. $schema_insert .= '</tr>';
  396. }
  397. $schema_insert .= '</table>';
  398. return $schema_insert;
  399. }
  400. /**
  401. * Outputs triggers
  402. *
  403. * @param string $db database name
  404. * @param string $table table name
  405. *
  406. * @return string Formatted triggers list
  407. */
  408. protected function getTriggers($db, $table)
  409. {
  410. global $dbi;
  411. $dump = '<table width="100%" cellspacing="1">';
  412. $dump .= '<tr class="print-category">';
  413. $dump .= '<th class="print">' . __('Name') . '</th>';
  414. $dump .= '<td class="print"><strong>' . __('Time') . '</strong></td>';
  415. $dump .= '<td class="print"><strong>' . __('Event') . '</strong></td>';
  416. $dump .= '<td class="print"><strong>' . __('Definition') . '</strong></td>';
  417. $dump .= '</tr>';
  418. $triggers = $dbi->getTriggers($db, $table);
  419. foreach ($triggers as $trigger) {
  420. $dump .= '<tr class="print-category">';
  421. $dump .= '<td class="print">'
  422. . htmlspecialchars($trigger['name'])
  423. . '</td>'
  424. . '<td class="print">'
  425. . htmlspecialchars($trigger['action_timing'])
  426. . '</td>'
  427. . '<td class="print">'
  428. . htmlspecialchars($trigger['event_manipulation'])
  429. . '</td>'
  430. . '<td class="print">'
  431. . htmlspecialchars($trigger['definition'])
  432. . '</td>'
  433. . '</tr>';
  434. }
  435. $dump .= '</table>';
  436. return $dump;
  437. }
  438. /**
  439. * Outputs table's structure
  440. *
  441. * @param string $db database name
  442. * @param string $table table name
  443. * @param string $crlf the end of line sequence
  444. * @param string $errorUrl the url to go back in case of error
  445. * @param string $exportMode 'create_table', 'triggers', 'create_view',
  446. * 'stand_in'
  447. * @param string $exportType 'server', 'database', 'table'
  448. * @param bool $do_relation whether to include relation comments
  449. * @param bool $do_comments whether to include the pmadb-style column
  450. * comments as comments in the structure;
  451. * this is deprecated but the parameter is
  452. * left here because /export calls
  453. * PMA_exportStructure() also for other
  454. * export types which use this parameter
  455. * @param bool $do_mime whether to include mime comments
  456. * @param bool $dates whether to include creation/update/check dates
  457. * @param array $aliases Aliases of db/table/columns
  458. */
  459. public function exportStructure(
  460. $db,
  461. $table,
  462. $crlf,
  463. $errorUrl,
  464. $exportMode,
  465. $exportType,
  466. $do_relation = false,
  467. $do_comments = false,
  468. $do_mime = false,
  469. $dates = false,
  470. array $aliases = []
  471. ): bool {
  472. global $dbi;
  473. $db_alias = $db;
  474. $table_alias = $table;
  475. $this->initAlias($aliases, $db_alias, $table_alias);
  476. $dump = '';
  477. switch ($exportMode) {
  478. case 'create_table':
  479. $dump .= '<h2>'
  480. . __('Table structure for table') . ' '
  481. . htmlspecialchars($table_alias)
  482. . '</h2>';
  483. $dump .= $this->getTableDef($db, $table, $do_relation, $do_comments, $do_mime, false, $aliases);
  484. break;
  485. case 'triggers':
  486. $dump = '';
  487. $triggers = $dbi->getTriggers($db, $table);
  488. if ($triggers) {
  489. $dump .= '<h2>'
  490. . __('Triggers') . ' ' . htmlspecialchars($table_alias)
  491. . '</h2>';
  492. $dump .= $this->getTriggers($db, $table);
  493. }
  494. break;
  495. case 'create_view':
  496. $dump .= '<h2>'
  497. . __('Structure for view') . ' ' . htmlspecialchars($table_alias)
  498. . '</h2>';
  499. $dump .= $this->getTableDef($db, $table, $do_relation, $do_comments, $do_mime, true, $aliases);
  500. break;
  501. case 'stand_in':
  502. $dump .= '<h2>'
  503. . __('Stand-in structure for view') . ' '
  504. . htmlspecialchars($table_alias)
  505. . '</h2>';
  506. // export a stand-in definition to resolve view dependencies
  507. $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases);
  508. }
  509. return $this->export->outputHandler($dump);
  510. }
  511. /**
  512. * Formats the definition for one column
  513. *
  514. * @param array $column info about this column
  515. * @param array $unique_keys unique keys of the table
  516. * @param string $col_alias Column Alias
  517. *
  518. * @return string Formatted column definition
  519. */
  520. protected function formatOneColumnDefinition(
  521. array $column,
  522. array $unique_keys,
  523. $col_alias = ''
  524. ) {
  525. if (empty($col_alias)) {
  526. $col_alias = $column['Field'];
  527. }
  528. $definition = '<tr class="print-category">';
  529. $extracted_columnspec = Util::extractColumnSpec($column['Type']);
  530. $type = htmlspecialchars($extracted_columnspec['print_type']);
  531. if (empty($type)) {
  532. $type = '&nbsp;';
  533. }
  534. if (! isset($column['Default'])) {
  535. if ($column['Null'] !== 'NO') {
  536. $column['Default'] = 'NULL';
  537. }
  538. }
  539. $fmt_pre = '';
  540. $fmt_post = '';
  541. if (in_array($column['Field'], $unique_keys)) {
  542. $fmt_pre = '<strong>' . $fmt_pre;
  543. $fmt_post .= '</strong>';
  544. }
  545. if ($column['Key'] === 'PRI') {
  546. $fmt_pre = '<em>' . $fmt_pre;
  547. $fmt_post .= '</em>';
  548. }
  549. $definition .= '<td class="print">' . $fmt_pre
  550. . htmlspecialchars($col_alias) . $fmt_post . '</td>';
  551. $definition .= '<td class="print">' . htmlspecialchars($type) . '</td>';
  552. $definition .= '<td class="print">'
  553. . ($column['Null'] == '' || $column['Null'] === 'NO'
  554. ? __('No')
  555. : __('Yes'))
  556. . '</td>';
  557. $definition .= '<td class="print">'
  558. . htmlspecialchars($column['Default'] ?? '')
  559. . '</td>';
  560. return $definition;
  561. }
  562. }