fixup-shadow.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @copyright 2010 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $longoptions = array('dry-run');
  25. $helptext = <<<END_OF_USERROLE_HELP
  26. fixup_shadow.php [options]
  27. Patches up stray ostatus_profile entries with corrupted shadow entries
  28. for local users and groups.
  29. --dry-run look but don't touch
  30. END_OF_USERROLE_HELP;
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. $dry = have_option('dry-run');
  33. // Look for user.uri matches... These may not match up with the current
  34. // URL schema if the site has changed names.
  35. echo "Checking for bogus ostatus_profile entries matching user.uri...\n";
  36. $user = new User();
  37. $oprofile = new Ostatus_profile();
  38. $user->joinAdd($oprofile, 'INNER', 'oprofile', 'uri');
  39. $user->find();
  40. $count = $user->N;
  41. echo "Found $count...\n";
  42. while ($user->fetch()) {
  43. $uri = $user->getUri();
  44. echo "user $user->id ($user->nickname) hidden by $uri";
  45. if ($dry) {
  46. echo " - skipping\n";
  47. } else {
  48. echo " - removing bogus ostatus_profile entry...";
  49. $evil = Ostatus_profile::getKV('uri', $uri);
  50. $evil->delete();
  51. echo " ok\n";
  52. }
  53. }
  54. echo "\n";
  55. // Also try user_group.uri matches for local groups.
  56. // Not all group entries will have this filled out, though, as it's new!
  57. echo "Checking for bogus ostatus_profile entries matching local user_group.uri...\n";
  58. $group = new User_group();
  59. $group->joinAdd(array('uri', 'ostatus_profile:uri'));
  60. $group->joinAdd(array('id', 'local_group:group_id'));
  61. $group->find();
  62. $count = $group->N;
  63. echo "Found $count...\n";
  64. while ($group->fetch()) {
  65. $uri = $group->getUri();
  66. echo "group $group->id ($group->nickname) hidden by $uri";
  67. if ($dry) {
  68. echo " - skipping\n";
  69. } else {
  70. echo " - removing bogus ostatus_profile entry...";
  71. $evil = Ostatus_profile::getKV('uri', $uri);
  72. $evil->delete();
  73. echo " ok\n";
  74. }
  75. }
  76. echo "\n";
  77. // And there may be user_group entries remaining where we've already killed
  78. // the ostatus_profile. These were "harmless" until our lookup started actually
  79. // using the uri field, at which point we can clearly see it breaks stuff.
  80. echo "Checking for leftover bogus user_group.uri entries obscuring local_group entries...\n";
  81. $group = new User_group();
  82. $group->joinAdd(array('id', 'local_group:group_id'), 'LEFT');
  83. $group->whereAdd('group_id IS NULL');
  84. $marker = mt_rand(31337, 31337000);
  85. $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
  86. $encGroup = $group->escape($groupTemplate, true);
  87. $encGroup = str_replace($marker, '%', $encGroup);
  88. echo " LIKE '$encGroup'\n";
  89. $group->whereAdd("uri LIKE '$encGroup'");
  90. $group->find();
  91. $count = $group->N;
  92. echo "Found $count...\n";
  93. while ($group->fetch()) {
  94. $uri = $group->getUri();
  95. if (preg_match('!/group/(\d+)/id!', $uri, $matches)) {
  96. $id = intval($matches[1]);
  97. $local = Local_group::getKV('group_id', $id);
  98. if ($local) {
  99. $nick = $local->nickname;
  100. } else {
  101. $nick = '<deleted>';
  102. }
  103. echo "local group $id ($local->nickname) hidden by $uri (bogus group id $group->id)";
  104. if ($dry) {
  105. echo " - skipping\n";
  106. } else {
  107. echo " - removing bogus user_group entry...";
  108. $evil = User_group::getKV('id', $group->id);
  109. $evil->delete();
  110. echo " ok\n";
  111. }
  112. }
  113. }
  114. echo "\n";
  115. // Fallback?
  116. echo "Checking for bogus profiles blocking local users/groups by URI pattern match...\n";
  117. $oprofile = new Ostatus_profile();
  118. $marker = mt_rand(31337, 31337000);
  119. $profileTemplate = common_local_url('userbyid', array('id' => $marker));
  120. $encProfile = $oprofile->escape($profileTemplate, true);
  121. $encProfile = str_replace($marker, '%', $encProfile);
  122. echo " LIKE '$encProfile'\n";
  123. $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
  124. $encGroup = $oprofile->escape($groupTemplate, true);
  125. $encGroup = str_replace($marker, '%', $encGroup);
  126. echo " LIKE '$encGroup'\n";
  127. $sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
  128. $oprofile->query(sprintf($sql, $encProfile, $encGroup));
  129. $count = $oprofile->N;
  130. echo "Found $count...\n";
  131. while ($oprofile->fetch()) {
  132. $uri = $oprofile->getUri();
  133. if (preg_match('!/group/(\d+)/id!', $oprofile->getUri(), $matches)) {
  134. $id = intval($matches[1]);
  135. $group = Local_group::getKV('group_id', $id);
  136. if ($group) {
  137. $nick = $group->nickname;
  138. } else {
  139. $nick = '<deleted>';
  140. }
  141. echo "group $id ($nick) hidden by $uri";
  142. } elseif (preg_match('!/user/(\d+)!', $uri, $matches)) {
  143. $id = intval($matches[1]);
  144. $user = User::getKV('id', $id);
  145. if ($user) {
  146. $nick = $user->nickname;
  147. } else {
  148. $nick = '<deleted>';
  149. }
  150. echo "user $id ($nick) hidden by $uri";
  151. } else {
  152. echo "$uri matched query, but we don't recognize it.\n";
  153. continue;
  154. }
  155. if ($dry) {
  156. echo " - skipping\n";
  157. } else {
  158. echo " - removing bogus ostatus_profile entry...";
  159. $evil = clone($oprofile);
  160. $evil->delete();
  161. echo " ok\n";
  162. }
  163. }
  164. if ($count && $dry) {
  165. echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";
  166. } else {
  167. echo "done.\n";
  168. }