aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Permcat.php
blob: 505ee2cfcc362ba12a549e235b77c7c4ff1f272a (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
<?php

namespace Zotlabs\Lib;

use \Zotlabs\Access as Zaccess;

class Permcat {

	private $permcats = [];

	public function __construct($channel_id) {

		$perms = [];

		// first check role perms for a perms_connect setting

		$role = get_pconfig($channel_id,'system','permissions_role');
		if($role) {
			$x = Zaccess\PermissionRoles::role_perms($role);
			if($x['perms_connect']) {
				$perms = Zaccess\Permissions::FilledPerms($x['perms_connect']);
			}
		}

		// if no role perms it may be a custom role, see if there any autoperms

		if(! $perms) {
			$perms = Zaccess\Permissions::FilledAutoPerms($channel_id);
		}

		// if no autoperms it may be a custom role with manual perms

		if(! $perms) {
			$r = q("select channel_hash from channel where channel_id = %d",
				intval($channel_id)
			);
			if($r) {
				$x = q("select * from abconfig where chan = %d and xchan = '%s' and cat = 'my_perms'",
					intval($channel_id),
					dbesc($r[0]['channel_hash'])
				);
				if($x) {
					foreach($x as $xv) {
						$perms[$xv['k']] = intval($xv['v']);
					}
				}
			}
		}

		// nothing was found - create a filled permission array where all permissions are 0

		if(! $perms) {
			$perms = Zaccess\Permissions::FilledPerms([]);
		}

		$this->permcats[] = [
			'name'      => 'default',
			'localname' => t('default','permcat'),
			'perms'     => Zaccess\Permissions::Operms($perms),
			'system'    => 1
		];


		$p = $this->load_permcats($channel_id);
		if($p) {
			for($x = 0; $x < count($p); $x++) {
				$this->permcats[] = [
					'name'      => $p[$x][0],
					'localname' => $p[$x][1],
					'perms'     => Zaccess\Permissions::Operms(Zaccess\Permissions::FilledPerms($p[$x][2])),
					'system'    => intval($p[$x][3])
				];
			}
		}
	}


	public function listing() {
		return $this->permcats;
	}

	public function fetch($name) {
		if($name && $this->permcats) {
			foreach($this->permcats as $permcat) {
				if(strcasecmp($permcat['name'],$name) === 0) {
					return $permcat;
				}
			}
		}
		return ['error' => true];
	}

	public function load_permcats($uid) {

		$permcats = [
			[ 'follower', t('follower','permcat'),
				[ 'view_stream','view_profile','view_contacts','view_storage','view_pages','view_wiki',
				  'post_like' ], 1
			],
			[ 'contributor', t('contributor','permcat'),
				[ 'view_stream','view_profile','view_contacts','view_storage','view_pages','view_wiki',
				  'post_wall','post_comments','write_wiki','post_like','tag_deliver','chat' ], 1
			],
			[ 'publisher', t('publisher','permcat'),
				[ 'view_stream','view_profile','view_contacts','view_storage','view_pages',
				  'write_storage','post_wall','write_pages','write_wiki','post_comments','post_like','tag_deliver',
				  'chat', 'republish' ], 1
			]
		];

		if($uid) {
			$x = q("select * from pconfig where uid = %d and cat = 'permcat'",
				intval($uid)
			);
			if($x) {
				foreach($x as $xv) {
					$value = ((preg_match('|^a:[0-9]+:{.*}$|s', $xv['v'])) ? unserialize($xv['v']) : $xv['v']);
					$permcats[] = [ $xv['k'], $xv['k'], $value, 0 ];
				}
			}
		}					

		call_hooks('permcats',$permcats);

		return $permcats;

	}

	static public function find_permcat($arr,$name) {
		if((! $arr) || (! $name))
			return false;
		foreach($arr as $p)
			if($p['name'] == $name)
				return $p['value'];
	}

	static public function update($channel_id, $name,$permarr) {
		PConfig::Set($channel_id,'permcat',$name,$permarr);
	}

	static public function delete($channel_id,$name) {
		PConfig::Delete($channel_id,'permcat',$name);
	}


}