Commit 02d02997 by san58

переработал парс csv

1 parent d8a8ed2c
...@@ -39,34 +39,77 @@ class ScanController extends Controller ...@@ -39,34 +39,77 @@ class ScanController extends Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
$hosts = Collection::getHostsByCsv(Yii::$app->params['csv_url']); $rows = 0;
$csv_date = date("Y-m-d H:i:s",strtotime($hosts['csv_date'])); $csv_file = Collection::download_file(Yii::$app->params['csv_url']);
foreach ($hosts['data'] as $host) if ($csv_file!=='')
{ {
$host_utf8 = utf8_encode($host); $csv_handle = fopen($csv_file ,'r');
$res = Yii::$app->db->createCommand(); if ($csv_handle !== false)
if ( Host::find()->where(['domain' =>$host_utf8])->count() ) {
$res->update( $csv_row = 0;
'{{%host}}', $csv_datatime = '';
[ while (($csv_data = fgetcsv($csv_handle, 1024, ';')) !== false)
'csv_date' => $csv_date], {
[ $csv_row++;
'domain' => $host_utf8 if ($csv_row===1)
] $csv_datatime = date(
); "Y-m-d H:i:s",
else strtotime(
$res->insert( str_replace('Updated: ', '', $csv_data[0])
'{{%host}}', )
[ );
'domain' =>$host_utf8, elseif (isset($csv_data[1])===true)
'created_at' => new Expression('NOW()'), {
'csv_date' => $csv_date, $host = str_replace(
] '*.',
); '',
$res->execute(); utf8_encode($csv_data[1])
} );
$host_level = count( explode('.', $host) );
if ($host_level === 2)
{
$db_request = Yii::$app->db->createCommand();
if ($db_request)
{
if ( Host::find()->where(['domain' =>$host])->count()>0 )
$db_request->update(
'{{%host}}',
[
'csv_date' => $csv_datatime],
[
'domain' => $host
]
);
else
$db_request->insert(
'{{%host}}',
[
'domain' =>$host,
'created_at' => new Expression('NOW()'),
'csv_date' => $csv_datatime,
]
);
$db_request->execute();
$rows++;
}
unset($db_request);
}
echo "\n".$this->ansiFormat('Количество полученных хостов', Console::FG_CYAN) . ' => ' . $this->ansiFormat(count($hosts['data']), Console::BOLD, Console::FG_GREEN) . "\n"; unset($host_level);
unset($host);
}
}
unset($csv_data);
unset($csv_datatime);
unset($csv_row);
fclose($csv_handle);
unlink($csv_file);
}
unset($csv_handle);
}
unset($csv_file);
echo "\n".$this->ansiFormat('Count of hosts processed: ', Console::FG_CYAN) . ' => ' . $this->ansiFormat($rows, Console::BOLD, Console::FG_GREEN) . "\n";
unset($rows);
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -7,32 +7,25 @@ use yii\base\Component; ...@@ -7,32 +7,25 @@ use yii\base\Component;
class Collection extends Component class Collection extends Component
{ {
public static function getHostsByCsv($csvFile, $separator = ";", $seporatorHosts = '|', $pos = 1, $unique = true) //Скачиваине URL файла и передача пути к нему
public static function download_file($csvFile)
{ {
$date = ''; $out = '';
$hosts = []; $temp_csv_name = tempnam(sys_get_temp_dir(), 'rkn_');
$row = 1; if ($temp_csv_name !== false)
if (($handle = fopen($csvFile, "r")) !== FALSE) { {
while (($data = fgetcsv($handle, 1000, $separator)) !== FALSE) { $file_handle = fopen((string)$csvFile, 'r');
if ($row > 1) { if ($file_handle!==false)
$hostsString = $data[$pos] ?? ''; {
if ($hostsString) { $file_write = file_put_contents($temp_csv_name, $file_handle);
//$arr = array_map('trim', explode($seporatorHosts, $hostsString)); if ($file_write!==false)
//$hosts = array_merge($hosts, $arr); $out = $temp_csv_name;
$hosts[] = str_replace('*.', '', $hostsString); unset($file_write);
} }
} else { unset($file_handle);
$date = $data[0];
}
$row++;
} }
fclose($handle); unset($temp_csv_name);
} return $out;
if ($unique && $hosts) {
$hosts = array_unique($hosts);
}
return ['csv_date' => str_replace('Updated: ', '', $date), 'data' => $hosts];
} }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!