]> git.mxchange.org Git - friendica.git/blob - addon/facebook/facebook.php
resolve file inclusion conflicts w/ multiple plugins, improve the typo checker
[friendica.git] / addon / facebook / facebook.php
1 <?php
2
3 /**
4  * This module still needs a lot of work, but is functional today.
5  * Please review this section if you upgrade because things will change.
6  * If you have issues upgrading, remove facebook from the addon list, 
7  * view a page on your site, then add it back to the list. This will reset
8  * all of the plugin 'hooks'. 
9  *
10  * 1. register an API key from developer.facebook.com
11  *   a. We'd be very happy if you include "Friendika" in the application name
12  *      to increase name recognition.
13  *   b. The url should be your site URL with a trailing slash
14  *   c. Set the following values in your .htconfig.php file
15  *         $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
16  *         $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
17  *      Replace with the settings Facebook gives you.
18  * 2. Enable the facebook plugin by including it in .htconfig.php - e.g. 
19  *     $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
20  * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook)
21  *    and click 'Install Facebook posting'.
22  * 4. This will ask you to login to Facebook and grant permission to the 
23  *    plugin to do its stuff. Allow it to do so. 
24  * 5. You're done. To turn it off visit your site's /facebook page again and
25  *    'Remove Facebook posting'.
26  *
27  * Turn logging on (see the github Friendika wiki page 'Settings') and 
28  * repeat these steps if you have trouble.
29  * Vidoes and embeds will not be posted if there is no other content. Links 
30  * and images will be converted to text and long posts truncated - with a link
31  * to view the full post. Posts with permission settings and comments will
32  * not be posted to Facebook. 
33  *
34  */
35
36 define('FACEBOOK_MAXPOSTLEN', 420);
37
38 /* declare the facebook_module function so that /facebook url requests will land here */
39
40 function facebook_module() {}
41
42
43
44 /* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */
45
46 function facebook_init(&$a) {
47
48         if($a->argc != 2)
49                 return;
50         $nick = $a->argv[1];
51         if(strlen($nick))
52                 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
53                                 dbesc($nick)
54                 );
55         if(! count($r))
56                 return;
57
58         $uid           = $r[0]['uid'];
59         $auth_code     = (($_GET['code']) ? $_GET['code'] : '');
60         $error         = (($_GET['error_description']) ? $_GET['error_description'] : '');
61
62
63         if($error)
64                 logger('facebook_init: Error: ' . $error);
65
66         if($auth_code && $uid) {
67
68                 $appid = get_config('facebook','appid');
69                 $appsecret = get_config('facebook', 'appsecret');
70
71                 $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
72                         . $appid . '&client_secret=' . $appsecret . '&redirect_uri='
73                         . urlencode($a->get_baseurl() . '/facebook/' . $nick) 
74                         . '&code=' . $auth_code);
75
76                 logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);
77
78                 if(strpos($x,'access_token=') !== false) {
79                         $token = str_replace('access_token=', '', $x);
80                         if(strpos($token,'&') !== false)
81                                 $token = substr($token,0,strpos($token,'&'));
82                         set_pconfig($uid,'facebook','access_token',$token);
83                         set_pconfig($uid,'facebook','post','1');
84                 }
85
86                 // todo: is this a browser session or a server session? where do we go? 
87         }
88
89 }
90
91 function facebook_content(&$a) {
92
93         if(! local_user()) {
94                 notice( t('Permission denied.') . EOL);
95                 return '';
96         }
97
98         if($a->argc > 1 && $a->argv[1] === 'remove') {
99                 del_pconfig(local_user(),'facebook','post');
100                 notice( t('Facebook disabled') . EOL);
101         }
102
103         $appid = get_config('facebook','appid');
104
105         if(! $appid) {
106                 notify( t('Facebook API key is missing.') . EOL);
107                 return '';
108         }
109
110         $o .= '<h3>' . t('Facebook Connect') . '</h3>';
111
112         $o .= '<br />';
113
114         $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
115                 . $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook post connector') . '</a><br /><br />';
116
117         $o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook post connector') . '</a><br />';
118
119
120         return $o;
121 }
122
123 function facebook_install() {
124         register_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
125         register_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
126         register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
127 }
128
129
130 function facebook_uninstall() {
131         unregister_hook('post_local_end',  'addon/facebook/facebook.php', 'facebook_post_hook');
132         unregister_hook('jot_networks',    'addon/facebook/facebook.php', 'facebook_jot_nets');
133         unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings');
134 }
135
136
137 function facebook_plugin_settings(&$a,&$b) {
138
139         $b .= '<h3>' . t('Facebook') . '</h3>';
140         $b .= '<a href="facebook">' . t('Facebook Connector Settings') . '</a><br />';
141
142 }
143
144 function facebook_jot_nets(&$a,&$b) {
145         if(! local_user())
146                 return;
147
148         $fb_post = get_pconfig(local_user(),'facebook','post');
149         if(intval($fb_post) == 1) {
150                 $fb_defpost = get_pconfig(local_user(),'facebook','post_by_default');
151                 $selected = ((intval($fb_defpost == 1)) ? ' selected="selected" ' : '');
152                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="facebook_enable"' . $selected . 'value="1" /> ' 
153                         . t('Post to Facebook') . '</div>';     
154         }
155 }
156
157
158 function facebook_post_hook(&$a,&$b) {
159
160         /**
161          * Post to Facebook stream
162          */
163
164         logger('Facebook post');
165
166         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {
167
168
169                 $appid  = get_config('facebook', 'appid'  );
170                 $secret = get_config('facebook', 'appsecret' );
171
172                 if($appid && $secret) {
173
174                         logger('facebook: have appid+secret');
175
176                         $fb_post   = intval(get_pconfig(local_user(),'facebook','post'));
177                         $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
178                         $fb_token  = get_pconfig(local_user(),'facebook','access_token');
179
180                         logger('facebook: $fb_post: ' . $fb_post . ' $fb_enable: ' . $fb_enable . ' $fb_token: ' . $fb_token,LOGGER_DEBUG); 
181                         if($fb_post && $fb_token && $fb_enable) {
182                                 logger('facebook: able to post');
183                                 require_once('library/facebook.php');
184                                 require_once('include/bbcode.php');     
185
186                                 $msg = $b['body'];
187
188                                 logger('Facebook post: original msg=' . $msg, LOGGER_DATA);
189
190                                 // make links readable before we strip the code
191
192                                 $msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);
193
194                                 $msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);
195
196                                 $msg = trim(strip_tags(bbcode($msg)));
197                                 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
198
199                                 if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
200                                         $shortlink = "";
201                                         require_once('library/slinky.php');
202
203                                         $display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
204                                         $slinky = new Slinky( $posturl );
205                                         // setup a cascade of shortening services
206                                         // try to get a short link from these services
207                                         // in the order ur1.ca, trim, id.gd, tinyurl
208                                         $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
209                                         $shortlink = $slinky->short();
210                                         // the new message will be shortened such that "... $shortlink"
211                                         // will fit into the character limit
212                                         $msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
213                                         $msg .= '... ' . $shortlink;
214                                 }
215                                 if(! strlen($msg))
216                                         return;
217
218                                 logger('Facebook post: msg=' . $msg, LOGGER_DATA);
219
220                                 $postvars = array('access_token' => $fb_token, 'message' => $msg);
221
222                                 $x = post_url('https://graph.facebook.com/me/feed', $postvars);
223                                 
224                                 logger('Facebook post returns: ' . $x, LOGGER_DEBUG);
225
226                         }
227                 }
228         }
229 }
230