1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: function checkLangInClients($aClients, $iLang, $aCfg, $oDb) {
37: $oClientLanguageCollection = new cApiClientLanguageCollection();
38: return $oClientLanguageCollection->hasLanguageInClients($iLang, $aClients);
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
61: function copyRightsForElement($area, $iditem, $newiditem, $idlang = false) {
62: global $perm, $auth, $area_tree;
63:
64: if (!is_object($perm)) {
65: return false;
66: }
67: if (!is_object($auth)) {
68: return false;
69: }
70:
71: $oDestRightCol = new cApiRightCollection();
72: $oSourceRighsColl = new cApiRightCollection();
73: $whereUsers = array();
74: $whereAreaActions = array();
75:
76:
77: $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']);
78:
79:
80: $userIDContainer[] = $auth->auth['uid'];
81: foreach ($userIDContainer as $key) {
82: $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
83: }
84: $whereUsers = '(' . implode(' OR ', $whereUsers) . ')';
85:
86:
87:
88: $areaContainer = $area_tree[$perm->showareas($area)];
89:
90:
91: $oActionColl = new cApiActionCollection();
92: $oActionColl->select('idarea IN (' . implode(',', $areaContainer) . ')');
93: while (($oItem = $oActionColl->next()) !== false) {
94: $whereAreaActions[] = '(idarea = ' . (int) $oItem->get('idarea') . ' AND idaction = ' . (int) $oItem->get('idaction') . ')';
95: }
96: $whereAreaActions = '(' . implode(' OR ', $whereAreaActions) . ')';
97:
98:
99:
100:
101:
102:
103: $sWhere = "{$whereAreaActions} AND {$whereUsers} AND idcat = {$iditem}";
104: if ($idlang) {
105: $sWhere .= ' AND idlang=' . (int) $idlang;
106: }
107:
108: $oSourceRighsColl->select($sWhere);
109: while (($oItem = $oSourceRighsColl->next()) !== false) {
110: $rs = $oItem->toObject();
111: $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $newiditem, $rs->idclient, $rs->idlang, $rs->type);
112: }
113:
114:
115: $perm->load_permissions(true);
116:
117: return true;
118: }
119:
120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137:
138: function createRightsForElement($area, $iditem, $idlang = false) {
139: global $perm, $auth, $area_tree, $client;
140:
141: if (!is_object($perm)) {
142: return false;
143: }
144: if (!is_object($auth)) {
145: return false;
146: }
147:
148: $oDestRightCol = new cApiRightCollection();
149: $oSourceRighsColl = new cApiRightCollection();
150: $whereUsers = array();
151: $rightsCache = array();
152:
153:
154: $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']);
155:
156:
157: $userIDContainer[] = $auth->auth['uid'];
158: foreach ($userIDContainer as $key) {
159: $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
160: }
161: $whereUsers = '(' . implode(' OR ', $whereUsers) . ')';
162:
163:
164:
165: $areaContainer = $area_tree[$perm->showareas($area)];
166:
167:
168:
169: $sWhere = 'idclient=' . (int) $client . ' AND idarea IN (' . implode(',', $areaContainer) . ')' . ' AND idcat != 0 AND idaction != 0 AND ' . $whereUsers;
170: if ($idlang) {
171: $sWhere .= ' AND idlang=' . (int) $idlang;
172: }
173:
174: $oSourceRighsColl->select($sWhere);
175: while (($oItem = $oSourceRighsColl->next()) !== false) {
176: $rs = $oItem->toObject();
177:
178:
179: $key = $rs->user_id . '-' . $rs->idarea . '-' . $rs->idaction . '-' . $iditem . '-' . $rs->idclient . '-' . $rs->idlang . '-' . $rs->type;
180: if (isset($rightsCache[$key])) {
181: continue;
182: }
183:
184:
185: $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $iditem, $rs->idclient, $rs->idlang, $rs->type);
186:
187: $rightsCache[$key] = true;
188: }
189:
190:
191: $perm->load_permissions(true);
192:
193: return true;
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208:
209: function deleteRightsForElement($area, $iditem, $idlang = false) {
210: global $perm, $area_tree, $client;
211:
212:
213: $areaContainer = $area_tree[$perm->showareas($area)];
214:
215: $sWhere = "idcat=" . (int) $iditem . " AND idclient=" . (int) $client . " AND idarea IN (" . implode(',', $areaContainer) . ")";
216: if ($idlang) {
217: $sWhere .= " AND idlang=" . (int) $idlang;
218: }
219:
220: $oRightColl = new cApiRightCollection();
221: $oRightColl->deleteByWhereClause($sWhere);
222:
223:
224: $perm->load_permissions(true);
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241:
242: function buildUserOrGroupPermsFromRequest($bAddUserToClient = false) {
243: global $cfg, $msysadmin, $madmin, $mclient, $mlang, $auth, $client;
244:
245: $aPerms = array();
246:
247:
248:
249: $bSysadmin = (isset($msysadmin) && $msysadmin);
250:
251: $aAdmin = (isset($madmin) && is_array($madmin)) ? $madmin : array();
252: foreach ($aAdmin as $p => $value) {
253: if (!is_numeric($value)) {
254: unset($aAdmin[$p]);
255: }
256: }
257:
258: $aClient = (isset($mclient) && is_array($mclient)) ? $mclient : array();
259: foreach ($aClient as $p => $value) {
260: if (!is_numeric($value)) {
261: unset($aClient[$p]);
262: }
263: }
264:
265: $aLang = (isset($mlang) && is_array($mlang)) ? $mlang : array();
266: foreach ($aLang as $p => $value) {
267: if (!is_numeric($value)) {
268: unset($aLang[$p]);
269: }
270: }
271:
272:
273:
274: if ($bSysadmin) {
275: $aPerms[] = 'sysadmin';
276: }
277:
278: foreach ($aAdmin as $value) {
279: $aPerms[] = sprintf('admin[%s]', $value);
280: }
281:
282: foreach ($aClient as $value) {
283: $aPerms[] = sprintf('client[%s]', $value);
284: }
285:
286: if (count($aClient) == 0 && $bAddUserToClient) {
287:
288:
289:
290:
291: $aUserPerm = explode(',', $auth->auth['perm']);
292: if (!in_array('sysadmin', $aUserPerm)) {
293: $aPerms[] = sprintf('client[%s]', $client);
294: }
295: }
296:
297: if (count($aLang) > 0 && count($aClient) > 0) {
298:
299:
300: $db = cRegistry::getDb();
301: foreach ($aLang as $value) {
302: if (checkLangInClients($aClient, $value, $cfg, $db)) {
303: $aPerms[] = sprintf('lang[%s]', $value);
304: }
305: }
306: }
307:
308: return $aPerms;
309: }
310:
311: 312: 313: 314: 315: 316: 317: 318:
319: function saveRights() {
320: global $perm, $notification, $db, $userid;
321: global $rights_list, $rights_list_old, $rights_client, $rights_lang;
322: global $aArticleRights, $aCategoryRights, $aTemplateRights;
323:
324:
325: if (!is_array($rights_list)) {
326: $rights_list = array();
327: }
328:
329:
330: $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
331:
332:
333: $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
334: $oAreaColl = new cApiAreaCollection();
335:
336: if (is_array($arraydel)) {
337: foreach ($arraydel as $value) {
338:
339: $data = explode('|', $value);
340:
341:
342: if (!empty($_REQUEST['filter_rights'])) {
343: if (($_REQUEST['filter_rights'] != 'article' && in_array($data[1], $aArticleRights)) ||
344: ($_REQUEST['filter_rights'] != 'category' && in_array($data[1], $aCategoryRights)) ||
345: ($_REQUEST['filter_rights'] != 'template' && in_array($data[1], $aTemplateRights))) {
346: continue;
347: }
348:
349: if ($_REQUEST['filter_rights'] != 'other' && !in_array($data[1], array_merge($aArticleRights, $aCategoryRights, $aTemplateRights))) {
350: continue;
351: }
352: }
353:
354: $data[0] = $oAreaColl->getAreaID($data[0]);
355: $data[1] = $perm->getIDForAction($data[1]);
356:
357: $where = "user_id = '" . $db->escape($userid) . "' AND idclient = " . (int) $rights_client . " AND idlang = " . (int) $rights_lang . " AND idarea = " . (int) $data[0] . " AND idcat = " . (int) $data[2] . " AND idaction = " . (int) $data[1] . " AND type = 0";
358: $oRightColl = new cApiRightCollection();
359: $oRightColl->deleteByWhereClause($where);
360: }
361: }
362:
363: unset($data);
364:
365:
366: if (is_array($arraysave)) {
367: foreach ($arraysave as $value) {
368:
369: $data = explode('|', $value);
370:
371:
372:
373:
374: $data[0] = $oAreaColl->getAreaID($data[0]);
375: $data[1] = $perm->getIDForAction($data[1]);
376:
377: if (!isset($data[1])) {
378: $data[1] = 0;
379: }
380:
381:
382: $oRightColl = new cApiRightCollection();
383: $oRightColl->create($userid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 0);
384: }
385: }
386:
387: $rights_list_old = $rights_list;
388:
389: return true;
390:
391: }
392:
393: 394: 395: 396: 397: 398: 399: 400:
401: function saveGroupRights() {
402: global $perm, $notification, $db, $groupid;
403: global $rights_list, $rights_list_old, $rights_client, $rights_lang;
404: global $aArticleRights, $aCategoryRights, $aTemplateRights;
405:
406:
407: if (!is_array($rights_list)) {
408: $rights_list = array();
409: }
410:
411:
412: $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
413:
414:
415: $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
416:
417: $oAreaColl = new cApiAreaCollection();
418:
419: if (is_array($arraydel)) {
420: foreach ($arraydel as $value) {
421: $data = explode('|', $value);
422:
423:
424: if (!empty($_REQUEST['filter_rights'])) {
425: if (($_REQUEST['filter_rights'] != 'article' && in_array($data[1], $aArticleRights)) ||
426: ($_REQUEST['filter_rights'] != 'category' && in_array($data[1], $aCategoryRights)) ||
427: ($_REQUEST['filter_rights'] != 'template' && in_array($data[1], $aTemplateRights))) {
428: continue;
429: }
430:
431: if ($_REQUEST['filter_rights'] != 'other' && !in_array($data[1], array_merge($aArticleRights, $aCategoryRights, $aTemplateRights))) {
432: continue;
433: }
434: }
435:
436: $data[0] = $oAreaColl->getAreaID($data[0]);
437: $data[1] = $perm->getIDForAction($data[1]);
438:
439: $where = "user_id = '" . $db->escape($groupid) . "' AND idclient = " . (int) $rights_client . " AND idlang = " . (int) $rights_lang . " AND idarea = " . (int) $data[0] . " AND idcat = " . (int) $data[2] . " AND idaction = " . (int) $data[1] . " AND type = 1";
440: $oRightColl = new cApiRightCollection();
441: $oRightColl->deleteByWhereClause($where);
442: }
443: }
444:
445: unset($data);
446:
447:
448: if (is_array($arraysave)) {
449: foreach ($arraysave as $value) {
450:
451: $data = explode('|', $value);
452:
453:
454:
455:
456: $data[0] = $oAreaColl->getAreaID($data[0]);
457: $data[1] = $perm->getIDForAction($data[1]);
458:
459: if (!isset($data[1])) {
460: $data[1] = 0;
461: }
462:
463:
464: $oRightColl = new cApiRightCollection();
465: $oRightColl->create($groupid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 1);
466: }
467: }
468:
469: $rights_list_old = $rights_list;
470: return true;
471: }
472:
473: 474: 475: 476: 477:
478: function getRightsList()
479: {
480: $areas = new cApiAreaCollection();
481: $navSubs = new cApiNavSubCollection();
482: $actions = new cApiActionCollection();
483:
484: try {
485: $rights = [];
486:
487: $areas->select('relevant = 1 AND online = 1 AND name != "login"');
488: while ($area = $areas->next()) {
489: $right = [
490: 'perm' => $area->get('name'),
491: 'location' => '',
492: ];
493:
494:
495: $navSubs->select('idarea = ' . (int)$area->get('idarea'));
496: if ($navSubItem = $navSubs->next()) {
497: $right['location'] = $navSubItem->get('location');
498: }
499:
500:
501: $actions->select('relevant = 1 AND idarea = ' . (int)$area->get('idarea'));
502: while ($action = $actions->next()) {
503: $right['action'][] = $action->get('name');
504: }
505:
506:
507: if ($area->get('parent_id') == '0') {
508: $key = $area->get('name');
509: } else {
510: $key = $area->get('parent_id');
511: }
512: $rights[$key][$area->get('name')] = $right;
513: }
514: } catch (cDbException $e) {
515: $rights = [];
516: } catch (cException $e) {
517: $rights = [];
518: }
519:
520: return $rights;
521: }
522: