We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

Can anybody put a facebook chat mod togeth with this code ? just a thought | Forum

Pete
Pete Feb 4 '13

<?php
error_reporting(E_ALL);

class FacebookChat {
 public $ua    = "Firefox/3.5.3 (lzyc)";
 public $cookiefile  = "cookie.txt";
 public $logged  = 0;
 
 private $email;
 private $password;
 private $attempt  = 0;
 private $html;
 
 private $uid;
 private $channel;
 private $post_form_id;
 
  function __construct($email = NULL, $password = NULL){
   
   if(!extension_loaded('curl')) die("Install curl\r\n");
   if(!extension_loaded('json')) die("Install json\r\n");
   if(version_compare(PHP_VERSION, '5.3', '<')) die("Update php to 5.3\r\n");
   
   $this->email = $email;
   $this->password = $password;
   
   $this->resetcookie();
  }


  public function login(){
   
   
   $data = array
      (
      "charset_test"  => "%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84",
      "locale"   => "en_US",
      "non_com_login" => "",
      "persistent"  => "1",
      "email"   => urlencode($this->email),
      "pass"   => urlencode($this->password),
      "charset_test" => "%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84",
      "lsd"   => "str7a"
      );
      
   $this->html = $this->postreq("http://www.facebook.com/login.php?login_attempt=1", $data, 1);
      
    
    preg_match("/<title>(.+?)<\/title>/", $this->html, $out);

    if(preg_match("/Home/", $out[1])){
        preg_match("/c_user=(.+?);/", $this->html, $out);
        $this->uid = $out[1];
      
        #preg_match("/channel(\d+)/", $this->html, $out);
        #$this->channel = $out[1];
   
        preg_match("/name=\"post_form_id\" value=\"(.+?)\"/", $this->html, $out);
        $this->post_form_id = $out[1];
   
       goto a;
     
    } else {

     $this->attempt++;
      if($this->attempt > 3){
       goto b;
      }
     $this->login();
    }
    
    a: {
     $this->logged = 1;
     return true;
    }
    
    b: return false;
  }
  
  public function buddylist()
  {
   
   if(!$this->logged) die("Are you logged?");
      
   $buddies = $this->postreq("http://www.facebook.com/ajax/chat/buddy_list.php" , array(
      "user"     => $this->uid,
      "popped_out"   => "false",
      "force_render"   => "true",
      "buddy_list"   => "1",
      "notifications"   => "0",
      "post_form_id"   => $this->post_form_id,
      "fb_dtsg"    => "90rZa",
      "post_form_id_source" => "AsyncRequest",
      "__a"     => "1"
     ));

   
    $buddies = $this->json($buddies);
    
    if(!$buddies['payload']['buddy_list']['userInfos']) die("Error\r\n");
        
    function delenableVC(&$val)
    {
     unset($val['enableVC']);
    }
    
    array_walk($buddies['payload']['buddy_list']['userInfos'], 'delenableVC');
    
    return $buddies['payload']['buddy_list']['userInfos'];

  }
  
  public function sendmsg($msg, $friend){
      
   $data = array(
      "msg_text"  => $msg,
      "msg_id"  => rand(99999, 999999),
      "to"   => $friend,
      "client_time" => time(),
      "post_form_id" => $this->post_form_id
     );
   
   print $this->postreq("http://www.facebook.com/ajax/chat/send.php", $data);
   
  }
  
  
  private function resetcookie(){
    $f = fopen($this->cookiefile, "wb");
    fclose($f); 
    
   return $f;
  }
  
  
  private function postreq($url, $postdata, $header = 0){
    $ch = curl_init($url);
    
     $opt =  curl_setopt_array ($ch, array (
        CURLOPT_REFERER   => "http://www.facebook.com",
        CURLOPT_HEADER   => $header,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST   => 1,
        CURLOPT_POSTFIELDS  => $this->postdata($postdata),
        CURLOPT_USERAGENT  => $this->ua,
        CURLOPT_COOKIEFILE  => $this->cookiefile,
        CURLOPT_COOKIEJAR  => $this->cookiefile,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_SSL_VERIFYHOST => 0
       ));

    $html = curl_exec($ch);
    curl_close($ch);
    
   return $html;
  }
  
  private function json($json)
  {
   return json_decode(substr($json, 9, strlen($json)), true);
  }
  
  private function postdata($array)
  {
   $final = NULL;
   foreach($array as $key => $val) $final .= $key."=".$val."&";
   return rtrim($final, "&");
  }
}


?>.

Michael I.
Michael I. Feb 4 '13
Pete, could you specify what chat do you want to use? I mean, provide more details, posting just raw code is not a good idea.
Pete
Pete Feb 5 '13

Its the code for facebook chat ,i thought it may give some people ides with this code ,to come up with a mod so members can chat to there facebook friends from members websites ,it would be good as a mod for members websites .

Pardesi
Pardesi Jun 13 '13
Quote from Pete

Its the code for facebook chat ,i thought it may give some people ides with this code ,to come up with a mod so members can chat to there facebook friends from members websites ,it would be good as a mod for members websites .


gr8 

i agree !

suwat pb
suwat pb Jun 16 '13
update ? new plugin to buy :-) 
dave Leader
dave Jun 18 '13

Personally if i wanted to chat with my facebook friends i would sign into facebook.  If everything we do is going to bleed or connect off of facebook then how are we going to establish ourselves as a growing entity in the social market.  If that is the case why not just change the name to Oxface (Facebooks younger brother)  lol..

 

IMO there is a whole market out there of people that hate FB and hope one day that FB will die a horrble death.  On my sites i am reaching out to that market with open arms to say we offer the great features you love without the FB issues.  

 

IMO we need more original plugins and less plugins that just exploit or bleed off of FB. 

Oxwall Accessories
Oxwall Accessories Jun 21 '13
would be great for niche sites. 
TeamX
TeamX Jul 1 '13
how to create a good chat plugin that the ordinary oxwall chat plugin