Guest User

relink

a guest
Jan 29th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.73 KB | None | 0 0
  1. #!/usr/bin/php
  2.  
  3. <?
  4. if ($_SERVER["REMOTE_ADDR"]) {
  5.   print "This script must be run from command line\n";
  6.   exit;
  7. }
  8.  
  9. require (dirname($_SERVER["SCRIPT_FILENAME"]).'/../config.php');
  10. require (dirname($_SERVER["SCRIPT_FILENAME"]).'/../lib/lib.php');
  11.  
  12. if (file_exists($linker_lock_file)) {
  13.   print "Lock file exist! Another linker is running?\n";
  14.   exit;
  15. }
  16.  
  17. touch($linker_lock_file);
  18.  
  19. connect_to_sql($sql_host,$sql_base,$sql_user,$sql_pass);
  20.  
  21. // ▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒ ▒▒▒▒▒▒▒▒▒
  22. mysqli_query($link, "create temporary table `tmp` (
  23.   `id` bigint(64) NOT NULL,
  24.   `msgid` varchar(128) NULL,
  25.   `reply` varchar(128) NULL,
  26.   `recieved` datetime NULL,
  27.   `area` text NULL,
  28.   `thread` text NULL,
  29.   `level` bigint(64) NOT NULL,
  30.   `inthread` bigint(64) NOT NULL,
  31.   `fromname` varchar(128) NULL,
  32.   `fromaddr` varchar(128) NULL,
  33.   `hash` varchar(128) NULL,
  34.   `date` varchar(128) NULL,
  35.   `subject` text NULL
  36. ) CHARSET=utf8");
  37.  
  38. // ▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒ ▒▒▒▒▒▒
  39.   mysqli_query($link, "CREATE temporary TABLE `tmp_threads` (
  40.  `area` varchar(128) NOT NULL default '',
  41.  `thread` varchar(128) NOT NULL default '',
  42.  `hash` varchar(128) NOT NULL default '',
  43.  `subject` text NOT NULL,
  44.  `author` varchar(128) NOT NULL default '',
  45.  `author_address` varchar(128) NOT NULL default '',
  46.  `author_date` varchar(128) NOT NULL default '',
  47.  `last_author` varchar(128) NOT NULL default '',
  48.  `last_author_address` varchar(128) NOT NULL default '',
  49.  `last_author_date` varchar(128) NOT NULL default '',
  50.  `num` bigint(20) NOT NULL default '0',
  51.  `lastupdate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  52.  UNIQUE KEY `area_2` (`area`,`thread`),
  53.  KEY `area` (`area`)
  54. ) DEFAULT CHARSET=utf8");
  55.  
  56. if ($argv['1']) {
  57.   $area=$argv['1'];
  58. } else {
  59.   $area="";
  60. }
  61.  
  62. if ($area) {
  63.   $query=("select upper(area) as area from `areas` where area=\"$area\";");
  64. } else {
  65.   $query=("select upper(area) as area from `areas` where area!='' order by area;");
  66. }
  67. $result=mysqli_query($link, $query);
  68. while ($row=mysqli_fetch_object($result)) {
  69.   $strict = "SET sql_mode = ''";
  70.   mysqli_query($link, $strict);
  71.  
  72.   // ▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒
  73.   mysqli_query($link, "delete from `tmp`;");
  74.   // ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒
  75.   mysqli_query($link, "insert into `tmp` (id, msgid, reply, recieved,area,thread,level,inthread,fromname,fromaddr,hash,date,subject)
  76.                           select id, msgid, reply, recieved,area,thread,level,inthread,fromname,fromaddr,hash,date,subject
  77.                            from `messages` where area=\"$row->area\";");
  78.  
  79.   print $row->area ."\n";
  80.   $area_end=0;
  81.   while ($area_end==0){
  82.     $result2=mysqli_query($link, "select msgid,reply,subject,fromname,fromaddr,date,recieved,hash from `tmp` order by recieved limit 1;");
  83.     if (mysqli_num_rows($result2)){
  84.       $row2=mysqli_fetch_object($result2);
  85.       $thread_info = array (
  86.         'area'                  => $row->area,
  87.         'thread'                => find_begin($row2->msgid),
  88.         'subject'               => $row2->subject,
  89.         'author'                => $row2->fromname,
  90.         'author_address'        => $row2->fromaddr,
  91.         'author_date'           => $row2->date,
  92.         'last_author'           => $row2->fromname,
  93.         'last_author_address'   => $row2->fromaddr,
  94.         'last_author_date'      => $row2->date,
  95.         'num'                   => 0,
  96.         'lastupdate'            => $row2->recieved,
  97.         'last_hash'             => $row2->hash
  98.       );
  99. //      print_r ($thread_info);
  100.       $thread_info=set_thread($thread_info);
  101.       var_dump($row2->received);
  102. //      print_r ($thread_info);
  103.     } else {
  104.       $area_end=1;
  105.     }
  106.     save_thread($thread_info);
  107.   }
  108.   mysqli_query($link, "delete from `threads` where area=\"$row->area\";");
  109.   mysqli_query($link, "insert into `threads` select * from `tmp_threads`;");
  110.   mysqli_query($link, "delete from `tmp_threads` where area=\"$row->area\"';");
  111.  
  112.  
  113. }
  114.  
  115. unlink($linker_lock_file);
  116.  
  117. function find_begin($msgid){
  118.   global $link;
  119.   $result=mysqli_query($link, "select reply from `tmp` where msgid=\"$msgid\"");
  120.   if(mysqli_num_rows($result)){
  121.     $row=mysqli_fetch_object($result);
  122.     if ($row->reply) {
  123.       $return=find_begin($row->reply);
  124.       if (!$return) {
  125.         $return=$msgid;
  126.       }
  127.     } else {
  128.       $return=$msgid;
  129.     }
  130.   } else {
  131.     $return=0;
  132.   }
  133.   return $return;
  134. }
  135.  
  136. function set_thread($thread_info,$msgid=0,$level=0){
  137.   global $link;
  138.   if (!$msgid){ $msgid=$thread_info['thread']; }
  139.   mysqli_query($link, "delete from `tmp` where msgid=\"$msgid\";");
  140.   mysqli_query($link, "update `messages` set thread=\"".$thread_info['thread']."\", inthread=\"".$thread_info['num']."\", level=\"$level\" where msgid=\"$msgid\" and area=\"".$thread_info['area']."\";");
  141.   $thread_info['num']++;
  142.   $result=mysqli_query($link, "select msgid,recieved,date,fromaddr,fromname,hash,subject from `tmp` where reply=\"$msgid\" order by recieved;");
  143.   while ($row=mysqli_fetch_object($result)){
  144.     if (match_text($row->subject,$thread_info['subject'])){
  145.       if ($row->recieved > $thread_info['recieved']){
  146.         $thread_info['lastupdate']=$row->recieved;
  147.         $thread_info['last_author']=$row->fromname;
  148.         $thread_info['last_author_address']=$row->fromaddr;
  149.         $thread_info['last_author_date']=$row->date;
  150.         $thread_info['last_hash']=$row->hash;
  151.       }
  152.       $tread_info=set_thread($thread_info,$row->msgid,$level+1);
  153.     }else{
  154.       $new_thread_info = array (
  155.         'area'                  => $thread_info['area'],
  156.         'thread'                => $row->msgid,
  157.         'subject'               => $row->subject,
  158.         'author'                => $row->fromname,
  159.         'author_address'        => $row->fromaddr,
  160.         'author_date'           => $row->date,
  161.         'last_author'           => $row->fromname,
  162.         'last_author_address'   => $row->fromaddr,
  163.         'last_author_date'      => $row->date,
  164.         'num'                   => 0,
  165.         'lastupdate'            => $row->recieved,
  166.         'last_hash'             => $row->hash
  167.         );
  168.       $new_thread_info=set_thread($new_thread_info);
  169.       save_thread($new_thread_info);
  170.     }
  171.   }
  172.   return $thread_info;
  173. }
  174.  
  175. function match_text($str1,$str2){
  176.   $str1=preg_replace('/^Re/','',$str1);
  177.   $str2=preg_replace('/^Re/','',$str2);
  178.   $str1=preg_replace('/▒/','H',$str1);
  179.   $str2=preg_replace('/▒/','H',$str2);
  180.   $str1=preg_replace('/(:|\^| |[0-9]|\[.*\]|\(no subject\))/','',$str1);
  181.   $str2=preg_replace('/(:|\^| |[0-9]|\[.*\]|\(no subject\))/','',$str2);
  182.   if (levenshtein($str1,$str2, 1,10,1) < 20) {
  183.     return 1;
  184.   }else{
  185.     print "|\n";
  186.     return 0;
  187.   }
  188. }
  189.  
  190. function save_thread($thread_info){
  191.     global $link;
  192.     mysqli_query($link, "
  193.        insert into `tmp_threads` set
  194.          area=\"".$thread_info['area']."\",
  195.          thread=\"".$thread_info['thread']."\",
  196.          hash=\"".$thread_info['last_hash']."\",
  197.          subject=\"".$thread_info['subject']."\",
  198.          author=\"".$thread_info['author']."\",
  199.          author_address=\"".$thread_info['author_address']."\",
  200.          author_date=\"".$thread_info['author_date']."\",
  201.          last_author=\"".$thread_info['last_author']."\",
  202.          last_author_date=\"".$thread_info['last_author_date']."\",
  203.          num=\"".$thread_info['num']."\",
  204.          lastupdate=\"".$thread_info['lastupdate']."\";
  205.    ");
  206.  
  207. }
  208.  
  209. ?>
Advertisement
Add Comment
Please, Sign In to add comment