]> git.mxchange.org Git - friendica.git/blob - addon/facebook/facebook.php
post to FB is working
[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 /* If a->argv[1] is a nickname, this is a 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($error)
41                 logger('facebook_init: Error: ' . $error);
42
43         if($auth_code && $uid) {
44
45                 $appid = get_config('facebook','appid');
46                 $appsecret = get_config('facebook', 'appsecret');
47
48                 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
49                         . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
50                         . urlencode($a->get_baseurl() . '/facebook/' . $nick) 
51                         . '&code=' . $auth_code);
52
53                 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
54
55                 if(strpos($x,'access_token=') !== false) {
56                         $token = str_replace('access_token=', '', $x);
57                         if(strpos($token,'&') !== false)
58                                 $token = substr($token,0,strpos($token,'&'));
59                         set_pconfig($uid,'facebook','access_token',$token);
60                         set_pconfig($uid,'facebook','post','true');
61                 }
62
63                 // todo: is this a browser session or a server session? where do we go? 
64         }
65
66 }
67
68 function facebook_content(&$a) {
69
70         if(! local_user()) {
71                 notice( t('Permission denied.') . EOL);
72                 return '';
73         }
74
75         if($a->argc > 1 && $a->argv[1] === 'remove') {
76                 del_pconfig(local_user(),'facebook','post');
77                 notice( t('Facebook disabled') . EOL);
78         }
79
80         $appid = get_config('facebook','appid');
81
82         if(! $appid) {
83                 notify( t('Facebook API key is missing.') . EOL);
84                 return '';
85         }
86
87         $o .= '<h3>' . t('Facebook Connect') . '</h3>';
88
89         $o .= '<br />';
90
91         $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
92                 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook posting') . '</a><br />';
93
94         $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook posting') . '</a><br />';
95
96
97         return $o;
98 }
99
100 function facebook_install() {
101         register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
102 }
103
104
105 function facebook_uninstall() {
106         unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
107 }
108
109
110 function facebook_post_hook(&$a,&$b) {
111
112         /**
113          * Post to Facebook stream
114          */
115
116         logger('Facebook post');
117
118         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
119
120
121                 $appid  = get_config('facebook', 'appid'  );
122                 $secret = get_config('facebook', 'appsecret' );
123
124                 if($appid && $secret) {
125
126                         $fb_post  = get_pconfig(local_user(),'facebook','post');
127                         $fb_token = get_pconfig(local_user(),'facebook','access_token');
128
129                         if($fb_post && $fb_token) {
130                                 require_once('library/facebook.php');
131                                 require_once('include/bbcode.php');     
132
133                                 $msg = $b['body'];
134
135                                 logger('Facebook post2: msg=' . $msg, LOGGER_DATA);
136
137                                 // make links readable before we strip the code
138
139                                 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
140
141                                 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
142
143                                 $msg = trim(strip_tags(bbcode($msg)));
144
145                                 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
146                                         $shortlink = "";
147                                         require_once('addon/twitter/slinky.php');
148
149                                         $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
150                                         $slinky = new Slinky( $posturl );
151                                         // setup a cascade of shortening services
152                                         // try to get a short link from these services
153                                         // in the order ur1.ca, trim, id.gd, tinyurl
154                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
155                                         $shortlink = $slinky->short();
156                                         // the new message will be shortened such that "... $shortlink"
157                                         // will fit into the character limit
158                                         $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
159                                         $msg .= '... ' . $shortlink;
160                                 }
161                                 if(! strlen($msg))
162                                         return;
163
164                                 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
165
166                                 $postvars = array('access_token' => $fb_token, 'message' => $msg);
167
168                                 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
169                                 
170                                 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
171
172                         }
173                 }
174         }
175 }
176