Development

Changeset 5654

You must first sign up to be able to contribute.

Changeset 5654

Show
Ignore:
Timestamp:
10/24/07 00:12:45 (1 year ago)
Author:
kupokomapa
Message:

[sfFacebookPlatformPlugin] Many changes to the plugin making it really support facebook platform and not just include several helpers. Still need to update the read me but wanted to get the code commited.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfFacebookPlatformPlugin/lib/helper/FBMLHelper.php

    r4862 r5654  
    11<?php 
    22 
    3 function fb_action($text = '', $internal_uri ='', $options = array()) 
     3sfLoader::loadHelpers(array('Javascript')); 
     4 
     5 
     6function fb_include_title() 
    47{ 
    5    
    6   $options['href'] = _fb_url($internal_uri); 
    7   return content_tag('fb:action', $text, $options)
     8  $title = sfContext::getInstance()->getResponse()->getTitle(); 
     9 
     10  echo content_tag('fb:title', $title)."\n"
    811} 
    912 
    10 function fb_form_tag($action) 
     13 
     14function fb_include_javascripts() 
    1115{ 
    12   $url = _fb_url($action); 
    13   return form_tag($action); 
     16  $response = sfContext::getInstance()->getResponse(); 
     17  $response->setParameter('javascripts_included', true, 'symfony/view/asset'); 
     18 
     19  $already_seen = array(); 
     20  $js = ''; 
     21 
     22  foreach (array('first', '', 'last') as $position) 
     23  { 
     24    foreach ($response->getJavascripts($position) as $files) 
     25    { 
     26      if (!is_array($files)) 
     27      { 
     28        $files = array($files); 
     29      } 
     30 
     31      foreach ($files as $file) 
     32      { 
     33        $file = javascript_path($file); 
     34 
     35        if (isset($already_seen[$file])) continue; 
     36 
     37        $already_seen[$file] = 1; 
     38         
     39        // TODO: the following logic needs to be fixed the tested. For now it works OK. 
     40        global $sf_symfony_data_dir; 
     41 
     42        $webdir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'web'; 
     43        $dir    = $webdir.DIRECTORY_SEPARATOR.'js'; 
     44 
     45        if (substr($file, 0, 4) == '/sf/') { 
     46          $file = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.$file; 
     47        } else if (substr($file, 0, 3) == 'sf/') { 
     48          $file = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.$file; 
     49        } else if (0 === strpos($file, '/')) { 
     50          $file = realpath($webdir.$file); 
     51        } else if (eregi('^/?sf(.*)Plugin', $file)) { 
     52          $file = realpath($webdir.DIRECTORY_SEPARATOR.$file); 
     53        } else { 
     54          $file = realpath($dir.DIRECTORY_SEPARATOR.$file); 
     55        } 
     56        if (is_readable($file)) { 
     57          $js .= file_get_contents($file); 
     58        } 
     59      } 
     60    } 
     61  } 
     62   
     63  echo content_tag('script', $js); 
    1464} 
    1565 
    16 function _fb_url($internal_uri) 
     66/** 
     67 * Return one <style> tag for all stylesheets configured in view.yml or added to the response object. * 
     68 * You can use this helper to decide the location of stylesheets in pages. 
     69 * By default, if you don't call this helper, symfony will automatically include stylesheets before </head>. 
     70 * Calling this helper disables this behavior. 
     71 * 
     72 * @return string <style> tag 
     73 */ 
     74function fb_include_stylesheets() 
    1775{ 
    18   if (!is_array($internal_uri) && preg_match('#^[a-z]+\://#', $internal_uri)) 
     76  $response = sfContext::getInstance()->getResponse(); 
     77  $response->setParameter('stylesheets_included', true, 'symfony/view/asset'); 
     78 
     79  $css = ''; 
     80  $already_seen = array(); 
     81  foreach (array('first', '', 'last') as $position) 
    1982  { 
    20     $url = $internal_uri; 
    21   }  
    22   else  
    23   { 
    24     $host = sfConfig::get('app_facebook_canvas_url'); 
    25     $sf_no_script_name = sfConfig::get('sf_no_script_name'); 
    26     sfConfig::set('sf_no_script_name', true); 
    27     $path = url_for($internal_uri); 
    28     sfConfig::set('sf_no_script_name', $sf_no_script_name); 
     83    foreach ($response->getStylesheets($position) as $files => $options) 
     84    { 
     85      if (!is_array($files)) { 
     86        $files = array($files); 
     87      } 
    2988 
    30     $url = rtrim($host, '/') . $path; 
     89      foreach ($files as $file) 
     90      { 
     91        if (isset($already_seen[$file])) { 
     92          continue; 
     93        } else { 
     94          $already_seen[$file] = 1; 
     95        } 
     96 
     97        $absolute = isset($options['absolute']); 
     98        unset($options['absolute']); 
     99 
     100        if(!isset($options['raw_name'])) 
     101        { 
     102          $file = stylesheet_path($file, $absolute); 
     103        } 
     104        else 
     105        { 
     106          unset($options['raw_name']); 
     107        } 
     108         
     109        // TODO: the following logic needs to be fixed the tested. For now it works OK. 
     110        global $sf_symfony_data_dir; 
     111 
     112        $webdir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'web'; 
     113        $dir    = $webdir.DIRECTORY_SEPARATOR.'css'; 
     114         
     115        if (substr($file, 0, 4) == '/sf/') { 
     116          $file = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.$file; 
     117        } else if (substr($file, 0, 3) == 'sf/') { 
     118          $file = $sf_symfony_data_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.$file; 
     119        } else if (0 === strpos($file, '/')) { 
     120          $file = realpath($webdir.$file); 
     121        } else if (eregi('^sf(.*)Plugin', $file)) { 
     122          $file = realpath($webdir.DIRECTORY_SEPARATOR.$file); 
     123        } else { 
     124          $file = realpath($dir.DIRECTORY_SEPARATOR.$file); 
     125        } 
     126        if (is_readable($file)) { 
     127          $css .= file_get_contents($file); 
     128        } 
     129      } 
     130    } 
    31131  } 
    32   return $url; 
     132 
     133  echo "\n".tag('style', array('type' => 'text/css'), true)."\n".$css."\n</style>\n"; 
    33134} 
    34135 
    35 function fb_link_to($text = '', $internal_uri ='', $options = array()
     136function fb_link_to_remote($name, $options
    36137{ 
    37   $url = _fb_url($internal_uri); 
    38   return link_to($text, $url, $options); 
     138  if (isset($options['update'])) { 
     139    $update = $options['update']; 
     140    unset($options['update']); 
     141  } else { 
     142    return false; 
     143  } 
     144   
     145  if (isset($options['url'])) { 
     146    $url = url_for($options['url'], true); 
     147    unset($options['url']); 
     148  } else { 
     149    return false; 
     150  } 
     151   
     152  if (isset($options['loading'])) { 
     153    $options['onLoading'] = "function() { ".$options['loading']."; }"; 
     154    unset($options['loading']); 
     155  } 
     156  if (isset($options['complete'])) { 
     157    $options['onComplete'] = "function() { ".$options['complete']."; }"; 
     158    unset($options['complete']); 
     159  } 
     160  if (!isset($options['type'])) { 
     161    $options['type'] = 'Ajax.RAW'; 
     162  } 
     163  $options = _options_for_javascript($options); 
     164   
     165  return link_to_function($name, sprintf("fb_ajax_updater('%s', '%s', %s)", $update, $url,  $options), $options); 
    39166} 
  • plugins/sfFacebookPlatformPlugin/lib/sfFacebook.class.php

    r4862 r5654  
    33class sfFacebook 
    44{ 
    5   protected static 
    6     $instance          = null; 
     5  private $facebook = null; 
    76   
    8   private $facebook = null; 
    9      
     7  private static $instance = null; 
     8  private static $is_valid = false; 
     9   
     10  const NAMESPACE = 'fb_sig'; 
     11 
    1012  public static function getInstance() 
    1113  { 
    12     if (!isset(self::$instance)) 
     14    $class = __CLASS__; 
     15    if (!(self::$instance instanceof $class)) 
    1316    { 
    14       $class = __CLASS__; 
    1517      self::$instance = new $class(); 
    1618      self::$instance->initialize(); 
     
    1921    return self::$instance; 
    2022  } 
     23 
     24  public static function isFacebookRequest() 
     25  { 
     26    if (isset($_POST['fb_sig']))  
     27    { 
     28      $fb_sig = $_POST['fb_sig']; 
     29      $params = $_POST; 
     30    }  
     31    elseif (isset($_GET['fb_sig'])) 
     32    { 
     33      $fb_sig = $_GET['fb_sig']; 
     34      $params = $_GET; 
     35    }  
     36    else 
     37    { 
     38      return false; 
     39    } 
     40     
     41    if (empty($fb_sig)) { 
     42      return false; 
     43    } else if (self::$is_valid === true) { 
     44      return true; 
     45    } 
     46     
     47    $facebook = self::getInstance(); 
     48    $params = $facebook->get_valid_fb_params($params, null, self::NAMESPACE); 
     49 
     50    return self::$is_valid = $facebook->verify_signature($params, $fb_sig); 
     51  } 
    2152   
     53  public static function generateQueryString() 
     54  { 
     55    $params = (isset($_POST['fb_sig']))?$_POST:$_GET; 
     56    if (isset($params[self::NAMESPACE.'_in_canvas']) && isset($params[self::NAMESPACE.'_in_iframe'])) { 
     57      unset($params[self::NAMESPACE.'_in_iframe']); 
     58    } 
     59     
     60    $facebook = self::getInstance(); 
     61    $params = $facebook->get_valid_fb_params($params, null, self::NAMESPACE); 
     62    unset($params['friends'], $params['in_canvas']); 
     63    $params['in_iframe'] = 1; 
     64    $fb_sig = $facebook->generate_sig($params, $facebook->secret); 
     65 
     66    foreach ($params as $key => $value)  
     67    { 
     68      unset($params[$key]); 
     69      $params[sfFacebook::NAMESPACE.'_'.$key] = $value; 
     70    } 
     71    $params[self::NAMESPACE] = $fb_sig; 
     72     
     73    return http_build_query($params, '', '&'); 
     74  } 
    2275   
    2376  protected function initialize() 
     
    3184    require_once dirname(__FILE__).'/facebook-platform/client/facebook.php'; 
    3285   
    33     $appapikey      = sfConfig::get('app_facebook_api_key'); 
    34     $appsecret      = sfConfig::get('app_facebook_api_secret'); 
     86    $appapikey = sfConfig::get('app_facebook_api_key'); 
     87    $appsecret = sfConfig::get('app_facebook_api_secret'); 
    3588 
    3689    if (!$appapikey) 
     
    3891      throw new sfException("app_facebook_api_key not defined in app.yml"); 
    3992    } 
    40  
    41     if (!$appsecret) 
     93    else if (!$appsecret) 
    4294    { 
    4395      throw new sfException("app_facebook_api_secret not defined in app.yml"); 
     
    4597 
    4698    $this->facebook = new Facebook($appapikey, $appsecret); 
    47  
    4899  } 
    49100   
    50    
    51   public function __call($m,$a)  
     101  public function __call($m, $a) 
    52102  { 
    53     return call_user_func_array(array($this->facebook, $m), $a); 
     103    return call_user_func_array(array($this->facebook, $m), $a); 
    54104  } 
    55105   
  • plugins/sfFacebookPlatformPlugin/lib/sfFacebookFilter.class.php

    r4862 r5654  
    11<?php 
    22 
    3   #doc 
    4   # classname:  sfFacebookFilter 
    5   # scope:    PUBLIC 
    6   # 
    7   #/doc 
     3class sfFacebookFilter extends sfFilter 
     4
     5  public function execute($filterChain) 
     6  { 
     7    // We want to store as much data as we can about the facebook user 
     8    // at least get the FB id, and then we can take care of the rest.   
     9    if ($this->isFirstCall()) 
     10    { 
     11      $user = $this->getContext()->getUser(); 
     12     
     13      $facebook = sfFacebook::getInstance(); 
    814 
    9   class sfFacebookFilter extends sfFilter 
    10   { 
    11     public function execute ($filterChain) 
    12     { 
    13        
    14       // We want to store as much data as we can about the facebook user 
    15       // at least get the FB id, and then we can take care of the rest.   
    16       if ($this->isFirstCall()) 
     15      if (sfConfig::get('app_facebook_require') == 'add') { 
     16        $facebook_id = $facebook->require_add(); 
     17      } else { 
     18        $facebook_id = $facebook->require_login(); 
     19      } 
     20      $user->setAttribute('id', $facebook_id, 'facebook'); 
     21 
     22      // catch the exception that gets thrown if the cookie has an invalid session_key in it 
     23      try  
    1724      { 
    18         $user = $this->getContext()->getUser(); 
    19        
    20         $this->facebook = sfFacebook::getInstance(); 
    21         // our Facebook ID 
    22         $this->user     = $this->facebook->require_login(); 
    23  
    24         $user->setAttribute('id', $this->user, 'facebook'); 
    25  
    26         
    27  
    28         $appcallbackurl = sfConfig::get('app_facebook_callback_url');   
    29  
    30         //catch the exception that gets thrown if the cookie has an invalid session_key in it 
    31         try  
     25        if (sfConfig::get('app_facebook_require') == 'add')  
    3226        { 
    33           if (!$this->facebook->api_client->users_isAppAdded()) { 
    34             $this->facebook->redirect($facebook->get_add_url()); 
     27          if (!$facebook->api_client->users_isAppAdded()) { 
     28            $facebook->redirect($facebook->get_add_url()); 
    3529          } 
    36         }  
    37         catch (Exception $ex)  
    38         { 
    39           //this will clear cookies for your application and redirect them to a login prompt 
    40           $this->facebook->set_user(null, null); 
    41           $this->facebook->redirect($appcallbackurl); 
     30        } 
     31      }  
     32      catch (Exception $e)  
     33      { 
     34        //this will clear cookies for your application and redirect them to a login prompt 
     35        $facebook->set_user(null, null); 
     36        if ($callback_url = sfConfig::get('app_facebook_callback_url')) { 
     37          $facebook->redirect($callback_url); 
    4238        } 
    4339      } 
     40    } 
    4441 
    45  
    46       $filterChain->execute(); 
    47     } 
     42    $filterChain->execute(); 
    4843  } 
    49   ### 
     44