ExportPdf.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Produce a PDF report (export) from a query
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\Plugins\Export\Helpers\Pdf;
  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\RadioPropertyItem;
  12. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  13. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  14. use function class_exists;
  15. // phpcs:disable PSR1.Files.SideEffects
  16. /**
  17. * Skip the plugin if TCPDF is not available.
  18. */
  19. if (! class_exists('TCPDF')) {
  20. $GLOBALS['skip_import'] = true;
  21. return;
  22. }
  23. // phpcs:enable
  24. /**
  25. * Handles the export for the PDF class
  26. */
  27. class ExportPdf extends ExportPlugin
  28. {
  29. /**
  30. * PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  31. *
  32. * @var Pdf
  33. */
  34. private $pdf;
  35. /**
  36. * PDF Report Title
  37. *
  38. * @var string
  39. */
  40. private $pdfReportTitle;
  41. public function __construct()
  42. {
  43. parent::__construct();
  44. // initialize the specific export PDF variables
  45. $this->initSpecificVariables();
  46. $this->setProperties();
  47. }
  48. /**
  49. * Initialize the local variables that are used for export PDF
  50. *
  51. * @return void
  52. */
  53. protected function initSpecificVariables()
  54. {
  55. if (! empty($_POST['pdf_report_title'])) {
  56. $this->setPdfReportTitle($_POST['pdf_report_title']);
  57. }
  58. $this->setPdf(new Pdf('L', 'pt', 'A3'));
  59. }
  60. /**
  61. * Sets the export PDF properties
  62. *
  63. * @return void
  64. */
  65. protected function setProperties()
  66. {
  67. $exportPluginProperties = new ExportPluginProperties();
  68. $exportPluginProperties->setText('PDF');
  69. $exportPluginProperties->setExtension('pdf');
  70. $exportPluginProperties->setMimeType('application/pdf');
  71. $exportPluginProperties->setForceFile(true);
  72. $exportPluginProperties->setOptionsText(__('Options'));
  73. // create the root group that will be the options field for
  74. // $exportPluginProperties
  75. // this will be shown as "Format specific options"
  76. $exportSpecificOptions = new OptionsPropertyRootGroup(
  77. 'Format Specific Options'
  78. );
  79. // general options main group
  80. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  81. // create primary items and add them to the group
  82. $leaf = new TextPropertyItem(
  83. 'report_title',
  84. __('Report title:')
  85. );
  86. $generalOptions->addProperty($leaf);
  87. // add the group to the root group
  88. $exportSpecificOptions->addProperty($generalOptions);
  89. // what to dump (structure/data/both) main group
  90. $dumpWhat = new OptionsPropertyMainGroup(
  91. 'dump_what',
  92. __('Dump table')
  93. );
  94. $leaf = new RadioPropertyItem('structure_or_data');
  95. $leaf->setValues(
  96. [
  97. 'structure' => __('structure'),
  98. 'data' => __('data'),
  99. 'structure_and_data' => __('structure and data'),
  100. ]
  101. );
  102. $dumpWhat->addProperty($leaf);
  103. // add the group to the root group
  104. $exportSpecificOptions->addProperty($dumpWhat);
  105. // set the options for the export plugin property item
  106. $exportPluginProperties->setOptions($exportSpecificOptions);
  107. $this->properties = $exportPluginProperties;
  108. }
  109. /**
  110. * Outputs export header
  111. *
  112. * @return bool Whether it succeeded
  113. */
  114. public function exportHeader()
  115. {
  116. $pdf_report_title = $this->getPdfReportTitle();
  117. $pdf = $this->getPdf();
  118. $pdf->Open();
  119. $attr = [
  120. 'titleFontSize' => 18,
  121. 'titleText' => $pdf_report_title,
  122. ];
  123. $pdf->setAttributes($attr);
  124. $pdf->setTopMargin(30);
  125. return true;
  126. }
  127. /**
  128. * Outputs export footer
  129. *
  130. * @return bool Whether it succeeded
  131. */
  132. public function exportFooter()
  133. {
  134. $pdf = $this->getPdf();
  135. // instead of $pdf->Output():
  136. return $this->export->outputHandler($pdf->getPDFData());
  137. }
  138. /**
  139. * Outputs database header
  140. *
  141. * @param string $db Database name
  142. * @param string $db_alias Aliases of db
  143. *
  144. * @return bool Whether it succeeded
  145. */
  146. public function exportDBHeader($db, $db_alias = '')
  147. {
  148. return true;
  149. }
  150. /**
  151. * Outputs database footer
  152. *
  153. * @param string $db Database name
  154. *
  155. * @return bool Whether it succeeded
  156. */
  157. public function exportDBFooter($db)
  158. {
  159. return true;
  160. }
  161. /**
  162. * Outputs CREATE DATABASE statement
  163. *
  164. * @param string $db Database name
  165. * @param string $export_type 'server', 'database', 'table'
  166. * @param string $db_alias Aliases of db
  167. *
  168. * @return bool Whether it succeeded
  169. */
  170. public function exportDBCreate($db, $export_type, $db_alias = '')
  171. {
  172. return true;
  173. }
  174. /**
  175. * Outputs the content of a table in NHibernate format
  176. *
  177. * @param string $db database name
  178. * @param string $table table name
  179. * @param string $crlf the end of line sequence
  180. * @param string $error_url the url to go back in case of error
  181. * @param string $sql_query SQL query for obtaining data
  182. * @param array $aliases Aliases of db/table/columns
  183. *
  184. * @return bool Whether it succeeded
  185. */
  186. public function exportData(
  187. $db,
  188. $table,
  189. $crlf,
  190. $error_url,
  191. $sql_query,
  192. array $aliases = []
  193. ) {
  194. $db_alias = $db;
  195. $table_alias = $table;
  196. $this->initAlias($aliases, $db_alias, $table_alias);
  197. $pdf = $this->getPdf();
  198. $attr = [
  199. 'currentDb' => $db,
  200. 'currentTable' => $table,
  201. 'dbAlias' => $db_alias,
  202. 'tableAlias' => $table_alias,
  203. 'aliases' => $aliases,
  204. 'purpose' => __('Dumping data'),
  205. ];
  206. $pdf->setAttributes($attr);
  207. $pdf->mysqlReport($sql_query);
  208. return true;
  209. }
  210. /**
  211. * Outputs result of raw query in PDF format
  212. *
  213. * @param string $err_url the url to go back in case of error
  214. * @param string $sql_query the rawquery to output
  215. * @param string $crlf the end of line sequence
  216. *
  217. * @return bool if succeeded
  218. */
  219. public function exportRawQuery(string $err_url, string $sql_query, string $crlf): bool
  220. {
  221. $pdf = $this->getPdf();
  222. $attr = [
  223. 'dbAlias' => '----',
  224. 'tableAlias' => '----',
  225. 'purpose' => __('Query result data'),
  226. ];
  227. $pdf->setAttributes($attr);
  228. $pdf->mysqlReport($sql_query);
  229. return true;
  230. }
  231. /**
  232. * Outputs table structure
  233. *
  234. * @param string $db database name
  235. * @param string $table table name
  236. * @param string $crlf the end of line sequence
  237. * @param string $error_url the url to go back in case of error
  238. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  239. * 'stand_in'
  240. * @param string $export_type 'server', 'database', 'table'
  241. * @param bool $do_relation whether to include relation comments
  242. * @param bool $do_comments whether to include the pmadb-style column
  243. * comments as comments in the structure;
  244. * this is deprecated but the parameter is
  245. * left here because /export calls
  246. * PMA_exportStructure() also for other
  247. * export types which use this parameter
  248. * @param bool $do_mime whether to include mime comments
  249. * @param bool $dates whether to include creation/update/check dates
  250. * @param array $aliases aliases for db/table/columns
  251. *
  252. * @return bool Whether it succeeded
  253. */
  254. public function exportStructure(
  255. $db,
  256. $table,
  257. $crlf,
  258. $error_url,
  259. $export_mode,
  260. $export_type,
  261. $do_relation = false,
  262. $do_comments = false,
  263. $do_mime = false,
  264. $dates = false,
  265. array $aliases = []
  266. ) {
  267. $db_alias = $db;
  268. $table_alias = $table;
  269. $purpose = null;
  270. $this->initAlias($aliases, $db_alias, $table_alias);
  271. $pdf = $this->getPdf();
  272. // getting purpose to show at top
  273. switch ($export_mode) {
  274. case 'create_table':
  275. $purpose = __('Table structure');
  276. break;
  277. case 'triggers':
  278. $purpose = __('Triggers');
  279. break;
  280. case 'create_view':
  281. $purpose = __('View structure');
  282. break;
  283. case 'stand_in':
  284. $purpose = __('Stand in');
  285. }
  286. $attr = [
  287. 'currentDb' => $db,
  288. 'currentTable' => $table,
  289. 'dbAlias' => $db_alias,
  290. 'tableAlias' => $table_alias,
  291. 'aliases' => $aliases,
  292. 'purpose' => $purpose,
  293. ];
  294. $pdf->setAttributes($attr);
  295. /**
  296. * comment display set true as presently in pdf
  297. * format, no option is present to take user input.
  298. */
  299. $do_comments = true;
  300. switch ($export_mode) {
  301. case 'create_table':
  302. $pdf->getTableDef(
  303. $db,
  304. $table,
  305. $do_relation,
  306. $do_comments,
  307. $do_mime,
  308. false,
  309. $aliases
  310. );
  311. break;
  312. case 'triggers':
  313. $pdf->getTriggers($db, $table);
  314. break;
  315. case 'create_view':
  316. $pdf->getTableDef(
  317. $db,
  318. $table,
  319. $do_relation,
  320. $do_comments,
  321. $do_mime,
  322. false,
  323. $aliases
  324. );
  325. break;
  326. case 'stand_in':
  327. /* export a stand-in definition to resolve view dependencies
  328. * Yet to develop this function
  329. * $pdf->getTableDefStandIn($db, $table, $crlf);
  330. */
  331. }
  332. return true;
  333. }
  334. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  335. /**
  336. * Gets the PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  337. *
  338. * @return Pdf
  339. */
  340. private function getPdf()
  341. {
  342. return $this->pdf;
  343. }
  344. /**
  345. * Instantiates the PhpMyAdmin\Plugins\Export\Helpers\Pdf class
  346. *
  347. * @param Pdf $pdf The instance
  348. *
  349. * @return void
  350. */
  351. private function setPdf($pdf)
  352. {
  353. $this->pdf = $pdf;
  354. }
  355. /**
  356. * Gets the PDF report title
  357. *
  358. * @return string
  359. */
  360. private function getPdfReportTitle()
  361. {
  362. return $this->pdfReportTitle;
  363. }
  364. /**
  365. * Sets the PDF report title
  366. *
  367. * @param string $pdfReportTitle PDF report title
  368. *
  369. * @return void
  370. */
  371. private function setPdfReportTitle($pdfReportTitle)
  372. {
  373. $this->pdfReportTitle = $pdfReportTitle;
  374. }
  375. }