Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.24 KB | None | 0 0
  1. <?php
  2. class StoreResult extends Threaded {
  3.     private $total = 0;
  4.     private $valid = 0;
  5.     private $isConnected = true;
  6.     public function AddValid() {
  7.         return $this->valid ++;
  8.     }
  9.     public function AddTotal() {
  10.         return $this->total ++;
  11.     }
  12.     public function GetResults() {
  13.         $arr [0] = $this->total;
  14.         $arr [1] = $this->valid;
  15.         return $arr;
  16.     }
  17.     public function SetConnectionStatus($conn_status = false) {
  18.         $conn_status == false ? $this->isConnected == false : $this->isConnected == true;
  19.     }
  20.     public function IsConnected() {
  21.         return $this->isConnected;
  22.     }
  23. }
  24. class my_thread extends Thread {
  25.     private $url;
  26.     public $result = false;
  27.     private $outfile;
  28.     private $store_result;
  29.     private $not_valid_file = "not_valid.txt";
  30.     function __construct($url, $out_file, StoreResult $store_result) {
  31.         $this->url = $url;
  32.         $this->store_result = $store_result;
  33.         $this->outfile = $out_file;
  34.     }
  35.     public function run() {
  36.         // echo $this->url."\n";
  37.         //delete non-printable characters
  38.         $this->url = preg_replace('/[^\PC\s]/u', '', $this->url);
  39.         $data = explode ( "|", $this->url );
  40.        
  41.                
  42.         $vars = GetServerData ( $url );
  43.         $this->store_result->AddTotal ();
  44.         //print_r($vars);
  45.         if ($vars) {
  46.             $this->store_result->AddValid ();
  47.             $str = "";
  48.             $n_ar = array ();
  49.            
  50.             foreach ( $vars as $k => $v ) {
  51.                 if ($v != "")
  52.                     $v = trim ( $v );
  53.                 $n_ar [$k] = $v;
  54.             }
  55.             // print_r($vars);
  56.             $vars = $n_ar;
  57.             //
  58.             // $str = implode ( ",", $vars );
  59.             $str .= "\r\n";
  60.             $fp = fopen ( $this->outfile, "a" ) or exit ( "Can't open " . $this->outfile . "!\n" );
  61.             if (flock ( $fp, LOCK_EX )) {
  62.                 // fputs ( $fp, $str );
  63.                 fputcsv ( $fp, $vars, ",", "'" );
  64.                 fflush ( $fp );
  65.                 flock ( $fp, LOCK_UN );
  66.             } else {
  67.                
  68.                 echo "Can't flock() " . $this->outfile . "!\n";
  69.                 // $this->setGarbage ();
  70.             }
  71.             fclose($fp);
  72.             $this->result = true;
  73.            
  74.         } else {
  75.            
  76.             //echo $this->outfile."\n";
  77.             $path = realpath(dirname($this->outfile));
  78.             //echo $path."\n";
  79.             $fp = fopen($path."\\".$this->not_valid_file, "a");
  80.             if( flock ( $fp, LOCK_EX ) )
  81.             {
  82.                 fputs ( $fp, trim($this->url)."\n");
  83.                 fflush ( $fp );
  84.                 flock ( $fp, LOCK_UN );
  85.             }
  86.             fclose($fp);
  87.        
  88.         }
  89.     }
  90. }
  91.  
  92. class schedule_thread extends Thread {
  93.     private $url;
  94.     public $result = false;
  95.     private $store_result;
  96.     private $in_data;
  97.     private $DB;
  98.     function __construct($data, $db_data, StoreResult $store_result) {
  99.         $this->store_result = $store_result;
  100.         $this->in_data = $data;
  101.         $this->DB = $db_data;
  102.     }
  103.     public function run() {
  104.         // echo $this->url."\n";
  105.         // print_r($this->in_data);
  106.         $url = $this->in_data ["url_value"];
  107.    
  108.         $vars = GetServerData ( $url );
  109.         $this->store_result->AddTotal ();
  110.         // print_r($vars);
  111.         $DB = $this->DB;
  112.         $d = $this->in_data;
  113.         // print_r($d);
  114.         // print_r($DB);
  115.         if ($vars) {
  116.            
  117.             // echo "$url valid!\n";
  118.             $this->store_result->AddValid ();
  119.            
  120.             $mysqli_obj = CreateMysqli ( $DB ['host'], $DB ['user'], $DB ['password'], $DB ['database'] );
  121.             if ($mysqli_obj == FALSE) {
  122.                 $error_info = sprintf ( "('%d') %s ", mysqli_connect_errno (), mysqli_connect_error () );
  123.                 error_msg ( "MySQL connection error!", $error_info );
  124.                 return;
  125.             }
  126.                    
  127.             $query = sprintf ( UPDATE QUERY );
  128.             //echo $query;
  129.             // var_dump($mysqli_obj);
  130.             $res = run_query ( $mysqli_obj, $query );
  131.             // var_dump($res);
  132.             if (! $res) {
  133.                 echo "Error updating record in url_data table!\n";
  134.                 $mysqli_obj->close ();
  135.                 return;
  136.             }
  137.            
  138.             $mysqli_obj->close ();
  139.            
  140.             $this->result = true;
  141.            
  142.         } else {
  143.            
  144.             echo "not valid!\n";
  145.            
  146.             if (curlPost ( "", "", true )) {
  147.                 // echo "Okay";
  148.                
  149.                 $mysqli_obj = CreateMysqli ( $DB ['host'], $DB ['user'], $DB ['password'], $DB ['database'] );
  150.                 if ($mysqli_obj == FALSE) {
  151.                     $error_info = sprintf ( "('%d') %s ", mysqli_connect_errno (), mysqli_connect_error () );
  152.                     error_msg ( "MySQL connection error!", $error_info );
  153.                     return;
  154.                 }
  155.                
  156.            
  157.                 $query = sprintf ( INSERT QUERY )
  158.                 // echo $query;
  159.                 // var_dump($mysqli_obj);
  160.                 $res = run_query ( $mysqli_obj, $query );
  161.                 // var_dump($res);
  162.                 if (! $res) {
  163.                     echo "Error adding record to temp table!\n";
  164.                     $mysqli_obj->close ();
  165.                     return;
  166.                 }
  167.                
  168.                
  169.                 $query = sprintf ( DELETE QUERY );
  170.                
  171.                 $res = run_query ( $mysqli_obj, $query );
  172.                 if (! $res) {
  173.                     echo "Can't delete record from main table!\n";
  174.                     $mysqli_obj->close ();
  175.                     return;
  176.                 }
  177.                
  178.                 $mysqli_obj->close ();
  179.                
  180.             } else {
  181.                
  182.                 echo "No network connection";
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. ?>
  189.  
  190. /////////////////////////////////////
  191.  
  192.  
  193. $data = file ( $data_list );
  194. $workers = [ ];
  195. $t_count = 80;
  196. $flag = 1;
  197. $k = 0;
  198. $perc_diff = 1;
  199. $last_percent = 0;
  200. // echo "$data_size";
  201. $connected = true;
  202. $s_time = microtime ( TRUE );
  203. while ( $flag === 1 ) {
  204.    
  205.     /*
  206.      * //echo "$k\n";
  207.      * if($k >= $data_size)
  208.      * {
  209.      * //echo "111"; exit();
  210.      * $flag = 0;
  211.      * break 2;
  212.      * }
  213.      */
  214.    
  215.     if (curlPost ( "", "", true )) {
  216.         // echo "Okay";
  217.     } else {
  218.        
  219.         echo "No active network connection avaliable! Shutting down...\n";
  220.         $flag = false;
  221.        
  222.         $c_w = count ( $workers );
  223.         for($i = 0; $i < $c_w; $i ++) {
  224.             if ($workers [$i]->join ()) {
  225.                 // echo "joining $i\n";
  226.                 unset ( $workers [$i] );
  227.             }
  228.         }
  229.        
  230.         $connected = false;
  231.         break;
  232.     }
  233.    
  234.     $c_w = count ( $workers );
  235.    
  236.     if ($c_w < $t_count) {
  237.         for($i = $c_w; $i < $t_count - $c_w; $i ++) {
  238.             if ($k >= $data_size) {
  239.                 $flag = 0;
  240.                 break;
  241.             }
  242.            
  243.             $workers [$i] = new my_thread ( $data [$k], $out_file, $result );
  244.             // echo $data[$k]."\n";
  245.             $workers [$i]->start ();
  246.             $k ++;
  247.         }
  248.     }
  249.    
  250.     $c_w = count ( $workers );
  251.     for($i = 0; $i < $c_w; $i ++) {
  252.         if ($workers [$i]->join ()) {
  253.             // echo "joining $i\n";
  254.             unset ( $workers [$i] );
  255.         }
  256.     }
  257.     $arr = $result->GetResults ();
  258.     $valid = $arr [1];
  259.     $e_time = microtime ( TRUE );
  260.     $t = $e_time - $s_time;
  261.     $percent = ($k / $data_size) * 100;
  262.     if (($percent - $last_percent) >= $perc_diff || $percent == 100) {
  263.         $last_percent = $percent;
  264.         SetStatus ( $mysqli_obj, $DB ['worker_table'], 1, "Working", ( int ) $percent, "" );
  265.         echo "progress: " . $percent . " percent ($k of $data_size, valid: $valid) \n";
  266.         echo "time: " . $t . "\n";
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement