]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_facebook.php
02e47cefea514a16369f0100944de4f2d879d7fb
[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 = "/home/zach/laconica/scripts/facebook_last_updated";
35
36 // Lock file name
37 $tmp_file = "/tmp/update_facebook.lock";
38
39 // Make sure only one copy of the script is running at a time
40 if (!($tmp_file = @fopen($tmp_file, "w")))
41 {
42         die("Can't open lock file. Script already running?");
43 }
44
45 $facebook = get_facebook();
46
47 $current_time = time();
48
49 $notice = get_facebook_notices(get_last_updated());
50
51 while($notice->fetch()) {
52
53     $flink = Foreign_link::getByUserID($notice->profile_id, 2);
54     $fbuid = $flink->foreign_id;
55     $content = $notice->content;
56
57     if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
58
59         // If it's not a reply, or if the user WANTS to send replies...
60         if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $content) ||
61             (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) {
62                 update_status($fbuid, $content);
63             }
64     }
65 }
66
67 update_last_updated($current_time);
68
69 exit(0);
70
71
72
73 function update_status($fbuid, $content) {
74     global $facebook;
75
76     try {
77         $result = $facebook->api_client->users_setStatus($content, $fbuid, false, true);
78     } catch(FacebookRestClientException $e){
79         print_r($e);
80     }
81 }
82
83 function get_last_updated(){
84         global $last_updated_file, $current_time;
85
86         $file = fopen($last_updated_file, 'r');
87
88         if ($file) {
89             $last = fgets($file);
90         } else {
91             print "Unable to read $last_updated_file. Using current time.\n";
92             return $current_time;
93         }
94
95         fclose($file);
96
97         return $last;
98 }
99
100 function update_last_updated($time){
101         global $last_updated_file;
102         $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!");
103         fwrite($file, $time);
104         fclose($file);
105 }