PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/newbb/class/permission.php

https://gitlab.com/VoyaTrax/vtCMS2
PHP | 419 lines | 254 code | 53 blank | 112 comment | 40 complexity | e6849f1a974d99e5740fc58e6ccacd23 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MIT, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * NewBB 4.3x, the forum module for XOOPS project
  4. *
  5. * @copyright XOOPS Project (http://xoops.org)
  6. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  7. * @author Taiwen Jiang (phppp or D.J.) <phppp@users.sourceforge.net>
  8. * @since 4.00
  9. * @package module::newbb
  10. */
  11. // defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
  12. defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
  13. define('NEWBB_HANDLER_PERMISSION', 1);
  14. load_functions('cache');
  15. // Initializing XoopsGroupPermHandler if not loaded yet
  16. if (!class_exists('XoopsGroupPermHandler')) {
  17. require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
  18. }
  19. /**
  20. * Class NewbbPermissionHandler
  21. */
  22. class NewbbPermissionHandler extends XoopsGroupPermHandler
  23. {
  24. public $_handler;
  25. /**
  26. * @param $db
  27. */
  28. public function __construct($db)
  29. {
  30. $this->db = $db;
  31. parent::__construct($db);
  32. }
  33. /**
  34. * @param $name
  35. * @return mixed
  36. */
  37. public function _loadHandler($name)
  38. {
  39. if (!isset($this->_handler[$name])) {
  40. require_once __DIR__ . "/permission.{$name}.php";
  41. $className = 'NewbbPermission' . ucfirst($name) . 'Handler';
  42. $this->_handler[$name] = new $className($this->db);
  43. }
  44. return $this->_handler[$name];
  45. }
  46. /**
  47. * @param bool $fullname
  48. * @return mixed
  49. */
  50. public function getValidForumPerms($fullname = false)
  51. {
  52. $handler = $this->_loadHandler('forum');
  53. return $handler->getValidPerms($fullname);
  54. }
  55. /**
  56. * @param int $forum
  57. * @param bool $topic_locked
  58. * @param bool $isadmin
  59. * @return mixed
  60. */
  61. public function &permission_table($forum = 0, $topic_locked = false, $isadmin = false)
  62. {
  63. $handler = $this->_loadHandler('forum');
  64. $perm = $handler->permission_table($forum, $topic_locked, $isadmin);
  65. return $perm;
  66. }
  67. /**
  68. * @param $forum_id
  69. * @return mixed
  70. */
  71. public function deleteByForum($forum_id)
  72. {
  73. mod_clearCacheFile('permission_forum', 'newbb');
  74. $handler = $this->_loadHandler('forum');
  75. return $handler->deleteByForum($forum_id);
  76. }
  77. /**
  78. * @param $cat_id
  79. * @return mixed
  80. */
  81. public function deleteByCategory($cat_id)
  82. {
  83. mod_clearCacheFile('permission_category', 'newbb');
  84. $handler = $this->_loadHandler('category');
  85. return $handler->deleteByCategory($cat_id);
  86. }
  87. /**
  88. * @param $category
  89. * @param array $groups
  90. * @return mixed
  91. */
  92. public function setCategoryPermission($category, array $groups = array())
  93. {
  94. mod_clearCacheFile('permission_category', 'newbb');
  95. $handler = $this->_loadHandler('category');
  96. return $handler->setCategoryPermission($category, $groups);
  97. }
  98. /**
  99. * @param $type
  100. * @param string $gperm_name
  101. * @param int $id
  102. * @return bool
  103. */
  104. public function getPermission($type, $gperm_name = 'access', $id = 0)
  105. {
  106. global $xoopsModule;
  107. $ret = false;
  108. if ($GLOBALS['xoopsUserIsAdmin'] && $xoopsModule->getVar('dirname') === 'newbb') {
  109. $ret = true;
  110. }
  111. $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
  112. if (!$groups) {
  113. $ret = false;
  114. }
  115. if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
  116. $ret = false;
  117. }
  118. if (count(array_intersect($allowed_groups, $groups)) > 0) {
  119. $ret = true;
  120. }
  121. return $ret;
  122. }
  123. /**
  124. * @param string $perm_name
  125. * @return array
  126. */
  127. public function &getCategories($perm_name = 'access')
  128. {
  129. $ret = $this->getAllowedItems('category', "category_{$perm_name}");
  130. return $ret;
  131. }
  132. /**
  133. * @param string $perm_name
  134. * @return array
  135. */
  136. public function getForums($perm_name = 'access')
  137. {
  138. $ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
  139. return $ret;
  140. }
  141. /**
  142. * @param $type
  143. * @param $perm_name
  144. * @return array
  145. */
  146. public function getAllowedItems($type, $perm_name)
  147. {
  148. $ret = array();
  149. $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
  150. if (count($groups) < 1) {
  151. return $ret;
  152. }
  153. if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
  154. return $ret;
  155. }
  156. $allowed_items = array();
  157. foreach ($_cachedPerms as $id => $allowed_groups) {
  158. if ($id == 0 || empty($allowed_groups)) {
  159. continue;
  160. }
  161. if (array_intersect($groups, $allowed_groups)) {
  162. $allowed_items[$id] = 1;
  163. }
  164. }
  165. unset($_cachedPerms);
  166. $ret = array_keys($allowed_items);
  167. return $ret;
  168. }
  169. /**
  170. * @param $gperm_name
  171. * @param int $id
  172. * @return array
  173. */
  174. public function getGroups($gperm_name, $id = 0)
  175. {
  176. $_cachedPerms = $this->loadPermData($gperm_name);
  177. $groups = empty($_cachedPerms[$id]) ? array() : array_unique($_cachedPerms[$id]);
  178. unset($_cachedPerms);
  179. return $groups;
  180. }
  181. /**
  182. * @param string $perm_name
  183. * @return array
  184. */
  185. public function createPermData($perm_name = 'forum_all')
  186. {
  187. global $xoopsModule;
  188. $perms = array();
  189. if (is_object($xoopsModule) && $xoopsModule->getVar('dirname') === 'newbb') {
  190. $modid = $xoopsModule->getVar('mid');
  191. } else {
  192. $module_handler = xoops_getHandler('module');
  193. $module = $module_handler->getByDirname('newbb');
  194. $modid = $module->getVar('mid');
  195. unset($module);
  196. }
  197. if (in_array($perm_name, array('forum_all', 'category_all'), true)) {
  198. $memberHandler = xoops_getHandler('member');
  199. $groups = array_keys($memberHandler->getGroupList());
  200. $type = ($perm_name === 'category_all') ? 'category' : 'forum';
  201. $object_handler = xoops_getModuleHandler($type, 'newbb');
  202. $object_ids = $object_handler->getIds();
  203. foreach ($object_ids as $item_id) {
  204. $perms[$perm_name][$item_id] = $groups;
  205. }
  206. } else {
  207. $gpermHandler = xoops_getHandler('groupperm');
  208. $criteria = new CriteriaCompo(new Criteria('gperm_modid', $modid));
  209. if (!empty($perm_name) && $perm_name !== 'forum_all' && $perm_name !== 'category_all') {
  210. $criteria->add(new Criteria('gperm_name', $perm_name));
  211. }
  212. $permissions = $this->getObjects($criteria);
  213. foreach ($permissions as $gperm) {
  214. $item_id = $gperm->getVar('gperm_itemid');
  215. $group_id = (int)$gperm->getVar('gperm_groupid');
  216. $perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
  217. }
  218. }
  219. load_functions('cache');
  220. if (count($perms) > 0) {
  221. foreach (array_keys($perms) as $perm) {
  222. mod_createCacheFile($perms[$perm], 'permission_{$perm}', 'newbb');
  223. }
  224. }
  225. $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
  226. return $ret;
  227. }
  228. /**
  229. * @param string $perm_name
  230. * @return array|mixed|null
  231. */
  232. public function &loadPermData($perm_name = 'forum_access')
  233. {
  234. load_functions('cache');
  235. if (!$perms = mod_loadCacheFile('permission_{$perm_name}', 'newbb')) {
  236. $perms = $this->createPermData($perm_name);
  237. }
  238. return $perms;
  239. }
  240. /**
  241. * @param $perm
  242. * @param $itemid
  243. * @param $groupid
  244. * @param null $mid
  245. * @return bool
  246. */
  247. public function validateRight($perm, $itemid, $groupid, $mid = null)
  248. {
  249. if (empty($mid)) {
  250. if (is_object($GLOBALS['xoopsModule']) && $GLOBALS['xoopsModule']->getVar('dirname') === 'newbb') {
  251. $mid = $GLOBALS['xoopsModule']->getVar('mid');
  252. } else {
  253. $module_handler = xoops_getHandler('module');
  254. $mod = $module_handler->getByDirname('newbb');
  255. $mid = $mod->getVar('mid');
  256. unset($mod);
  257. }
  258. }
  259. if ($this->_checkRight($perm, $itemid, $groupid, $mid)) {
  260. return true;
  261. }
  262. load_functions('cache');
  263. mod_clearCacheFile('permission', 'newbb');
  264. $this->addRight($perm, $itemid, $groupid, $mid);
  265. return true;
  266. }
  267. /**
  268. * Check permission (directly)
  269. *
  270. * @param string $gperm_name Name of permission
  271. * @param int $gperm_itemid ID of an item
  272. * @param int /array $gperm_groupid A group ID or an array of group IDs
  273. * @param int $gperm_modid ID of a module
  274. *
  275. * @return bool TRUE if permission is enabled
  276. */
  277. public function _checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
  278. {
  279. $ret = false;
  280. $criteria = new CriteriaCompo(new Criteria('gperm_modid', $gperm_modid));
  281. $criteria->add(new Criteria('gperm_name', $gperm_name));
  282. $gperm_itemid = (int)$gperm_itemid;
  283. if ($gperm_itemid > 0) {
  284. $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
  285. }
  286. if (is_array($gperm_groupid)) {
  287. $criteria2 = new CriteriaCompo();
  288. foreach ($gperm_groupid as $gid) {
  289. $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
  290. }
  291. $criteria->add($criteria2);
  292. } else {
  293. $criteria->add(new Criteria('gperm_groupid', $gperm_groupid));
  294. }
  295. if ($this->getCount($criteria) > 0) {
  296. $ret = true;
  297. }
  298. return $ret;
  299. }
  300. /**
  301. * @param $perm
  302. * @param $itemid
  303. * @param $groupid
  304. * @param null $mid
  305. * @return bool
  306. */
  307. public function deleteRight($perm, $itemid, $groupid, $mid = null)
  308. {
  309. mod_clearCacheFile('permission', 'newbb');
  310. if (null === $mid) {
  311. if (is_object($GLOBALS['xoopsModule']) && $GLOBALS['xoopsModule']->getVar('dirname') === 'newbb') {
  312. $mid = $GLOBALS['xoopsModule']->getVar('mid');
  313. } else {
  314. $module_handler = xoops_getHandler('module');
  315. $mod = $module_handler->getByDirname('newbb');
  316. $mid = $mod->getVar('mid');
  317. unset($mod);
  318. }
  319. }
  320. if (is_callable(array(&$this->XoopsGroupPermHandler, 'deleteRight'))) {
  321. return parent::deleteRight($perm, $itemid, $groupid, $mid);
  322. } else {
  323. $criteria = new CriteriaCompo(new Criteria('gperm_name', $perm));
  324. $criteria->add(new Criteria('gperm_groupid', $groupid));
  325. $criteria->add(new Criteria('gperm_itemid', $itemid));
  326. $criteria->add(new Criteria('gperm_modid', $mid));
  327. $perms_obj = $this->getObjects($criteria);
  328. if (!empty($perms_obj)) {
  329. foreach ($perms_obj as $perm_obj) {
  330. $this->delete($perm_obj);
  331. }
  332. }
  333. unset($criteria, $perms_obj);
  334. }
  335. return true;
  336. }
  337. /**
  338. * @param $forum
  339. * @param int $mid
  340. * @return mixed
  341. */
  342. public function applyTemplate($forum, $mid = 0)
  343. {
  344. mod_clearCacheFile('permission_forum', 'newbb');
  345. $handler = $this->_loadHandler('forum');
  346. return $handler->applyTemplate($forum, $mid);
  347. }
  348. /**
  349. * @return mixed
  350. */
  351. public function getTemplate()
  352. {
  353. $handler = $this->_loadHandler('forum');
  354. $template = $handler->getTemplate();
  355. return $template;
  356. }
  357. /**
  358. * @param $perms
  359. * @return mixed
  360. */
  361. public function setTemplate($perms)
  362. {
  363. $handler = $this->_loadHandler('forum');
  364. return $handler->setTemplate($perms);
  365. }
  366. }