+
+function fb_get_app_access_token() {
+
+ $acc_token = get_config('facebook','app_access_token');
+
+ if ($acc_token !== false) return $acc_token;
+
+ $appid = get_config('facebook','appid');
+ $appsecret = get_config('facebook', 'appsecret');
+
+ if ($appid === false || $appsecret === false) {
+ logger('fb_get_app_access_token: appid and/or appsecret not set', LOGGER_DEBUG);
+ return false;
+ }
+ logger('https://graph.facebook.com/oauth/access_token?client_id=' . $appid . '&client_secret=' . $appsecret . '&grant_type=client_credentials', LOGGER_DATA);
+ $x = fetch_url('https://graph.facebook.com/oauth/access_token?client_id=' . $appid . '&client_secret=' . $appsecret . '&grant_type=client_credentials');
+
+ if(strpos($x,'access_token=') !== false) {
+ logger('fb_get_app_access_token: returned access token: ' . $x, LOGGER_DATA);
+
+ $token = str_replace('access_token=', '', $x);
+ if(strpos($token,'&') !== false)
+ $token = substr($token,0,strpos($token,'&'));
+
+ if ($token == "") {
+ logger('fb_get_app_access_token: empty token: ' . $x, LOGGER_DEBUG);
+ return false;
+ }
+ set_config('facebook','app_access_token',$token);
+ return $token;
+ } else {
+ logger('fb_get_app_access_token: response did not contain an access_token: ' . $x, LOGGER_DATA);
+ return false;
+ }
+}
+
+function facebook_subscription_del_users() {
+ $a = get_app();
+ $access_token = fb_get_app_access_token();
+
+ $url = "https://graph.facebook.com/" . get_config('facebook', 'appid' ) . "/subscriptions?access_token=" . $access_token;
+ facebook_delete_url($url);
+
+ if (!facebook_check_realtime_active()) del_config('facebook', 'realtime_active');
+}
+
+function facebook_subscription_add_users($second_try = false) {
+ $a = get_app();
+ $access_token = fb_get_app_access_token();
+
+ $url = "https://graph.facebook.com/" . get_config('facebook', 'appid' ) . "/subscriptions?access_token=" . $access_token;
+
+ list($usec, $sec) = explode(" ", microtime());
+ $verify_token = sha1($usec . $sec . rand(0, 999999999));
+ set_config('facebook', 'cb_verify_token', $verify_token);
+
+ $cb = $a->get_baseurl() . '/facebook/?realtime_cb=1';
+
+ $j = post_url($url,array(
+ "object" => "user",
+ "fields" => "feed,friends",
+ "callback_url" => $cb,
+ "verify_token" => $verify_token,
+ ));
+ del_config('facebook', 'cb_verify_token');
+
+ if ($j) {
+ $x = json_decode($j);
+ logger("Facebook reponse: " . $j, LOGGER_DATA);
+ if (isset($x->error)) {
+ logger('facebook_subscription_add_users: got an error: ' . $j);
+ if ($x->error->type == "OAuthException" && $x->error->code == 190) {
+ del_config('facebook', 'app_access_token');
+ if ($second_try === false) facebook_subscription_add_users(true);
+ }
+ } else {
+ logger('facebook_subscription_add_users: sucessful');
+ if (facebook_check_realtime_active()) set_config('facebook', 'realtime_active', 1);
+ }
+ };
+}
+
+function facebook_subscriptions_get() {
+
+ $access_token = fb_get_app_access_token();
+ if (!$access_token) return null;
+
+ $url = "https://graph.facebook.com/" . get_config('facebook', 'appid' ) . "/subscriptions?access_token=" . $access_token;
+ $j = fetch_url($url);
+ $ret = null;
+ if ($j) {
+ $x = json_decode($j);
+ if (isset($x->data)) $ret = $x->data;
+ }
+ return $ret;
+}
+
+
+function facebook_check_realtime_active() {
+ $ret = facebook_subscriptions_get();
+ if (is_null($ret)) return false;
+ if (is_array($ret)) foreach ($ret as $re) if (is_object($re) && $re->object == "user") return true;
+ return false;
+}
+
+
+
+
+// DELETE-request to $url
+
+if(! function_exists('facebook_delete_url')) {
+function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
+ $a = get_app();
+ $ch = curl_init($url);
+ if(($redirects > 8) || (! $ch))
+ return false;
+
+ curl_setopt($ch, CURLOPT_HEADER, true);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
+ curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
+
+ if(intval($timeout)) {
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+ }
+ else {
+ $curl_time = intval(get_config('system','curl_timeout'));
+ curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
+ }
+
+ if(defined('LIGHTTPD')) {
+ if(!is_array($headers)) {
+ $headers = array('Expect:');
+ } else {
+ if(!in_array('Expect:', $headers)) {
+ array_push($headers, 'Expect:');
+ }
+ }
+ }
+ if($headers)
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+
+ $check_cert = get_config('system','verifyssl');
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
+ $prx = get_config('system','proxy');
+ if(strlen($prx)) {
+ curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
+ curl_setopt($ch, CURLOPT_PROXY, $prx);
+ $prxusr = get_config('system','proxyuser');
+ if(strlen($prxusr))
+ curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
+ }
+
+ $a->set_curl_code(0);
+
+ // don't let curl abort the entire application
+ // if it throws any errors.
+
+ $s = @curl_exec($ch);
+
+ $base = $s;
+ $curl_info = curl_getinfo($ch);
+ $http_code = $curl_info['http_code'];
+
+ $header = '';
+
+ // Pull out multiple headers, e.g. proxy and continuation headers
+ // allow for HTTP/2.x without fixing code
+
+ while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
+ $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
+ $header .= $chunk;
+ $base = substr($base,strlen($chunk));
+ }
+
+ if($http_code == 301 || $http_code == 302 || $http_code == 303) {
+ $matches = array();
+ preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
+ $url = trim(array_pop($matches));
+ $url_parsed = @parse_url($url);
+ if (isset($url_parsed)) {
+ $redirects++;
+ return delete_url($url,$headers,$redirects,$timeout);
+ }
+ }
+ $a->set_curl_code($http_code);
+ $body = substr($s,strlen($header));
+
+ $a->set_curl_headers($header);
+
+ curl_close($ch);
+ return($body);
+}}