ExportOds.php 12 KB

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