]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_facebook.php
Merge branch '0.7.x' into 0.8.x
[quix0rs-gnu-social.git] / scripts / update_facebook.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * Laconica - a distributed open-source microblogging tool
5  * Copyright (C) 2008, Controlez-Vous, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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";
24     exit();
25 }
26
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28 define('LACONICA', true);
29
30 require_once INSTALLDIR . '/lib/common.php';
31 require_once INSTALLDIR . '/lib/facebookutil.php';
32
33 // For storing the last run date-time
34 $last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated';
35
36 // Lock file name
37 $lock_file = INSTALLDIR . '/scripts/update_facebook.lock';
38
39 // Make sure only one copy of the script is running at a time
40 $lock_file = @fopen($lock_file, "w+");
41 if (!flock( $lock_file, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock) {
42     die("Can't open lock file. Script already running?\n");
43 }
44
45 $facebook = getFacebook();
46 $current_time = time();
47 $since = getLastUpdated();
48 updateLastUpdated($current_time);
49 $notice = getFacebookNotices($since);
50 $cnt = 0;
51
52 while($notice->fetch()) {
53
54     $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
55     $user = $flink->getUser();
56     $fbuid = $flink->foreign_id;
57     
58     if (!userCanUpdate($fbuid)) {
59         continue;
60     }
61
62     $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
63     $content = "$prefix $notice->content";
64     
65     if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
66
67         // If it's not a reply, or if the user WANTS to send replies...
68         if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $content) ||
69             (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) {
70              
71                 // Avoid a Loop
72                 if ($notice->source != 'Facebook') {
73                     
74                     try {
75                         $facebook->api_client->users_setStatus($content, 
76                             $fbuid, false, true);
77                         updateProfileBox($facebook, $flink, $notice);
78                         $cnt++;
79                     } catch(FacebookRestClientException $e) {
80                         print "Couldn't sent notice $notice->id!\n";
81                         print $e->getMessage();
82
83                         // Remove flink?
84                     }
85                 }
86         }
87     }
88 }
89
90 if ($cnt > 0) {
91     print date('r', $current_time) . 
92     ": Found $cnt new notices for Facebook since last run at " . 
93      date('r', $since) . "\n";
94 }
95
96 fclose($lock_file);
97 exit(0);
98
99
100 function userCanUpdate($fbuid) {
101     
102     global $facebook;
103
104     $result = false;
105     
106     try {
107         $result = $facebook->api_client->users_hasAppPermission('status_update', $fbuid);
108     } catch(FacebookRestClientException $e){
109         print_r($e);
110     }
111
112     return $result;
113 }
114
115 function getLastUpdated(){
116     global $last_updated_file, $current_time;
117     $last = $current_time;
118
119     if (file_exists($last_updated_file) && 
120         ($file = fopen($last_updated_file, 'r'))) {
121             $last = fgets($file);
122     } else {
123         print "$last_updated_file doesn't exit. Trying to create it...\n";
124         $file = fopen($last_updated_file, 'w+') or 
125             die("Can't open $last_updated_file for writing!\n");
126         print 'Success. Using current time (' . date('r', $last) . 
127             ") to look for new notices.\n";
128     }
129     
130     fclose($file);
131     return $last;
132 }
133
134 function updateLastUpdated($time){
135     global $last_updated_file;
136     $file = fopen($last_updated_file, 'w') or
137         die("Can't open $last_updated_file for writing!");
138     fwrite($file, $time);
139     fclose($file);
140 }
141