Dia.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Classes to create relation schema in Dia format.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Dia;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Response;
  9. use XMLWriter;
  10. use function ob_end_clean;
  11. use function ob_get_clean;
  12. use function strlen;
  13. /**
  14. * This Class inherits the XMLwriter class and
  15. * helps in developing structure of DIA Schema Export
  16. *
  17. * @see https://www.php.net/manual/en/book.xmlwriter.php
  18. *
  19. * @access public
  20. */
  21. class Dia extends XMLWriter
  22. {
  23. /**
  24. * Upon instantiation This starts writing the Dia XML document
  25. *
  26. * @see XMLWriter::openMemory()
  27. * @see XMLWriter::setIndent()
  28. * @see XMLWriter::startDocument()
  29. */
  30. public function __construct()
  31. {
  32. $this->openMemory();
  33. /*
  34. * Set indenting using three spaces,
  35. * so output is formatted
  36. */
  37. $this->setIndent(true);
  38. $this->setIndentString(' ');
  39. /*
  40. * Create the XML document
  41. */
  42. $this->startDocument('1.0', 'UTF-8');
  43. }
  44. /**
  45. * Starts Dia Document
  46. *
  47. * dia document starts by first initializing dia:diagram tag
  48. * then dia:diagramdata contains all the attributes that needed
  49. * to define the document, then finally a Layer starts which
  50. * holds all the objects.
  51. *
  52. * @see XMLWriter::startElement()
  53. * @see XMLWriter::writeAttribute()
  54. * @see XMLWriter::writeRaw()
  55. *
  56. * @param string $paper the size of the paper/document
  57. * @param float $topMargin top margin of the paper/document in cm
  58. * @param float $bottomMargin bottom margin of the paper/document in cm
  59. * @param float $leftMargin left margin of the paper/document in cm
  60. * @param float $rightMargin right margin of the paper/document in cm
  61. * @param string $orientation orientation of the document, portrait or landscape
  62. *
  63. * @return void
  64. *
  65. * @access public
  66. */
  67. public function startDiaDoc(
  68. $paper,
  69. $topMargin,
  70. $bottomMargin,
  71. $leftMargin,
  72. $rightMargin,
  73. $orientation
  74. ) {
  75. if ($orientation === 'P') {
  76. $isPortrait = 'true';
  77. } else {
  78. $isPortrait = 'false';
  79. }
  80. $this->startElement('dia:diagram');
  81. $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
  82. $this->startElement('dia:diagramdata');
  83. $this->writeRaw(
  84. '<dia:attribute name="background">
  85. <dia:color val="#ffffff"/>
  86. </dia:attribute>
  87. <dia:attribute name="pagebreak">
  88. <dia:color val="#000099"/>
  89. </dia:attribute>
  90. <dia:attribute name="paper">
  91. <dia:composite type="paper">
  92. <dia:attribute name="name">
  93. <dia:string>#' . $paper . '#</dia:string>
  94. </dia:attribute>
  95. <dia:attribute name="tmargin">
  96. <dia:real val="' . $topMargin . '"/>
  97. </dia:attribute>
  98. <dia:attribute name="bmargin">
  99. <dia:real val="' . $bottomMargin . '"/>
  100. </dia:attribute>
  101. <dia:attribute name="lmargin">
  102. <dia:real val="' . $leftMargin . '"/>
  103. </dia:attribute>
  104. <dia:attribute name="rmargin">
  105. <dia:real val="' . $rightMargin . '"/>
  106. </dia:attribute>
  107. <dia:attribute name="is_portrait">
  108. <dia:boolean val="' . $isPortrait . '"/>
  109. </dia:attribute>
  110. <dia:attribute name="scaling">
  111. <dia:real val="1"/>
  112. </dia:attribute>
  113. <dia:attribute name="fitto">
  114. <dia:boolean val="false"/>
  115. </dia:attribute>
  116. </dia:composite>
  117. </dia:attribute>
  118. <dia:attribute name="grid">
  119. <dia:composite type="grid">
  120. <dia:attribute name="width_x">
  121. <dia:real val="1"/>
  122. </dia:attribute>
  123. <dia:attribute name="width_y">
  124. <dia:real val="1"/>
  125. </dia:attribute>
  126. <dia:attribute name="visible_x">
  127. <dia:int val="1"/>
  128. </dia:attribute>
  129. <dia:attribute name="visible_y">
  130. <dia:int val="1"/>
  131. </dia:attribute>
  132. <dia:composite type="color"/>
  133. </dia:composite>
  134. </dia:attribute>
  135. <dia:attribute name="color">
  136. <dia:color val="#d8e5e5"/>
  137. </dia:attribute>
  138. <dia:attribute name="guides">
  139. <dia:composite type="guides">
  140. <dia:attribute name="hguides"/>
  141. <dia:attribute name="vguides"/>
  142. </dia:composite>
  143. </dia:attribute>'
  144. );
  145. $this->endElement();
  146. $this->startElement('dia:layer');
  147. $this->writeAttribute('name', 'Background');
  148. $this->writeAttribute('visible', 'true');
  149. $this->writeAttribute('active', 'true');
  150. }
  151. /**
  152. * Ends Dia Document
  153. *
  154. * @see XMLWriter::endElement()
  155. * @see XMLWriter::endDocument()
  156. *
  157. * @return void
  158. *
  159. * @access public
  160. */
  161. public function endDiaDoc()
  162. {
  163. $this->endElement();
  164. $this->endDocument();
  165. }
  166. /**
  167. * Output Dia Document for download
  168. *
  169. * @see XMLWriter::flush()
  170. *
  171. * @param string $fileName name of the dia document
  172. *
  173. * @return void
  174. *
  175. * @access public
  176. */
  177. public function showOutput($fileName)
  178. {
  179. if (ob_get_clean()) {
  180. ob_end_clean();
  181. }
  182. $output = $this->flush();
  183. Response::getInstance()->disable();
  184. Core::downloadHeader(
  185. $fileName,
  186. 'application/x-dia-diagram',
  187. strlen($output)
  188. );
  189. print $output;
  190. }
  191. }