]> git.mxchange.org Git - friendica.git/blob - include/poller.php
e946dabf6096f4c1bae325ba68db9e44ba3a54d0
[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
19
20         $a->set_baseurl(get_config('system','url'));
21
22         $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' 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                 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
77
78                 $xml = fetch_url($url);
79 echo "URL: " . $url;
80 echo "XML: " . $xml;
81                 if(! $xml)
82                         continue;
83
84                 $res = simplexml_load_string($xml);
85
86                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
87                         continue;
88
89                 $postvars = array();
90
91                 $sent_dfrn_id = hex2bin($res->dfrn_id);
92
93                 $final_dfrn_id = '';
94                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
95                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
96                 if($final_dfrn_id != $contact['dfrn-id']) {
97                         // did not decode properly - cannot trust this site 
98                         continue;
99                 }
100
101                 $postvars['dfrn_id'] = $contact['dfrn-id'];
102                 $challenge = hex2bin($res->challenge);
103
104                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
105
106                 $xml = post_url($contact['poll'],$postvars);
107
108 echo "XML response:" . $xml . "\r\n";
109 echo "Length:" . strlen($xml) . "\r\n";
110
111                 if(! strlen($xml)) {
112                         // an empty response may mean there's nothing new - record the fact that we checked
113                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
114                                 dbesc(datetime_convert()),
115                                 intval($contact['id'])
116                         );
117                         continue;
118                 }
119
120                 $feed = new SimplePie();
121                 $feed->set_raw_data($xml);
122                 $feed->enable_order_by_date(false);
123                 $feed->init();
124
125                 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
126                 if($photo_rawupdate) {
127                         $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
128                         $photo_url = $feed->get_image_url();
129                         if(strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) {
130
131                                 require_once("Photo.php");
132
133                                 $photo_failure = false;
134
135                                 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
136                                         intval($contact['id']),
137                                         intval($contact['uid'])
138                                 );
139                                 if(count($r)) {
140                                         $resource_id = $r[0]['resource-id'];
141                                         $img_str = fetch_url($photo_url,true);
142                                         $img = new Photo($img_str);
143                                         if($img) {
144                                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
145                                                         dbesc($resource_id),
146                                                         intval($contact['id']),
147                                                         intval($contact['uid'])
148                                                 );
149
150                                                 $img->scaleImageSquare(175);
151                                         
152                                                 $hash = $resource_id;
153
154                                                 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
155                                         
156                                                 $img->scaleImage(80);
157                                                 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
158                                                 if($r)
159                                                         q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
160                                                                 dbesc(datetime_convert()),
161                                                                 intval($contact['uid']),
162                                                                 intval($contact['id'])
163                                                         );
164                                         }
165                                 }
166                         }
167                 }
168
169                 
170                 foreach($feed->get_items() as $item) {
171
172                         $deleted = false;
173
174                         $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
175                         if(isset($rawdelete[0]['attribs']['']['ref'])) {
176                                 $uri = $rawthread[0]['attribs']['']['ref'];
177                                 $deleted = true;
178                                 if(isset($rawdelete[0]['attribs']['']['when'])) {
179                                         $when = $rawthread[0]['attribs']['']['when'];
180                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
181                                 }
182                                 else
183                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
184                         }
185                         if($deleted) {
186                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
187                                         dbesc($uri),
188                                         intval($importer['uid'])
189                                 );
190                                 if(count($r)) {
191                                         if($r[0]['uri'] == $r[0]['parent-uri']) {
192                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
193                                                         `body` = '', `title` = ''
194                                                         WHERE `parent-uri` = '%s'",
195                                                         dbesc($when),
196                                                         dbesc($r[0]['uri'])
197                                                 );
198                                         }
199                                         else {
200                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
201                                                         `body` = '', `title` = '' 
202                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
203                                                         dbesc($when),
204                                                         dbesc($uri),
205                                                         intval($importer['uid'])
206                                                 );
207                                         }
208                                 }       
209                                 continue;
210                         }
211
212
213                         $is_reply = false;              
214                         $item_id = $item->get_id();
215                         $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
216                         if(isset($rawthread[0]['attribs']['']['ref'])) {
217                                 $is_reply = true;
218                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
219                         }
220
221
222                         if($is_reply) {
223
224                                 // Have we seen it? If not, import it.
225
226                                 $item_id = $item->get_id();
227
228                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
229                                         dbesc($item_id),
230                                         intval($importer['uid'])
231                                 );
232                                 // FIXME update content if 'updated' changes
233                                 if(count($r)) {
234                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
235                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
236                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
237                                                         intval($allow[0]['data']),
238                                                         dbesc($item_id),
239                                                         intval($importer['uid'])
240                                                 );
241                                         }
242                                         continue;
243                                 }
244                                 $datarray = get_atom_elements($item);
245                                 $datarray['parent-uri'] = $parent_uri;
246                                 $datarray['uid'] = $importer['uid'];
247                                 $datarray['contact-id'] = $contact['id'];
248                                 $r = post_remote($a,$datarray);
249                                 continue;
250                         }
251
252                         else {
253                                 // Head post of a conversation. Have we seen it? If not, import it.
254
255                                 $item_id = $item->get_id();
256                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
257                                         dbesc($item_id),
258                                         intval($importer['uid'])
259                                 );
260                                 if(count($r)) {
261                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
262                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
263                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
264                                                         intval($allow[0]['data']),
265                                                         dbesc($item_id),
266                                                         intval($importer['uid'])
267                                                 );
268                                         }
269                                         continue;
270                                 }
271
272                                 $datarray = get_atom_elements($item);
273                                 $datarray['parent-uri'] = $item_id;
274                                 $datarray['uid'] = $importer['uid'];
275                                 $datarray['contact-id'] = $contact['id'];
276                                 $r = post_remote($a,$datarray);
277                                 continue;
278
279                         }
280
281                 }
282
283                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
284                         dbesc(datetime_convert()),
285                         intval($contact['id'])
286                 );
287
288         }
289                 
290         killme();
291
292
293