/TBBV2/cleanup_blocklist.php

https://github.com/meitar/twitter_shared_blocklist · PHP · 217 lines · 145 code · 17 blank · 55 comment · 20 complexity · e162ed61177f878ca36f8a21c6bdca2f MD5 · raw file

  1. <?php
  2. require_once('/var/www/html/sign_up/twitteroauth/twitteroauth.php');
  3. require_once('/var/www/html/sign_up/config.php');
  4. require_once('/var/www/apbb/util.php');
  5. /**
  6. * Get blockees from /var/www/apbb/blocks/level_1/2/3
  7. * Licensed to the Apache Software Foundation (ASF) under one
  8. or more contributor license agreements. See the NOTICE file
  9. distributed with this work for additional information
  10. regarding copyright ownership. The ASF licenses this file
  11. to you under the Apache License, Version 2.0 (the
  12. "License"); you may not use this file except in compliance
  13. with the License. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing,
  16. software distributed under the License is distributed on an
  17. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. KIND, either express or implied. See the License for the
  19. specific language governing permissions and limitations
  20. under the License.
  21. * Scan and if any names or ids change then reset the block list
  22. */
  23. #Start timer
  24. $starttime=start_clock();
  25. # Locking to make sure this script only runs once and nothing else runs at the same time
  26. if (check_lock()){
  27. log_it("FATAL","PICKUP MORPHS NOT RAN: BLOCKEM STILL RUNNING! MAY NEED TO REMOVE LOCK!");
  28. exit;
  29. }
  30. /* Create a TwitterOauth object with consumer/user tokens. */
  31. $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, BB_ACCESSTOKEN, BB_ACCESSTOKENSECRET);
  32. # Big call out to @secular_steve (Now @stevewcoa) for his great work in demonstrating this bug
  33. # A user can morph their account so the numerical id appears to change -->
  34. # 587589987¬secular_steve
  35. # 1158787213¬secular_steve
  36. # Whereas the original is now oool0n not secular_steve
  37. # Without this code no one would get off the blocklist but the screen_name and ids would not match
  38. # So just loop over the block list and where the user id does not match the screen name
  39. # -> delete and rewrite so it does!
  40. #Need to loop for each level level1/2/3 and process separately
  41. foreach (array("level_1/","level_2/","level_3/") as $level_dir) {
  42. # Get blockees...$blockees[0] are the numerical ids and $blockees[1] are the string ids
  43. $blockees=get_users_in_dir(BLOCKS.$level_dir);
  44. $my_user_id_blockees=$blockees[0];
  45. $my_screen_name_blockees=$blockees[1];
  46. # Now need to loop through and get user data for all of them.. Do they match or have they secular steve'd?
  47. log_it("INFO","Number of blocks: ".count($my_user_id_blockees));
  48. # Get array of Twitter user objects from the array of ids
  49. $users=get_users_from_ids($my_user_id_blockees,$connection);
  50. # Loop through em all and check if any in the list are not there at all.
  51. $twitter_ids = array();
  52. $twitter_screen_names = array();
  53. foreach ($users as $user) {
  54. $screen_name=strtolower($user->screen_name);
  55. $user_id=$user->id;
  56. $my_file = BLOCKS.$level_dir.$user_id.'¬'.$screen_name;
  57. if (file_exists($my_file)) {
  58. # Excellent... Now check it for duplicates and delete them
  59. $files = glob(BLOCKS.$level_dir.$user_id."¬*");
  60. foreach ($files as $file){
  61. if ($file != $my_file){
  62. log_it("ERROR","FOUND DUPLICATE FAKE: ".$file);
  63. unlink($file);
  64. }
  65. }
  66. } else {
  67. log_it("ERROR","FOUND MORPH! ".$level_dir.$user_id.'¬'.$screen_name);
  68. $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
  69. fwrite($handle, "blocked!");
  70. fclose($handle);
  71. log_it("INFO","NAUGHTY MORPH ADDED TO BLOCKLIST: ".$level_dir.$screen_name);
  72. }
  73. array_push($twitter_ids, $user_id);
  74. array_push($twitter_screen_names, $screen_name);
  75. }
  76. # REGET Get blockees.. Now the duplicates are gone..
  77. $blockees=get_users_in_dir(BLOCKS.$level_dir);
  78. $my_user_id_blockees=$blockees[0];
  79. $my_screen_name_blockees=$blockees[1];
  80. $user_ids_diff=array_diff($my_user_id_blockees,$twitter_ids);
  81. $screen_name_diff=array_diff($my_screen_name_blockees,$twitter_screen_names);
  82. #Check any ids for the right screen name
  83. foreach ($user_ids_diff as $user_id) {
  84. $user=$connection->get("users/show",array('user_id' => $user_id));
  85. if (isset($user->screen_name)){
  86. $screen_name=strtolower($user->screen_name);
  87. $user_id=$user->id;
  88. # check to see if file exists ->
  89. $my_file = BLOCKS.$level_dir.$user_id.'¬'.$screen_name;
  90. if (file_exists($my_file)) {
  91. # Excellent... Do nothing!
  92. } else {
  93. log_it("ERROR","FOUND MORPH FROM HANGING IDS! ".$level_dir.$user_id.'¬'.$screen_name);
  94. $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
  95. fwrite($handle, "blocked!");
  96. fclose($handle);
  97. log_it("INFO","NAUGHTY MORPH ADDED TO BLOCKLIST FROM HANGING IDS: ".$level_dir.$screen_name);
  98. }
  99. } else {
  100. $files = glob(BLOCKS.$level_dir.$user_id."¬*");
  101. foreach ($files as $file) {
  102. log_it("ERROR","MOVED TO DEAD BLOCKS $file : USER ID IS AVAILABLE");
  103. $filename = substr(strrchr($file, "/"), 1);
  104. if (copy($file,DEAD_BLOCKS.$level_dir.$filename)) {
  105. unlink($file);
  106. }
  107. }
  108. }
  109. }
  110. #Check any screen names for the right id
  111. foreach ($screen_name_diff as $screen_name) {
  112. $user=$connection->get("users/show",array('screen_name' => $screen_name));
  113. if (isset($user->screen_name)){
  114. $screen_name=strtolower($user->screen_name);
  115. $user_id=$user->id;
  116. # check to see if file exists ->
  117. $my_file = BLOCKS.$level_dir.$user_id.'¬'.$screen_name;
  118. if (file_exists($my_file)) {
  119. # Excellent... Do nothing!
  120. } else {
  121. log_it("ERROR","FOUND MORPH FROM HANGING SCREEN NAME! ".$level_dir.$user_id.'¬'.$screen_name);
  122. $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
  123. fwrite($handle, "blocked!");
  124. fclose($handle);
  125. log_it("INFO","NAUGHTY MORPH ADDED TO BLOCKLIST FROM HANGING SCREEN NAME!: ".$level_dir.$screen_name);
  126. }
  127. } else {
  128. $files = glob(BLOCKS.$level_dir."*¬".$screen_name);
  129. foreach ($files as $file) {
  130. log_it("ERROR","MOVED TO DEAD BLOCKS $file : SCREEN NAME IS AVAILABLE");
  131. $filename = substr(strrchr($file, "/"), 1);
  132. if (copy($file,DEAD_BLOCKS.$level_dir.$filename)) {
  133. unlink($file);
  134. }
  135. }
  136. }
  137. }
  138. #Now scan the dead_blocks list and check all those little fellas out. Some may have resurrected!
  139. $blockees=get_users_in_dir(DEAD_BLOCKS.$level_dir);
  140. $my_user_id_blockees=$blockees[0];
  141. $my_screen_name_blockees=$blockees[1];
  142. # Now need to loop through and get user data for all of them.. Do they match or have they secular steve'd?
  143. log_it("INFO","Number of dead blocks: ".$level_dir." : ".count($my_user_id_blockees));
  144. if (count($my_user_id_blockees)>0){
  145. # Get array of Twitter user objects from the array of ids
  146. $users=get_users_from_ids($my_user_id_blockees,$connection);
  147. # Loop over any there and just create in blocks - delete the equivalent one from dead_blocks
  148. # Keep in mind it may have morphed!
  149. foreach ($users as $user) {
  150. $screen_name=strtolower($user->screen_name);
  151. $user_id=$user->id;
  152. $files = glob(DEAD_BLOCKS.$level_dir.$user_id."¬*");
  153. foreach ($files as $file){
  154. unlink($file);
  155. $my_file = BLOCKS.$level_dir.$user_id."¬".$screen_name;
  156. $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
  157. fwrite($handle, "blocked!");
  158. fclose($handle);
  159. log_it("ERROR","FOUND ZOMBIE IN DEAD BLOCKS! RE-ADDED TO BLOCKS: ".$file);
  160. }
  161. }
  162. }
  163. }
  164. stop_clock($starttime,"PICKUP MORPHS");
  165. # return twitter user objects from an array of ids.
  166. # Calls users/lookup in blocks of 90 to reduce calls
  167. function get_users_from_ids($my_user_id_blockees,$connection){
  168. $num_per_call = 90;
  169. # Used to count how many have been added and what array index
  170. $count=0;
  171. $index=0;
  172. # Arrays of ids in comma separated list.
  173. $call_array_ids = array();
  174. $call_array_ids_tmp = array();
  175. for ($i=0; $i<=(count($my_user_id_blockees)-1); $i++)
  176. {
  177. # Do in blocks of x
  178. if ($count>=$num_per_call) {
  179. $count=0;
  180. $call_array_ids[$index]= implode(",", $call_array_ids_tmp);
  181. $call_array_ids_tmp = array();
  182. $index++;
  183. }
  184. array_push($call_array_ids_tmp, $my_user_id_blockees[$i]);
  185. $count++;
  186. }
  187. $call_array_ids[$index]= implode(",", $call_array_ids_tmp);
  188. $users=array();
  189. # Loop over all and get the corresponding user objects
  190. for ($i=0; $i<=(count($call_array_ids)-1); $i++)
  191. {
  192. $tmp_users=$connection->get("users/lookup",array('user_id' => $call_array_ids[$i]));
  193. if (!isset($tmp_users->errors)) {
  194. $users=array_merge($users,$tmp_users);
  195. }
  196. }
  197. return $users;
  198. }