<?php
/**
 * World Gloominess (v0.2)
 * by Zaim Bakar, zaimlah@yahoo.com
 * http://zaim.frens.net/
 *
 * This script is released as public domain.
 */
 
 
# show source?
if (isset($_GET['src'])) {
    
highlight_file('index.php');
    die();
}


/** start script: **/

# what url to scrape
$q_url = isset($_GET['url']) ? trim($_GET['url']) : '';
if (
$q_url !== '' && !file_exists($q_url)) {
    
$gn false;
    
$gn_url $q_url;
} else {
    
$gn true;
    
$gn_url 'http://news.google.com/news?ned=tus';
}

# open google news
$gn_text file_get_contents($gn_url);

# strip tags
$gn_text strip_tags($gn_text);

# load bad words
$wordsets file('words.txt');
$words = array();
foreach (
$wordsets as $wordset) {
    
$wordset trim($wordset);
    
$wl explode(','$wordset);
    
$words[$wl[0]] = $wl;
}

# find words
$found = array();
$total 0;
foreach (
$words as $w) {
    
$c 0;
    foreach (
$w as $f) {
        
$F ucfirst($f);
        
$c $c substr_count($gn_text"$f ");
        
$c $c substr_count($gn_text"$F ");
    }
    
$found[$w[0]] = $c;
    
$total $total $c;
}
arsort($found);
reset($found);

$gn_title = ($gn==true?'google news':$gn_url);
$gn_link  "<a href=\"$gn_url\" title=\"$gn_title\">$gn_title</a>";
?>
<!DOCTYPE html
 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>World Gloominess</title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
    <style type="text/css">
    body { margin: 0; font-family:sans-serif }
    a { color: #444 }
    #title { position:absolute; padding:1px; background-color: #eee; color: #777; font-size: 9pt }
    #words { text-align: center; padding-top:1em }
    .word a { text-decoration: none }
    .word a:hover { background-color: #f5f5f5 }
    .count { font-size: 9pt; vertical-align: top }
    </style>
</head>

<body>

<div id="title">world gloominess as gathered by <?=$gn_link?></div>

<div id="words">
<!--word list-->
<?
if ($total 0) {
    foreach (
$found as $w=>$c) {
        if (
$c 0) {
            
$fsize $c 10;  // font size
            
$lineh $fsize - ($fsize 4);  // line height
            
$title implode(','$words[$w]);  // link title
            
echo "\n<div class=\"word\">\n";
            echo 
"<a style=\"font-size:{$fsize}pt; line-height:{$lineh}pt\" ";
            echo 
"href=\"$gn_url\" title=\"$title\">$w</a>\n";
            echo 
"<span class=\"count\">$c</span>\n";
            echo 
"</div>\n";
        }
    }
} else {
    echo 
"<p>Congratulations! $gn_link is gloom free.</p>\n";
}
?>

<!--end word list-->
</div>

</body>
</html>