-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickacl.php
157 lines (134 loc) · 3.79 KB
/
quickacl.php
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
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @package quickACL
* @copyright Copyright (C) 2012 Julien Vonthron. All rights reserved. Based on original inlineACL plugin from keep http://joomla.blog.hu
* @license GNU/GPL, see LICENSE.php
* quickACL is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Import library dependencies
jimport('joomla.plugin.plugin');
class plgContentQuickacl extends JPlugin {
public function __construct( $subject, $params )
{
parent::__construct( $subject, $params );
$this->loadLanguage();
}
/**
* Plugin that loads events lists within content
*
* @param string The context of the content being passed to the plugin.
* @param object The article object. Note $article->text is also available
* @param object The article params
* @param int The 'page' number
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
if ( JString::strpos( $article->text, 'qACL' ) === false
&& JString::strpos( $article->text, 'iACL' ) === false) {
return true;
}
$regex = "#{(?:q|i)ACL.*type=([^\s]+)\s(.*)}(.*){/(?:q|i)ACL}#sU";
$article->text = preg_replace_callback( $regex, array($this, '_replacer'), $article->text );
return true;
}
protected function _replacer( &$matches )
{
$user = JFactory::getUser();
$db = JFactory::getDBO();
$text = "";
$cbfield = $this->params->get('cbfield', '');
$ids = explode(",", $matches[2]);
$ids = array_map('trim', $ids);
switch($matches[1]) {
case "userid":
if (in_array($user->id, $ids)) {
$text = $matches[3];
}
break;
case "!userid":
if (!in_array($user->id, $ids)) {
$text = $matches[3];
}
break;
case "username":
if (in_array($user->username, $ids)) {
$text = $matches[3];
}
break;
case "!username":
if (!in_array($user->username, $ids)) {
$text = $matches[3];
}
break;
case "group":
if (self::isInGroup($user, $ids)) {
$text = $matches[3];
}
break;
case "!group":
if (!self::isInGroup($user, $ids)) {
$text = $matches[3];
}
break;
case "cbfield":
if(empty($cbfield)) return;
$query = "SELECT $cbfield as f
FROM #__comprofiler
WHERE id = " . $user->id;
$db->setQuery($query);
$list = $db->loadObjectList();
$fieldValue = $list[0]->f;
if(in_array($fieldValue, $ids)) {
$text = $matches[3];
}
break;
case "!cbfield":
if(empty($cbfield)) return;
$query = "SELECT $cbfield as f
FROM #__comprofiler
WHERE id = " . $user->id;
$db->setQuery($query);
$list = $db->loadObjectList();
$fieldValue = $list[0]->f;
if(!in_array($fieldValue, $ids)) {
$text = $matches[3];
}
break;
}
return $text;
}
/**
* returns true if the user belongs to one of the groups
*
* @param JUser $user
* @param Array group names
* @return array
*/
protected static function isInGroup(JUser $user, array $names)
{
// check if 'guest'
if (in_array("guest", $names))
{
return $user->id > 0 ? false : true;
}
$groups = JAccess::getGroupsByUser($user->id, true);
if (!$groups) {
return array();
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('LOWER(grp.title)')
->from('`#__usergroups` AS grp');
$query->where('grp.id IN ('.implode(',', $groups).')');
$db->setQuery($query);
$res = $db->loadResultArray();
$names = array_map('strtolower', $names);
$inter = array_intersect($res, $names);
return count($inter) > 0;
}
}