RelationCleanup.php 15 KB

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