Advertisement
terorama

fbn / fb.inc.php

Dec 22nd, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 37.32 KB | None | 0 0
  1. <?php
  2.  
  3. require('params.inc.php');
  4. require 'fbbase.inc.php';
  5.  
  6. class fbManager {
  7.  
  8.    const DEBUGFILE = 'debugf.txt';
  9.    
  10.    const FBPAGE = 'http://www.facebook.com/UnisampleDebug';
  11.    
  12.   //----------------------------------------------------------------
  13.   const USERFQL ='select uid, username, first_name, middle_name, last_name,
  14.       name, pic_small, pic_big, pic_square, pic, affiliations, profile_update_time,
  15.       timezone, religion, birthday, birthday_date, devices, sex, hometown_location,
  16.       meeting_sex, meeting_for, relationship_status, significant_other_id, political,
  17.       current_location, activities, interests, is_app_user, music, tv, movies, books, quotes,
  18.       about_me, notes_count, wall_count, status, online_presence, locale, proxied_email,
  19.       profile_url, pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo,
  20.       pic_cover, allowed_restrictions, verified, profile_blurb, family, website, is_blocked, contact_email,
  21.       email, third_party_id, name_format, video_upload_limits, games, work, education, sports,
  22.       favorite_athletes, favorite_teams, inspirational_people, languages, likes_count,
  23.       friend_count, mutual_friend_count, can_post from user ';
  24.    
  25.    
  26.    private $apigr_reqs = array("",
  27.    "achievements","activities","albums","apprequests",
  28.    "books","checkins","events","family",
  29.       "friendrequests","friends","games","groups","interests","likes",
  30.       "links","locations","movies",
  31.       "music","notes",
  32.       "photos","photos/uploaded","picture",
  33.       "posts","questions","scores","sharedposts",
  34.       "statuses","subscribedto","subscribers",
  35.       "tagged","television","videos");
  36.    
  37.    private static $instance;
  38.    public $db;
  39.    
  40.    public $debug_mode=false;
  41.    public $debug_fname;
  42.    public $debug_array=array();
  43.    private $pgn=0;
  44.    
  45.    private $user=0;
  46.    public $loginUrl='';
  47.    public $logoutUrl='';
  48.    
  49.    //-----------------------------------------
  50.    //       debug mode on
  51.    //-----------------------------------------
  52.    public function debugOn() {
  53.    
  54.       $this->debug_mode = true;
  55.       $zuu=explode('.',fbManager::DEBUGFILE);
  56.      
  57.       $this->debug_fname = array_shift($zuu).'_'.
  58.                              date('d_m_Y_H_i_s_').rand(100,300).'.'.array_shift($zuu);
  59.                              
  60.             $_SESSION['debug_mode']=$this->debug_mode;               
  61.             $_SESSION['debug_fname']=$this->debug_fname;
  62.        
  63.        $this->dewr ('started');
  64.        
  65.        $_SESSION['pgn']=0;
  66.        
  67.        return 'debug mode switched on<br/>'.
  68.           '<a href="'.$this->debug_fname.'" target="_blank">'.$this->debug_fname.'</a><br/>';  
  69.    }
  70.    
  71.    //-----------------------------------------
  72.    //        debug mode off
  73.    //-----------------------------------------
  74.    public function debugOff() {
  75.    
  76.       $this->debug_mode = false;
  77.       $_SESSION['debug_mode'] = $this->debug_mode;
  78.      
  79.       return 'debug mode switched off<br/>';
  80.      
  81.    }
  82.    
  83.    //-----------------------------------------
  84.    //        switch debug mode
  85.    //-----------------------------------------
  86.    public function switchDebug() {
  87.    
  88.       if ($this->debug_mode) {
  89.      
  90.          return $this->debugOff();
  91.       } else {
  92.      
  93.          return $this->debugOn();
  94.       }
  95.    }
  96.    
  97.    //-----------------------------------------
  98.    //        cleanup debug files
  99.    //-----------------------------------------
  100.    public function cleanUpDebug() {
  101.    
  102.       $s = '';
  103.       $dir = opendir('.');
  104.       while ($f=readdir($dir)) {
  105.          if (preg_match('/^debugf.*?\.txt/i',$f)>0) {
  106.             $s.=$f.'<br/>';
  107.             unlink($f);
  108.             }
  109.       }
  110.      
  111.       closedir($dir);
  112.      
  113.       return 'deleted files:<br/>'.$s;
  114.    }
  115.    
  116.    //-----------------------------------------
  117.    //    write debug information
  118.    //-----------------------------------------
  119.    private function dewr($s)  {
  120.    
  121.       if (($this->debug_mode) && ($this->debug_fname)) {
  122.      
  123.          if (file_exists($this->debug_fname)) {
  124.             $fh = fopen($this->debug_fname,'a');
  125.          }
  126.          else
  127.             {
  128.                $fh = fopen ($this->debug_fname,'w');
  129.             }
  130.        
  131.          $cpath = pathinfo($_SERVER["PHP_SELF"]);
  132.          $cname = $cpath['basename'];
  133.          
  134.          fwrite($fh, date('d.m.Y H:i:s ').$cname.' '.$s.' ('.$this->pgn.')'."\r\n\r\n");
  135.          fclose($fh);
  136.                  
  137.          //$this->debug_array[]=$s;
  138.          }
  139.    }
  140.  
  141.    //-------------------------------------------get instance
  142.    public static function getInstance() {
  143.       if (!self::$instance) {
  144.          self::$instance = new fbManager();
  145.       }
  146.      
  147.       return self::$instance;
  148.    }
  149.    //------------------------------------------init facebook object
  150.    private function __construct() {
  151.    
  152.       if (isset($_SESSION['debug_fname']))
  153.          $this->debug_fname = $_SESSION['debug_fname'];
  154.          
  155.       if (isset($_SESSION['debug_mode']))
  156.          $this->debug_mode = $_SESSION['debug_mode'];
  157.    
  158.       if (isset($_SESSION['pgn'])) {
  159.      
  160.          $_SESSION['pgn']++;         
  161.          $this->pgn=$_SESSION['pgn'];
  162.       }
  163.      
  164.  
  165.       if (isset($_REQUEST["logoutapp"])) {
  166.      
  167.          unset($_SESSION['fb_'.APPID.'_access_token']);
  168.          unset($_SESSION['fb_'.APPID.'_user_id']);  
  169.       }
  170.      
  171.       if (isset($_SESSION['fb_'.APPID.'_user_id']))
  172.          $this->user = $_SESSION['fb_'.APPID.'_user_id'];
  173.       else
  174.          $this->user = $this->getUserAsync();
  175.      
  176.       $this->db = Baser::getInstance($this->user);
  177.       $this->dewr('fbManager constructed');
  178.    }
  179.    
  180.    
  181.    //--------------------------------------------------------------
  182.    //                    get urls  
  183.    //--------------------------------------------------------------
  184.    public function getUrls() {
  185.    
  186.      
  187.          $this->logoutUrl = $this->getLogoutUrlAsync();
  188.          $this->loginUrl = $this->getLoginUrlAsync();
  189.  
  190.      
  191.    }
  192.    
  193.    //--------------------------------------------------------------
  194.    //     direct calls to Baser methods
  195.    //--------------------------------------------------------------
  196.    public function __call($name, $arguments) {
  197.      
  198.       if (substr($name,0,3)=='db_') {
  199.      
  200.          $pname = substr($name,3);
  201.          
  202.          if (method_exists($this->db, $pname)) {
  203.          
  204.             $rez = call_user_func_array(array($this->db, $pname), $arguments);
  205.             return $rez;
  206.          }
  207.          else {
  208.             return 'error: unknown db method: '.$pname;
  209.          }       
  210.       }
  211.      
  212.       else
  213.          return 'error: unknown method: '.$name;
  214.          
  215.    }
  216.    
  217.    //--------------------------------------------connected
  218.    public function connected() {
  219.       return $this->user;
  220.    }
  221.    
  222.    //-----------------------------------------register error
  223.    private function saveError($e, $mtname, $args) {
  224.    
  225.       $s = 'Exception: '.$e->getType().':'.$e->getMessage()."\r\n";
  226.       $s.= 'in method: '.$mtname."\r\n";
  227.       $s.= 'arguments: '.implode(',',$args)."\r\n\r\n";
  228.      
  229.      
  230.       return $s;
  231.    }  
  232.  
  233.    //--------------------------------------get login url async
  234.    public function getUserAsync() {
  235.    
  236.      return $this->getAsync('', 'reqtype=user');     
  237.    }      
  238.    
  239.    //--------------------------------------get login url async
  240.    public function getLoginUrlAsync() {
  241.    
  242.      return $this->getAsync('http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"],
  243.                          'reqtype=loginurl');    
  244.    }  
  245.    
  246.    //--------------------------------------get logout url async
  247.    public function getLogoutUrlAsync() {
  248.    
  249.      return $this->getAsync('http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"].'?logoutapp',
  250.                          'reqtype=logouturl');   
  251.    }  
  252.    
  253.    //--------------------------------------get GraphApi node async
  254.    public function getNodeAsync($inf) {
  255.    
  256.      return $this->getAsync($inf, 'reqtype=apigraph');   
  257.    }  
  258.    
  259.    //--------------------------------------get Fql async
  260.    public function getFqlAsync($inf) {
  261.    
  262.       return $this->getAsync($inf, 'reqtype=fql');
  263.    }
  264.    
  265.    //--------------------------------------get async query
  266.    public function getAsync($inf, $reqt) {
  267.    
  268.       $cpath = pathinfo($_SERVER["PHP_SELF"]);
  269.       $cdir = $cpath['dirname'];
  270.       $url = 'http://'.$_SERVER["SERVER_NAME"].$cdir.'/async.php'.
  271.       '?ajax&'.$reqt.'&req='.urlencode($inf);
  272.      
  273.       $this->dewr('start async curl: '.$url);
  274.      
  275.       $ch = curl_init();
  276.       curl_setopt($ch, CURLOPT_HEADER,0);
  277.       curl_setopt($ch, CURLOPT_URL, $url);
  278.       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  279.       curl_setopt($ch, CURLOPT_TIMEOUT,10);
  280.       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  281.       curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
  282.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  283.      
  284.       if (isset($_COOKIE[session_name()]))
  285.          curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
  286.      
  287.       session_write_close();
  288.       $result = curl_exec($ch);
  289.          
  290.       if (curl_errno($ch)) {
  291.  
  292.          $inf = array();
  293.          $inf['err'] = 'curl error: '.curl_error($ch).'<br/>'.$url;
  294.          $this->dewr($inf['err']); 
  295.          curl_close($ch);
  296.  
  297.    
  298.          return $inf;
  299.       }
  300.       $this->dewr('curl success');
  301.       curl_close($ch);
  302.       session_start();
  303.      
  304.       $inf = unserialize($result);
  305.       return $inf;
  306.    }      
  307.    
  308.    
  309.    //------------------------------------create structure for node
  310.    
  311.    public function createNodeTable($inf, $checkifexists=false) {
  312.      
  313.          $desc = $this->getNodeAsync($inf);
  314.          echo $desc;
  315.          
  316.          if (isset($desc['err']))
  317.             return $desc;
  318.  
  319.          $tabname = 'fb_'.array_pop(explode('/',$inf));
  320.          $res = $this->db->createTable($desc, $tabname, $checkifexists );
  321.        
  322.      
  323.       return $res;
  324.          
  325.    }
  326.    
  327.    //-------------------------------------create table for fql_query
  328.    
  329.    public function createFqlTable($fql, $checkifexists=false) {
  330.      
  331.          $desc = $this->getFqlAsync($fql);
  332.          
  333.          if (isset($desc['err']))
  334.             return $desc;
  335.          
  336.          if (isset($desc[0]))
  337.             $desc = $desc[0];  
  338.            
  339.          $tname = trim(substr($fql, strpos($fql, 'from')+4));
  340.          $tname = substr($tname,0, strpos($tname,' '));
  341.          
  342.          $tname='fql_'.$tname;
  343.          
  344.          $res = $this->db->createTable($desc, $tname, $checkifexists);
  345.      
  346.       return $res;
  347.    }
  348.    
  349.    //-----------------------------------filling node table
  350.    
  351.    public function fillNodeTable($inf, $clear=true) {
  352.  
  353.          $desc = $this->getNodeAsync($inf);
  354.          
  355.          if (isset($desc['err']))
  356.             return $desc;
  357.          
  358.          if (strpos($inf,'/')===false) {
  359.          
  360.             $usr = $inf;
  361.             $tabname = 'fb_me';
  362.             }
  363.          else {
  364.             $usr = substr($inf,0,strpos($inf,'/'));
  365.             $tabname = 'fb_'.array_pop(explode('/',$inf));
  366.             }
  367.            
  368.        
  369.            
  370.          $res = $this->db->fillTable($desc, $tabname, $clear, $usr);
  371.    
  372.       return $res;
  373.    }
  374.    
  375.    //-----------------------------------fill fql table
  376.    
  377.    public function fillFqlTable($fql, $clear=true) {
  378.    
  379.          $desc = $this->getFqlAsync($fql);
  380.          
  381.           if (isset($desc['err']))
  382.             return $desc;
  383.          
  384.          
  385.          $tname = trim(substr($fql, strpos($fql,'from')+4));
  386.          $tname = substr($tname, 0, strpos($tname,' '));
  387.          $tname='fql_'.$tname;
  388.          
  389.          $res = $this->db->fillTable($desc, $tname, $clear);
  390.      
  391.       return $res;
  392.    }
  393.    
  394.    //---------------------------------insert users into fql_user
  395.    
  396.    public function insertFqlUsers($filter) {
  397.    
  398.       $inf = $this->getFqlAsync(fbManager::USERFQL.$filter);
  399.       if (isset($inf['err']))
  400.          return $inf;
  401.          
  402.       $tname='fql_user';
  403.       $this->dewr('filling fql_user table');
  404.       $res = array();
  405.       for ($i=0; $i<count($inf); $i++) {
  406.          if (!$this->db->checkQuery("select * from  $tname where fb_uid={$inf[$i]["uid"]}")) {
  407.          
  408.             $res = array_merge_recursive($res, $this->db->fillTable($inf[$i],$tname,false));
  409.          }         
  410.       }
  411.       $this->dewr('insertFqlUsers finished');
  412.       return $res;  
  413.    }
  414.    
  415.    //-------------------------insert fb_likes ->fb_base_user_id into fql_users
  416.    
  417.    public function likesBasesToFqlUsers() {
  418.    
  419.       $usrs = $this->db->fetchQueryField(
  420.          'select fb_base_user_id from fb_likes group by fb_base_user_id',
  421.          'fb_base_user_id');
  422.          
  423.       if (isset($usrs['err']))
  424.          return $usrs;
  425.          
  426.       $filt=' where uid in('.implode(',',$usrs).')';
  427.      
  428.       $rez = $this->insertFqlUsers($filt);
  429.       return $rez;
  430.  
  431.    }
  432.    //---------------------------------------------get user info
  433.    public function getUserInfo($userid='') {
  434.    
  435.       $cuser = ($userid=='') ? $this->user : $userid;
  436.      
  437.       $info = $this->db->getQuery('select fb_name, fb_pic_square, fb_uid, fb_profile_url '.
  438.       'from fql_user where fb_uid=\''.$cuser.'\'');
  439.      
  440.       if (isset($info['err']))
  441.          $info= $info['err'];
  442.          
  443.       return($info);
  444.  
  445.    }
  446.    
  447.    
  448.    //---------------------------------------find Related Users
  449.    
  450.    public function findRelatedUsers() {
  451.    
  452.       $result = array();
  453.      
  454.       $result['inf']=$this->db->getQuery(
  455.       'select a.fb_name, a.fb_category, c.fb_name, c.fb_pic_square,  '.
  456.       'c.fb_profile_url, c.fb_uid '.
  457.       'from fb_likes a, fbs_likes_users b, fql_user c '.
  458.       'where a.fb_base_user_id=b.fb_base_user_id '.
  459.       ' '.
  460.       'and a.fb_id=b.fb_like_id and b.fb_reluser_id=c.fb_uid '.
  461.       'and a.fb_base_user_id=\''.$this->user.'\'');
  462.      
  463.       $result['likes']=$this->db->getQuery(
  464.          'select * from fb_likes where fb_base_user_id=\''.$this->user.'\'');
  465.    
  466.       $result['rel_users']=$this->db->getQuery(
  467.          'select * from fbs_likes_users where fb_base_user_id=\''.$this->user.'\'');
  468.        
  469.    
  470.  
  471.       return $result;
  472.    }
  473.    
  474.    //-------------------------------------------------------------
  475.    //          draw related users page
  476.    //-------------------------------------------------------------
  477.    public function getAcceptors() {
  478.    
  479.       $result=$this->db->getQuery(
  480.          'select a.fb_base_user_id as user_id, b.fb_name as user_name,
  481.          b.fb_pic_square as userpic from fbs_likes_users a, fql_user b
  482.          where a.fb_base_user_id=b.fb_uid
  483.          group by a.fb_base_user_id, b.fb_name, b.fb_pic_square');
  484.          
  485.       return $result;
  486.    }
  487.    
  488.    //-------------------------------------------------------------
  489.    //          draw related users page
  490.    //-------------------------------------------------------------
  491.    public function drawRelatedUsers($userid, $altmode=false) {
  492.    
  493.       $userid = ($userid) ? $userid : $this->user;
  494.  
  495.       if (!$altmode) {
  496.          $result=$this->db->getQuery(
  497.          'select a.fb_name as likename, a.fb_category as likecategory, '.
  498.          'c.fb_name as username, c.fb_pic_square as userpic,  '.
  499.          'c.fb_profile_url as userurl, c.fb_uid as userid '.
  500.          'from fb_likes a, fbs_likes_users b, fql_user c '.
  501.          'where a.fb_base_user_id=b.fb_base_user_id '.
  502.          ' '.
  503.          'and a.fb_id=b.fb_like_id and b.fb_reluser_id=c.fb_uid '.
  504.          'and a.fb_base_user_id=\''.$userid.'\'');
  505.       } else {
  506.      
  507.          $result=$this->db->getQuery(
  508.          'select count(*) as lk_count, '.
  509.          'c.fb_name as username, c.fb_pic_square as userpic,  '.
  510.          'c.fb_profile_url as userurl, c.fb_uid as userid, '.
  511.          'c.fb_sex as usersex, c.fb_locale as userlocale '.
  512.          'from fb_likes a, fbs_likes_users b, fql_user c '.
  513.          'where a.fb_base_user_id=b.fb_base_user_id '.
  514.          ' '.
  515.          'and a.fb_id=b.fb_like_id and b.fb_reluser_id=c.fb_uid '.
  516.          'and a.fb_base_user_id=\''.$userid.'\''.
  517.          ' group by c.fb_name, c.fb_pic_square, c.fb_profile_url, '.
  518.          ' c.fb_uid, c.fb_sex, c.fb_locale');
  519.          
  520.       }
  521.      
  522.       if ($altmode) {
  523.          if (isset($result['err']))
  524.             $result = $result['err'];
  525.            
  526.          return $result;
  527.          }
  528.      
  529.       $s = '<table><tr><td>like</td><td>username</td><td>userpic</td></tr>';
  530.       foreach ($result as $el) {
  531.          
  532.          $s.= "<tr><td>{$el["likename"]}</td>".
  533.                 "<td>{$el["username"]}</td>".
  534.                 "<td><a href=\"{$el["userurl"]}\" target=\"_blank\">".
  535.                 "<img src=\"{$el["userpic"]}\" /></a></td></tr>";
  536.       }
  537.       $s.='</table>';
  538.      
  539.       return $s;
  540.    }
  541.    
  542.    //---------------------------------------------------
  543.    //          halt on error
  544.    //---------------------------------------------------
  545.    private function halt($err, $header='<html><head></head>') {
  546.    
  547.       if (isset($err['err'])) {
  548.      
  549.          if (strpos($err['err'],'argument is empty')!==false)
  550.             return;
  551.            
  552.          if (strpos($err['err'],'fb query returns empty result')!==false)
  553.             return;    
  554.  
  555.          if (strpos($err['err'],'Unsupported operation')!==false)
  556.             return;            
  557.            
  558.          echo $header;
  559.          echo '<body><h3>error during operation</h3>';
  560.          echo '<pre>'.print_r($err,true).'</pre></body></html>';
  561.          
  562.          exit();
  563.       }
  564.    }
  565.    //---------------------------------------------------
  566.    //          download csv file
  567.    //---------------------------------------------------
  568.    public function downloadcsv() {
  569.      
  570.      
  571.       $result=array();
  572.      
  573.       header('content-type:text/csv');
  574.       header('content-disposition: attachment; filename=allinfo.csv');
  575.      
  576.       $f = fopen('php://output','w');
  577.      
  578.       $func = create_function('$a','return strtr($a,"\n\r\t","   ");');
  579.      
  580.       $fstr= array_fill(0,50,'fld');
  581.       fputcsv($f, array_map($func,$fstr), "\t");
  582.      
  583.      
  584.       foreach ($this->apigr_reqs as $str) {
  585.          if ($str=='')
  586.             $str='me';
  587.        
  588.          $zinf = array();
  589.          $zinf[] = 'output '.$str;
  590.          
  591.          fputcsv($f, array_map($func,$zinf), "\t");
  592.          
  593.          $rez = @$this->db->getQuery('select b.fb_username, a.* from fb_'.$str.' a, fql_user b
  594.                               where a.fb_base_user_id=b.fb_uid');
  595.                              
  596.           if (isset($rez['err']))
  597.              continue;
  598.    
  599.           if (count($rez)==0)
  600.              continue;
  601.              
  602.           $z = $rez[0];
  603.           $headers = array_keys($z);
  604.           fputcsv($f, array_map($func,$headers), "\t");
  605.          
  606.           foreach ($rez as $el) {
  607.              $vals = array_values($el);
  608.              fputcsv($f, array_map($func,$vals), "\t");
  609.           }
  610.    
  611.       }
  612.      
  613.       fclose($f);
  614.    }  
  615.    //---------------------------------------------------
  616.    //         create/fill all apigraph tables
  617.    //---------------------------------------------------
  618.    public function writeApiGraphTables() {
  619.    
  620.       $header = '<html><head><script type="text/javascript">
  621.          setTimeout(function() {location.replace(
  622.          location.protocol+"//"+location.host+location.pathname+
  623.          location.search.replace("start","cont"));   
  624.          }, 1000);
  625.       </script></head>';
  626.          
  627.       $header4 = '<html><head><script type="text/javascript">
  628.      
  629.       window.onload = function() {
  630.      
  631.          var el = document.createElement("DIV");
  632.          document.body.appendChild(el);
  633.          
  634.          el.num=10;
  635.          
  636.          el.zz = setInterval(
  637.             function() {
  638.                el.innerHTML= "reload after "+el.num+" s";
  639.                el.num--;
  640.                if (el.num==0) {
  641.                   clearInterval(el.zz);
  642.                   location.replace(location.href);
  643.                }
  644.             }
  645.          ,1000)
  646.       }
  647.      
  648.       </script></head>';
  649.      
  650.       if (isset($_REQUEST['start']))
  651.          $_SESSION['step']=1;
  652.          
  653.       switch ($_SESSION['step']) {
  654.          //----------------------------------
  655.          //           step 1
  656.          //----------------------------------
  657.          case 1:
  658.             $func = create_function('$el', 'return $el[\'user_id\'];');
  659.             $z_acceptors = array_map($func,$this->getAcceptors());
  660.             array_unshift($z_acceptors,"me");
  661.      
  662.             $_SESSION['z_acceptors']=$z_acceptors;
  663.            
  664.             $_SESSION['step']+=2;
  665.             $_SESSION['step_ac']=0;
  666.             $_SESSION['step_gr']=0;
  667.            
  668.             echo $header;
  669.             echo '<body>step 1. getting acceptors</body></html>';
  670.             break;
  671.            
  672.         //----------------------------------
  673.         //           step 3
  674.         //----------------------------------
  675.         case 3:
  676.        
  677.            $acceptor = $_SESSION['z_acceptors'][$_SESSION['step_ac']];
  678.            $grnode = $this->apigr_reqs[$_SESSION['step_gr']];
  679.            
  680.            $rez = $this->fillNodeTable($acceptor.(($grnode=='')?'':'/').$grnode, true);
  681.            $this->halt($rez, $header4);
  682.            
  683.          
  684.            $tab = $this->db->drawTab('fb_'.(($grnode=='')?'me':array_pop(explode('/',$grnode))));
  685.            
  686.            //-------------------------------
  687.            $_SESSION['step_gr']++;
  688.            if ($_SESSION['step_gr']>= count($this->apigr_reqs)) {
  689.               $_SESSION['step_gr']=0;
  690.               $_SESSION['step_ac']++;
  691.              
  692.               if ($_SESSION['step_ac']>=count($_SESSION['z_acceptors'])) {
  693.                  
  694.                  $_SESSION['step']++;
  695.               }
  696.            }
  697.            
  698.            echo $header;
  699.            echo "<body>step 3. filling apigraph tables<br/>
  700.                step_ac = {$_SESSION['step_ac']} <br/>
  701.                step_gr = {$_SESSION['step_gr']} <br/><br/>
  702.                {$tab}
  703.               </body></html>";
  704.            
  705.            
  706.            break;
  707.            
  708.         //----------------------------------
  709.         //           step 4
  710.         //----------------------------------
  711.         case 4:
  712.        
  713.            echo '<html><head></head><body>operation finished</body></html>';
  714.            break;  
  715.            
  716.       }
  717.    }
  718.    
  719.    
  720.    //---------------------------------------------------
  721.    //         fill related users table
  722.    //---------------------------------------------------
  723.    public function writeRelatedUsers() {
  724.      
  725.       //header('content-type: text/html; charset=utf-8');    
  726.       //set_time_limit(0);
  727.       //ob_implicit_flush(true);  
  728.       //$exc = 1/0;
  729.       //@ini_set('zlib.output_compression',0);
  730.       //@ini_set('implicit_flush',1);    
  731.      
  732.       $this->dewr('start writeRelatedUsers');
  733.      
  734.      
  735.       $header = '<html><head><script type="text/javascript">'.
  736.                 ''.
  737.                 'setTimeout(function(){location.replace(location.href);},2000);'.
  738.                 '</script></head>';
  739.                
  740.       $header4 = '<html><head><script type="text/javascript">
  741.                  
  742.                   window.onload=function() {
  743.                  
  744.                   var inf = document.createElement(\'DIV\');
  745.                   inf.id=\'inf8\';
  746.                   document.body.appendChild(inf);
  747.                  
  748.                   inf.num=15;
  749.                  
  750.                   inf.zint = setInterval( function () {
  751.                      inf.innerHTML=\'reload after \'+inf.num+\' s\';
  752.                      inf.num--;
  753.                      
  754.                      if (inf.num==0) {
  755.                         clearInterval(inf.zint);
  756.                         location.replace(location.href);
  757.                      }}, 1000);
  758.                      
  759.                   }
  760.                
  761.                 </script></head>';         
  762.                
  763.      
  764.       $lk_addr = <<<INF
  765.          http://www.facebook.com/plugins/likebox.php?
  766.          href=https%3A%2F%2Fwww.facebook.com%2F
  767.          ##LIKEID##
  768.          &width=200&height=6000&
  769.          colorscheme=light&show_faces=true&border_color&
  770.          stream=false
  771. INF;
  772.          
  773.       $lk_addr = str_replace(array(chr(10),chr(9),chr(32)), "", $lk_addr);   
  774.      
  775.       $opts = array(
  776.             'http'=>array (
  777.                'method'=>'GET',
  778.                'header'=>"Accept: text/html\r\n".
  779.                          "User-Agent: Mozilla/5.0\r\n"));
  780.                      
  781.       $context = stream_context_create($opts);     
  782.      
  783.       //------------------------------
  784.       function restruct($val, $key) {
  785.      
  786.             $z = $key;
  787.             if ($val<4)
  788.                return $z;
  789.          }   
  790.       //------------------------------
  791.    
  792.       if (!isset($_SESSION["step"]))
  793.          $_SESSION["step"]=1;
  794.          
  795.    
  796.       //----------------------------------
  797.       switch ($_SESSION["step"]) {
  798.      
  799.       //----------------------------------
  800.       //         step 1
  801.       //----------------------------------
  802.          case 1:
  803.                        
  804.             //---------------------------        
  805.             $this->dewr('(step 1) start fill likes');
  806.             $err  = $this->fillNodeTable('me/likes');      
  807.             $this->halt($err);
  808.                        
  809.             $err = $this->createFqlTable(fbManager::USERFQL.' where uid=me()', true);
  810.             $this->halt($err);
  811.          
  812.             $like_ids = $this->db->fetchField('fb_likes','fb_id');
  813.             $this->halt($like_ids);
  814.          
  815.             $_SESSION['like_ids']=$like_ids;
  816.             $_SESSION['users_likes']=array();
  817.             $_SESSION['step_ids']=0;
  818.             $_SESSION['step']+=2;
  819.          
  820.          
  821.             echo $header;
  822.             echo $_SESSION['step'].'<br/>';
  823.             echo '<body>Start processing...'.count($like_ids).' likes</body></html>';
  824.          
  825.          break;
  826.          
  827.       //----------------------------------------------
  828.       //            step 3
  829.       //----------------------------------------------
  830.          case 3:
  831.            
  832.             $like_ids = $_SESSION['like_ids'];
  833.          
  834.             //------------------------------               
  835.             $users_likes = $_SESSION['users_likes'];
  836.  
  837.             $j= $_SESSION['step_ids'];
  838.             $this->dewr('(step 3) get widget for like objects :step_ids='.$j);
  839.             //-----------------------------    
  840.             $lk_addr3 = str_replace('##LIKEID##', $like_ids[$j], $lk_addr);
  841.                          
  842.             $users = array();    
  843.            
  844.             for ($i=0; $i<3; $i++) {
  845.                
  846.                $inf = file_get_contents($lk_addr3, false, $context);
  847.      
  848.                preg_match_all('/<div[^>]*class=[\'"]grid_item[\'"][^>]*>'.
  849.                '[^<]*<a[^>]*href=([\'"])([^\1>]*?)\1[^>]*>/ims', $inf, $out);
  850.          
  851.                $users = array_merge($users, $out[2]);
  852.             }
  853.        
  854.             $users3 = array_count_values($users);    
  855.             $users4 = array_values(array_filter(array_map('restruct', $users3, array_keys($users3))));
  856.          
  857.             $users_like=array();
  858.             $users_like['like']=$like_ids[$j];
  859.             $users_like['users']=$users4;
  860.          
  861.             $users_likes[]=$users_like;
  862.          
  863.             $_SESSION["users_likes"]=$users_likes;
  864.             $_SESSION['step_ids']++;
  865.            
  866.             if ($_SESSION['step_ids']==min(3000,count($like_ids)))
  867.                $_SESSION['step']++;
  868.          
  869.             echo $header;          
  870.             echo '<body>iteration...'.$j.' like='.$like_ids[$j].'</body></html>';                      
  871.          break;
  872.          
  873.       //----------------------------------------------
  874.       //            step 4
  875.       //----------------------------------------------
  876.          case 4:
  877.              $this->dewr('(step 4) delete from fbs_likes_users');
  878.              
  879.             $insstr = $this->db->execQuery(
  880.             'delete from fbs_likes_users where fb_base_user_id=\''.$this->user.'\'');                      
  881.            
  882.             $this->halt($insstr);
  883.            
  884.             $users_likes=$_SESSION['users_likes'];
  885.            
  886.             $likes_users = array();
  887.            
  888.             $f = create_function('$el1,$el2',
  889.                '$rez["like"]=$el1; $rez["user"]=$el2; return $rez;');
  890.            
  891.             foreach ($users_likes as $el) {
  892.            
  893.                $arr1= array_fill(0,count($el['users']),$el['like']);
  894.                $likes_users=array_merge($likes_users, array_map($f, $arr1, $el['users']));
  895.             }
  896.            
  897.             $_SESSION['step']++;
  898.             $_SESSION['step_lu']=0;
  899.             $_SESSION['likes_users']=$likes_users;
  900.            
  901.             echo $header;
  902.             echo '<body>step 4, delete previous info<br/>'.
  903.             '<pre>'.print_r($insstr,true).'</pre>'.
  904.             '</body></html>';
  905.          
  906.          break;      
  907.  
  908.       //----------------------------------------------
  909.       //            step 5
  910.       //----------------------------------------------
  911.          case 5:   
  912.      
  913.             $this->dewr('(step 5) collect user information : step_lu='.$_SESSION['step_lu']);
  914.             $likes_users = $_SESSION['likes_users'];
  915.            
  916.             $el = $likes_users[$_SESSION['step_lu']];
  917.             $reluser = $el['user'];
  918.             $reluser = substr(strrchr($reluser,'/'),1);
  919.            
  920.             $user_all = $this->getNodeAsync($reluser);
  921.             $this->halt($user_all, $header4);
  922.            
  923.             $userid = $user_all['id'];
  924.            
  925.             $user_all3 = $this->insertFqlUsers(' where username ="'.$reluser.'"'); 
  926.            
  927.             //-------------------------------------
  928.             if (isset($user_all3['err']))
  929.                {
  930.                //--------------------------ftg
  931.                $_SESSION["errorsz"]++;
  932.                echo $header4;
  933.                
  934.                echo '<body>error '.$_SESSION['errorsz'].'on step '.$_SESSION['step_lu'].'<br/>';
  935.                echo '<pre>'.print_r($user_all3['err'],true).'</pre></body></html>';
  936.                
  937.                }
  938.             else {
  939.                   $this->dewr('fill fbs_likes_users');
  940.  
  941.                   $insstr=$this->db->execQuery(
  942.                   'insert into fbs_likes_users(fb_base_user_id, fb_like_id, fb_reluser_id) '.
  943.                   ' values(\''.$this->user.'\',\''.$el['like'].'\',\''.$userid.'\')');
  944.            
  945.                   $this->halt($insstr);
  946.            
  947.                   //$out_fql_user = $this->db->drawTab('fql_user');
  948.                   $out_likes_users = $this->db->drawTab('fbs_likes_users');
  949.            
  950.                   $_SESSION['step_lu']++;
  951.            
  952.                   if ($_SESSION['step_lu']==min(8000, count($likes_users)))
  953.                      $_SESSION['step']++;
  954.                      
  955.                   $this->dewr('starting output');
  956.              
  957.                   echo $header;
  958.                   echo '<body>current step: '.$_SESSION['step_lu'].'<br/>';
  959.                   //echo 'fql_user<br/>';
  960.                   //echo $out_fql_user.'<br/><br/>';
  961.                   echo 'fbs_likes_users<br/>';
  962.                   echo $out_likes_users.'</body></html>';
  963.                  
  964.                   $this->dewr('output finished');
  965.             }
  966.  
  967.          break;
  968.          
  969.       //----------------------------------------------
  970.       //            step 6 ftg
  971.       //----------------------------------------------
  972.          case 6:   
  973.             echo '<h3>Information collected</h3>';
  974.            
  975.             $out_fql_user = $this->db->drawTab('fql_user');
  976.             $out_likes_users = $this->db->drawTab('fbs_likes_users');
  977.            
  978.             echo 'fql_user<br/>';
  979.             echo $out_fql_user.'<br/><br/>';
  980.             echo 'fbs_likes_users<br/>';
  981.             echo $out_likes_users;
  982.            
  983.            
  984.            
  985.          break;
  986.      
  987.      
  988.       //---------------------------------------------------------
  989.       }
  990.  
  991.    }
  992.    
  993.       //---------------------------------------------draw widget
  994.    public function drawWidget($widget) {
  995.    
  996.      ob_start();
  997.      //---------------------------------------
  998.      //           common part
  999.      //---------------------------------------
  1000.    ?>
  1001.    
  1002.    <html xmlns:fb="http://ogp.me/ns/fb#">
  1003.    
  1004.    <head>
  1005.       <script type="text/javascript" src="jquery.min.js"></script>
  1006.    </head>
  1007.    
  1008.    <body>
  1009.      <div id="fb-root"></div>
  1010.      
  1011.      <script>
  1012.      
  1013.      (function(d, s, id) {
  1014.             //---------------------------------------------------
  1015.             //    load all.js asynchronously
  1016.             //---------------------------------------------------
  1017.             var js;
  1018.             var fjs = d.getElementsByTagName(s)[0];
  1019.            
  1020.             if (d.getElementById(id))
  1021.                 return;
  1022.                
  1023.             js = d.createElement(s);
  1024.             js.id = id;
  1025.            
  1026.             js.src = "//connect.facebook.net/ru_RU/all.js#xfbml=1&appId=<?php echo APPID;?>";
  1027.             fjs.parentNode.insertBefore(js, fjs);
  1028.            
  1029.            }(document, 'script', 'facebook-jssdk'));
  1030.      </script>
  1031.            
  1032.      <script type="text/javascript">
  1033.             //---------------------------------------------------
  1034.             //      get widget iframe ref
  1035.             //---------------------------------------------------
  1036.             $(document).ready(
  1037.              setTimeout(
  1038.                      function() {
  1039.                        var ifr=$('.fb_iframe_widget iframe:first');
  1040.                        
  1041.                        $(ifr).parents('.fb_iframe_widget').before($('<div/>')
  1042.                          .css('word-wrap','break-word')                      
  1043.                          .html('<a href="'+ifr.get(0).src+
  1044.                            '" target="_blank">'+ifr.get(0).src+'</a>'));
  1045.                     },3000));
  1046.     </script>  
  1047.            
  1048.    
  1049. <?php  
  1050.      //------------------------------------------
  1051.      //     choose  widget
  1052.      //------------------------------------------
  1053.      
  1054.      if (isset($_REQUEST["alt3"]))
  1055.      
  1056.         $targ = fbManager::FBPAGE;
  1057.      else
  1058.         $targ = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
  1059.      
  1060.      switch($widget) {
  1061.      
  1062.        
  1063.         //---------------------------  
  1064.         case 'login_html5':
  1065.          
  1066.             echo '<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1">';
  1067.             echo '</div>';
  1068.             break;
  1069.            
  1070.         //---------------------------
  1071.         case 'login_opengraph':
  1072.        
  1073.            echo '<fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button>';          
  1074.            break;
  1075.            
  1076.         //---------------------------
  1077.         case 'activity_html5':
  1078.        
  1079.            echo '<div class="fb-activity"'.
  1080.            ' data-href="'.$targ.'"'.
  1081.            ' data-app-id="'.APPID.'"'.
  1082.            ' data-width="300" data-height="300" '.
  1083.            ' data-header="true" data-recommendations="false"></div>';
  1084.            break;
  1085.            
  1086.         //---------------------------    
  1087.         case 'activity_opengraph':
  1088.        
  1089.            echo '<fb:activity '.
  1090.            ' href="'.$targ.'" '.
  1091.            ' app_id="'.APPID.'" '.
  1092.            ' width="300" height="300" header="true" recommendations="false"></fb:activity>';
  1093.            break;
  1094.            
  1095.         //---------------------------  
  1096.         case 'activity_iframe':
  1097.        
  1098.            echo '<iframe '.
  1099.            'src="//www.facebook.com'.
  1100.            '/plugins/activity.php'.
  1101.            '?href='.urlencode($targ).'&amp;'.
  1102.            'app_id='.APPID.'&amp;'.
  1103.            'action&amp;width=300&amp;height=300&amp;header=true&amp;'.
  1104.            'colorscheme=light&amp;linktarget=_blank&amp;border_color&amp;'.
  1105.            'font&amp;recommendations=false&amp;'.
  1106.            'appId='.APPID.'" scrolling="no" frameborder="0"'.
  1107.            ' style="border:none; overflow:hidden;'.
  1108.            ' width:300px; height:300px;" allowTransparency="true"></iframe>';
  1109.            break;
  1110.            
  1111.         //---------------------------  
  1112.         case 'activity_link':
  1113.        
  1114.            $lnk = 'http://www.facebook.com/plugins/activity.php'.
  1115.            '?href='.urlencode($targ).'&'.
  1116.            'app_id='.APPID.
  1117.            '&action&width=300&height=300&'.
  1118.            'header=true&colorscheme=light&linktarget=_blank'.
  1119.            '&border_color&font&recommendations=false&appId='.APPID;
  1120.            
  1121.            echo "<a href=\"$lnk\" target=\"_blank\">$lnk</a>";
  1122.            break;
  1123.            
  1124.         //---------------------------
  1125.         case 'comments_html5':
  1126.            
  1127.            echo
  1128.            '<div class="fb-comments" '.
  1129.            ' data-href="'.$targ.'" '.
  1130.            ' data-num-posts="6" data-width="470"></div>';
  1131.            break;
  1132.            
  1133.         //---------------------------
  1134.         case 'comments_opengraph':
  1135.        
  1136.            echo '<fb:comments href="'.$targ.'"'.
  1137.            ' num_posts="6" width="470"></fb:comments>';
  1138.            break;
  1139.            
  1140.         //---------------------------
  1141.         case 'facepile_html5':
  1142.        
  1143.            echo '<div class="fb-facepile" '.
  1144.            'data-href="'.$targ.'" '.
  1145.            'data-size="large" data-max-rows="6" data-width="300"></div>';
  1146.            break;
  1147.            
  1148.         //---------------------------
  1149.         case 'facepile_opengraph':
  1150.        
  1151.            echo '<fb:facepile href="'.$targ.'" '.
  1152.            'size="large" max_rows="6" width="300"></fb:facepile>';
  1153.            break;
  1154.            
  1155.         //---------------------------  
  1156.         case 'facepile_iframe':
  1157.        
  1158.            echo '<iframe '.
  1159.            ' src="//www.facebook.com/plugins/facepile.php?'.
  1160.            'href='.urlencode($targ).'&amp;'.
  1161.            'action&amp;size=large&amp;'.
  1162.            'max_rows=6&amp;width=300&amp;'.
  1163.            'colorscheme=light&amp;appId='.APPID.
  1164.            '" scrolling="no" frameborder="0"'.
  1165.            ' style="border:none; overflow:hidden;'.
  1166.            ' width:300px;" allowTransparency="true"></iframe>';
  1167.            break;
  1168.            
  1169.         //---------------------------
  1170.         case 'facepile_link':
  1171.        
  1172.            $lnk = 'http://www.facebook.com/plugins/facepile.php?'.
  1173.            'href='.urlencode($targ).
  1174.            '&action&size=large&max_rows=6&width=300'.
  1175.            '&colorscheme=light&appId='.APPID;
  1176.            
  1177.            echo "<a href=\"$lnk\" target=\"_blank\">$lnk</a>";
  1178.            break;
  1179.            
  1180.         //---------------------------
  1181.         case 'likebox_html5':
  1182.            echo '<div class="fb-like-box" '.
  1183.            'data-href="'.$targ.'" '.
  1184.            'data-width="292" data-height="700" '.
  1185.            'data-show-faces="true" data-stream="true" '.
  1186.            'data-header="true"></div>';
  1187.            break;
  1188.          
  1189.         //---------------------------
  1190.         case 'likebox_opengraph':
  1191.            echo '<fb:like-box '.
  1192.            'href="'.$targ.'" '.
  1193.            'width="292" height="700" show_faces="true"'.
  1194.            ' stream="true" header="true"></fb:like-box>';
  1195.            break;
  1196.        
  1197.         //---------------------------
  1198.         case 'likebox_iframe':
  1199.            echo
  1200.            '<iframe src="//www.facebook.com/plugins/likebox.php'.
  1201.            '?href='.urlencode($targ).
  1202.            '&amp;width=292&amp;height=590&amp;'.
  1203.            'colorscheme=light&amp;show_faces=true&amp;'.
  1204.            'border_color&amp;stream=true&amp;'.
  1205.            'header=true&amp;appId='.APPID.'" '.
  1206.            'scrolling="no" frameborder="0" '.
  1207.            'style="border:none; overflow:hidden; '.
  1208.            'width:292px; height:590px;" allowTransparency="true"></iframe>';
  1209.            break;
  1210.            
  1211.         //----------------------------
  1212.         case 'likebox_link':
  1213.            
  1214.            $lnk = 'http://www.facebook.com/plugins/likebox.php'.'
  1215.            ?href='.urlencode($targ).'&'.
  1216.            'width=292&height=590&colorscheme=light&'.
  1217.            'show_faces=true&border_color&stream=true'.'
  1218.            &header=true&appId='.APPID;
  1219.            
  1220.            echo "<a href=\"$lnk\" target=\"_blank\">$lnk</a>";
  1221.            break;
  1222.            
  1223.         //----------------------------
  1224.         case 'likebutton_html5':   
  1225.        
  1226.            echo '<div class="fb-like" '.
  1227.            'data-href="'.$targ.'" '.
  1228.            'data-send="true" '.
  1229.            'data-width="450" '.
  1230.            'data-show-faces="true"></div>';
  1231.            break;
  1232.            
  1233.         //----------------------------
  1234.         case 'likebutton_opengraph':   
  1235.        
  1236.            echo '<fb:like href="'.$targ.'" '.
  1237.            'send="true" width="450" show_faces="true"></fb:like>';
  1238.            break;
  1239.            
  1240.         //----------------------------
  1241.         case 'recommendbar_html5':
  1242.        
  1243.            echo '<div class="fb-recommendations-bar" '.
  1244.            'data-href="'.$targ.'"></div>';
  1245.            break;
  1246.            
  1247.         //----------------------------
  1248.         case 'recommendbar_opengraph': 
  1249.        
  1250.            echo '<fb:recommendations-bar '.
  1251.            'href="'.$targ.'"></fb:recommendations-bar>';
  1252.            break;
  1253.            
  1254.         //----------------------------
  1255.         case 'recommendbox_html5':
  1256.        
  1257.            echo '<div class="fb-recommendations" '.
  1258.            'data-site="'.$targ.'" data-app-id="'.APPID.'" '.
  1259.            'data-width="300" data-height="300" data-header="true"></div>';
  1260.            break;
  1261.            
  1262.         //----------------------------
  1263.         case 'recommendbox_opengraph': 
  1264.        
  1265.            echo '<fb:recommendations site="'.$targ.'" '.
  1266.            'app_id="'.APPID.'" '.
  1267.            'width="300" height="300" header="true"></fb:recommendations>';
  1268.            break;
  1269.            
  1270.         //----------------------------
  1271.         case 'sendbutton_html5':
  1272.        
  1273.            echo '<div class="fb-send" data-href="'.$targ.'"></div>';
  1274.            break;
  1275.            
  1276.         //----------------------------
  1277.         case 'sendbutton_opengraph':
  1278.            
  1279.            echo '<fb:send href="'.$targ.'"></fb:send>';
  1280.            break;
  1281.            
  1282.         //----------------------------
  1283.         case 'sharedactivity_html5':
  1284.        
  1285.            echo '<div class="fb-shared-activity" data-width="300" '.
  1286.            'data-height="300"></div>';
  1287.            break;
  1288.        
  1289.         //----------------------------
  1290.         case 'sharedactivity_opengraph':
  1291.        
  1292.            echo '<fb:shared-activity width="300" height="300">'.
  1293.            '</fb:shared-activity>';
  1294.            break;
  1295.            
  1296.         //----------------------------
  1297.         case 'subscribebutton_html5':
  1298.        
  1299.            echo '<div class="fb-subscribe" data-href="'.$targ.'" '.
  1300.            'data-show-faces="true" data-width="450"></div>';
  1301.            break;
  1302.  
  1303.     }
  1304.    
  1305.     echo '</body></html>';
  1306.    
  1307.     $inf = ob_get_clean();
  1308.    
  1309.     return $inf;
  1310.    }
  1311.    
  1312.    
  1313.    
  1314.    
  1315.    
  1316. }
  1317.  
  1318.  
  1319.  
  1320. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement