TemplateModel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Export;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Util;
  6. use function sprintf;
  7. final class TemplateModel
  8. {
  9. /** @var DatabaseInterface */
  10. private $dbi;
  11. public function __construct(DatabaseInterface $dbi)
  12. {
  13. $this->dbi = $dbi;
  14. }
  15. /** @return bool|string */
  16. public function create(string $db, string $table, Template $template)
  17. {
  18. $query = sprintf(
  19. 'INSERT INTO %s.%s (`username`, `export_type`, `template_name`, `template_data`)'
  20. . ' VALUES (\'%s\', \'%s\', \'%s\', \'%s\');',
  21. Util::backquote($db),
  22. Util::backquote($table),
  23. $this->dbi->escapeString($template->getUsername()),
  24. $this->dbi->escapeString($template->getExportType()),
  25. $this->dbi->escapeString($template->getName()),
  26. $this->dbi->escapeString($template->getData())
  27. );
  28. $result = $this->dbi->tryQuery(
  29. $query,
  30. DatabaseInterface::CONNECT_CONTROL,
  31. 0,
  32. false
  33. );
  34. if ($result === false) {
  35. return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
  36. }
  37. return true;
  38. }
  39. /** @return bool|string */
  40. public function delete(string $db, string $table, string $user, int $id)
  41. {
  42. $query = sprintf(
  43. 'DELETE FROM %s.%s WHERE `id` = %s AND `username` = \'%s\';',
  44. Util::backquote($db),
  45. Util::backquote($table),
  46. $id,
  47. $this->dbi->escapeString($user)
  48. );
  49. $result = $this->dbi->tryQuery(
  50. $query,
  51. DatabaseInterface::CONNECT_CONTROL,
  52. 0,
  53. false
  54. );
  55. if ($result === false) {
  56. return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
  57. }
  58. return true;
  59. }
  60. /** @return Template|string|bool */
  61. public function load(string $db, string $table, string $user, int $id)
  62. {
  63. $query = sprintf(
  64. 'SELECT * FROM %s.%s WHERE `id` = %s AND `username` = \'%s\';',
  65. Util::backquote($db),
  66. Util::backquote($table),
  67. $id,
  68. $this->dbi->escapeString($user)
  69. );
  70. $result = $this->dbi->tryQuery(
  71. $query,
  72. DatabaseInterface::CONNECT_CONTROL,
  73. 0,
  74. false
  75. );
  76. if ($result === false) {
  77. return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
  78. }
  79. $data = [];
  80. while ($row = $this->dbi->fetchAssoc($result)) {
  81. $data = $row;
  82. }
  83. $this->dbi->freeResult($result);
  84. return Template::fromArray([
  85. 'id' => (int) $data['id'],
  86. 'username' => $data['username'],
  87. 'exportType' => $data['export_type'],
  88. 'name' => $data['template_name'],
  89. 'data' => $data['template_data'],
  90. ]);
  91. }
  92. /** @return bool|string */
  93. public function update(string $db, string $table, Template $template)
  94. {
  95. $query = sprintf(
  96. 'UPDATE %s.%s SET `template_data` = \'%s\' WHERE `id` = %s AND `username` = \'%s\';',
  97. Util::backquote($db),
  98. Util::backquote($table),
  99. $this->dbi->escapeString($template->getData()),
  100. $template->getId(),
  101. $this->dbi->escapeString($template->getUsername())
  102. );
  103. $result = $this->dbi->tryQuery(
  104. $query,
  105. DatabaseInterface::CONNECT_CONTROL,
  106. 0,
  107. false
  108. );
  109. if ($result === false) {
  110. return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
  111. }
  112. return true;
  113. }
  114. /** @return Template[]|string|bool */
  115. public function getAll(string $db, string $table, string $user, string $exportType)
  116. {
  117. $query = sprintf(
  118. 'SELECT * FROM %s.%s WHERE `username` = \'%s\' AND `export_type` = \'%s\' ORDER BY `template_name`;',
  119. Util::backquote($db),
  120. Util::backquote($table),
  121. $this->dbi->escapeString($user),
  122. $this->dbi->escapeString($exportType)
  123. );
  124. $result = $this->dbi->tryQuery(
  125. $query,
  126. DatabaseInterface::CONNECT_CONTROL,
  127. 0,
  128. false
  129. );
  130. if ($result === false) {
  131. return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
  132. }
  133. $templates = [];
  134. while ($row = $this->dbi->fetchAssoc($result)) {
  135. $templates[] = Template::fromArray([
  136. 'id' => (int) $row['id'],
  137. 'username' => $row['username'],
  138. 'exportType' => $row['export_type'],
  139. 'name' => $row['template_name'],
  140. 'data' => $row['template_data'],
  141. ]);
  142. }
  143. $this->dbi->freeResult($result);
  144. return $templates;
  145. }
  146. }