<?php

include "../common.inc.php";
$profile = new SiteProfile($conf);
// this should not be able to run for a long time ... just in case
@set_time_limit(10);

function GetTitle($url) {
    global $profile,$db;
    $q = $db->Execute("Select title from {$profile->tablename_urls} where url=".$db->Quote($url));
    $data = $q->FetchRow();    
    return strip_tags($data['title']);
}

function UpdateTitle($url) {
	global $profile,$db;
	$doc = new DOMDocument();
    @$doc->loadHTML( file_get_contents( "https://".$profile->confdomain.$url ) );
    $xpt = new DOMXPath( $doc );
    $output = $xpt->query("//title")->item(0)->nodeValue;
    $output = strip_tags($output);
    $db->Execute("update {$profile->tablename_urls} set title=".$db->Quote($output)." where url=".$db->Quote($url));
}

if (!empty($_REQUEST['url'])) {
    echo GetTitle($_REQUEST['url']);    
}

if (!empty($_REQUEST['update'])) {
    echo UpdateTitle($_REQUEST['update']);
}

?>
