Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Alto
/
rkn
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 02d02997
authored
2021-06-02 18:48:41 +0300
by
san58
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
переработал парс csv
1 parent
d8a8ed2c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
49 deletions
commands/ScanController.php
components/Collection.php
commands/ScanController.php
View file @
02d0299
...
...
@@ -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
(
'{{%host}}'
,
[
'csv_date'
=>
$csv_date
],
[
'domain'
=>
$host_utf8
]
);
else
$res
->
insert
(
'{{%host}}'
,
[
'domain'
=>
$host_utf8
,
'created_at'
=>
new
Expression
(
'NOW()'
),
'csv_date'
=>
$csv_date
,
]
);
$res
->
execute
();
}
$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_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
components/Collection.php
View file @
02d0299
...
...
@@ -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
);
$
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
);
}
}
else
{
$date
=
$data
[
0
];
}
$row
++
;
unset
(
$file_handle
);
}
fclose
(
$handle
);
}
if
(
$unique
&&
$hosts
)
{
$hosts
=
array_unique
(
$hosts
);
}
return
[
'csv_date'
=>
str_replace
(
'Updated: '
,
''
,
$date
),
'data'
=>
$hosts
];
unset
(
$temp_csv_name
);
return
$out
;
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment