Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class StoreResult extends Threaded {
- private $total = 0;
- private $valid = 0;
- private $isConnected = true;
- public function AddValid() {
- return $this->valid ++;
- }
- public function AddTotal() {
- return $this->total ++;
- }
- public function GetResults() {
- $arr [0] = $this->total;
- $arr [1] = $this->valid;
- return $arr;
- }
- public function SetConnectionStatus($conn_status = false) {
- $conn_status == false ? $this->isConnected == false : $this->isConnected == true;
- }
- public function IsConnected() {
- return $this->isConnected;
- }
- }
- class my_thread extends Thread {
- private $url;
- public $result = false;
- private $outfile;
- private $store_result;
- private $not_valid_file = "not_valid.txt";
- function __construct($url, $out_file, StoreResult $store_result) {
- $this->url = $url;
- $this->store_result = $store_result;
- $this->outfile = $out_file;
- }
- public function run() {
- // echo $this->url."\n";
- //delete non-printable characters
- $this->url = preg_replace('/[^\PC\s]/u', '', $this->url);
- $data = explode ( "|", $this->url );
- $vars = GetServerData ( $url );
- $this->store_result->AddTotal ();
- //print_r($vars);
- if ($vars) {
- $this->store_result->AddValid ();
- $str = "";
- $n_ar = array ();
- foreach ( $vars as $k => $v ) {
- if ($v != "")
- $v = trim ( $v );
- $n_ar [$k] = $v;
- }
- // print_r($vars);
- $vars = $n_ar;
- //
- // $str = implode ( ",", $vars );
- $str .= "\r\n";
- $fp = fopen ( $this->outfile, "a" ) or exit ( "Can't open " . $this->outfile . "!\n" );
- if (flock ( $fp, LOCK_EX )) {
- // fputs ( $fp, $str );
- fputcsv ( $fp, $vars, ",", "'" );
- fflush ( $fp );
- flock ( $fp, LOCK_UN );
- } else {
- echo "Can't flock() " . $this->outfile . "!\n";
- // $this->setGarbage ();
- }
- fclose($fp);
- $this->result = true;
- } else {
- //echo $this->outfile."\n";
- $path = realpath(dirname($this->outfile));
- //echo $path."\n";
- $fp = fopen($path."\\".$this->not_valid_file, "a");
- if( flock ( $fp, LOCK_EX ) )
- {
- fputs ( $fp, trim($this->url)."\n");
- fflush ( $fp );
- flock ( $fp, LOCK_UN );
- }
- fclose($fp);
- }
- }
- }
- class schedule_thread extends Thread {
- private $url;
- public $result = false;
- private $store_result;
- private $in_data;
- private $DB;
- function __construct($data, $db_data, StoreResult $store_result) {
- $this->store_result = $store_result;
- $this->in_data = $data;
- $this->DB = $db_data;
- }
- public function run() {
- // echo $this->url."\n";
- // print_r($this->in_data);
- $url = $this->in_data ["url_value"];
- $vars = GetServerData ( $url );
- $this->store_result->AddTotal ();
- // print_r($vars);
- $DB = $this->DB;
- $d = $this->in_data;
- // print_r($d);
- // print_r($DB);
- if ($vars) {
- // echo "$url valid!\n";
- $this->store_result->AddValid ();
- $mysqli_obj = CreateMysqli ( $DB ['host'], $DB ['user'], $DB ['password'], $DB ['database'] );
- if ($mysqli_obj == FALSE) {
- $error_info = sprintf ( "('%d') %s ", mysqli_connect_errno (), mysqli_connect_error () );
- error_msg ( "MySQL connection error!", $error_info );
- return;
- }
- $query = sprintf ( UPDATE QUERY );
- //echo $query;
- // var_dump($mysqli_obj);
- $res = run_query ( $mysqli_obj, $query );
- // var_dump($res);
- if (! $res) {
- echo "Error updating record in url_data table!\n";
- $mysqli_obj->close ();
- return;
- }
- $mysqli_obj->close ();
- $this->result = true;
- } else {
- echo "not valid!\n";
- if (curlPost ( "", "", true )) {
- // echo "Okay";
- $mysqli_obj = CreateMysqli ( $DB ['host'], $DB ['user'], $DB ['password'], $DB ['database'] );
- if ($mysqli_obj == FALSE) {
- $error_info = sprintf ( "('%d') %s ", mysqli_connect_errno (), mysqli_connect_error () );
- error_msg ( "MySQL connection error!", $error_info );
- return;
- }
- $query = sprintf ( INSERT QUERY )
- // echo $query;
- // var_dump($mysqli_obj);
- $res = run_query ( $mysqli_obj, $query );
- // var_dump($res);
- if (! $res) {
- echo "Error adding record to temp table!\n";
- $mysqli_obj->close ();
- return;
- }
- $query = sprintf ( DELETE QUERY );
- $res = run_query ( $mysqli_obj, $query );
- if (! $res) {
- echo "Can't delete record from main table!\n";
- $mysqli_obj->close ();
- return;
- }
- $mysqli_obj->close ();
- } else {
- echo "No network connection";
- }
- }
- }
- }
- ?>
- /////////////////////////////////////
- $data = file ( $data_list );
- $workers = [ ];
- $t_count = 80;
- $flag = 1;
- $k = 0;
- $perc_diff = 1;
- $last_percent = 0;
- // echo "$data_size";
- $connected = true;
- $s_time = microtime ( TRUE );
- while ( $flag === 1 ) {
- /*
- * //echo "$k\n";
- * if($k >= $data_size)
- * {
- * //echo "111"; exit();
- * $flag = 0;
- * break 2;
- * }
- */
- if (curlPost ( "", "", true )) {
- // echo "Okay";
- } else {
- echo "No active network connection avaliable! Shutting down...\n";
- $flag = false;
- $c_w = count ( $workers );
- for($i = 0; $i < $c_w; $i ++) {
- if ($workers [$i]->join ()) {
- // echo "joining $i\n";
- unset ( $workers [$i] );
- }
- }
- $connected = false;
- break;
- }
- $c_w = count ( $workers );
- if ($c_w < $t_count) {
- for($i = $c_w; $i < $t_count - $c_w; $i ++) {
- if ($k >= $data_size) {
- $flag = 0;
- break;
- }
- $workers [$i] = new my_thread ( $data [$k], $out_file, $result );
- // echo $data[$k]."\n";
- $workers [$i]->start ();
- $k ++;
- }
- }
- $c_w = count ( $workers );
- for($i = 0; $i < $c_w; $i ++) {
- if ($workers [$i]->join ()) {
- // echo "joining $i\n";
- unset ( $workers [$i] );
- }
- }
- $arr = $result->GetResults ();
- $valid = $arr [1];
- $e_time = microtime ( TRUE );
- $t = $e_time - $s_time;
- $percent = ($k / $data_size) * 100;
- if (($percent - $last_percent) >= $perc_diff || $percent == 100) {
- $last_percent = $percent;
- SetStatus ( $mysqli_obj, $DB ['worker_table'], 1, "Working", ( int ) $percent, "" );
- echo "progress: " . $percent . " percent ($k of $data_size, valid: $valid) \n";
- echo "time: " . $t . "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement