ExportRelationSchema.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\ExportRelationSchema class which is
  4. * inherited by all schema classes.
  5. */
  6. declare(strict_types=1);
  7. namespace PhpMyAdmin\Plugins\Schema;
  8. use PhpMyAdmin\Relation;
  9. use PhpMyAdmin\Url;
  10. use PhpMyAdmin\Util;
  11. use function htmlspecialchars;
  12. use function rawurldecode;
  13. /**
  14. * This class is inherited by all schema classes
  15. * It contains those methods which are common in them
  16. * it works like factory pattern
  17. */
  18. class ExportRelationSchema
  19. {
  20. /** @var string */
  21. protected $db;
  22. /** @var Dia\Dia|Eps\Eps|Pdf\Pdf|Svg\Svg|null */
  23. protected $diagram;
  24. /** @var bool */
  25. protected $showColor;
  26. /** @var bool */
  27. protected $tableDimension;
  28. /** @var bool */
  29. protected $sameWide;
  30. /** @var bool */
  31. protected $showKeys;
  32. /** @var string */
  33. protected $orientation;
  34. /** @var string */
  35. protected $paper;
  36. /** @var int */
  37. protected $pageNumber;
  38. /** @var bool */
  39. protected $offline;
  40. /** @var Relation */
  41. protected $relation;
  42. /**
  43. * @param string $db database name
  44. * @param Pdf\Pdf|Svg\Svg|Eps\Eps|Dia\Dia|Pdf\Pdf|null $diagram schema diagram
  45. */
  46. public function __construct($db, $diagram)
  47. {
  48. global $dbi;
  49. $this->db = $db;
  50. $this->diagram = $diagram;
  51. $this->setPageNumber((int) $_REQUEST['page_number']);
  52. $this->setOffline(isset($_REQUEST['offline_export']));
  53. $this->relation = new Relation($dbi);
  54. }
  55. /**
  56. * Set Page Number
  57. *
  58. * @param int $value Page Number of the document to be created
  59. */
  60. public function setPageNumber(int $value): void
  61. {
  62. $this->pageNumber = $value;
  63. }
  64. /**
  65. * Returns the schema page number
  66. *
  67. * @return int schema page number
  68. */
  69. public function getPageNumber()
  70. {
  71. return $this->pageNumber;
  72. }
  73. /**
  74. * Sets showColor
  75. *
  76. * @param bool $value whether to show colors
  77. */
  78. public function setShowColor(bool $value): void
  79. {
  80. $this->showColor = $value;
  81. }
  82. /**
  83. * Returns whether to show colors
  84. *
  85. * @return bool whether to show colors
  86. */
  87. public function isShowColor()
  88. {
  89. return $this->showColor;
  90. }
  91. /**
  92. * Set Table Dimension
  93. *
  94. * @param bool $value show table co-ordinates or not
  95. */
  96. public function setTableDimension(bool $value): void
  97. {
  98. $this->tableDimension = $value;
  99. }
  100. /**
  101. * Returns whether to show table dimensions
  102. *
  103. * @return bool whether to show table dimensions
  104. */
  105. public function isTableDimension()
  106. {
  107. return $this->tableDimension;
  108. }
  109. /**
  110. * Set same width of All Tables
  111. *
  112. * @param bool $value set same width of all tables or not
  113. */
  114. public function setAllTablesSameWidth(bool $value): void
  115. {
  116. $this->sameWide = $value;
  117. }
  118. /**
  119. * Returns whether to use same width for all tables or not
  120. *
  121. * @return bool whether to use same width for all tables or not
  122. */
  123. public function isAllTableSameWidth()
  124. {
  125. return $this->sameWide;
  126. }
  127. /**
  128. * Set Show only keys
  129. *
  130. * @param bool $value show only keys or not
  131. *
  132. * @access public
  133. */
  134. public function setShowKeys(bool $value): void
  135. {
  136. $this->showKeys = $value;
  137. }
  138. /**
  139. * Returns whether to show keys
  140. *
  141. * @return bool whether to show keys
  142. */
  143. public function isShowKeys()
  144. {
  145. return $this->showKeys;
  146. }
  147. /**
  148. * Set Orientation
  149. *
  150. * @param string $value Orientation will be portrait or landscape
  151. *
  152. * @access public
  153. */
  154. public function setOrientation(string $value): void
  155. {
  156. $this->orientation = $value === 'P' ? 'P' : 'L';
  157. }
  158. /**
  159. * Returns orientation
  160. *
  161. * @return string orientation
  162. */
  163. public function getOrientation()
  164. {
  165. return $this->orientation;
  166. }
  167. /**
  168. * Set type of paper
  169. *
  170. * @param string $value paper type can be A4 etc
  171. *
  172. * @access public
  173. */
  174. public function setPaper(string $value): void
  175. {
  176. $this->paper = $value;
  177. }
  178. /**
  179. * Returns the paper size
  180. *
  181. * @return string paper size
  182. */
  183. public function getPaper()
  184. {
  185. return $this->paper;
  186. }
  187. /**
  188. * Set whether the document is generated from client side DB
  189. *
  190. * @param bool $value offline or not
  191. *
  192. * @access public
  193. */
  194. public function setOffline(bool $value): void
  195. {
  196. $this->offline = $value;
  197. }
  198. /**
  199. * Returns whether the client side database is used
  200. *
  201. * @return bool
  202. *
  203. * @access public
  204. */
  205. public function isOffline()
  206. {
  207. return $this->offline;
  208. }
  209. /**
  210. * Get the table names from the request
  211. *
  212. * @return string[] an array of table names
  213. */
  214. protected function getTablesFromRequest(): array
  215. {
  216. $tables = [];
  217. if (isset($_POST['t_tbl'])) {
  218. foreach ($_POST['t_tbl'] as $table) {
  219. $tables[] = rawurldecode($table);
  220. }
  221. }
  222. return $tables;
  223. }
  224. /**
  225. * Returns the file name
  226. *
  227. * @param string $extension file extension
  228. *
  229. * @return string file name
  230. */
  231. protected function getFileName($extension): string
  232. {
  233. global $dbi;
  234. $filename = $this->db . $extension;
  235. // Get the name of this page to use as filename
  236. if ($this->pageNumber != -1 && ! $this->offline) {
  237. $_name_sql = 'SELECT page_descr FROM '
  238. . Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  239. . Util::backquote($GLOBALS['cfgRelation']['pdf_pages'])
  240. . ' WHERE page_nr = ' . $this->pageNumber;
  241. $_name_rs = $this->relation->queryAsControlUser($_name_sql);
  242. $_name_row = $dbi->fetchRow($_name_rs);
  243. $filename = $_name_row[0] . $extension;
  244. }
  245. return $filename;
  246. }
  247. /**
  248. * Displays an error message
  249. *
  250. * @param int $pageNumber ID of the chosen page
  251. * @param string $type Schema Type
  252. * @param string $error_message The error message
  253. *
  254. * @return void
  255. *
  256. * @access public
  257. */
  258. public static function dieSchema($pageNumber, $type = '', $error_message = '')
  259. {
  260. echo '<p><strong>' , __('SCHEMA ERROR: ') , $type , '</strong></p>' , "\n";
  261. if (! empty($error_message)) {
  262. $error_message = htmlspecialchars($error_message);
  263. }
  264. echo '<p>' , "\n";
  265. echo ' ' , $error_message , "\n";
  266. echo '</p>' , "\n";
  267. echo '<a href="';
  268. echo Url::getFromRoute('/database/designer', [
  269. 'db' => $GLOBALS['db'],
  270. 'server' => $GLOBALS['server'],
  271. 'page' => $pageNumber,
  272. ]);
  273. echo '">' . __('Back') . '</a>';
  274. echo "\n";
  275. exit;
  276. }
  277. }