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