1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_group_info()
9{
10	return [
11		'name' => tra('Group'),
12		'documentation' => 'PluginGroup',
13		'description' => tra('Display content based on the user\'s groups or friends'),
14		'body' => tr('Wiki text to display if conditions are met. The body may contain %0. Text after the marker
15			will be displayed to users not matching the conditions.', '<code>{ELSE}</code>'),
16		'prefs' => ['wikiplugin_group'],
17		'iconname' => 'group',
18		'filter' => 'wikicontent',
19		'introduced' => 1,
20		'tags' => [ 'basic' ],
21		'params' => [
22			'friends' => [
23				'required' => false,
24				'name' => tra('Allowed User Friends'),
25				'description' => tra('Pipe separated list of users whose friends are allowed to view the block.
26					Example:') . ' <code>admin|johndoe|foo</code>',
27				'since' => '4.0',
28				'filter' => 'username',
29				'default' => ''
30			],
31			'groups' => [
32				'required' => false,
33				'name' => tra('Allowed Groups'),
34				'description' => tra('Pipe separated list of groups allowed to view the block.
35					Example:') . ' <code>Admins|Developers</code>',
36				'since' => '1',
37				'filter' => 'groupname',
38				'default' => ''
39			],
40			'notgroups' => [
41				'required' => false,
42				'name' => tra('Denied Groups'),
43				'description' => tra('Pipe-separated list of groups not allowed to view the block.'),
44				'since' => '1',
45				'filter' => 'groupname',
46				'default' => ''
47			],
48			'pending' => [
49				'required' => false,
50				'name' => tra('Allowed Groups Pending Membership'),
51				'description' => tra('User allowed to view block if membership payment to join group (or pipe-separated
52					list of groups) is outstanding.'),
53				'since' => '13.0',
54				'filter' => 'groupname',
55				'default' => ''
56			],
57			'notpending' => [
58				'required' => false,
59				'name' => tra('Allowed Groups Full Membership'),
60				'description' => tra('User allowed to view block if membership in the group (or pipe-separated list of
61					groups) is not pending.'),
62				'since' => '13.0',
63				'filter' => 'groupname',
64				'default' => ''
65			],
66		],
67	];
68}
69
70function wikiplugin_group($data, $params)
71{
72	// TODO : Re-implement friend filter
73	global $user, $groupPluginReturnAll;
74	$tikilib = TikiLib::lib('tiki');
75	$dataelse = '';
76	if (strrpos($data, '{ELSE}')) {
77		$dataelse = substr($data, strrpos($data, '{ELSE}') + 6);
78		$data = substr($data, 0, strrpos($data, '{ELSE}'));
79	}
80
81	if (isset($groupPluginReturnAll) && $groupPluginReturnAll == true) {
82		return $data . $dataelse;
83	}
84
85	if (! empty($params['groups'])) {
86		$groups = explode('|', $params['groups']);
87	}
88	if (! empty($params['notgroups'])) {
89		$notgroups = explode('|', $params['notgroups']);
90	}
91	$userPending = [];
92	if (! empty($params['pending']) || ! empty($params['notpending'])) {
93		$attributelib = TikiLib::lib('attribute');
94		$attributes = $attributelib->get_attributes('user', $user);
95		$userlib = TikiLib::lib('user');
96		if (! empty($params['pending'])) {
97			$pending = explode('|', $params['pending']);
98			foreach ($pending as $pgrp) {
99				$grpinfo = $userlib->get_group_info($pgrp);
100				$attname = 'tiki.memberextend.' . $grpinfo['id'];
101				if (isset($attributes[$attname])) {
102					$userPending[] = $pgrp;
103				}
104			}
105		}
106		if (! empty($params['notpending'])) {
107			$notpending = explode('|', $params['notpending']);
108			foreach ($notpending as $npgrp) {
109				$grpinfo = $userlib->get_group_info($npgrp);
110				$attname = 'tiki.memberextend.' . $grpinfo['id'];
111				if (! isset($attributes[$attname])) {
112					$userNotPending[] = $npgrp;
113				}
114			}
115		}
116	}
117
118	if (empty($groups) && empty($notgroups) && empty($pending) && empty($notpending)) {
119		return '';
120	}
121
122	$userGroups = $tikilib->get_user_groups($user);
123	$smarty = TikiLib::lib('smarty');
124	if (count($userGroups) > 1) { //take away the anonymous as everybody who is registered is anonymous
125		foreach ($userGroups as $key => $grp) {
126			if ($grp == 'Anonymous') {
127				$userGroups[$key] = '';
128				break;
129			}
130		}
131	}
132	if (! empty($groups) || ! empty($pending)) {
133		$ok = false;
134		if (! empty($groups)) {
135			foreach ($userGroups as $grp) {
136				if (in_array($grp, $groups)) {
137					$ok = true;
138					$smarty->assign('groupValid', 'y');
139					break;
140				}
141				$smarty->assign('groupValid', 'n');
142			}
143		}
144		if (count($userPending) > 0) {
145			$ok = true;
146		}
147		if (! $ok) {
148			return $dataelse;
149		}
150	}
151
152	if (! empty($notgroups) || ! empty($notpending)) {
153		$ok = true;
154		if (! empty($notgroups)) {
155			foreach ($userGroups as $grp) {
156				if (in_array($grp, $notgroups)) {
157					$ok = false;
158					$smarty->assign('notgroupValid', 'y');
159					break;
160				}
161				$smarty->assign('notgroupValid', 'n');
162			}
163		}
164		if (isset($userNotPending) && (count($userNotPending) < count($notpending))) {
165			$ok = false;
166		}
167		if (! $ok) {
168			return $dataelse;
169		}
170	}
171
172
173	return $data;
174}
175