Created
July 6, 2010 03:01
-
-
Save shimondoodkin/464949 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// These are so benificial, they default to true. | |
if (!isset($use_page_cache)) | |
$use_page_cache = 1; | |
if (!isset($use_compression)) | |
$use_compression = 1; | |
// Add browsers here as we must detect them. Opera is an oddball, if we don't detect | |
// it specifically, it will turn up as MSIE | |
$browser="other"; | |
if (isset($_SERVER['HTTP_USER_AGENT'])) { | |
$agent = $_SERVER['HTTP_USER_AGENT']; | |
if (eregi("opera",$agent)){ | |
$browser="opera"; | |
}elseif(eregi("msie",$agent)){ | |
$browser="msie"; | |
} | |
} | |
if ($use_compression && !( $use_page_cache && $browser == "msie")) { | |
// Turn on compression, makes quite a difference in bandwith usage. | |
// However, MSIE (all versions) have a bug with compression and caching. So for MSIE | |
// it's either compression or caching. We choose caching. | |
ob_start('ob_gzhandler'); | |
} | |
ob_start(); | |
session_cache_limiter("must-revalidate"); | |
session_start(); | |
//while(ob_get_level()) ob_end_flush(); | |
set_time_limit(20*60*60); | |
header("Content-type: text/html; charset=utf-8"); | |
// Copyright 2007 Facebook Corp. All Rights Reserved. | |
// | |
// Application: FriendGroups | |
// File: 'index.php' | |
// This is a sample skeleton for your application. | |
// | |
$groups_count=array(); | |
$groups_friends=array(); | |
$groups_name=array(); | |
require_once('facebook/php/facebook.php'); | |
require_once('simple_html_dom.php'); | |
require_once('httpclient.php'); | |
require_once('wget_download.php'); | |
require_once('cache.php'); | |
$appapikey = 'cef37************eff6'; | |
$appsecret = 'd33e*****************60007'; | |
$facebook = new Facebook($appapikey, $appsecret); | |
$user_id = $facebook->require_login(); | |
if($fbuser = $facebook->get_loggedin_user()){ | |
try { | |
$userql=$facebook->api_client->fql_query('SELECT uid,name FROM user WHERE uid='.$fbuser); | |
} catch (Exception $ex) { | |
$facebook->clear_cookie_state(); | |
unset($fbuser); | |
} | |
} | |
else | |
{ | |
die("facebook login error.. refresh might help"); | |
} | |
$userql=$facebook->api_client->fql_query('SELECT uid,name FROM user WHERE uid='.$fbuser); | |
$sort=1; | |
if(isset($_REQUEST['sort']))$sort=intval($_REQUEST['sort']); | |
if($sort<-4) $sort=-4; | |
if($sort==0) $sort=2; | |
if($sort>4) $sort=4; | |
if (!OutputCache::Start("output", $userql[0]['uid']."_".$userql[0]['name'].'_'.$sort.'_v5', 600)) | |
{ | |
// Generate some output (as you do)... | |
ob_start(); | |
//ob_implicit_flush(true); | |
?> | |
<?/* | |
<!-- | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<title>facebook my Friend's Popular Groups</title> | |
</head> | |
<body> | |
--> | |
*/?> | |
<h1>facebook my Friend's Popular Groups</h1> | |
It takes between 2 to 5 minutes to download all of your groups member count. this application does not counts privet and closed groups. | |
<style> | |
a {color:#000044;text-decoration:none;} | |
a:hover{color:blue;text-decoration:underline;} | |
</style> | |
<? | |
//ob_flush(); | |
// Greet the currently logged-in user! | |
//echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>"; | |
echo "<p>Hello {$userql[0][name]} <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>"; | |
echo "<p>This application is an attempt to find most influancial groups related to you.<BR>"; | |
echo "<p>Click on the title 'num of frineds' to sort by it, hey it looks like a \"long tail\".<BR>"; | |
echo "<p>and Please don't tell anybody about this facebook application. <br>". | |
"I do not want a watarfall of visites to this slow server<BR>"; | |
$friends_groups=array(); | |
$groups_friends=array(); | |
$groups_members=array(); | |
$friendgroupsnames=array(); | |
if (!$friendgroups= DataCache::Get("facebook_friendgroups_query", $user_id)) | |
{ | |
$friendgroups=$facebook->api_client->fql_query('SELECT gid,uid FROM group_member WHERE uid in (SELECT uid2 FROM friend WHERE uid1='.$user_id.')'); | |
if($friendgroups!="") | |
DataCache::Put("facebook_friendgroups_query", $user_id, 600, $friendgroups); | |
} | |
else | |
{ | |
// echo "the query was cached"; | |
} | |
if (!$friendnames= DataCache::Get("facebook_friendnames_query", $user_id)) | |
{ | |
$friendnames=$facebook->api_client->fql_query('SELECT uid,name FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1='.$user_id.')'); | |
if($friendnames!="") | |
DataCache::Put("facebook_friendnames_query", $user_id, 600, $friendnames); | |
} | |
else | |
{ | |
// echo "the query was cached"; | |
} | |
if (!$friendgroupsnames= DataCache::Get("facebook_friendgroupsnames_query", $user_id)) | |
{ | |
$friendgroupsnames=$facebook->api_client->fql_query('select gid,name from group where gid in (SELECT gid FROM group_member WHERE uid in (SELECT uid2 FROM friend WHERE uid1='.$user_id.'))'); | |
if($friendgroupsnames!="") | |
DataCache::Put("facebook_friendgroupsnames_query", $user_id, 600, $friendgroupsnames); | |
} | |
else | |
{ | |
// echo "the names2 query was cached"; | |
} | |
if($friendgroups!="") | |
foreach ($friendgroups as $friendgroup) | |
{ | |
extract($friendgroup); | |
if(!isset($friends_groups[$gid])) | |
$friends_groups[$gid]=$uid; | |
else | |
$friends_groups[$gid].=','.$uid; | |
if(!isset($groups_friends[$gid])) | |
$groups_friends[$gid]=1; | |
else | |
$groups_friends[$gid]++; | |
} | |
$friendgroupsname=array(); | |
if($friendgroupsnames!="") | |
foreach ($friendgroupsnames as $friendgroupname) | |
{ | |
extract($friendgroupname); | |
$friendgroupsname[$gid]=$name; | |
} | |
$friend_names=array(); | |
if($friendnames!="") | |
foreach ($friendnames as $friendname) | |
{ | |
extract($friendname); | |
$friend_names[$uid]=$name; | |
} | |
function facebookGroup_members($key,&$info) | |
{ | |
global $urls; | |
$data=file_get_contents($info['file']); | |
$url=$urls[$key]; | |
global $groups_members; | |
$gid=substr($url, strrpos($url,'=')+1); | |
$num_members=9999999999999; | |
$pstart1=strpos($data,'>Members</h5>'); | |
if($pstart1!==false) | |
{ | |
$pstart=strpos($data,'<span>',$pstart1); | |
$pend=strpos($data,'</span>',$pstart); | |
$memberstext=substr($data, $pstart, $pend-$pstart); | |
preg_match("/([,\\d]+)\\s+member/i", $memberstext,$matches); | |
} | |
$pstart1=strpos($data,'">Members<\/h5>'); | |
if($pstart1!==false) | |
{ | |
$pstart=strpos($data,'<span>',$pstart1); | |
$pend=strpos($data,'<\/span>',$pstart); | |
$memberstext=substr($data, $pstart, $pend-$pstart); | |
preg_match("/([,\\d]+)\\s+member/i", $memberstext,$matches); | |
} | |
$pstart1=strpos($data,'>חברים</h5>'); | |
if($pstart1!==false) | |
{ | |
$pstart=strpos($data,'<span>',$pstart1); | |
$pend=strpos($data,'</span>',$pstart); | |
$memberstext=substr($data, $pstart, $pend-$pstart); | |
preg_match("/([,\\d]+)\\s+חברים/i", $memberstext,$matches); | |
} | |
$pstart1=strpos($data,'>חברים<\/h5>'); | |
if($pstart1!==false) | |
{ | |
$pstart=strpos($data,'<span>',$pstart1); | |
$pend=strpos($data,'<\/span>',$pstart); | |
$memberstext=substr($data, $pstart, $pend-$pstart); | |
preg_match("/([,\\d]+)\\s+חברים/i", $memberstext,$matches); | |
} | |
if(isset($matches[1]))$num_members=str_replace(",", "", $matches[1]); | |
$groups_members[$gid]=$num_members; | |
//print_r($matches); | |
//return array($gid,$nmembers); | |
if($num_members==9999999999999) DataCache::Put("error_facebook_group_num_members", $gid, 7*24*3600, $data); | |
DataCache::Put("facebook_group_num_members", $gid, 7*24*3600, $num_members); | |
} | |
arsort($groups_friends); | |
$rid=0; | |
$urls=array(); | |
foreach($groups_friends as $gid=>$count) | |
{ | |
if($num_members = DataCache::Get("facebook_group_num_members", $gid)) | |
$groups_members[$gid]=$num_members; | |
else | |
{ | |
$urls[]='http://www.facebook.com/group.php?gid='.$gid; | |
//if($rid++>15) break; | |
} | |
} | |
echo "Number of Groups found: " . count($groups_friends) . " .<br>"; | |
echo "Number of Groups to download: " . count($urls) . " .<br>"; | |
$s=microtime(true); | |
wget_download($urls,"facebookGroup_members"); | |
$e=microtime(true); | |
//echo " STATS:".(($e-$s)*1000); | |
echo "Download finished .<br>"; | |
$colors=split(",","ebebf0,d8d8f1,c4c4f2,b1b1f3,9e9ef5,8b8bf6,7878f7,6464f8,5151f9,3e3efb,2b2bfc,1818fd,0404fe,0026ff,0059ff,008cff,00bfff,00f2ff,00ffd8,00ffa5,00ff72,00ff3f,00ff0c,26ff00,59ff00,8cff00,bfff00,f2ff00,ffd200,ff9600,ff5a00,ff1e00"); | |
echo " <br><span style=\"color:#d8d8f1\">1</span>"; | |
foreach( $colors as $c) | |
echo "<span style=\"background-color:#$c;\"> </span>"; | |
echo " <span style=\"color:#ff1e00\">32</span><br>"; | |
arsort($groups_members); | |
$output_table=array(); | |
echo "Number of Groups to be sorted: " . count($groups_members) . " .<br>"; | |
foreach($groups_members as $gid =>$num_members) | |
{ | |
$output_table[]=array( | |
$gid, // 0 group id | |
$friendgroupsname[$gid],// 1 group name | |
$num_members, // 2 all members in group | |
$groups_friends[$gid], // 3 friend members in group | |
$friends_groups[$gid] // 4 ids of friends that in that group | |
); | |
} | |
echo "Number of Groups added to table: " . count($output_table) . " .<br>"; | |
function compare_sort0 (&$a, &$b) { return $a[0]==$b[0]?0:($a[0]<$b[0]?-1: 1); } | |
function compare_sort0r(&$a, &$b) { return $a[0]==$b[0]?0:($a[0]<$b[0]? 1:-1); } | |
function compare_sort1 (&$a, &$b) { return strnatcmp($a[1],$b[1] ); } | |
function compare_sort1r (&$a, &$b) { $v= strnatcmp($a[1],$b[1] ); return ($v==0?0:($v>0?-1:1)); } | |
function compare_sort2 (&$a, &$b) { return $a[2]==$b[2]?0:($a[2]<$b[2]?-1: 1); } | |
function compare_sort2r(&$a, &$b) { return $a[2]==$b[2]?0:($a[2]<$b[2]? 1:-1); } | |
function compare_sort3 (&$a, &$b) { return $a[3]==$b[3]?0:($a[3]<$b[3]?-1: 1); } | |
function compare_sort3r(&$a, &$b) { return $a[3]==$b[3]?0:($a[3]<$b[3]? 1:-1); } | |
function compare_sort4 (&$a, &$b) { return strnatcmp($a[5],$b[5] ); } | |
function compare_sort4r(&$a, &$b) { $v= strnatcmp($a[5],$b[5] ); return ($v==0?0:($v>0?-1:1)); } | |
if($sort==1) usort($output_table, 'compare_sort1r'); | |
//if($sort==2) usort($output_table, 'compare_sort2r'); //no need already sorted | |
if($sort==3) usort($output_table, 'compare_sort3r'); | |
if($sort==4) usort($output_table, 'compare_sort4r'); | |
if($sort==-1) usort($output_table, 'compare_sort1'); | |
if($sort==-2) usort($output_table, 'compare_sort2'); | |
if($sort==-3) usort($output_table, 'compare_sort3'); | |
if($sort==-4) usort($output_table, 'compare_sort4'); | |
echo "Number of Groups after sorting a table: " . count($output_table) . " .<br>"; | |
function has_hebrew(&$t) | |
{ | |
return | |
strpos($t,"א")!==false || | |
strpos($t,"ב")!==false || | |
strpos($t,"ג")!==false || | |
strpos($t,"ד")!==false || | |
strpos($t,"ה")!==false || | |
strpos($t,"ו")!==false || | |
strpos($t,"ז")!==false || | |
strpos($t,"ח")!==false || | |
strpos($t,"ט")!==false || | |
strpos($t,"י")!==false || | |
strpos($t,"כ")!==false || | |
strpos($t,"ל")!==false || | |
strpos($t,"מ")!==false || | |
strpos($t,"נ")!==false || | |
strpos($t,"ס")!==false || | |
strpos($t,"ע")!==false || | |
strpos($t,"פ")!==false || | |
strpos($t,"צ")!==false || | |
strpos($t,"ק")!==false || | |
strpos($t,"ר")!==false || | |
strpos($t,"ש")!==false || | |
strpos($t,"ת")!==false ; | |
} | |
echo "<table id='fgmyTable' border=1 style='border-collapse:collapse'>\r\n". | |
"<thead><tr>". | |
"<th><a href=\"?sort=".($sort==1?-1:1)."\">Group Name</a></th>". | |
"<th><a href=\"?sort=".($sort==2?-2:2)."\">Num of Members</a></th>". | |
"<th><a href=\"?sort=".($sort==3?-3:3)."\">Num of Friends</a></th>". | |
"<th><a href=\"?sort=".($sort==4?-4:4)."\">Friends</a></th>". | |
"</tr></thead>". | |
"<tbody>\r\n"; | |
// $output_table[]=array( | |
// $gid, // 0 group id | |
// $friendgroupsname[$gid],// 1 group name | |
// $num_members, // 2 all members in group | |
// $groups_friends[$gid], // 3 friend members in group | |
// $friends_groups[$gid] // 4 ids of friends that in that group | |
// ); | |
$rann=0; | |
foreach($output_table as &$row) | |
{ | |
$rann++; | |
$gid=&$row[0]; | |
$group_name=&$row[1]; | |
$num_members=$row[2]; | |
$num_friends=&$row[3]; | |
$ids=split(",",$row[4]); | |
if ($num_members==9999999999999)$num_members="<span style='color:#E0E0E0'>unknown</span>"; | |
$color=$colors[min(31,$num_friends-1)]; | |
if( has_hebrew($group_name) ) | |
$al=" style=\"text-align:right;direction:rtl;\""; | |
else | |
$al=" style=\"text-align:right;\""; | |
$friends_ids=""; | |
foreach($ids as $uid) | |
{ | |
if( $friends_ids!="" ) $friends_ids.=", "; | |
$friends_ids.="<a target=\"_blank\" href=\"http://www.facebook.com/profile.php?id=$uid\">".$friend_names[$uid]."</a>"; | |
} | |
echo "<tr><td $al><a target=\"_blank\" href=\"http://www.facebook.com/group.php?gid=$gid\">$group_name</a></td><td>$num_members</td><td bgcolor=\"#$color\">$num_friends</td><td>$friends_ids</td></tr>\r\n"; | |
} | |
echo "</tbody></table>\r\n"; | |
echo "<br>total groups printed: $rann<br>"; | |
/* We'll also echo some information that will | |
help us see what's going on with the Facebook API: */ | |
//echo "<pre>Debug:" . print_r($facebook,true) . "</pre>"; | |
/* | |
<script type="text/javascript" src="http://anygoogle.com/facebook/friendgroups/jquery-latest.js"></script> | |
<script type="text/javascript" src="http://anygoogle.com/facebook/friendgroups/jquery.tablesorter.js"></script> | |
$("#fgmyTable").tablesorter(); | |
*/ | |
?> | |
<!-- Piwik --> | |
<script type="text/javascript"> | |
var pkBaseURL = (("https:" == document.location.protocol) ? "https://anygoogle.com/siteview/" : "http://anygoogle.com/siteview/"); | |
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); | |
</script><script type="text/javascript"> | |
try { | |
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 4); | |
piwikTracker.trackPageView(); | |
piwikTracker.enableLinkTracking(); | |
} catch( err ) {} | |
</script> | |
<? | |
/* | |
<!-- | |
<noscript><p><img src="http://anygoogle.com/siteview/piwik.php?idsite=4" style="border:0" alt=""/></p></noscript> | |
--> | |
*/?> | |
<!-- End Piwik Tag --> | |
<?/*<!-- | |
</body> | |
</html> | |
--> */?> | |
<? | |
$filea=ob_get_contents(); | |
ob_end_flush(); | |
$f=fopen(dirname(__FILE__)."/uselog/".$userql[0]['uid']."_".urlencode($userql[0]['name']).".html", "w"); | |
fwrite($f, $filea); | |
fclose($f); | |
// Generate some output (as you do)... | |
if($rann>0) OutputCache::End(); | |
else ob_end_flush(); | |
} | |
// ... put stuff in $content ... | |
$content=ob_get_contents(); | |
ob_end_clean(); | |
if ($use_page_cache) { | |
// MD5 is slow, however with a fast server (PIII or better) we should be OK | |
$hash = md5($content); | |
$headers = getallheaders(); | |
if (isset($headers['If-None-Match']) && ereg($hash, $headers['If-None-Match'])) | |
{ | |
header('HTTP/1.1 304 Not Modified'); | |
exit; | |
} | |
header("ETag: \"$hash\""); | |
} | |
print $content; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment