4 * Laconica - a distributed open-source microblogging tool
5 * Copyright (C) 2008, Controlez-Vous, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23 print "This script must be run from the command line\n";
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28 define('LACONICA', true);
30 require_once INSTALLDIR . '/lib/common.php';
31 require_once INSTALLDIR . '/lib/facebookutil.php';
33 // For storing the last run date-time
34 $last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated';
37 $tmp_file = INSTALLDIR . '/scripts/update_facebook.lock';
39 // Make sure only one copy of the script is running at a time
40 if (!($tmp_file = @fopen($tmp_file, "w")))
42 die("Can't open lock file. Script already running?");
45 $facebook = getFacebook();
47 $current_time = time();
49 $since = getLastUpdated();
51 $notice = getFacebookNotices($since);
55 while($notice->fetch()) {
57 $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
58 $user = $flink->getUser();
59 $fbuid = $flink->foreign_id;
61 if (!userCanUpdate($fbuid)) {
65 $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
66 $content = "$prefix $notice->content";
68 if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
70 // If it's not a reply, or if the user WANTS to send replies...
71 if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $content) ||
72 (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) {
75 if ($notice->source != 'Facebook') {
76 updateStatus($fbuid, $content);
77 updateProfileBox($facebook, $flink, $notice);
85 print date('r', $current_time) .
86 ": Found $cnt new notices to send to Facebook since last run at " .
87 date('Y-m-d H:i:s', $since) . "\n";
91 #Save the last updated time. It needs to do this even if there were no
92 #changes made, otherwise it will never create it and thus never send
94 updateLastUpdated($current_time);
99 function userCanUpdate($fbuid) {
106 $result = $facebook->api_client->users_hasAppPermission('status_update', $fbuid);
107 } catch(FacebookRestClientException $e){
115 function updateStatus($fbuid, $content) {
119 $result = $facebook->api_client->users_setStatus($content, $fbuid, false, true);
120 } catch(FacebookRestClientException $e){
125 function getLastUpdated(){
126 global $last_updated_file, $current_time;
128 $file = fopen($last_updated_file, 'r');
131 $last = fgets($file);
133 print "Unable to read $last_updated_file. Using current time.\n";
134 return $current_time;
142 function updateLastUpdated($time){
143 global $last_updated_file;
144 $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!");
145 fwrite($file, $time);