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 * Tiki_Profile_ChannelList
10 *
11 * @package
12 */
13class Tiki_Profile_ChannelList
14{
15	private $channels = [];
16
17	public static function fromConfiguration($string)
18	{
19		$list = new self;
20
21		$string = str_replace("\r", '', $string);
22		$lines = explode("\n", $string);
23
24		foreach ($lines as $line) {
25			$parts = explode(',', $line);
26			if (count($parts) < 3) {
27				continue;
28			} elseif (count($parts) == 3) {
29				$parts[] = 'Admins';
30			}
31
32			$parts = array_map('trim', $parts);
33			list($name, $domain, $profile) = array_slice($parts, 0, 3);
34			$groups = array_slice($parts, 3);
35
36			$list->channels[$name] = [
37				'domain' => $domain,
38				'profile' => $profile,
39				'groups' => $groups,
40			];
41		}
42
43		return $list;
44	}
45
46	function canExecuteChannels(array $channelNames, array $groups, $skipInputCheck = false)
47	{
48		foreach ($channelNames as $channel) {
49			if (! array_key_exists($channel, $this->channels)) {
50				return false;
51			}
52
53			// At least one match is required
54			if (count(array_intersect($groups, $this->channels[$channel]['groups'])) == 0) {
55				return false;
56			}
57
58			// Checking against input if required (note that unlike normal groups, all must match)
59			foreach ($this->channels[$channel]['groups'] as $g) {
60				if ($skipInputCheck) {
61					break;
62				}
63				if (preg_match('/\$profilerequest\:(\w+)\$/', $g, $matches)) {
64					for ($i = 1, $count_matches = count($matches); $i < $count_matches; $i++) {
65						if (empty($_REQUEST[$matches[$i]])) {
66							return false;
67						} else {
68							$tocheck = str_replace($matches[0], $_REQUEST[$matches[$i]], $g);
69							if (! in_array($tocheck, $groups)) {
70								return false;
71							}
72						}
73					}
74				}
75			}
76		}
77
78		return true;
79	}
80
81	function getProfiles(array $channelNames)
82	{
83		$profiles = [];
84
85		foreach ($channelNames as $channelName) {
86			$info = $this->channels[$channelName];
87
88			if ($profile = Tiki_Profile::fromNames($info['domain'], $info['profile'])) {
89				$profiles[$channelName] = $profile;
90			}
91		}
92
93		return $profiles;
94	}
95
96	function addChannel($name, $domain, $profile, $groups)
97	{
98		$this->channels[ $name ] = [
99			'domain' => $domain,
100			'profile' => $profile,
101			'groups' => $groups,
102		];
103	}
104
105	function getConfiguration()
106	{
107		$out = '';
108		foreach ($this->channels as $name => $info) {
109			$parts = $info['groups'];
110			array_unshift($parts, $name, $info['domain'], $info['profile']);
111
112			$out .= implode(', ', $parts) . "\n";
113		}
114
115		return trim($out);
116	}
117}
118