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