1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15: 
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29: 
 30: function checkLangInClients($aClients, $iLang, $aCfg, $oDb) {
 31:     $oClientLanguageCollection = new cApiClientLanguageCollection();
 32:     return $oClientLanguageCollection->hasLanguageInClients($iLang, $aClients);
 33: }
 34: 
 35:  36:  37:  38:  39:  40:  41:  42:  43:  44: 
 45: function copyRightsForElement($area, $iditem, $newiditem, $idlang = false) {
 46:     global $perm, $auth, $area_tree;
 47: 
 48:     if (!is_object($perm)) {
 49:         return false;
 50:     }
 51:     if (!is_object($auth)) {
 52:         return false;
 53:     }
 54: 
 55:     $oDestRightCol = new cApiRightCollection();
 56:     $oSourceRighsColl = new cApiRightCollection();
 57:     $whereUsers = array();
 58:     $whereAreaActions = array();
 59: 
 60:     
 61:     $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']); 
 62:     
 63:     
 64:     $userIDContainer[] = $auth->auth['uid']; 
 65:     foreach ($userIDContainer as $key) {
 66:         $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
 67:     }
 68:     $whereUsers = '(' . implode(' OR ', $whereUsers) . ')'; 
 69:     
 70:     
 71:     
 72:     $areaContainer = $area_tree[$perm->showareas($area)];
 73: 
 74:     
 75:     $oActionColl = new cApiActionCollection();
 76:     $oActionColl->select('idarea IN (' . implode(',', $areaContainer) . ')');
 77:     while (($oItem = $oActionColl->next()) !== false) {
 78:         $whereAreaActions[] = '(idarea = ' . (int) $oItem->get('idarea') . ' AND idaction = ' . (int) $oItem->get('idaction') . ')';
 79:     }
 80:     $whereAreaActions = '(' . implode(' OR ', $whereAreaActions) . ')'; 
 81:     
 82:     
 83:     
 84:     
 85:     
 86:     
 87:     $sWhere = "{$whereAreaActions} AND {$whereUsers} AND idcat = {$iditem}";
 88:     if ($idlang) {
 89:         $sWhere .= ' AND idlang=' . (int) $idlang;
 90:     }
 91: 
 92:     $oSourceRighsColl->select($sWhere);
 93:     while (($oItem = $oSourceRighsColl->next()) !== false) {
 94:         $rs = $oItem->toObject();
 95:         $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $newiditem, $rs->idclient, $rs->idlang, $rs->type);
 96:     }
 97: 
 98:     
 99:     $perm->load_permissions(true);
100: 
101:     return true;
102: }
103: 
104: 105: 106: 107: 108: 109: 110: 111: 112: 
113: function createRightsForElement($area, $iditem, $idlang = false) {
114:     global $perm, $auth, $area_tree, $client;
115: 
116:     if (!is_object($perm)) {
117:         return false;
118:     }
119:     if (!is_object($auth)) {
120:         return false;
121:     }
122: 
123:     $oDestRightCol = new cApiRightCollection();
124:     $oSourceRighsColl = new cApiRightCollection();
125:     $whereUsers = array();
126:     $rightsCache = array();
127: 
128:     
129:     $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']); 
130:     
131:     
132:     $userIDContainer[] = $auth->auth['uid']; 
133:     foreach ($userIDContainer as $key) {
134:         $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
135:     }
136:     $whereUsers = '(' . implode(' OR ', $whereUsers) . ')'; 
137:     
138:     
139:     
140:     $areaContainer = $area_tree[$perm->showareas($area)];
141: 
142:     
143:     
144:     $sWhere = 'idclient=' . (int) $client . ' AND idarea IN (' . implode(',', $areaContainer) . ')' . ' AND idcat != 0 AND idaction != 0 AND ' . $whereUsers;
145:     if ($idlang) {
146:         $sWhere .= ' AND idlang=' . (int) $idlang;
147:     }
148: 
149:     $oSourceRighsColl->select($sWhere);
150:     while (($oItem = $oSourceRighsColl->next()) !== false) {
151:         $rs = $oItem->toObject();
152: 
153:         
154:         $key = $rs->user_id . '-' . $rs->idarea . '-' . $rs->idaction . '-' . $iditem . '-' . $rs->idclient . '-' . $rs->idlang . '-' . $rs->type;
155:         if (isset($rightsCache[$key])) {
156:             continue;
157:         }
158: 
159:         
160:         $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $iditem, $rs->idclient, $rs->idlang, $rs->type);
161: 
162:         $rightsCache[$key] = true;
163:     }
164: 
165:     
166:     $perm->load_permissions(true);
167: 
168:     return true;
169: }
170: 
171: 172: 173: 174: 175: 176: 177: 
178: function deleteRightsForElement($area, $iditem, $idlang = false) {
179:     global $perm, $area_tree, $client;
180: 
181:     
182:     $areaContainer = $area_tree[$perm->showareas($area)];
183: 
184:     $sWhere = "idcat=" . (int) $iditem . " AND idclient=" . (int) $client . " AND idarea IN (" . implode(',', $areaContainer) . ")";
185:     if ($idlang) {
186:         $sWhere .= " AND idlang=" . (int) $idlang;
187:     }
188: 
189:     $oRightColl = new cApiRightCollection();
190:     $oRightColl->deleteByWhereClause($sWhere);
191: 
192:     
193:     $perm->load_permissions(true);
194: }
195: 
196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 
207: function buildUserOrGroupPermsFromRequest($bAddUserToClient = false) {
208:     global $cfg, $msysadmin, $madmin, $mclient, $mlang, $auth, $client;
209: 
210:     $aPerms = array();
211: 
212:     
213: 
214:     $bSysadmin = (isset($msysadmin) && $msysadmin);
215: 
216:     $aAdmin = (isset($madmin) && is_array($madmin)) ? $madmin : array();
217:     foreach ($aAdmin as $p => $value) {
218:         if (!is_numeric($value)) {
219:             unset($aAdmin[$p]);
220:         }
221:     }
222: 
223:     $aClient = (isset($mclient) && is_array($mclient)) ? $mclient : array();
224:     foreach ($aClient as $p => $value) {
225:         if (!is_numeric($value)) {
226:             unset($aClient[$p]);
227:         }
228:     }
229: 
230:     $aLang = (isset($mlang) && is_array($mlang)) ? $mlang : array();
231:     foreach ($aLang as $p => $value) {
232:         if (!is_numeric($value)) {
233:             unset($aLang[$p]);
234:         }
235:     }
236: 
237:     
238: 
239:     if ($bSysadmin) {
240:         $aPerms[] = 'sysadmin';
241:     }
242: 
243:     foreach ($aAdmin as $value) {
244:         $aPerms[] = sprintf('admin[%s]', $value);
245:     }
246: 
247:     foreach ($aClient as $value) {
248:         $aPerms[] = sprintf('client[%s]', $value);
249:     }
250: 
251:     if (count($aClient) == 0 && $bAddUserToClient) {
252:         
253:         
254:         
255:         
256:         $aUserPerm = explode(',', $auth->auth['perm']);
257:         if (!in_array('sysadmin', $aUserPerm)) {
258:             $aPerms[] = sprintf('client[%s]', $client);
259:         }
260:     }
261: 
262:     if (count($aLang) > 0 && count($aClient) > 0) {
263:         
264:         
265:         $db = cRegistry::getDb();
266:         foreach ($aLang as $value) {
267:             if (checkLangInClients($aClient, $value, $cfg, $db)) {
268:                 $aPerms[] = sprintf('lang[%s]', $value);
269:             }
270:         }
271:     }
272: 
273:     return $aPerms;
274: }
275: 
276: function saveRights() {
277:     global $perm, $notification, $db, $userid;
278:     global $rights_list, $rights_list_old, $rights_client, $rights_lang;
279: 
280:     
281:     if (!is_array($rights_list)) {
282:         $rights_list = array();
283:     }
284: 
285:     
286:     $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
287: 
288:     
289:     $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
290: 
291:     if (is_array($arraydel)) {
292:         foreach ($arraydel as $value) {
293:             $data = explode('|', $value);
294:             $data[0] = $perm->getIDForArea($data[0]);
295:             $data[1] = $perm->getIDForAction($data[1]);
296: 
297:             $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";
298:             $oRightColl = new cApiRightCollection();
299:             $oRightColl->deleteByWhereClause($where);
300:         }
301:     }
302: 
303:     unset($data);
304: 
305:     
306:     if (is_array($arraysave)) {
307:         foreach ($arraysave as $value) {
308:             
309:             $data = explode('|', $value);
310: 
311:             
312:             
313:             
314:             $data[0] = $perm->getIDForArea($data[0]);
315:             $data[1] = $perm->getIDForAction($data[1]);
316: 
317:             if (!isset($data[1])) {
318:                 $data[1] = 0;
319:             }
320: 
321:             
322:             $oRightColl = new cApiRightCollection();
323:             $oRightColl->create($userid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 0);
324:         }
325:     }
326: 
327:     $rights_list_old = $rights_list;
328: 
329:     return true;
330: 
331: }
332: 
333: function saveGroupRights() {
334:     global $perm, $notification, $db, $groupid;
335:     global $rights_list, $rights_list_old, $rights_client, $rights_lang;
336: 
337:     
338:     if (!is_array($rights_list)) {
339:         $rights_list = array();
340:     }
341: 
342:     
343:     $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
344: 
345:     
346:     $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
347: 
348:     if (is_array($arraydel)) {
349:         foreach ($arraydel as $value) {
350:             $data = explode('|', $value);
351:             $data[0] = $perm->getIDForArea($data[0]);
352:             $data[1] = $perm->getIDForAction($data[1]);
353: 
354:             $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";
355:             $oRightColl = new cApiRightCollection();
356:             $oRightColl->deleteByWhereClause($where);
357:         }
358:     }
359: 
360:     unset($data);
361: 
362:     
363:     if (is_array($arraysave)) {
364:         foreach ($arraysave as $value) {
365:             
366:             $data = explode('|', $value);
367: 
368:             
369:             
370:             
371:             $data[0] = $perm->getIDForArea($data[0]);
372:             $data[1] = $perm->getIDForAction($data[1]);
373: 
374:             if (!isset($data[1])) {
375:                 $data[1] = 0;
376:             }
377: 
378:             
379:             $oRightColl = new cApiRightCollection();
380:             $oRightColl->create($groupid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 1);
381:         }
382:     }
383: 
384:     $rights_list_old = $rights_list;
385:     return true;
386: }
387: