+
+ $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
+ dbesc($orig_post['uri']),
+ intval($uid)
+ );
+
+ if(count($myconv)) {
+ $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
+
+ foreach($myconv as $conv) {
+
+ // now if we find a match, it means we're in this conversation
+
+ if(! link_compare($conv['author-link'],$importer_url))
+ continue;
+
+ require_once('include/enotify.php');
+
+ $conv_parent = $conv['parent'];
+
+ notification(array(
+ 'type' => NOTIFY_COMMENT,
+ 'notify_flags' => $user[0]['notify-flags'],
+ 'language' => $user[0]['language'],
+ 'to_name' => $user[0]['username'],
+ 'to_email' => $user[0]['email'],
+ 'uid' => $user[0]['uid'],
+ 'item' => $cmntdata,
+ 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $item,
+ 'source_name' => $cmntdata['author-name'],
+ 'source_link' => $cmntdata['author-link'],
+ 'source_photo' => $cmntdata['author-avatar'],
+ 'verb' => ACTIVITY_POST,
+ 'otype' => 'item',
+ 'parent' => $conv_parent,
+ ));
+
+ // only send one notification
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+
+
+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);
+
+ 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);