Source

redditmag / index.php

Full commit
<?php
require_once('ThumbLib.inc.php');
require_once('simple_html_dom.php');
require_once('Predis.php');

$redis_server = array('host' => '127.0.0.1', 'port' => 6379, 'database' => 12);
$db = new Predis_Client($redis_server);
//$db->flushdb();

$subreddit = (isset($_GET['r'])) ? $_GET['r'] : 'pics';

if(strtolower($subreddit) != 'pics'
&& strtolower($subreddit) != 'wallpaper'
&& strtolower($subreddit) != 'wallpapers'
&& strtolower($subreddit) != 'funny'
&& strtolower($subreddit) != 'windowshots'
&& strtolower($subreddit) != 'pic'
&& strtolower($subreddit) != 'gaming'
&& strtolower($subreddit) != 'adviceanimals'
&& strtolower($subreddit) != 'aww'
&& strtolower($subreddit) != 'fffffffuuuuuuuuuuuu'
&& strtolower($subreddit) != 'comics'
&& strtolower($subreddit) != 'battlestations'
&& strtolower($subreddit) != 'itookapicture'){
	echo '"I\'m sorry Dave, I\'m afraid I can\'t do that."';
	exit();
}

if($_GET['df']){
	echo '<p><code>Total : ' . number_format(disk_total_space('./'),2,'.',',') . '</code></p>';
	echo '<p><code>Free_ : ' . number_format(disk_free_space('./'),2,'.',',')  . '</code></p>';
	exit;
}

if($_GET['a']){
	$url = 'http://reddit.com/r/'.$subreddit.'/.json';
	if($_GET['after']) $url.= '?after=t3_' . $_GET['after'];
	$key = "json:".md5($url);
	if($db->exists($key)){
		$r = json_decode($db->get($key));
		if(!$r || !$r->data || !$r->data->children){
			exit;
		}
	} else {
		$d = file_get_contents($url);
		if($d){
			$r = json_decode($d);
			$db->set($key, $d);
			$db->expire($key, 7200);
		}
	}

	foreach($r->data->children as $a){
		$has_image = false;
		$text_link = false;
		$imgkey = "thumb:".$a->data->id;
		if($db->exists($imgkey)) $has_image = true;
		$panel = '<div id="'.$a->data->id.'" class="panel';
		if($has_image) $panel .= ' check checked';
		if(strpos(strtolower($a->data->title),'nsfw') !== false) $panel .= ' nsfw';
		if($a->data->selftext){
			$panel .= ' textlink';
			$text_link = true;
		}
		$panel .= '"><a class="image" href="'.$a->data->url.'">';
		if($has_image && !$text_link) $panel .= '<img class="thumbnail" src="?url='.rawurlencode($a->data->url).'&id='.$a->data->id.'">';
		if($text_link) $panel .= '<p>' . $a->data->selftext . '</p>';
		$panel .= '</a><a href="'.$a->data->url.'" class="title"><span>'.$a->data->title.'</span></a><a class="comments" href="http://reddit.com'.$a->data->permalink.'">c:'.$a->data->num_comments.'/k:'.$a->data->score.'</a></div>';
		echo $panel;
	}
	exit;
}

if($_GET['url']){
	set_time_limit(8);
	$output->id = $_GET['id'];
	$key = "thumb:".$_GET['id'];
	
	if($db->exists($key)){
		header('Content-Type: image/jpeg');
		$db->expire($key, 172800);
		echo base64_decode($db->get($key));
		exit;
	}
	
	$img_found = false;
	$s = array(260,260);

	$filename = rawurldecode($_GET['url']);

	$size = @getimagesize($filename);
	if($size){
		if($size[0] >= 260 && $size[1] >= 260){
			$thumb = PhpThumbFactory::create($filename);
			$thumb->adaptiveResize($s[0], $s[1]);
			$imgstr = base64_encode($thumb->getImageAsString());
			$db->set($key, $imgstr);
			$img_found = true;
		}
	} else {
		$html = file_get_html($filename);
		$img = null;
		$largest = 0;
		foreach($html->find('img') as $element){
			$size = @getimagesize($element->src);
			if($size && $size[0] >= 260 && $size[1] >= 260){
				if($size[0] + $size[1] > $largest){
					$largest = $size[0] + $size[1];
					$img = $element->src;
				}
			}
		}
		if($img){
			$thumb = PhpThumbFactory::create($img);
			$thumb->adaptiveResize($s[0], $s[1]);
			$imgstr = base64_encode($thumb->getImageAsString());
			$db->set($key, $imgstr);
			$img_found = true;
		}
	}
	if(!$img_found){
		$db->set($key, base64_encode(file_get_contents('noimage.png')));
	}
	$db->expire($key, 172800);
	header('Content-Type: image/jpeg');
	echo base64_decode($db->get($key));
	exit;
}

?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="description" content="On redditmag you can see the content of /r/pics and other subreddits in a pleasant manner">
<title>redditmag - <?php echo $subreddit; ?></title>	
<style type="text/css">
* { padding: 0; margin:0; line-height:100%; -webkit-transition-property : all; -webkit-transition-duration : 0.5s; }
body { margin: 0 auto; font-family: sans-serif; background: #FFF; }
img { border:none; }
#menu{ color: #FFF; background: #333; padding: 5px; } 
#menu a { color: #DDD; text-decoration:none; font-size: 12px; } 
#menu a:hover { color: #FFF } 
.panel { opacity:0; color:#FFF; font-size:18px; text-decoration:none; font-family: 'Vollkorn', arial, serif; width : 260px; height: 260px; overflow:hidden; float: left; margin:5px; position:relative; }
.panel p { position: absolute; left: 0; top: 0; color:#AAA; z-index:1; font-size: 25px; overflow:hidden; height:200px; width : 260px; text-align: justify;  }
.panel.check { opacity:.5; }
.panel.check.checked { opacity:1; }
.panel a { display:block; position:absolute; text-decoration:none; color:#FFF; }
.panel a.image { background:#EEE no-repeat 50% 50%; left:0;top:0;width:100%;height:100%; }
.panel.nsfw a.image { display:none; }
.panel .title { padding-right:10px; position:absolute; bottom:0; left:0; display:block; width:250px; z-index: 100; }
#adviceanimals .panel .title { display: none; }
.panel .title span { background:rgba(0,0,0,.3); text-shadow: rgba(0,0,0,.7) 1px 1px 1px; line-height:26px; }
.panel .comments { padding:2px; position:absolute;right:0;top:0;color:#999;opacity:0;background:#FFF; z-index:50; }
.panel .comments:hover { color:#000; }
.panel .voting { background:#FFF;position:absolute;right:0;top:0;display:none;overflow:hidden;width:69px;height:52px; z-index:50; }
.panel:hover .voting { display:block; }
/*
.panel a.image .shadow { left:0;top:0;width:100%;height:100%;position:absolute; background: -webkit-gradient(radial, 130 130, 260, 130 130, 270, from(rgba(0,0,0,0)), to(rgba(0,0,0,.2))); }
.panel:hover a.image:hover .shadow { opacity:0; }
.panel:hover .comments { opacity:1; }
*/
.panel:hover .title span { background:rgba(0,0,0,.7); }
</style>
<link href=' http://fonts.googleapis.com/css?family=Vollkorn' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">

function trace(str){
	//if(console && console.log) console.log(str);
}

function addVote(){
	$('.panel:not(".has_vote")').each(function(){
		$(this).append('<div class="voting"><iframe src="http://www.reddit.com/static/button/button3.html?width=69&url='+escape($(this).children('a.title').attr('href'))+'" height="52" width="69" scrolling="no" frameborder="0"></iframe></div>');
		$(this).addClass('has_vote');
	});
}

function isScrollBottom() {
	var documentHeight = $(document).height();
	var scrollPosition = $(window).height() + $(window).scrollTop();
	trace(scrollPosition + ' :: ' + documentHeight);
	return (scrollPosition > 0 && documentHeight - 300 <= scrollPosition);
}

function isScrolledIntoView(elem){
	var docViewTop = $(window).scrollTop();
	var docViewBottom = docViewTop + $(window).height();

	var elemTop = $(elem).offset().top;
	var elemBottom = elemTop + 0;

	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
	&& (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop));
}

var afters = {};
var stuff_to_get = 0;

function moar(){
	var after = $('.panel:last').attr('id');
	trace('moar');
	if(after in afters){}
	else {
		$('#loader').show();
		afters[after] = true;
		trace(after);
		if(typeof(_trackEvent) != "undefined") _trackEvent('moar', after);
		$.get('index.php', {a:'moar', after:after, r:'<?php echo $subreddit; ?>'}, function(data){
			$('body').append(data);
			$('#loader').hide();
			checkPanels();
		});
	}
}

function checkPanels(){
	$('.panel:not(.check)').each(function(){
		if(isScrolledIntoView(this)){
			getStuff(this);			
		}
	});
	$('.panel:not(.has_vote)').mouseenter(function(){
		if(!$(this).hasClass('has_vote')){
			$(this).append('<div class="voting"><iframe src="http://www.reddit.com/static/button/button3.html?width=69&url='+escape($(this).children('a.title').attr('href'))+'" height="52" width="69" scrolling="no" frameborder="0"></iframe></div>');
			$(this).addClass('has_vote');
		}
	});
	$('.panel.has_vote').mouseleave(function(){
		$(this).children('div.voting').remove();
		$(this).removeClass('has_vote');
	});
}

function getStuff(el){
	if(!$(el).hasClass('check')){
		if(stuff_to_get < 4){
			$(el).addClass('check');
			stuff_to_get ++;
			$(el).children('a.image').css({'background-image':'url("load.gif")'});
			if(typeof(_trackEvent) != "undefined") _trackEvent('stuff', el.id);
			var thumb = new Image();
			thumb.class = "thumbnail";
			thumb.style.display = "none";
			thumb.rel = el.id;
			thumb.onload = function(){
				var el = $("#" + this.rel);
				el.children('a.image').css({'background-image':'none'}).append(this);
				el.addClass('checked');
				$(this).fadeIn();
				stuff_to_get--;
			};
			thumb.src = '?url='+escape($(el).children('a.image').attr("href"))+'&id='+el.id;
		}
	}
}

var nsfw = false;

$(function(){
	moar();
	window.setInterval(
		function(){
			if(isScrollBottom()){
				trace('bttm');
				moar();
			}
			checkPanels();
		}, 500
	);
	
	$(document).dblclick( function(){
		if(nsfw) {
			$('.nsfw a.image').hide();
			nsfw = false;
		} else {
			$('.nsfw a.image').show();
			nsfw = true;
		}
	});
});
</script>
</head>
<body id="<?php echo $subreddit ?>">
  <div id="menu">
  <a href="?r=pics">pics</a>
  <a href="?r=funny">funny</a>
  <a href="?r=wallpapers">wallpapers</a>
  <a href="?r=wallpaper">wallpaper</a>
  <a href="?r=itookapicture">itookapicture</a>
  <a href="?r=windowshots">windowshots</a>
  <a href="?r=aww">aww</a>
  <a href="?r=battlestations">battlestations</a>
  <a href="?r=pic">pic</a>
  <a href="?r=fffffffuuuuuuuuuuuu">fffffffuuuuuuuuuuuu</a>
  <a href="?r=adviceanimals">adviceanimals</a>
  |
  <a href="javascript:alert('NSFW contents are hidden by default, double-click in the white area (between the images) to show or hide them.')">what about nsfw?</a>
  <a href="http://bitbucket.org/saibotd/redditmag/">sourcecode</a>
  <a href="https://spreadsheets.google.com/viewform?formkey=dHRKeGk3bkRlTEN2UzNKcFNjR2RuMkE6MQ&theme=0AX42CRMsmRFbUy1kN2NjY2Y0Mi1iYWFmLTQxMWMtYjM0NC04NmZmYTIwZDRkMGQ&ifq">contact</a>
  <img width="0" height="0" src="http://www.reddit.com/static/blog_snoo.gif">
  </div>

<img src="load.gif" style="background:rgba(255,255,255,.85);padding:10px 25px;display:none;position:fixed;left:50%;margin-left:-25px;bottom:0px;z-index:100;" id="loader">
  </body>
</html>