Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Trying to implement the facebook logon procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
749 committed Oct 13, 2016
1 parent b676a04 commit f4e0269
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 21 deletions.
9 changes: 9 additions & 0 deletions _build/data/transport.chunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

$chunks = array();

$chunks[1]= $modx->newObject('modChunk');
$chunks[1]->fromArray(array(
'id' => 1,
'name' => 'facebook_feed_tpl',
'description' => 'Default template for the Facebook Feed',
'snippet' => file_get_contents($sources['source_core'].'/chunks/facebook_feed_tpl.chunk.tpl'),
'properties' => '',
),'',true,true);

return $chunks;
18 changes: 18 additions & 0 deletions _build/data/transport.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,22 @@
'area' => 'App_Key'
),'',true,true);

$settings['facebook_feed.page_id']= $modx->newObject('modSystemSetting');
$settings['facebook_feed.page_id']->fromArray(array(
'key' => 'facebook_feed.page_id',
'value' => '',
'xtype' => 'textfield',
'namespace' => 'facebook_feed',
'area' => 'Page'
),'',true,true);

$settings['facebook_feed.page_token']= $modx->newObject('modSystemSetting');
$settings['facebook_feed.page_token']->fromArray(array(
'key' => 'facebook_feed.page_token',
'value' => '',
'xtype' => 'textfield',
'namespace' => 'facebook_feed',
'area' => 'Page'
),'',true,true);

return $settings;
12 changes: 12 additions & 0 deletions assets/components/facebook_feed/js/feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(!FB_Feed)
FB_Feed = {};

Ext.onReady(function() {
document.getElementById("FB_Login").onclick = function() {
FB.Login(function(response) {
if (response.authResponse) {
//reload the page to get managed pages
}
}, {scope: 'manage_pages'});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="post">
<img src="[[+img]]">
<p class="posted">by [[+name]] [[+time_ago]]</p>
<p>[[+message]]</p>
</div>
32 changes: 26 additions & 6 deletions core/components/facebook_feed/controllers/home.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Facebook_feedHomeManagerController extends Facebook_feedManagerController
* @param array $scriptProperties
*/
public function process(array $scriptProperties = array()) {

$data = $this->feed->getPages();
return '<p><a href="javascript:" id="FB_Login">Login with Facebook</a></p>'.print_r($data, true);
}
/**
* The pagetitle to put in the <title> attribute.
Expand All @@ -21,13 +22,32 @@ public function getPageTitle() {
*/
public function loadCustomCssJs() {
/*$this->addCss('url/to/some/css_file.css');
$this->addJavascript('url/to/some/javascript.js');
$this->addLastJavascript('url/to/some/javascript_load_last.js');
$this->addJavascript('url/to/some/javascript.js');*/
$this->addJavascript($this->feed->config['jsUrl'].'feed.js');
$this->addHtml('<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : \''.$this->feed->config['app_id'].'\',
xfbml : false,
version: \'v2.8\'
});
FB.getLoginStatus(function(status){
if (response.status === \'connected\') {
//remove login button
var elem = document.getElementById("FB_Login").parentNode;
elem.parentNode.removeChild(elem);
}
});
};
Ext.onReady(function() {
// We could run some javascript here
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, \'script\', \'facebook-jssdk\'));
});
</script>');*/
</script>');
}
public function getTemplateFile() { return $this->feed->config['templatesPath'].'home.tpl'; }
}
12 changes: 6 additions & 6 deletions core/components/facebook_feed/index.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public function initialize() {
$this->feed = new Feed($this->modx);
//$this->addCss($this->quip->config['cssUrl'].'mgr.css');
//$this->addJavascript($this->quip->config['jsUrl'].'quip.js');
//$this->addHtml('<script type="text/javascript">
//Ext.onReady(function() {
// Quip.config = '.$this->modx->toJSON($this->quip->config).';
// Quip.config.connector_url = "'.$this->quip->config['connectorUrl'].'";
//});
//</script>');
$this->addHtml('<script type="text/javascript">
Ext.onReady(function() {
FB_Feed.config = '.$this->modx->toJSON($this->feed->config).';
FB_Feed.config.connector_url = "'.$this->feed->config['connectorUrl'].'";
});
</script>');
return parent::initialize();
}
public function getLanguageTopics() {
Expand Down
89 changes: 81 additions & 8 deletions core/components/facebook_feed/model/fb_feed/feed.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ class Feed {
*/
public $modx = null;

/**
* The Facebook Application Secret
*/
protected $app_secret;
protected $page_token;

public $config;

function __construct(modX &$modx,array $config = array()) {
$this->modx =& $modx;

Expand All @@ -33,8 +41,62 @@ function __construct(modX &$modx,array $config = array()) {
'connectorUrl' => $assetsUrl.'connector.php',

'app_id' => $this->modx->getOption('facebook_feed.app_id', null, ''),
'app_secret' => $this->modx->getOption('facebook_feed.app_secret', null, '')
'page_id' => $this->modx->getOption('facebook_feed.page_id', null, '')
),$config);
$this->app_secret = $this->modx->getOption('facebook_feed.app_secret', null, '');
$this->page_token = $this->modx->getOption('facebook_feed.page_token', null, '');
}

function initFB() {
return new Facebook\Facebook([
'app_id' => $this->config['app_id'],
'app_secret' => $this->app_secret,
'default_graph_version' => 'v2.8',
'default_access_token' => '300618826991721|sPBUAkM_V0ok3SbpYHy0iwWtSCY'
]);
}

function getExtendedToken($fb) {
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
return '';
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
return '';
}

if($accessToken){
$oAuth2Client = $fb->getOAuth2Client();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
return '';
}
}
$_SESSION['FBF_Token'] = $accessToken;
return $accessToken;
}
}

function getPages() {
$fb = $this->initFB();

$accessToken = $_SESSION['FBF_Token'];

if(!$accessToken)
$accessToken = $this->getExtendedToken($fb);
if(!$accessToken)
return '';
$response = $fb->get('/me/accounts?fields=access_token,name&access_token='.$accessToken->getValue());
return $response->getGraphEdge();
}

function runFeed($scriptProperties) {
Expand All @@ -45,14 +107,25 @@ function runFeed($scriptProperties) {
'tpl' => 'facebook_feed_tpl'
), $scriptProperties);

$fb = new Facebook\Facebook([
'app_id' => $this->config['app_id'],
'app_secret' => $this->config['app_secret'],
'default_graph_version' => 'v2.8',
]);
$fb = $this->initFB();
$response = $fb->get('/' . $config['page'] . '/feed?fields=full_picture,created_time,id,message,description,story,likes.limit(2).summary(true),shares,comments_mirroring_domain,comments,link&summary=true&limit=' . $config['limit']);
$graphObject = $response->getGraphEdge();
$output = print_r($graphObject, true);
$data = $response->getDecodedBody()['data'];
foreach ($data as $post) {
$pinfo = array();
$pinfo['img'] = $post['full_picture'];
$pinfo['name'] = $post['name'];
//$pinfo['time_ago'] = $this->calcTimeAgo($post['created_time']);
if(isset($post['message'])){
$pinfo['message'] = nl2br($post['message']);
} else if(isset($post['description'])) {
$pinfo['message'] = nl2br($post['description']);
} else {
//ignore other types of posts
continue;
}
$output .= $modx->getChunk($config['tpl'], $pinfo);
}
$output .= print_r($data,true);
return $output;
}
}
2 changes: 1 addition & 1 deletion core/components/facebook_feed/templates/home.tpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h2 class="modx-page-header">It\'s alive!</h2><p>This is your first custom manager page. You are awesome!</p>
<p><a href="javascript:" id="FB_Login">Login with Facebook</a></p>

0 comments on commit f4e0269

Please sign in to comment.