Tobias Duehr avatar Tobias Duehr committed a7e0d57

NSFW Images are now hidden by default
parallel AJAX-requests are now limited to 4
some webkit-only fx
PHP timeout for content-scraping is set to 8 seconds

Comments (0)

Files changed (1)

 <?php
-session_start();
-
 require_once('ThumbLib.inc.php');
 require_once('simple_html_dom.php');
 
-if($_GET['nsfw']){
-	if($_GET['nsfw'] == 'ok') $_SESSION['nsfw'] = true;
-	else $_SESSION['nsfw'] = false;
-}
+$subreddit = (isset($_GET['r'])) ? $_GET['r'] : 'pics';
 
 if($_GET['a']){
-	$url = 'http://reddit.com/r/pics/.json';
+	$url = 'http://reddit.com/r/'.$subreddit.'/.json';
 	if($_GET['after']) $url.= '?after=t3_' . $_GET['after'];
-
 	if(is_file('.json/'.md5($url)) && time() - filemtime('.json/'.md5($url)) < 120){
 		$r = json_decode(file_get_contents('.json/'.md5($url)));
 	} else {
 		$r = json_decode($d);
 		file_put_contents('.json/'.md5($url), $d);
 	}
-  
-  foreach($r->data->children as $a){
-		/*if(!$_SESSION['nsfw'] && strpos(strtolower($a->data->title), 'nsfw'))*/
+
+	foreach($r->data->children as $a){
 		$has_image = false;
 		if(is_file('.thumbs/'.$a->data->id)) $has_image = true;
 		$panel = '<div id="'.$a->data->id.'" class="panel';
-		 if($has_image) $panel .= ' check';
-		 $panel .= '"><a class="image" href="'.$a->data->url.'"';
-		 if($has_image) $panel .= ' style="background-image:url(.thumbs/'.$a->data->id.');"';
-		 $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($has_image) $panel .= ' check checked';
+		if(strpos(strtolower($a->data->title),'nsfw')) $panel .= ' nsfw';
+		$panel .= '"><a class="image" href="'.$a->data->url.'"';
+		if($has_image) $panel .= ' style="background-image:url(.thumbs/'.$a->data->id.');"';
+		$panel .= '><span class="shadow"></span></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']){
-$thumbs_dir = '.thumbs';
-$thumbnail_file = $thumbs_dir.'/'.$_GET['id'];
-$output->id = $_GET['id'];
+	set_time_limit(8);
+	$thumbs_dir = '.thumbs';
+	$thumbnail_file = $thumbs_dir.'/'.$_GET['id'];
+	$output->id = $_GET['id'];
 
-if(is_file($thumbnail_file)){
-  $output->img = $thumbnail_file;
-	echo json_encode($output);
+	if(is_file($thumbnail_file)){
+		$output->img = $thumbnail_file;
+		echo json_encode($output);
+		exit;
+	}
+
+	$s = array(260,260);
+
+	$filename = $_GET['url'];
+
+	$size = @getimagesize($filename);
+	if($size){
+		$thumb = PhpThumbFactory::create($filename);
+		$thumb->adaptiveResize($s[0], $s[1]);
+		$thumb->save($thumbnail_file);
+		$output->img = $thumbnail_file;
+		echo json_encode($output);
+		exit;
+	} else {
+		$html = file_get_html($filename);
+		$img = null;
+		$largest = 0;
+		foreach($html->find('img') as $element){
+			$size = @getimagesize($element->src);
+			if($size){
+				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]);
+			$thumb->save($thumbnail_file);
+			$output->img = $thumbnail_file;
+			$output->img = $thumbnail_file;
+			echo json_encode($output);  
+			exit;
+		}
+	}
+	$output->img = 'noimage.png';
+	echo json_encode($output);  
 	exit;
 }
 
-$s = array(260,260);
+?><!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="Content-type" content="text/html; charset=utf-8">
+<meta name="viewport" content="width=device-width; initial-scale=0.3; user-scalable=yes" /> 
+<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">
+<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; }
+.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.check { opacity:.5; }
+.panel.check.checked { opacity:1; }
+.panel a { display:block; position:absolute; text-decoration:none; color:#FFF; }
+.panel a.image { background:#FFF 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; }
+.panel .title span { background:rgba(0,0,0,.3); text-shadow: rgba(0,0,0,.7) 1px 1px 1px; line-height:22px; }
+.panel .comments { padding:2px; position:absolute;right:0;top:0;color:#999;opacity:0;background:#FFF; }
+.panel .comments:hover { color:#000; }
+.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">
 
-$filename = $_GET['url'];
-
-$size = @getimagesize($filename);
-if($size){
-	$thumb = PhpThumbFactory::create($filename);
-	$thumb->adaptiveResize($s[0], $s[1]);
-	$thumb->save($thumbnail_file);
-	$output->img = $thumbnail_file;
-	echo json_encode($output);
-	exit;
-} else {
-    $html = file_get_html($filename);
-    $img = null;
-    $largest = 0;
-    foreach($html->find('img') as $element){ 
-      $size = @getimagesize($element->src);
-      if($size){
-        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]);
-	$thumb->save($thumbnail_file);
-	$output->img = $thumbnail_file;
-$output->img = $thumbnail_file;
-	echo json_encode($output);  
-  exit;
-}
-}
-$output->img = 'noimage.png';
-echo json_encode($output);  
-exit;
+function isScrollBottom() {
+	var documentHeight = $(document).height();
+	var scrollPosition = $(window).height() + $(window).scrollTop();
+	return (scrollPosition > 0 && documentHeight == scrollPosition && documentHeight != $(window).height());
 }
 
-?>
+function isScrolledIntoView(elem){
+	var docViewTop = $(window).scrollTop();
+	var docViewBottom = docViewTop + $(window).height();
 
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
-    <title>redditmag</title>	
-	
-	<style type="text/css">
-  * { padding: 0; margin:0; line-height:100%; -webkit-transition-property : all; -webkit-transition-duration : 0.5s; }
-	.panel{ 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 a { display:block; position:absolute; text-decoration:none; color:#FFF; }
-	.panel a.image { background:#FFF no-repeat 50% 50%; left:0;top:0;width:100%;height:100%; }
-  .panel .title { padding-right:10px; position:absolute; bottom:0; left:0; display:block; width:250px; }
-  .panel .title span { background:rgba(0,0,0,.3); text-shadow: rgba(0,0,0,.7) 1px 1px 1px; line-height:22px; }
-.panel .comments { padding:2px; position:absolute;right:0;top:0;color:#999;opacity:0;background:#FFF; }
-.panel .comments:hover { color:#000; }
-.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 isScrollBottom() {
-		var documentHeight = $(document).height();
-		var scrollPosition = $(window).height() + $(window).scrollTop();
-		return (scrollPosition > 0 && documentHeight == scrollPosition && documentHeight != $(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');
+	if(after in afters){}
+	else {
+		afters[after] = true;
+		$.get('index.php', {a:'moar', after:after, r:'<?php echo $subreddit; ?>'}, function(data){
+		$('body').append(data);
+		checkPanels();
+		});
 	}
-	
-	function isScrolledIntoView(elem){
-    var docViewTop = $(window).scrollTop();
-    var docViewBottom = docViewTop + $(window).height();
+}
 
-    var elemTop = $(elem).offset().top;
-    var elemBottom = elemTop + 0;
+function checkPanels(){
+	$('.panel:not(.check)').each(function(){
+		if(isScrolledIntoView(this)){
+			getStuff(this);
+		}
+	});
+}
 
-    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
-      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
-	}
-	
-	var afters = {};
-	
-	function moar(){
-		var after = $('.panel:last').attr('id');
-		if(after in afters){}
-		else {
-		afters[after] = true;
-		$.get('index.php', {a:'moar', after:after}, function(data){
-			$('body').append(data);
-			checkPanels();
-		});
+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")'});
+			$.getJSON('index.php', {id:el.id,url:$(el).children('a.image').attr('href')}, function(data){
+				stuff_to_get--;
+				if(data && data.id){
+					$('#'+data.id).addClass('checked');
+					if(data && data.img) $('#'+data.id).children('a.image').css({'background-image':'url("'+data.img+'")'});
+				}
+			});
 		}
 	}
-	
-	function checkPanels(){
-		$('.panel:not(.check)').each(function(){
-			if(isScrolledIntoView(this)){
-				getStuff(this);
+}
+
+var nsfw = false;
+
+$(function(){
+	moar();
+	window.setInterval(
+		function(){
+			if(isScrollBottom()){
+				moar();
 			}
-		});
-	}
-	
-	function getStuff(el){
-		if(!$(el).hasClass('check')){
-		$(el).addClass('check');
-		$(el).children('a.image').css({'background-image':'url("load.gif")'});
-      $.getJSON('index.php', {id:el.id,url:$(el).children('a.image').attr('href')}, function(data){
-        if(data && data.img) $('#'+data.id).children('a.image').css({'background-image':'url("'+data.img+'")'});
-      });
-	  }
-	}
-	
-  $(function(){
-	 $(window).scroll(function(){
-      if(isScrollBottom()){
-        moar();
-      }
-	  checkPanels();
-    });
-	moar();
-  });
-	
-	</script>
-  </head>
-  <body>
-  </body>
+			checkPanels();
+		}, 500
+	);
+	$(document).dblclick( function(){
+		if(nsfw) {
+			$('.nsfw a.image').hide();
+			nsfw = false;
+		} else {
+			$('.nsfw a.image').show();
+			nsfw = true;
+		}
+	});
+});
+</script>
+</head>
+<body></body>
 </html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.