Skip to content

Instantly share code, notes, and snippets.

@aels
Created July 12, 2022 22:28
Show Gist options
  • Save aels/1c102c708f967cf8571352c8bfe88481 to your computer and use it in GitHub Desktop.
Save aels/1c102c708f967cf8571352c8bfe88481 to your computer and use it in GitHub Desktop.
<?php
if( empty($argv[1]) ) {
die("Usage:\nphp {$argv[0]} file");
}
function red($str) {
return "\033[91m".$str."\033[0m";
}
function orange($str) {
return "\033[93m".$str."\033[0m";
}
function green($str) {
return "\033[92m".$str."\033[0m";
}
function get_top_host($str) {
$str_arr = explode('.', $str);
return implode('.', array_slice($str_arr, strlen(array_slice($str_arr, -2, 1)[0])<4?-3:-2));
}
$filename = $argv[1];
$filename_ext = @end(explode('.', $filename));
$filename_titled = str_replace('.'.$filename_ext, '_titled.'.$filename_ext, $filename);
$lines = file($filename);
$goods = array();
for( $i=$argv[2]??0;$i<count($lines);$i++ ) {
echo $i.": ";
$line = trim($lines[$i]);
$host = explode('/', $line)[2];
if( !isset($goods[$host]) ) {
$ctx = stream_context_create(array('http'=>array('timeout'=>1),'ftp'=>array('timeout'=>1)));
$page_html = preg_match("/^http/i", $line)?@file_get_contents($line, false, $ctx):'';
preg_match("/<title>([^<]+)<\/title>/i", $page_html, $matches);
$title = trim($matches[1]??'');
$goods[$host] = $title;
}
else {
$title = $goods[$host];
}
echo $line.(empty($title)?'':", ".green('[ '.$title.' ]'))."\n";
file_put_contents($filename_titled, $line.(empty($title)?'':", [ ".$title.' ]')."\n", FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment