Commit 02d02997 by san58

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

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