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