RelationCleanup.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Set of functions used for cleaning up phpMyAdmin tables
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. /**
  8. * PhpMyAdmin\RelationCleanup class
  9. */
  10. class RelationCleanup
  11. {
  12. /** @var Relation */
  13. public $relation;
  14. /** @var DatabaseInterface */
  15. public $dbi;
  16. /**
  17. * @param DatabaseInterface $dbi DatabaseInterface object
  18. * @param Relation $relation Relation object
  19. */
  20. public function __construct($dbi, Relation $relation)
  21. {
  22. $this->dbi = $dbi;
  23. $this->relation = $relation;
  24. }
  25. /**
  26. * Cleanup column related relation stuff
  27. *
  28. * @param string $db database name
  29. * @param string $table table name
  30. * @param string $column column name
  31. *
  32. * @return void
  33. */
  34. public function column($db, $table, $column)
  35. {
  36. $cfgRelation = $this->relation->getRelationsParam();
  37. if ($cfgRelation['commwork']) {
  38. $remove_query = 'DELETE FROM '
  39. . Util::backquote($cfgRelation['db'])
  40. . '.' . Util::backquote($cfgRelation['column_info'])
  41. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  42. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  43. . '\''
  44. . ' AND column_name = \'' . $this->dbi->escapeString($column)
  45. . '\'';
  46. $this->relation->queryAsControlUser($remove_query);
  47. }
  48. if ($cfgRelation['displaywork']) {
  49. $remove_query = 'DELETE FROM '
  50. . Util::backquote($cfgRelation['db'])
  51. . '.' . Util::backquote($cfgRelation['table_info'])
  52. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  53. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  54. . '\''
  55. . ' AND display_field = \'' . $this->dbi->escapeString($column)
  56. . '\'';
  57. $this->relation->queryAsControlUser($remove_query);
  58. }
  59. if (! $cfgRelation['relwork']) {
  60. return;
  61. }
  62. $remove_query = 'DELETE FROM '
  63. . Util::backquote($cfgRelation['db'])
  64. . '.' . Util::backquote($cfgRelation['relation'])
  65. . ' WHERE master_db = \'' . $this->dbi->escapeString($db)
  66. . '\''
  67. . ' AND master_table = \'' . $this->dbi->escapeString($table)
  68. . '\''
  69. . ' AND master_field = \'' . $this->dbi->escapeString($column)
  70. . '\'';
  71. $this->relation->queryAsControlUser($remove_query);
  72. $remove_query = 'DELETE FROM '
  73. . Util::backquote($cfgRelation['db'])
  74. . '.' . Util::backquote($cfgRelation['relation'])
  75. . ' WHERE foreign_db = \'' . $this->dbi->escapeString($db)
  76. . '\''
  77. . ' AND foreign_table = \'' . $this->dbi->escapeString($table)
  78. . '\''
  79. . ' AND foreign_field = \'' . $this->dbi->escapeString($column)
  80. . '\'';
  81. $this->relation->queryAsControlUser($remove_query);
  82. }
  83. /**
  84. * Cleanup table related relation stuff
  85. *
  86. * @param string $db database name
  87. * @param string $table table name
  88. *
  89. * @return void
  90. */
  91. public function table($db, $table)
  92. {
  93. $cfgRelation = $this->relation->getRelationsParam();
  94. if ($cfgRelation['commwork']) {
  95. $remove_query = 'DELETE FROM '
  96. . Util::backquote($cfgRelation['db'])
  97. . '.' . Util::backquote($cfgRelation['column_info'])
  98. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  99. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  100. . '\'';
  101. $this->relation->queryAsControlUser($remove_query);
  102. }
  103. if ($cfgRelation['displaywork']) {
  104. $remove_query = 'DELETE FROM '
  105. . Util::backquote($cfgRelation['db'])
  106. . '.' . Util::backquote($cfgRelation['table_info'])
  107. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  108. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  109. . '\'';
  110. $this->relation->queryAsControlUser($remove_query);
  111. }
  112. if ($cfgRelation['pdfwork']) {
  113. $remove_query = 'DELETE FROM '
  114. . Util::backquote($cfgRelation['db'])
  115. . '.' . Util::backquote($cfgRelation['table_coords'])
  116. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  117. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  118. . '\'';
  119. $this->relation->queryAsControlUser($remove_query);
  120. }
  121. if ($cfgRelation['relwork']) {
  122. $remove_query = 'DELETE FROM '
  123. . Util::backquote($cfgRelation['db'])
  124. . '.' . Util::backquote($cfgRelation['relation'])
  125. . ' WHERE master_db = \'' . $this->dbi->escapeString($db)
  126. . '\''
  127. . ' AND master_table = \'' . $this->dbi->escapeString($table)
  128. . '\'';
  129. $this->relation->queryAsControlUser($remove_query);
  130. $remove_query = 'DELETE FROM '
  131. . Util::backquote($cfgRelation['db'])
  132. . '.' . Util::backquote($cfgRelation['relation'])
  133. . ' WHERE foreign_db = \'' . $this->dbi->escapeString($db)
  134. . '\''
  135. . ' AND foreign_table = \'' . $this->dbi->escapeString($table)
  136. . '\'';
  137. $this->relation->queryAsControlUser($remove_query);
  138. }
  139. if ($cfgRelation['uiprefswork']) {
  140. $remove_query = 'DELETE FROM '
  141. . Util::backquote($cfgRelation['db'])
  142. . '.' . Util::backquote($cfgRelation['table_uiprefs'])
  143. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  144. . ' AND table_name = \'' . $this->dbi->escapeString($table)
  145. . '\'';
  146. $this->relation->queryAsControlUser($remove_query);
  147. }
  148. if (! $cfgRelation['navwork']) {
  149. return;
  150. }
  151. $remove_query = 'DELETE FROM '
  152. . Util::backquote($cfgRelation['db'])
  153. . '.' . Util::backquote($cfgRelation['navigationhiding'])
  154. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\''
  155. . ' AND (table_name = \'' . $this->dbi->escapeString($table)
  156. . '\''
  157. . ' OR (item_name = \'' . $this->dbi->escapeString($table)
  158. . '\''
  159. . ' AND item_type = \'table\'))';
  160. $this->relation->queryAsControlUser($remove_query);
  161. }
  162. /**
  163. * Cleanup database related relation stuff
  164. *
  165. * @param string $db database name
  166. *
  167. * @return void
  168. */
  169. public function database($db)
  170. {
  171. $cfgRelation = $this->relation->getRelationsParam();
  172. if ($cfgRelation['commwork']) {
  173. $remove_query = 'DELETE FROM '
  174. . Util::backquote($cfgRelation['db'])
  175. . '.' . Util::backquote($cfgRelation['column_info'])
  176. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  177. $this->relation->queryAsControlUser($remove_query);
  178. }
  179. if ($cfgRelation['bookmarkwork']) {
  180. $remove_query = 'DELETE FROM '
  181. . Util::backquote($cfgRelation['db'])
  182. . '.' . Util::backquote($cfgRelation['bookmark'])
  183. . ' WHERE dbase = \'' . $this->dbi->escapeString($db) . '\'';
  184. $this->relation->queryAsControlUser($remove_query);
  185. }
  186. if ($cfgRelation['displaywork']) {
  187. $remove_query = 'DELETE FROM '
  188. . Util::backquote($cfgRelation['db'])
  189. . '.' . Util::backquote($cfgRelation['table_info'])
  190. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  191. $this->relation->queryAsControlUser($remove_query);
  192. }
  193. if ($cfgRelation['pdfwork']) {
  194. $remove_query = 'DELETE FROM '
  195. . Util::backquote($cfgRelation['db'])
  196. . '.' . Util::backquote($cfgRelation['pdf_pages'])
  197. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  198. $this->relation->queryAsControlUser($remove_query);
  199. $remove_query = 'DELETE FROM '
  200. . Util::backquote($cfgRelation['db'])
  201. . '.' . Util::backquote($cfgRelation['table_coords'])
  202. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  203. $this->relation->queryAsControlUser($remove_query);
  204. }
  205. if ($cfgRelation['relwork']) {
  206. $remove_query = 'DELETE FROM '
  207. . Util::backquote($cfgRelation['db'])
  208. . '.' . Util::backquote($cfgRelation['relation'])
  209. . ' WHERE master_db = \''
  210. . $this->dbi->escapeString($db) . '\'';
  211. $this->relation->queryAsControlUser($remove_query);
  212. $remove_query = 'DELETE FROM '
  213. . Util::backquote($cfgRelation['db'])
  214. . '.' . Util::backquote($cfgRelation['relation'])
  215. . ' WHERE foreign_db = \'' . $this->dbi->escapeString($db)
  216. . '\'';
  217. $this->relation->queryAsControlUser($remove_query);
  218. }
  219. if ($cfgRelation['uiprefswork']) {
  220. $remove_query = 'DELETE FROM '
  221. . Util::backquote($cfgRelation['db'])
  222. . '.' . Util::backquote($cfgRelation['table_uiprefs'])
  223. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  224. $this->relation->queryAsControlUser($remove_query);
  225. }
  226. if ($cfgRelation['navwork']) {
  227. $remove_query = 'DELETE FROM '
  228. . Util::backquote($cfgRelation['db'])
  229. . '.' . Util::backquote($cfgRelation['navigationhiding'])
  230. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  231. $this->relation->queryAsControlUser($remove_query);
  232. }
  233. if ($cfgRelation['savedsearcheswork']) {
  234. $remove_query = 'DELETE FROM '
  235. . Util::backquote($cfgRelation['db'])
  236. . '.' . Util::backquote($cfgRelation['savedsearches'])
  237. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  238. $this->relation->queryAsControlUser($remove_query);
  239. }
  240. if (! $cfgRelation['centralcolumnswork']) {
  241. return;
  242. }
  243. $remove_query = 'DELETE FROM '
  244. . Util::backquote($cfgRelation['db'])
  245. . '.' . Util::backquote($cfgRelation['central_columns'])
  246. . ' WHERE db_name = \'' . $this->dbi->escapeString($db) . '\'';
  247. $this->relation->queryAsControlUser($remove_query);
  248. }
  249. /**
  250. * Cleanup user related relation stuff
  251. *
  252. * @param string $username username
  253. *
  254. * @return void
  255. */
  256. public function user($username)
  257. {
  258. $cfgRelation = $this->relation->getRelationsParam();
  259. if ($cfgRelation['bookmarkwork']) {
  260. $remove_query = 'DELETE FROM '
  261. . Util::backquote($cfgRelation['db'])
  262. . '.' . Util::backquote($cfgRelation['bookmark'])
  263. . " WHERE `user` = '" . $this->dbi->escapeString($username)
  264. . "'";
  265. $this->relation->queryAsControlUser($remove_query);
  266. }
  267. if ($cfgRelation['historywork']) {
  268. $remove_query = 'DELETE FROM '
  269. . Util::backquote($cfgRelation['db'])
  270. . '.' . Util::backquote($cfgRelation['history'])
  271. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  272. . "'";
  273. $this->relation->queryAsControlUser($remove_query);
  274. }
  275. if ($cfgRelation['recentwork']) {
  276. $remove_query = 'DELETE FROM '
  277. . Util::backquote($cfgRelation['db'])
  278. . '.' . Util::backquote($cfgRelation['recent'])
  279. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  280. . "'";
  281. $this->relation->queryAsControlUser($remove_query);
  282. }
  283. if ($cfgRelation['favoritework']) {
  284. $remove_query = 'DELETE FROM '
  285. . Util::backquote($cfgRelation['db'])
  286. . '.' . Util::backquote($cfgRelation['favorite'])
  287. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  288. . "'";
  289. $this->relation->queryAsControlUser($remove_query);
  290. }
  291. if ($cfgRelation['uiprefswork']) {
  292. $remove_query = 'DELETE FROM '
  293. . Util::backquote($cfgRelation['db'])
  294. . '.' . Util::backquote($cfgRelation['table_uiprefs'])
  295. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  296. . "'";
  297. $this->relation->queryAsControlUser($remove_query);
  298. }
  299. if ($cfgRelation['userconfigwork']) {
  300. $remove_query = 'DELETE FROM '
  301. . Util::backquote($cfgRelation['db'])
  302. . '.' . Util::backquote($cfgRelation['userconfig'])
  303. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  304. . "'";
  305. $this->relation->queryAsControlUser($remove_query);
  306. }
  307. if ($cfgRelation['menuswork']) {
  308. $remove_query = 'DELETE FROM '
  309. . Util::backquote($cfgRelation['db'])
  310. . '.' . Util::backquote($cfgRelation['users'])
  311. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  312. . "'";
  313. $this->relation->queryAsControlUser($remove_query);
  314. }
  315. if ($cfgRelation['navwork']) {
  316. $remove_query = 'DELETE FROM '
  317. . Util::backquote($cfgRelation['db'])
  318. . '.' . Util::backquote($cfgRelation['navigationhiding'])
  319. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  320. . "'";
  321. $this->relation->queryAsControlUser($remove_query);
  322. }
  323. if ($cfgRelation['savedsearcheswork']) {
  324. $remove_query = 'DELETE FROM '
  325. . Util::backquote($cfgRelation['db'])
  326. . '.' . Util::backquote($cfgRelation['savedsearches'])
  327. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  328. . "'";
  329. $this->relation->queryAsControlUser($remove_query);
  330. }
  331. if (! $cfgRelation['designersettingswork']) {
  332. return;
  333. }
  334. $remove_query = 'DELETE FROM '
  335. . Util::backquote($cfgRelation['db'])
  336. . '.' . Util::backquote($cfgRelation['designer_settings'])
  337. . " WHERE `username` = '" . $this->dbi->escapeString($username)
  338. . "'";
  339. $this->relation->queryAsControlUser($remove_query);
  340. }
  341. }