]> git.mxchange.org Git - friendica.git/blob - include/poller.php
e0b4d79b21aac18e4f970c7f75d8cd9190da6d33
[friendica.git] / include / poller.php
1 <?php
2
3
4 require_once('boot.php');
5
6 $a = new App;
7
8 @include('.htconfig.php');
9 require_once('dba.php');
10 $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
11         unset($db_host, $db_user, $db_pass, $db_data);
12
13 require_once('session.php');
14 require_once('datetime.php');
15 require_once('simplepie/simplepie.inc');
16 require_once('include/items.php');
17
18 if($argc < 2)
19         exit;
20 dbg(3);
21         $a->set_baseurl($argv[1]);
22
23         $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
24
25         if(! count($contacts))
26                 killme();
27
28         foreach($contacts as $contact) {
29
30                 if($contact['priority']) {
31
32                         $update = false;
33                         $t = $contact['last-update'];
34
35                         switch ($contact['priority']) {
36                                 case 5:
37                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
38                                                 $update = true;
39                                         break;                                  
40                                 case 4:
41                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
42                                                 $update = true;
43                                         break;
44                                 case 3:
45                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
46                                                 $update = true;
47                                         break;
48                                 case 2:
49                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
50                                                 $update = true;
51                                         break;
52                                 case 1:
53                                 default:
54                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
55                                                 $update = true;
56                                         break;
57                         }
58                         if(! $update)
59                                 continue;
60                 }
61
62
63                 $importer_uid = $contact['uid'];
64
65                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
66                         intval($importer_uid)
67                 );
68                 if(! count($r))
69                         continue;
70
71                 $importer = $r[0];
72
73                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
74                         ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
75                         : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
76
77                 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
78
79                 $xml = fetch_url($url);
80 echo "URL: " . $url;
81 echo "XML: " . $xml;
82                 if(! $xml)
83                         continue;
84
85                 $res = simplexml_load_string($xml);
86
87                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $contact['dfrn-id']))
88                         continue;
89
90                 $postvars = array();
91
92                 $postvars['dfrn_id'] = $contact['dfrn-id'];
93                 $challenge = hex2bin($res->challenge);
94
95                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
96
97                 $xml = post_url($contact['poll'],$postvars);
98
99 echo "XML response:" . $xml . "\r\n";
100 echo "Length:" . strlen($xml) . "\r\n";
101
102                 if(! strlen($xml)) {
103                         // an empty response may mean there's nothing new - record the fact that we checked
104                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
105                                 dbesc(datetime_convert()),
106                                 intval($contact['id'])
107                         );
108                         continue;
109                 }
110
111                 $feed = new SimplePie();
112                 $feed->set_raw_data($xml);
113                 $feed->enable_order_by_date(false);
114                 $feed->init();
115
116                 foreach($feed->get_items() as $item) {
117
118                         $deleted = false;
119
120                         $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
121                         if(isset($rawdelete[0]['attribs']['']['ref'])) {
122                                 $uri = $rawthread[0]['attribs']['']['ref'];
123                                 $deleted = true;
124                                 if(isset($rawdelete[0]['attribs']['']['when'])) {
125                                         $when = $rawthread[0]['attribs']['']['when'];
126                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
127                                 }
128                                 else
129                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
130                         }
131                         if($deleted) {
132                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
133                                         dbesc($uri),
134                                         intval($importer['uid'])
135                                 );
136                                 if(count($r)) {
137                                         if($r[0]['uri'] == $r[0]['parent-uri']) {
138                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
139                                                         WHERE `parent-uri` = '%s'",
140                                                         dbesc($when),
141                                                         dbesc($r[0]['uri'])
142                                                 );
143                                         }
144                                         else {
145                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
146                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
147                                                         dbesc($when),
148                                                         dbesc($uri),
149                                                         intval($importer['uid'])
150                                                 );
151                                         }
152                                 }       
153                                 continue;
154                         }
155
156
157                         $is_reply = false;              
158                         $item_id = $item->get_id();
159                         $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
160                         if(isset($rawthread[0]['attribs']['']['ref'])) {
161                                 $is_reply = true;
162                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
163                         }
164
165
166                         if($is_reply) {
167
168                                 // Have we seen it? If not, import it.
169
170                                 $item_id = $item->get_id();
171
172                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
173                                         dbesc($item_id),
174                                         intval($importer['uid'])
175                                 );
176                                 // FIXME update content if 'updated' changes
177                                 if(count($r)) {
178                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
179                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
180                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
181                                                         intval($allow[0]['data']),
182                                                         dbesc($item_id),
183                                                         intval($importer['uid'])
184                                                 );
185                                         }
186                                         continue;
187                                 }
188                                 $datarray = get_atom_elements($item);
189                                 $datarray['parent-uri'] = $parent_uri;
190                                 $datarray['uid'] = $importer['uid'];
191                                 $datarray['contact-id'] = $contact['id'];
192                                 $r = post_remote($a,$datarray);
193                                 continue;
194                         }
195
196                         else {
197                                 // Head post of a conversation. Have we seen it? If not, import it.
198
199                                 $item_id = $item->get_id();
200                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
201                                         dbesc($item_id),
202                                         intval($importer['uid'])
203                                 );
204                                 if(count($r)) {
205                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
206                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
207                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
208                                                         intval($allow[0]['data']),
209                                                         dbesc($item_id),
210                                                         intval($importer['uid'])
211                                                 );
212                                         }
213                                         continue;
214                                 }
215
216                                 $datarray = get_atom_elements($item);
217                                 $datarray['parent-uri'] = $item_id;
218                                 $datarray['uid'] = $importer['uid'];
219                                 $datarray['contact-id'] = $contact['id'];
220                                 $r = post_remote($a,$datarray);
221                                 continue;
222
223                         }
224
225                 }
226
227                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
228                         dbesc(datetime_convert()),
229                         intval($contact['id'])
230                 );
231
232         }
233                 
234         killme();
235
236
237