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