Hello i have a script that extracts the email addresses of my friends on facebook but i cannot get it to work locally. The script runs but seems to stop when trying to make a connection to facebook. Please can someone help!! thanks..
<?php
session_start();
require("font.php");
set_time_limit (60*60*24) ;
error_reporting(E_ERROR);
// Print a mass matrix
function printMas($mas) {
for($y=0;$y<sizeof($mas);$y++) {
for($x=0;$x<sizeof($mas[0]);$x++) {
echo ($mas[$y][$x] == 0) ? "<b><font color=#EEEDE5>0</font></b>" : "<b><font color=black>9</font></b>";
}
echo "<br/>";
}
}
// print a specified rectange of a mass matrix
function printMasRect($mas, $sx, $ex, $sy, $ey) {
for($y = $sy; $y < $ey; $y++) {
for($x = $sx; $x < $ex; $x++) {
echo ($mas[$y][$x] == 0) ? "<b><font color=#EEEDE5>0</font></b>" : "<b><font color=black>9</font></b>";
}
echo "<br/>";
}
}
// get the first non-blank pixel in the mas matrix pass column
function getFirstPixel($mas, $column) {
for($y = 0; $y < sizeof($mas); $y++) {
if($mas[$y][$column] != 0) return $y;
}
}
// load an image and remove all not needed pixels
function getClearImage($image, $debug = false) {
$pngFile = fopen("email.png", "w");
fputs($pngFile, $image);
fclose($pngFile);
$imageSize = @GetImageSize("email.png");
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$im = imagecreatefrompng("email.png");
unlink("email.png");
$mas = array();
for($y=0;$y<$imageHeight;$y++) {
for($x=0;$x<$imageWidth;$x++) {
$rgb = ImageColorAt($im, $x, $y);
$rgpComp = imagecolorsforindex($im, $rgb);
if($rgpComp["red"] >= 140 || $rgpComp["green"] >= 140 || $rgpComp["blue"] >= 140) {
$rgb = 0x000000;
} else $rgb = 0xFFFFFF;
imagesetpixel($im, $x, $y, $rgb);
$mas[$y][$x] = ($rgb > 0) ? 9 : 0;
}
}
//imagepng($im, "out.png");
if($debug) printMas($mas);
return $mas;
}
function checkCharBegin($mas, $charHorzEnd, $debug = false) {
$beginPixel = getFirstPixel($mas, $charHorzEnd);
$charsValues = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
"p","q","r","s","t","u","v","w","x","y","z","@","0","1","2","3","4","5","6","7","8","9","_","-",".");
for($letterIndex = 0; $letterIndex < sizeof($GLOBALS['char_global_mas']); ++$letterIndex) {
$letter = $GLOBALS['char_global_mas'][$letterIndex]["char"];
$letterBeginPixel = getFirstPixel($letter, 0);
if($debug) echo "Check for letter : $charsValues[$letterIndex]<br/>";
if($beginPixel - $letterBeginPixel + sizeof($letter) - 1 < sizeof($mas) &&
$charHorzEnd + sizeof($letter[0]) - 1 < sizeof($mas[0])) {
if($debug) echo "Valid Width<br/>";
if($beginPixel - $letterBeginPixel >= 0 &&
$beginPixel + sizeof($letter) - $letterBeginPixel < sizeof($mas)) {
if($debug) echo "Valid Height<br/>";
$same = true;
for($y = 0; $y < sizeof($letter); ++$y) {
for($x = 0; $x < sizeof($letter[0]); ++$x) {
if($mas[$y + $beginPixel - $letterBeginPixel][$x + $charHorzEnd] != $letter[$y][$x]) {
$same = false;
break;
}
}
}
if($debug) echo "SAME : $same<br/>";
if($same == true) return array("F" => true, "LetterIndex" =>$letterIndex);
}
if($debug) echo "COL : $charHorzEnd, BEGIN PIXEL : $beginPixel, LET_BEGIN : $letterBeginPixel<br/>";
}
}
return array("F" => false, "LetterIndex" => 0);
}
function parseEmail($image) {
$mas = getClearImage($image, false);
$charsValues = array("a","b","c","d","e","f","g","h","i","j","k","p","l","m","n","o",
"q","r","s","t","u","v","w","x","y","z","@","0","1","2","3","4","5","6","7","8","9","_","-",".");
//checkCharBegin($mas, 87, true); return;
$emailString = '';
$begin = 0;
while($begin < sizeof($mas[0])) {
//echo "BEG : $begin<br/>";
$res = checkCharBegin($mas, $begin);
if($res["F"] == true) {
$emailString .= $charsValues[$res["LetterIndex"]];
$begin += sizeof($GLOBALS['char_global_mas'][$res["LetterIndex"]]["char"][0]);
} else $begin++;
}
return $emailString;
}
/*$mail = "facebook.png";
echo "<img src=\"mails/$mail\"><br/>";
$emailImage = file_get_contents("mails/$mail");
echo parseEmail($emailImage);
return;
/*$d = dir("mails");
echo "<table>";
while (false !== ($entry = $d->read())) {
if($entry != '.' && $entry != '..' && $entry != 'Thumbs.db' && !is_dir($entry)) {
echo "<tr>";
echo "<td><img src=\"mails/$entry\"></td>";
$emailImage = file_get_contents("mails/$entry");
echo "<td>".parseEmail($emailImage)."</td>";
echo "</tr>";
}
}
$d->close();
echo "</table>";
return;*/
function getProfileInformation($profileId, $profile) {
$record = '';
//$record .= "\"";
/*if(preg_match('/<dt>Networks:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Networks\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";;
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Sex:<\/dt><dd>[a-z|A-Z]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Sex\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";;
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Birthday:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Birthday\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";;
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Hometown:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Hometown\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Relationship Status:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Relationship Status\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Mobile:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Mobile\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Other:<\/dt><dd>[a-z|A-Z|0-9|\040|\-|\.|_|,]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Other\:<\/dt>|<dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;z2=[^\&\;]*\&\;o=[0-9]*\">[^<\/a>]*<\/a>/i', $profile, $matches) != 0)
$record .= preg_replace('[<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;z2=[^\&\;]*\&\;o=[0-9]*\">|<\/a>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;c2=[0-9]*\&\;o=[0-9]*\">[^<]*<\/a>/i', $profile, $matches) != 0)
$record .= preg_replace('[<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;c2=[0-9]*\&\;o=[0-9]*\">|<\/a>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;a2=[^&]*\&\;o=[0-9]*\">[^<]*<\/a>/i', $profile, $matches) != 0)
$record .= preg_replace('/[<a href=\"\/s\.php\?adv\&\;k=[0-9]*\&\;n=[-|0-9]*\&\;a2=[^&]*\&\;o=[0-9]*\">|<\/a>]/i', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<a href=\"aim\:goim\?screenname=[^\"]*\"[^<]*>[^<]*<\/a>/i', $profile, $matches) != 0)
$record .= preg_replace('[<a href=\"aim\:goim\?screenname=[^\"]*\"[^<]*>|<\/a>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Google Talk\:<\/dt><dd>[^<]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Google Talk\:<\/dt><dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Windows Live\:<\/dt><dd>[^<]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Windows Live\:<\/dt><dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Skype\:<\/dt><dd>[^<]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Skype\:<\/dt><dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Yahoo\:<\/dt><dd>[^<]*<\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Yahoo\:<\/dt><dd>|<\/dd>]', '', $matches[0]);
else $record .= "Not Defined";
$record .= "\"";
$record .= ',';
$record .= "\"";
if(preg_match('/<dt>Website\:<\/dt><dd><a[^>]*>[^<]*<\/a><\/dd>/i', $profile, $matches) != 0)
$record .= preg_replace('[<dt>Website\:<\/dt><dd><a[^>]*>|<\/a><\/dd>]', '', $matches[0]);
else $record .= "Not Defined";*/
//$record .= "\"";
if(preg_match_all('/<img src=\"\/string_image.php[^>]*/i', $profile, $matches) != 0) {
$i = 0;
for(; $i < sizeof($matches[0]) && $i < 1; ++$i) {
$email = $matches[0][$i];
preg_match('/src=\"[^\"]*\"/i', $email, $imgSrc);
$emailImage = file_get_contents("http://www.facebook.com".preg_replace('[src=|\"]', '', $imgSrc[0])."<br/>");
/*$Ifile = fopen("$profileId - ".($i).".png", "w");
fputs($Ifile, $emailImage);
fclose($Ifile);*/
$record .= "\"".parseEmail($emailImage)."\"";
}
while($i++ < 1) $record .= "\"Not Defined\"";
} else {
$i = 0;
for(; $i < 1; ++$i) {
$record .= "\"Not Defined\"";
}
}
return $record;
}
$err = '';
$errstr = '';
$context = stream_context_create();
$result = stream_context_set_option($context, 'ssl', 'verify_host', true);
$result = stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
$request_data = http_build_query(array('email'=>$_REQUEST["email"],'pass'=>$_REQUEST["password"]));
$header = "POST /login.php HTTP/1.1\r\n";
$header .= "Host: login.facebook.com\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207\r\n";
$header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$header .= "Accept-Charset: windows-1256,utf-8;q=0.7,*;q=0.7\r\n";
$header .= "Accept-Language: ar,en-us;q=0.7,en;q=0.3\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Cookie: test_cookie=1\r\n";
$header .= "Content-Length: ".strlen($request_data)."\r\n";
$header .= "Connection: close\r\n\r\n";
echo "<b>Trying to connect to login.facebook.com</b><br/>"; flush(); ob_flush();
$fp = stream_socket_client("ssl://login.facebook.com:443", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
trigger_error('Connection Error : '.$errstr);
return NULL;
}
echo "<b>Connection succeeded</b><br/>"; flush(); ob_flush();
echo "<b>Sending login request</b><br/>"; flush(); ob_flush();
fputs($fp, $header);
fputs($fp, $request_data);
$loginPage = '';
while(!feof($fp)) { $loginPage .= fgets($fp, 2048); }
$res_map = explode("\n",$loginPage);
fclose($fp);
if(true) echo "<b>Login succeeded</b><br/>";
else echo "<b>Login Failed...Incorrect email or password</b><br/>";
flush(); ob_flush();
//------------------------------------------------------------------
echo "<b>Fetching and parsing friends page</b><br/>"; flush(); ob_flush();
$cookiesHeader = '';
$header = "GET /friends/?everyone&ref=tn HTTP/1.1\r\n";
$header .= "Host: www.facebook.com\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207\r\n";
$header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$header .= "Accept-Charset: windows-1256,utf-8;q=0.7,*;q=0.7\r\n";
$header .= "Accept-Language: ar,en-us;q=0.7,en;q=0.3\r\n";
$cookiesHeader = 'Cookie: ';
foreach($res_map as $res_item) {
$parts = explode(":", $res_item);
if(trim($parts[0] == 'Set-Cookie')) {
$innerParts = explode(";", $parts[1]);
$cookiesHeader .= $innerParts[0].";";
}
}
$header .= $cookiesHeader."\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = stream_socket_client("www.facebook.com:80", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
trigger_error('Connection Error : '.$errstr);
return NULL;
}
fputs($fp, $header);
$allFriendsPage = '';
while(!feof($fp)) { $allFriendsPage .= fgets($fp, 2048); }
fclose($fp);
preg_match('/You have [0-9]* friends/i', $allFriendsPage, $matches);
echo "<b>Friends page was parsed and ".$matches[0]."</b><br/>"; flush(); ob_flush();
preg_match_all('/Friends.dnd\(event, this, [0-9]*, \'[a-z|A-Z|0-9|\040|\-|\.|_]*\'\);/i', $allFriendsPage, $matches);
$csvFile = "Name,Email,\r\n";
$count = 2;
foreach($matches[0] as $match) {
//if($count == 0) break;
//$count--;
$item = str_replace(array("Friends.dnd(event, this, ", "'", ")", ";"), "", $match);
$parts = explode(",", $item);
$profileId = trim($parts[0]);
$friendName = trim($parts[1]);
echo "<b>Extracting '$friendName' Profile Information</b><br/>"; flush(); ob_flush();
$request_data = http_build_query(array('id'=>$profileId));
$header = "GET /profile.php?id=$profileId&v=info HTTP/1.1\r\n";
$header .= "Host: www.facebook.com\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207\r\n";
$header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$header .= "Accept-Charset: windows-1256,utf-8;q=0.7,*;q=0.7\r\n";
$header .= "Accept-Language: ar,en-us;q=0.7,en;q=0.3\r\n";
$header .= $cookiesHeader."\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = stream_socket_client("www.facebook.com:80", $err, $errstr, 6000, STREAM_CLIENT_CONNECT, $context);
fputs($fp, $header);
fputs($fp, $request_data);
$profilePage = '';
while(!feof($fp)) { $profilePage .= fgets($fp, 2048); }
$csvFile .= "\"".$friendName."\",".getProfileInformation($profileId, $profilePage)."\r\n";
fclose($fp);
}
echo "<b>Building the CSV file</b><br/>"; flush(); ob_flush();
//$output = fopen("friends_contact_information.csv", "w");
$output = fopen("friends_contact_information_".session_id().".csv", "w");
fputs($output, $csvFile);
fclose($output);
echo "Click <a href=\"download.php\">here</a> to download the CSV file<br/>"; //flush(); ob_flush();
?>