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