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