X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fupdate_facebook.php;h=d2c1c3ffb92f8390dbc6141aeae08194cb9ce47e;hb=8e4f985e4502a14d66f84f9e5c8d4a54bb8e5594;hp=15c0e49f84fdc2f12e3e94eff63e08c6c48994c9;hpb=02877224b20f87af304553f739b69544d7ac4cfa;p=quix0rs-gnu-social.git diff --git a/scripts/update_facebook.php b/scripts/update_facebook.php index 15c0e49f84..d2c1c3ffb9 100755 --- a/scripts/update_facebook.php +++ b/scripts/update_facebook.php @@ -27,14 +27,14 @@ if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('LACONICA', true); -require_once(INSTALLDIR . '/lib/common.php'); -require_once(INSTALLDIR . '/lib/facebookutil.php'); +require_once INSTALLDIR . '/lib/common.php'; +require_once INSTALLDIR . '/lib/facebookutil.php'; // For storing the last run date-time -$last_updated_file = "/home/zach/laconica/scripts/facebook_last_updated"; +$last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated'; // Lock file name -$tmp_file = "/tmp/update_facebook.lock"; +$tmp_file = INSTALLDIR . '/scripts/update_facebook.lock'; // Make sure only one copy of the script is running at a time if (!($tmp_file = @fopen($tmp_file, "w"))) @@ -42,11 +42,11 @@ if (!($tmp_file = @fopen($tmp_file, "w"))) die("Can't open lock file. Script already running?"); } -$facebook = get_facebook(); +$facebook = getFacebook(); $current_time = time(); -$notice = get_facebook_notices(get_last_updated()); +$notice = getFacebookNotices(getLastUpdated()); print date('r', $current_time) . " Looking for notices to send to Facebook...\n"; @@ -54,30 +54,57 @@ $cnt = 0; while($notice->fetch()) { - $flink = Foreign_link::getByUserID($notice->profile_id, 2); + $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE); + $user = $flink->getUser(); $fbuid = $flink->foreign_id; - $content = $notice->content; + + if (!userCanUpdate($fbuid)) { + continue; + } + $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid); + $content = "$prefix $notice->content"; + if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) { // If it's not a reply, or if the user WANTS to send replies... if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $content) || (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) { - update_status($fbuid, $content); - $cnt++; + + // Avoid a Loop + if ($notice->source != 'Facebook') { + updateStatus($fbuid, $content); + updateProfileBox($facebook, $flink, $notice); + $cnt++; + } } } } -update_last_updated($current_time); +updateLastUpdated($current_time); print "Sent $cnt notices to Facebook.\n"; exit(0); +function userCanUpdate($fbuid) { + + global $facebook; + + $result = false; + + try { + $result = $facebook->api_client->users_hasAppPermission('status_update', $fbuid); + } catch(FacebookRestClientException $e){ + print_r($e); + } + + return $result; +} + -function update_status($fbuid, $content) { +function updateStatus($fbuid, $content) { global $facebook; try { @@ -87,7 +114,7 @@ function update_status($fbuid, $content) { } } -function get_last_updated(){ +function getLastUpdated(){ global $last_updated_file, $current_time; $file = fopen($last_updated_file, 'r'); @@ -104,7 +131,7 @@ function get_last_updated(){ return $last; } -function update_last_updated($time){ +function updateLastUpdated($time){ global $last_updated_file; $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); fwrite($file, $time);