Guest User

link

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