<?php
function is404() {
    header('HTTP/1.1 404 Not Found', TRUE, 404);
    echo '404 - Page Not Found';
    exit();
}

$path = $_GET['path_text'];
if ($path) {
    $title = '';
    $subtitle = '';
    $htmltitle = '';
    $adtext = '';
    $adwidth = '';
    $imageurl = '';
    $imagealt = '';
    $aid = 0;
    $body = '';
    $metakeywords = '';
    $metadescription = '';

    require_once('TransManager.php');
    $trans = null;

    try {
        $trans = TransManager::getInstance();
        $trans->beginTrans();
        $pdo = $trans->getPDO();

        $stmt = $pdo->prepare('SELECT article_id, title, htmltitle, subtitle, adtext, ad_width, image_url, image_alt, meta_keywords, meta_description FROM article WHERE path_text = ?');
        $stmt->bindValue(1, $path);
        $stmt->execute();

        if ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
            $aid = $myrow[0];
            $title = $myrow[1];
            $htmltitle = $myrow[2];
            $subtitle = $myrow[3];
            $adtext = $myrow[4];
            $adwidth = $myrow[5];
            $imageurl = $myrow[6];
            $imagealt = $myrow[7];
            $metakeywords = $myrow[8];
            $metadescription = $myrow[9];
        }
        else
            is404();

?>
<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title><?= $htmltitle ?></title>
<?php
     if ($metadescription)
         echo '<meta name="description" content="'. $metadescription .'">';
     if ($metakeywords)
         echo '<meta name="keywords" content="'. $metakeywords .'">';
?>
  <link rel="stylesheet" href="/master.min.css">
  <link rel="icon" href="/img/favico.gif" sizes="16x16" type="image/gif">
  <!--[if lt IE 9]>
  <script>var e=("article,footer,header,nav").split(',');for(var i=0;i<e.length;i++)document.createElement(e[i]);</script>
  <![endif]-->
</head>
<body>
<div id="contentArea">
  <h1><?= $title ?></h1>

  <div id="aContainer">

    <div id="jj1">
<script type="text/javascript"><!--
google_ad_client = "pub-7795630243408100";
/* 10spot 728x90, created 11/23/10 */
google_ad_slot = "7324398833";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
    </div>


<?php
        flush();
        $count = 0;
        $stmt = $pdo->prepare('SELECT content, tag, paragraph_id FROM paragraph WHERE article_id = ? ORDER BY paragraph_id ASC');
        $stmt->bindValue(1, $aid);
        $stmt->execute();
        
        while ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
            if ($count++ == 9 && $adtext)
                $body .= '<div class="adincl" style="width: '. $adwidth .'px;">'. $adtext .'</div>'. "\r\n";
            $body .= '<'. $myrow[1] .'>'. $myrow[0] .'</'. $myrow[1] .'>' ."\r\n";
        }
        
        $links = '';
        $latest = '';
        $stmt = $pdo->prepare('SELECT path_text, title, article_id, SUBSTRING(blurb, 1, 84), sm_image_url, image_alt FROM article WHERE status = \'A\' AND article_id != ? ORDER BY article_id DESC LIMIT 5');
        $stmt->bindValue(1, $aid);
        $stmt->execute();
        
        while ($myrow = $stmt->fetch(PDO::FETCH_NUM)) {
            if (!$latest)
                $latest = '<a href="/articles/'. $myrow[0] .'.html">'. $myrow[1] .'</a>'. "\r\n";
            if ($myrow[4])
                $links .= '<li><img src="'. $myrow[4] .'" alt="'. $myrow[5] .'">';
            $blurb = $myrow[3];
            $links .= '<a href="/articles/'. $myrow[0] .'.html">'. $myrow[1] .'</a>'. substr($blurb, 0, strrpos($blurb, ' ')) ."...</li>\r\n";
        }
        
?>

    <article>
    <?php
     if ($imageurl)
         echo '<img src="'. $imageurl .'" class="article" alt="'. $imagealt .'">'. "\r\n";
?>
      <h2><?= $subtitle ?></h2>
      <?= $body ?>
      <p><b>Latest Article at 10Spot.net:</b> <?= $latest ?></p>

    </article>

    <div id="jj2">
<script type="text/javascript"><!--
google_ad_client = "pub-7795630243408100";
/* 10spot 728x90, created 11/23/10 */
google_ad_slot = "7324398833";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
    </div>
  </div>

  <nav>
    <div class="navhead navheadTop">
      Sponsored Links
    </div>
    <div id="invbox">
<script type="text/javascript"><!--
google_ad_client = "pub-7795630243408100";
/* 10spot 250x250, created 9/29/11 */
google_ad_slot = "0024254696";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
    </div>

    <div class="navhead">
      Navigation
    </div>
    <ul class="box">
      <li>
        <a href="/">10 Spot Home</a>
      </li>
      <li>
        <a href="/lowcostliving/">Low Cost Living Blog</a>
      </li>
    </ul>

    <div class="navhead">
      Latest Articles
    </div>

    <ul id="latest">
      <?= $links ?>
    </ul>
  </nav>

  <div class="clr"></div>

  <footer>
    <h2>Liked It? Link It!</h2>
    <p>Bloggers, I welcome your links.  This article may be of interest to your readers.
    However I have to ask, no wholesale copying of my articles!</p>

    <div id="copyr" class="flr">
      <p>This article is Copyright <?= date('Y') ?>, 10Spot.net 
        <img src="/img/copyscap.png" alt="Protected by Copyscape">
      </p>
    </div>

    <p class="fll">View the 10Spot <a href="/privacy.html" rel="nofollow">Privacy Policy</a></p>
    <div class="clr"></div>
  </footer>
</div>

  </body>
</html>
<?php
        //log this
        $referer = $_SERVER['HTTP_REFERER'];
        if($referer && substr($referer, 0, 7) == 'http://') {
            $searchval = 'NULL';
            if (stristr($referer, 'google.com/search?')) {
                $start = strpos($referer, '&q=');
                if ($start) {
                    $end = strpos($referer, '&', $start + 3);
                    if ($end)
                        $searchval = '\''. urldecode(substr($referer, $start + 3, $end)) .'\'';
                    else
                        $searchval = '\''. urldecode(substr($referer, $start + 3)) .'\'';
                }
            }
            else if (stristr($referer, 'search.msn.com/results')) {
                $start = strpos($referer, 'q=');
                if ($start) {
                    $end = strpos($referer, '&', $start + 2);
                    if ($end)
                        $searchval = '\''. urldecode(substr($referer, $start + 2, $end)) .'\'';
                    else
                        $searchval = '\''. urldecode(substr($referer, $start + 2)) .'\'';
                }
            }

            $stmt = $pdo->prepare('INSERT INTO referer (url, searchval, article_id, hit_date) VALUES (?, ?, ?, CURRENT_TIMESTAMP)');
            $stmt->bindValue(1, $referer);
            $stmt->bindValue(2, $searchval);
            $stmt->bindValue(3, $aid);
            $stmt->execute();
        }
        $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if (strpos($useragent, 'googlebot') || strpos($useragent, 'slurp') || strpos($useragent, 'msnbot')) {
            $stmt = $pdo->prepare('INSERT INTO referer (url, article_id, hit_date) VALUES (?, ?, CURRENT_TIMESTAMP)');
            $stmt->bindValue(1, $useragent);
            $stmt->bindValue(1, $aid);
            $stmt->execute();

        }

        $trans->commit(true);
    }
    catch (Exception $e) {
        if ($trans)
            $trans->rollback();
    }
}
else
    is404();
?>
