<?php 
ob_start
();
// Set cookies
if($_POST['formsubmitted']==1){
    
// delete old cookies
    
if(@$_COOKIE['comics']){
        foreach(
$_COOKIE['comics'] as $key => $comic){
            
setcookie("comics[$key]","CHECKED", (time()-1000));
        }
    }
    
// Set new cookies
    
foreach($_POST as $key => $comic){
        
setcookie("comics[$key]","CHECKED", (time()+9999999));
        
$comicschecked[$key] = "CHECKED";
    }
}
else if(@
$_COOKIE['comics']){
    foreach(
$_COOKIE['comics'] as $key => $comic){
        
$comicschecked[$key] = "CHECKED";
    }
}
if(@
$_POST['zoom']){
    
setcookie("zoom",$_POST['zoom'], (time()+9999999));
    
$zoom=$_POST['zoom'];
}
else if(@
$_COOKIE['zoom']){
    
$zoom=$_COOKIE['zoom'];
}
else{
$zoom="none";}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
    <title>
        MrTwig's comic-strip ripper
    </title>
     <style type="text/css">
        body            {background-color:#80a0be; color:#000000;}
        caption.left    {font-family: verdana; font-size: 13px; font-weight:bold; letter-spacing: 1px; caption-side: top; width: auto; text-align: left; }
        .cap            {font-family: verdana; font-size: 13px; font-weight:bold; letter-spacing: 1px; padding:10px 0px 0px; }
        td,th            {font-family: verdana; font-size: 11px; text-align: left; vertical-align:top; } 
        td.l,th.l        {text-align: left; border: #000000 solid 1px;} 
        td.c,th.c        {text-align: center; border: #000000 solid 1px;} 
        td.r,th.r        {text-align: right;  border: #000000 solid 1px;} 
        td.j,th.j        {text-align: justify;  border: #000000 solid 1px;} 
        td.h            {text-align: center;  border: #000000 solid 1px; font-weight:bold; background-color:#cdc7c7;  padding:0px 2px;} 
        td.head            {vertical-align:top; text-align:center; border-bottom:solid #000000 1px; background-color:#7f9ec9;}
        a,a:visited        {color:#000000; text-decoration: none;  font-weight:bold;} 
        a:hover            {text-decoration: none; color: #ffffff; font-weight:bold;}
        h1                {font-family: verdana; font-size: 13px; letter-spacing: 2px; margin: 0; }
        h1.tight        {font-family: verdana; font-size: 12px; letter-spacing: 1px; margin: 0; }
        h2                {font-family: verdana; font-size: 11px; letter-spacing: 1px; margin: 0; }
        img                {border:0px;}
        table.container {width: 100%;}
        td.container    {text-align: center;}
        table.centered    {margin-left: auto; margin-right: auto;}
    </style>
</head>

<body>
    <table class="container" border="0" summary="Container table for centering"  cellspacing="0" cellpadding="0">
        <tr>
            <td class="container">
                <table class="centered" border="0" summary="main layout table" width="785" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="c" style=" background-color:#b9cbdb">
                            <a href="http://mrtwig.net"><img src="images/header_logo.png" width="785" height="81" alt="header logo" /></a>

                            <table width="785" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td style="background-color:#b9cbdb">
                                        &nbsp;<b>.:</b>&nbsp;<a href="mailto:mrtwigREMOVETHECAPS@start.no">e-mail</a>&nbsp;<b>.:.</b>&nbsp;<a href="board/index.php">message&nbsp;Board</a>&nbsp;<b>.:.</b>&nbsp;<a href="guestbook/">guestbook</a>&nbsp;<b>.:.</b>&nbsp;<a href="faq.php"><acronym title="Frequently asked questions">faq</acronym></a>&nbsp;<b>:.</b>&nbsp;
                                    </td>
                                    <td style="text-align:right; background-color:#b9cbdb">
                                        &nbsp;.:&nbsp;<a href="http://mrtwig.net">www.mrtwig.net</a>&nbsp;:.&nbsp;
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <table class="centered" border="0" summary="main layout table" width="785" cellspacing="5" cellpadding="0">
                    <tr>
                        <td class="c" style="background-color:#b9cbdb">
                            <table border="0" width="785" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="head">
                                        <h1>Comic strip ripper.</h1>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="background-color:#b9cbdb">
                                        

<?php /*###################################################################################
#####                       COMMON STUFF                                           ####
#####################################################################################*/




//*** Generate dates ***************************************************************

    
if(!@$_GET['days']){@$days=0;}
    else{
$days $_GET['days'];}
    
$timestamp = -86400*@$days;
    
$d date("d",time()+$timestamp);
    
$m date("m",time()+$timestamp);
    
$y date("y",time()+$timestamp);
    
$Y date("Y",time()+$timestamp);
    
$savenum = ("$Y-$m-$d");

//*** Generate links ***************************************************************

echo "<div style=\"text-align:center; margin:3px 0px 0px;\">";
for(
$i=32$i>0$i--){
    
$dd=date("d",time()-86400*$i);
    if(@
$days && $i==@$days){
        echo 
"<FONT COLOR=red>$dd</FONT> ";
    }
    else{
        echo 
"<A HREF=\"".$_SERVER['PHP_SELF']."?days={$i}\">$dd</A> ";
    }
}
echo 
"<A HREF=\"scripts_strips.php?days=0\">Today</A></div>";
?>




<table class="centered" border="0" summary="main layout table" cellspacing="5" cellpadding="0">
    <tr>
        <td class="l" style="background-color:#ffffff; padding:2px;">
            <form method="post" action="<?php  echo $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="formsubmitted" value="1" />
                <label title="Show all Dilbert comics " for="dilbert">
                    <input type="checkbox" id="dilbert" name="dilbert" <?php echo @$comicschecked['dilbert'];?> /> Dilbert 
                </label>            

                <label title="Show all Pondus comics " for="pondus">
                    <input type="checkbox" id="pondus" name="pondus" <?php echo @$comicschecked['pondus'];?> /> Pondus 
                </label>            

                <label title="Show all Sherman's Lagoon comics " for="shermanslagoon">
                    <input type="checkbox" id="shermanslagoon" name="shermanslagoon" <?php echo @$comicschecked['shermanslagoon'];?> /> Sherman's Lagoon 
                </label>            

                <label title="Show all Zits comics " for="zits">
                    <input type="checkbox" id="zits" name="zits" <?php echo @$comicschecked['zits'];?> /> Zits 
                </label>            
    
                <label title="Show all Garfield comics " for="garfield">
                    <input type="checkbox" id="garfield" name="garfield" <?php echo @$comicschecked['garfield'];?> /> Garfield 
                </label>            

                <label title="Show all Beetle Bailey comics " for="beetlebailey">
                    <input type="checkbox" id="beetlebailey" name="beetlebailey" <?php echo @$comicschecked['beetlebailey'];?> /> Beetle Bailey 
                </label>            

                <label title="Show all Hagar the Horrible comics " for="hagarthehorrible">
                    <input type="checkbox" id="hagarthehorrible" name="hagarthehorrible" <?php echo @$comicschecked['hagarthehorrible'];?> /> Hagar the Horrible 
                </label>            
    
                <label title="Show all User Friendly comics " for="userfriendly">
                    <input type="checkbox" id="userfriendly" name="userfriendly" <?php echo @$comicschecked['userfriendly'];?> /> User Friendly 
                </label>            

                <label title="Show all Norwegian Comics" for="norwegiancomics">
                    <input type="checkbox" id="norwegiancomics" name="norwegiancomics"  <?php echo @$comicschecked['norwegiancomics'];?> /> Norwegian Comics 
                </label>            
                &nbsp;&nbsp;&nbsp;
                <select name="zoom">
                    <option value="none">Zoom (width)</option>
                    <option value="765" <?php if($zoom==765){echo "SELECTED";}?>>795</option>
                    <option value="980" <?php if($zoom==980){echo "SELECTED";}?>>980</option>
                    <option value="1200" <?php if($zoom==1200){echo "SELECTED";}?>>1200</option>
                    <option value="1540" <?php if($zoom==1540){echo "SELECTED";}?>>1540</option>
                </select>
    
                <input type="submit" value="Save Settings" />
            </form>
        </td>
    </tr>
<?php 
/*#####################################################################################
#####                             Arrays                                           ####
#####################################################################################*/
/*
$info['']['method']        = ;
$info['']['page']        = ;
$info['']['expr']        = ;
$info['']['match']        = ;
$info['']['host']        = ;
$info['']['path']        = ;
$info['']['referer']    = ;
*/
$info['lagunen']['method']        = null;
$info['lagunen']['page']        = null;
$info['lagunen']['expr']        = null;
$info['lagunen']['match']        = null;
$info['lagunen']['host']        = "www.start.no";
$info['lagunen']['path']        = "/tegneserier/lagunen/lag-striper/lag$Y$m$d.gif";
$info['lagunen']['referer']    = "http://www.start.no/tegneserier/lagunen/";

$info['dilbertstart']['method']        = null;
$info['dilbertstart']['page']        = null;
$info['dilbertstart']['expr']        = null;
$info['dilbertstart']['match']        = null;
$info['dilbertstart']['host']        = "www.start.no";
$info['dilbertstart']['path']        = "/tegneserier/dilbert/dil-striper/dil$Y$m$d.gif";
$info['dilbertstart']['referer']    = "http://www.start.no/tegneserier/dilbert/";

$info['ShermansLagoon']['method']    = null;
$info['ShermansLagoon']['page']        = null;
$info['ShermansLagoon']['expr']        = null;
$info['ShermansLagoon']['match']    = null;
$info['ShermansLagoon']['host']        = "pst.rbma.com";
$info['ShermansLagoon']['path']        = "/content/Shermans_Lagoon?date=$Y$m$d";
$info['ShermansLagoon']['referer']    = "Referer: http://seattlepi.nwsource.com/fun/shermans.asp\r\n";


$info['BeetleBailey']['method']        = null;
$info['BeetleBailey']['page']        = null;
$info['BeetleBailey']['expr']        = null;
$info['BeetleBailey']['match']        = null;
$info['BeetleBailey']['host']        = "pst.rbma.com";
$info['BeetleBailey']['path']        = "/content/Beetle_Bailey?date=$Y$m$d";
$info['BeetleBailey']['referer']    = "Referer: http://seattlepi.nwsource.com/fun/beetlebailey.asp\r\n";

$info['HagarTheHorrible']['method']        = null;
$info['HagarTheHorrible']['page']        = null;
$info['HagarTheHorrible']['expr']        = null;
$info['HagarTheHorrible']['match']        = null;
$info['HagarTheHorrible']['host']        = "pst.rbma.com";
$info['HagarTheHorrible']['path']        = "/content/Hagar_The_Horrible?date=$Y$m$d";
$info['HagarTheHorrible']['referer']    = "Referer: http://seattlepi.nwsource.com/fun/hagar.asp\r\n";

$info['Zits']['method']        = null;
$info['Zits']['page']        = null;
$info['Zits']['expr']        = null;
$info['Zits']['match']        = null;
$info['Zits']['host']        = "pst.rbma.com";
$info['Zits']['path']        = "/content/Zits?date=$Y$m$d";
$info['Zits']['referer']    = "Referer: http://seattlepi.nwsource.com/fun/zits.asp\r\n";

$info['Pondusbt']['method']        = null;
$info['Pondusbt']['page']        = null;
$info['Pondusbt']['expr']        = null;
$info['Pondusbt']['match']        = null;
$info['Pondusbt']['host']        = "www.bt.no";
$info['Pondusbt']['path']        = "/template/ver1-0/gfx/cartoons/pondus//$d$m$y.gif";
$info['Pondusbt']['referer']    = null;


$info['Pondus']['method']    = 'substring';
$info['Pondus']['page']        = '/tegneserie/index.html';
$info['Pondus']['pagehost']    = "www.dagbladet.no";
$info['Pondus']['expr']        = "pondusarkiv/serve.php?";
$info['Pondus']['match']    = null;
$info['Pondus']['host']        = "www.dagbladet.no";
$info['Pondus']['path']        = "/tegneserie/";
$info['Pondus']['referer']    = 'http://www.dagbladet.no/tegneserie/index.html';

$info['UserFriendly']['method']        = 'expr';
$info['UserFriendly']['page']        = "/cartoons/?id=$Y$m$d&mode=classic";///cartoons/index.php?id=$Y$m$d
$info['UserFriendly']['pagehost']    = "ars.userfriendly.org";
$info['UserFriendly']['expr']        = "/^(.*http:\/\/www\.userfriendly\.org\/cartoons\/archives\/)?([0-9a-zA-Z\/]+)(\.gif)?.*$/"
$info['UserFriendly']['match']        = 2;
$info['UserFriendly']['host']        = "www.userfriendly.org";
$info['UserFriendly']['path']        = "/cartoons/archives/";
$info['UserFriendly']['referer']    = "http://www.userfriendly.org/cartoons/archives/2003.html";

$info['PondusEng']['method']    = null;
$info['PondusEng']['page']        = null;
$info['PondusEng']['expr']        = null;
$info['PondusEng']['match']        = null;
$info['PondusEng']['host']        = "my.opera.com";
$info['PondusEng']['path']        = "/community/graphics/pondus/$Y/$m/$d.gif";
$info['PondusEng']['referer']    = null;


$info['Nodilbert']['method']    = null;
$info['Nodilbert']['page']        = null;
$info['Nodilbert']['expr']        = null;
$info['Nodilbert']['match']        = null;
$info['Nodilbert']['host']        = "www.vg.no";
$info['Nodilbert']['path']        = "/grafikk/dilbert/dilbert-$Y-$m-$d.gif";
$info['Nodilbert']['referer']    = null;

$info['Nodilbertbt']['method']    = null;
$info['Nodilbertbt']['page']    = null;
$info['Nodilbertbt']['expr']    = null;
$info['Nodilbertbt']['match']    = null;
$info['Nodilbertbt']['host']    = "www.bt.no";
$info['Nodilbertbt']['path']    = "/template/ver1-0/gfx/cartoons/dilbert//$d$m$y.gif";
$info['Nodilbertbt']['referer']    = null;

$info['Garfield']['method']        = null;
$info['Garfield']['page']        = null;
$info['Garfield']['expr']        = null;
$info['Garfield']['match']        = null;
$info['Garfield']['host']        = "images.ucomics.com";
$info['Garfield']['path']        = "/comics/ga/$Y/ga$y$m$d.gif";
$info['Garfield']['referer']    = null;

$info['Dilbert']['method']     = 'expr';
$info['Dilbert']['page']     = "/comics/dilbert/archive/dilbert-$Y$m$d.html"//""
$info['Dilbert']['pagehost'] = 'dilbert.com';
$info['Dilbert']['expr']     = "/(comics\/dilbert\/archive\/images\/dilbert)([0-9]+)\.(gif|jpg)/";
$info['Dilbert']['match']     = 2;
$info['Dilbert']['host']     = 'dilbert.com';
$info['Dilbert']['path']     = "/comics/dilbert/archive/images/dilbert";
$info['Dilbert']['referer']     = null;


/*#####################################################################################
#####                             FUNCTIONS                                        ####
#####################################################################################*/
function findimage($comic){
    
    global 
$info$savenum,$y,$m,$d,$Y,$zoom;
    
$localimage "comics/$comic-$savenum.gif";
    
    
// If image not exists, or it's fake, go get it. or serve local
    
if(!file_exists($localimage) || filesize("comics/{$comic}-{$savenum}.gif")==20716){
        
// For those pesky pages with random imagenames
        
if($info[$comic]['page']!=null){
            
//####### Parse Doc Subroutine ####################
            
$fulltext="";
            
$fp fsockopen ($info[$comic]['pagehost'], 80$errno$errstr30);
            if (!
$fp) {
                
//echo "$errstr ($errno)<br>\n";
            
}
            else{
                
fputs ($fp"GET {$info[$comic]['page']} HTTP/1.1\r\nHost:{$info[$comic]['pagehost']}\r\nConnection: Close\r\n\r\n");
                while (!
feof($fp)) {
                    
$fulltext .= fgets ($fp,128);
                }
                
fclose ($fp);
            }
            
$begin strpos($fulltext,"\r\n\r\n");
            
$fulltext substr($fulltext,$begin);
            
$fulltext str_replace(chr(10),"",$fulltext);
            
$fulltext str_replace(chr(13),"",$fulltext);
            if(
$info[$comic]['method'] == 'expr'){
                if(
preg_match($info[$comic]['expr'], $fulltext$match)){
                    
                    if(
$comic=='Dilbert'){
                        
$info[$comic]['path'] = ($info[$comic]['path'] . $match[$info[$comic]['match']] . ".".$match[3]);
                    }else{
                        
$info[$comic]['path'] = ($info[$comic]['path'] . $match[$info[$comic]['match']] . ".gif");
                    }
                }
                else{
                    return ;
                }
            }
            else if(
$info[$comic]['method'] == 'substring'){
                
                
$exprlen=strlen($info[$comic]['expr']);
                
$pos strpos($fulltext$info[$comic]['expr']);
                
$posend strpos($fulltext"width" ,$pos) -1;
                if(
$posend!=0){
                    
$info[$comic]['path'] .= substr($fulltext $pos , ($posend-$pos));
                }
                else{
                    return ;
                }
            
            }

            
//####### END Parse Doc Subroutine ####################
        
}
        
        
// The following is only for Pondus dagbladet, due to it's lack of direct links to previous days.
        
if($info[$comic]['method'] == 'substring'){ 
            
$im="";
            
$fp fsockopen ($info[$comic]['host'], 80$errno$errstr30);
            
$fp2fsockopen ($info[$comic]['host'], 80$errno$errstr30);
            if (
$fp) {
                
fputs ($fp"GET {$info[$comic]['path']} HTTP/1.1\r\nHost:{$info[$comic]['host']}\r\n{$info[$comic]['referer']}Connection: Close\r\n\r\n");
                while (!
feof($fp)) {
                    
$im .= fread ($fp,128);
                }
                
fclose ($fp);
            }

            
$pos strpos($im"/tegneserie/pondusarkiv/");
            
$posend strpos($im".gif" ,$pos) +4;
            if(
$posend!=0){
                
$info[$comic]['path'] = substr($im $pos , ($posend-$pos));
            }
                    
            
fputs ($fp2"GET {$info[$comic]['path']} HTTP/1.1\r\nHost:{$info[$comic]['host']}\r\n{$info[$comic]['referer']}Connection: Close\r\n\r\n");
            while (!
feof($fp2)) {
                
$im2 .= fread ($fp2,128);
            }
    
            
$im=$im2;
            
$begin strpos($im,"\r\n\r\n");
            
$im substr($im,$begin+4);
            
$imgfile fopen ("comics/{$comic}-{$savenum}.gif""wb");
            
$fp fwrite($imgfile,$im);
            
fclose($imgfile);
        }
        else{ 
            
$im="";
            
$fp fsockopen ($info[$comic]['host'], 80$errno$errstr30);
            if (!
$fp) {
                
//echo "$errstr ($errno)<br>\n";
            
} else {
                
fputs ($fp"GET {$info[$comic]['path']} HTTP/1.1\r\nHost:{$info[$comic]['host']}\r\n{$info[$comic]['referer']}Connection: Close\r\n\r\n");
                while (!
feof($fp)) {
                    
$im .= fread ($fp,128);
                }
                
fclose ($fp);
            }
            
$begin strpos($im,"\r\n\r\n");
            
$im substr($im,$begin+4);
            
$imgfile fopen ("comics/{$comic}-{$savenum}.gif""wb");
            
$fp fwrite($imgfile,$im);
            
fclose($imgfile);
        }

    }

    @list(
$width$height$type$tag) = getimagesize($localimage);
    if(
$width){$relation $height/$width;}else{return;}

    if(
$zoom!="none"){    
        
$newheight floor($zoom $relation);
        
$tag "width=\"$zoom\" height=\"$newheight\"";
    }
    else{
        
$tag "width=\"$width\" height=\"$height\"";
    }
    
$returnimage "<tr><td class=\"c\" style=\"background-color:#ffffff; padding:2px;\"><img src=\"$localimage\" $tag /></td></tr>";
    return 
$returnimage;
}

if(
$comicschecked['pondus']=="CHECKED"){echo findimage('PondusEng');}
if(
$comicschecked['dilbert']=="CHECKED"){echo findimage('Dilbert');}
if(
$comicschecked['shermanslagoon']=="CHECKED"){echo findimage('ShermansLagoon');}
if(
$comicschecked['zits']=="CHECKED"){echo findimage('Zits');}
if(
$comicschecked['garfield']=="CHECKED"){echo findimage('Garfield');}
if(
$comicschecked['beetlebailey']=="CHECKED"){echo findimage('BeetleBailey');}
if(
$comicschecked['hagarthehorrible']=="CHECKED"){echo findimage('HagarTheHorrible');}
if(
$comicschecked['userfriendly']=="CHECKED"){echo findimage('UserFriendly') ;}
if(
$comicschecked['shermanslagoon']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('lagunen');}
if(
$comicschecked['pondus']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('Pondus');}
if(
$comicschecked['pondus']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('Pondusbt');}
if(
$comicschecked['dilbert']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('Nodilbert');}
if(
$comicschecked['dilbert']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('dilbertstart');}
if(
$comicschecked['dilbert']=="CHECKED" && $comicschecked['norwegiancomics']=="CHECKED"){echo findimage('Nodilbertbt');}
?>                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="c" style="background-color:#b9cbdb">
                        A website from Cudlybear design &copy; 2000
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
</html>