]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
more work
[friendica.git] / include / notifier.php
1 <?php
2
3 require_once("boot.php");
4
5 $a = new App;
6
7 @include(".htconfig.php");
8 require_once("dba.php");
9 $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
10         unset($db_host, $db_user, $db_pass, $db_data);
11
12 require_once("session.php");
13 require_once("datetime.php");
14
15 // FIXME - generalise for other content, probably create a notify queue in 
16 // the db with type and recipient list
17
18 if($argc < 3)
19         exit;
20
21         $baseurl = trim(hex2bin($argv[1]));
22
23         $cmd = $argv[2];
24
25         switch($cmd) {
26
27                 default:
28                         $item_id = intval($argv[3]);
29                         if(! $item_id)
30                                 killme();
31                         break;
32         }
33
34
35         $is_parent = false;
36
37         $recipients = array();
38
39         // fetch requested item
40
41         $r = q("SELECT `item`.*,  `contact`.*,`item`.`id` AS `item_id` FROM `item` LEFT JOIN `contact` ON `item`.`contact-id` = `contact`.`id` 
42                 WHERE `item`.`id` = %d LIMIT 1",
43                 intval($item_id)
44         );
45         if(! count($r))
46                 killme();
47
48         $item = $r[0];
49
50         $recipients[] = $item['contact-id'];
51
52         if($item['parent'] == $item['id']) {
53                 $is_parent = true;
54         }
55         else {
56                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
57                         intval($item['parent'])
58                 );
59                 if(count($r))
60                         $parent = $r[0];
61         }
62
63         if(is_array($parent))
64                 $recipients[] = $parent['contact-id'];
65
66         $r = q("SELECT `contact-id` FROM `item` WHERE `hash` = '%s' AND `id` != %d AND `id` != %d",
67                 dbesc($item['hash']),
68                 intval($item['id']),
69                 intval($item['parent'])
70         );
71         if(count($r)) {
72                 foreach($r as $rr) {
73                         if($rr['contact-id'] != $item['contact-id'])
74                                 $recipients[] = $rr['contact-id'];
75                 }
76         }
77
78         $tpl = file_get_contents('view/atomic.tpl');
79
80         // FIXME should dump the entire conversation
81
82         $atom = replace_macros($tpl, array(
83                 '$feed_id' => xmlify($baseurl),
84                 '$feed_title' => xmlify('Wall Item'),
85                 '$feed_updated' => xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00' ,'Y-m-d\Th:i:s\Z')) ,
86                 '$name' => xmlify($item['name']),
87                 '$profile_page' => xmlify($item['url']),
88                 '$thumb' => xmlify($item['thumb']),
89                 '$item_id' => xmlify($item['hash'] . '-' . $item['id']),
90                 '$title' => xmlify(''),
91                 '$link' => xmlify($baseurl . '/item/' . $item['id']),
92                 '$updated' => xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00' ,'Y-m-d\Th:i:s\Z')),
93                 '$summary' => xmlify(''),
94                 '$content' => xmlify($item['body'])
95         ));
96
97 print_r($atom);
98         // atomify
99
100         // expand list of recipients
101
102 dbg(3);
103
104
105         $recipients = array_unique($recipients);
106 print_r($recipients);
107         $recip_str = implode(', ', $recipients);
108
109         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
110                 dbesc($recip_str)
111         );
112         if(! count($r))
113                 killme();
114
115         // delivery loop
116
117         foreach($r as $rr) {
118                 if($rr['self'])
119                         continue;
120
121                 if(! strlen($rr['dfrn-id']))
122                         continue;
123                 $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id'];
124 print_r($url);
125                 $xml = fetch_url($url);
126 echo $xml;
127
128 print_r($xml);
129                 if(! $xml)
130                         continue;
131
132                 $res = simplexml_load_string($xml);
133 print_r($res);
134 var_dump($res);
135
136                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $rr['dfrn-id']))
137                         continue;
138
139                 $postvars = array();
140
141                 $postvars['dfrn_id'] = $rr['dfrn-id'];
142                 $challenge = hex2bin($res->challenge);
143 echo "dfrn-id:" . $res->dfrn_id . "\r\n";
144 echo "challenge:" . $res->challenge . "\r\n";
145 echo "pubkey:" . $rr['pubkey'] . "\r\n";
146
147                 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
148
149                 $postvars['data'] = $atom;
150
151 print_r($postvars);
152                 $xml = fetch_url($url,$postvars);
153
154                                 
155         }
156
157         killme();
158