aboutsummaryrefslogtreecommitdiffstats
path: root/addon/facebook/facebook.php
blob: a41344a3d00cbb7411bcb39c788a7adb91a7d2c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

/**
 * This module still needs a lot of work, but is functional today.
 * Please review this section if you upgrade because things will change.
 *
 * 1. register an API key from developer.facebook.com
 *   a. We'd be very happy if you include "Friendika" in the application name
 *      to increase name recognition.
 *   b. The url should be your site URL with a trailing slash
 *   c. Set the following values in your .htconfig.php file
 *         $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
 *         $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
 *      Replace with the settings Facebook gives you.
 * 2. Enable the facebook plugin by including it in .htconfig.php - e.g. 
 *     $a->config['system']['addon'] = 'plugin1,plugin2,facebook';
 * 3. Visit your site url + '/facebook' (e.g. http://example.com/facebook)
 *    and click 'Install Facebook posting'.
 * 4. This will ask you to login to Facebook and grant permission to the 
 *    plugin to do its stuff. Allow it to do so. 
 * 5. You're done. To turn it off visit your site's /facebook page again and
 *    'Remove Facebook posting'.
 *
 * Turn logging on (see the github Friendika wiki page 'Settings') and 
 * repeat these steps if you have trouble.
 * Vidoes and embeds will not be posted if there is no other content. Links 
 * and images will be converted to text and long posts truncated - with a link
 * to view the full post. Posts with permission settings and comments will
 * not be posted to Facebook. 
 *
 */

define('FACEBOOK_MAXPOSTLEN', 420);

/* declare the facebook_module function so that /facebook url requests will land here */

function facebook_module() {}



/* If a->argv[1] is a nickname, this is a callback from Facebook oauth requests. */

function facebook_init(&$a) {

	if($a->argc != 2)
		return;
	$nick = $a->argv[1];
	if(strlen($nick))
		$r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
				dbesc($nick)
		);
	if(! count($r))
		return;

	$uid           = $r[0]['uid'];
	$auth_code     = (($_GET['code']) ? $_GET['code'] : '');
	$error         = (($_GET['error_description']) ? $_GET['error_description'] : '');


	if($error)
		logger('facebook_init: Error: ' . $error);

	if($auth_code && $uid) {

		$appid = get_config('facebook','appid');
		$appsecret = get_config('facebook', 'appsecret');

		$x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id='
			. $appid . '&client_secret=' . $appsecret . '&redirect_uri='
			. urlencode($a->get_baseurl() . '/facebook/' . $nick) 
			. '&code=' . $auth_code);

		logger('facebook_init: returned access token: ' . $x, LOGGER_DATA);

		if(strpos($x,'access_token=') !== false) {
			$token = str_replace('access_token=', '', $x);
 			if(strpos($token,'&') !== false)
				$token = substr($token,0,strpos($token,'&'));
			set_pconfig($uid,'facebook','access_token',$token);
			set_pconfig($uid,'facebook','post','true');
		}

		// todo: is this a browser session or a server session? where do we go? 
	}

}

function facebook_content(&$a) {

	if(! local_user()) {
		notice( t('Permission denied.') . EOL);
		return '';
	}

	if($a->argc > 1 && $a->argv[1] === 'remove') {
		del_pconfig(local_user(),'facebook','post');
		notice( t('Facebook disabled') . EOL);
	}

	$appid = get_config('facebook','appid');

	if(! $appid) {
		notify( t('Facebook API key is missing.') . EOL);
		return '';
	}

	$o .= '<h3>' . t('Facebook Connect') . '</h3>';

	$o .= '<br />';

	$o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
		. $a->get_baseurl() . '/facebook/' . $a->user['nickname'] . '&scope=publish_stream,read_stream,offline_access">' . t('Install Facebook posting') . '</a><br />';

	$o .= '<a href="' . $a->get_baseurl() . '/facebook/remove' . '">' . t('Remove Facebook posting') . '</a><br />';


	return $o;
}

function facebook_install() {
	register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
}


function facebook_uninstall() {
	unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook');
}


function facebook_post_hook(&$a,&$b) {

	/**
	 * Post to Facebook stream
	 */

	logger('Facebook post');

	if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent'])) {


		$appid  = get_config('facebook', 'appid'  );
		$secret = get_config('facebook', 'appsecret' );

		if($appid && $secret) {

			$fb_post  = get_pconfig(local_user(),'facebook','post');
			$fb_token = get_pconfig(local_user(),'facebook','access_token');

			if($fb_post && $fb_token) {
				require_once('library/facebook.php');
				require_once('include/bbcode.php');	

				$msg = $b['body'];

				logger('Facebook post2: msg=' . $msg, LOGGER_DATA);

				// make links readable before we strip the code

				$msg = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is",'$2 ($1)',$msg);

				$msg = preg_replace("/\[img\](.+?)\[\/img\]/is", t('Image: ') . '$1',$msg);

				$msg = trim(strip_tags(bbcode($msg)));

				if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
					$shortlink = "";
					require_once('addon/twitter/slinky.php');

					$display_url = $a->get_baseurl() . '/display/' . $a->user['nickname'] . '/' . $b['id'];
					$slinky = new Slinky( $posturl );
					// setup a cascade of shortening services
					// try to get a short link from these services
					// in the order ur1.ca, trim, id.gd, tinyurl
					$slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
					$shortlink = $slinky->short();
					// the new message will be shortened such that "... $shortlink"
					// will fit into the character limit
					$msg = substr($msg, 0, FACEBOOK_MAXPOSTLEN - strlen($shortlink) - 4);
					$msg .= '... ' . $shortlink;
				}
				if(! strlen($msg))
					return;

				logger('Facebook post: msg=' . $msg, LOGGER_DATA);

				$postvars = array('access_token' => $fb_token, 'message' => $msg);

				$x = post_url('https://graph.facebook.com/me/feed', $postvars);
				
				logger('Facebook post returns: ' . $x, LOGGER_DEBUG);

			}
		}
	}
}