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