aboutsummaryrefslogtreecommitdiffstats
path: root/include/follow.php
blob: a094a979f69f6e5f796e935878c705454765b52e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php


//
// Takes a $uid and the channel associated with the uid, and a url/handle and adds a new channel

// Returns an array
//  $return['success'] boolean true if successful
//  $return['abook'] Address book entry joined with xchan if successful
//  $return['message'] error text if success is false.

require_once('include/zot.php');

function new_contact($uid,$url,$channel,$interactive = false) {

	$result = array('success' => false,'message' => '');

	$a = get_app();

	if(! allowed_url($url)) {
		$result['message'] = t('Channel is blocked on this site.');
		return $result;
	}

	if(! $url) {
		$result['message'] = t('Channel location missing.');
		return $result;
	}

	$arr = array('url' => $url, 'channel' => array());

	call_hooks('follow', $arr);

	if($arr['channel']['success']) 
		$ret = $arr['channel'];
	else
		$ret = zot_finger($url,$channel);

	if($ret['success']) {
		$j = json_decode($ret['body'],true);
	}
	else {
		$result['message'] = t('Channel discovery failed. Website may be down or misconfigured.');
		logger('mod_follow: ' . $result['message']);
		return $result;
	}

	logger('follow: ' . $url . ' ' . print_r($j,true));

	if(! $j) {
		$result['message'] = t('Response from remote channel was not understood.');
		logger('mod_follow: ' . $result['message']);
		return $result;
	}


	if(! ($j['success'] && $j['guid'])) {
		$result['message'] = t('Response from remote channel was incomplete.');
		logger('mod_follow: ' . $result['message']);
		return $result;
	}


	// check service class limits

	$r = q("select count(*) as total from abook where abook_channel = %d and not (abook_flags & %d) ",
		intval($uid),
		intval(ABOOK_FLAG_SELF)
	);
	if($r)
		$total_channels = $r[0]['total'];

	if(! service_class_allows($uid,'total_channels',$total_channels)) {
		$result['message'] = upgrade_message();
		return $result;
	}

	// do we have an xchan and hubloc?
	// If not, create them.	

	$x = import_xchan($j);

	if(! $x['success']) 
		return $x;

	$xchan_hash = $x['hash'];


	$their_perms = 0;

	$global_perms = get_perms();

	if( array_key_exists('permissions',$j) && array_key_exists('data',$j['permissions'])) {
		$permissions = aes_unencapsulate(array(
			'data' => $j['permissions']['data'],
			'key'  => $j['permissions']['key'],
			'iv'   => $j['permissions']['iv']),
			$channel['channel_prvkey']);
		if($permissions)
			$permissions = json_decode($permissions,true);
		logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA);
	}
	else
		$permissions = $j['permissions'];

	foreach($permissions as $k => $v) {
		if($v) {
			$their_perms = $their_perms | intval($global_perms[$k][1]);
		}
	}

	if((local_user()) && $uid == local_user()) {
		$aid = get_account_id();
		$hash = $a->observer['xchan_hash'];
	}
	else {
		$r = q("select * from channel where channel_id = %d limit 1",
			intval($uid)
		);
		if(! $r) {
			$result['message'] = t('local account not found.');
			return $result;
		}
		$aid = $r[0]['channel_account_id'];
		$hash = $r[0]['channel_hash'];			
	}
	
	if($hash == $xchan_hash) {
		$result['message'] = t('Cannot connect to yourself.');
		return $result;
	}

	$r = q("select abook_xchan from abook where abook_xchan = '%s' and abook_channel = %d limit 1",
		dbesc($xchan_hash),
		intval($uid)
	);
	if($r) {
		$x = q("update abook set abook_their_perms = %d where abook_id = %d limit 1",
			intval($their_perms),
			intval($r[0]['abook_id'])
		);		
	}
	else {
		$r = q("insert into abook ( abook_account, abook_channel, abook_xchan, abook_their_perms, abook_created, abook_updated )
			values( %d, %d, '%s', %d, '%s', '%s' ) ",
			intval($aid),
			intval($uid),
			dbesc($xchan_hash),
			intval($their_perms),
			dbesc(datetime_convert()),
			dbesc(datetime_convert())
		);
	}

	if(! $r)
		logger('mod_follow: abook creation failed');

	$r = q("select abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash 
		where abook_xchan = '%s' and abook_channel = %d limit 1",
		dbesc($xchan_hash),
		intval($uid)
	);
	if($r)
		$result['abook'] = $r[0];

	// Then send a ping/message to the other side


/*

	$r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
			`writable`, `hidden`, `blocked`, `readonly`, `pending` )
			VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0) ",
			intval($uid),
			dbesc(datetime_convert()),
			dbesc($ret['url']),
			dbesc(normalise_link($ret['url'])),
			dbesc($ret['addr']),
			dbesc($ret['alias']),
			dbesc($ret['batch']),
			dbesc($ret['notify']),
			dbesc($ret['poll']),
			dbesc($ret['poco']),
			dbesc($ret['name']),
			dbesc($ret['nick']),
			dbesc($ret['photo']),
			dbesc($ret['network']),
			dbesc($ret['pubkey']),
			intval($new_relation),
			intval($ret['priority']),
			intval($writeable),
			intval($hidden)
		);
	}

	$r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
		dbesc($ret['url']),
		intval($uid)
	);

	if(! count($r)) {
		$result['message'] .=  t('Unable to retrieve contact information.') . EOL;
		return $result;
	}

	$contact = $r[0];
	$contact_id  = $r[0]['id'];


	$g = q("select def_gid from user where uid = %d limit 1",
		intval($uid)
	);
	if($g && intval($g[0]['def_gid'])) {
		require_once('include/group.php');
		group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
	}

*/


	$result['success'] = true;
	return $result;



}