Pdf.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * PhpMyAdmin\Plugins\Export\Helpers\Pdf class
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage PDF
  8. */
  9. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Export\Helpers;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Pdf as PdfLib;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\Transformations;
  15. use PhpMyAdmin\Util;
  16. use TCPDF_STATIC;
  17. /**
  18. * Adapted from a LGPL script by Philip Clarke
  19. *
  20. * @package PhpMyAdmin-Export
  21. * @subpackage PDF
  22. */
  23. class Pdf extends PdfLib
  24. {
  25. public $tablewidths;
  26. public $headerset;
  27. private $dataY;
  28. private $cellFontSize;
  29. private $titleFontSize;
  30. private $titleText;
  31. private $dbAlias;
  32. private $tableAlias;
  33. private $purpose;
  34. private $colTitles;
  35. private $results;
  36. private $colAlign;
  37. private $titleWidth;
  38. private $colFits;
  39. private $display_column;
  40. private $numFields;
  41. private $fields;
  42. private $sColWidth;
  43. private $currentDb;
  44. private $currentTable;
  45. private $aliases;
  46. /**
  47. * @var Relation
  48. */
  49. private $relation;
  50. /**
  51. * @var Transformations
  52. */
  53. private $transformations;
  54. /**
  55. * Constructs PDF and configures standard parameters.
  56. *
  57. * @param string $orientation page orientation
  58. * @param string $unit unit
  59. * @param string $format the format used for pages
  60. * @param boolean $unicode true means that the input text is unicode
  61. * @param string $encoding charset encoding; default is UTF-8.
  62. * @param boolean $diskcache if true reduce the RAM memory usage by caching
  63. * temporary data on filesystem (slower).
  64. * @param boolean $pdfa If TRUE set the document to PDF/A mode.
  65. *
  66. * @access public
  67. */
  68. public function __construct(
  69. $orientation = 'P',
  70. $unit = 'mm',
  71. $format = 'A4',
  72. $unicode = true,
  73. $encoding = 'UTF-8',
  74. $diskcache = false,
  75. $pdfa = false
  76. ) {
  77. parent::__construct(
  78. $orientation,
  79. $unit,
  80. $format,
  81. $unicode,
  82. $encoding,
  83. $diskcache,
  84. $pdfa
  85. );
  86. $this->relation = new Relation($GLOBALS['dbi']);
  87. $this->transformations = new Transformations();
  88. }
  89. /**
  90. * Add page if needed.
  91. *
  92. * @param float|int $h cell height. Default value: 0
  93. * @param mixed $y starting y position, leave empty for current
  94. * position
  95. * @param boolean $addpage if true add a page, otherwise only return
  96. * the true/false state
  97. *
  98. * @return boolean true in case of page break, false otherwise.
  99. */
  100. public function checkPageBreak($h = 0, $y = '', $addpage = true)
  101. {
  102. if (TCPDF_STATIC::empty_string($y)) {
  103. $y = $this->y;
  104. }
  105. $current_page = $this->page;
  106. if ((($y + $h) > $this->PageBreakTrigger)
  107. && (! $this->InFooter)
  108. && $this->AcceptPageBreak()
  109. ) {
  110. if ($addpage) {
  111. //Automatic page break
  112. $x = $this->x;
  113. $this->AddPage($this->CurOrientation);
  114. $this->y = $this->dataY;
  115. $oldpage = $this->page - 1;
  116. $this_page_orm = $this->pagedim[$this->page]['orm'];
  117. $old_page_orm = $this->pagedim[$oldpage]['orm'];
  118. $this_page_olm = $this->pagedim[$this->page]['olm'];
  119. $old_page_olm = $this->pagedim[$oldpage]['olm'];
  120. if ($this->rtl) {
  121. if ($this_page_orm != $old_page_orm) {
  122. $this->x = $x - ($this_page_orm - $old_page_orm);
  123. } else {
  124. $this->x = $x;
  125. }
  126. } else {
  127. if ($this_page_olm != $old_page_olm) {
  128. $this->x = $x + ($this_page_olm - $old_page_olm);
  129. } else {
  130. $this->x = $x;
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. // account for columns mode
  137. return $current_page != $this->page;
  138. }
  139. /**
  140. * This method is used to render the page header.
  141. *
  142. * @return void
  143. */
  144. // @codingStandardsIgnoreLine
  145. public function Header()
  146. {
  147. global $maxY;
  148. // We don't want automatic page breaks while generating header
  149. // as this can lead to infinite recursion as auto generated page
  150. // will want header as well causing another page break
  151. // FIXME: Better approach might be to try to compact the content
  152. $this->SetAutoPageBreak(false);
  153. // Check if header for this page already exists
  154. if (! isset($this->headerset[$this->page])) {
  155. $this->SetY($this->tMargin - ($this->FontSizePt / $this->k) * 5);
  156. $this->cellFontSize = $this->FontSizePt;
  157. $this->SetFont(
  158. PdfLib::PMA_PDF_FONT,
  159. '',
  160. ($this->titleFontSize
  161. ?: $this->FontSizePt)
  162. );
  163. $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C');
  164. $this->SetFont(PdfLib::PMA_PDF_FONT, '', $this->cellFontSize);
  165. $this->SetY($this->tMargin - ($this->FontSizePt / $this->k) * 2.5);
  166. $this->Cell(
  167. 0,
  168. $this->FontSizePt,
  169. __('Database:') . ' ' . $this->dbAlias . ', '
  170. . __('Table:') . ' ' . $this->tableAlias . ', '
  171. . __('Purpose:') . ' ' . $this->purpose,
  172. 0,
  173. 1,
  174. 'L'
  175. );
  176. $l = $this->lMargin;
  177. foreach ($this->colTitles as $col => $txt) {
  178. $this->SetXY($l, $this->tMargin);
  179. $this->MultiCell(
  180. $this->tablewidths[$col],
  181. $this->FontSizePt,
  182. $txt
  183. );
  184. $l += $this->tablewidths[$col];
  185. $maxY = $maxY < $this->GetY() ? $this->GetY() : $maxY;
  186. }
  187. $this->SetXY($this->lMargin, $this->tMargin);
  188. $this->SetFillColor(200, 200, 200);
  189. $l = $this->lMargin;
  190. foreach ($this->colTitles as $col => $txt) {
  191. $this->SetXY($l, $this->tMargin);
  192. $this->Cell(
  193. $this->tablewidths[$col],
  194. $maxY - $this->tMargin,
  195. '',
  196. 1,
  197. 0,
  198. 'L',
  199. 1
  200. );
  201. $this->SetXY($l, $this->tMargin);
  202. $this->MultiCell(
  203. $this->tablewidths[$col],
  204. $this->FontSizePt,
  205. $txt,
  206. 0,
  207. 'C'
  208. );
  209. $l += $this->tablewidths[$col];
  210. }
  211. $this->SetFillColor(255, 255, 255);
  212. // set headerset
  213. $this->headerset[$this->page] = 1;
  214. }
  215. $this->dataY = $maxY;
  216. $this->SetAutoPageBreak(true);
  217. }
  218. /**
  219. * Generate table
  220. *
  221. * @param int $lineheight Height of line
  222. *
  223. * @return void
  224. */
  225. public function morepagestable($lineheight = 8)
  226. {
  227. // some things to set and 'remember'
  228. $l = $this->lMargin;
  229. $startheight = $h = $this->dataY;
  230. $startpage = $currpage = $this->page;
  231. // calculate the whole width
  232. $fullwidth = 0;
  233. foreach ($this->tablewidths as $width) {
  234. $fullwidth += $width;
  235. }
  236. // Now let's start to write the table
  237. $row = 0;
  238. $tmpheight = [];
  239. $maxpage = $this->page;
  240. while ($data = $GLOBALS['dbi']->fetchRow($this->results)) {
  241. $this->page = $currpage;
  242. // write the horizontal borders
  243. $this->Line($l, $h, $fullwidth + $l, $h);
  244. // write the content and remember the height of the highest col
  245. foreach ($data as $col => $txt) {
  246. $this->page = $currpage;
  247. $this->SetXY($l, $h);
  248. if ($this->tablewidths[$col] > 0) {
  249. $this->MultiCell(
  250. $this->tablewidths[$col],
  251. $lineheight,
  252. $txt,
  253. 0,
  254. $this->colAlign[$col]
  255. );
  256. $l += $this->tablewidths[$col];
  257. }
  258. if (! isset($tmpheight[$row . '-' . $this->page])) {
  259. $tmpheight[$row . '-' . $this->page] = 0;
  260. }
  261. if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) {
  262. $tmpheight[$row . '-' . $this->page] = $this->GetY();
  263. }
  264. if ($this->page > $maxpage) {
  265. $maxpage = $this->page;
  266. }
  267. unset($data[$col]);
  268. }
  269. // get the height we were in the last used page
  270. $h = $tmpheight[$row . '-' . $maxpage];
  271. // set the "pointer" to the left margin
  272. $l = $this->lMargin;
  273. // set the $currpage to the last page
  274. $currpage = $maxpage;
  275. unset($data[$row]);
  276. $row++;
  277. }
  278. // draw the borders
  279. // we start adding a horizontal line on the last page
  280. $this->page = $maxpage;
  281. $this->Line($l, $h, $fullwidth + $l, $h);
  282. // now we start at the top of the document and walk down
  283. for ($i = $startpage; $i <= $maxpage; $i++) {
  284. $this->page = $i;
  285. $l = $this->lMargin;
  286. $t = $i == $startpage ? $startheight : $this->tMargin;
  287. $lh = $i == $maxpage ? $h : $this->h - $this->bMargin;
  288. $this->Line($l, $t, $l, $lh);
  289. foreach ($this->tablewidths as $width) {
  290. $l += $width;
  291. $this->Line($l, $t, $l, $lh);
  292. }
  293. }
  294. // set it to the last page, if not it'll cause some problems
  295. $this->page = $maxpage;
  296. }
  297. /**
  298. * Sets a set of attributes.
  299. *
  300. * @param array $attr array containing the attributes
  301. *
  302. * @return void
  303. */
  304. public function setAttributes(array $attr = [])
  305. {
  306. foreach ($attr as $key => $val) {
  307. $this->$key = $val;
  308. }
  309. }
  310. /**
  311. * Defines the top margin.
  312. * The method can be called before creating the first page.
  313. *
  314. * @param float $topMargin the margin
  315. *
  316. * @return void
  317. */
  318. public function setTopMargin($topMargin)
  319. {
  320. $this->tMargin = $topMargin;
  321. }
  322. /**
  323. * Prints triggers
  324. *
  325. * @param string $db database name
  326. * @param string $table table name
  327. *
  328. * @return void
  329. */
  330. public function getTriggers($db, $table)
  331. {
  332. $triggers = $GLOBALS['dbi']->getTriggers($db, $table);
  333. if ([] === $triggers) {
  334. return; //prevents printing blank trigger list for any table
  335. }
  336. unset($this->tablewidths);
  337. unset($this->colTitles);
  338. unset($this->titleWidth);
  339. unset($this->colFits);
  340. unset($this->display_column);
  341. unset($this->colAlign);
  342. /**
  343. * Making table heading
  344. * Keeping column width constant
  345. */
  346. $this->colTitles[0] = __('Name');
  347. $this->tablewidths[0] = 90;
  348. $this->colTitles[1] = __('Time');
  349. $this->tablewidths[1] = 80;
  350. $this->colTitles[2] = __('Event');
  351. $this->tablewidths[2] = 40;
  352. $this->colTitles[3] = __('Definition');
  353. $this->tablewidths[3] = 240;
  354. for ($columns_cnt = 0; $columns_cnt < 4; $columns_cnt++) {
  355. $this->colAlign[$columns_cnt] = 'L';
  356. $this->display_column[$columns_cnt] = true;
  357. }
  358. // Starting to fill table with required info
  359. $this->SetY($this->tMargin);
  360. $this->AddPage();
  361. $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9);
  362. $l = $this->lMargin;
  363. $startheight = $h = $this->dataY;
  364. $startpage = $currpage = $this->page;
  365. // calculate the whole width
  366. $fullwidth = 0;
  367. foreach ($this->tablewidths as $width) {
  368. $fullwidth += $width;
  369. }
  370. $row = 0;
  371. $tmpheight = [];
  372. $maxpage = $this->page;
  373. $data = [];
  374. foreach ($triggers as $trigger) {
  375. $data[] = $trigger['name'];
  376. $data[] = $trigger['action_timing'];
  377. $data[] = $trigger['event_manipulation'];
  378. $data[] = $trigger['definition'];
  379. $this->page = $currpage;
  380. // write the horizontal borders
  381. $this->Line($l, $h, $fullwidth + $l, $h);
  382. // write the content and remember the height of the highest col
  383. foreach ($data as $col => $txt) {
  384. $this->page = $currpage;
  385. $this->SetXY($l, $h);
  386. if ($this->tablewidths[$col] > 0) {
  387. $this->MultiCell(
  388. $this->tablewidths[$col],
  389. $this->FontSizePt,
  390. $txt,
  391. 0,
  392. $this->colAlign[$col]
  393. );
  394. $l += $this->tablewidths[$col];
  395. }
  396. if (! isset($tmpheight[$row . '-' . $this->page])) {
  397. $tmpheight[$row . '-' . $this->page] = 0;
  398. }
  399. if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) {
  400. $tmpheight[$row . '-' . $this->page] = $this->GetY();
  401. }
  402. if ($this->page > $maxpage) {
  403. $maxpage = $this->page;
  404. }
  405. }
  406. // get the height we were in the last used page
  407. $h = $tmpheight[$row . '-' . $maxpage];
  408. // set the "pointer" to the left margin
  409. $l = $this->lMargin;
  410. // set the $currpage to the last page
  411. $currpage = $maxpage;
  412. unset($data);
  413. $row++;
  414. }
  415. // draw the borders
  416. // we start adding a horizontal line on the last page
  417. $this->page = $maxpage;
  418. $this->Line($l, $h, $fullwidth + $l, $h);
  419. // now we start at the top of the document and walk down
  420. for ($i = $startpage; $i <= $maxpage; $i++) {
  421. $this->page = $i;
  422. $l = $this->lMargin;
  423. $t = $i == $startpage ? $startheight : $this->tMargin;
  424. $lh = $i == $maxpage ? $h : $this->h - $this->bMargin;
  425. $this->Line($l, $t, $l, $lh);
  426. foreach ($this->tablewidths as $width) {
  427. $l += $width;
  428. $this->Line($l, $t, $l, $lh);
  429. }
  430. }
  431. // set it to the last page, if not it'll cause some problems
  432. $this->page = $maxpage;
  433. }
  434. /**
  435. * Print $table's CREATE definition
  436. *
  437. * @param string $db the database name
  438. * @param string $table the table name
  439. * @param bool $do_relation whether to include relation comments
  440. * @param bool $do_comments whether to include the pmadb-style column
  441. * comments as comments in the structure;
  442. * this is deprecated but the parameter is
  443. * left here because export.php calls
  444. * PMA_exportStructure() also for other
  445. * export types which use this parameter
  446. * @param bool $do_mime whether to include mime comments
  447. * @param bool $view whether we're handling a view
  448. * @param array $aliases aliases of db/table/columns
  449. *
  450. * @return void
  451. */
  452. public function getTableDef(
  453. $db,
  454. $table,
  455. $do_relation,
  456. $do_comments,
  457. $do_mime,
  458. $view = false,
  459. array $aliases = []
  460. ) {
  461. // set $cfgRelation here, because there is a chance that it's modified
  462. // since the class initialization
  463. global $cfgRelation;
  464. unset($this->tablewidths);
  465. unset($this->colTitles);
  466. unset($this->titleWidth);
  467. unset($this->colFits);
  468. unset($this->display_column);
  469. unset($this->colAlign);
  470. /**
  471. * Gets fields properties
  472. */
  473. $GLOBALS['dbi']->selectDb($db);
  474. /**
  475. * All these three checks do_relation, do_comment and do_mime is
  476. * not required. As presently all are set true by default.
  477. * But when, methods to take user input will be developed,
  478. * it will be of use
  479. */
  480. // Check if we can use Relations
  481. if ($do_relation) {
  482. // Find which tables are related with the current one and write it in
  483. // an array
  484. $res_rel = $this->relation->getForeigners($db, $table);
  485. $have_rel = ! empty($res_rel);
  486. } else {
  487. $have_rel = false;
  488. } // end if
  489. //column count and table heading
  490. $this->colTitles[0] = __('Column');
  491. $this->tablewidths[0] = 90;
  492. $this->colTitles[1] = __('Type');
  493. $this->tablewidths[1] = 80;
  494. $this->colTitles[2] = __('Null');
  495. $this->tablewidths[2] = 40;
  496. $this->colTitles[3] = __('Default');
  497. $this->tablewidths[3] = 120;
  498. for ($columns_cnt = 0; $columns_cnt < 4; $columns_cnt++) {
  499. $this->colAlign[$columns_cnt] = 'L';
  500. $this->display_column[$columns_cnt] = true;
  501. }
  502. if ($do_relation && $have_rel) {
  503. $this->colTitles[$columns_cnt] = __('Links to');
  504. $this->display_column[$columns_cnt] = true;
  505. $this->colAlign[$columns_cnt] = 'L';
  506. $this->tablewidths[$columns_cnt] = 120;
  507. $columns_cnt++;
  508. }
  509. if ($do_comments /*&& $cfgRelation['commwork']*/) {
  510. $this->colTitles[$columns_cnt] = __('Comments');
  511. $this->display_column[$columns_cnt] = true;
  512. $this->colAlign[$columns_cnt] = 'L';
  513. $this->tablewidths[$columns_cnt] = 120;
  514. $columns_cnt++;
  515. }
  516. if ($do_mime && $cfgRelation['mimework']) {
  517. $this->colTitles[$columns_cnt] = __('Media (MIME) type');
  518. $this->display_column[$columns_cnt] = true;
  519. $this->colAlign[$columns_cnt] = 'L';
  520. $this->tablewidths[$columns_cnt] = 120;
  521. $columns_cnt++;
  522. }
  523. // Starting to fill table with required info
  524. $this->SetY($this->tMargin);
  525. $this->AddPage();
  526. $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9);
  527. // Now let's start to write the table structure
  528. if ($do_comments) {
  529. $comments = $this->relation->getComments($db, $table);
  530. }
  531. if ($do_mime && $cfgRelation['mimework']) {
  532. $mime_map = $this->transformations->getMime($db, $table, true);
  533. }
  534. $columns = $GLOBALS['dbi']->getColumns($db, $table);
  535. // some things to set and 'remember'
  536. $l = $this->lMargin;
  537. $startheight = $h = $this->dataY;
  538. $startpage = $currpage = $this->page;
  539. // calculate the whole width
  540. $fullwidth = 0;
  541. foreach ($this->tablewidths as $width) {
  542. $fullwidth += $width;
  543. }
  544. $row = 0;
  545. $tmpheight = [];
  546. $maxpage = $this->page;
  547. $data = [];
  548. // fun begin
  549. foreach ($columns as $column) {
  550. $extracted_columnspec
  551. = Util::extractColumnSpec($column['Type']);
  552. $type = $extracted_columnspec['print_type'];
  553. if (empty($type)) {
  554. $type = ' ';
  555. }
  556. if (! isset($column['Default'])) {
  557. if ($column['Null'] != 'NO') {
  558. $column['Default'] = 'NULL';
  559. }
  560. }
  561. $data[] = $column['Field'];
  562. $data[] = $type;
  563. $data[] = $column['Null'] == '' || $column['Null'] == 'NO'
  564. ? 'No'
  565. : 'Yes';
  566. $data[] = isset($column['Default']) ? $column['Default'] : '';
  567. $field_name = $column['Field'];
  568. if ($do_relation && $have_rel) {
  569. $data[] = isset($res_rel[$field_name])
  570. ? $res_rel[$field_name]['foreign_table']
  571. . ' (' . $res_rel[$field_name]['foreign_field']
  572. . ')'
  573. : '';
  574. }
  575. if ($do_comments) {
  576. $data[] = isset($comments[$field_name])
  577. ? $comments[$field_name]
  578. : '';
  579. }
  580. if ($do_mime) {
  581. $data[] = isset($mime_map[$field_name])
  582. ? $mime_map[$field_name]['mimetype']
  583. : '';
  584. }
  585. $this->page = $currpage;
  586. // write the horizontal borders
  587. $this->Line($l, $h, $fullwidth + $l, $h);
  588. // write the content and remember the height of the highest col
  589. foreach ($data as $col => $txt) {
  590. $this->page = $currpage;
  591. $this->SetXY($l, $h);
  592. if ($this->tablewidths[$col] > 0) {
  593. $this->MultiCell(
  594. $this->tablewidths[$col],
  595. $this->FontSizePt,
  596. $txt,
  597. 0,
  598. $this->colAlign[$col]
  599. );
  600. $l += $this->tablewidths[$col];
  601. }
  602. if (! isset($tmpheight[$row . '-' . $this->page])) {
  603. $tmpheight[$row . '-' . $this->page] = 0;
  604. }
  605. if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) {
  606. $tmpheight[$row . '-' . $this->page] = $this->GetY();
  607. }
  608. if ($this->page > $maxpage) {
  609. $maxpage = $this->page;
  610. }
  611. }
  612. // get the height we were in the last used page
  613. $h = $tmpheight[$row . '-' . $maxpage];
  614. // set the "pointer" to the left margin
  615. $l = $this->lMargin;
  616. // set the $currpage to the last page
  617. $currpage = $maxpage;
  618. unset($data);
  619. $row++;
  620. }
  621. // draw the borders
  622. // we start adding a horizontal line on the last page
  623. $this->page = $maxpage;
  624. $this->Line($l, $h, $fullwidth + $l, $h);
  625. // now we start at the top of the document and walk down
  626. for ($i = $startpage; $i <= $maxpage; $i++) {
  627. $this->page = $i;
  628. $l = $this->lMargin;
  629. $t = $i == $startpage ? $startheight : $this->tMargin;
  630. $lh = $i == $maxpage ? $h : $this->h - $this->bMargin;
  631. $this->Line($l, $t, $l, $lh);
  632. foreach ($this->tablewidths as $width) {
  633. $l += $width;
  634. $this->Line($l, $t, $l, $lh);
  635. }
  636. }
  637. // set it to the last page, if not it'll cause some problems
  638. $this->page = $maxpage;
  639. }
  640. /**
  641. * MySQL report
  642. *
  643. * @param string $query Query to execute
  644. *
  645. * @return void
  646. */
  647. public function mysqlReport($query)
  648. {
  649. unset($this->tablewidths);
  650. unset($this->colTitles);
  651. unset($this->titleWidth);
  652. unset($this->colFits);
  653. unset($this->display_column);
  654. unset($this->colAlign);
  655. /**
  656. * Pass 1 for column widths
  657. */
  658. $this->results = $GLOBALS['dbi']->query(
  659. $query,
  660. DatabaseInterface::CONNECT_USER,
  661. DatabaseInterface::QUERY_UNBUFFERED
  662. );
  663. $this->numFields = $GLOBALS['dbi']->numFields($this->results);
  664. $this->fields = $GLOBALS['dbi']->getFieldsMeta($this->results);
  665. // sColWidth = starting col width (an average size width)
  666. $availableWidth = $this->w - $this->lMargin - $this->rMargin;
  667. $this->sColWidth = $availableWidth / $this->numFields;
  668. $totalTitleWidth = 0;
  669. // loop through results header and set initial
  670. // col widths/ titles/ alignment
  671. // if a col title is less than the starting col width,
  672. // reduce that column size
  673. $colFits = [];
  674. $titleWidth = [];
  675. for ($i = 0; $i < $this->numFields; $i++) {
  676. $col_as = $this->fields[$i]->name;
  677. $db = $this->currentDb;
  678. $table = $this->currentTable;
  679. if (! empty($this->aliases[$db]['tables'][$table]['columns'][$col_as])) {
  680. $col_as = $this->aliases[$db]['tables'][$table]['columns'][$col_as];
  681. }
  682. $stringWidth = $this->GetStringWidth($col_as) + 6;
  683. // save the real title's width
  684. $titleWidth[$i] = $stringWidth;
  685. $totalTitleWidth += $stringWidth;
  686. // set any column titles less than the start width to
  687. // the column title width
  688. if ($stringWidth < $this->sColWidth) {
  689. $colFits[$i] = $stringWidth;
  690. }
  691. $this->colTitles[$i] = $col_as;
  692. $this->display_column[$i] = true;
  693. switch ($this->fields[$i]->type) {
  694. case 'int':
  695. $this->colAlign[$i] = 'R';
  696. break;
  697. case 'blob':
  698. case 'tinyblob':
  699. case 'mediumblob':
  700. case 'longblob':
  701. /**
  702. * @todo do not deactivate completely the display
  703. * but show the field's name and [BLOB]
  704. */
  705. if (false !== stripos($this->fields[$i]->flags, 'BINARY')) {
  706. $this->display_column[$i] = false;
  707. unset($this->colTitles[$i]);
  708. }
  709. $this->colAlign[$i] = 'L';
  710. break;
  711. default:
  712. $this->colAlign[$i] = 'L';
  713. }
  714. }
  715. // title width verification
  716. if ($totalTitleWidth > $availableWidth) {
  717. $adjustingMode = true;
  718. } else {
  719. $adjustingMode = false;
  720. // we have enough space for all the titles at their
  721. // original width so use the true title's width
  722. foreach ($titleWidth as $key => $val) {
  723. $colFits[$key] = $val;
  724. }
  725. }
  726. // loop through the data; any column whose contents
  727. // is greater than the column size is resized
  728. /**
  729. * @todo force here a LIMIT to avoid reading all rows
  730. */
  731. while ($row = $GLOBALS['dbi']->fetchRow($this->results)) {
  732. foreach ($colFits as $key => $val) {
  733. $stringWidth = $this->GetStringWidth($row[$key]) + 6;
  734. if ($adjustingMode && ($stringWidth > $this->sColWidth)) {
  735. // any column whose data's width is bigger than
  736. // the start width is now discarded
  737. unset($colFits[$key]);
  738. } else {
  739. // if data's width is bigger than the current column width,
  740. // enlarge the column (but avoid enlarging it if the
  741. // data's width is very big)
  742. if ($stringWidth > $val
  743. && $stringWidth < ($this->sColWidth * 3)
  744. ) {
  745. $colFits[$key] = $stringWidth;
  746. }
  747. }
  748. }
  749. }
  750. $totAlreadyFitted = 0;
  751. foreach ($colFits as $key => $val) {
  752. // set fitted columns to smallest size
  753. $this->tablewidths[$key] = $val;
  754. // to work out how much (if any) space has been freed up
  755. $totAlreadyFitted += $val;
  756. }
  757. if ($adjustingMode) {
  758. $surplus = (count($colFits) * $this->sColWidth) - $totAlreadyFitted;
  759. $surplusToAdd = $surplus / ($this->numFields - count($colFits));
  760. } else {
  761. $surplusToAdd = 0;
  762. }
  763. for ($i = 0; $i < $this->numFields; $i++) {
  764. if (! array_key_exists($i, $colFits)) {
  765. $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd;
  766. }
  767. if ($this->display_column[$i] == false) {
  768. $this->tablewidths[$i] = 0;
  769. }
  770. }
  771. ksort($this->tablewidths);
  772. $GLOBALS['dbi']->freeResult($this->results);
  773. // Pass 2
  774. $this->results = $GLOBALS['dbi']->query(
  775. $query,
  776. DatabaseInterface::CONNECT_USER,
  777. DatabaseInterface::QUERY_UNBUFFERED
  778. );
  779. $this->SetY($this->tMargin);
  780. $this->AddPage();
  781. $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9);
  782. $this->morepagestable($this->FontSizePt);
  783. $GLOBALS['dbi']->freeResult($this->results);
  784. } // end of mysqlReport function
  785. } // end of Pdf class