Skip to content

Instantly share code, notes, and snippets.

@AliceWonderMiscreations
Created June 29, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliceWonderMiscreations/6f3d0a331cc7ed1e4a6f to your computer and use it in GitHub Desktop.
Save AliceWonderMiscreations/6f3d0a331cc7ed1e4a6f to your computer and use it in GitHub Desktop.
RTALabel Checker
<?php
//needs more debug and error handle and stuff
//called by rtaCheckHeader()
function rtaCheckMeta($dom) {
$headList = $dom->getElementsByTagName('head');
if($headList->length > 0) {
$head = $headList->item(0);
$metaList = $head->getElementsByTagName('meta');
$j = $metaList->length;
for($i=0; $i<$j; $i++) {
$meta = $metaList->item($i);
if($meta->hasAttribute('name')) {
$name = strtoupper(trim($meta->getAttribute('name')));
if(strcmp($name, 'RATING') == 0) {
if($meta->hasAttribute('content')) {
$content = strtoupper(trim($meta->getAttribute('content')));
if(strcmp($content,'RTA-5042-1996-1400-1577-RTA') == 0) {
return TRUE;
}
}
}
}
}
}
return FALSE;
}
function rtaCheckHeader($link) {
$return = new stdClass();
$return->rta = FALSE;
$return->scheme = 'default';
$return->mime = 'foo/bar';
$return->httpcode = 0;
$parsed = parse_url($link);
if(isset($parsed['scheme'])) {
$return->scheme = strtolower(trim($parsed['scheme']));
}
if(isset($parsed['port'])) {
$port = 0 + $parsed['port'];
switch($return->scheme) {
case 'http':
if($port == 80) {
$port = 0;
}
break;
case 'https':
if($port == 443) {
$port = 0;
}
break;
case 'ftp':
if($port = 21) {
$port = 0;
}
} //end case
if($port > 0) {
$return->port = $port;
}
}
if(isset($parsed['host'])) {
$host = $parsed['host'];
if(function_exists('idn_to_ascii')) {
$return->host = trim(strtolower(idn_to_ascii($host)));
} else {
$return->host = trim(strtolower($host));
}
}
if(isset($parsed['path'])) {
$return->path = trim($parsed['path']);
} else {
$return->path = '/';
}
if(isset($parsed['query'])) {
$return->query = $parsed['query'];
}
$hypertext = FALSE;
if(strcmp($return->scheme, 'http')) {
$hypertext = TRUE;
} elseif(strcmp($return->scheme, 'https')) {
$hypertext = TRUE;
}
if(! isset($return->host)) {
$hypertext == FALSE;
}
if($hypertext) {
$link = $return->scheme . '://' . $return->host;
if(isset($return->port)) {
$link .= ':' . $return->port;
}
$link .= $return->path;
if(isset($return->query)) {
$link .= '?' . $return->query;
}
if(filter_var($link, FILTER_VALIDATE_URL)) {
$return->link = $link;
}
}
if(isset($return->link)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $return->link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Godzilla/5.0 (AmigaOS 3.1; OOH64) BananaSplit/1337.69 (KSGML, like Anguid) Aluminum/43.7.2774.666 Party/445.22');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, true);
$data = explode("\n", trim(curl_exec($ch)));
$j = count($data);
if($j > 0) {
$response = preg_replace('/\s\s+/', ' ', trim($data[0]));
//fixme check for integer
$return->httpcode = 0 + explode(' ', $response)[1];
for($i=0; $i<$j; $i++) {
$data[$i] = preg_replace('/\s\s+/', ' ', trim($data[$i]));
//$string = strtoupper($data[$i]);
if(strcmp(strtoupper($data[$i]), 'RATING: RTA-5042-1996-1400-1577-RTA') == 0) {
$return->rta = TRUE;
}
$substring = substr(strtoupper($data[$i]), 0, 13);
if(strcmp($substring, 'CONTENT-TYPE:') == 0) {
$return->mime = preg_replace('/;/', '', strtoupper(explode(' ', $data[$i])[1]));
}
}
if(! $return->rta) {
$scrape = FALSE;
if($return->httpcode = 200) {
switch($return->mime) {
case 'TEXT/HTML':
$scrape = TRUE;
break;
case 'APPLICATION/XHTML+XML':
$scrape = TRUE;
break;
} //end switch
if($scrape) {
$return->foo = 'bar';
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, false);
$data = trim(curl_exec($ch));
$config = array('output-xhtml' => true);
$tidy = new tidy;
$tidy->parseString($data, $config, 'utf8');
$tidy->cleanRepair();
$data = $tidy->value;
$dom = new DOMDocument("1.0", "UTF-8");
$dom->loadHTML($data);
$return->rta = rtaCheckMeta($dom);
}
}
}
}
//var_dump($data);
curl_close($ch);
}
//var_dump($parsed);
//var_dump($return);
return $return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment