2021-02-21 16:37:27 +00:00
< ? php
require __DIR__ . '/vendor/autoload.php' ;
require __DIR__ . '/config.php' ;
$pdo = ( new App\DB ( $options )) -> pdo ;
$util = new App\Utils ;
$error = " " ;
$aContext = array (
'http' => array (
'method' => 'GET' ,
'proxy' => $options [ 'http_proxy' ],
'user_agent' => 'MYOB/6.66 (AN/ON)' ,
'request_fulluri' => true ,
'timeout' => 120.0 ,
),
);
2021-02-23 12:08:49 +00:00
$STH = $pdo -> query ( " SELECT `name`, `url`, `modified`, `etag` FROM `subscriptions` WHERE `active` = 1 " );
2021-02-21 16:37:27 +00:00
$lists = $STH -> fetchAll ( PDO :: FETCH_ASSOC );
foreach ( $lists as $list ) {
2021-02-23 12:08:49 +00:00
echo " Processing " . $list [ 'name' ] . " subscription... " ;
if ( ! empty ( $list [ 'modified' ]))
$aContext [ 'http' ][ 'header' ] = 'If-Modified-Since: ' . $list [ 'modified' ] . '\r\n' ;
2021-02-21 16:37:27 +00:00
if ( ! empty ( $list [ 'etag' ]))
$aContext [ 'http' ][ 'header' ] = 'If-None-Match: ' . $list [ 'etag' ] . '\r\n' ;
$cxContext = stream_context_create ( $aContext );
$f = fopen ( $list [ 'url' ], " r " , false , $cxContext );
if ( $f ) {
$f_meta = stream_get_meta_data ( $f );
if ( strpos ( $f_meta [ 'wrapper_data' ][ 0 ], " 200 " ) === false ) {
2021-02-23 12:08:49 +00:00
echo " no changes. " . PHP_EOL ;
2021-02-21 16:37:27 +00:00
continue ;
}
2021-02-23 12:08:49 +00:00
$lastmod = $util -> getResponseHeader ( " Last-Modified " , $f_meta [ 'wrapper_data' ]);
$etag = $util -> getResponseHeader ( " Etag " , $f_meta [ 'wrapper_data' ]);
2021-02-21 16:37:27 +00:00
2021-02-23 12:08:49 +00:00
if ( ! empty ( $lastmod ) || ! empty ( $etag )) {
$pdo -> exec ( " UPDATE `subscriptions` SET " . ( ! empty ( $lastmod ) ? ( " `modified` = ' " . $lastmod . " ' " ) : " " ) . ( ! empty ( $etag ) ? ( " `etag` = ' " . $etag . " ' " ) : " " ) . " WHERE `name` = ' " . $list [ 'name' ] . " ' " );
2021-02-21 16:37:27 +00:00
}
2021-02-23 12:08:49 +00:00
// reset line
echo " fetching updated list. " . PHP_EOL ;
2021-02-21 16:37:27 +00:00
while (( $buffer = fgets ( $f , 4096 )) !== false ) {
$domain = " " ;
$record = $util -> parseHostRecord ( $buffer );
if ( ! $util -> isValidAddress ( $record [ 'host' ], $error )) {
echo " Error while validating " . $record [ 'host' ] . " : " . $error . PHP_EOL ;
continue ;
} else {
if ( $util -> isPunycodeDomain ( $record [ 'host' ])) {
$domain = idn_to_utf8 ( $record [ 'host' ]);
} else {
$domain = $record [ 'host' ];
}
}
if ( ! $util -> isValidBase64 ( $record [ 'b64' ])) {
continue ;
}
if (( isset ( $record [ 'commands' ]) && isset ( $record [ 'commands' ][ 'action' ]) && $record [ 'commands' ][ 'action' ] == " adddest " && isset ( $record [ 'commands' ][ 'olddest' ])) &&
$pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $domain . " ' AND `base64` = ' " . $record [ 'commands' ][ 'olddest' ] . " ' LIMIT 1 " ) -> fetchColumn ()) {
$base32 = $util -> b32from64 ( $record [ 'b64' ]);
$pdo -> exec ( " UPDATE `hosts` SET `base64` = ' " . $record [ " b64 " ] . " ', `base32` = ' " . $base32 . " ' WHERE `host` = ' " . $domain . " ' " );
echo " Processed " . $domain . " (adddest) " . PHP_EOL ;
continue ;
}
if (( isset ( $record [ 'commands' ]) && isset ( $record [ 'commands' ][ 'action' ]) && $record [ 'commands' ][ 'action' ] == " addsubdomain " && isset ( $record [ 'commands' ][ 'oldname' ]) && isset ( $record [ 'commands' ][ 'olddest' ])) &&
( $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $record [ 'commands' ][ 'oldname' ] . " ' AND `base64` = ' " . $record [ 'commands' ][ 'olddest' ] . " ' LIMIT 1 " ) -> fetchColumn () &&
! $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $domain . " ' " ) -> fetchColumn ())) {
$base32 = $util -> b32from64 ( $record [ 'b64' ]);
$pdo -> exec ( " INSERT INTO `hosts` (`host`, `base64`, `base32`, `approved`) VALUES (' " . $domain . " ', ' " . $record [ " b64 " ] . " ', ' " . $base32 . " ', 1) " );
echo " Processed " . $domain . " (addsubdomain) " . PHP_EOL ;
continue ;
}
if ( isset ( $record [ 'commands' ]) && ! $pdo -> query ( " SELECT COUNT(*) FROM `hosts` WHERE `host` = ' " . $domain . " ' LIMIT 1 " ) -> fetchColumn ()) {
$base32 = $util -> b32from64 ( $record [ 'b64' ]);
$pdo -> exec ( " INSERT INTO `hosts` (`host`, `base64`, `base32`, `approved`) VALUES (' " . $domain . " ', ' " . $record [ " b64 " ] . " ', ' " . $base32 . " ', 1) " );
echo " Processed " . $domain . PHP_EOL ;
continue ;
}
}
if ( ! feof ( $f )) {
echo " Error: fgets() ended earlier than needed. " . PHP_EOL ;
}
fclose ( $f );
} else {
echo " Empty response while fetching update from " . $list [ 'name' ] . " . " . PHP_EOL ;
}
}
$pdo = null ;