Home

LeSinge.org source code

So this script sucks in several ways, but I'm still glad I finally implemented the nested, threaded, recursive comment thing.

Let's see: sensible mod_rewrite urls, searchable archive, threaded commenting with live (and yes, yoinked) JS preview, a unique (and to my mind very effective) story/comment layout and even some rudimentary spam protection... the code may be a little hairy in spots, but this is as good as my site has ever been. I like it. Screw you.

To do:

<?
if(!$_GET['test']){
//    header("Location: holder.html");
}
include(
"diary/db.inc");
/**** Functions ***/
function isblank($var){
    return 
$var == "" || $var == "__";
}

function 
spit($cid,$level){ // "spits out" the appropriate comment from the array; $level isn't used here because I'm using <ul>'s standard indent instead; it's just an int to track how big the "left" CSS attribute of an absolutely positioned div would be, if one were using absolutely positioned divs, youknowlike.
        
global $c//array of comments for current entry, created in makediv() whence this function is called
        //find the row
        
for ($i=0;$i<=sizeof($c);$i++){
            if(
$c[$i]['cid']==$cid and $c[$i]['isspam'] != 1){
                
$poster $c[$i]['poster'];
                
$url $c[$i]['url'];    
                if(
strlen($url) > 3){
                    if(
substr($url,0,7) != "http://"){
                        
$url "http://".$url;
                    }
                    
$nameandurl "<a href='".$url."'>".$poster."</a>";
                }else{
                    
$nameandurl $poster;
                }
                            
$out $c[$i];
                            echo 
"<li>\n\t";
                echo 
"<div class=comment>\n\t";
                            echo 
"<span class=chead><b>".$nameandurl."</b> 
said:</span><p>"
.str_replace("\n","<br>",stripslashes(strip_tags($out['comment'],"<strike><pre><a><b><i><img><code><ul><ol><li>")));
                            echo 
"</p><p align=right>";
                echo 
"<small>[".$out['date']." GMT]</small> ";
                echo 
"<nobr>[<a href=\"http://lesinge.org/commentform.php?entry=".$out['eid']."&replyto=".$out['cid']."\">Reply to this comment</a>]</nobr>";    
                echo 
"</p></div></li><br>\n\n";
            }
        }
}

function 
get_kids($pcid,$level){ // find each comment with this parent comment id; for each, spit and get their kids
        
global $c//array of comments for current entry, created in makediv() whence this function is called
        
echo "<ul class='comm'>\n\t";
        for (
$i=0;$i<=sizeof($c);$i++){
                if(
$c[$i]['pcid']==$pcid){ // found one!
                        
spit($c[$i]['cid'],($level+1));
                        
get_kids($c[$i]['cid'],($level+1)); //recursion! are you watching, Hugh Gibbons and Carl Vogel?
                
}
        }
        echo 
"</ul>";
}

function 
makediv($id,$vis){
        global 
$db;

        echo 
"<div class='listing' id='listing_$id'>"//they're not really "listings", are they? whatever :)

    
if(is_numeric($id)){

        
/* print diary entry */
            
$entry $db->GetRow("select * from diary where id = '$id'");
        
$when parsedate($entry['created']);

        echo 
"<b><big>".stripslashes($entry['title'])."</big> - ".$when[2]."/".$when[1]."/".$when[0]." - ".$when[3].":".$when[4]." GMT</b>";
        echo 
" - <small>[<a href=/".$when[0]."/".$when[1]."/".$when[2]."/".mktitle($entry['title']).".html>Link</a>]</small><br><br>";
        echo 
stripslashes($entry['text']);
        echo 
"<br><br>";

        
/* retrieve and print comments by thread */
        
$getcommq "select * from comments where eid='$id' order by eid";
        global 
$c;
        
$c $db->GetAll($getcommq);
    
        echo 
"<ul class='comm'>";
        for (
$i=0;$i<sizeof($c);$i++){
                if(
notset($c[$i]['pcid'])){
                        
spit($c[$i]['cid'],0); //comment ID and tree level
                        
get_kids($c[$i]['cid'],0); //what comments have this comment id as their parent comment id?
                
}
        }
        echo 
"</ul>";
        echo 
"<br>[<a href=/commentform.php?entry=".$id.">Post new comment</a>]";

    }else{
        
//it's "About" or "Exit" or whatever
        
include("$id.php");
    }
        echo 
"</div>";
    echo 
"\n\n";

}


function 
parsedate($t){ // splits a timestamp into a manageable array
    //2007-11-06 17:21:59
        
$yr substr($t,0,4);
        
$mh substr($t,5,2);
        
$dy substr($t,8,2);
        
$hr substr($t,11,2);
        
$mn substr($t,14,2);
        
$sc substr($t,17,2);
    return array(
$yr,$mh,$dy,$hr,$mn,$sc);
}


function 
addzero($val){ // guess what this does...
    
if (strlen($val) == 1){
        
$val "0".$val;
    }
    return 
$val;
}


/*** Bye-bye, spammers! ***/

$no $db->GetRow("select * from shitlist where ip like '%".$HTTP_SERVER_VARS['HTTP_USER_AGENT']."%' or ip = '".gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR'])."'");
if (
sizeof($no) > 0){
    
header("Location: http://www.google.com/intl/xx-hacker/");
}


if(isset(
$_GET['date'])){ //The old site used to have this date variable passed in. I'd like Google to stop indexing these pages now. NOW!
    
$redir explode("-",$_GET['date']);
    
header("HTTP/1.1 404 File Not Found");
    
header("Location:http://lesinge.org/".$redir[0]."/".$redir[1]."/".$redir[2]);
}else if(isset(
$_GET['body'])){ 
/* same goes for 'body'. and to think this all started in the summer of 2000 when I was working for Havas Interactive and I wanted a temporary $body 
variable to show some amusing content on my then very static web page... ah me. */
    
header("HTTP/1.1 301 Moved Permanently");
    if(
$_GET['body']=="archive.inc" || $_GET['body']=="a3.php" ||$_GET['body']=="a2.inc"){
        
header("Location:http://lesinge.org/archive.php");
    }
    
header("Location:http://lesinge.org");
}else{ 
//...load the page as normal

/****** QUERIES & VARIABLES *****/

$show $_GET['show'];
if(!isset(
$show)){
    
$show 5;
}

if(
$_GET['searchby']=="d"){
/* I once had a notion of having a text field on the front page via which you could search; it never eventuated. */
/* there used to be year/month/day dropdowns on the front page. I considered having a second set to allow searches within a certain timespan, hence 
"fyr", "fmh" and "fdy", which would have been paired off with "tyr", "tmh" and "tdy" ("from" and "to", if you still haven't got it) */
        
if(strlen($_GET['fyr']) == 0){
                
$_GET['fyr']="____";
        }

        if(
strlen($_GET['fmh']) == 0){
                
$_GET['fmh']="__";
        }else{
                
$_GET['fmh'] = addzero($_GET['fmh']);
        }

        if(
strlen($_GET['fdy']) == 0){
                
$_GET['fdy']="__";
        }else{
                
$_GET['fdy'] = addzero($_GET['fdy']);
        }


        
$where "where created like '".$_GET['fyr']."-".$_GET['fmh']."-".$_GET['fdy']."%'";
        
$ascdesc="asc";
    
$limit "limit 31";
}else{
        
$lim "limit ".$show;
        
$ascdesc "desc";
}


$sql "select id, created, title from diary $where order by created $ascdesc $lim"//yup, http://lesinge.org?show=99 or whatever...
//echo $sql;
$rs $db->GetAll($sql); //I {heart} PEAR
//print_r($rs);

$hit $db->query("insert into hits values ('',
    '"
.date("Y-m-d-h-i-s")."',
    '"
.$HTTP_SERVER_VARS['REQUEST_URI']."',
    '"
.gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR'])."',
    '"
.$HTTP_SERVER_VARS['HTTP_REFERER']."',
    '"
.$HTTP_SERVER_VARS['HTTP_USER_AGENT']."'
    )"

or die(
mysql_error()); 


/* Title bar */
if(sizeof($rs)==1){ // single entry
    
$titlebar "diary entry: ".stripslashes($rs[0]['title']);
}else if((
$_GET['fyr'] != "__") && ($_GET['fmh'] != "__") && ($_GET['fdy'] == "__")){
    
$mname = array("January","February","March","April","May","June","July","August","September","October","November","December");
    
$mnum $_GET['fmh'];
//    $titlebar = $mnum;
    
$titlebar "archive: ".$mname[$mnum 1]." ".$_GET['fyr'];
}else{ 
// default
    
$titlebar "v3.3";
}

/* IDs */
global $ids//this is the separate list of switchable divs used in generating the JS display:none/display:block stuff for show2() later
$ids = array();

for(
$g=0;$g<sizeof($rs);$g++){
    
$ids[$g] = $rs[$g]['id'];
}

$static = array("About");

$ids array_merge($ids,$static); //tack on the static pages to the end of the list; they've all gotta be generated!

//print_r($ids);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LeSinge.org <? echo $titlebar?></title>
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 Feed for LeSinge.org" href="/rss.php" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 Feed for LeSinge.org with comment count" href="/rss.php?comm=true" />
<link rel="stylesheet" type="text/css" href="/style.css" />
<meta name="robots" content="index,follow" />
<meta name="rating" content="general" />
<meta name="revisit-after" content="2 days" />
<meta name="distribution" content="global" />
<meta name="coverage" content="worldwide" />
<?
if($_GET['showmethemonkey']=="true"){
    echo 
"<style>
        #wrapper{background:url(/gfx/monkey.jpg) top left no-repeat}
    </style>"
;
}
?>
<script type="text/javascript">

//Yeah, I finally took Aidan's code in http://lesinge.org/2006/08/02/When_yer_in_trouble_you_call_DW.html :)
//2007/05/03: Hey, guess what: IE6 and 7 don't like for(var in array) loops! This was broken for months and I never noticed. Sorry, IE people! Please come back now...
function switchTo(thisDiv){
  var ids = new Array();
  <?php
    
for($i=0;$i<sizeof($ids);$i++){
        echo 
"ids[$i] = \"listing_".$ids[$i]."\";\n";
    
//    echo "alert(ids[$i]);\n";
    
}
    
?>
  for (var i = 0;i<ids.length;i++) {
    //alert("listing_" + thisDiv + " = " + ids[i]);
    document.getElementById(ids[i]).style.display = ("listing_" + thisDiv == ids[i]) ? 'block' : 'none';
    document.getElementById(ids[i]).style.visibility = "visible";
  }
}
</script>
</head>
<?
if (isset($_GET['e'])){
    if(
array_search($_GET['e'],$ids)||is_numeric($_GET['e'])){
        echo 
"<body onload=\"switchTo('".$_GET['e']."')\">";
    }else{
        echo 
"<body><p align=center><blink><h1>".$_GET['e']."? No.</h1></blink></p>";
        
mail("diemotherfucker@lesinge.org","hack attempt - $REMOTE_ADDR","$REMOTE_ADDR\n\n$e");
    }
}else{
    echo 
"<body onload=\"switchTo('".$ids[0]."')\">";
}
?>
<div id="wrapper">
<div id="top">
    <h1><a href="/">LeSinge.org</a></h1>
    <div>
    <?
    $guff 
explode("\n",fread(fopen("guff.txt","r"),filesize("guff.txt")));
    
$index rand(0,(sizeof($guff)-1));
    echo 
$guff[$index];
    
?>
    </div>
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href='#' onclick="switchTo('About')">About</a></li>
        <li><a href='/archive.php'>Archive</a></li>
        <li><a href='/src.php'>Source</a></li>
        <li><a href='/gallery'>Gallery</a></li>
    </ul>
</div>

<div id="lbar">
    <h4>Latest entries</h4>
    <dl id='entries'>
    <?
    
for($i=0;$i+sizeof($static)<sizeof($ids);$i++){ //...and THAT'S the reason for the array_merge() up the top; keeps $ids[] nice and neat
        
$dm parsedate($rs[$i]['created']);

        
/* the JS to show the div... */
        
echo "
            <dt>"
.$dm[2]."/".$dm[1]."</dt>
            <dd>
                <a href=# name="
.$ids[$i]." onclick=\"switchTo('".$ids[$i]."')\">".stripslashes($rs[$i]['title'])."</a>
            </dd>
        "
;
    }
    
?>
    </dl>

<?


/* Next/Previous month buttons */
if(isset($_GET['fyr']) &&isset($_GET['fmh']) && isblank($_GET['fdy'])){
    
$tyr $_GET['fyr'];
    
$tmh $_GET['fmh'];
    if (
$tyr.addzero($tmh) < date("Ym")){
    if (
$tmh == 12 && $tyr <= date("Y")){
        
$nmh 1;
        
$nyr $tyr 1;
    }else{
        
$nyr $tyr;
        
$nmh $tmh 1;
    }
    echo 
"<a href=/$nyr/$nmh>Next month</a>";
    }

    if (
$tyr.addzero($tmh) > "200204"){
    echo 
" | ";
    if (
$tmh == "1" && $tyr >= 2002){
        
$pmh 12;
        
$pyr $tyr 1;
    }else{
        
$pyr $tyr;
        
$pmh $tmh 1;
    }
    echo 
"<a href=/$pyr/$pmh>Previous month</a>";
    }

}

/* This whole month */
if($_GET['fdy'] != "" and $_GET['fdy'] != "__"){
    echo 
"<a href=/".$_GET['fyr']."/".$_GET['fmh']."/>Entire month</a>";
}


/* Recent comments box */
if(strlen($HTTP_SERVER_VARS['QUERY_STRING'])<1){
    
/* begin new comments */

    
$sqlnc "select comments.*,diary.title, diary.id, diary.created
    from comments,diary where diary.id = comments.eid and comments.isspam != 1 order by comments.cid desc,comments.eid desc limit 5"
;
    
$rsnc $db->GetAll($sqlnc);

    echo 
"<h4 id='recent'>Recent comments</h4>
        <dl id='ncomm'>"
;

    for(
$i=0;$i<sizeof($rsnc);$i++){
        
//if this comment belongs to one of the five most recent entries, show appropriate div; otherwise, load separate page 
        
if(in_array($rsnc[$i]['eid'],$ids)){
            
$linkto "# onclick=\"switchTo('".$rsnc[$i]['eid']."')\"";
        }else{
            
$lt parsedate($rsnc[$i]['created']);
            
$linkto "/".$lt[0]."/".$lt[1]."/".$lt[2]."/".mktitle($rsnc[$i]['title']).".html";
        }
        echo 
"
        <dt>
            <strong>"
.$rsnc[$i]['poster']."</strong> re <a href=$linkto>".stripslashes($rsnc[$i]['title'])."</a>
        </dt>
        <dd>
            "
.stripslashes(substr(strip_tags($rsnc[$i]['comment']),0,100))."...
        </dd>
        "
;
        
    }
}
?>
</dl>
</div>

<?

for($i=0;$i+sizeof($static)<sizeof($ids);$i++){
    
/* Generate the entry divs */
    
makediv($ids[$i],"block");
}
foreach(
$static as $v){
    
/* Generate static divs */
    
makediv($v,"block");
}
reset($static);

?>
<!-- ukey="7C75F5E9" -->


<div id="rss">
        <!--
<h4>Current favourite things:</h4>
-->


    <!--<li>
        <a href="http://www.textfiles.com/humor/strine.txt">LET STALK STRINE</a>
        <br>
        <span>13/2/2008</span>
    </li>
    <li>
        <a href="http://www.sfgate.com/cgi-bin/article.cgi?f=/g/a/2008/02/06/notes020608.DTL">The machine-gun of capitalism</a>
        <br>
        <span>6/2/2008 via <a href="http://www.google.com/reader/shared/18373978344818481116/">RSS</a></span>
    </li>
    <li>
        <a href="http://greghoustondesign.com/demos/mocha/">MooTools Mocha UI v0.5</a>
        <br>
        <span>22/11/2007 via <a 
href="http://tigermouse.epsi.pl/">Mike</a></span>
    </li>
    <li>
        <a href="http://www.youtube.com/watch?v=S7GGkKpBR-g">The Electro Funk-Daddy Superstar Break</a>
        <br>
        <span>14/11/2007</span>
    </li>
</ul>-->
<h4 id='blogsam'>Blogsam &amp; Linksam</h4>
<?

echo "<ul>";
$res $db->GetAll("select * from pond order by id desc limit 15");
foreach(
$res as $r){
    
$pw = array();
        
$pw[0] = substr($r['date'],0,4);
        
$pw[1] = substr($r['date'],4,2);
        
$pw[2] = substr($r['date'],6,2);

    
//345120
    
if($r['via']!=""){    
        
$via =($r['viaurl']!="") ?
            
"<a href='".$r['viaurl']."'>".$r['via']."</a>" :
            
$r['via'];
        
$via "via ".stripslashes($via);
    }else{
        
$via "";
    }
    if(
strpos($r['url'],"youtube.com")){
        
$tube "<img border='0' align='left' src='/gfx/tube.gif'>";
    }else{
        
$tube "";
    }
     echo 
"<li$new><a href='".$r['url']."'>$tube".stripslashes($r['text'])."</a><br>
    <span>"
.$pw[2]."/".$pw[1]."/".$pw[0]." ".$via."</span></li>\n";
}
echo 
"</ul>";
?>
<!--
<script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script>
<script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/18373978344818481116/state/com.google/broadcast?n=10&callback=GRC_p(%7Bc%3A%22blue%22%2Ct%3A%22Recycled%20Site%20Syndication%22%2Cs%3A%22true%22%7D)%3Bnew%20GRC"></script>
-->
</div>
</div>
<?
$db
->disconnect();
?>

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-408577-1";
urchinTracker();
</script>
</body>
</html>
<? //end of very first if statement at the top
?>

© 2004-2008 Dave Whyte. Send comments to me@thisdomainname
Use whatever you want, but some credit and maybe even a link would be groovy.