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
8/**
9 * Smarty plugin
10 * @package Smarty
11 * @subpackage plugins
12 *
13 * \brief Smarty plugin to display content only to some groups, friends or combination of all per specified user(s)
14 * (if user is not specified, current user is used)
15 * ex.: {display groups='Anonymous,-Registered,foo' friends=$f_42[ error='You may not see this item']}$f_1...$f_9///else///Become friend with $_42 first{/display}
16 * TODO : Re-implement friend filter
17 */
18
19//this script may only be included - so its better to die if called directly.
20if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
21	header("location: index.php");
22	exit;
23}
24
25function smarty_block_display($params, $content, $smarty, &$repeat)
26{
27	global $prefs, $user;
28	$userlib = TikiLib::lib('user');
29
30	if ($repeat) {
31		return;
32	}
33	$ok = true;
34	if (! empty($params['groups'])) {
35		$groups = explode(',', $params['groups']);
36		$userGroups = $userlib->get_user_groups($user);
37	}
38
39	$content = explode('///else///', $content);
40
41	if (! empty($params['error'])) {
42		$errmsg = $params['error'];
43	} elseif (empty($params['error']) && isset($groups)) {
44		$errmsg = '';
45	} else {
46		$errmsg = 'Smarty block.display.php: Missing error param';
47	}
48
49	$anon = false; // see the workaround to exclude Registered below
50
51	foreach ($groups as $gr) {
52		$gr = trim($gr);
53		if ($gr == 'Anonymous') {
54			$anon = true;
55		}
56		if (substr($gr, 0, 1) == '-') {
57			$nogr = substr($gr, 1);
58			if ((in_array($nogr, $userGroups) && $nogr != 'Registered') or (in_array($nogr, $userGroups) && $nogr == 'Registered' && $anon == true)) {
59				// workaround to display to Anonymous only if Registered excluded (because Registered includes Anonymous always)
60				$ok = false;
61				$anon = false;
62			}
63		} elseif (! in_array($gr, $userGroups) && $anon == false) {
64			$ok = false;
65		} else {
66			$ok = true;
67		}
68	}
69
70	/* is it ok ? */
71	if (! $ok) {
72		if (isset($content[1])) {
73			return $content[1];
74		} else {
75			return $errmsg;
76		}
77	} else {
78		return $content[0];
79	}
80}
81