Skip to content

Commit

Permalink
fb updates
Browse files Browse the repository at this point in the history
  • Loading branch information
voitto committed May 8, 2010
1 parent e3642cb commit 995d7aa
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 28 deletions.
63 changes: 47 additions & 16 deletions app/facebook/plugins/facebook.php
Expand Up @@ -44,7 +44,7 @@ function send_to_facebook( &$model, &$rec ) {

if ($db->num_rows($result) == 1) {

// Facebook Streams http://brianjesse.com

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL & ~E_NOTICE );
Expand All @@ -55,15 +55,10 @@ function send_to_facebook( &$model, &$rec ) {
$agent = environment('facebookAppName')." (curl)";

$Post =& $db->model('Post');
add_include_path(library_path());
add_include_path(library_path().'facebook-platform/php');
add_include_path(library_path().'facebook_stream');

if (!function_exists('json_encode'))
lib_include('json');
require_once "facebook.php";
require_once "FacebookStream.php";
require_once "Services/Facebook.php";

if (!function_exists('json_encode'))
lib_include('json');



//$sesskey = environment('facebookSession');
Expand All @@ -75,10 +70,7 @@ function send_to_facebook( &$model, &$rec ) {

//$fb->api_client->session_key = $sesskey;
//$fb->api_client->user = $uid;
$fs = new FacebookStream($consumer_key,$consumer_secret,$agent,$app_id);

// $fs->api->sessionKey = $sesskey;
$fs->setSess($sesskey);

$notice_content = $rec->attributes['title'];

if ($uid && isset($_SESSION['copied_blob'])) {
Expand Down Expand Up @@ -128,8 +120,29 @@ function send_to_facebook( &$model, &$rec ) {
}



$Setting =& $db->model('Setting');

$stat = $Setting->find_by(array('name'=>'facebook_uid','person_id'=>get_person_id()));

if ($stat->exists)
$uid = $stat->value;

if ($download){


add_include_path(library_path());
add_include_path(library_path().'facebook-platform/php');
add_include_path(library_path().'facebook_stream');

require_once "facebook.php";
require_once "FacebookStream.php";
require_once "Services/Facebook.php";
$fs = new FacebookStream($consumer_key,$consumer_secret,$agent,$app_id);

// $fs->api->sessionKey = $sesskey;
$fs->setSess($sesskey);



$fs->photoUpload($download, 0, $notice_content,$uid);

Expand All @@ -151,7 +164,25 @@ function send_to_facebook( &$model, &$rec ) {

} else {
// $fb->api_client->stream_publish($notice_content);
$fs->setStatus($notice_content,$uid);


$next = $request->base;

add_include_path(library_path().'facebook_stream');
require_once "Services/Facebook.php";
db_include('helper');
db_include('facebook');
$f = new Facebook(
$consumer_key,
$consumer_secret,
$appid,
$agent,
$sesskey,
$next
);


$f->update($notice_content,$uid);
}

}
Expand Down
86 changes: 79 additions & 7 deletions db/library/dbscript/facebook.php
Expand Up @@ -115,18 +115,24 @@ function authorize_from_access() {
$this->userid = $this->api->users->getLoggedInUser();
}

function permission_to( $perm ) {
function permission_to( $perm, $uid=false, $force=false ) {
$params = array(
'ext_perm' => $perm,
'uid' => $this->userid
);
$response = $this->api->users->callMethod( 'users.hasAppPermission', $params );
$xml = simplexml_load_string($response->asXML());
$xml = (array) $xml;
if (!$xml[0]) {
if ($uid)
$params['uid'] = $uid;
if (!$force){
$response = $this->api->users->callMethod( 'users.hasAppPermission', $params );
$xml = simplexml_load_string($response->asXML());
$xml = (array) $xml;
}
if ($force || !$xml[0]) {
$url = $this->api_root . '/authorize.php';
$params = array('api_key' => Services_Facebook::$apiKey,
'v' => '1.0');
if ($uid)
$params['uid'] = $uid;
$params['ext_perm'] = $perm;
$params['next'] = $this->next;
$url = $url . '?' . http_build_query($params);
Expand All @@ -135,11 +141,29 @@ function permission_to( $perm ) {
}
}

function update( $status ) {
$this->permission_to( 'status_update' );
function publish( $status, $uid=false ) {
$this->permission_to( 'publish_stream', $uid );
$params = array(
'uid' => $this->userid,
);
if ($uid)
$params['uid'] = $uid;
if (is_bool($status) && $status === true) {
$params['clear'] = 'true';
} else {
$params['status'] = $status;
}
$res = $this->api->users->callMethod( 'stream.publish', $params );
return (intval((string)$res) == 1);
}

function update( $status, $uid=false ) {
$this->permission_to( 'status_update', $uid );
$params = array(
'uid' => $this->userid,
);
if ($uid)
$params['uid'] = $uid;
if (is_bool($status) && $status === true) {
$params['clear'] = 'true';
} else {
Expand All @@ -152,5 +176,53 @@ function update( $status ) {
function search( $string ) {
}

function getpages() {
$fieldlist = array(
'page_id',
'name'
);
$fields = implode(',',$fieldlist);
$params = array(
'uid' => $this->userid,
'api_key' => Services_Facebook::$apiKey,
'call_id' => microtime(true),
'sig' => md5("app_id=".$this->appid."session_key=". $this->api->sessionKey."source_id=".$this->userid.Services_Facebook::$secret),
'v' => '1.0',
'fields' => $fields,
'session_key' => $this->api->sessionKey
);
$pages = array();
$response = $this->api->users->callMethod( 'pages.getinfo', $params );
$xml = simplexml_load_string($response->asXML());
foreach($xml as $k=>$v){
foreach($v as $b=>$r){
if ((string) $b == 'name')
$name = (string) $r;
if ((string) $b == 'page_id')
$pid = (string) $r;
}
$pages[$pid] = array('name'=>$name);
}
return $pages;
}

function ispageadmin( $p ) {
$params = array(
'page_id' => $p,
'uid' => $this->userid,
'api_key' => Services_Facebook::$apiKey,
'call_id' => microtime(true),
'sig' => md5("app_id=".$this->appid."session_key=". $this->api->sessionKey."source_id=".$this->userid.Services_Facebook::$secret),
'v' => '1.0',
'session_key' => $this->api->sessionKey
);
//return true;
$response = $this->api->users->callMethod( 'pages.isAdmin', $params );
$xml = simplexml_load_string($response->asXML());
$xml = (array) $xml;
if (!$xml[0])
return false;
return true;
}
}

35 changes: 30 additions & 5 deletions db/library/facebook_stream/FacebookStream.php
Expand Up @@ -136,7 +136,12 @@ function getInfo($userid,$fields) {

}

function verifyPerms($userid,$perms,$path='') {
function verifyPerms($userid,$perms,$path='',$force=false) {

if ($force){
$this->showPopup(implode(',',$perms),$path,$userid);
return;
}

$showperms = array();

Expand All @@ -154,7 +159,7 @@ function verifyPerms($userid,$perms,$path='') {
}

if (count($showperms) > 0)
$this->showPopup(implode(',',$showperms),$path);
$this->showPopup(implode(',',$showperms),$path,$userid);

}

Expand Down Expand Up @@ -255,23 +260,43 @@ function showJs(){

}

function showPopup($perms,$path){
function showPopup($perms,$path,$uid=false){

$key = $this->getApiKey();

$path = $path.'xd_receiver.htm';

if ($uid)

echo <<<EOD
<script type="text/javascript">
function ondone(st) {
//alert('done '+st);
}
FB_RequireFeatures(["XFBML"], function(){
FB.Facebook.init('$key', '$path', null);
FB.ensureInit(function () {
FB.Connect.showPermissionDialog('$perms', function(accepted) { window.close(); } )
FB.Connect.showPermissionDialog('$perms', ondone, true, [$uid] );
});
});
</script>
EOD;

else

echo <<<EOD
<script type="text/javascript">
FB_RequireFeatures(["XFBML"], function(){
FB.Facebook.init('$key', '$path', null);
FB.ensureInit(function () {
FB.Connect.showPermissionDialog('$perms', function(accepted) { window.close(); } )
});
});
</script>
EOD;

}

}
Expand Down

0 comments on commit 995d7aa

Please sign in to comment.