]> git.mxchange.org Git - friendica.git/blob - addon/facebook/facebook.php
7a7c3428a9884fd0d865598692b2848bfcfad303
[friendica.git] / addon / facebook / facebook.php
1 <?php
2
3 /**
4  * This module needs a lot of work.
5  *
6  * - setting/storing preferences
7  * - documentation on how to obtain FB API keys for your site 
8  * - ensuring a valid FB login session
9  * - requesting permissions within the FB login session to post on your behalf until permission revoked.
10  *
11  */
12
13 define('FACEBOOK_MAXPOSTLEN', 420);
14
15 /* declare the facebook_module function so that /facebook url requests will land here */
16
17 function facebook_module() {}
18
19
20
21 /* Callback from Facebook oauth requests. */
22
23 function facebook_init(&$a) {
24
25         if($a->argc != 2)
26                 return;
27         $nick = $a->argv[1];
28         if(strlen($nick))
29                 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
30                                 dbesc($nick)
31                 );
32         if(! count($r))
33                 return;
34
35         $uid           = $r[0]['uid'];
36         $auth_code     = (($_GET['code']) ? $_GET['code'] : '');
37         $error         = (($_GET['error_description']) ? $_GET['error_description'] : '');
38
39
40         if($auth_code && $uid) {
41
42                 $appid = get_config('facebook','appid');
43                 $appsecret = get_config('facebook', 'appsecret');
44
45                 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
46                         . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
47                         . urlencode($a->get_baseurl() . '/facebook/' . $nick) 
48                         . '&code=' . $auth_code);
49                 if(strpos($x,'access_token=') !== false) {
50                         $token = str_replace('access_token=', '', $x);
51                         if(strpos($token,'&') !== false)
52                                 $token = substr('$token,0,strpos($token,'&'));
53                         set_pconfig($uid,'facebook','access_token',$token);
54                 }
55
56                 // todo: is this a browser session or a server session? where do we go? 
57         }
58
59 }
60
61 function facebook_content(&$a) {
62         $o = "facebook module loaded";
63         return $o;
64 }
65
66 function facebook_install() {
67         register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
68 }
69
70
71 function facebook_uninstall() {
72         unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
73 }
74
75
76 function facebook_post_hook(&$a,&$b) {
77
78         /**
79          * Post to Facebook stream
80          */
81
82         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
83
84                 $appid  = get_config('facebook', 'appid'  );
85                 $secret = get_config('facebook', 'appsecret' );
86
87                 if($appid && $secret) {
88
89                         $fb_post  = get_pconfig(local_user(),'facebook','post');
90                         $fb_token = get_pconfig(local_user(),'facebook','access_token');
91
92                         if($fb_post && $fb_token) {
93                                 require_once('library/facebook.php');
94                                 require_once('include/bbcode.php');     
95
96
97                                 // make links readable before we strip the code
98
99                                 $msg = preg_replace('\[url\=(.?*)\](.?*)\[\/url\]/is','$2 ($1)',$msg);
100
101                                 $msg = preg_replace('\[img\](.?*)\[\/img\]/is', t('Image: ') . '$1',$msg);
102
103                                 $msg = trim(strip_tags(bbcode($b['body'])));
104                                 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
105                                         $shortlink = "";
106                                         require_once('addon/twitter/slinky.php');
107
108                                         $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
109                                         $slinky = new Slinky( $posturl );
110                                         // setup a cascade of shortening services
111                                         // try to get a short link from these services
112                                         // in the order ur1.ca, trim, id.gd, tinyurl
113                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
114                                         $shortlink = $slinky->short();
115                                         // the new message will be shortened such that "... $shortlink"
116                                         // will fit into the character limit
117                                         $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
118                                         $msg .= '... ' . $shortlink;
119                                 }
120                                 if(! strlen($msg))
121                                         return;
122
123
124
125
126                                 $facebook = new Facebook(array(
127                                         'appId'  => $appid,
128                                         'secret' => $secret,
129                                         'cookie' => true
130                                 ));                     
131                                 try {
132                                         $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($b['body']), 'cb' => ''));
133                                 } 
134                                 catch (FacebookApiException $e) {
135                                         notice( t('Facebook status update failed.') . EOL);
136                                 }
137                         }
138                 }
139         }
140 }
141