<?php

// prefs - to make up for the fact that I'm not logged in
// (not working out - disabling for now)
/*
$t='5'; // threshold
$s='0'; // comment sort - 0=oldest first
$m='thread'; // mode = threaded
*/
#if (isset($_GET['url'])) {
#    $url=$_GET['url']."&threshold=".$t."&commentsort=".$s."&mode=".$m;
#} else {
    
$url="http://slashdot.org/";
#}

// comments? suggestions? complaints? takedown notices? 
// created by brianashe at gmail dot com 
// March 2009

// I just grabbed this 'curl' chunk from the rss2html.php page at http://www.feedforall.com
// so there might be extra stuff here I don't need. I don't know, but it works.

// His original script has a copyright notice but it does a lot more.
// All I'm using is the curl stuff which is pretty standard so hopefully
// he won't mind.

    
$curlHandle curl_init();
    
curl_setopt($curlHandleCURLOPT_URL$url);
    
curl_setopt($curlHandleCURLOPT_HEADER0);
    
curl_setopt($curlHandleCURLOPT_RETURNTRANSFER1);
    
curl_setopt($curlHandleCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1;) Gecko/2008070208 Firefox/3.0.1");
    
//    curl_setopt($curlHandle, CURLOPT_AUTOREFERER, 1);
    
curl_setopt($curlHandleCURLOPT_REFERER$filename);
    if (!(
ini_get("safe_mode") || ini_get("open_basedir"))) {
      
curl_setopt($curlHandleCURLOPT_FOLLOWLOCATION1);
    }
    if (isset(
$connectTimeoutLimit) && $connectTimeoutLimit != 0) {
      
curl_setopt($curlHandleCURLOPT_CONNECTTIMEOUT$connectTimeoutLimit);
    }
    
curl_setopt($curlHandleCURLOPT_MAXREDIRS10);
    
$result curl_exec($curlHandle);
    if (
curl_errno($curlHandle)) {
      
$ReadErrorString curl_error($curlHandle);
      
curl_close($curlHandle);
      return 
FALSE;
    }
    
$http_response curl_getinfo($curlHandleCURLINFO_HTTP_CODE);
    if ((
$http_response 200) || (300 <= $http_response)) {
      
$ReadErrorString "HTTP ERROR: $http_response";
      
curl_close($curlHandle);
      return 
FALSE;
    }
    
curl_close($curlHandle);

// give the front page a different title (not because I want to be famus, but to make the default bookmark name better)
$result=str_replace("<title>Slashdot - News for nerds, stuff that matters</title>","<title>Slashdot for iPhone</title>",$result);

// comment out all scripts
$result=str_replace("<script","<!-- ",$result);
$result=str_replace("</script>"," -->",$result);

// dump all images
$result=str_replace("<img ","<blop ",$result);

// dump all stylesheets
$result=str_replace("rel=\"stylesheet\"","blop ",$result);

// just to make sure they die
$result=str_replace("http://images.slashdot.org/","",$result);

// get rid of empty spans ("soda pop1" firehose stuff)
$result=str_replace("<span>&nbsp;</span>","",$result);

// you know what? let's switch H3s to 'div class='headline'
$result=str_replace("<h3 class=\"story\"","<div class=\"headline\"",$result);
$result=str_replace("</h3>","</div><!-- headline -->",$result);

// now that I've done that, for some reason, 'Create a new account' came back to life
$result=str_replace("<a class=\"hideItAll\" href=\"//slashdot.org/login.pl?op=newuserform\"","<a name=\"blop\">",$result);

// eh, what the hell, show the quote at the bottom of the page.
$result=str_replace("<div class=\"rightcontent\">","<div class=\"rightcontent\">QOTD: ",$result);

// I *do* like 'top' tags, don't like the rest. giving new class so I can make them small, grey, whatever.
$result=str_replace("class=\"tag-display-stub\" context=\"top\"","class=\"toptag\"",$result);

// hide the 'jump' links that appear when you strip styling
$result=str_replace("id=\"jump\"","class=\"hideItAll\"",$result);

// ditch topic icons
// someday I'll learn regexp
/*
$result=str_replace('style="width:75px; height:74px;"','',$result);
$result=str_replace('style="width:73px; height:59px;"','',$result);
$result=str_replace('style="width:75px; height:55px;"','',$result);
$result=str_replace('style="width:71px; height:37px;"','',$result);
*/

// or, just move them offscreen
$result=str_replace('style="width','style="position: absolute; left: -200px; top: -200px; width',$result);

// and now, run ALL pages through my system, mwa ha ha ha ha!
// (never mind, not working.)
# $result=str_replace("href=\"//","href=\"index.php?url=http://",$result);

// just to make sure the stylesheets die - if this is above the above line it works on the desktop but fails on the iPhone.
$result=str_replace("http://images.slashdot.org/","",$result);

// this indented block is for the comment pages
    
$result=str_replace("<ul","<span",$result);
    
$result=str_replace("</ul>","</span>",$result);
    
$result=str_replace("<li","<span",$result);
    
$result=str_replace("</li>","</span>",$result);
    
$result=str_replace("class=\"details\"","",$result);
    
$result=str_replace("<div class=\"title\">","<div class=\"commenthead\">",$result);

// add a viewport setting, the local stylesheet, and the location-bar-hiding-thing, and an icon
$result=str_replace("<head>","<head>\n<meta name=\"apple-touch-fullscreen\" content=\"YES\" />\n<link rel=\"apple-touch-icon\" href=\"slashdot.png\">\n<meta name=\"viewport\" content=\"user-scalable=no, width=device-width\">\n<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">\n<script type=\"text/javascript\">\nfunction hideBar()\n{\nwindow.scrollTo(0, 1);\n}\n</script>\n",$result);
$result=str_replace("<body>","<body onload=\"hideBar();\">",$result);

// for some reason this part just won't die
$result=str_replace("<a href=\"#main-articles\">Stories</a>","",$result);
$result=str_replace("<br>","",$result);
$result=str_replace("<a href=\"#blocks\">Slash Boxes</a>","",$result);
$result=str_replace("<br>","",$result);
$result=str_replace("<a href=\"#comments\">Comments</a>","",$result);

// and this double-indented block is my cool system to create links to jump down to the next story
if ('http://slashdot.org/'==$url) {
    
$result=str_replace("<div class=\"headline\"","NEXTNEXTNEXT<div class=\"headline\"",$result);
    
$parts=explode("NEXTNEXTNEXT",$result);
    
$numparts=count($parts);
    
$final=$parts[0];
    for (
$i=1;$i<$numparts;$i++) {
        if (
1==$i) { // all stories but the first get an up arrow. first gets a space. last gets 'top'
            
$final .= "<div align='right'><a name='p".$i."' href='/slashdot/'><img src='info.jpg' width='50' height='50' border='0' alt='About'></a><img src='/dot.gif' width='110' height='1' alt='' border='0'><a href='#p".($i+1)."'><img src='downarrow.jpg' width='50' height='50' border='0' alt='Next story'></a></div>";
        } else {
            
$next=($i+1);
            if (
$next==$numparts) { // the final row - 'prev' and 'top' links, no 'next'
                // start div
                
$final .= "<div align='right'>";
                
// link to top
                
$final .= "<a name='p".$i."' href='#p1'><img src='2uparrow.jpg' width='50' height='50' border='0' alt='Top'></a>";
                
// spacer
                
$final .= "<img src='/dot.gif' width='30' height='1' alt='' border='0'>";
                
// link to previous story
                
$final .= "<a name='p".$i."' href='#p".($i-1)."'><img src='uparrow.jpg' width='50' height='50' border='0' alt='Previous story'></a>";
                
// spacer
                
$final .= "<img src='/dot.gif' width='30' height='1' alt='' border='0'>";
                
// placeholder
                
$final .= "<img src='/dot.gif' width='50' height='50' alt='' border='0'>";
                
// end div
                
$final .= "</div>";
            } else { 
// any other row - gets prev/next links
                // start div
                
$final .= "<div align='right'>";
                
// link to previous story
                
$final .= "<a name='p".$i."' href='#p".($i-1)."'><img src='uparrow.jpg' width='50' height='50' border='0' alt='Previous story'></a>";
                
// spacer
                
$final .= "<img src='/dot.gif' width='30' height='1' alt='' border='0'>";
                
// link to next story
                
$final .= "<a href='#p".($i+1)."'><img src='downarrow.jpg' width='50' height='50' border='0' alt='Next story'></a>";
                
// end div
                
$final .= "</div>";
            }
        }
        
$final .= $parts[$i];
    }
    print 
$final;
} else {
    print 
$result;
}

?>