acl_selectors.php 8.14 KB
Newer Older
Zot's avatar
Zot committed
1
<?php
2 3 4
/**
 * 
 */
Zot's avatar
Zot committed
5

6 7 8
/**
 * @package acl_selectors 
 */
Zot's avatar
Zot committed
9
function group_select($selname,$selclass,$preselected = false,$size = 4) {
Zot's avatar
Zot committed
10

Friendika's avatar
Friendika committed
11 12
	$a = get_app();

Zot's avatar
Zot committed
13 14
	$o = '';

15
	$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
Zot's avatar
Zot committed
16

Zot's avatar
Zot committed
17
	$r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
18
		intval(local_user())
Zot's avatar
Zot committed
19 20
	);

Friendika's avatar
Friendika committed
21 22 23 24 25 26 27

	$arr = array('group' => $r, 'entry' => $o);

	// e.g. 'network_pre_group_deny', 'profile_pre_group_allow'

	call_hooks($a->module . '_pre_' . $selname, $arr);

Zot's avatar
Zot committed
28 29
	if(count($r)) {
		foreach($r as $rr) {
Zot's avatar
Zot committed
30
			if((is_array($preselected)) && in_array($rr['id'], $preselected))
Zot's avatar
Zot committed
31 32 33
				$selected = " selected=\"selected\" ";
			else
				$selected = '';
34
			$trimmed = mb_substr($rr['name'],0,12);
Zot's avatar
Zot committed
35

36
			$o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
Zot's avatar
Zot committed
37 38 39 40 41
		}
	
	}
	$o .= "</select>\r\n";

Friendika's avatar
Friendika committed
42 43
	call_hooks($a->module . '_post_' . $selname, $o);

Zot's avatar
Zot committed
44 45 46 47 48

	return $o;
}


49 50 51 52 53 54 55
function contact_selector($selname, $selclass, $preselected = false, $options) {

	$a = get_app();

	$mutual = false;
	$networks = null;
	$single = false;
56 57 58 59 60 61
	$exclude = false;
	$size = 4;

	if(is_array($options)) {
		if(x($options,'size'))
			$size = $options['size'];
62 63 64 65 66 67 68

		if(x($options,'mutual_friends'))
			$mutual = true;
		if(x($options,'single'))
			$single = true;
		if(x($options,'multiple'))
			$single = false;
69 70 71
		if(x($options,'exclude'))
			$exclude = $options['exclude'];

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		if(x($options,'networks')) {
			switch($options['networks']) {
				case 'DFRN_ONLY':
					$networks = array('dfrn');
					break;
				case 'PRIVATE':
					if(is_array($a->user) && $a->user['prvnets'])
						$networks = array('dfrn','mail','dspr');
					else
						$networks = array('dfrn','face','mail', 'dspr');
					break;
				case 'TWO_WAY':
					if(is_array($a->user) && $a->user['prvnets'])
						$networks = array('dfrn','mail','dspr');
					else
						$networks = array('dfrn','face','mail','dspr','stat');
					break;					
				default:
					break;
			}
		}
	}
		
95
	$x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
96 97 98 99 100 101 102 103

	call_hooks('contact_select_options', $x);

	$o = '';

	$sql_extra = '';

	if($x['mutual']) {
Friendika's avatar
Friendika committed
104
		$sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
105 106
	}

107 108 109
	if(intval($x['exclude']))
		$sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));

110 111 112 113 114 115 116 117
	if(is_array($x['networks']) && count($x['networks'])) {
		for($y = 0; $y < count($x['networks']) ; $y ++)
			$x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
		$str_nets = implode(',',$x['networks']);
		$sql_extra .= " AND `network` IN ( $str_nets ) ";
	}

	if($x['single'])
118
		$o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" >\r\n";
119
	else 
120
		$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" >\r\n";
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

	$r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
		WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `notify` != ''
		$sql_extra
		ORDER BY `name` ASC ",
		intval(local_user())
	);


	$arr = array('contact' => $r, 'entry' => $o);

	// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'

	call_hooks($a->module . '_pre_' . $selname, $arr);

	if(count($r)) {
		foreach($r as $rr) {
			if((is_array($preselected)) && in_array($rr['id'], $preselected))
				$selected = " selected=\"selected\" ";
			else
				$selected = '';

			$trimmed = mb_substr($rr['name'],0,20);

			$o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
		}
	
	}

	$o .= "</select>\r\n";

	call_hooks($a->module . '_post_' . $selname, $o);

	return $o;
}


Zot's avatar
Zot committed
158

159
function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false) {
Zot's avatar
Zot committed
160

Friendika's avatar
Friendika committed
161 162
	$a = get_app();

Zot's avatar
Zot committed
163 164
	$o = '';

165
	// When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
Zot's avatar
Zot committed
166 167
	// to one recipient. By default our selector allows multiple selects amongst all contacts.

Zot's avatar
Zot committed
168 169 170
	$sql_extra = '';

	if($privmail || $celeb) {
Friendika's avatar
Friendika committed
171
		$sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
Zot's avatar
Zot committed
172
	}
Zot's avatar
Zot committed
173

174
	if($privmail) {
friendica's avatar
friendica committed
175
		$sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
176 177
	}
	elseif($privatenet) {	
178
		$sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
179
	}
180

181 182


Zot's avatar
Zot committed
183 184 185
	if($privmail)
		$o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" >\r\n";
	else 
186
		$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
Zot's avatar
Zot committed
187

Zot's avatar
Zot committed
188
	$r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
Friendika's avatar
Friendika committed
189
		WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `notify` != ''
Zot's avatar
Zot committed
190
		$sql_extra
Zot's avatar
Zot committed
191
		ORDER BY `name` ASC ",
Zot's avatar
Zot committed
192
		intval(local_user())
Zot's avatar
Zot committed
193 194
	);

Friendika's avatar
Friendika committed
195 196 197 198 199 200 201

	$arr = array('contact' => $r, 'entry' => $o);

	// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'

	call_hooks($a->module . '_pre_' . $selname, $arr);

Zot's avatar
Zot committed
202 203
	if(count($r)) {
		foreach($r as $rr) {
Zot's avatar
Zot committed
204
			if((is_array($preselected)) && in_array($rr['id'], $preselected))
Zot's avatar
Zot committed
205 206 207
				$selected = " selected=\"selected\" ";
			else
				$selected = '';
208

209
			$trimmed = mb_substr($rr['name'],0,20);
210 211

			$o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
Zot's avatar
Zot committed
212 213 214
		}
	
	}
Friendika's avatar
Friendika committed
215

Zot's avatar
Zot committed
216 217
	$o .= "</select>\r\n";

Friendika's avatar
Friendika committed
218
	call_hooks($a->module . '_post_' . $selname, $o);
Zot's avatar
Zot committed
219 220 221 222

	return $o;
}

223

Zot's avatar
Zot committed
224 225 226
function fixacl(&$item) {
	$item = intval(str_replace(array('<','>'),array('',''),$item));
}
Zot's avatar
Zot committed
227

Zot's avatar
Zot committed
228
function populate_acl($user = null,$celeb = false) {
Zot's avatar
Zot committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	$allow_cid = $allow_gid = $deny_cid = $deny_gid = false;

	if(is_array($user)) {
		$allow_cid = ((strlen($user['allow_cid'])) 
			? explode('><', $user['allow_cid']) : array() );
		$allow_gid = ((strlen($user['allow_gid']))
			? explode('><', $user['allow_gid']) : array() );
		$deny_cid  = ((strlen($user['deny_cid']))
			? explode('><', $user['deny_cid']) : array() );
		$deny_gid  = ((strlen($user['deny_gid']))
			? explode('><', $user['deny_gid']) : array() );
		array_walk($allow_cid,'fixacl');
		array_walk($allow_gid,'fixacl');
		array_walk($deny_cid,'fixacl');
		array_walk($deny_gid,'fixacl');
	}
Zot's avatar
Zot committed
246

Fabio Comuni's avatar
Fabio Comuni committed
247
	/*$o = '';
Zot's avatar
Zot committed
248 249
	$o .= '<div id="acl-wrapper">';
	$o .= '<div id="acl-permit-outer-wrapper">';
250
	$o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
Zot's avatar
Zot committed
251 252 253 254
	$o .= '<div id="acl-permit-text-end"></div>';
	$o .= '<div id="acl-permit-wrapper">';
	$o .= '<div id="group_allow_wrapper">';
	$o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
Zot's avatar
Zot committed
255
	$o .= group_select('group_allow','group_allow',$allow_gid);
Zot's avatar
Zot committed
256 257 258
	$o .= '</div>';
	$o .= '<div id="contact_allow_wrapper">';
	$o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
259
	$o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
Zot's avatar
Zot committed
260 261 262 263 264 265 266 267 268 269
	$o .= '</div>';
	$o .= '</div>' . "\r\n";
	$o .= '<div id="acl-allow-end"></div>' . "\r\n";
	$o .= '</div>';
	$o .= '<div id="acl-deny-outer-wrapper">';
	$o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
	$o .= '<div id="acl-deny-text-end"></div>';
	$o .= '<div id="acl-deny-wrapper">';
	$o .= '<div id="group_deny_wrapper" >';
	$o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
Zot's avatar
Zot committed
270
	$o .= group_select('group_deny','group_deny', $deny_gid);
Zot's avatar
Zot committed
271 272 273
	$o .= '</div>';
	$o .= '<div id="contact_deny_wrapper" >';
	$o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
274
	$o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
Zot's avatar
Zot committed
275 276 277 278 279
	$o .= '</div>';
	$o .= '</div>' . "\r\n";
	$o .= '<div id="acl-deny-end"></div>' . "\r\n";
	$o .= '</div>';
	$o .= '</div>' . "\r\n";
Fabio Comuni's avatar
Fabio Comuni committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293
	$o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
	
	$tpl = get_markup_template("acl_selector.tpl");
	$o = replace_macros($tpl, array(
		'$showall'=> t("Visible to everybody"),
		'$show'		 => t("show"),
		'$hide'		 => t("don't show"),
		'$allowcid' => json_encode($allow_cid),
		'$allowgid' => json_encode($allow_gid),
		'$denycid' => json_encode($deny_cid),
		'$denygid' => json_encode($deny_gid),
	));
	
	
Zot's avatar
Zot committed
294 295
	return $o;

Zot's avatar
Zot committed
296 297
}