]> git.mxchange.org Git - friendica.git/blob - include/poller.php
show members of locked conversations
[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);
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         require_once('include/Contact.php');
19
20         $debugging = get_config('system','debugging');
21
22         $a->set_baseurl(get_config('system','url'));
23
24         $contacts = q("SELECT * FROM `contact` 
25                 WHERE ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)) 
26                 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
27
28         if(! count($contacts))
29                 killme();
30
31         foreach($contacts as $contact) {
32
33                 if($contact['priority']) {
34
35                         $update = false;
36                         $t = $contact['last-update'];
37
38                         switch ($contact['priority']) {
39                                 case 5:
40                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
41                                                 $update = true;
42                                         break;                                  
43                                 case 4:
44                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
45                                                 $update = true;
46                                         break;
47                                 case 3:
48                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
49                                                 $update = true;
50                                         break;
51                                 case 2:
52                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
53                                                 $update = true;
54                                         break;
55                                 case 1:
56                                 default:
57                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
58                                                 $update = true;
59                                         break;
60                         }
61                         if(! $update)
62                                 continue;
63                 }
64
65                 $importer_uid = $contact['uid'];
66
67                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
68                         intval($importer_uid)
69                 );
70                 if(! count($r))
71                         continue;
72
73                 $importer = $r[0];
74
75                 if($debugging)
76                         echo "IMPORTER: {$importer['name']}";
77
78                 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
79                         ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
80                         : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
81
82
83
84                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
85
86                 if(intval($contact['duplex']) && $contact['dfrn-id'])
87                         $idtosend = '0:' . $orig_id;
88                 if(intval($contact['duplex']) && $contact['issued-id'])
89                         $idtosend = '1:' . $orig_id;            
90
91                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
92                 $xml = fetch_url($url);
93
94                 if($debugging) {
95                         echo "URL: " . $url . "\r\n";
96                         echo "XML: " . $xml . "\r\n";
97                 }
98
99                 if(! $xml) {
100                         // dead connection - might be a transient event, or this might
101                         // mean the software was uninstalled or the domain expired. 
102                         // Will keep trying for one month.
103                         mark_for_death($contact);
104                         continue;
105                 }
106
107
108                 $res = simplexml_load_string($xml);
109
110                 if(intval($res->status) == 1) {
111                         // we may not be friends anymore. Will keep trying for one month.
112                         mark_for_death($contact);
113                 }
114                 else {
115                         if($contact['term-date'] != '0000-00-00 00:00:00')
116                                 unmark_for_death($contact);
117                 }
118
119                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
120                         continue;
121
122                 $postvars = array();
123
124                 $sent_dfrn_id = hex2bin($res->dfrn_id);
125                 $challenge    = hex2bin($res->challenge);
126
127                 $final_dfrn_id = '';
128
129                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
130                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
131                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
132
133                 }
134                 else {
135                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
136                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
137                 }
138
139                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
140
141                 if(strpos($final_dfrn_id,':') == 1)
142                         $final_dfrn_id = substr($final_dfrn_id,2);
143
144                 if($final_dfrn_id != $orig_id) {
145
146                         // did not decode properly - cannot trust this site 
147                         continue;
148                 }
149
150                 $postvars['dfrn_id'] = $idtosend;
151
152
153                 $xml = post_url($contact['poll'],$postvars);
154
155                 if($debugging) {
156                         echo "XML response:" . $xml . "\r\n";
157                         echo "Length:" . strlen($xml) . "\r\n";
158                 }
159
160                 if(! strlen($xml))
161                         continue;
162
163                 $feed = new SimplePie();
164                 $feed->set_raw_data($xml);
165                 $feed->enable_order_by_date(false);
166                 $feed->init();
167
168                 // Check at the feed level for updated contact name and/or photo
169
170                 $name_updated  = '';
171                 $new_name = '';
172                 $photo_timestamp = '';
173                 $photo_url = '';
174
175                 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, author);
176                 if($rawtags) {
177                         $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
178                         if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
179                                 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
180                                 $new_name = $elems['name'][0]['data'];
181                         } 
182                         if(($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
183                                 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
184                                 $photo_url = $elems['link'][0]['attribs']['']['href'];
185                         }
186                 }
187                 if(! $photo_timestamp) {
188                         $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
189                         if($photo_rawupdate) {
190                                 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
191                                 $photo_url = $feed->get_image_url();
192                         }
193                 }
194                 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
195
196                         require_once("Photo.php");
197                         $photo_failure = false;
198
199                         $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
200                                 intval($contact['id']),
201                                 intval($contact['uid'])
202                         );
203                         if(count($r)) {
204                                 $resource_id = $r[0]['resource-id'];
205                                 $img_str = fetch_url($photo_url,true);
206                                 $img = new Photo($img_str);
207                                 if($img) {
208                                         q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
209                                                 dbesc($resource_id),
210                                                 intval($contact['id']),
211                                                 intval($contact['uid'])
212                                         );
213
214                                         $img->scaleImageSquare(175);
215                                 
216                                         $hash = $resource_id;
217                                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
218                                         
219                                         $img->scaleImage(80);
220                                         $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
221                                         if($r)
222                                                 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
223                                                         dbesc(datetime_convert()),
224                                                         intval($contact['uid']),
225                                                         intval($contact['id'])
226                                                 );
227                                 }
228                         }
229                 }
230
231                 if(($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
232                         q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
233                                 dbesc(notags(trim($new_name))),
234                                 dbesc(datetime_convert()),
235                                 intval($contact['uid']),
236                                 intval($contact['id'])
237                         );
238                 }
239
240                 // Now process the feed
241                 if($feed->get_item_quantity()) {                
242                         foreach($feed->get_items() as $item) {
243
244                                 $deleted = false;
245
246                                 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
247                                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
248                                         $uri = $rawthread[0]['attribs']['']['ref'];
249                                         $deleted = true;
250                                         if(isset($rawdelete[0]['attribs']['']['when'])) {
251                                                 $when = $rawthread[0]['attribs']['']['when'];
252                                                 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
253                                         }
254                                         else
255                                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
256                                 }
257                                 if($deleted) {
258                                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
259                                                 dbesc($uri),
260                                                 intval($importer['uid'])
261                                         );
262                                         if(count($r)) {
263                                                 $item = $r[0];
264                                                 if($item['uri'] == $item['parent-uri']) {
265                                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
266                                                                 `body` = '', `title` = ''
267                                                                 WHERE `parent-uri` = '%s' AND `uid` = %d",
268                                                                 dbesc($when),
269                                                                 dbesc(datetime_convert()),
270                                                                 dbesc($item['uri']),
271                                                                 intval($importer['uid'])
272                                                         );
273                                                 }
274                                                 else {
275                                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
276                                                                 `body` = '', `title` = '' 
277                                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
278                                                                 dbesc($when),
279                                                                 dbesc(datetime_convert()),
280                                                                 dbesc($uri),
281                                                                 intval($importer['uid'])
282                                                         );
283                                                         if($item['last-child']) {
284                                                                 // ensure that last-child is set in case the comment that had it just got wiped.
285                                                                 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
286                                                                         dbesc(datetime_convert()),
287                                                                         dbesc($item['parent-uri']),
288                                                                         intval($item['uid'])
289                                                                 );
290                                                                 // who is the last child now? 
291                                                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d 
292                                                                         ORDER BY `edited` DESC LIMIT 1",
293                                                                                 dbesc($item['parent-uri']),
294                                                                                 intval($importer['uid'])
295                                                                 );
296                                                                 if(count($r)) {
297                                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
298                                                                                 intval($r[0]['id'])
299                                                                         );
300                                                                 }
301                                                         }       
302                                                 }
303                                         }       
304                                         continue;
305                                 }
306
307
308                                 $is_reply = false;              
309                                 $item_id = $item->get_id();
310                                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
311                                 if(isset($rawthread[0]['attribs']['']['ref'])) {
312                                         $is_reply = true;
313                                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
314                                 }
315
316
317                                 if($is_reply) {
318         
319                                         // Have we seen it? If not, import it.
320         
321                                         $item_id = $item->get_id();
322         
323                                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
324                                                 dbesc($item_id),
325                                                 intval($importer['uid'])
326                                         );
327                                         // FIXME update content if 'updated' changes
328                                         if(count($r)) {
329                                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
330                                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
331                                                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
332                                                                 dbesc(datetime_convert()),
333                                                                 dbesc($parent_uri),
334                                                                 intval($importer['uid'])
335                                                         );
336                                                         $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
337                                                                 intval($allow[0]['data']),
338                                                                 dbesc(datetime_convert()),
339                                                                 dbesc($item_id),
340                                                                 intval($importer['uid'])
341                                                         );
342
343
344                                                 }
345                                                 continue;
346                                         }
347                                         $datarray = get_atom_elements($item);
348                                         $datarray['parent-uri'] = $parent_uri;
349                                         $datarray['uid'] = $importer['uid'];
350                                         $datarray['contact-id'] = $contact['id'];
351                                         if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
352                                                 $datarray['type'] = 'activity';
353                                                 $datarray['gravity'] = GRAVITY_LIKE;
354                                         }
355         
356                                         $r = item_store($datarray);
357                                         continue;
358                                 }
359
360                                 else {
361                                         // Head post of a conversation. Have we seen it? If not, import it.
362         
363                                         $item_id = $item->get_id();
364                                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
365                                                 dbesc($item_id),
366                                                 intval($importer['uid'])
367                                         );
368                                         if(count($r)) {
369                                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
370                                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
371                                                         $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
372                                                                 intval($allow[0]['data']),
373                                                                 dbesc(datetime_convert()),
374                                                                 dbesc($item_id),
375                                                                 intval($importer['uid'])
376                                                         );
377                                                 }
378                                                 continue;
379                                         }
380
381                                         $datarray = get_atom_elements($item);
382                                         $datarray['parent-uri'] = $item_id;
383                                         $datarray['uid'] = $importer['uid'];
384                                         $datarray['contact-id'] = $contact['id'];
385                                         $r = item_store($datarray);
386                                         continue;
387         
388                                 }
389                         }
390                 }
391                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
392                         dbesc(datetime_convert()),
393                         intval($contact['id'])
394                 );
395
396         }
397                 
398         killme();
399
400
401