Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 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.  
  93. /////////////////////////////////////
  94.  
  95.  
  96. $data = file ( $data_list );
  97. $workers = [ ];
  98. $t_count = 80;
  99. $flag = 1;
  100. $k = 0;
  101. $perc_diff = 1;
  102. $last_percent = 0;
  103. // echo "$data_size";
  104. $connected = true;
  105. $s_time = microtime ( TRUE );
  106. while ( $flag === 1 ) {
  107.    
  108.     /*
  109.      * //echo "$k\n";
  110.      * if($k >= $data_size)
  111.      * {
  112.      * //echo "111"; exit();
  113.      * $flag = 0;
  114.      * break 2;
  115.      * }
  116.      */
  117.    
  118.     if (curlPost ( "", "", true )) {
  119.         // echo "Okay";
  120.     } else {
  121.        
  122.         echo "No active network connection avaliable! Shutting down...\n";
  123.         $flag = false;
  124.        
  125.         $c_w = count ( $workers );
  126.         for($i = 0; $i < $c_w; $i ++) {
  127.             if ($workers [$i]->join ()) {
  128.                 // echo "joining $i\n";
  129.                 unset ( $workers [$i] );
  130.             }
  131.         }
  132.        
  133.         $connected = false;
  134.         break;
  135.     }
  136.    
  137.     $c_w = count ( $workers );
  138.    
  139.     if ($c_w < $t_count) {
  140.         for($i = $c_w; $i < $t_count - $c_w; $i ++) {
  141.             if ($k >= $data_size) {
  142.                 $flag = 0;
  143.                 break;
  144.             }
  145.            
  146.             $workers [$i] = new my_thread ( $data [$k], $out_file, $result );
  147.             // echo $data[$k]."\n";
  148.             $workers [$i]->start ();
  149.             $k ++;
  150.         }
  151.     }
  152.    
  153.     $c_w = count ( $workers );
  154.     for($i = 0; $i < $c_w; $i ++) {
  155.         if ($workers [$i]->join ()) {
  156.             // echo "joining $i\n";
  157.             unset ( $workers [$i] );
  158.         }
  159.     }
  160.     $arr = $result->GetResults ();
  161.     $valid = $arr [1];
  162.     $e_time = microtime ( TRUE );
  163.     $t = $e_time - $s_time;
  164.     $percent = ($k / $data_size) * 100;
  165.     if (($percent - $last_percent) >= $perc_diff || $percent == 100) {
  166.         $last_percent = $percent;
  167.         SetStatus ( $mysqli_obj, $DB ['worker_table'], 1, "Working", ( int ) $percent, "" );
  168.         echo "progress: " . $percent . " percent ($k of $data_size, valid: $valid) \n";
  169.         echo "time: " . $t . "\n";
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement