]> git.mxchange.org Git - friendica.git/blob - include/poller.php
08028cdeed56200f6b0e3f0e0ab7ea9fe53bbcd5
[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                         $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
78                         print_r($rawdelete);
79                         if($deleted) {
80                                 // pick out ref and when from attribs
81                                 // check hasn't happened already, verify ownership and then process it
82
83
84                                 continue;
85                         }
86
87                         $is_reply = false;              
88                         $item_id = $item->get_id();
89                         $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
90                         if(isset($rawthread[0]['attribs']['']['ref'])) {
91                                 $is_reply = true;
92                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
93                         }
94
95
96                         if($is_reply) {
97
98                                 // Have we seen it? If not, import it.
99
100                                 $item_id = $item->get_id();
101
102                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
103                                         dbesc($item_id),
104                                         intval($importer['uid'])
105                                 );
106                                 // FIXME update content if 'updated' changes
107                                 if(count($r)) {
108                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
109                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
110                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
111                                                         intval($allow[0]['data']),
112                                                         dbesc($item_id),
113                                                         intval($importer['uid'])
114                                                 );
115                                         }
116                                         continue;
117                                 }
118                                 $datarray = get_atom_elements($item);
119                                 $datarray['parent-uri'] = $parent_uri;
120                                 $datarray['uid'] = $importer['uid'];
121                                 $datarray['contact-id'] = $importer['id'];
122                                 $r = post_remote($a,$datarray);
123                                 continue;
124                         }
125
126                         else {
127                                 // Head post of a conversation. Have we seen it? If not, import it.
128
129                                 $item_id = $item->get_id();
130                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
131                                         dbesc($item_id),
132                                         intval($importer['uid'])
133                                 );
134                                 if(count($r)) {
135                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
136                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
137                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
138                                                         intval($allow[0]['data']),
139                                                         dbesc($item_id),
140                                                         intval($importer['uid'])
141                                                 );
142                                         }
143                                         continue;
144                                 }
145
146                                 $datarray = get_atom_elements($item);
147                                 $datarray['parent-uri'] = $item_id;
148                                 $datarray['uid'] = $importer['uid'];
149                                 $datarray['contact-id'] = $importer['id'];
150                                 $r = post_remote($a,$datarray);
151                                 continue;
152
153                         }
154
155                 }
156
157                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
158                         dbesc(datetime_convert()),
159                         intval($contact['id'])
160                 );
161
162         }
163                 
164         killme();
165
166
167