]> git.mxchange.org Git - friendica.git/blob - include/poller.php
d1fc898d58e1fbe3e7655e9f36f2c3701cd8410c
[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
21         $a->set_baseurl($argv[1]);
22
23         $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 ORDER BY RAND()");
24
25         if(! count($contacts))
26                 killme();
27
28         foreach($contacts as $contact) {
29
30                 $importer_uid = $contact['uid'];
31
32                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
33                         intval($importer_uid)
34                 );
35                 if(! count($r))
36                         continue;
37
38                 $importer = $r[0];
39
40                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
41                         ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
42                         : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
43
44                 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
45
46                 $xml = fetch_url($url);
47
48                 if(! $xml)
49                         continue;
50
51                 $res = simplexml_load_string($xml);
52
53                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $contact['dfrn-id']))
54                         continue;
55
56                 $postvars = array();
57
58                 $postvars['dfrn_id'] = $contact['dfrn-id'];
59                 $challenge = hex2bin($res->challenge);
60
61                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
62
63                 $xml = post_url($contact['poll'],$postvars);
64                 if(! strlen($xml))
65                         continue;
66
67 echo "XML response:" . $xml . "\r\n";
68 echo "Length:" . strlen($xml) . "\r\n";
69
70                 $feed = new SimplePie();
71                 $feed->set_raw_data($xml);
72                 $feed->enable_order_by_date(false);
73                 $feed->init();
74
75                 foreach($feed->get_items() as $item) {
76
77                         $deleted = false;
78
79                         $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
80                         if(isset($rawdelete[0]['attribs']['']['ref'])) {
81                                 $uri = $rawthread[0]['attribs']['']['ref'];
82                                 $deleted = true;
83                                 if(isset($rawdelete[0]['attribs']['']['when'])) {
84                                         $when = $rawthread[0]['attribs']['']['when'];
85                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
86                                 }
87                                 else
88                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
89                         }
90                         if($deleted) {
91                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
92                                         dbesc($uri),
93                                         intval($importer['uid'])
94                                 );
95                                 if(count($r)) {
96                                         if($r[0]['uri'] == $r[0]['parent-uri']) {
97                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
98                                                         WHERE `parent-uri` = '%s'",
99                                                         dbesc($when),
100                                                         dbesc($r[0]['uri'])
101                                                 );
102                                         }
103                                         else {
104                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
105                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
106                                                         dbesc($when),
107                                                         dbesc($uri),
108                                                         intval($importer['uid'])
109                                                 );
110                                         }
111                                 }       
112                                 continue;
113                         }
114
115
116                         $is_reply = false;              
117                         $item_id = $item->get_id();
118                         $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
119                         if(isset($rawthread[0]['attribs']['']['ref'])) {
120                                 $is_reply = true;
121                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
122                         }
123
124
125                         if($is_reply) {
126
127                                 // Have we seen it? If not, import it.
128
129                                 $item_id = $item->get_id();
130
131                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
132                                         dbesc($item_id),
133                                         intval($importer['uid'])
134                                 );
135                                 // FIXME update content if 'updated' changes
136                                 if(count($r)) {
137                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
138                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
139                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
140                                                         intval($allow[0]['data']),
141                                                         dbesc($item_id),
142                                                         intval($importer['uid'])
143                                                 );
144                                         }
145                                         continue;
146                                 }
147                                 $datarray = get_atom_elements($item);
148                                 $datarray['parent-uri'] = $parent_uri;
149                                 $datarray['uid'] = $importer['uid'];
150                                 $datarray['contact-id'] = $contact['id'];
151                                 $r = post_remote($a,$datarray);
152                                 continue;
153                         }
154
155                         else {
156                                 // Head post of a conversation. Have we seen it? If not, import it.
157
158                                 $item_id = $item->get_id();
159                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
160                                         dbesc($item_id),
161                                         intval($importer['uid'])
162                                 );
163                                 if(count($r)) {
164                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
165                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
166                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
167                                                         intval($allow[0]['data']),
168                                                         dbesc($item_id),
169                                                         intval($importer['uid'])
170                                                 );
171                                         }
172                                         continue;
173                                 }
174
175                                 $datarray = get_atom_elements($item);
176                                 $datarray['parent-uri'] = $item_id;
177                                 $datarray['uid'] = $importer['uid'];
178                                 $datarray['contact-id'] = $contact['id'];
179                                 $r = post_remote($a,$datarray);
180                                 continue;
181
182                         }
183
184                 }
185
186                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
187                         dbesc(datetime_convert()),
188                         intval($contact['id'])
189                 );
190
191         }
192                 
193         killme();
194
195
196